[AstroPy] Remove a model from a composite model in astropy.modeling

Eric Emsellem eric.emsellem at eso.org
Sun Aug 23 06:48:36 EDT 2015

> right now the easiest way to do what you're asking would just be to take two
> slices that exclude model2 and combine them.  For example with:
>   >>> modelC = model1 + model2 + model3
> model2 has the index 1 (you can check this via the output of print(modelC)).
> Then you can do:
>   >>> modelC = modelC[:1] + modelC[2:]
> This is even easier if you give names to the individual models so that you don't
> have to worry about their numeric index.  Although in this case it's a little
> awkward because when you use names in slices the end points are*inclusive*  so
> you have to know which models come before and after 'B'
>   >>> model = model1.rename('A') + model2.rename('B') + model3.rename('C')
>   >>> model = model[:'A'] + model['C':]
> I could see a case for a .remove() method, as well as .insert(), etc.  But it
> would have to take some care with how it handles the operators.  If you're just
> talking a linear sum it's straightforward enough.  But what if you have:
>   >>> model = a + b * c
> If you do model.remove('b') should it become  a + c or a * c?  I think in that
> case probably a + c makes sense due to the order of operations.  But what about
>   >>> model = a + b ** c?
> This is less likely to show up, but is possible.  Neither a + c or a ** c make
> much sense as a result.
> So again I think this would be a good thing to have but there are subtleties
> that would have to be worked out.
> Erik

Thanks so much Erik. This makes a lot of sense although I had NOT 
realised you could just slice a composite model. I was a bit worried 
that adding was easy but "removing" was not due to the number of 
parameters you had to "clean".

I'll try the solutions you indicated.

As for a "remove" function: an idea would be to act on "blocks" instead 
and not on models directly. This would require some thinking indeed. In 
your example b**c would be a block, c would be a model. Only the second 
one could have a "remove" function. This would mean a hierarchy. Not 
sure you wish to pursue that path, which is quite a complex one and 
maybe not worth it.

In any cases, I am developing something which only uses additions, so I 
won't have that problem.

THANKS again!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/astropy/attachments/20150823/6b6d101f/attachment.html>

More information about the AstroPy mailing list