Beispiel #1
0
 def store_models(self, specs, ivar):
     self.models = n.zeros( (specs.shape) )
     for i in xrange(self.models.shape[0]):
         minloc = n.unravel_index( self.zchi2arr[i].argmin(),
                                  self.zchi2arr[i].shape )
         pmat = n.zeros( (specs.shape[-1],self.npoly+1) )
         this_temp = self.templates[minloc[:-1]]
         pmat[:,0] = this_temp[(minloc[-1]*self.npixstep) +
                               self.pixoffset:(minloc[-1]*self.npixstep) +
                               self.pixoffset+specs.shape[-1]]
         polyarr = poly_array(self.npoly, specs.shape[-1])
         pmat[:,1:] = n.transpose(polyarr)
         ninv = n.diag(ivar[i])
         try: # Some eBOSS spectra have ivar[i] = 0 for all i
             '''
             f = n.linalg.solve( n.dot(n.dot(n.transpose(pmat),ninv),pmat),
                             n.dot( n.dot(n.transpose(pmat),ninv),specs[i]) )
             '''
             f = nnls( n.dot(n.dot(n.transpose(pmat),ninv),pmat),
                      n.dot( n.dot(n.transpose(pmat),ninv),specs[i]) );\
                             f = n.array(f)[0]
             self.models[i] = n.dot(pmat, f)
         except Exception as e:
             self.models[i] = n.zeros(specs.shape[-1])
             print "Exception: %r" % r
Beispiel #2
0
 def create_model(self, fname, npoly, npixstep, minvector, zfindobj, flux,
                  ivar):
     """Return the best fit model for a given template at a
         given redshift.
     """
     try:
         pixoffset = zfindobj.pixoffset
         temps = read_ndArch(
             join(environ['REDMONSTER_TEMPLATES_DIR'], fname))[0]
         pmat = n.zeros((self.npixflux, npoly + 1))
         this_temp = temps[minvector[:-1]]
         pmat[:,0] = this_temp[(minvector[-1]*npixstep)+pixoffset:\
                               (minvector[-1]*npixstep)+pixoffset + \
                               self.npixflux]
         polyarr = poly_array(npoly, self.npixflux)
         pmat[:, 1:] = n.transpose(polyarr)
         ninv = n.diag(ivar)
         f = linalg.solve(n.dot(n.dot(n.transpose(pmat), ninv), pmat),
                          n.dot(n.dot(n.transpose(pmat), ninv), flux))             \
                             f = n.array(f)
         if f[0] < 0:
             try:
                 f = nnls(n.dot(n.dot(n.transpose(pmat), ninv), pmat),
                          n.dot(n.dot(n.transpose(pmat), ninv), flux))[0]                     \
                                             f = n.array(f)
                 return n.dot(pmat, f), tuple(f)
             except Exception as e:
                 print("Exception: %r" % e)
                 return n.zeros(self.npixflux), (0, )
         else:
             return n.dot(pmat, f), tuple(f)
     except Exception as e:
         print("Exception: %r" % e)
         return n.zeros(self.npixflux), (0, )
Beispiel #3
0
 def store_models(self, specs, ivar):
     self.models = n.zeros((specs.shape))
     for i in range(self.models.shape[0]):
         minloc = n.unravel_index(self.zchi2arr[i].argmin(),
                                  self.zchi2arr[i].shape)
         pmat = n.zeros((specs.shape[-1], self.npoly + 1))
         this_temp = self.templates[minloc[:-1]]
         pmat[:,
              0] = this_temp[(minloc[-1] * self.npixstep) +
                             self.pixoffset:(minloc[-1] * self.npixstep) +
                             self.pixoffset + specs.shape[-1]]
         polyarr = poly_array(self.npoly, specs.shape[-1])
         pmat[:, 1:] = n.transpose(polyarr)
         ninv = n.diag(ivar[i])
         try:  # Some eBOSS spectra have ivar[i] = 0 for all i
             '''
             f = linalg.solve( n.dot(n.dot(n.transpose(pmat),ninv),pmat),
                             n.dot( n.dot(n.transpose(pmat),ninv),specs[i]) )
             '''
             f = nnls(n.dot(n.dot(n.transpose(pmat), ninv), pmat),
                      n.dot(n.dot(n.transpose(pmat), ninv), specs[i]))                \
                                             f = n.array(f)[0]
             self.models[i] = n.dot(pmat, f)
         except Exception as e:
             self.models[i] = n.zeros(specs.shape[-1])
             print("Exception: %r" % r)
