Skip to content

Add a method to get a number of models in CompoundModel class #4093

Closed

Description

@piyanatk

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.

Activity

  1. embray commented on Aug 25, 2015

    @embray
    Member

    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.

  2. embray commented on Aug 25, 2015

    @embray
    Member

    Incidentally, I did just remember that there is a public member on compound models called submodel_names. So can always do len(model.submodel_names).

  3. piyanatk commented on Aug 25, 2015

    @piyanatk
    Author

    @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.

    len(model.submodel_names) works perfectly.

  4. embray commented on Aug 26, 2015

    @embray
    Member

    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:

    all_params = [(model[m].amplitude, model[m].mean, model[m].stddev)
                    for m in model.submodel_names]

    or something to that effect anyways.

  5. piyanatk commented on Aug 26, 2015

    @piyanatk
    Author

    That looks like a very pytonic solution. Thanks Erik.

  6. self-assigned this
    on Aug 27, 2015
  7. added
    Close?Tell stale bot that this issue/PR is stale
    on Jan 26, 2017
  8. pllim commented on Jan 26, 2017

    @pllim
    Member

    len(model.submodel_names) does work, so this is not an issue anymore?

  9. mirca commented on Jan 26, 2017

    @mirca
    Member

    +1 to having get_num_models().

  10. mirca commented on Jan 26, 2017

    @mirca
    Member

    @pllim @embray @piyanatk let me know if you think having a method for this is useful, I can submit a fix.

  11. pllim commented on Jan 27, 2017

    @pllim
    Member

    The name get_num_models can be potentially confusing. Each model has _n_models attribute that means something completely different. Also, when _n_models > 1, should get_num_models take account of that? Perhaps just adding clearer documentation about len(model.submodel_names) in http://docs.astropy.org/en/latest/modeling/compound-models.html would be sufficient?

  12. nden commented on Jan 27, 2017

    @nden
    Contributor

    How about a property n_submodels which returns len(model.submodel_names)?

  13. pllim commented on Jan 27, 2017

    @pllim
    Member

    n_submodels would be more concise, so 👍 if you want to do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions