Пример #1
0
    def z_refine(self, threshold=23.3, width=15):
        # Default threshold of 23.3 is delta (chi_r)**2 = .005, and
        # width is 1000 km/s
        self.threshold = threshold
        self.width = width
        for ifiber in range(self.zchi2.shape[0]):
            self.minvector.append((ifiber, ) + n.unravel_index(
                self.zchi2[ifiber].argmin(), self.zchi2[ifiber].shape))
            bestzvec = n.zeros(self.zchi2.shape[-1])
            for iz in range(self.zchi2.shape[-1]):
                bestzvec[iz] = n.min(self.zchi2[ifiber, ..., iz])
            posinvec = n.where(bestzvec == n.min(bestzvec))[0][0]
            # Flag and skip interpolation fit if best chi2 is at edge of z-range
            if (posinvec == 0) or (posinvec == bestzvec.shape[0] - 1):
                self.flag_z_fitlimit(ifiber)
                self.z[ifiber] = -1.
                self.z_err[ifiber] = -1.
            else:
                # Find best redshift using global chi2 minimum
                xp = n.linspace(self.zbase[posinvec - 1],
                                self.zbase[posinvec + 1], 1000)
                f = quadfit(self.zbase[posinvec - 1:posinvec + 2],
                            bestzvec[posinvec - 1:posinvec + 2])
                fit = quad_for_fit(xp, f[0], f[1], f[2])
                #p.plot(xp, fit, color='red', hold=False)
                #p.plot(self.zbase[posinvec-1:posinvec+2],
                #bestzvec[posinvec-1:posinvec+2], 'ko', hold=True)
                self.z[ifiber, 0] = xp[n.where(fit == n.min(fit))[0][0]]
                self.z_err[ifiber, 0] = self.estimate_z_err(xp, fit)
                # Find second- and third-, and fourth-best redshifts
                zspline = gs.GridSpline(bestzvec)
                zminlocs = n.round(zspline.get_min())
                zminvals = zspline.get_val(zminlocs)
                if len(zminvals) == 1:
                    self.z[ifiber, 1] = -1.
                    self.z_err[ifiber, 1] = -1.
                    self.z[ifiber, 2] = -1
                    self.z_err[ifiber, 2] = -1
                    self.z[ifiber, 3] = -1
                    self.z_err[ifiber, 3] = -1
                    self.z[ifiber, 4] = -1
                    self.z_err[ifiber, 4] = -1
                elif len(zminvals) == 2:
                    self.z[ifiber, 2] = -1
                    self.z_err[ifiber, 2] = -1
                    self.z[ifiber, 3] = -1
                    self.z_err[ifiber, 3] = -1
                    self.z[ifiber, 4] = -1
                    self.z_err[ifiber, 4] = -1
                elif len(zminvals) == 3:
                    self.z[ifiber, 3] = -1
                    self.z_err[ifiber, 3] = -1
                    self.z[ifiber, 4] = -1
                    self.z_err[ifiber, 4] = -1
                elif len(zminvals) == 4:
                    self.z[ifiber, 4] = -1
                    self.z_err[ifiber, 4] = -1
                imin = 0
                while (self.z[ifiber,1] == 0) | (self.z[ifiber,2] == 0) | \
                        (self.z[ifiber,3] == 0) | (self.z[ifiber,4] == 0):
                    imin += 1
                    nextpos = zminlocs[n.where(
                        zminvals == n.sort(zminvals)[imin])[0][0]]
                    if (abs(posinvec - nextpos) > width) & (nextpos != 0) & \
                            (nextpos != bestzvec.shape[0]-1) :
                        xp = n.linspace(self.zbase[nextpos - 1],
                                        self.zbase[nextpos + 1], 1000)
                        f = quadfit(self.zbase[nextpos - 1:nextpos + 2],
                                    bestzvec[nextpos - 1:nextpos + 2])
                        fit = quad_for_fit(xp, f[0], f[1], f[2])
                        self.z[ifiber,
                               imin] = xp[n.where(fit == n.min(fit))[0][0]]
                        self.z_err[ifiber, imin] = self.estimate_z_err(xp, fit)
                    else:
                        self.z[ifiber, imin] = -1.
                        self.z_err[ifiber, imin] = -1.

                # Flag fibers with small delta chi2 in redshift
                self.flag_small_dchi2(ifiber,
                                      bestzvec,
                                      threshold=threshold,
                                      width=width)
