Пример #1
0
 def plot_model_components(self, legend = True):
     '''Plot contribution of individual model components
     
     If matplotlib can be imported, it is used directly, otherwise
     if falls back to Sherpa plotting (e.g. with chips).
     
     Returns
     -------
     fig : matplotlib figure instance
     '''
     fig = plt.figure()
     if has_mpl:
         ax = fig.add_subplot(111)
         pl = get_data_plot(self.id)
         ax.errorbar(pl.x, pl.y, yerr=pl.yerr, fmt='+', label = 'data')
         for part in smh.get_model_parts():
             pl = get_model_component_plot(self.id, model = part)
             ax.plot(pl.x, pl.y, label = part)
         if legend:
             ax.legend(ncol = 2)
         ax.set_title('Individual model components')
     else:
         plot_fit(self.id)
         for part in smh.get_model_parts():
             plot_model_component(self.id, part, overplot = True)
     return fig
Пример #2
0
 def line_name_list(self):
     '''get list of all model components that are lines
     '''
     parts_list = smh.get_model_parts(self.id)
     return list([part for part in parts_list if part.startswith(self.linemodname)])
Пример #3
0
def mainloop(mymodel, fwhm, id = None, maxiter = 5, mindist = 0., do_plots = 0):
    
    if id is None:
        id = ui.get_default_id()
    data = ui.get_data(id)
    wave = data.get_indep()[0]
    error = data.get_error()[0]
    
    # model could habe been initalized with arbitrary values
    ui.fit(id) 

    for i in range(maxiter):
        oldmodel = smh.get_model_parts(id)
        res_flux = ui.get_resid_plot(id).y
        if smoothwindow is not None:
            fwhminpix = int(fwhm / np.diff(wave).mean())
            y = smooth(res_flux/error, window_len = 3*fwhminpix, window = smoothwindow)
        else:
            y = res_flux/error
        peaks = findlines(wave, y, fwhm, smoothwindow = None, sigma_threshold = sigma_threshold)
        if has_mpl and (do_plots > 2):
            plt.figure()
            plt.plot(wave, res_flux/error, 's')
            for pos in mymodel.line_value_list('pos'):
                plt.plot([pos, pos], plt.ylim(),'k:')
            for peak in peaks:
                plt.plot([wave[peak], wave[peak]], plt.ylim())
            plt.plot(wave, y)
            plt.draw()
            
        for peak in peaks:
            if (len(mymodel.line_value_list('pos')) == 0) or (min(np.abs(mymodel.line_value_list('pos') - wave[peak])) >= mindist):
                mymodel.add_line(**mymodel.guess(wave, smooth(res_flux, window_len = 3*fwhminpix, window = smoothwindow), peak, fwhm = fwhm))
        newmodel = smh.get_model_parts(id)
        print 'Iteration {0:3n}: {1:3n} lines added'.format(i, len(newmodel) - len(oldmodel))
        
        if set(newmodel) == set(oldmodel):
            print 'No new lines added this step - fitting finished'
            break
        # Now do the fitting in Sherpa
        #ui.set_method('simplex')
        ui.fit(id)
        #ui.set_method('moncar')
        #ui.fit(id)
        
        if has_mpl and (do_plots > 0):
            if do_plots > 1:
                plt.figure()
            else:
                plt.clf()
            ui.plot_fit(id)
            for pos in mymodel.line_value_list('pos'):
                plt.plot([pos, pos], plt.ylim(),'k:')
            for peak in peaks:
                plt.plot([wave[peak], wave[peak]], plt.ylim())
            plt.plot(wave, res_flux)
            plt.draw()
        

    else:
        print 'Max number of iterations reached'
    #model.cleanup() #remove lines running to 0 etc.
    return mymodel