Пример #1
0
    def uncorrectUncertainty(self,correction=None):
        """
        Undoes correction on measurement uncertainty.

        correction:   Information on how to perform the correction.
                      May be a path to a pickled file, a float, or 
                      list of values.
        """
        if isinstance(correction,(str)):
            correction = acs.pklread(correction)
        if isinstance(correction,(float,int)):
            self.spectra_errs /= np.sqrt(correction)
        elif isinstance(correction,(list)):
            correction = np.array(correction)
        if isinstance(correction,(np.ndarray)):
            if correction.shape != self.spectra_errs.shape:
                correction = np.tile(correction,(self.spectra_errs.shape[0],1))
            self.spectra_errs = np.sqrt(self.spectra_errs**2/correction)
Пример #2
0
def comp_R2(ms,direc=None,savename=None,
            labels = ['raw sample,','corrected \nsample,','M.A.D.,']):
    """
    Plot comparison of R^2 arrays for different models.

    ms:       Either list of model objects or list of strings where
              models are stored.
    direc:    Directory to find model files if ms a list of strings
    labels:   Label corresponding to both models
    """
        
    # Start figure
    plt.figure(figsize = (10,8))
    # Find equally spaced colours from the plasma colourmap
    colors = plt.get_cmap('plasma')(np.linspace(0, 0.8, len(ms)))
    # Choose linestypes and markertypes
    types = ['o','s','^']
    start = 0
    vecs = np.array([],dtype=int)
    # For each model
    t=0
    for i in range(len(ms)):
        if t >= len(types):
            t = 0
        # Construct latex label
        r2noise_label = '\n$\mathbf{R^2_{noise}}$ = '
        # Find model - if ms[i] a string, read from file, otherwise use entry
        if isinstance(ms[i],str):
            m = acs.pklread(direc+'/'+ms[i])
        else:
            m = ms[i]
        
        # Set consistent plot boundaries
        plt.ylim(0,1)
        plt.xlim(-1,len(m.R2Array))
        
        # Find where R^2 values cross the R^2_noise line
        crossvec = np.where(m.R2Array > m.R2noise)
        if crossvec[0] != []:
            crossvec = crossvec[0][0]-1
            if crossvec < 0:
                crossvec=0
            plt.axvline(crossvec,0,m.R2Array[crossvec],color=colors[i],
                        linestyle='-',lw=2)
            vecs = np.append(vecs,crossvec)
        nummarker = np.min([len(m.R2Array),10])
        if nummarker < len(m.R2Array):
            vec_points = np.arange(len(m.R2Array))[start::len(m.R2Array)/10]
            R2_points = m.R2Array[start::len(m.R2Array)/10]
            start += len(m.R2Array)/(10*len(ms))
        else:
            vec_points = np.arange(len(m.R2Array))
            R2_points = m.R2Array
        # Plot R^2 as a function of number of eigenvectors
        plt.plot(m.R2Array,color=colors[i],ls='-',lw=3)
        plt.plot(vec_points,R2_points,ls='',ms=14,marker=types[t],markeredgecolor='k',
                 markeredgewidth=2,markerfacecolor=colors[i],
                 label = r'{0} {1} {2:.2f}'.format(labels[i],r2noise_label,
                                                   m.R2noise))
        plt.axhline(m.R2noise,color=colors[i],ls='--',lw=3)
        t+=1
    # Label axes and add the legend
    step=10**np.floor(np.log10(len(m.R2Array)))
    #ticklist = np.concatenate((np.arange(0,len(m.R2Array),step,dtype=int),vecs))
    #plt.xticks(ticklist,ticklist.astype(str))
    for vec in vecs:
        plt.text(vec+0.01*len(m.R2Array),0.02,'{0}'.format(vec))
    plt.ylabel(r'$\mathbf{R}^2$',fontsize=font['size']+2)
    plt.xlabel('n',fontsize=font['size']+2)
    legend = plt.legend(loc='best',fontsize=font['size']-2)
    legend.get_frame().set_linewidth(0.0)
    if not savename:
        savename = '{0}/R2_comp.png'.format(direc)
    plt.savefig(savename)