How do I best handle a state that can change in many subtle ways? #67
Replies: 3 comments 2 replies
-
|
I suppose one solution to part 2 is that I treat the "master" model as an initial guess and make that very explicit in naming it and examples. Then I can also introduce explicit methods (with examples) to change the parameters of the generated I could of course have two internal lists, one for the diffusion models and one for other models. Hmm.. |
Beta Was this translation helpful? Give feedback.
-
|
Why not use the Parameter's callback mechanism to silently update the state? This is what it has been created for. |
Beta Was this translation helpful? Give feedback.
-
|
Something maybe like class SampleModel(ModelBase):
def __init__(self, components, Q, ...):
......
# Cache tracking
self._dirty = True
self._attach_callbacks_to_master_parameters()
def _attach_callbacks_to_master_parameters(self) -> None:
for param in self._components.get_all_variables():
if isinstance(param, Parameter):
callback = property(
fset=lambda value, self=self: self._on_parameter_changed(value),
)
param._callback = callback
def _on_parameter_changed(self, new_value) -> None:
"""Called when any master parameter value changes."""
self._dirty = Trueand then regenerate the collection inside the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
In my
SampleModelI will generate a list ofComponentCollection(self._component_collections, generated by_generate_component_collections(self)); one for eachQthat the user gives. The list is generated by making copies from a "master"ComponentCollection(self._components) and a list ofDiffusionModels (which itself generates a list ofComponentCollection).How do I make sure that
self._component_collectionsstays up to date?The simplest solution is of course to call
self._generate_component_collections()any time someone asks for the list ofComponentCollections, but that seems very wasteful (I'll generate tens to hundreds of new uniqueParameters every time). It's especially bad if I'm fitting something, since each iteration would generate new collections.It's perhaps straightforward enough to check if users update
self._Q,self._componentsandself._diffusion_modelsand updateself._component_collectionsaccordingly. Question 1: What is the best way to actually do this?There's a deeper problem as well.
Question 2: What do I do if a user makes changes to e.g. the underlying
ComponentCollection? E.g.When creating the
SampleModelI generate the components corresponding to the inputQand models usingself._generate_component_collections(). theevaluatemethod then uses the generatedComponentCollection's evaluate at the inputx. Therefore,y1will be a list of length 2, each element will be 101 values corresponding to theGaussianevaluated atQ=1.0andQ=2.0, respectively. This Gaussian will have area 1.0.Ideally, I would want
y2to be a similar list, but with a Gaussian with area 2, since I updated the value. But how do I make that happen without calling_generate_component_collectionsinside theevaluatemethod? I can implement checks that the user changes the components using the methods inSampleModel, but I'm not sure if/how to check if the user changes e.g. aParameterin collection.I hope I made this somewhat clear.
Beta Was this translation helpful? Give feedback.
All reactions