Beispiel #1
0
    def updated(self, **kwargs):
        """Return a (deep) copy of the state, updated from `kwargs`.

        This method has `dict.update` semantics with immutability of `sorted`.
        Currently an exception is the `debug` key, if it exists, for which a
        depth-unlimited recursive merge is executed.

        Example:

            >>> state = State()
            >>> state
            {}
            >>> newstate = state.updated(problem="test")
            >>> newstate
            {'problem': 'test'}
        """

        overwrite = lambda a,b: b

        # use a special merge strategy for `debug` (op=overwrite, max_depth=None)
        debug = merge(self.get('debug', {}), kwargs.get('debug', {}), op=overwrite)
        if debug:
            self.setdefault('debug', PliableDict())
            kwargs['debug'] = PliableDict(debug)
        return State(merge(self, kwargs, max_depth=1, op=overwrite))
def map_with_stats(fn, argset, verbose=False):
    """Run `fn` over a list of parameters (positional arguments) in `argset`,
    calculating stats of all values present in response of each run.
    """
    def add_listified(x, y):
        if not isinstance(x, list):
            x = [x]
        if not isinstance(y, list):
            y = [y]
        return x + y

    sums = {}
    for args in argset:
        measures = fn(*args)
        if verbose:
            print("Run with args %r measures:" % args, measures)
        sums = merge(sums, measures, add_listified, recurse_list=False)

    stats = {'meta': {'n_samples': len(argset)}}
    for k, v in sums.items():
        try:
            stats[k] = dict(total=sum(v),
                            mean=statistics.mean(v),
                            median=statistics.median(v),
                            stdev=statistics.stdev(v))
        except:
            print('failed for params:', argset[0])
            raise InvalidModelParam
    return stats
 def load_config(**kwargs):
     file_conf = dict(token='token')
     return merge(kwargs, file_conf, op=lambda a, b: a or b)
 def load_config(**kwargs):
     return merge(kwargs, conf, op=lambda a, b: a or b)
Beispiel #5
0
 def test_lists_of_equal_len_no_recurse(self):
     self.assertEqual(merge([1, 2], [3, 4]), [1, 2, 3, 4])
Beispiel #6
0
 def test_lists_no_recurse_in_dict(self):
     self.assertEqual(merge({'x': [1]}, {'x': [2]}), {'x': [1, 2]})
Beispiel #7
0
 def test_lists_of_equal_len_recurse(self):
     self.assertEqual(merge([1, 2], [3, 4], recurse_list=True), [4, 6])
Beispiel #8
0
 def test_lists_of_inequal_len_recurse(self):
     self.assertEqual(merge([1, 2], [3], recurse_list=True), [1, 2, 3])
Beispiel #9
0
 def test_dict_with_different_keysets(self):
     self.assertEqual(merge({"a": "x"}, {"b": "y"}), {"a": "x", "b": "y"})
Beispiel #10
0
 def test_dict_with_numeric_values(self):
     self.assertEqual(merge({"a": 1.2}, {"a": 2.3}), {"a": 3.5})
Beispiel #11
0
 def test_dict_with_string_values(self):
     self.assertEqual(merge({"a": "x"}, {"a": "y"}), {"a": "xy"})
Beispiel #12
0
 def test_leaf_str(self):
     self.assertEqual(merge("abc", "def"), "abcdef")
Beispiel #13
0
 def test_leaf_float_int(self):
     self.assertEqual(merge(1.2, 3), 4.2)
Beispiel #14
0
 def test_leaf_float(self):
     self.assertEqual(merge(1.2, 2.3), 3.5)
Beispiel #15
0
 def test_leaf_int(self):
     self.assertEqual(merge(1, 2), 3)