def __call__(self, composite: Composite): bmis = [ bmi for period, bmi in composite.get_all_observations( self.annual_bmi_var) ] mean_bmi = numpy.average(bmis) composite.put_immutable(self.mean_bmi_var, mean_bmi)
def alter(self, composite_id: str, composite: Composite) -> None: overall_rank = self.ranked["overall"][composite_id] composite.put_immutable(self.bmi_rank_overall_var, overall_rank) gender: str = self._get_gender(composite_id) gender_rank = self.ranked[gender][composite_id] composite.put_immutable(self.bmi_rank_gender_var, gender_rank)
def __call__(self, composite: Composite): periods: List[str] = list(composite.periods) annual_prods = [ composite.get_observation(self.annual_prod_var, period) for period in periods ] mean_prod = numpy.average(annual_prods) composite.put_immutable(self.mean_prod_var, mean_prod)
def __call__(self, composite: Composite): zip_code: str = composite.get_immutable(self.zip_var) city = self.lookups["zipcodes"][zip_code]["City"] composite.put_immutable(self.city_var, city) state = self.lookups["zipcodes"][zip_code]["State"] composite.put_immutable(self.state_var, state)
def __call__(self, composite: Composite): years = sorted([int(year) for year in composite.periods]) weights = [composite.get_observation(self.annual_weight_var, str(year)) for year in years] slope, intercept, r_value, p_value, std_err = scipy.stats.linregress( np.asarray(years), np.asarray(weights) ) composite.put_immutable(self.weight_slope_var, slope) composite.put_immutable(self.weight_pval_var, p_value)
def alter(self, composite_id: str, composite: Composite) -> None: value: Optional[int] = composite.get_immutable( self.source, treat_missing_as_null=True) if value is None: return assert value in self.value_to_quantile, "Value {} did not get recorded in value-to-quantile map".format( value) quantile: float = self.value_to_quantile[value] composite.put_immutable(self.target, quantile)
def alter(self, composite_id: str, composite: Composite) -> None: try: value: Any = composite.get_immutable(self.source) except MissingDataError: return assert value in self.ranks, "Value {} did not get recorded in value-to-rank map".format( value) rank: int = self.ranks[value] composite.put_immutable(self.target, rank)
def __call__(self, composite: Composite): template = "%s's favorite color is %s (%s). Over the observation period, %s gained %0.1f lbs." name: str = composite.get_immutable(self.person_name_var) color: str = composite.get_immutable(self.color_name_var) rgb: str = composite.get_immutable(self.rgb_var) weight_gain: float = composite.get_immutable(self.weight_gain_var) pronoun = self.get_pronoun(composite) sentence = template % (name, color, rgb, pronoun, weight_gain) composite.put_immutable(self.sentence_var, sentence)
def __call__(self, composite: Composite) -> None: total: float = 0.0 n: int = 0 for period in composite.periods: try: value: Optional[Any] = composite.get_observation( self.subject, period) if value is not None: total += float(value) n += 1 except MissingDataError: continue if n == 0: composite.put_immutable(self.target, None) else: avg: float = total / n composite.put_immutable(self.target, avg)
def __call__(self, composite: Composite) -> None: periods_to_consider: List[str] = [] if len(self.temporal_sources) > 0: periods_to_consider = sorted(composite.periods, reverse=True) if self.use_only_current and len(periods_to_consider) > 0: periods_to_consider = [periods_to_consider[0]] for source_var_id in self.sources: value: Any if source_var_id in self.temporal_sources: value = self._get_temporal_value(composite, source_var_id, periods_to_consider) else: value = self._get_immutable_value(composite, source_var_id) if value is not None: composite.put_immutable(self.target, value) return
def __call__(self, composite: Composite): logging.debug("Beginning CalculateWeightGain") periods = list(composite.periods) logging.debug("Observed the following periods: %s" % ", ".join(periods)) earliest = min(periods) latest = max(periods) earliest_weight = composite.get_observation(self.weight_var, earliest) logging.debug("Earliest weight: %0.2f" % earliest_weight) latest_weight = composite.get_observation(self.weight_var, latest) logging.debug("Latest weight: %0.2f" % latest_weight) # I know, should have called it "weight change." weight_gain = round(latest_weight - earliest_weight, 2) logging.debug("Weight gain: %0.2f" % weight_gain) composite.put_immutable(self.weight_gain_var, weight_gain) logging.debug("Finished CalculateWeightGain.")
def __call__(self, composite: Composite) -> None: limit: Optional[Any] = None limit_period: Optional[str] = POLYTROPOS_NA for period in composite.periods: try: value: Optional[Any] = composite.get_observation( self.subject, period) if value is not None and self._sets_new_limit(value, limit): limit = value limit_period = period except MissingDataError: continue if limit is not None: composite.put_immutable(self.target, limit) if self.period_id_target is not None: assert limit_period != POLYTROPOS_NA, "Non-null minimum or maximum found, yet no period identified?" composite.put_immutable(self.period_id_target, limit_period)
def __call__(self, composite: Composite) -> None: if self.immutable_source is not None: try: value: Optional[Any] = composite.get_immutable( self.immutable_source) if value is not None: composite.put_immutable(self.target, value) return except MissingDataError: pass to_consider: List[str] = sorted(composite.periods, reverse=True) if not self.use_older_periods and len(to_consider) > 0: to_consider = [to_consider[0]] for period in to_consider: try: value = composite.get_observation(self.temporal_source, period) if value is not None: composite.put_immutable(self.target, value) return except MissingDataError: continue
def alter(self, composite_id: str, composite: Composite) -> None: rank = self.ranked[composite_id] composite.put_immutable(self.bmi_rank_var, rank)
def __call__(self, composite: Composite): # IRL, you'd have to handle nulls, decide how to deal with temporal variables, etc. color_name = composite.get_immutable(self.color_name_var) rgb_value = self.lookups["color_names"][color_name] composite.put_immutable(self.rgb_var, rgb_value)
def test_put_immutable_overwrites(simple_composite: Composite): simple_composite.put_immutable("the_rgb_var", "F0F0F0") expected = "F0F0F0" actual = simple_composite.content["immutable"]["color_info"]["rgb_value"] assert actual == expected
def __call__(self, composite: Composite): person_name: str = composite.get_immutable(self.person_name_var) lc_name = person_name.lower() gender = self.lookups["genders"][lc_name] composite.put_immutable(self.gender_var, gender)