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 passes(self, composite: Composite) -> bool: try: value: int = composite.get_immutable(self.threshold_var) except MissingDataError: return False return value >= self.threshold
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 immutables_pass(self, composite: Composite) -> bool: for var_id in self.immutable_vars: try: actual: Optional[Any] = composite.get_immutable(var_id) except MissingDataError: return False if actual != self.expected[var_id]: return False return True
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 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 __call__(self, composite: Composite) -> bool: if not self.parent.variable.temporal: try: value: Any = composite.get_immutable(self.parent.var_id) except MissingDataError: value = MISSING_VALUE return self.parent.compares_case_insensitive(value) return self.passes_temporal(composite)
def get_pronoun(self, composite: Composite): gender: str = composite.get_immutable(self.gender_var) pronoun_mapping = { "male": "he", "female": "she" } return pronoun_mapping[gender]
def _get_immutable_value(self, composite: Composite, source_var_id: VariableId) -> Any: try: value = composite.get_immutable(source_var_id) if value is not None: return value except MissingDataError: pass return None
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 passes(self, composite: Composite) -> bool: male: bool = composite.get_immutable(self.male_flag) return not male
def extract(self, composite: Composite) -> float: mean_bmi = composite.get_immutable(self.mean_bmi_var) return mean_bmi
def __call__(self, composite: Composite): h_squared = composite.get_immutable(self.height_var) ** 2 for period, weight in composite.get_all_observations(self.annual_weight_var): bmi = weight / h_squared * 703 composite.put_observation(self.annual_bmi_var, period, bmi)
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 extract(self, composite: Composite) -> Optional[int]: return composite.get_immutable(self.source, treat_missing_as_null=True)
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)
def extract(self, composite: Composite) -> Tuple[bool, float]: mean_bmi = composite.get_immutable(self.mean_bmi_var) is_male = composite.get_immutable(self.male_flag) return is_male, mean_bmi
def test_get_immutable(simple_composite: Composite): expected: str = "FFA000" actual: str = simple_composite.get_immutable("the_rgb_var") assert actual == expected
def _get_immutable_group(self, composite: Composite) -> Optional[str]: if self.immutable_grouping_var is None: return None return composite.get_immutable(self.immutable_grouping_var, treat_missing_as_null=True)