def correct(self, cbvs=[1, 2], method='powell', options={}): """ Correct the SAP_FLUX by fitting a number of cotrending basis vectors `cbvs`. Parameters ---------- cbvs : list of ints The list of cotrending basis vectors to fit to the data. For example, [1, 2] will fit the first two basis vectors. method : str Numerical optimization method. See scipy.optimize.minimize for the full list of methods. options : dict Dictionary of options to be passed to scipy.optimize.minimize. """ module, output = channel_to_module_output(self.lc_file.channel) cbv_file = pyfits.open(self.get_cbv_url()) cbv_data = cbv_file['MODOUT_{0}_{1}'.format(module, output)].data cbv_array = [] for i in cbvs: cbv_array.append( cbv_data.field( 'VECTOR_{}'.format(i))[self.lc_file.quality_mask]) cbv_array = np.asarray(cbv_array) sap_lc = self.lc_file.SAP_FLUX median_sap_flux = np.nanmedian(sap_lc.flux) norm_sap_flux = sap_lc.flux / median_sap_flux - 1 norm_err_sap_flux = sap_lc.flux_err / median_sap_flux def mean_model(*theta): coeffs = np.asarray(theta) return np.dot(coeffs, cbv_array) prior = self.prior(mean=np.zeros(len(cbvs)), var=16.) likelihood = self.likelihood(data=norm_sap_flux, mean=mean_model, var=norm_err_sap_flux) x0 = likelihood.fit(x0=prior.mean, method=method, options=options).x posterior = oktopus.Posterior(likelihood=likelihood, prior=prior) self._opt_result = posterior.fit(x0=x0, method=method, options=options) self._coeffs = self._opt_result.x flux_hat = sap_lc.flux - median_sap_flux * mean_model(self._coeffs) return LightCurve(time=sap_lc.time, flux=flux_hat.reshape(-1))
def correct(self, cbvs=(1, 2), method='powell', options=None): """ Correct the SAP_FLUX by fitting a number of cotrending basis vectors `cbvs`. Parameters ---------- cbvs : list of ints The list of cotrending basis vectors to fit to the data. For example, [1, 2] will fit the first two basis vectors. method : str Numerical optimization method. See scipy.optimize.minimize for the full list of methods. options : dict Dictionary of options to be passed to scipy.optimize.minimize. """ if options is None: options = {} median_flux = np.nanmedian(self.lc.flux) norm_flux = self.lc.flux / median_flux - 1 norm_err_flux = self.lc.flux_err / median_flux # Trim down to the right number of cbvs clip = np.in1d(np.arange(1, len(self.cbv_array) + 1), np.asarray(cbvs)) time_clip = np.in1d(self.cbv_cadenceno, self.lc.cadenceno) def mean_model(*theta): coeffs = np.asarray(theta) return np.dot(coeffs, self.cbv_array[clip, :][:, time_clip]) prior = self.prior(mean=np.zeros(len(cbvs)), var=16.) likelihood = self.likelihood(data=norm_flux, mean=mean_model, var=norm_err_flux) x0 = likelihood.fit(x0=prior.mean, method=method, options=options).x posterior = oktopus.Posterior(likelihood=likelihood, prior=prior) self._opt_result = posterior.fit(x0=x0, method=method, options=options) self._coeffs = self._opt_result.x flux_hat = self.lc.flux - median_flux * mean_model(self._coeffs) clc = self.lc.copy() clc.flux = flux_hat.reshape(-1) return clc
def correct(self, cbvs=[1, 2], method='powell', options={}): """ Correct the SAP_FLUX by fitting a number of cotrending basis vectors `cbvs`. Parameters ---------- cbvs : list of ints The list of cotrending basis vectors to fit to the data. For example, [1, 2] will fit the first two basis vectors. method : str Numerical optimization method. See scipy.optimize.minimize for the full list of methods. options : dict Dictionary of options to be passed to scipy.optimize.minimize. """ cbv_array, _ = self._get_cbv_data(cbvs) sap_lc = self.lc_file.SAP_FLUX median_sap_flux = np.nanmedian(sap_lc.flux) norm_sap_flux = sap_lc.flux / median_sap_flux - 1 norm_err_sap_flux = sap_lc.flux_err / median_sap_flux def mean_model(*theta): coeffs = np.asarray(theta) return np.dot(coeffs, cbv_array) prior = self.prior(mean=np.zeros(len(cbvs)), var=16.) likelihood = self.likelihood(data=norm_sap_flux, mean=mean_model, var=norm_err_sap_flux) x0 = likelihood.fit(x0=prior.mean, method=method, options=options).x posterior = oktopus.Posterior(likelihood=likelihood, prior=prior) self._opt_result = posterior.fit(x0=x0, method=method, options=options) self._coeffs = self._opt_result.x flux_hat = sap_lc.flux - median_sap_flux * mean_model(self._coeffs) return LightCurve(time=sap_lc.time, flux=flux_hat.reshape(-1), flux_err=sap_lc.flux_err)