Beispiel #4
0
 def create_model(self, fname, npoly, npixstep, minvector, zfindobj,
                  flux, ivar):
     """Return the best fit model for a given template at a
         given redshift.
     """
     try:
         pixoffset = zfindobj.pixoffset
         temps = read_ndArch( join( environ['REDMONSTER_TEMPLATES_DIR'],
                                   fname ) )[0]
         pmat = n.zeros( (self.npixflux, npoly+1) )
         this_temp = temps[minvector[:-1]]
         pmat[:,0] = this_temp[(minvector[-1]*npixstep)+pixoffset:\
                               (minvector[-1]*npixstep)+pixoffset + \
                               self.npixflux]
         polyarr = poly_array(npoly, self.npixflux)
         pmat[:,1:] = n.transpose(polyarr)
         ninv = n.diag(ivar)
         f = linalg.solve( n.dot(n.dot(n.transpose(pmat),ninv),pmat),
                            n.dot( n.dot(n.transpose(pmat),ninv),flux) ); \
                 f = n.array(f)
         if f[0] < 0:
             try:
                 f = nnls( n.dot(n.dot(n.transpose(pmat),ninv),pmat),
                          n.dot( n.dot(n.transpose(pmat),ninv),flux) )[0]; \
                         f = n.array(f)
                 return n.dot(pmat,f), tuple(f)
             except Exception as e:
                 print("Exception: %r" % e)
                 return n.zeros(self.npixflux), (0,)
         else:
             return n.dot(pmat,f), tuple(f)
     except Exception as e:
         print("Exception: %r" % e)
         return n.zeros(self.npixflux), (0,)
Beispiel #5
0
 def fit_linear_combo(self, loc):
     # Find locations of best and second best z from chi2 surfaces
     chi2vec = self.bestchi2vecs[loc]
     minloc = chi2vec.argmin()
     if (minloc > self.width) & (minloc < (chi2vec.shape[0] - self.width)):
         minlessarg = chi2vec[:minloc - self.width].argmin()
         minmorearg = chi2vec[minloc + self.width:].argmin() + (
             minloc + self.width
         )  # Argmin gives index within the slice; a value of 2 from chi2vec[minloc:].argmin() means minloc+2 was the minimum
         secondminloc = minlessarg if (
             chi2vec[minlessarg] < chi2vec[minmorearg]) else minmorearg
     elif (minloc < self.width):
         secondminloc = chi2vec[minloc + self.width:].argmin() + (minloc +
                                                                  width)
     else:
         secondminloc = chi2vec[:minloc - self.width].argmin()
     # Read in template
     temps = fits.open(
         join(environ['REDMONSTER_DIR'], 'templates',
              eval(self.fname[loc])))[0].data
     flattemps = n.reshape(temps, (-1, temps.shape[-1]))
     fitchi2s = n.zeros((flattemps.shape[0], flattemps.shape[0]))
     data = self.flux[loc]
     ivar = self.ivar[loc]
     sn2_data = n.sum((data**2) * ivar)
     polyarr = poly_array(self.npoly, data.shape[0])
     # Start setting up matrices for fitting
     pmat = n.zeros((data.shape[0], self.npoly + 2))
     pmat[:, 2], pmat[:, 3], pmat[:, 4], pmat[:, 5] = polyarr[0], polyarr[
         1], polyarr[2], polyarr[3]
     ninv = n.diag(ivar)
     for i in range(flattemps.shape[0]):  #changed from flattemps.shape[0]
         print(i)
         #import pdb; pdb.set_trace()
         pmat[:, 0] = flattemps[i, minloc:minloc + data.shape[0]]
         for j in range(
                 flattemps.shape[0]):  #changed from flattemps.shape[0]
             pmat[:,
                  1] = flattemps[j,
                                 secondminloc:secondminloc + data.shape[0]]
             pmattrans = n.transpose(pmat)
             a = n.dot(n.dot(pmattrans, ninv), pmat)
             b = n.dot(n.dot(pmattrans, ninv), data)
             f = n.linalg.solve(
                 a, b
             )  # Maybe this should use nnls instead of solve to preserve physicality?
             fitchi2s[i, j] = sn2_data - n.dot(n.dot(f, a), f)
     return fitchi2s
