コード例 #1
0
  def FitErrorFunction(self, fitpars):
    """
    The error function for the fitter. This should never be called directly!
    """
    if self.return_resolution:
      model, resolution = self.GenerateModel(fitpars, return_resolution=True)
    else:
      model = self.GenerateModel(fitpars)
    outfile = open("chisq_summary.dat", 'a')
    weights = 1.0 / self.data.err**2

    #Find the regions to use (ignoring the parts that were defined as bad)
    good = numpy.arange(self.data.x.size, dtype=numpy.int32)
    for region in self.ignore:
      x0 = min(region)
      x1 = max(region)
      tmp1 = [self.data.x[i] in self.data.x[good] for i in range(self.data.x.size)]
      tmp2 = numpy.logical_or(self.data.x<x0, self.data.x>x1)
      good = numpy.where(numpy.logical_and(tmp1, tmp2))[0]

    
    return_array = (self.data.y - self.data.cont*model.y)[good]**2 * weights[good]
    #Evaluate bound conditions and output the parameter value to the logfile.
    fit_idx = 0
    for i in range(len(self.bounds)):
      if self.fitting[i]:
        if len(self.bounds[i]) == 2:
          return_array += FittingUtilities.bound(self.bounds[i], fitpars[fit_idx])
        outfile.write("%.12g\t" %fitpars[fit_idx])
        self.parvals[i].append(fitpars[fit_idx])
        fit_idx += 1
      elif len(self.bounds[i]) == 2 and self.parnames[i] != "resolution":
        return_array += FittingUtilities.bound(self.bounds[i], self.const_pars[i])
    outfile.write("%g\n" %(numpy.sum(return_array)/float(weights.size)))
    
    self.chisq_vals.append(numpy.sum(return_array)/float(weights.size))
    print "X^2 = ", numpy.sum(return_array)/float(weights.size)
    outfile.close()
    
    return return_array
コード例 #2
0
  def ResolutionFitError(self, resolution, data, model):
    """
    This function gets called by scipy.optimize.fminbound in FitResolution.
    Not meant to be called directly by the user!
    """
    resolution = max(1000.0, float(int(float(resolution) + 0.5)))
    if self.debug and self.debug_level >= 5:
      print "Saving inputs for R = ", resolution
      print " to Debug_ResFit.log and Debug_ResFit2.log"
      numpy.savetxt("Debug_ResFit.log", numpy.transpose((data.x, data.y, data.cont)))
      numpy.savetxt("Debug_Resfit2.log", numpy.transpose((model.x, model.y)))
    newmodel = FittingUtilities.ReduceResolution(model, resolution, extend=False)
    newmodel = FittingUtilities.RebinData(newmodel, data.x, synphot=False)

    #Find the regions to use (ignoring the parts that were defined as bad)
    good = numpy.arange(self.data.x.size, dtype=numpy.int32)
    for region in self.ignore:
      x0 = min(region)
      x1 = max(region)
      tmp1 = [self.data.x[i] in self.data.x[good] for i in range(self.data.x.size)]
      tmp2 = numpy.logical_or(self.data.x<x0, self.data.x>x1)
      good = numpy.where(numpy.logical_and(tmp1, tmp2))[0]

    weights = 1.0/data.err**2
    returnvec = (data.y - data.cont*newmodel.y)[good]**2 * weights[good] + FittingUtilities.bound(self.resolution_bounds, resolution)
    if self.debug:
      print "Resolution-fitting X^2 = ", numpy.sum(returnvec)/float(good.size), "at R = ", resolution
    if numpy.isnan(numpy.sum(returnvec**2)):
      print "Error! NaN found in ResolutionFitError!"
      outfile=open("ResolutionFitError.log", "a")
      outfile.write("#Error attempting R = %g\n" %(resolution))
      numpy.savetxt(outfile, numpy.transpose((data.x, data.y, data.cont, newmodel.x, newmodel.y)), fmt="%.10g")
      outfile.write("\n\n\n\n")
      numpy.savetxt(outfile, numpy.transpose((model.x, model.y)), fmt="%.10g")
      outfile.write("\n\n\n\n")
      outfile.close()
      raise ValueError
    return returnvec