예제 #1
0
class TestOldLoop(TestCase):
    def setUp(self) -> None:
        self.p_sweep = Parameter("p_sweep", set_cmd=None)
        self.p_measure = Parameter("p_measure", set_cmd=None)
        self.p_sweep.connect(self.p_measure)

    def test_old_loop_1D(self):
        loop = Loop(self.p_sweep.sweep(0, 10, 1)).each(self.p_measure, self.p_measure)
        data = loop.run(name="old_loop_1D", thread=False)

        self.assertEqual(data.metadata.get("measurement_type"), "Loop")

        # Verify that the measurement dataset records the correct measurement type
        loaded_data = load_data(data.location)
        self.assertEqual(loaded_data.metadata.get("measurement_type"), "Loop")

    def test_old_loop_2D(self):
        self.p_sweep2 = Parameter("p_sweep2", set_cmd=None)

        loop = (
            Loop(self.p_sweep.sweep(0, 5, 1))
            .loop(self.p_sweep2.sweep(0, 5, 1))
            .each(self.p_measure, self.p_measure)
        )
        loop.run(name="old_loop_2D", thread=False)

    def test_old_loop_1D_2D(self):
        self.p_sweep2 = Parameter("p_sweep2", set_cmd=None)

        loop = Loop(self.p_sweep.sweep(0, 5, 1)).each(
            self.p_measure, Loop(self.p_sweep2.sweep(0, 5, 1)).each(self.p_measure)
        )
        loop.run(name="old_loop_1D_2D", thread=False)
