[AstroPy] Composite models or input arrays

Erik Bray embray at stsci.edu
Wed Aug 26 11:48:52 EDT 2015

On 08/26/2015 03:19 AM, Eric Emsellem wrote:
> => However, I don't know how to slice the model set then (e.g., compute
> ModelT(x) just using e.g., the 1st and 3rd model of that set). By the way, with
> a compound model, I guess I cannot do ModelT[[0,2]] to access only model 1 and 3.

Currently not possible--you'd have to slice the parameter arrays themselves and 
just create a new model instance with the sliced parameters.

You've hit on exactly what I *want* to be able to do though.  Just as the 
Astropy Time object [1] can represent an array of times (of the same format and 
scale) and SkyCoord can represent an array of coordinates in the same frame, a 
Model object (with n_models > 1) can be thought of an array of models of the 
same type just with different parameters.

This feature just wasn't conceived of in exact analogy to those (though in 
retrospect it's a good way to think of it), so it isn't quite possible yet to 
fully manipulate a model set like an array of models.  But I would very much 
*like* to have that working sooner or later.

The main trickiness is that indexing of model objects is already used by 
compound models (and only by compound models) to index submodels / subexpressions.

What I might do is just have multiple indices--so that when using a model set 
the first index indexes models in the set, and the second index (if it's a 
compound model) indexes submodels.  For single "scalar(?)" models, the first 
index would always just index submodels of compound models.  That's what I'm 
thinking anyways, but I haven't looked into what the impact is on existing code.

> => More generally, and as mentioned in a previous email, I would also like to be
> able to add more instances of MyModel, or remove some of them.
> With Option 1, I can just do ModelT = ModelT + MyModel(par1=, par2=, par3=) to
> add one
> or
> ModelT = ModelT[0] + ModelT[2] to remove one (as you suggested)
> Could you do that easily with a model set? (1- compute the output on a slice of
> the models, a selected subset, and later on add or even remove completely one of
> the models in the model set?).

Ah, I see.  That would be a good use case.  If I can get array-like operations 
working better on model sets then you'd be able to slice/stack them.  You should 
open a ticket for this on GitHub if you think of it.

> Anyways, this is quite amazing. I was a bit dubious when I started to examine
> the possibility to use the astropy.modeling. I am more than convinced now and
> this is going well beyond what I had imagined (and I haven't seen everything yet
> by far). Thanks so much for these developments!

Thanks!  I hope it will be useful to you.


[1] http://docs.astropy.org/en/stable/time/index.html#getting-started
[2] http://docs.astropy.org/en/stable/coordinates/index.html#getting-started

More information about the AstroPy mailing list