示例#1
0
    def test_measure_callable_verification_error(self):
        def f():
            return {"param1": 1, "param2": 2}

        def f_same():
            return {"param1": 1, "param2": 2}

        def different_f():
            return {"param2": 2, "param1": 1}

        with self.assertRaises(RuntimeError):
            with Measurement("test_measure_callable_verification_error1") as msmt:
                for k, _ in enumerate(Sweep(range(10), "sweep_param")):
                    if k < 7:
                        msmt.measure(f)
                    else:
                        msmt.measure(different_f)

        with Measurement("test_measure_callable_verification_error2") as msmt:
            for k, _ in enumerate(Sweep(range(10), "sweep_param")):
                if k < 7:
                    msmt.measure(f, name="f")
                else:
                    msmt.measure(f_same, name="f")

        with self.assertRaises(RuntimeError):
            with Measurement("test_measure_callable_verification_error3") as msmt:
                for k, _ in enumerate(Sweep(range(10), "sweep_param")):
                    if k < 7:
                        msmt.measure(f, name="f")
                    else:
                        msmt.measure(different_f, name="f")
示例#2
0
 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")
示例#3
0
    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)
示例#4
0
    def test_measure_dict_no_name(self):
        with Measurement("measure_dict") as msmt:
            for k in Sweep(range(10), "repetition"):
                with self.assertRaises(SyntaxError):
                    msmt.measure({"a": k, "b": 2 * k})

                msmt.measure({"a": k, "b": 2 * k}, "dict_msmt")
示例#5
0
    def test_measure_parameter_array_in_node(self):
        arrs = {}

        node = self.MeasurableNode("measurable_node")

        with Measurement("new_loop_parameter_array_2D") as msmt:
            for k, val in enumerate(Sweep(self.p_sweep.sweep(0, 1, 0.1))):
                results = msmt.measure(node.get)

                # Save results to verification arrays
                for kk, result in enumerate(results.values()):
                    shape = msmt.loop_shape
                    if isinstance(result, np.ndarray):
                        shape += result.shape
                    arrs.setdefault((0, 0, kk), np.zeros(shape))
                    arrs[(0, 0, kk)][k] = result

        dataset = verify_msmt(msmt, arrs)

        # Perform additional test on set arrays
        set_array = np.broadcast_to(np.arange(5), (11, 5))
        np.testing.assert_array_almost_equal(
            dataset.arrays["result1D_set0_0_0_1"], set_array
        )

        set_array = np.broadcast_to(np.arange(5), (11, 5))
        np.testing.assert_array_almost_equal(
            dataset.arrays["result2D_set0_0_0_2"], set_array
        )
        set_array = np.broadcast_to(np.arange(6), (11, 5, 6))
        np.testing.assert_array_almost_equal(
            dataset.arrays["result2D_set1_0_0_2_0"], set_array
        )
示例#6
0
    def test_measure_dict(self):
        with Measurement("measure_dict") as msmt:
            for k in Sweep(range(10), "repetition"):
                msmt.measure({"a": k, "b": 2 * k}, "dict_msmt")

        verification_arrays = {(0, 0, 0): range(10), (0, 0, 1): 2 * np.arange(10)}
        verify_msmt(msmt, verification_arrays=verification_arrays)
示例#7
0
    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)
示例#8
0
    def test_double_thread_sweep(self):
        job = new_job(self.create_measurement)
        sleep(0.2)
        Sweep([1, 2, 3], "sweep_parameter")

        running_measurement().resume()
        job.join()
