Ejemplo n.º 1
0
    def passes_period(self, composite: Composite, period: str) -> bool:
        try:
            value: Any = composite.get_observation(self.var_id, period)
        except MissingDataError:
            value = MISSING_VALUE

        return self.compares_case_insensitive(value)
Ejemplo n.º 2
0
 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)
Ejemplo n.º 3
0
 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)
Ejemplo n.º 4
0
 def temporals_pass(self, composite: Composite, period: str) -> bool:
     for var_id in self.temporal_vars:
         try:
             actual: Optional[Any] = composite.get_observation(var_id, period)
         except MissingDataError:
             return False
         if actual != self.expected[var_id]:
             return False
     return True
Ejemplo n.º 5
0
    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.")
Ejemplo n.º 6
0
    def passes_temporal(self, composite: Composite) -> bool:
        periods: List = list(composite.periods)
        if len(periods) == 0:
            return self.parent.missing_value_passes()

        latest: str = max(periods)
        try:
            value = composite.get_observation(self.parent.var_id, latest)
        except MissingDataError:
            value = MISSING_VALUE
        return self.parent.compares_case_insensitive(value)
Ejemplo n.º 7
0
    def _get_temporal_value(self, composite: Composite,
                            source_var_id: VariableId,
                            periods: List[str]) -> Any:
        for period in periods:
            try:
                value = composite.get_observation(source_var_id, period)
                if value is not None:
                    return value
            except MissingDataError:
                continue

        return None
Ejemplo n.º 8
0
    def narrow(self, composite: Composite) -> None:
        if not self.narrows:
            return
        assert self.variable.temporal, "Attempting to narrow on immutable variable despite post-init check"

        for period in composite.periods:
            try:
                value: Any = composite.get_observation(self.var_id, period)
            except MissingDataError:
                value = MISSING_VALUE

            if not self.compares_case_insensitive(value):
                del composite.content[period]
Ejemplo n.º 9
0
    def passes_temporal(self, composite: Composite) -> bool:
        periods: List = list(composite.periods)
        if len(periods) == 0 and self.parent.missing_value_passes():
            return True

        for period in periods:
            try:
                value = composite.get_observation(self.parent.var_id, period)
            except MissingDataError:
                value = MISSING_VALUE
            if self.parent.compares_case_insensitive(value):
                return True

        return False
Ejemplo n.º 10
0
    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)
Ejemplo n.º 11
0
    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)
Ejemplo n.º 12
0
    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
Ejemplo n.º 13
0
 def _get_temporal_group(self, composite: Composite, period: str) -> Optional[str]:
     if self.temporal_grouping_var is None:
         return None
     return composite.get_observation(self.temporal_grouping_var, period, treat_missing_as_null=True)