def test_process_next_active(self):

        data = simpledataset()
        sup = simplesupervisor(data[1])

        agent = ag.ActiveAgent(0.5,
                               1,
                               ag.ObjectsMemory(),
                               ag.SupervisionMemory(),
                               simplemodel(),
                               sup,
                               bootstrap=2,
                               max_neigh_check=1,
                               add_seen_element=ag.add_seen_separate)

        output = [
            agent.process_next([data[0][itx]], data[2][itx])
            for itx in range(len(data[0]))
        ]

        asked_sup = np.array([o[2] for o in output])

        for itx in range(len(output)):
            with self.subTest(n=itx):
                if itx > 0:
                    self.assertTrue(output[itx][1] < itx)

        self.assertTrue(len(agent.sup_mem), asked_sup.sum())
    def test_predict(self):
        data = simpledataset()
        sup = simplesupervisor(data[1])

        agent = ag.ActiveAgent(0.5,
                               1,
                               ag.ObjectsMemory(),
                               ag.SupervisionMemory(),
                               simplemodel(),
                               sup,
                               bootstrap=2,
                               max_neigh_check=1,
                               add_seen_element=ag.add_seen_separate)

        output = [
            agent.process_next([data[0][itx]], data[2][itx])
            for itx in range(len(data[0]))
        ]

        predictions = [agent.predict([d])[1] for d in data[0]]
        is_known = [agent.predict([d])[0] for d in data[0]]

        npt.assert_equal(np.concatenate(predictions), data[2])

        all_pred = agent.predict(list(data[0]))

        npt.assert_equal(np.concatenate(is_known), all_pred[0])
        npt.assert_equal(np.concatenate(predictions), all_pred[1])

        npt.assert_equal(agent.predict(list(data[0])), all_pred)
    def test_refine(self):
        data = simpledataset()
        sup = simplesupervisor(data[1])

        def refine(agent):
            optim = torch.optim.sgd()
            l = loss.ContrastiveLoss()
            e = ag.create_siamese_trainer(agent, optim, l)
            sampler = samp.SeadableRandomSampler(agent.sup_mem, 1)
            data_loader = torch.utils.data.DataLoader(agent.sup_mem,
                                                      sampler=sampler)

            e.run(data_loader, max_epochs=2)

        agent = ag.Agent(1,
                         ag.ObjectsMemory(),
                         ag.SupervisionMemory(),
                         simplemodel(),
                         sup,
                         bootstrap=2,
                         max_neigh_check=1,
                         add_seen_element=ag.add_seen_separate,
                         refine=refine)

        for itx in range(len(data[0])):
            with self.subTest(n=itx):
                out = agent.process_next([data[0][itx]], data[2][itx])

                if itx < 2 and itx != 0:
                    self.assertTrue(out[2])
                self.assertEqual(len(agent.obj_mem), itx + 1)
                self.assertEqual(len(agent.sup_mem), itx)
    def test_process_next_internals(self):

        data = simpledataset()
        sup = simplesupervisor(data[1])

        agent = ag.Agent(1,
                         ag.ObjectsMemory(),
                         ag.SupervisionMemory(),
                         simplemodel(),
                         sup,
                         bootstrap=2,
                         max_neigh_check=1,
                         add_seen_element=ag.add_seen_separate)

        for itx in range(len(data[0])):
            with self.subTest(n=itx):
                out = agent.process_next([data[0][itx]], data[2][itx])

                if itx < 2 and itx != 0:
                    self.assertTrue(out[2])
                self.assertEqual(len(agent.obj_mem), itx + 1)
                self.assertEqual(len(agent.sup_mem), itx)

        data = simpledataset()
        sup = simplesupervisor(data[1])
    def test_del_entry(self):
        m = ag.SupervisionMemory()

        d1, l1 = bogus_data(), ([0], [3.0])
        d2, l2 = bogus_data(), ([1], [0.5])
        d3, l3 = bogus_data(), ([0], [1.5])
        d4, l4 = bogus_data(), ([1], [1.0])

        m.add_entry(d1, *l1)
        m.add_entry(d2, *l2)
        m.add_entry(d3, *l3)
        m.add_entry(d4, *l4)

        m.del_entry()

        npt.assert_equal(m.labels, np.array([1, 1, 0]))
        npt.assert_equal(m.distances, np.array([0.5, 1.0, 1.5]))
        npt.assert_equal(m.insertion_orders, np.array([1, 3, 2]))

        npt.assert_equal(m.couples, [d2, d4, d3])
        self.assertEqual(len(m), 3)

        m.del_entry(1)

        npt.assert_equal(m.labels, np.array([1, 0]))
        npt.assert_equal(m.distances, np.array([0.5, 1.5]))
        npt.assert_equal(m.insertion_orders, np.array([1, 2]))

        npt.assert_equal(m.couples, [d2, d3])
        self.assertEqual(len(m), 2)
def af(seed, sup):
    torch.manual_seed(seed)
    agent = ag.Agent(seed,
                     ag.ObjectsMemory(),
                     ag.SupervisionMemory(),
                     simplemodel(),
                     sup,
                     bootstrap=2,
                     max_neigh_check=1,
                     add_seen_element=ag.add_seen_separate)

    return agent
    def test_supervision(self):
        data = simpledataset()
        sup = simplesupervisor(data[1])

        agent = ag.ActiveAgent(1.0,
                               1,
                               ag.ObjectsMemory(),
                               ag.SupervisionMemory(),
                               simplemodel(),
                               sup,
                               bootstrap=2,
                               max_neigh_check=1,
                               add_seen_element=ag.add_seen_separate)

        output = np.array([
            agent.process_next([data[0][itx]], data[2][itx])
            for itx in range(len(data[0]))
        ])

        self.assertTrue(output[1:, 2].any())

        agent = ag.ActiveAgent(0.01,
                               1,
                               ag.ObjectsMemory(),
                               ag.SupervisionMemory(),
                               simplemodel(),
                               sup,
                               bootstrap=2,
                               max_neigh_check=1,
                               add_seen_element=ag.add_seen_separate)

        output = np.array([
            agent.process_next([data[0][itx]], data[2][itx])
            for itx in range(len(data[0]))
        ])

        self.assertFalse(output[1:, 2].all())
    def test_getitem(self):
        m = ag.SupervisionMemory()

        d1, l1 = bogus_data(), ([0], [3.0])
        d2, l2 = bogus_data(), ([1], [0.5])
        d3, l3 = bogus_data(), ([0], [1.5])
        d4, l4 = bogus_data(), ([1], [1.0])

        m.add_entry(d1, *l1)
        m.add_entry(d2, *l2)
        m.add_entry(d3, *l3)
        m.add_entry(d4, *l4)

        item = m[2]

        npt.assert_equal(item, (d3, l3[0][0]))

        m.del_entry(1)

        item = m[1]

        npt.assert_equal(item, (d3, l3[0][0]))
    def test_process_next_out(self):

        data = simpledataset()
        sup = simplesupervisor(data[1])

        agent = ag.Agent(1,
                         ag.ObjectsMemory(),
                         ag.SupervisionMemory(),
                         simplemodel(),
                         sup,
                         bootstrap=2,
                         max_neigh_check=1,
                         add_seen_element=ag.add_seen_separate)

        output = [
            agent.process_next([data[0][itx]], data[2][itx])
            for itx in range(len(data[0]))
        ]

        for itx in range(1, len(output)):
            with self.subTest(n=itx):
                self.assertTrue(output[itx][1] < itx)
                self.assertTrue(output[itx][2])