Beispiel #6
0
 def fit_linear_combo(self, loc):
     # Find locations of best and second best z from chi2 surfaces
     chi2vec = self.bestchi2vecs[loc]
     minloc = chi2vec.argmin()
     if (minloc > self.width) & (minloc < (chi2vec.shape[0]-self.width)):
         minlessarg = chi2vec[:minloc-self.width].argmin()
         minmorearg = chi2vec[minloc+self.width:].argmin() + (minloc+self.width) # Argmin gives index within the slice; a value of 2 from chi2vec[minloc:].argmin() means minloc+2 was the minimum
         secondminloc = minlessarg if (chi2vec[minlessarg] < chi2vec[minmorearg]) else minmorearg
     elif (minloc < self.width):
         secondminloc = chi2vec[minloc+self.width:].argmin() + (minloc+width)
     else:
         secondminloc = chi2vec[:minloc-self.width].argmin()
     # Read in template
     temps = fits.open(join(environ['REDMONSTER_DIR'],'templates',eval(self.fname[loc])))[0].data
     flattemps = n.reshape(temps, (-1,temps.shape[-1]))
     fitchi2s = n.zeros( (flattemps.shape[0],flattemps.shape[0]) )
     data = self.flux[loc]
     ivar = self.ivar[loc]
     sn2_data = n.sum( (data**2)*ivar )
     polyarr = poly_array(self.npoly,data.shape[0])
     # Start setting up matrices for fitting
     pmat = n.zeros( (data.shape[0],self.npoly+2) )
     pmat[:,2], pmat[:,3], pmat[:,4], pmat[:,5] = polyarr[0],polyarr[1],polyarr[2],polyarr[3]
     ninv = n.diag(ivar)
     for i in xrange(flattemps.shape[0]): #changed from flattemps.shape[0]
         print i
         #import pdb; pdb.set_trace()
         pmat[:,0] = flattemps[i,minloc:minloc+data.shape[0]]
         for j in xrange(flattemps.shape[0]): #changed from flattemps.shape[0]
             pmat[:,1] = flattemps[j,secondminloc:secondminloc+data.shape[0]]
             pmattrans = n.transpose(pmat)
             a = n.dot(n.dot(pmattrans,ninv),pmat)
             b = n.dot(n.dot(pmattrans,ninv),data)
             f = n.linalg.solve(a,b) # Maybe this should use nnls instead of solve to preserve physicality?
             fitchi2s[i,j] = sn2_data - n.dot(n.dot(f,a),f)
     return fitchi2s