示例#9
0
    def test_double_multi_parameter(self):
        multi_parameter1 = MultiParameterTest("multi_param1")
        multi_parameter2 = MultiParameterTest("multi_param2")
        multi_parameter2.values = (2,3,4)
        sweep_param = Parameter("sweep_param", set_cmd=None)

        with Measurement("test_double_multi_parameter") as msmt:
            for val in Sweep(sweep_param.sweep(0, 10, 1)):
                msmt.measure(multi_parameter1)
                msmt.measure(multi_parameter2)

        self.assertEqual(msmt.data_groups[(0, 0)].name, "multi_param1")
        self.assertEqual(msmt.data_groups[(0, 1)].name, "multi_param2")

        verification_arrays = {
            (0, 0, 0): [1] * 11,
            (0, 0, 1): [2] * 11,
            (0, 0, 2): [3] * 11,
            (0, 1, 0): [2] * 11,
            (0, 1, 1): [3] * 11,
            (0, 1, 2): [4] * 11,
        }
        self.assertListEqual(list(verification_arrays), list(msmt.data_arrays))

        verify_msmt(msmt, verification_arrays=verification_arrays)
示例#10
0
    def test_measure_callable_verification(self):
        def f():
            return {"param1": 1, "param2": 2}

        with Measurement("test_measure_callable_verification") as msmt:
            for k, _ in enumerate(Sweep(range(10), "sweep_param")):
                msmt.measure(f)
示例#11
0
    def test_mask_attr(self):
        class C:
            def __init__(self):
                self.x = 1

        c = C()

        with Measurement("mask_parameter") as msmt:
            self.assertEqual(c.x, 1)
            msmt.mask(c, x=2)
            self.assertEqual(len(msmt._masked_properties), 1)
            self.assertDictEqual(
                msmt._masked_properties[0],
                {
                    "type": "attr",
                    "obj": c,
                    "attr": "x",
                    "original_value": 1,
                    "value": 2,
                },
            )

            for k in Sweep(range(10), "repetition"):
                msmt.measure({"a": 3, "b": 4}, "acquire_values")
                self.assertEqual(c.x, 2)
            self.assertEqual(c.x, 2)

        self.assertEqual(c.x, 1)
        self.assertEqual(len(msmt._masked_properties), 0)
示例#12
0
    def test_step_out(self):
        with Measurement('test_step_out') as msmt:
            for k in Sweep(range(5), 'outer_sweep'):
                for kk in Sweep(range(5), 'inner_sweep'):
                    msmt.measure(123, 'measurable')

                    if kk > 2:
                        msmt.step_out()
                        break

        verification_arrays = {
            (0, 0, 0): 123 * np.ones((5,5)),
        }
        verification_arrays[(0,0,0)][:,4:] = np.nan

        verify_msmt(msmt, verification_arrays=verification_arrays, allow_nan=True)
示例#13
0
    def test_revert_multi_parameter(self):
        multi_parameter = MultiParameterTest("multi_param")
        sweep_param = Parameter("sweep_param", set_cmd=None)

        with Measurement("test_multi_parameter") as msmt:
            for k, val in enumerate(Sweep(sweep_param.sweep(0, 10, 1))):
                msmt.measure(multi_parameter)

                if k == 4:
                    msmt.revert()

                    multi_parameter.values = (2,3,4)
                    msmt.measure(multi_parameter)
                    multi_parameter.values = (1,2,3)

        self.assertEqual(msmt.data_groups[(0, 0)].name, "multi_param")

        verification_arrays = {
            (0, 0, 0): [1] * 11,
            (0, 0, 1): [2] * 11,
            (0, 0, 2): [3] * 11,
        }
        for key, val in verification_arrays.items():
            val[4] += 1

        verify_msmt(msmt, verification_arrays=verification_arrays)
示例#14
0
    def test_revert_1D_twice(self):
        arrs = {}

        with Measurement("revert_1D_twice") as msmt:
            p_measure1 = Parameter('p_measure1', set_cmd=None)
            p_measure2 = Parameter('p_measure2', set_cmd=None)
            p_sweep = Parameter('p_sweep', set_cmd=None)

            for k, val in enumerate(Sweep(p_sweep.sweep(0, 1, 0.1))):
                arr1 = arrs.setdefault(msmt.action_indices, np.zeros(msmt.loop_shape))
                p_measure1(1)
                arr1[k] = msmt.measure(p_measure1)

                arr2 = arrs.setdefault(msmt.action_indices, np.zeros(msmt.loop_shape))
                p_measure2(2)
                arr2[k] = msmt.measure(p_measure2)

                if k == 5:
                    msmt.revert(2)
                    p_measure1(3)
                    p_measure2(4)
                    arr1[k] = msmt.measure(p_measure1)
                    arr2[k] = msmt.measure(p_measure2)

        verify_msmt(msmt, arrs)