Пример #2
0
    def z_refine(self, threshold=23.3, width=15):
        # Default threshold of 23.3 is delta (chi_r)**2 = .005, and
        # width is 1000 km/s
        self.threshold = threshold
        self.width = width
        for ifiber in range(self.zchi2.shape[0]):
            self.minvector.append( (ifiber,) +
                                  n.unravel_index(self.zchi2[ifiber].argmin(),
                                                  self.zchi2[ifiber].shape))
            bestzvec = n.zeros( self.zchi2.shape[-1])
            for iz in range(self.zchi2.shape[-1]):
                bestzvec[iz] = n.min( self.zchi2[ifiber,...,iz] )
            posinvec = n.where( bestzvec == n.min(bestzvec) )[0][0]
            # Flag and skip interpolation fit if best chi2 is at edge of z-range
            if (posinvec == 0) or (posinvec == bestzvec.shape[0]-1):
                self.flag_z_fitlimit(ifiber)
                self.z[ifiber] = -1.
                self.z_err[ifiber] = -1.
            else:
                # Find best redshift using global chi2 minimum
                xp = n.linspace(self.zbase[posinvec-1], self.zbase[posinvec+1],
                                1000)
                f = quadfit(self.zbase[posinvec-1:posinvec+2],
                            bestzvec[posinvec-1:posinvec+2])
                fit = quad_for_fit(xp, f[0], f[1], f[2])
                #p.plot(xp, fit, color='red', hold=False)
                #p.plot(self.zbase[posinvec-1:posinvec+2],
                        #bestzvec[posinvec-1:posinvec+2], 'ko', hold=True)
                self.z[ifiber,0] = xp[n.where(fit == n.min(fit))[0][0]]
                self.z_err[ifiber,0] = self.estimate_z_err(xp, fit)
                # Find second- and third-, and fourth-best redshifts
                zspline = gs.GridSpline(bestzvec)
                zminlocs = n.round(zspline.get_min())
                zminvals = zspline.get_val(zminlocs)
                if len(zminvals) == 1:
                    self.z[ifiber,1] = -1.
                    self.z_err[ifiber,1] = -1.
                    self.z[ifiber,2] = -1
                    self.z_err[ifiber,2] = -1
                    self.z[ifiber,3] = -1
                    self.z_err[ifiber,3] = -1
                    self.z[ifiber,4] = -1
                    self.z_err[ifiber,4] = -1
                elif len(zminvals) == 2:
                    self.z[ifiber,2] = -1
                    self.z_err[ifiber,2] = -1
                    self.z[ifiber,3] = -1
                    self.z_err[ifiber,3] = -1
                    self.z[ifiber,4] = -1
                    self.z_err[ifiber,4] = -1
                elif len(zminvals) == 3:
                    self.z[ifiber,3] = -1
                    self.z_err[ifiber,3] = -1
                    self.z[ifiber,4] = -1
                    self.z_err[ifiber,4] = -1
                elif len(zminvals) == 4:
                    self.z[ifiber,4] = -1
                    self.z_err[ifiber,4] = -1
                imin = 0
                while (self.z[ifiber,1] == 0) | (self.z[ifiber,2] == 0) | \
                        (self.z[ifiber,3] == 0) | (self.z[ifiber,4] == 0):
                    imin += 1
                    nextpos = zminlocs[n.where(
                            zminvals == n.sort(zminvals)[imin])[0][0]]
                    if (abs(posinvec - nextpos) > width) & (nextpos != 0) & \
                            (nextpos != bestzvec.shape[0]-1) :
                        xp = n.linspace(self.zbase[nextpos-1],
                                        self.zbase[nextpos+1], 1000)
                        f = quadfit(self.zbase[nextpos-1:nextpos+2],
                                    bestzvec[nextpos-1:nextpos+2])
                        fit = quad_for_fit(xp, f[0], f[1], f[2])
                        self.z[ifiber,imin] = xp[n.where(
                                fit == n.min(fit))[0][0]]
                        self.z_err[ifiber,imin] = self.estimate_z_err(xp, fit)
                    else:
                        self.z[ifiber,imin] = -1.
                        self.z_err[ifiber,imin] = -1.

                # Flag fibers with small delta chi2 in redshift
                self.flag_small_dchi2(ifiber, bestzvec, threshold=threshold,
                                      width=width)