Beispiel #7
0
    def zchi2(self, specs, specloglam, ivar, npixstep=1, chi2file=False,
              plate=None, mjd=None, fiberid=None):
        self.chi2file = chi2file
        self.npixstep = npixstep
        self.zwarning = n.zeros(specs.shape[0])
        flag_val_unplugged = int('0b10000000',2)
        flag_val_neg_model = int('0b1000',2)
        self.create_z_baseline(specloglam[0])
        if (self.zmin != None) and (self.zmax != None) and \
                (self.zmax > self.zmin):
            
            zminpix, zmaxpix = self.conv_zbounds()
            self.pixoffset = zminpix
            num_z = int(n.floor( (zmaxpix - zminpix) / npixstep ))
            zinds = zminpix + n.arange(num_z)*npixstep
            self.zbase = self.zbase[zinds]
        else:
            zminpix = 0
            # Number of pixels to be fitted in redshift
            num_z = int(n.floor( (zself.origshape[-1] - specs.shape[-1]) /
                                npixstep ))
        
        # Create arrays for use in routine
        # Create chi2 array of shape (# of fibers, template_parameter_1,
        # ..., template_parameter_N, # of redshifts)
        zchi2arr = n.zeros((specs.shape[0], self.templates_flat.shape[0],
                            num_z))
        temp_zwarning = n.zeros(zchi2arr.shape)
        
        # Pad data and SSPs to a power of 2 for faster FFTs
        data_pad = n.zeros(specs.shape[:-1] + (self.fftnaxis1,), dtype=float)
        data_pad[...,:specs.shape[-1]] = specs
        ivar_pad = n.zeros(ivar.shape[:-1] + (self.fftnaxis1,), dtype=float)
        ivar_pad[...,:specs.shape[-1]] = ivar
        
        # Pre-compute FFTs for use in convolutions
        data_fft = n.fft.fft(data_pad * ivar_pad)
        ivar_fft = n.fft.fft(ivar_pad)
        
        if self.npoly>0 :
            # Compute poly terms, noting that they will stay fixed with
            # the data - assumes data is passed in as shape (nfibers, npix)
            polyarr = poly_array(self.npoly, specs.shape[1])
            pmat = n.zeros( (self.npoly+1, self.npoly+1, self.fftnaxis1),
                            dtype=float)
            bvec = n.zeros( (self.npoly+1, self.fftnaxis1), dtype=float)
        
            # Pad to a power of 2 for faster FFTs
            poly_pad = n.zeros((self.npoly, self.fftnaxis1), dtype=float)
            poly_pad[...,:polyarr.shape[-1]] = polyarr
            
            # Pre-compute FFTs for use in convolutions
            poly_fft = n.zeros((ivar_pad.shape[0], self.npoly, self.fftnaxis1),dtype=complex)
            for i in xrange(self.npoly):
                poly_fft[:,i,:] = n.fft.fft(poly_pad[i] * ivar_pad)
            

       
        
        
        # Compute z for all fibers
        
        for i in xrange(specs.shape[0]): # Loop over fibers

            start=time.time()  

            #print 'INFO Fitting fiber %s of %s for template %s' % \
            #        (i+1, specs.shape[0], self.fname)
            
            # If flux is all zeros, flag as unplugged according to BOSS
            # zwarning flags and don't bother with doing fit
            if len(n.where(specs[i] != 0.)[0]) == 0:
                self.zwarning[i] = int(self.zwarning[i]) | flag_val_unplugged
            else: # Otherwise, go ahead and do fit

                self.sn2_data.append (n.sum( (specs[i]**2)*ivar[i] ) )
                
                if self.npoly>0 :
                    for ipos in xrange(self.npoly):
                        bvec[ipos+1] = n.sum( poly_pad[ipos] * data_pad[i] *
                                              ivar_pad[i])
                    for ipos in xrange(self.npoly):
                        for jpos in xrange(self.npoly):
                            pmat[ipos+1,jpos+1] = n.sum( poly_pad[ipos] *
                                                         poly_pad[jpos] *ivar_pad[i]) # CAN GO FASTER HERE (BUT NOT LIMITING = 0.001475
                
                    f_null = n.linalg.solve(pmat[1:,1:,0],bvec[1:,0])
                    self.f_nulls.append( f_null )
                    self.chi2_null.append( self.sn2_data[i] -
                                           n.dot(n.dot(f_null,pmat[1:,1:,0]),f_null))
                else :
                    self.chi2_null.append( self.sn2_data[i] )
                # print 'INFO Chi^2_null value is %s' % self.chi2_null[i]
                # Loop over templates
                # multiprocessing
                
                func_args = []
                for j in xrange(self.templates_flat.shape[0]):
                    if self.npoly>0 :
                        arguments = {"j":j,"poly_fft":poly_fft[i], "t_fft":self.t_fft[j], "t2_fft":self.t2_fft[j], "data_fft":data_fft[i], "ivar_fft":ivar_fft[i], "pmat_pol":pmat, "bvec_pol":bvec, "chi2_0":self.sn2_data[i], "chi2_null":self.chi2_null[i],"num_z":num_z, "npixstep":self.npixstep, "zminpix":zminpix,"flag_val_neg_model":flag_val_neg_model}
                    else :
                        arguments = {"j":j,"t_fft":self.t_fft[j], "t2_fft":self.t2_fft[j], "data_fft":data_fft[i], "ivar_fft":ivar_fft[i], "chi2_0":self.sn2_data[i], "num_z":num_z, "npixstep":self.npixstep, "zminpix":zminpix, "flag_val_neg_model":flag_val_neg_model}
                    func_args.append(arguments)
                    
                
                              
                pool = multiprocessing.Pool(self.nproc)
                if self.npoly>0 :
                    results = pool.map(_zchi2, func_args)
                else :
                    results = pool.map(_zchi2_no_poly, func_args)
                pool.close()
                pool.join()
                
                for result in results :
                    j                  = result[0]
                    zchi2arr[i,j]      = result[1]
                    temp_zwarning[i,j] = result[2]
                
                stop=time.time()
                
                print "INFO fitted fiber %d/%d, chi2_null=%f, %d templates in %s, npoly=%d, using %d procs in %f sec"%(i+1, specs.shape[0],self.chi2_null[i],self.templates_flat.shape[0],self.fname,self.npoly,self.nproc,stop-start)
        
        
        # Use only neg_model flag from best fit model/redshift and add
        # it to self.zwarning
        for i in xrange(self.zwarning.shape[0]):
            minpos = ( n.where(zchi2arr[i] == n.min(zchi2arr[i]))[0][0],
                      n.where(zchi2arr[i] == n.min(zchi2arr[i]))[1][0] )
            self.zwarning[i] = int(self.zwarning[i]) | \
                    int(temp_zwarning[i,minpos[0],minpos[1]])
        zchi2arr = n.reshape(zchi2arr, (specs.shape[0],) + self.origshape[:-1] +
                             (num_z,) )
        bestl = n.where(zchi2arr == n.min(zchi2arr))[-1][0]
        thisz = ((10**(specloglam[0]))/self.tempwave[bestl+zminpix])-1
        
        #return zchi2arr
        self.zchi2arr = zchi2arr
        #self.store_models(specs, ivar)
        if self.chi2file is True:
            if (plate is not None) & (mjd is not None) & (fiberid is not None):
                write_chi2arr(plate, mjd, fiberid, self.zchi2arr)
            else:
                print 'WARNING Plate/mjd/fiberid not given - unable to write chi2 file!'
        else:
            #print 'INFO Not writing chi2'
            pass