示例#15
0
    def test_measure_parameter_array_2D(self):
        arrs = {}

        p_measure = Parameter("p_measure", get_cmd=lambda: np.random.rand(5, 12))

        with Measurement("new_loop_parameter_array_2D") 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 + (5, 12))
                )
                result = msmt.measure(p_measure)
                arr[k] = result

        dataset = verify_msmt(msmt, arrs)

        # Perform additional test on set arrays
        set_array = np.broadcast_to(np.arange(5), (11, 5))
        np.testing.assert_array_almost_equal(
            dataset.arrays["p_measure_set0_0_0"], set_array
        )

        set_array = np.broadcast_to(np.arange(12), (11, 5, 12))
        np.testing.assert_array_almost_equal(
            dataset.arrays["p_measure_set1_0_0_0"], set_array
        )
示例#16
0
    def test_mask_parameter_attr(self):
        p = Parameter("masking_param", set_cmd=None, initial_value=1)
        p.x = 1

        with Measurement("mask_parameter_attr") as msmt:
            self.assertEqual(p.x, 1)
            msmt.mask(p, x=2)
            self.assertEqual(len(msmt._masked_properties), 1)
            self.assertDictEqual(
                msmt._masked_properties[0],
                {
                    "type": "attr",
                    "obj": p,
                    "attr": "x",
                    "original_value": 1,
                    "value": 2,
                },
            )

            for k in Sweep(range(10), "repetition"):
                msmt.measure({"a": 3, "b": 4}, "acquire_values")
                self.assertEqual(p.x, 2)
                self.assertEqual(p(), 1)
            self.assertEqual(p.x, 2)

        self.assertEqual(p.x, 1)
        self.assertEqual(len(msmt._masked_properties), 0)
示例#17
0
 def test_measure_dict_wrong_ordering(self):
     with Measurement("measure_dict") as msmt:
         for k in Sweep(range(10), "repetition"):
             if k < 5:
                 msmt.measure({"a": k, "b": 2 * k}, "first_ordering")
             else:
                 with self.assertRaises(RuntimeError):
                     msmt.measure({"b": k, "c": 2 * k}, "second_ordering")
示例#18
0
 def test_simple_measurement_verification_error(self):
     with self.assertRaises(RuntimeError):
         with Measurement("test_simple_measurement_verification_error") as msmt:
             for k, val in enumerate(Sweep(range(10), "sweep_param")):
                 if k < 7:
                     msmt.measure(val + 2, "msmt_param")
                 else:
                     msmt.measure(val + 2, "different_msmt_param")
示例#19
0
    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)
示例#20
0
    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")
示例#21
0
        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
示例#22
0
    def test_mask(self):
        class C:
            def __init__(self):
                self.x = 1

        c = C()
        p = Parameter("masking_param", set_cmd=None, initial_value=1)
        d = dict(x=1)

        with Measurement("mask_parameter") as msmt:
            self.assertEqual(c.x, 1)
            self.assertEqual(p(), 1)
            self.assertEqual(d["x"], 1)

            msmt.mask(c, x=2)
            msmt.mask(p, 2)
            msmt.mask(d, x=2)

            self.assertEqual(len(msmt._masked_properties), 3)
            self.assertListEqual(
                msmt._masked_properties,
                [
                    {
                        "type": "attr",
                        "obj": c,
                        "attr": "x",
                        "original_value": 1,
                        "value": 2,
                    },
                    {"type": "parameter", "obj": p, "original_value": 1, "value": 2},
                    {
                        "type": "key",
                        "obj": d,
                        "key": "x",
                        "original_value": 1,
                        "value": 2,
                    },
                ],
            )

            for k in Sweep(range(10), "repetition"):
                msmt.measure({"a": 3, "b": 4}, "acquire_values")
                self.assertEqual(c.x, 2)
                self.assertEqual(p(), 2)
                self.assertEqual(d["x"], 2)
            self.assertEqual(c.x, 2)
            self.assertEqual(p(), 2)
            self.assertEqual(d["x"], 2)

        self.assertEqual(c.x, 1)
        self.assertEqual(p(), 1)
        self.assertEqual(d["x"], 1)
        self.assertEqual(len(msmt._masked_properties), 0)