Пример #3
0
 def z_refine2(self, threshold=23.3, width=15, num_z=5):
     # Default threshold of 23.3 is delta (chi_r)**2 = .005,
     # and width is 1000 km/s
     self.num_z = num_z
     self.threshold = threshold
     self.width = width
     self.z = n.zeros((self.zchi2.shape[0], num_z))
     self.z_err = n.zeros((self.zchi2.shape[0], num_z))
     self.minvectors = []
     self.chi2vals = []
     for ifiber in range(self.zchi2.shape[0]):
         allminvectors = []
         bestminvectors = []
         bestchi2vals = []
         bestzvec = n.zeros(self.zchi2.shape[-1])
         # Make vector of minimum chi2 at each trial redshift
         for iz in range(self.zchi2.shape[-1]):
             bestzvec[iz] = n.min(self.zchi2[ifiber, ..., iz])
             # Location in zchi2[ifiber,...,iz] of min
             allminvectors.append(
                 n.unravel_index(self.zchi2[ifiber, ..., iz].argmin(),
                                 self.zchi2[ifiber, ..., iz].shape))
         zspline = gs.GridSpline(bestzvec)  # Spline up bestzvec
         zminlocs = list(map(int, n.round(
             zspline.get_min())))  # Minimun locations
         zminvals = zspline.get_val(zminlocs)  # Minimum values
         z_ind = 0
         stop = False
         #import pdb; pdb.set_trace()
         if len(zminvals) == 0:
             self.flag_null_fit(ifiber)
             self.z[ifiber] = -1.
             self.z_err[ifiber] = -1.
             bestminvectors = [(-1, )] * num_z
             bestchi2vals = [n.max(bestzvec)] * num_z
         else:
             while (z_ind < num_z) & (stop == False):
                 # Location in zminvals vector of minimum
                 thisminloc = zminvals.argmin()
                 # Location in bestzvec of minimum
                 posinvec = zminlocs[thisminloc]
                 # Flag and skip interpretation if best fit chi2 is
                 # at edge of z-range
                 if (posinvec == 0) or (posinvec == bestzvec.shape[0] - 1):
                     # If it's the first redshift, set all num_z
                     # redshifts and errors to -1
                     if z_ind == 0:
                         self.flag_z_fitlimit(ifiber)
                         self.z[ifiber] = -1.
                         self.z_err[ifiber] = -1.
                         bestminvectors = [(-1, )] * num_z
                         bestchi2vals = [n.max(bestzvec)] * num_z
                         stop = True
                     # If it's not the first, and if it's on the
                     # left edge, set the 15 pixels to the right to
                     # n.max(bestzvec) and move on
                     elif posinvec == 0:
                         k = 0
                         while True:
                             k += 1
                             try:
                                 if n.abs(zminlocs[thisminloc+k] - \
                                          zminlocs[thisminloc]) < self.width:
                                     zminvals[thisminloc +
                                              k] = n.max(bestzvec)
                                 else:
                                     break
                             except IndexError:
                                 break
                         z_ind += 1
                     # If it's not the first, and if it's on the
                     # right edge, set the 15 pixels to the left =
                     # n.max(bestzvec) and move on
                     elif posinvec == bestzvec.shape[0] - 1:
                         k = 0
                         while True:
                             k += 1
                             try:
                                 if n.abs(zminlocs[thisminloc-k] - \
                                          zminlocs[thisminloc]) < self.width:
                                     zminvals[thisminloc -
                                              k] = n.max(bestzvec)
                                 else:
                                     break
                             except IndexError:
                                 break
                         z_ind += 1
                 # Fit with quadratic and find minimum and error
                 else:
                     xp = n.linspace(self.zbase[posinvec - 1],
                                     self.zbase[posinvec + 1], 1000)
                     f = quadfit(self.zbase[posinvec - 1:posinvec + 2],
                                 bestzvec[posinvec - 1:posinvec + 2])
                     fit = quad_for_fit(xp, f[0], f[1], f[2])
                     if not xp[n.where(fit == n.min(fit))[0][0]] in \
                             self.z[ifiber]:
                         self.z[ifiber, z_ind] = xp[n.where(
                             fit == n.min(fit))[0][0]]
                         self.z_err[ifiber,
                                    z_ind] = self.estimate_z_err(xp, fit)
                         bestchi2vals.append(bestzvec[posinvec])
                         bestminvectors.append(allminvectors[posinvec] +
                                               (posinvec, ))
                     else:
                         self.z[ifiber, z_ind] = -1.
                         self.z_err[ifiber, z_ind] = -1.
                         bestchi2vals.append(n.max(bestzvec))
                         bestminvectors.append((-1, ))
                     zminvals[thisminloc] = n.max(bestzvec)
                     k = 0
                     # Set width points to the right and left of minimum
                     # to n.max(bestzvec)
                     while True:
                         k += 1
                         try:
                             if n.abs(zminlocs[thisminloc+k] - \
                                      zminlocs[thisminloc]) < self.width:
                                 zminvals[thisminloc + k] = n.max(bestzvec)
                             else:
                                 break
                         except IndexError:
                             break
                     k = 0
                     while True:
                         k += 1
                         try:
                             if n.abs(zminlocs[thisminloc-k] - \
                                      zminlocs[thisminloc]) < self.width:
                                 zminvals[thisminloc - k] = n.max(bestzvec)
                             else:
                                 break
                         except IndexError:
                             break
                     z_ind += 1
         self.minvectors.append(bestminvectors)
         self.chi2vals.append(bestchi2vals)
     self.flag_small_dchi2_2()
