def time_ccdcesque1(self, setup): """ Bench with 'defaults' defined in setup with most tests turned off """ kwargs = version_kwargs(setup['kwargs']) for i in range(n): model = CCDCesque(**kwargs) model.fit(setup['X'], setup['Y'], setup['dates'])
def pixel_CCDCesque(pipe, require, output, config=None): """ Run :class:`yatsm.algorithms.CCDCesque` on a pixel Users should pass to ``require`` both ``X`` and ``Y`` arguments, which are interpreted as: .. code-block:: python X, Y = require[0], require[1:] Args: pipe (yatsm.pipeline.Pipe): Piped data to operate on require (dict[str, list[str]]): Labels for the requirements of this calculation output (dict[str, list[str]]): Label for the result of this calculation config (dict): Configuration to pass to :class:`CCDCesque`. Should contain `init` section Returns: yatsm.pipeline.Pipe: Piped output """ XY = pipe.data[require['data']].dropna('time', how='any') X = XY[require['data'][0]] Y = XY[require['data'][1:]].to_array() model = CCDCesque(**config.get('init', {})) model.py, model.px = Y.y, Y.x model = model.fit(X, Y.values, XY['ordinal']) pipe.record[output[RECORD][0]] = model.record return pipe
def time_ccdcesque3(self, setup): """ Bench with remove_noise, dynamic_rmse turned on """ kwargs = version_kwargs(setup['kwargs']) kwargs.update({'remove_noise': True, 'dynamic_rmse': True}) for i in range(n): model = CCDCesque(**kwargs) model.fit(setup['X'], setup['Y'], setup['dates'])
def time_ccdcesque1(self, setup): """ Bench with 'defaults' defined in setup with most tests turned off """ kwargs = version_kwargs(setup['kwargs']) model = CCDCesque(**kwargs) for col in range(setup['Y'].shape[-1]): _Y, _X, _dates = setup['Y'][..., col], setup['X'], setup['dates'] mask = np.in1d(_Y[-1, :], [0, 1]) model.fit(_X[mask, :], _Y[:, mask], _dates[mask])
def time_ccdcesque3(self, setup): """ Bench with remove_noise, dynamic_rmse turned on """ kwargs = version_kwargs(setup['kwargs']) kwargs.update({'remove_noise': True, 'dynamic_rmse': True}) model = CCDCesque(**kwargs) for col in range(setup['Y'].shape[-1]): _Y, _X, _dates = setup['Y'][..., col], setup['X'], setup['dates'] mask = np.in1d(_Y[-1, :], [0, 1]) model.fit(_X[mask, :], _Y[:, mask], _dates[mask])
def pixel_CCDCesque(work, require, output, **config): """ Run CCDCesque on a pixel """ arr = work['data'][require['data']].dropna('time', how='any').to_array() model = CCDCesque(**config.get('init', {})) model.py, model.px = arr.y, arr.x ordinal = arr.indexes['time'].map(lambda x: x.toordinal()) design = config.get('fit', {}).get('design', '1 + ordinal') X = patsy.dmatrix(design, data=arr, eval_env=patsy.EvalEnvironment.capture()) work['record'][output['record'][0]] = model.fit(X, arr.values, ordinal) return work
def _fetch_results_live(self): """ Run YATSM and get results """ logger.debug('Calculating YATSM results on the fly') # Setup design matrix, Y, and dates self.X = patsy.dmatrix( self.controls['design'].value, { 'x': self.series[0].images['ordinal'], 'sensor': self.series[0].sensor, 'pr': self.series[0].pathrow }) self._design_info = self.X.design_info.column_name_indexes self.Y = self.series[0].data.astype(np.int16) self.dates = np.asarray(self.series[0].images['ordinal']) mask = self.Y[self.config['mask_band'].value[0] - 1, :] Y_data = np.delete(self.Y, self.config['mask_band'].value[0] - 1, axis=0) # Mask out masked values clear = np.in1d(mask, self.mask_values, invert=True) valid = get_valid_mask(Y_data, self.config['min_values'].value, self.config['max_values'].value).astype(np.bool) clear *= valid # Setup parameters estimator = sklearn.linear_model.Lasso(alpha=20) reg = self.controls['regression_type'].value if hasattr(yatsm.regression, 'packaged'): if reg in yatsm.regression.packaged.packaged_regressions: reg_fn = yatsm.regression.packaged.find_packaged_regressor(reg) try: estimator = jl.load(reg_fn) except: logger.error('Cannot load regressor: %s' % reg) else: logger.debug('Loaded regressor %s from %s' % (reg, reg_fn)) else: logger.error('Cannot use unknown regression %s' % reg) else: logger.warning( 'Using failsafe Lasso(lambda=20) from scikit-learn. ' 'Upgrade to yatsm>=0.5.1 to access more regressors.') kwargs = dict( estimator=estimator, test_indices=self.controls['test_indices'].value, consecutive=self.controls['consecutive'].value, threshold=self.controls['threshold'].value, min_obs=self.controls['min_obs'].value, min_rmse=(None if self.controls['enable_min_rmse'].value else self.controls['min_rmse'].value), screening_crit=self.controls['screen_crit'].value, remove_noise=self.controls['remove_noise'].value, dynamic_rmse=self.controls['dynamic_rmse'].value, ) self.yatsm_model = CCDCesque(**version_kwargs(kwargs)) # Don't want to have DEBUG logging when we run YATSM log_level = logger.level logger.setLevel(logging.INFO) if self.controls['reverse'].value: self.yatsm_model.fit(np.flipud(self.X[clear, :]), np.fliplr(Y_data[:, clear]), self.dates[clear][::-1]) else: self.yatsm_model.fit(self.X[clear, :], Y_data[:, clear], self.dates[clear]) if self.controls['commit_test'].value: self.yatsm_model.record = postprocess.commission_test( self.yatsm_model, self.controls['commit_alpha'].value) # if self.controls['robust_results'].value: # self.coef_name = 'robust_coef' # self.yatsm_model.record = postprocess.refit_record( # self.yatsm_model, 'robust' # else: # self.coef_name = 'coef' if self.config['calc_pheno'].value: # TODO: parameterize band indices & scale factor ltm = pheno.LongTermMeanPhenology() self.yatsm_model.record = ltm.fit(self.yatsm_model) # Restore log level logger.setLevel(log_level)