示例#23
0
    def test_step_out_multiparameter(self):
        multi_parameter = MultiParameterTest("multi_param")

        with Measurement('test_step_out_multiparameter') as msmt:
            for k in Sweep(range(5), 'outer_sweep'):
                for kk in Sweep(range(5), 'inner_sweep'):
                    msmt.measure(multi_parameter)

                    if kk > 2:
                        msmt.step_out()
                        break

        verification_arrays = {
            (0, 0, 0, 0): 1 * np.ones((5,5)),
            (0, 0, 0, 1): 2 * np.ones((5,5)),
            (0, 0, 0, 2): 3 * np.ones((5,5)),
        }
        verification_arrays[(0,0,0,0)][:,4:] = np.nan
        verification_arrays[(0,0,0,2)][:,4:] = np.nan
        verification_arrays[(0,0,0,1)][:,4:] = np.nan

        verify_msmt(msmt, verification_arrays=verification_arrays, allow_nan=True)
示例#24
0
    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")
示例#25
0
    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)
示例#26
0
    def test_simple_measurement_verification_no_error_parameter(self):
        msmt_param = Parameter("msmt_param", get_cmd=np.random.rand)
        msmt_param2 = Parameter("msmt_param", get_cmd=np.random.rand)
        # Notice we give the same name

        with Measurement(
            "test_simple_measurement_verification_no_error_parameter"
        ) as msmt:
            for k, _ in enumerate(Sweep(range(10), "sweep_param")):
                if k < 7:
                    msmt.measure(msmt_param)
                else:
                    msmt.measure(msmt_param2)
示例#27
0
    def test_simple_measurement_verification_error_parameter(self):
        msmt_param = Parameter("msmt_param", get_cmd=np.random.rand)
        different_msmt_param = Parameter("different_msmt_param", get_cmd=np.random.rand)

        with self.assertRaises(RuntimeError):
            with Measurement(
                "test_simple_measurement_verification_error_parameter"
            ) as msmt:
                for k, _ in enumerate(Sweep(range(10), "sweep_param")):
                    if k < 7:
                        msmt.measure(msmt_param)
                    else:
                        msmt.measure(different_msmt_param)
示例#28
0
    def test_measure_node_dict(self):
        arrs = {}
        node = self.DictResultsNode("measurable_node")
        p_sweep = Parameter("sweep", set_cmd=None)

        with Measurement("measure_node") as msmt:
            for k, val in enumerate(Sweep(p_sweep.sweep(0, 1, 0.1))):
                results = msmt.measure(node.get)

                # Save results to verification arrays
                for kk, result in enumerate(results.values()):
                    arrs.setdefault((0, 0, kk), np.zeros(msmt.loop_shape))
                    arrs[(0, 0, kk)][k] = result
示例#29
0
    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")
示例#30
0
    def test_measure_double_dict(self):
        with Measurement("measure_double_dict") as msmt:
            for k in Sweep(range(10), "repetition"):
                msmt.measure({"a": k, "b": 2 * k}, "dict1_msmt")
                msmt.measure({"a": k+1, "b": 2 * k+1}, "dict2_msmt")

        verification_arrays = {
            (0, 0, 0): range(10),
            (0, 0, 1): 2 * np.arange(10),
            (0, 1, 0): range(1, 11),
            (0, 1, 1): 2 * np.arange(10) + 1,
        }
        self.assertListEqual(list(verification_arrays), list(msmt.data_arrays))
        verify_msmt(msmt, verification_arrays=verification_arrays)