Пример #4
0
 def z_refine2(self, threshold=23.3, width=15, num_z=5):
     # Default threshold of 23.3 is delta (chi_r)**2 = .005,
     # and width is 1000 km/s
     self.num_z = num_z
     self.threshold = threshold
     self.width = width
     self.z = n.zeros((self.zchi2.shape[0],num_z))
     self.z_err = n.zeros((self.zchi2.shape[0],num_z))
     self.minvectors = []
     self.chi2vals = []
     for ifiber in range(self.zchi2.shape[0]):
         allminvectors = []
         bestminvectors = []
         bestchi2vals = []
         bestzvec = n.zeros( self.zchi2.shape[-1] )
         # Make vector of minimum chi2 at each trial redshift
         for iz in range(self.zchi2.shape[-1]):
             bestzvec[iz] = n.min( self.zchi2[ifiber,...,iz] )
             # Location in zchi2[ifiber,...,iz] of min
             allminvectors.append( n.unravel_index(
                     self.zchi2[ifiber,...,iz].argmin(),
                     self.zchi2[ifiber,...,iz].shape ) )
         zspline = gs.GridSpline(bestzvec) # Spline up bestzvec
         zminlocs = list(map(int,n.round(zspline.get_min()))) # Minimun locations
         zminvals = zspline.get_val(zminlocs) # Minimum values
         z_ind = 0
         stop = False
         #import pdb; pdb.set_trace()
         if len(zminvals) == 0:
             self.flag_null_fit(ifiber)
             self.z[ifiber] = -1.
             self.z_err[ifiber] = -1.
             bestminvectors = [(-1,)]*num_z
             bestchi2vals = [n.max(bestzvec)]*num_z
         else:
             while (z_ind < num_z) & (stop == False):
                 # Location in zminvals vector of minimum
                 thisminloc = zminvals.argmin()
                 # Location in bestzvec of minimum
                 posinvec = zminlocs[thisminloc]
                 # Flag and skip interpretation if best fit chi2 is
                 # at edge of z-range
                 if (posinvec == 0) or (posinvec == bestzvec.shape[0]-1):
                     # If it's the first redshift, set all num_z
                     # redshifts and errors to -1
                     if z_ind == 0:
                         self.flag_z_fitlimit(ifiber)
                         self.z[ifiber] = -1.
                         self.z_err[ifiber] = -1.
                         bestminvectors = [(-1,)]*num_z
                         bestchi2vals = [n.max(bestzvec)]*num_z
                         stop = True
                     # If it's not the first, and if it's on the
                     # left edge, set the 15 pixels to the right to
                     # n.max(bestzvec) and move on
                     elif posinvec == 0:
                         k = 0
                         while True:
                             k += 1
                             try:
                                 if n.abs(zminlocs[thisminloc+k] - \
                                          zminlocs[thisminloc]) < self.width:
                                     zminvals[thisminloc+k] = n.max(bestzvec)
                                 else:
                                     break
                             except IndexError:
                                 break
                         z_ind += 1
                     # If it's not the first, and if it's on the
                     # right edge, set the 15 pixels to the left =
                     # n.max(bestzvec) and move on
                     elif posinvec == bestzvec.shape[0]-1:
                         k = 0
                         while True:
                             k += 1
                             try:
                                 if n.abs(zminlocs[thisminloc-k] - \
                                          zminlocs[thisminloc]) < self.width:
                                     zminvals[thisminloc-k] = n.max(bestzvec)
                                 else:
                                     break
                             except IndexError:
                                 break
                         z_ind += 1
                 # Fit with quadratic and find minimum and error
                 else:
                     xp = n.linspace(self.zbase[posinvec-1],
                                     self.zbase[posinvec+1], 1000)
                     f = quadfit(self.zbase[posinvec-1:posinvec+2],
                                 bestzvec[posinvec-1:posinvec+2])
                     fit = quad_for_fit(xp, f[0], f[1], f[2])
                     if not xp[n.where(fit == n.min(fit))[0][0]] in \
                             self.z[ifiber]:
                         self.z[ifiber,z_ind] = xp[n.where(fit ==
                                                           n.min(fit))[0][0]]
                         self.z_err[ifiber,z_ind] = self.estimate_z_err(
                                 xp, fit)
                         bestchi2vals.append( bestzvec[posinvec] )
                         bestminvectors.append( allminvectors[posinvec] +
                                               (posinvec,) )
                     else:
                         self.z[ifiber,z_ind] = -1.
                         self.z_err[ifiber,z_ind] = -1.
                         bestchi2vals.append(n.max(bestzvec))
                         bestminvectors.append((-1,))
                     zminvals[thisminloc] = n.max(bestzvec)
                     k = 0
                     # Set width points to the right and left of minimum
                     # to n.max(bestzvec)
                     while True:
                         k += 1
                         try:
                             if n.abs(zminlocs[thisminloc+k] - \
                                      zminlocs[thisminloc]) < self.width:
                                 zminvals[thisminloc+k] = n.max(bestzvec)
                             else:
                                 break
                         except IndexError:
                             break
                     k = 0
                     while True:
                         k += 1
                         try:
                             if n.abs(zminlocs[thisminloc-k] - \
                                      zminlocs[thisminloc]) < self.width:
                                 zminvals[thisminloc-k] = n.max(bestzvec)
                             else:
                                 break
                         except IndexError:
                             break
                     z_ind += 1
         self.minvectors.append( bestminvectors )
         self.chi2vals.append( bestchi2vals )
     self.flag_small_dchi2_2()