MCPcopy Create free account
hub / github.com/microsoft/TRELLIS / dict_reduce

Function dict_reduce

trellis/utils/general_utils.py:53–71  ·  view source on GitHub ↗

Reduce a list of dictionaries. Leaf values must be scalars.

(dicts, func, special_func={})

Source from the content-addressed store, hash-verified

51
52
53def dict_reduce(dicts, func, special_func={}):
54 """
55 Reduce a list of dictionaries. Leaf values must be scalars.
56 """
57 assert isinstance(dicts, list), 'input must be a list of dictionaries'
58 assert all([isinstance(d, dict) for d in dicts]), 'input must be a list of dictionaries'
59 assert len(dicts) > 0, 'input must be a non-empty list of dictionaries'
60 all_keys = set([key for dict_ in dicts for key in dict_.keys()])
61 reduced_dict = {}
62 for key in all_keys:
63 vlist = [dict_[key] for dict_ in dicts if key in dict_.keys()]
64 if isinstance(vlist[0], dict):
65 reduced_dict[key] = dict_reduce(vlist, func, special_func)
66 else:
67 if key in special_func.keys():
68 reduced_dict[key] = special_func[key](vlist)
69 else:
70 reduced_dict[key] = func(vlist)
71 return reduced_dict
72
73
74def dict_any(dic, func):

Callers 4

runMethod · 0.85
run_stepMethod · 0.85
run_snapshotMethod · 0.85
run_snapshotMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected