bmritz / datajet

A Data Dependency Graph Framework and Executor
https://bmritz.github.io/datajet/
MIT License
2 stars 1 forks source link

Allow for a "constant" dict in a DataMap or in a context #27

Closed bmritz closed 2 years ago

bmritz commented 2 years ago
In [983]: from bisect import bisect_right
     ...: from statistics import mean
     ...:
     ...: def find_le(a, x, default=None):
     ...:     'Find rightmost value of `a` less than or equal to `x`'
     ...:     i = bisect_right(a, x)
     ...:     if i:
     ...:         return a[i-1]
     ...:     return default
     ...:
     ...: data_map = {
     ...:     "exam_grades": [{"in": [], "f": lambda: [98, 73, 65, 95, 88, 58, 40, 94]}],
     ...:     "letter_grade_cutoffs": [{"in": [], "f": lambda: {90: "A", 80: "B", 70: "C", 60: "D"}}],
     ...:     "lowest_grade": [{"in": [], "f": lambda: "F"}],
     ...:     "passing_grades": [{"in": [], "f": lambda: set(["A", "B", "C", "D"])}],
     ...:     "exam_letter_grades": lambda exam_grades, letter_grade_cutoffs, lowest_grade: [
     ...:         letter_grade_cutoffs.get(find_le(sorted(letter_grade_cutoffs), grade), lowest_grade)
     ...:         for grade in exam_grades
     ...:     ],
     ...:     "exam_pass_fail_grades": lambda passing_grades, exam_letter_grades: [grade in passing_grades for grade in exam_letter_grades],
     ...:     "pct_passing": lambda exam_pass_fail_grades: mean(exam_pass_fail_grades),
     ...: }

In [984]:

In [984]: execute(data_map, fields=['exam_pass_fail_grades', 'pct_passing'])
Out[984]:
{'exam_pass_fail_grades': [True, True, True, True, True, False, False, True],
 'pct_passing': 0.75}

In [985]: execute(data_map, context={"letter_grade_cutoffs": {60: "Pass"}, "passing_grades": "Pass"}, fields=['exam_pass_fail_grades', 'pct_passing'])
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Input In [985], in <cell line: 1>()
----> 1 execute(data_map, context={"letter_grade_cutoffs": {60: "Pass"}, "passing_grades": "Pass"}, fields=['exam_pass_fail_grades', 'pct_passing'])

File ~/projects/datajet/datajet/datajet.py:25, in execute(data_map, fields, context)
     22     data_map = copy.copy(data_map)
     23     data_map.update(context)
---> 25 data_map = _normalize_data_map(data_map)
     26 if not _is_valid_normalized_data_map(data_map):
     27     msg = _normalized_data_map_validation_error(data_map)

File ~/projects/datajet/datajet/_normalization.py:56, in _normalize_data_map(data_map)
     55 def _normalize_data_map(data_map: dict) -> dict:
---> 56     return {k: _norm(v) for k, v in data_map.items()}

File ~/projects/datajet/datajet/_normalization.py:56, in <dictcomp>(.0)
     55 def _normalize_data_map(data_map: dict) -> dict:
---> 56     return {k: _norm(v) for k, v in data_map.items()}

File ~/projects/datajet/datajet/_normalization.py:34, in _norm(v)
     32     return [{"in": _get_list_of_input_variables_from_function(v), "f": v}]
     33 if isinstance(v, dict):
---> 34     return [{"in": v.get("in", _get_list_of_input_variables_from_function(v["f"])), "f": v["f"]}]
     35 if isinstance(v, list):
     36     if v == []:

KeyError: 'f'
bmritz commented 2 years ago

Closed by

bmritz commented 2 years ago

closed by https://github.com/bmritz/datajet/commit/e64e1c012fa5b528a1ee73a8e95ee88cdd9cfbf4

bmritz commented 2 years ago

closed by https://github.com/bmritz/datajet/commit/e64e1c012fa5b528a1ee73a8e95ee88cdd9cfbf4