Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overload assignment operator for replacing model components with other functions #298

Closed
JamesPHoughton opened this issue Oct 22, 2021 · 4 comments
Milestone

Comments

@JamesPHoughton
Copy link
Collaborator

Context: in 2.0, will have a dependencies dictionary that lets you execute/cache model components more intelligently. In order for this to work while still allowing users to assign new functions in place of old model components, we need to overload the assignment operator.

@enekomartinmartinez - this looks like a promising example: https://stackoverflow.com/a/11024909/6361632

@JamesPHoughton JamesPHoughton added this to the 2.0.0 milestone Oct 22, 2021
@enekomartinmartinez
Copy link
Collaborator

enekomartinmartinez commented Oct 25, 2021

Hi,
I have been researching how to proceed with that and I found out some troubles.

  1. __setattr__ cannot be defined in the Macro or Model class, as we are interested in changing the attributes of model.component. We need to overwrite model.component.__setattr__.
  2. As __setattr__ is a magic method, these methods are called from the class instance and not the object instance. This means that model.component.__setattr__ = new_method will not work. We need to modify the class definition.
  3. The class that is used is the one given by importlib, which creates a 'module' type object.
  4. Therefore we need to rewrite the definition of 'module' built-in class' magic method.

This makes the redefinition of __setattr__ quite complicated and I do not know how to proceed safely.

@enekomartinmartinez
Copy link
Collaborator

enekomartinmartinez commented Oct 25, 2021

Hi @JamesPHoughton
I found a workaround defining a new class called Components in the components.py file together with Time. You can check it in the new branch new_components. I was able to redefine how the components are accessed and modified. However, the complexity of that object has increased and may increase the time needed for integration.

Please, let me know what do you think about these changes. The class now is indirectly tested by the already existing tests, if you think is a good approach, I will add unit tests for each class method.

@JamesPHoughton
Copy link
Collaborator Author

I think the components class is fine. Its hard to know what the performance hit would be, but it shouldn't be too bad, right? Do we have a way to test that? One argument for going away from this would be if we end up using a JIT compiler or something similar that can't navigate through the class. I don't know if that's going to happen any time soon, though.

I don't see anything wrong with this, if the performance hit is small.

@enekomartinmartinez
Copy link
Collaborator

Hi @JamesPHoughton
After running the model with and without component class I have not seen significant differences in the performance with both small and big models. Therefore, I have opened a PR (#299) with the content in new_components branch.
Let's keep this discussion there if necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants