def val(self, value): if not has_astropy_unit(value) and self.name in par_units_set: self._par.value = \ (value * par_units[self.name]).to(par_units_set[self.name]) elif has_astropy_unit(value) and self.name in par_units_set: self._par.value = value.to(par_units_set[self.name]) elif not has_astropy_unit(value): self._par.value = value else: self._par.value = value
def err(self, value): #self._par.uncertainty = value if not has_astropy_unit(value) and self.name in err_units_set: self._par.uncertainty = \ (value * err_units[self.name]).to(err_units_set[self.name]) elif has_astropy_unit(value) and self.name in err_units_set: self._par.uncertainty = value.to(err_units_set[self.name]) elif not has_astropy_unit(value): self._par.uncertainty = value else: self._par.uncertainty = value
def set_params(self, fitp): """Set the model parameters to the value contained in the input dict. Ex. fitter.set_params({'F0':60.1,'F1':-1.3e-15}) """ for k, v in fitp.items(): # The check for astropy units should be able to go away once params are fixed getattr(self.model, k).num_value = v.value if has_astropy_unit(v) else v
def set_params(self,fitp): """ Set the model parameters to the value contained in the input dict. Ex. fitter.set_params({'F0':60.1,'F1':-1.3e-15}) """ for p,val in zip(fitp.keys(),fitp.values()): # If value is unitless but model parameter is not, interpret the # value as being in the same units as the model modval=getattr(self.model,p).value # Right now while the code below preserves the unit, Angle types # become generic astropy quantities. Still, the timing model appears # to work. if not has_astropy_unit(val) and has_astropy_unit(modval): if type(modval) is astropy.coordinates.angles.Angle: val=astropy.coordinates.angles.Angle(val,unit=modval.unit) else: val=val*modval.unit getattr(self.model,p).value = val
def fit(self,iters=1): """tempopulsar.fit(iters=1) Runs `iters` iterations of the tempo2 fit, recomputing barycentric TOAs and residuals each time.""" f = fitter.wls_fitter(toas=self.t, model=self.model) for ii in range(iters+1): f.call_minimize() fitp = f.get_fitparams() # TODO: handle these units correctly for p, val in zip(fitp.keys(), fitp.values()): modval = getattr(f.model, p).value if (not has_astropy_unit(val)) and has_astropy_unit(modval): if type(modval) is ang.Angle: val = ang.Angle(val, unit=modval.unit) else: val = val * modval.unit self[p].val = val
def fit(self, iters=1): """tempopulsar.fit(iters=1) Runs `iters` iterations of the tempo2 fit, recomputing barycentric TOAs and residuals each time.""" f = fitter.wls_fitter(toas=self.t, model=self.model) for ii in range(iters + 1): f.call_minimize() fitp = f.get_fitparams() # TODO: handle these units correctly for p, val in zip(fitp.keys(), fitp.values()): modval = getattr(f.model, p).value if (not has_astropy_unit(val)) and has_astropy_unit(modval): if type(modval) is ang.Angle: val = ang.Angle(val, unit=modval.unit) else: val = val * modval.unit self[p].val = val
def call_minimize(self, method='weighted', maxiter=20): """Run a linear weighted least-squared fitting method""" fitp = self.get_fitparams() # Input variables must be unitless for k, v in zip(fitp.keys(), fitp.values()): if has_astropy_unit(v): fitp[k] = v.value # Define the linear system M, params, units = self.model.designmatrix(toas=self.toas.table, incfrozen=False, incoffset=True) Nvec = numpy.array(self.toas.get_errors().to(u.s))**2 self.update_resids() residuals = self.resids.time_resids.to(u.s) # Weighted linear fit Sigma_inv = numpy.dot(M.T / Nvec, M) U, s, Vt = sl.svd(Sigma_inv) Sigma = numpy.dot(Vt.T / s, U.T) dpars = numpy.dot(Sigma, numpy.dot(M.T, residuals.value / Nvec)) # Uncertainties errs = numpy.sqrt(numpy.diag(Sigma)) # Set the new parameter values # TODO: Now have to do the units manually, because not all parameters # have units everywhere in the code yet. Eventually, this can be # removed conv = { 'F0': u.Hz, 'F1': u.Hz / u.s, 'RAJ': u.hourangle, 'DECJ': u.degree, 'PMRA': u.mas / u.yr, 'PMDEC': u.mas / u.yr, 'PX': u.mas, 'DM': u.s / u.s } # TODO: units and fitp have a different ordering. That is confusing for ii, pn in enumerate(fitp.keys()): uind = params.index(pn) # Index of designmatrix un = 1.0 / (units[uind] / u.s) # Unit in designmatrix pv, dpv = fitp[pn] * conv[pn], dpars[uind] * un fitp[pn] = float((pv + dpv) / conv[pn]) # TODO: Also record the uncertainties in minimize_func chi2 = self.minimize_func(list(fitp.values()), *fitp.keys())
def call_minimize(self, method='weighted', maxiter=20): """Run a linear weighted least-squared fitting method""" fitp = self.get_fitparams() # Input variables must be unitless for k, v in zip(fitp.keys(), fitp.values()): if has_astropy_unit(v): fitp[k] = v.value # Define the linear system M, params, units = self.model.designmatrix(toas=self.toas.table, incfrozen=False, incoffset=True) Nvec = numpy.array(self.toas.get_errors().to(u.s))**2 self.update_resids() residuals = self.resids.time_resids.to(u.s) # Weighted linear fit Sigma_inv = numpy.dot(M.T / Nvec, M) U, s, Vt = sl.svd(Sigma_inv) Sigma = numpy.dot(Vt.T / s, U.T) dpars = numpy.dot(Sigma, numpy.dot(M.T, residuals.value / Nvec)) # Uncertainties errs = numpy.sqrt(numpy.diag(Sigma)) # Set the new parameter values # TODO: Now have to do the units manually, because not all parameters # have units everywhere in the code yet. Eventually, this can be # removed conv = {'F0': u.Hz, 'F1': u.Hz/u.s, 'RAJ':u.hourangle, 'DECJ':u.degree, 'PMRA':u.mas/u.yr, 'PMDEC':u.mas/u.yr, 'PX':u.mas, 'DM':u.s/u.s} # TODO: units and fitp have a different ordering. That is confusing for ii, pn in enumerate(fitp.keys()): uind = params.index(pn) # Index of designmatrix un = 1.0 / (units[uind]/u.s) # Unit in designmatrix pv, dpv = fitp[pn] * conv[pn], dpars[uind] * un fitp[pn] = float( (pv+dpv) / conv[pn] ) # TODO: Also record the uncertainties in minimize_func chi2 = self.minimize_func(list(fitp.values()), *fitp.keys())
def call_minimize(self,method='Powell',maxiter=20): """ Wrapper to scipy.optimize.minimize function. Ex. fitter.call_minimize(method='Powell',maxiter=20) """ # Initial guesses are model params fitp = self.get_fitparams() # Input variables must be unitless for k,v in zip(fitp.keys(),fitp.values()): if has_astropy_unit(v): fitp[k]=v.value self.fitresult=opt.minimize(self.minimize_func,fitp.values(),args=tuple(fitp.keys()), options={'maxiter':maxiter},method=method) # Update model and resids, as the last iteration of minimize is not # necessarily the one that yields the best fit self.minimize_func(numpy.atleast_1d(self.fitresult.x),*fitp.keys())
def __init__(self, par, parname, *args, **kwargs): """Initialize parameter object :param par: The PINT parameter object :param parname: The name (key) of the parameter """ self.name = parname self._par = par self._set = True # Convert the error # TODO: Units should of course be handled in the PINT interface if parname in err_conv: if has_astropy_unit(self._par.uncertainty): raise NotImplementedError("Deprecated conversion stuff!") ev = self._par.uncertainty * err_units_set[parname] self._par.uncertainty = ev.to(err_units[parname]).value
def val(self): if has_astropy_unit(self._par.value): return self._par.value.to(par_units[self.name]).value else: return self._par.value
def err(self): if has_astropy_unit(self._par.uncertainty): return self._par.uncertainty.to(err_units[self.name]).value else: return self._par.uncertainty