Closed colleenjg closed 1 year ago
This might be overkill. I could also just read my dictionary keys more carefully... 😂
No I really like this! I always manage to screw up handing in params (it is very much the downside of using dictionaries) this would be a really nice catch-all , let me take a closer look tomorrow :)))
thanks!
As discussed offline: let's shift this to a system where the default_params
dictionary lives just above the __init__()
then some utils
function performs a cascading check which concatenates all default_params
for the object, its parents, its parent's parents...up until parent.default_param
isn't an attribute and checks this against the passed params
dict for inconsistencies. It raises a warning
The hidden benefit of this is that users can also check what default_params
a class accepts without initialising a class (just typing GridCells.default_params
should do the trick).
The other benefit is this won't make a load of dummy classes
Hey @TomGeorge1234 , I was just working with
ratinabox
and decided to implement a small functionality for myself. I wanted to mention it here in case you'd like to add it / anyone else could use it.So, as we know, the downside to allowing users to specify a wide range of parameters using a
params
dictionary when initializing a new object (Agent, Environment, Neurons) is that incorrectly typed out parameter names can slip through easily. For example, you might be initializing aGridCells
object, accidentally using the keyfr_max
instead ofmax_fr
in your parameters dictionary, and not notice for a while that you failed to setmax_fr
as intended.So, I wrote a function that checks whether objects have unexpected attributes, and raises a warning if they do.
If called in the init of a class, as follows, the function will flag with a warning any keys in
params
that would not be attributes of a default object of that class. Specifically, usingGridCells
as an example:If you then call
GridCells(Agent, params={"fr_max": 2})
you get the following warning:UserWarning: Found 1 unexpected attribute(s) for GridCells: fr_max
So, this would be useful for any classes where (1) all parameters received are set as attributes, and (2) all acceptable parameters received have default values in the
default_params
dictionaries at some level of initialization. I believe most if not all of the classes that take aparams
dict as an input inratinabox
meet these two criteria.Still, because this is a bit... non-pythonic... I should mention a few potential unintended consequences I can currently foresee 1) an infinite loop, if calling this from the
__init__()
, which is avoided as long as the objects this is used with have acheck_attributes
keyword argument in their__init__()
, 2) spurious printing when the dummy object is created (the function suppresseswarnings.warn
calls, but can't suppress print calls, of course and doesn't currently suppress log calls), 3) this seems unlikely, but I'll mention it in case: a spurious reference to the dummy object could be created while runningcheck_attributes()
if, for example, when an Object, likeGridCells
is initialized, circular references are created (i.e., not only does theGridCells
object have a pointer to itsAgent
(self.Agent
), but theAgent
then also has a pointer added in return to any associated Neurons (e.g.,self.Agent.Neurons.append(self)
). As far as I can tell, this kind of circular referencing was not implemented inratinabox
, so I don't believe that my dummy object will create undue clutter in any associated objects.This got a bit complicated..., but anyway, do let me know if you think it would be useful!