nfibers = hdu[0].header['NFIBERS']

wave = 10**(hdu[0].header['COEFF0'] + np.arange(npix)*hdu[0].header['COEFF1'])

pwave = 10**(3.0 + np.arange(10000)*0.0001)
count = np.zeros(10000)
chi2 = np.zeros(10000)

for i in xrange(1000):
    print i
    if hdu[1].data.ZWARNING[i] == 0:
        if hdu[1].data.CLASS1[i] == 'ssp_galaxy_glob':
            spec = hduplate[0].data[i]
            ivar = hduplate[1].data[i]
            thiswave = wave / (1+hdu[1].data.Z1[i])
            pT = poly_array(npoly, npix)
            ninv = np.diag(ivar)
            a = linalg.solve( np.dot(np.dot(pT,ninv),np.transpose(pT)), np.dot(np.dot(pT,ninv), spec) )
            pmod = np.dot(np.transpose(pT),a)
            for j in xrange(w/2,npix-w/2):
                pmodchi2 = np.sum(((spec[j-w/2:j+w/2+1]-pmod[j-w/2:j+w/2+1])**2)*ivar[j-w/2:j+w/2+1]) / float(w)
                tmodchi2 = np.sum(((spec[j-w/2:j+w/2+1]-hdu[2].data[i,0][j-w/2:j+w/2+1])**2)*ivar[j-w/2:j+w/2+1]) / float(w)
                ind = np.abs(thiswave[j] - pwave).argmin()
                chi2[ind] += (pmodchi2 - tmodchi2)
                count[ind] += 1

y = chi2/count
for i,val in enumerate(y):
    if np.isnan(val): y[i] = 0
