Ejemplo n.º 1
0
    def test_nesting_2(self):
        loop = Loop(self.p1[1:3:1]).each(
            self.p1,
            Loop(self.p2[3:5:1]).each(
                self.p1, self.p2,
                Loop(self.p3[5:7:1]).each(self.p1, self.p2, self.p3)))

        data = loop.run_temp()
        keys = set(data.arrays.keys())

        self.assertEqual(data.p1_set.tolist(), [1, 2])
        self.assertEqual(data.p2_set.tolist(), [[3, 4]] * 2)
        self.assertEqual(data.p3_set.tolist(), [[[5, 6]] * 2] * 2)

        self.assertEqual(data.p1_0.tolist(), [1, 2])

        # TODO(alexcjohnson): these names are extra confusing...
        # perhaps we should say something like always include *all* indices
        # unless you can get rid of them all (ie that param only shows up
        # once, but separately for set and measured)
        self.assertEqual(data.p1_1_0.tolist(), [[1, 1], [2, 2]])
        self.assertEqual(data.p2_1.tolist(), [[3, 4]] * 2)

        self.assertEqual(data.p1_1_2_0.tolist(), [[[1, 1]] * 2, [[2, 2]] * 2])
        self.assertEqual(data.p2_2_1.tolist(), [[[3, 3], [4, 4]]] * 2)
        self.assertEqual(data.p3.tolist(), [[[5, 6]] * 2] * 2)

        # make sure rerunning this doesn't cause any problems
        data2 = loop.run_temp()
        keys2 = set(data.arrays.keys())
        self.assertEqual(keys, keys2)
Ejemplo n.º 2
0
    def test_enqueue(self):
        c1 = self.gates.chan1
        loop = Loop(c1[1:5:1], 0.01).each(c1)
        data1 = loop.run(location=self.location,
                         quiet=True,
                         background=True,
                         data_manager=True)

        # second running of the loop should be enqueued, blocks until
        # the first one finishes.
        # TODO: check what it prints?
        data2 = loop.run(location=self.location2,
                         quiet=True,
                         background=True,
                         data_manager=True)

        data1.sync()
        data2.sync()
        self.assertEqual(data1.gates_chan1.tolist(), [1, 2, 3, 4])
        for v in data2.gates_chan1:
            self.assertTrue(np.isnan(v))

        loop.process.join()
        data2.sync()
        self.assertEqual(data2.gates_chan1.tolist(), [1, 2, 3, 4])

        # and while we're here, check that running a loop in the
        # foreground *after* the background clears its .process
        self.assertTrue(hasattr(loop, 'process'))
        loop.run_temp()
        self.assertFalse(hasattr(loop, 'process'))
Ejemplo n.º 3
0
    def test_tasks_waits(self):
        delay0 = 0.01
        delay1 = 0.03
        loop = Loop(self.p1[1:3:1], delay0).each(Task(self.p2.set, -1),
                                                 Wait(delay1), self.p2,
                                                 Task(self.p2.set, 1), self.p2)
        delay_array = []
        loop._monitor = FakeMonitor(delay_array)

        # give it a "process" as if it was run in the bg before,
        # check that this gets cleared
        loop.process = 'TDD'

        data = loop.run_temp()

        self.assertFalse(hasattr(loop, 'process'))

        self.assertEqual(data.p1_set.tolist(), [1, 2])
        self.assertEqual(data.p2_2.tolist(), [-1, -1])
        self.assertEqual(data.p2_4.tolist(), [1, 1])

        self.assertEqual(len(delay_array), 4)
        for i, delay in enumerate(delay_array):
            target = delay1 if i % 2 else delay0
            self.assertLessEqual(delay, target)
            self.assertGreater(delay, target - 0.001)
Ejemplo n.º 4
0
    def test_delay0(self, sleep_mock):
        self.p2.set(3)

        loop = Loop(self.p1[1:3:1]).each(self.p2)

        self.assertEqual(loop.delay, 0)

        data = loop.run_temp()
        self.assertEqual(data.p1_set.tolist(), [1, 2])
        self.assertEqual(data.p2.tolist(), [3, 3])

        self.assertEqual(sleep_mock.call_count, 0)