예제 #2
0
class TestNewLoopBasics(TestCase):
    def setUp(self) -> None:
        self.p_sweep = Parameter("p_sweep", set_cmd=None, initial_value=10)
        self.p_measure = Parameter("p_measure", set_cmd=None)
        self.p_sweep.connect(self.p_measure, scale=10)

    def test_empty_measurement(self):
        with Measurement("empty_measurement") as msmt:
            pass

    def test_new_loop_1D(self):
        arrs = {}

        with Measurement("new_loop_1D") as msmt:
            for k, val in enumerate(Sweep(self.p_sweep.sweep(0, 1, 0.1))):
                arr = arrs.setdefault(msmt.action_indices, np.zeros(msmt.loop_shape))
                arr[k] = msmt.measure(self.p_measure)

        verify_msmt(msmt, arrs)

        # Verify that the measurement dataset records the correct measurement type
        data = load_data(msmt.dataset.location)
        self.assertEqual(data.metadata.get("measurement_type"), "Measurement")

    def test_new_loop_1D_double(self):
        arrs = {}

        with Measurement("new_loop_1D_double") as msmt:
            for k, val in enumerate(Sweep(self.p_sweep.sweep(0, 1, 0.1))):
                arr = arrs.setdefault(msmt.action_indices, np.zeros(msmt.loop_shape))
                arr[k] = msmt.measure(self.p_measure)

                arr = arrs.setdefault(msmt.action_indices, np.zeros(msmt.loop_shape))
                arr[k] = msmt.measure(self.p_measure)

        self.assertListEqual([(0, 0), (0, 1)], list(msmt.data_arrays))

        verify_msmt(msmt, arrs)

    def test_new_loop_2D(self):
        arrs = {}
        self.p_sweep2 = Parameter("p_sweep2", set_cmd=None)

        with Measurement("new_loop_1D_double") as msmt:
            for k, val in enumerate(Sweep(self.p_sweep2.sweep(0, 1, 0.1))):
                for kk, val2 in enumerate(Sweep(self.p_sweep.sweep(0, 1, 0.1))):
                    self.assertEqual(msmt.loop_shape, (11, 11))
                    arr = arrs.setdefault(
                        msmt.action_indices, np.zeros(msmt.loop_shape)
                    )
                    arr[k, kk] = msmt.measure(self.p_measure)

        verify_msmt(msmt, arrs)

    def test_new_loop_dual_sweep(self):
        with Measurement("outer") as msmt:
            self.assertEqual(msmt.action_indices, (0,))
            for _ in Sweep(range(10), "sweep0"):
                self.assertEqual(msmt.action_indices, (0, 0))
                for _ in Sweep(range(10), "sweep1"):
                    self.assertEqual(msmt.action_indices, (0, 0, 0))
                    msmt.measure(np.random.rand(), "random_value1")
                self.assertEqual(msmt.action_indices, (0, 1))
                for _ in Sweep(range(10), "sweep2"):
                    self.assertEqual(msmt.action_indices, (0, 1, 0))
                    msmt.measure(np.random.rand(), "random_value2")

    def test_new_loop_break(self):
        arrs = {}
        self.p_sweep2 = Parameter("p_sweep2", set_cmd=None)

        with Measurement("new_loop_1D_double") as msmt:
            for k, val in enumerate(Sweep(self.p_sweep2.sweep(0, 1, 0.2))):
                for kk, val2 in enumerate(Sweep(self.p_sweep.sweep(0, 1, 0.2))):
                    self.assertEqual(msmt.loop_shape, (6, 6))
                    arr = arrs.setdefault(
                        msmt.action_indices, np.nan * np.zeros(msmt.loop_shape)
                    )
                    arr[k, kk] = msmt.measure(self.p_measure)
                    if kk == 2:
                        msmt.step_out(reduce_dimension=True)
                        break
                print("hi")

        verify_msmt(msmt, arrs, allow_nan=True)

    def test_skip_action(self):
        with Measurement("test") as msmt:
            for k in Sweep(range(5), "sweeper"):
                msmt.measure(k, "idx")
                if k % 2:
                    msmt.measure(2 * k, "double_idx")
                else:
                    msmt.skip()
                msmt.measure(3 * k, "triple_idx")

        arrs = {
            (0, 0): np.arange(5),
            (0, 1): [np.nan, 2, np.nan, 6, np.nan],
            (0, 2): 3 * np.arange(5),
        }

        verify_msmt(msmt, arrs, allow_nan=True)

    def test_new_loop_0D(self):
        # TODO Does not work yet
        with Measurement("new_loop_0D") as msmt:
            self.assertEqual(msmt.loop_shape, ())
            msmt.measure(self.p_measure)

    def test_new_loop_1D_0D(self):
        # TODO Does not work yet
        arrs = {}

        with Measurement("new_loop_1D_0D") as msmt:
            self.assertEqual(msmt.loop_shape, ())
            for k, val in enumerate(Sweep(self.p_sweep.sweep(0, 1, 0.1))):
                arr = arrs.setdefault(msmt.action_indices, np.zeros(msmt.loop_shape))
                arr[k] = msmt.measure(self.p_measure)
            arr = arrs.setdefault(msmt.action_indices, np.zeros((1,)))
            arr[0] = msmt.measure(self.p_measure)

        verify_msmt(msmt, arrs)

        # self.verify_msmt(msmt, arrs)

    def test_noniterable_sweep_error(self):
        with Measurement("noniterable_sweep_error") as msmt:
            with self.assertRaises(SyntaxError):
                Sweep(1, "noniterable")

    def test_new_loop_1D_None_result(self):
        arrs = {}
        p_measure = Parameter("p_measure", set_cmd=None)

        with Measurement("new_loop_1D_None_result") as msmt:
            for k, val in enumerate(Sweep(self.p_sweep.sweep(0, 1, 0.1))):
                arr = arrs.setdefault(msmt.action_indices, np.zeros(msmt.loop_shape))

                if not k % 2:
                    p_measure(k)
                else:
                    p_measure(None)
                arr[k] = msmt.measure(p_measure)

        verify_msmt(msmt, arrs, allow_nan=True)

        # Verify that the measurement dataset records the correct measurement type
        data = load_data(msmt.dataset.location)
        self.assertEqual(data.metadata.get("measurement_type"), "Measurement")

    def test_new_loop_1D_None_result_raw_value(self):
        arrs = {}
        with Measurement("new_loop_1D_None_result") as msmt:
            for k, val in enumerate(Sweep(self.p_sweep.sweep(0, 1, 0.1))):
                arr = arrs.setdefault(msmt.action_indices, np.zeros(msmt.loop_shape))

                if not k % 2:
                    arr[k] = msmt.measure(k, "p")
                else:
                    arr[k] = msmt.measure(None, "p")

        verify_msmt(msmt, arrs, allow_nan=True)

        # Verify that the measurement dataset records the correct measurement type
        data = load_data(msmt.dataset.location)
        self.assertEqual(data.metadata.get("measurement_type"), "Measurement")

    def test_measure_third_arg_error(self):
        with Measurement("measure_third_arg_error") as msmt:
            with self.assertRaises(TypeError):
                msmt.measure(1232, "measurable", 1232)

    def test_pass_label_unit(self):
        with Measurement("new_loop_pass_label_unit") as msmt:
            for k, val in enumerate(Sweep(self.p_sweep.sweep(0, 1, 0.1))):
                msmt.measure(k, "measurable", label="MyLabel", unit="Hz")

        # Verify that the measurement dataset records the correct measurement type
        data = load_data(msmt.dataset.location)
        self.assertEqual(data.measurable_0_0.label, "MyLabel")
        self.assertEqual(data.measurable_0_0.unit, "Hz")

    def test_pass_label_unit_to_parameter(self):
        p_measure = Parameter(
            "measurable", set_cmd=None, label="original_label", unit="V"
        )
        with Measurement("new_loop_pass_label_unit_to_parameter") as msmt:
            for k, val in enumerate(Sweep(self.p_sweep.sweep(0, 1, 0.1))):
                p_measure(k)
                # Override label and unit
                msmt.measure(p_measure, label="MyLabel", unit="Hz")

        # Verify that the measurement dataset records the correct measurement type
        data = load_data(msmt.dataset.location)
        self.assertEqual(data.measurable_0_0.label, "MyLabel")
        self.assertEqual(data.measurable_0_0.unit, "Hz")

    def test_error_when_array_limit_reached(self):
        with Measurement('error_when_array_limit_reached') as msmt:
            for k in range(msmt.max_arrays+1):  # Note the lack of an encapsulating Sweep
                if k < msmt.max_arrays:
                    msmt.measure(123, 'measurable')
                else:
                    with self.assertRaises(RuntimeError):
                        msmt.measure(123, 'measurable')

    def test_odd_numpy_datatypes(self):
        int_datatypes = [np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64]
        float_datatypes = [np.float_]
        for datatype in int_datatypes:
            with Measurement(f'odd_datatype_{datatype.__name__}') as msmt:
                msmt.measure(datatype(2), 'odd_value')

    def test_notify(self):
        try:
            is_notified = []
            Measurement.notify_function = lambda msmt, *args: is_notified.append(msmt.name)

            with Measurement('notify_msmt'):
                pass

            self.assertListEqual(is_notified, [])

            with Measurement('notify_msmt', notify=True) as msmt:
                pass

            self.assertListEqual(is_notified, ['notify_msmt'])

            with self.assertRaises(RuntimeError):
                with Measurement('notify_msmt_exception', notify=True) as msmt:
                    raise RuntimeError

            self.assertListEqual(is_notified, ['notify_msmt', 'notify_msmt_exception'])

        finally:
            Measurement.notify_function = None