p.plot(pwave, y)
p.axis([pwave[0], pwave[-1], min(y)*2, max(y)*1.2])
Beispiel #9
0
    def zchi2(self,
              specs,
              specloglam,
              ivar,
              npixstep=1,
              chi2file=False,
              plate=None,
              mjd=None,
              fiberid=None):
        self.chi2file = chi2file
        self.npixstep = npixstep
        self.zwarning = n.zeros(specs.shape[0])
        flag_val_unplugged = int('0b10000000', 2)
        flag_val_neg_model = int('0b1000', 2)
        self.create_z_baseline(specloglam[0])
        if (self.zmin != None) and (self.zmax != None) and \
                (self.zmax > self.zmin):

            zminpix, zmaxpix = self.conv_zbounds()
            self.pixoffset = zminpix
            num_z = int(n.floor((zmaxpix - zminpix) / npixstep))
            zinds = zminpix + n.arange(num_z) * npixstep
            self.zbase = self.zbase[zinds]
        else:
            zminpix = 0
            # Number of pixels to be fitted in redshift
            num_z = int(
                n.floor((zself.origshape[-1] - specs.shape[-1]) / npixstep))

        # Create arrays for use in routine
        # Create chi2 array of shape (# of fibers, template_parameter_1,
        # ..., template_parameter_N, # of redshifts)
        zchi2arr = n.zeros(
            (specs.shape[0], self.templates_flat.shape[0], num_z))
        temp_zwarning = n.zeros(zchi2arr.shape)

        # Pad data and SSPs to a power of 2 for faster FFTs
        data_pad = n.zeros(specs.shape[:-1] + (self.fftnaxis1, ), dtype=float)
        data_pad[..., :specs.shape[-1]] = specs
        ivar_pad = n.zeros(ivar.shape[:-1] + (self.fftnaxis1, ), dtype=float)
        ivar_pad[..., :specs.shape[-1]] = ivar

        # Pre-compute FFTs for use in convolutions
        data_fft = n.fft.fft(data_pad * ivar_pad)
        ivar_fft = n.fft.fft(ivar_pad)

        if self.npoly > 0:
            # Compute poly terms, noting that they will stay fixed with
            # the data - assumes data is passed in as shape (nfibers, npix)
            polyarr = poly_array(self.npoly, specs.shape[1])
            pmat = n.zeros((self.npoly + 1, self.npoly + 1, self.fftnaxis1),
                           dtype=float)
            bvec = n.zeros((self.npoly + 1, self.fftnaxis1), dtype=float)

            # Pad to a power of 2 for faster FFTs
            poly_pad = n.zeros((self.npoly, self.fftnaxis1), dtype=float)
            poly_pad[..., :polyarr.shape[-1]] = polyarr

            # Pre-compute FFTs for use in convolutions
            poly_fft = n.zeros((ivar_pad.shape[0], self.npoly, self.fftnaxis1),
                               dtype=complex)
            for i in range(self.npoly):
                poly_fft[:, i, :] = n.fft.fft(poly_pad[i] * ivar_pad)

        # Compute z for all fibers

        for i in range(specs.shape[0]):  # Loop over fibers

            start = time.time()

            #print 'INFO Fitting fiber %s of %s for template %s' % \
            #        (i+1, specs.shape[0], self.fname)

            # If flux is all zeros, flag as unplugged according to BOSS
            # zwarning flags and don't bother with doing fit
            if len(n.where(specs[i] != 0.)[0]) == 0:
                self.zwarning[i] = int(self.zwarning[i]) | flag_val_unplugged
            else:  # Otherwise, go ahead and do fit

                self.sn2_data.append(n.sum((specs[i]**2) * ivar[i]))

                if self.npoly > 0:
                    for ipos in range(self.npoly):
                        bvec[ipos + 1] = n.sum(poly_pad[ipos] * data_pad[i] *
                                               ivar_pad[i])
                    for ipos in range(self.npoly):
                        for jpos in range(self.npoly):
                            pmat[ipos + 1, jpos + 1] = n.sum(
                                poly_pad[ipos] * poly_pad[jpos] * ivar_pad[i]
                            )  # CAN GO FASTER HERE (BUT NOT LIMITING = 0.001475

                    f_null = linalg.solve(pmat[1:, 1:, 0], bvec[1:, 0])
                    self.f_nulls.append(f_null)
                    self.chi2_null.append(
                        self.sn2_data[i] -
                        n.dot(n.dot(f_null, pmat[1:, 1:, 0]), f_null))
                else:
                    self.chi2_null.append(self.sn2_data[i])
                # print 'INFO Chi^2_null value is %s' % self.chi2_null[i]
                # Loop over templates
                # multiprocessing

                func_args = []
                for j in range(self.templates_flat.shape[0]):
                    if self.npoly > 0:
                        arguments = {
                            "j": j,
                            "poly_fft": poly_fft[i],
                            "t_fft": self.t_fft[j],
                            "t2_fft": self.t2_fft[j],
                            "data_fft": data_fft[i],
                            "ivar_fft": ivar_fft[i],
                            "pmat_pol": pmat,
                            "bvec_pol": bvec,
                            "chi2_0": self.sn2_data[i],
                            "chi2_null": self.chi2_null[i],
                            "num_z": num_z,
                            "npixstep": self.npixstep,
                            "zminpix": zminpix,
                            "flag_val_neg_model": flag_val_neg_model
                        }
                    else:
                        arguments = {
                            "j": j,
                            "t_fft": self.t_fft[j],
                            "t2_fft": self.t2_fft[j],
                            "data_fft": data_fft[i],
                            "ivar_fft": ivar_fft[i],
                            "chi2_0": self.sn2_data[i],
                            "num_z": num_z,
                            "npixstep": self.npixstep,
                            "zminpix": zminpix,
                            "flag_val_neg_model": flag_val_neg_model
                        }
                    func_args.append(arguments)

                results = None
                if self.nproc > 1:
                    pool = multiprocessing.Pool(self.nproc)
                    if self.npoly > 0:
                        results = pool.map(_zchi2, func_args)
                    else:
                        results = pool.map(_zchi2_no_poly, func_args)
                    pool.close()
                    pool.join()
                else:
                    if self.npoly > 0:
                        results = [_zchi2(x) for x in func_args]
                    else:
                        results = [_zchi2_no_poly(x) for x in func_args]

                for result in results:
                    j = result[0]
                    zchi2arr[i, j] = result[1]
                    temp_zwarning[i, j] = result[2]

                stop = time.time()

                print(
                    "INFO fitted spectrum %d/%d, chi2_null=%f, %d templates in %s, npoly=%d, using %d procs in %f sec"
                    % (i + 1, specs.shape[0], self.chi2_null[i],
                       self.templates_flat.shape[0], self.fname, self.npoly,
                       self.nproc, stop - start))

        # Use only neg_model flag from best fit model/redshift and add
        # it to self.zwarning
        for i in range(self.zwarning.shape[0]):
            minpos = (n.where(zchi2arr[i] == n.min(zchi2arr[i]))[0][0],
                      n.where(zchi2arr[i] == n.min(zchi2arr[i]))[1][0])
            self.zwarning[i] = int(self.zwarning[i]) | \
                    int(temp_zwarning[i,minpos[0],minpos[1]])
        zchi2arr = n.reshape(zchi2arr, (specs.shape[0], ) +
                             self.origshape[:-1] + (num_z, ))
        bestl = n.where(zchi2arr == n.min(zchi2arr))[-1][0]
        thisz = ((10**(specloglam[0])) / self.tempwave[bestl + zminpix]) - 1

        #return zchi2arr
        self.zchi2arr = zchi2arr
        #self.store_models(specs, ivar)
        if self.chi2file is True:
            if (plate is not None) & (mjd is not None) & (fiberid is not None):
                write_chi2arr(plate, mjd, fiberid, self.zchi2arr)
            else:
                print(
                    'WARNING Plate/mjd/fiberid not given - unable to write chi2 file!'
                )
        else:
            print('INFO Not writing chi2')
