def __init__(self, name=None, model=None): self.manager = SpectralModelManager(model) _displayGUI(self, name) # this just propagates up the signals emitted by # the SpectralModelManager instance just created. self.changed = SignalModelChanged() self.manager.changed.connect(self._broadcastModelChange)
class ModelManager(object): """ Instances of this class hold a composite spectral model. A ModelManager instance contains a user-definable list of spectral components from astropy.modeling.models. From that list, a compound model (astropy 1.0) is built and used to compute flux values, given spectral coordinate values. The list of spectral components in any particular instance of ModelManager is displayed on screen, in a tabbed pane, and can be interacted with so that individual parameter values can be examined or set by the user. This class is basically a wrap-around of SpectralModelManager, to make it available to interactive users with a Python command prompt. Programmatic use should resort to instances of SpectralModelManager directly. Changes in the components or the structure of the model manager trigger signals of type SignalModelChanged. These signals can be caught with code that looks like this: managerInstance.changed.connect(handleSignal.....) Parameters ---------- name: str, optional The name string that goes in the tab associated with the ModelManager instance. If no name is provided, a name will be picked from the next unused string in the sequence '1', '2', '3', .... model: list, optional List with instances of spectral components from astropy.modeling.models. If not provided, the instance will be initialized with an empty list of components. Example: ------- How to create an instance: mm1 = ModelManager() mm2 = ModelManager('test1') mm3 = ModelManager(model=[Gaussian1D(1.,1.,1.)]) mm4 = ModelManager(model=[Gaussian1D(1.,1.,1.), Lorentz1D(1.,1.,1.)]) mm5 = ModelManager("test2", [Gaussian1D(1.,1.,1.), Lorentz1D(1.,1.,1.)]) How to catch a signal: >>> import sp_model_manager as mm >>> def f(): ... print 'Hello!' ... >>> a = mm.ModelManager() >>> a.changed.connect(f) >>> # do some interaction with the GUI, changing >>> Hello! # spectral component parameters or the model Hello! # structure. Hello! Hello! Hello! >>> """ def __init__(self, name=None, model=None): self.manager = SpectralModelManager(model) _displayGUI(self, name) # this just propagates up the signals emitted by # the SpectralModelManager instance just created. self.changed = SignalModelChanged() self.manager.changed.connect(self._broadcastModelChange) def _broadcastModelChange(self): self.changed.emit() # Use delegation to decouple the ModelManager API from # the GUI model manager API. def add(self, component): ''' Adds a new spectral component to the manager. This might be easier to do using the GUI itself, after all, that is the purpose of this class in the first place. Parameters ---------- component: astropy.modeling.Fittable1DModel The component to be added to the manager. ''' self.manager.addComponent(component) @property def selected(self): ''' Gets the currently selected component in the GUI. Returns ------- The currently selected component in the GUI, or None ''' return self.manager.selectedComponent() @property def components(self): ''' Gets a list with all components in the manager. Returns ------- list with all components in the manager. ''' return self.manager.components def setArrays(self, x, y): ''' Defines the region in spectral coordinate vs. flux 'space' to which the components in the model should refer to. For now, this region is being defined by the data arrays associated with the observational data at hand. The region could conceivably be defined by any other means, as long as the functional components can then use the region data to initialize their parameters with sensible values. This region is used by code in module sp_adjust. If no X and/or Y arrays are provided via this method, spectral components added to the compound model will be initialized to a default set of parameter values. Parameters ---------- x: numpy array Array with spectral coordinates y: numpy array Array with flux values ''' self.manager.setArrays(x, y) def spectrum(self, wave): ''' Computes the compound model for a given array of spectral coordinate values. Parameters ---------- wave: numpy array Array with spectral coordinate values. Returns ------- A numpy array with flux values. If no components exist in the model, a zero-valued array is returned instead. ''' return self.manager.spectrum(wave)