コード例 #1
0
    def setUp(self):
        self.n = 60
        self.names = ["obj.y"]
        self.rng = np.random.default_rng(0)
        self.y = self.rng.normal(size=self.n)

        self.monitor = AttributeMonitor(self.names)
        self.monitor.setup(self.n)
コード例 #2
0
    def test_monitor_history_is_same_when_chunk_hint_changes(self):
        monitor = AttributeMonitor(["output_"])
        self.pred.transform(self.x, self.y, chunk_hint=11, monitor=monitor)

        monitor_alt = AttributeMonitor(["output_"])
        self.pred.transform(self.x, self.y, chunk_hint=11, monitor=monitor_alt)

        np.testing.assert_allclose(
            monitor.history_.output_, monitor_alt.history_.output_
        )
コード例 #3
0
    def setUp(self):
        self.keys = [
            "x", "v", "hi_d", "i", "i_float", "b", "s", "obj", "i_again"
        ]
        self.monitor = AttributeMonitor(self.keys)

        self.rng = np.random.default_rng(2)
        self.x_data = [1.0, 2, 0.5, -1, 4, 0, 1, 2.1, -5.0, 3.2, 0.0, 0.5]
        self.n = len(self.x_data)
        self.v_data = self.rng.normal(size=(self.n, 5))
        self.hi_d_data = self.rng.normal(size=(self.n, 5, 3, 2, 4))
        self.i_data = np.arange(self.n)
        self.i_float_data = np.arange(self.n, 0, -1).astype(float)
        self.b_data = np.repeat([True, False], self.n // 2)
        self.s_data = np.repeat(["foo", "bar", "goo"], self.n // 3)
        self.obj_data = self.n * [object()]
        self.i_again_data = [1.0] + [2] * (self.n - 1)

        class ToTrack(object):
            def __init__(self):
                self.x = 0.0
                self.v = 0
                self.hi_d = np.array([])
                self.i = 0
                self.i_float = 0.0
                self.b = False
                self.s = ""
                self.obj: object = None
                self.i_again = 0

        self.obj = ToTrack()
        self.monitor.setup(self.n)

        for x, v, hi_d, i, i_float, b, s, obj, i_again in zip(
                self.x_data,
                self.v_data,
                self.hi_d_data,
                self.i_data,
                self.i_float_data,
                self.b_data,
                self.s_data,
                self.obj_data,
                self.i_again_data,
        ):
            self.obj.x = x
            self.obj.v = v
            self.obj.hi_d = hi_d
            self.obj.i = i
            self.obj.i_float = i_float
            self.obj.b = b
            self.obj.s = s
            self.obj.obj = obj
            self.obj.i_again = i_again

            self.monitor.record(self.obj)
コード例 #4
0
    def test_history_same_when_chunk_hint_changes(self):
        names = ["coef_", "var_"]
        monitor = AttributeMonitor(names)

        self.xcorr.transform(self.x, self.y, monitor=monitor, chunk_hint=13)

        xcorr_alt = OnlineCrosscorrelation(self.n_features, rate=self.rate)
        monitor_alt = AttributeMonitor(names)
        xcorr_alt.transform(self.x, self.y, monitor=monitor_alt, chunk_hint=2)

        np.testing.assert_allclose(monitor.history_.coef_,
                                   monitor_alt.history_.coef_)
        np.testing.assert_allclose(monitor.history_.var_,
                                   monitor_alt.history_.var_)
コード例 #5
0
    def test_chunk_hint_does_not_affect_monitoring(self):
        names = ["input_", "output_"]
        monitor1 = AttributeMonitor(names)
        self.arma.transform(X=self.source_data,
                            monitor=monitor1,
                            chunk_hint=11)

        monitor2 = AttributeMonitor(names)
        arma2 = Arma(self.a, self.b)
        arma2.transform(X=self.source_data, monitor=monitor2, chunk_hint=23)

        np.testing.assert_allclose(monitor1.history_.input_,
                                   monitor2.history_.input_)
        np.testing.assert_allclose(monitor1.history_.output_,
                                   monitor2.history_.output_)
コード例 #6
0
    def test_monitor_fit(self):
        names = ["weights_", "lateral_", "output_"]
        monitor = AttributeMonitor(names)

        n_samples = 100

        rng = np.random.default_rng(1)
        x = rng.normal(size=(n_samples, self.n_features))

        self.circuit.transform(x, monitor=monitor)

        circuit1 = NonRecurrent(rng=self.seed, **self.kwargs)
        weights = []
        lateral = []
        output = []
        for crt_x in x:
            weights.append(np.copy(circuit1.weights_))
            lateral.append(np.copy(circuit1.lateral_))
            output.append(np.copy(circuit1.output_))

            circuit1.transform([crt_x])

        np.testing.assert_allclose(monitor.history_.weights_, weights)
        np.testing.assert_allclose(monitor.history_.lateral_, lateral)
        np.testing.assert_allclose(monitor.history_.output_, output)
コード例 #7
0
    def test_monitor_history_is_returned_when_return_history_is_true(self):
        monitor = AttributeMonitor(["output_"])
        _, history = self.pred.transform(
            self.x, self.y, monitor=monitor, return_history=True
        )

        self.assertIs(history, monitor.history_)
コード例 #8
0
    def test_history_returned_when_return_history_is_true(self):
        monitor = AttributeMonitor(["xcorr.coef_"])
        _, history = self.xcorr.transform(
            self.x, self.y, monitor=monitor, return_history=True
        )

        self.assertIs(history, monitor.history_)
コード例 #9
0
    def test_var_history_matches_var(self):
        self.xcorr.transform(self.x[:-1], self.y[:-1])

        last_var = self.xcorr.var_
        monitor = AttributeMonitor(["var_"])
        self.xcorr.transform(self.x[[-1]], self.y[[-1]], monitor=monitor)

        self.assertAlmostEqual(monitor.history_.var_[0], last_var)
コード例 #10
0
    def test_when_monitor_is_object_history_returned_is_its_attrib(self):
        names = ["weights_"]
        monitor = AttributeMonitor(names)
        _, history = self.wta.transform(
            self.predictors, self.dependent, monitor=monitor, return_history=True
        )

        self.assertIs(history, monitor.history_)
コード例 #11
0
    def test_monitor_dispatches_attributes_according_to_trafo_name(self):
        monitor = AttributeMonitor(["trafo0.input_", "trafo1.input_"])
        self.chain.transform(X=self.x0, monitor=monitor)

        for trafo in self.chain._transformers[:-1]:
            self.assertEqual(1, len(trafo.transform.call_args[1]["monitor"].names))
            self.assertIn("input_", trafo.transform.call_args[1]["monitor"].names)

        self.assertNotIn("monitor", self.chain._transformers[-1].transform.call_args[1])
コード例 #12
0
    def test_result_is_the_same_with_and_without_monitor(self):
        monitor = AttributeMonitor(["xcorr.var_"])
        res = self.xcorr.transform(self.x, self.y, monitor=monitor)

        xcorr_alt = CrosscorrelationRegressor(
            self.n_models, self.n_features, rng=self.seed
        )
        res_alt = xcorr_alt.transform(self.x, self.y)

        np.testing.assert_allclose(res, res_alt)
コード例 #13
0
    def test_output_history_same_if_rate_is_constant_then_switches(self):
        schedule = np.zeros(self.n_samples)
        schedule[:self.n_partial] = self.rate
        circuit = NonRecurrent(rate=schedule, **self.kwargs)

        monitor = AttributeMonitor(["output_"])
        circuit.transform(self.x, monitor=monitor)
        np.testing.assert_allclose(
            monitor.history_.output_[:self.n_partial],
            self.monitor_partial.history_.output_,
        )
コード例 #14
0
    def test_monitor_as_object(self):
        names = ["weights_"]
        monitor = AttributeMonitor(names)
        self.wta.transform(self.predictors, self.dependent, monitor=monitor)

        wta_alt = BioWTARegressor(self.n_models, self.n_features)
        _, history_alt = wta_alt.transform(
            self.predictors, self.dependent, monitor=names
        )

        np.testing.assert_allclose(monitor.history_.weights_, history_alt.weights_)
コード例 #15
0
    def test_history_same_when_chunk_hint_changes(self):
        names = ["weights_", "lateral_", "output_"]
        monitor = AttributeMonitor(names)

        n_samples = 100

        rng = np.random.default_rng(1)
        x = rng.normal(size=(n_samples, self.n_features))

        self.circuit.transform(x, monitor=monitor, chunk_hint=13)

        circuit_alt = NonRecurrent(rng=self.seed, **self.kwargs)
        monitor_alt = AttributeMonitor(names)
        circuit_alt.transform(x, monitor=monitor_alt, chunk_hint=2)

        np.testing.assert_allclose(monitor.history_.weights_,
                                   monitor_alt.history_.weights_)
        np.testing.assert_allclose(monitor.history_.lateral_,
                                   monitor_alt.history_.lateral_)
        np.testing.assert_allclose(monitor.history_.output_,
                                   monitor_alt.history_.output_)
コード例 #16
0
    def test_monitor_var_correct(self):
        monitor = AttributeMonitor(["var_"])

        self.xcorr.transform(self.x, self.y, monitor=monitor)

        acorr_alt = OnlineCrosscorrelation(self.n_features, rate=self.rate)
        var_exp = np.zeros(self.n_samples)
        for i in range(self.n_samples):
            var_exp[i] = acorr_alt.var_
            acorr_alt.transform(self.x[[i]], self.y[[i]])

        np.testing.assert_allclose(monitor.history_.var_, var_exp)
コード例 #17
0
class TestAttributeMonitorHierarchical(unittest.TestCase):
    def setUp(self):
        self.n = 60
        self.names = ["obj.y"]
        self.rng = np.random.default_rng(0)
        self.y = self.rng.normal(size=self.n)

        self.monitor = AttributeMonitor(self.names)
        self.monitor.setup(self.n)

    def test_record_works_correctly(self):
        for i in range(self.n):
            tracked = SimpleNamespace(obj=SimpleNamespace(y=self.y[i]))
            self.monitor.record(tracked)

        self.assertTrue(hasattr(self.monitor.history_, "obj"))
        self.assertTrue(hasattr(self.monitor.history_.obj, "y"))
        np.testing.assert_allclose(self.monitor.history_.obj.y, self.y)

    def test_record_batch_works_correctly(self):
        chunk_size = 13
        for i in range(0, self.n, chunk_size):
            tracked = SimpleNamespace(obj=SimpleNamespace(
                y=self.y[i:i + chunk_size]))
            self.monitor.record_batch(tracked)

        self.assertTrue(hasattr(self.monitor.history_, "obj"))
        self.assertTrue(hasattr(self.monitor.history_.obj, "y"))
        np.testing.assert_allclose(self.monitor.history_.obj.y, self.y)
コード例 #18
0
    def test_raises_value_error_if_not_all_variables_are_the_same_length(self):
        monitor_alt = AttributeMonitor(["x", "y"])
        monitor_alt.setup(3)

        obj = SimpleNamespace(x=np.zeros(3), y=[1])
        with self.assertRaises(ValueError):
            monitor_alt.record_batch(obj)
コード例 #19
0
class TestAttributeMonitorStrAndRepr(unittest.TestCase):
    def setUp(self):
        self.names = ["foo", "bar"]
        self.step = 3
        self.monitor = AttributeMonitor(self.names, self.step)

    def test_str(self):
        s_exp = (f"AttributeMonitor(names={str(self.names)}, n_=None, " +
                 f"step={self.step}, t_=None)")
        s = str(self.monitor)

        self.assertEqual(s, s_exp)

    def test_str_after_setup(self):
        n = 12
        self.monitor.setup(n)
        s_exp = (f"AttributeMonitor(names={str(self.names)}, n_={n}, " +
                 f"step={self.step}, t_=0)")
        s = str(self.monitor)

        self.assertEqual(s, s_exp)

    def test_repr(self):
        r_exp = (f"AttributeMonitor(names={repr(self.names)}, n_=None, " +
                 f"step={self.step}, t_=None, i_=None, history_=None)")
        r = repr(self.monitor)

        self.assertEqual(r, r_exp)

    def test_repr_after_setup(self):
        n = 12
        self.monitor.setup(n)
        exp_hist = SimpleNamespace()
        for name in self.names:
            setattr(exp_hist, name, None)
        r_exp = (f"AttributeMonitor(names={repr(self.names)}, n_={n}, " +
                 f"step={self.step}, t_=0, i_=0, history_={repr(exp_hist)})")
        r = repr(self.monitor)

        self.assertEqual(r, r_exp)
コード例 #20
0
class TestAttributeMonitorInit(unittest.TestCase):
    def setUp(self):
        self.keys = ["foo", "bar", "x_y", "y.x"]
        self.monitor = AttributeMonitor(self.keys)

    def test_constructor_default_step_is_one(self):
        self.assertEqual(self.monitor.step, 1)

    def test_setup_makes_history_keys_for_all_attributes_without_dots(self):
        self.monitor.setup(3)

        is_in_history = [
            key in self.monitor.history_.__dict__ for key in self.keys
            if "." not in key
        ]
        self.assertTrue(all(is_in_history))

    def test_setup_makes_none_histories_for_all_attributes_without_dots(self):
        self.monitor.setup(4)

        is_none = [
            self.monitor.history_.__dict__[key] is None for key in self.keys
            if "." not in key
        ]
        self.assertTrue(all(is_none))

    def test_setup_makes_hierarchical(self):
        self.monitor.setup(1)
        self.assertTrue(hasattr(self.monitor.history_, "y"))
        self.assertTrue(hasattr(self.monitor.history_.y, "x"))
コード例 #21
0
    def test_monitor_as_sequence(self):
        names = ["trafo0.output_", "trafo2.input_"]
        monitor = AttributeMonitor(names)
        self.chain.transform(X=self.x0, monitor=monitor)

        _, history = self.chain.transform(X=self.x0, monitor=names)

        np.testing.assert_allclose(
            monitor.history_.trafo0.output_, history.trafo0.output_
        )
        np.testing.assert_allclose(
            monitor.history_.trafo2.input_, history.trafo2.input_
        )
コード例 #22
0
    def test_monitor_nothing(self):
        monitor = AttributeMonitor([])
        monitor.setup(5)

        obj = SimpleNamespace()
        monitor.record_batch(obj)

        self.assertEqual(len(monitor.history_.__dict__), 0)
コード例 #23
0
    def setUp(self):
        self.n_features = 5
        self.n_components = 3
        self.seed = 3
        self.kwargs = dict(n_features=self.n_features,
                           n_components=self.n_components,
                           rng=self.seed)

        self.rng = np.random.default_rng(0)
        self.n_samples = 53
        self.x = self.rng.normal(size=(self.n_samples, self.n_features))

        self.rate = 0.005

        self.monitor_full = AttributeMonitor(["output_"])
        self.circuit_full = NonRecurrent(rate=self.rate, **self.kwargs)
        self.circuit_full.transform(self.x, monitor=self.monitor_full)

        self.n_partial = self.n_samples // 2
        self.monitor_partial = AttributeMonitor(["output_"])
        self.circuit_partial = NonRecurrent(rate=self.rate, **self.kwargs)
        self.circuit_partial.transform(self.x[:self.n_partial],
                                       monitor=self.monitor_partial)
コード例 #24
0
    def test_monitor_nothing(self):
        monitor = AttributeMonitor([])
        n = 10
        monitor.setup(n)

        for i in range(n):
            obj = SimpleNamespace()
            monitor.record(obj)

        self.assertEqual(len(monitor.history_.__dict__), 0)
コード例 #25
0
    def test_monitor_as_sequence(self):
        names = ["xcorr.var_", "nsm.weights_"]
        _, history = self.xcorr.transform(
            self.x, self.y, monitor=names, return_history=True
        )

        monitor = AttributeMonitor(names)
        xcorr_alt = CrosscorrelationRegressor(
            self.n_models, self.n_features, rng=self.seed
        )
        _, history_alt = xcorr_alt.transform(
            self.x, self.y, monitor=monitor, return_history=True
        )

        np.testing.assert_allclose(history.xcorr.var_, history_alt.xcorr.var_)
        np.testing.assert_allclose(history.nsm.weights_, history_alt.nsm.weights_)
コード例 #26
0
    def test_correct(self):
        x_data = [1.0, 2, 0.5, -1, 4, 0, 1, 2.1, -5.0, 3.2, 0.0]
        step = 3

        monitor = AttributeMonitor(["x"], step=step)
        monitor.setup(len(x_data))

        for x in x_data:
            obj = SimpleNamespace(x=x)
            monitor.record(obj)

        np.testing.assert_allclose(monitor.history_.x, x_data[::step])
コード例 #27
0
    def test_store_copies_objects(self):
        class ToTrack(object):
            def __init__(self):
                self.x = None

        obj = ToTrack()

        x_data = [None, [1, 2, 3]]

        monitor = AttributeMonitor(["x"])
        monitor.setup(len(x_data))

        for x in x_data:
            obj.x = x
            monitor.record(obj)

        x0 = list(x_data[1])
        x_data[1][0] = 0

        np.testing.assert_allclose(monitor.history_.x[1], x0)
コード例 #28
0
    def test_one_by_one_same_as_batch_when_step_is_one(self):
        monitor_alt = AttributeMonitor(self.names)
        monitor_alt.setup(self.n)

        rng = np.random.default_rng(0)
        v = rng.normal(size=self.n)

        for i in range(self.n):
            obj = SimpleNamespace(x=v[i])
            self.monitor.record(obj)

        i = 0
        while i < self.n:
            k = min(rng.integers(1, 10), self.n - i)
            crt_v = v[i:i + k]
            obj_batch = SimpleNamespace(x=crt_v)
            monitor_alt.record_batch(obj_batch)

            i += k

        np.testing.assert_allclose(self.monitor.history_.x,
                                   monitor_alt.history_.x)
コード例 #29
0
    def test_monitor_output(self):
        monitor = AttributeMonitor(["output_"])
        norms = self.variance.transform(X=self.x, monitor=monitor)

        np.testing.assert_allclose(monitor.history_.output_, norms)
コード例 #30
0
    def test_monitor_coef_same_as_return_from_transform(self):
        monitor = AttributeMonitor(["coef_"])

        res = self.xcorr.transform(self.x, self.y, monitor=monitor)
        np.testing.assert_allclose(res, monitor.history_.coef_)