Beispiel #10
0
ztemp.zchi2(specs.flux, specs.loglambda, specs.ivar, npixstep=1)
zfit_temp = zfitter.ZFitter(ztemp.zchi2arr, ztemp.zbase)
zfit_temp.z_refine()
#temp_flags = misc.comb_flags(specs, ztemp, zfit_temp)
#zpick = zpicker.ZPicker(specs, ztemp, zfit_temp)

# Solve for parameters, create model
import pdb
pdb.set_trace()
minloc = n.unravel_index(ztemp.zchi2arr.argmin(), ztemp.zchi2arr.shape)
pmat = n.zeros((specs.flux.shape[-1], ztemp.npoly + 1))
this_temp = ztemp.templates[minloc[1:-1]]
pmat[:, 0] = this_temp[(minloc[-1] * ztemp.npixstep) +
                       ztemp.pixoffset:(minloc[-1] * ztemp.npixstep) +
                       ztemp.pixoffset + specs.flux.shape[-1]]
polyarr = poly_array(ztemp.npoly, specs.flux.shape[-1])
pmat[:, 1:] = n.transpose(polyarr)
ninv = n.diag(specs.ivar[0])
f = n.linalg.solve(n.dot(n.dot(n.transpose(pmat), ninv), pmat),
                   n.dot(n.dot(n.transpose(pmat), ninv), specs.flux[0]))
model = n.dot(pmat, f)

# Make plot
p.plot(10**specs.loglambda, specs.flux[0], 'r', label='Data')
p.plot(10**specs.loglambda, model, 'k', label='Model', hold=True)
p.title('Plate %s Fiber %s, z=%.4f' % (plate, fiberid[0], zfit_temp.z[0][0]),
        size=18)
p.xlabel(r'Wavelength ($\AA$)', size=16)
p.ylabel(r'Flux ($10^{-17}$ erg s$^{-1}$cm$^{-2}$$\AA^{-1}$)', size=16)
p.legend()