예제 #3
0
class TestNewLoopNesting(TestCase):
    def setUp(self) -> None:
        self.p_sweep = Parameter("p_sweep", set_cmd=None, initial_value=10)
        self.p_measure = Parameter("p_measure", set_cmd=None)
        self.p_sweep.connect(self.p_measure, scale=10)

    def test_nest_measurement(self):
        def nest_measurement():
            self.assertEqual(running_measurement().action_indices, (1,))
            with Measurement("nested_measurement") as msmt:
                self.assertEqual(running_measurement().action_indices, (1, 0))
                for val in Sweep(self.p_sweep.sweep(0, 1, 0.1)):
                    self.assertEqual(running_measurement().action_indices, (1, 0, 0))
                    msmt.measure(self.p_measure)
                    self.assertEqual(running_measurement().action_indices, (1, 0, 1))
                    msmt.measure(self.p_measure)

            return msmt

        with Measurement("outer_measurement") as msmt:
            for val in Sweep(self.p_sweep.sweep(0, 1, 0.1)):
                msmt.measure(self.p_measure)
            nested_msmt = nest_measurement()

        self.assertEqual(msmt.data_groups[(1,)], nested_msmt)

    def test_double_nest_measurement(self):
        def nest_measurement():
            self.assertEqual(running_measurement().action_indices, (1,))
            with Measurement("nested_measurement") as msmt:
                self.assertEqual(running_measurement().action_indices, (1, 0))
                for val in Sweep(self.p_sweep.sweep(0, 1, 0.1)):
                    self.assertEqual(running_measurement().action_indices, (1, 0, 0))
                    with Measurement("inner_nested_measurement") as inner_msmt:
                        self.assertEqual(
                            running_measurement().action_indices, (1, 0, 0, 0)
                        )
                        for val in Sweep(self.p_sweep.sweep(0, 1, 0.1)):
                            self.assertEqual(
                                running_measurement().action_indices, (1, 0, 0, 0, 0)
                            )
                            inner_msmt.measure(self.p_measure)
                            self.assertEqual(
                                running_measurement().action_indices, (1, 0, 0, 0, 1)
                            )
                            inner_msmt.measure(self.p_measure)

            return msmt, inner_msmt

        with Measurement("outer_measurement") as msmt:
            for val in Sweep(self.p_sweep.sweep(0, 1, 0.1)):
                msmt.measure(self.p_measure)
            nested_msmt, inner_nested_msmt = nest_measurement()

        self.assertEqual(msmt.data_groups[(1,)], nested_msmt)
        self.assertEqual(msmt.data_groups[(1, 0, 0)], inner_nested_msmt)

    def test_new_loop_two_nests(self):
        with Measurement("outer") as msmt:
            self.assertEqual(msmt.action_indices, (0,))
            for _ in Sweep(range(10), "sweep0"):
                self.assertEqual(msmt.action_indices, (0, 0))
                with Measurement("inner1") as msmt_inner:
                    self.assertEqual(msmt.action_indices, (0, 0, 0))
                    for _ in Sweep(range(10), "sweep1"):
                        self.assertEqual(msmt.action_indices, (0, 0, 0, 0))
                        msmt.measure(np.random.rand(), "random_value1")
                    self.assertEqual(msmt.action_indices, (0, 0, 1))
                self.assertEqual(msmt.action_indices, (0, 1))
                with Measurement("inner2") as msmt_inner:
                    self.assertEqual(msmt.action_indices, (0, 1, 0))
                    for _ in Sweep(range(10), "sweep2"):
                        self.assertEqual(msmt.action_indices, (0, 1, 0, 0))
                        msmt.measure(np.random.rand(), "random_value2")