Пример #1
0
def assign_bmi_by_year(person: Dict) -> None:
    height = nesteddicts.get(person, ["immutable", "height"])
    years = set(person.keys()) - {"immutable"}
    h_squared = height**2
    for year in years:
        weight = nesteddicts.get(person, [year, "weight"])
        bmi = weight / h_squared * 703
        nesteddicts.put(person, [year, "bmi"], bmi)
Пример #2
0
    def __call__(
            self,
            content: Dict) -> Iterator[Tuple[Optional[Any], Optional[str]]]:
        subjects: Dict[str, Dict] = nesteddicts.get(content,
                                                    self.subjects_path,
                                                    default=None)
        if subjects is None:
            return

        for identifier, subject in subjects.items():
            try:
                value: Optional[Any] = nesteddicts.get(subject, self.arg_path)
            except MissingDataError:
                continue
            yield value, identifier
Пример #3
0
    def decode_named_list(self, mappings: Dict[VariableId, str],
                          content: Dict) -> Dict:
        """Convert a schema-compliant version of a named list of dicts into some other format.
        :param mappings: A mapping between the variables and their string values.
        :param content: The content in the schema format.
        """
        path_mappings: Dict[VariableId, List[str]] = {}
        for var_id in mappings.keys():
            var: Variable = self.schema.get(var_id)
            if var is None:
                raise ValueError('Unrecognized variable ID "%s"' % var_id)
            path_mappings[var_id] = list(var.relative_path)

        ret: Dict = {}
        for key, list_item in content.items():
            decoded = {}
            for var_id, path in path_mappings.items():
                try:
                    value = nesteddicts.get(list_item, path)
                except MissingDataError:
                    continue
                internal_key = mappings[var_id]
                decoded[internal_key] = value
            ret[key] = decoded
        return ret
Пример #4
0
 def _single(self, content: Dict) -> None:
     raw: Optional[Any] = nesteddicts.get(content,
                                          self.source_path,
                                          default=None)
     if raw is None:
         return
     display: str = self.display_format(raw)
     nesteddicts.put(content, self.target_path, display)
Пример #5
0
 def _list(self, content: Dict) -> None:
     try:
         source_list: List[Dict] = nesteddicts.get(content,
                                                   self._list_base_path)
         for entry in source_list:
             self._single(entry)
     except MissingDataError:
         return
Пример #6
0
 def _handle_explicit_na(self, data_type: str, a_tree: Dict, path: ListType) -> None:
     a_val: Optional[Any] = nesteddicts.get(a_tree, path, default=POLYTROPOS_CONFIRMED_NA)
     if a_val == POLYTROPOS_NA:
         raise ValueError("Actual value contained ostensibly non-occurring sentinel value %s" % POLYTROPOS_NA)
     if a_val == POLYTROPOS_CONFIRMED_NA:
         self._record_match(path, data_type, POLYTROPOS_NA)
     else:
         self._record_mismatch(path, data_type, POLYTROPOS_NA, a_val)
Пример #7
0
 def _handle_explicit_none(self, data_type: str, a_tree: Dict,
                           path: ListType) -> None:
     a_val: Optional[Any] = nesteddicts.get(a_tree,
                                            path,
                                            default=POLYTROPOS_CONFIRMED_NA)
     if a_val is None:
         self._record_match(path, data_type, None)
     else:
         self._record_mismatch(path, data_type, None, a_val)
Пример #8
0
 def _keyed_list(self, content: Dict) -> None:
     try:
         source_klist: Dict[str,
                            Dict] = nesteddicts.get(content,
                                                    self._list_base_path)
         for entry in source_klist.values():
             self._single(entry)
     except MissingDataError:
         return
Пример #9
0
 def _inspect_primitive(self, data_type: str, f_val: Optional[Any], a_tree: Dict, path: ListType) -> None:
     assert f_val != POLYTROPOS_NA  # Should have been handled at _inspect
     a_val: Optional[Any] = nesteddicts.get(a_tree, path, default=POLYTROPOS_CONFIRMED_NA)
     if a_val == POLYTROPOS_CONFIRMED_NA:
         self._record_missing(path, data_type, f_val)
     elif compare_primitives(f_val, a_val):
         self._record_match(path, data_type, f_val)
     else:
         self._record_mismatch(path, data_type, f_val, a_val)
Пример #10
0
 def __call__(
         self,
         content: Dict) -> Iterator[Tuple[Optional[Any], Optional[str]]]:
     for identifier, argument_path in self.subjects.items():
         try:
             value: Optional[Any] = nesteddicts.get(content, argument_path)
         except MissingDataError:
             continue
         yield value, identifier
Пример #11
0
 def get_all_observations(self, var_id: str) -> Iterator[Tuple[str, Any]]:
     """Iterate over all observations of a temporal variable from this composite."""
     var = self.as_var(var_id, track_type=TrackType.TEMPORAL)
     var_path: List = list(var.absolute_path)
     for period in self.periods:
         try:
             yield period, nesteddicts.get(self.content,
                                           [period] + var_path)
         except MissingDataError:
             continue
Пример #12
0
    def __call__(
            self,
            content: Dict) -> Iterator[Tuple[Optional[Any], Optional[str]]]:
        subjects: ListType[Dict] = nesteddicts.get(content,
                                                   self.subjects_path,
                                                   default=None)
        if subjects is None:
            return

        for subject in subjects:
            try:
                value: Optional[Any] = nesteddicts.get(subject, self.arg_path)
            except MissingDataError:
                continue
            identifier: Optional[str] = POLYTROPOS_NA
            if self.identifier_path is not None:
                identifier = nesteddicts.get(subject,
                                             self.identifier_path,
                                             default=POLYTROPOS_NA)
            yield value, identifier