Ejemplo n.º 5
0
    def test_halt_quiet(self):
        p1 = AbortingGetter('p1',
                            count=2,
                            vals=Numbers(-10, 10),
                            msg=ActiveLoop.HALT)
        loop = Loop(p1[1:6:1], 0.005).each(p1)
        p1.set_queue(loop.signal_queue)

        # does not raise, just quits, but the data set looks the same
        # as in test_halt
        data = loop.run_temp()
        self.check_data(data)
Ejemplo n.º 6
0
    def test_composite_params(self):
        # this one has names and shapes
        mg = MultiGetter(one=1, onetwo=(1, 2))
        self.assertTrue(hasattr(mg, 'names'))
        self.assertTrue(hasattr(mg, 'shapes'))
        self.assertEqual(mg.name, 'multigetter')
        self.assertFalse(hasattr(mg, 'shape'))
        loop = Loop(self.p1[1:3:1], 0.001).each(mg)
        data = loop.run_temp()

        self.assertEqual(data.p1_set.tolist(), [1, 2])
        self.assertEqual(data.one.tolist(), [1, 1])
        self.assertEqual(data.onetwo.tolist(), [[1, 2]] * 2)
        self.assertEqual(data.index0_set.tolist(), [[0, 1]] * 2)

        # give it setpoints, names, and labels
        mg.setpoints = (None, ((10, 11), ))
        sp_name = 'highest'
        mg.setpoint_names = (None, (sp_name, ))
        sp_label = 'does it go to 11?'
        mg.setpoint_labels = (None, (sp_label, ))

        data = loop.run_temp()

        self.assertEqual(data.highest_set.tolist(), [[10, 11]] * 2)
        self.assertEqual(data.highest_set.label, sp_label)

        # setpoints as DataArray - name and label here override
        # setpoint_names and setpoint_labels attributes
        new_sp_name = 'bgn'
        new_sp_label = 'boogie nights!'
        sp_dataarray = DataArray(preset_data=[6, 7],
                                 name=new_sp_name,
                                 label=new_sp_label)
        mg.setpoints = (None, (sp_dataarray, ))

        data = loop.run_temp()
        self.assertEqual(data.bgn_set.tolist(), [[6, 7]] * 2)
        self.assertEqual(data.bgn_set.label, new_sp_label)

        # muck things up and test for errors
        mg.setpoints = (None, ((1, 2, 3), ))
        with self.assertRaises(ValueError):
            loop.run_temp()

        mg.setpoints = (None, ((1, 2), (3, 4)))
        with self.assertRaises(ValueError):
            loop.run_temp()

        del mg.setpoints, mg.setpoint_names, mg.setpoint_labels
        mg.names = mg.names + ('extra', )
        with self.assertRaises(ValueError):
            loop.run_temp()

        del mg.names
        with self.assertRaises(ValueError):
            loop.run_temp()

        # this one still has names and shapes
        mg = MultiGetter(arr=(4, 5, 6))
        self.assertTrue(hasattr(mg, 'name'))
        self.assertFalse(hasattr(mg, 'shape'))
        self.assertTrue(hasattr(mg, 'names'))
        self.assertTrue(hasattr(mg, 'shapes'))
        loop = Loop(self.p1[1:3:1], 0.001).each(mg)
        data = loop.run_temp()

        self.assertEqual(data.p1_set.tolist(), [1, 2])
        self.assertEqual(data.arr.tolist(), [[4, 5, 6]] * 2)
        self.assertEqual(data.index0_set.tolist(), [[0, 1, 2]] * 2)

        mg = MultiGetter(arr2d=((21, 22), (23, 24)))
        loop = Loop(self.p1[1:3:1], 0.001).each(mg)
        data = loop.run_temp()

        self.assertEqual(data.p1_set.tolist(), [1, 2])
        self.assertEqual(data.arr2d.tolist(), [[[21, 22], [23, 24]]] * 2)
        self.assertEqual(data.index0_set.tolist(), [[0, 1]] * 2)
        self.assertEqual(data.index1_set.tolist(), [[[0, 1]] * 2] * 2)