def _BSBEC_add_met_data(self, data, met_data, years): uids = misc.uniq_key(data, formats.UID) for uid in uids: for year in years: fll_reader = FLLReader(Location(Location.BSBEC, year)) emergence = fll_reader.get_plot_fll(uid) max_date = datetime(year + 1, 3, 10) subset = [ x for x in data if in_season_uid(x, uid, emergence, max_date) ] met_data_subset = [ x for x in met_data if in_season(x, emergence, max_date) ] accumulator = MetDataAccumulator(emergence, met_data_subset, self._t_base) for entry in subset: met_entry = accumulator.get_record(entry[formats.DATE]) entry[formats.DD] = met_entry[formats.DD] entry[formats.PAR] = met_entry[formats.PAR] entry[formats.RAINFALL] = met_entry[formats.RAINFALL] return data
def _fix_fll_reader(self, condition, **kwargs): if condition['year'] > 2014: location = Location(kwargs['location'], condition['year']) fll_reader = FLLReaderMissing(location) return {'fll_reader': fll_reader} else: return dict()
def get_fll_date(self, condition): """ This function gives the FLL info to the CDModel class, to be used in simulations""" geno = condition[formats.GENOTYPE] year = condition['year'] fll_reader = FLLReader(Location(self._location_name, year)) return fll_reader.get_genotype_fll(geno)
def _simulate(self, keep_all=False): years = set([x[formats.DATE].year for x in self._test_data]) genotypes = set([x[formats.GENOTYPE] for x in self._test_data]) conditions = [] for year in years: for genotype in genotypes: condition = {formats.GENOTYPE: genotype, 'year': year} conditions.append(condition) for condition in conditions: all_params = self._model.get_parameters() params = [ x for x in all_params if x['conditions'] == condition[formats.GENOTYPE] ] kwargs = dict() keys = ['k', 'LER', 'RUE'] for key in keys: kwargs[key] = next(x['parameter'] for x in params if x['variable'] == key) # hack to fix EMI-11 k value for 2012 and later # that applies just to paper_no_training scenario if self._model._scenario == ChrisDaveyModel.PAPER_NO_TRAINING and \ condition[formats.GENOTYPE] == "EMI-11" and \ condition[formats.YEAR] >= 2012: kwargs['k'] = 0.5533 if condition['year'] > 2014: location = Location(Location.BSBEC, condition['year']) kwargs['fll_reader'] = FLLReaderMissing(location) simulation = self._model.simulate(condition, t_base=0, **kwargs) for entry in self._test_data: match = [ x for x in simulation if x[formats.GENOTYPE] == entry[formats.GENOTYPE] and x[formats.DATE] == entry[formats.DATE] ] if len(match) > 1: # this should not happen raise Exception("How does that even?!") elif len(match) == 1: entry['predicted'] = match[0][formats.DW_PLANT] if keep_all: for var_name in [ formats.PAR, formats.DD, formats.LEAF_AREA_INDEX ]: entry[var_name] = match[0][var_name]
def _fix_start_date(self, years, genotype): start_dates = [] for year in years: location = Location(self._location.get_name(), year) fll_reader = FLLReader(location) fll_date = fll_reader.get_genotype_fll(genotype) start_dates.append(int(fll_date.strftime("%j"))) jul = sum(start_dates) / len(start_dates) date_string = '%d %s' % (jul, self._location.get_year()) start_date = datetime.strptime(date_string, "%j %Y") return start_date
def _inject_fll(self, data, preps=[], additional_phenos=False): # inject at FLL uids = misc.uniq_key(data, formats.UID) years = set([x[formats.DATE].year for x in data]) for year in years: reader = FLLReader(Location(Location.BSBEC, year)) for uid in uids: fll_date = reader.get_plot_fll(uid) entry = { formats.UID: uid, formats.DATE: fll_date, formats.GENOTYPE: misc.assign_geno_bsbec(uid), formats.TRANSMISSION: 1.0, formats.DD: 0.0, formats.PAR: 0.0, formats.LEAF_AREA_INDEX: 0.0, formats.STEM_COUNT: 0.0, formats.CANOPY_HEIGHT: 0.0, formats.DW_PLANT: 0.0 } if additional_phenos: if year < 2016: diam = None else: diam = 0 entry[formats.BASE_DIAM_1] = diam entry[formats.BASE_DIAM_2] = diam entry[formats.STEM_DIAM] = diam entry[formats.YEAR] = year entry = misc.row_col_BSBEC(entry) entry[formats.DOY] = int( entry[formats.DATE].strftime("%j")) entry[formats.FLOWERING_SCORE] = 0 entry[formats.RAINFALL] = 0 if preps != []: for prep in preps: new_entry = deepcopy(entry) new_entry[formats.PSEUDO_REP] = prep data.append(new_entry) else: data.append(entry) return data
def get_met_data(self, condition, t_base): location = Location(self._location_name, condition['year']) met_data = MetDataReaderCSV(location, t_base=t_base).get_records() return met_data
def _create_locations(self, years, location_name): locations = [] for year in years: locations.append(Location(location_name, year)) return locations
def create_locations(self, years): locations = [] for year in years: locations.append(Location(Location.BSBEC, year)) return locations