Пример #13
0
def assign_regression_stats(person: Dict) -> None:
    years = set(person.keys()) - {"immutable"}
    years_ordered = sorted([int(year) for year in years])
    weights = [
        nesteddicts.get(person, [str(year), "weight"])
        for year in years_ordered
    ]
    slope, intercept, r_value, p_value, std_err = scipy.stats.linregress(
        years_ordered, weights)
    nesteddicts.put(person, ["immutable", "weight_change", "slope"], slope)
    nesteddicts.put(person, ["immutable", "weight_change", "p_value"], p_value)
Пример #14
0
    def _inspect_complex(self, data_type: str, f_val: Optional[Any], a_tree: Dict, path: ListType) -> None:
        assert f_val != POLYTROPOS_NA  # Should have been handled at _inspect
        a_val: Optional[Any] = nesteddicts.get(a_tree, path, default=POLYTROPOS_CONFIRMED_NA)
        if a_val == POLYTROPOS_CONFIRMED_NA:
            self._record_missing(path, data_type, json.dumps(f_val, sort_keys=True))
            return

        compare: CompareComplexVariable = CompareComplexVariable(self.schema)
        if compare(f_val, a_val, path=path):
            self._record_match(path, data_type, json.dumps(f_val, sort_keys=True))
        else:
            self._record_mismatch(path, data_type, json.dumps(f_val, sort_keys=True), json.dumps(a_val, sort_keys=True))
Пример #15
0
 def get_observation(self,
                     var_id: VariableId,
                     period: str,
                     treat_missing_as_null: bool = False) -> Optional[Any]:
     """Get the value of a temporal variable for a particular observation period."""
     var = self.as_var(var_id, track_type=TrackType.TEMPORAL)
     var_path: List = list(var.absolute_path)
     try:
         return nesteddicts.get(self.content, [period] + var_path)
     except MissingDataError as e:
         if treat_missing_as_null:
             return None
         raise e
Пример #16
0
    def get_immutable(self,
                      var_id: VariableId,
                      treat_missing_as_null: bool = False) -> Optional[Any]:
        """Get an immutable variable from this composite."""
        var = self.as_var(var_id, track_type=TrackType.IMMUTABLE)

        path = ["immutable"] + list(var.absolute_path)
        try:
            return nesteddicts.get(self.content, path)
        except MissingDataError as e:
            if treat_missing_as_null:
                return None
            raise e
Пример #17
0
    def delete_multiple(self, var: Variable, period_content: Dict) -> None:
        list_base_var: Variable = self.schema.get(var.nearest_list)
        assert not list_base_var.descends_from_list, "Nested list handling not implemented"
        try:
            content: Union[Dict,
                           List] = nesteddicts.get(period_content,
                                                   list_base_var.absolute_path)
            if content is None:
                return
        except MissingDataError:
            return

        for element in _elements_of(content):  # type: Dict
            try:
                nesteddicts.delete(element, var.relative_path)
            except MissingDataError:
                continue
Пример #18
0
    def _add_one_to_many(self, ret: Dict, tree: Dict, block: Tuple) -> None:
        root_var: GenericList = self._get_root_var(block)
        root_path: ListType = list(root_var.relative_path)
        try:
            subtree: Union[ListType, Dict] = nesteddicts.get(tree, root_path)
        except nesteddicts.MissingDataError:
            return

        if root_var.data_type == "List":
            subtree = cast(ListType, subtree)
            ret[root_var.var_id] = list(self._handle_list(subtree, block[1:]))
        elif root_var.data_type == "KeyedList":
            subtree = cast(Dict, subtree)
            ret[root_var.var_id] = self._handle_keyed_list(subtree, block[1:])
        else:
            raise ValueError(
                'Unexpected data type "{}" for putative root variable {}'.
                format(root_var.data_type, root_var.var_id))
Пример #19
0
def test_get_not_nested(default: Optional[str]):
    spec: List[str] = ["a", "b", "c"]
    data: Dict = {"a": {"b": "I'm supposed to be nested, but I'm not"}}
    with raises(nesteddicts.IncompleteNestingError):
        nesteddicts.get(data, spec, default=default)
Пример #20
0
def _do_get_test(data: Dict,
                 spec: List[str],
                 expected: Optional[str] = "expected",
                 **kwargs):
    actual: Any = nesteddicts.get(data, spec, **kwargs)
    assert actual == expected
Пример #21
0
def assign_mean_bmi(person: Dict) -> None:
    years = set(person.keys()) - {"immutable"}
    bmis = [nesteddicts.get(person, [year, "bmi"]) for year in years]
    mean_bmi = numpy.average(bmis)
    nesteddicts.put(person, ["immutable", "mean_bmi"], mean_bmi)
Пример #22
0
 def _one_to_one(self, tree: Dict, var_id: VariableId) -> Optional[Any]:
     var: Variable = self.composite.schema.get(var_id)
     if var is None:
         raise ValueError('Unknown variable ID "%s"' % var_id)
     path: ListType[str] = list(var.relative_path)
     return nesteddicts.get(tree, path)
Пример #23
0
def test_empty_spec_returns_self():
    data: Dict = {"a": {"b": {"c": "expected"}}}
    spec: List[str] = []
    expected: Dict = data
    actual: Any = nesteddicts.get(data, spec)
    assert actual == expected