You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add a method to get a number of models in CompoundModel class #4093
Currently, len() does not give the number of models in a CompoundModel object. You can get around by using numpy.size(compound_model), but it would be nice to have an object method that does just that.
Yeah. The problem is that len(model) gives the number of models in a model set. It doesn't really apply to a compound model which, once created, should be no different from any other model except in the detail of how it was implemented.
This also speaks to the more general need for better introspection tools on compound models, but I don't know quite what it would look like.
What actually are you trying to do that requires knowing the number of submodels? Maybe there's another way.
@embray There is definitely a work around. I was trying to fit a compound model of multiple Gaussian1D models to a complex PSF, and I want my function to return a tuple of ((amp1, mean1, std1), (amp2, mean2, std2), ...) as a single tuple. Since I know I am fitting multiple Gaussian1D, I can reshape model.parameters to (n_models, 3) shape, determining n_models from model.parameters.size before turning the parameter array input a nested tuple. I could require the function to have an n_models or an init_params input parameters and check the number of model from that. But this might not be the case if you want to fit a more complex compound models.
Activity
embray commentedon Aug 25, 2015
Yeah. The problem is that
len(model)gives the number of models in a model set. It doesn't really apply to a compound model which, once created, should be no different from any other model except in the detail of how it was implemented.This also speaks to the more general need for better introspection tools on compound models, but I don't know quite what it would look like.
What actually are you trying to do that requires knowing the number of submodels? Maybe there's another way.
embray commentedon Aug 25, 2015
Incidentally, I did just remember that there is a public member on compound models called
submodel_names. So can always dolen(model.submodel_names).piyanatk commentedon Aug 25, 2015
@embray There is definitely a work around. I was trying to fit a compound model of multiple Gaussian1D models to a complex PSF, and I want my function to return a tuple of
((amp1, mean1, std1), (amp2, mean2, std2), ...)as a single tuple. Since I know I am fitting multiple Gaussian1D, I can reshapemodel.parametersto(n_models, 3)shape, determiningn_modelsfrommodel.parameters.sizebefore turning the parameter array input a nested tuple. I could require the function to have ann_modelsor aninit_paramsinput parameters and check the number of model from that. But this might not be the case if you want to fit a more complex compound models.len(model.submodel_names)works perfectly.embray commentedon Aug 26, 2015
Ah, I see now. I hope to improve the interfaces for getting at parameters, but for large models it is tricky.
Another thing you can do, you know, is address each submodel individually, like:
or something to that effect anyways.
piyanatk commentedon Aug 26, 2015
That looks like a very pytonic solution. Thanks Erik.
pllim commentedon Jan 26, 2017
len(model.submodel_names)does work, so this is not an issue anymore?mirca commentedon Jan 26, 2017
+1 to having
get_num_models().mirca commentedon Jan 26, 2017
@pllim @embray @piyanatk let me know if you think having a method for this is useful, I can submit a fix.
pllim commentedon Jan 27, 2017
The name
get_num_modelscan be potentially confusing. Each model has_n_modelsattribute that means something completely different. Also, when_n_models > 1, shouldget_num_modelstake account of that? Perhaps just adding clearer documentation aboutlen(model.submodel_names)in http://docs.astropy.org/en/latest/modeling/compound-models.html would be sufficient?nden commentedon Jan 27, 2017
How about a property
n_submodelswhich returnslen(model.submodel_names)?pllim commentedon Jan 27, 2017
n_submodelswould be more concise, so 👍 if you want to do that.