コード例 #1
0
    def setUpClass(self):
        self.station = station.Station()
        self.MC = measurement_control.MeasurementControl(
            'MC', live_plot_enabled=True, verbose=True)
        self.MC.station = self.station
        self.station.add_component(self.MC)

        self.mock_parabola = DummyParHolder('mock_parabola')
        self.station.add_component(self.mock_parabola)
コード例 #2
0
    def setUpClass(self):
        self.station = station.Station()
        self.datadir = os.path.join(pq.__path__[0], 'tests', 'test_data')
        self.MC = measurement_control.MeasurementControl(
            'MC', live_plot_enabled=False, verbose=False)
        self.MC.station = self.station
        self.MC.datadir(self.datadir)
        a_tools.datadir = self.datadir
        self.station.add_component(self.MC)

        self.mock_parabola = DummyParHolder('mock_parabola')
        self.station.add_component(self.mock_parabola)
        self.mock_parabola_2 = DummyParHolder('mock_parabola_2')
        self.station.add_component(self.mock_parabola_2)
コード例 #3
0
    def setup_class(cls):
        cls.station = station.Station()
        cls.MC = measurement_control.MeasurementControl(
            'MC', live_plot_enabled=False, verbose=False)
        cls.MC.station = cls.station
        cls.station.add_component(cls.MC)

        cls.mock_parabola = DummyParHolder('mock_parabola')
        cls.station.add_component(cls.mock_parabola)
コード例 #4
0
    def setUpClass(self):
        self.station = station.Station()
        # set up a pulsar with some mock settings for the element
        self.MC = measurement_control.MeasurementControl(
            'MC', live_plot_enabled=False, verbose=False)
        self.MC.station = self.station
        self.station.add_component(self.MC)

        self.mock_parabola = DummyParHolder('mock_parabola')
        self.station.add_component(self.mock_parabola)
コード例 #5
0
class Test_MeasurementControl(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        self.station = station.Station()
        self.MC = measurement_control.MeasurementControl(
            'MC', live_plot_enabled=True, verbose=True)
        self.MC.station = self.station
        self.station.add_component(self.MC)

        self.mock_parabola = DummyParHolder('mock_parabola')
        self.station.add_component(self.mock_parabola)

    def setUp(self):
        self.MC.soft_avg(1)

    def test_soft_sweep_1D(self):

        sweep_pts = np.linspace(0, 10, 30)
        self.MC.set_sweep_function(None_Sweep())
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Soft())
        dat = self.MC.run('1D_soft')
        dset = dat["dset"]
        x = dset[:, 0]
        xr = np.arange(len(x))/15
        y = np.array([np.sin(xr/np.pi), np.cos(xr/np.pi)])
        y0 = dset[:, 1]
        y1 = dset[:, 2]
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y0, y[0, :])
        np.testing.assert_array_almost_equal(y1, y[1, :])

        # Test that the return dictionary has the right entries
        dat_keys = set(['dset', 'opt_res_dset', 'sweep_parameter_names',
                        'sweep_parameter_units',
                        'value_names', 'value_units'])
        self.assertEqual(dat_keys, set(dat.keys()))

        self.assertEqual(dat['sweep_parameter_names'], ['pts'])
        self.assertEqual(dat['sweep_parameter_units'], ['arb. unit'])
        self.assertEqual(dat['value_names'], ['I', 'Q'])
        self.assertEqual(dat['value_units'], ['V', 'V'])

    def test_soft_sweep_1D_alt_shape(self):
        # This is a generalization of a 1D sweep function where instead of
        # a shape (2,) it has a shape (2,1). This isinconsistent with the
        # N-D hard sweeps. and should be addressed

        sweep_pts = np.linspace(0, 10, 30)
        self.MC.set_sweep_function(None_Sweep())
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Soft_diff_shape())
        dat = self.MC.run('1D_soft')
        dset = dat["dset"]
        x = dset[:, 0]
        xr = np.arange(len(x))/15
        y = np.array([np.sin(xr/np.pi), np.cos(xr/np.pi)])
        y0 = dset[:, 1]
        y1 = dset[:, 2]
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y0, y[0, :])
        np.testing.assert_array_almost_equal(y1, y[1, :])

        # Test that the return dictionary has the right entries
        dat_keys = set(['dset', 'opt_res_dset', 'sweep_parameter_names',
                        'sweep_parameter_units',
                        'value_names', 'value_units'])
        self.assertEqual(dat_keys, set(dat.keys()))

        self.assertEqual(dat['sweep_parameter_names'], ['pts'])
        self.assertEqual(dat['sweep_parameter_units'], ['arb. unit'])
        self.assertEqual(dat['value_names'], ['I', 'Q'])
        self.assertEqual(dat['value_units'], ['V', 'V'])

    @unittest.skipIf(
        True,
        "This test is currently broken")
    def test_data_location(self):
        sweep_pts = np.linspace(0, 10, 30)
        self.MC.set_sweep_function(None_Sweep())
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Soft())
        self.MC.run('datadir_test_file')
        # raises an error if the file is not found
        ma.MeasurementAnalysis(label='datadir_test_file')

        # change the datadir
        test_dir2 = os.path.abspath(os.path.join(
            os.path.dirname(pq.__file__), os.pardir, 'data_test_2'))
        self.MC.datadir(test_dir2)

        sweep_pts = np.linspace(0, 10, 30)
        self.MC.set_sweep_function(None_Sweep())
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Soft())
        self.MC.run('datadir_test_file_2')
        # raises an error if the file is not found
        with self.assertRaises(Exception):
            ma.MeasurementAnalysis(label='datadir_test_file_2')
        ma.a_tools.datadir = test_dir2
        # changing the dir makes it find the file now
        ma.MeasurementAnalysis(label='datadir_test_file_2')
        self.MC.datadir(get_default_datadir())

    def test_hard_sweep_1D(self):
        sweep_pts = np.linspace(0, 10, 5)
        self.MC.set_sweep_function(None_Sweep(sweep_control='hard'))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Hard())
        dat = self.MC.run('1D_hard')
        dset = dat['dset']
        x = dset[:, 0]
        y = [np.sin(x / np.pi), np.cos(x/np.pi)]
        y0 = dset[:, 1]
        y1 = dset[:, 2]
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y0, y[0])
        np.testing.assert_array_almost_equal(y1, y[1])
        d = self.MC.detector_function
        self.assertEqual(d.times_called, 1)

    def test_soft_sweep_2D(self):
        sweep_pts = np.linspace(0, 10, 30)
        sweep_pts_2D = np.linspace(0, 10, 5)
        self.MC.set_sweep_function(None_Sweep(sweep_control='soft'))
        self.MC.set_sweep_function_2D(None_Sweep(sweep_control='soft'))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_sweep_points_2D(sweep_pts_2D)
        self.MC.set_detector_function(det.Dummy_Detector_Soft())
        dat = self.MC.run('2D_soft', mode='2D')
        dset = dat["dset"]
        x = dset[:, 0]
        y = dset[:, 1]
        xr = np.arange(len(sweep_pts)*len(sweep_pts_2D))/15
        z = np.array([np.sin(xr/np.pi), np.cos(xr/np.pi)])
        z0 = dset[:, 2]
        z1 = dset[:, 3]

        x_tiled = np.tile(sweep_pts, len(sweep_pts_2D))
        y_rep = np.repeat(sweep_pts_2D, len(sweep_pts))
        np.testing.assert_array_almost_equal(x, x_tiled)
        np.testing.assert_array_almost_equal(y, y_rep)
        np.testing.assert_array_almost_equal(z0, z[0, :])
        np.testing.assert_array_almost_equal(z1, z[1, :])

    def test_soft_sweep_2D_with_reading_of_set_parameter(self):
        sweep_pts = np.linspace(0, 10, 30)
        sweep_pts_2D = np.linspace(0, 10, 5)
        self.MC.set_sweep_function(None_Sweep_With_Parameter_Returned(
            sweep_control='soft'))
        self.MC.set_sweep_function_2D(None_Sweep_With_Parameter_Returned(
            sweep_control='soft'))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_sweep_points_2D(sweep_pts_2D)
        self.MC.set_detector_function(det.Dummy_Detector_Soft())
        dat = self.MC.run('2D_soft', mode='2D')
        dset = dat["dset"]
        x = dset[:, 0]
        y = dset[:, 1]
        xr = np.arange(len(sweep_pts)*len(sweep_pts_2D))/15
        z = np.array([np.sin(xr/np.pi), np.cos(xr/np.pi)])
        z0 = dset[:, 2]
        z1 = dset[:, 3]

        # The +0.1 is to test if the return value is matching
        x_tiled = np.tile(sweep_pts+0.1, len(sweep_pts_2D))
        y_rep = np.repeat(sweep_pts_2D+0.1, len(sweep_pts))
        np.testing.assert_array_almost_equal(x, x_tiled)
        np.testing.assert_array_almost_equal(y, y_rep)
        np.testing.assert_array_almost_equal(z0, z[0, :])
        np.testing.assert_array_almost_equal(z1, z[1, :])

    def test_soft_sweep_2D_function_calls(self):
        sweep_pts = np.arange(0, 30, 1)
        sweep_pts_2D = np.arange(0, 5, 1)
        s1 = None_Sweep_idx(sweep_control='soft')
        s2 = None_Sweep_idx(sweep_control='soft')
        self.MC.set_sweep_function(s1)
        self.MC.set_sweep_function_2D(s2)
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_sweep_points_2D(sweep_pts_2D)
        self.MC.set_detector_function(det.Dummy_Detector_Soft())

        self.assertEqual(s1.num_calls, 0)
        self.assertEqual(s2.num_calls, 0)
        self.MC.run('2D_soft', mode='2D')

        # Test that the 2D scan only gets called 5 times (when it changes)
        # The 1D value always changes and as such should always be called
        self.assertEqual(s1.num_calls, 30*5)
        self.assertEqual(s2.num_calls, 5)

    def test_hard_sweep_2D(self):
        """
        Hard inner loop, soft outer loop
        """
        sweep_pts = np.linspace(10, 20, 3)
        sweep_pts_2D = np.linspace(0, 10, 5)
        self.MC.live_plot_enabled(False)
        self.MC.set_sweep_function(None_Sweep(sweep_control='hard'))
        self.MC.set_sweep_function_2D(None_Sweep(sweep_control='soft'))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_sweep_points_2D(sweep_pts_2D)
        self.MC.set_detector_function(det.Dummy_Detector_Hard())
        dat = self.MC.run('2D_hard', mode='2D')
        dset = dat["dset"]
        x = dset[:, 0]
        y = dset[:, 1]
        z = self.data = [np.sin(x / np.pi), np.cos(x/np.pi)]
        z0 = dset[:, 2]
        z1 = dset[:, 3]

        x_tiled = np.tile(sweep_pts, len(sweep_pts_2D))
        y_rep = np.repeat(sweep_pts_2D, len(sweep_pts))
        np.testing.assert_array_almost_equal(x, x_tiled)
        np.testing.assert_array_almost_equal(y, y_rep)
        np.testing.assert_array_almost_equal(z0, z[0])
        np.testing.assert_array_almost_equal(z1, z[1])
        d = self.MC.detector_function
        self.assertEqual(d.times_called, 5)

        self.MC.live_plot_enabled(True)

    def test_many_shots_hard_sweep(self):
        """
        Tests acquiring more than the maximum number of shots for a hard
        detector by setting the number of sweep points high
        """
        sweep_pts = np.arange(50)
        self.MC.set_sweep_function(None_Sweep(sweep_control='hard'))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Shots_Detector(max_shots=5))
        dat = self.MC.run('man_shots')
        dset = dat["dset"]
        x = dset[:, 0]
        y = dset[:, 1]

        self.assertEqual(np.shape(dset), (len(sweep_pts), 2))
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y, sweep_pts)

        d = self.MC.detector_function
        self.assertEqual(d.times_called, 10)

    def test_variable_sized_return_values_hard_sweep(self):
        """
        Tests a detector that acquires data in chunks of varying sizes
        """
        self.MC.soft_avg(1)
        counter_param = ManualParameter('counter', initial_value=0)

        def return_variable_size_values():
            idx = counter_param() % 3
            counter_param(counter_param()+1)

            if idx == 0:
                return np.arange(0, 7)
            elif idx == 1:
                return np.arange(7, 11)
            elif idx == 2:
                return np.arange(11, 30)

        sweep_pts = np.arange(30)

        d = det.Function_Detector(get_function=return_variable_size_values,
                                  value_names=['Variable size counter'],
                                  detector_control='hard')
        self.MC.set_sweep_function(None_Sweep(sweep_control='hard'))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(d)
        dat = self.MC.run('varying_chunk_size')
        dset = dat["dset"]
        x = dset[:, 0]
        y = dset[:, 1]

        self.assertEqual(np.shape(dset), (len(sweep_pts), 2))
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y, sweep_pts)

        self.assertEqual(self.MC.total_nr_acquired_values, 1*30)

    def test_soft_sweep_hard_det_1D(self):

        def mock_func():
            # to also test if the values are set correctly in the sweep
            arr = np.zeros([2, 2])
            arr[0, :] = np.array([self.mock_parabola.x()]*2)
            arr[1, :] = np.array([self.mock_parabola.x()+2]*2)
            return arr

        d = det.Function_Detector(get_function=mock_func,
                                  value_names=['x', 'x+2'],
                                  detector_control='hard')
        sweep_pts = np.repeat(np.arange(5), 2)
        self.MC.set_sweep_function(self.mock_parabola.x)
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(d)
        dat = self.MC.run('soft_sweep_hard_det')
        dset = dat["dset"]

        x = dset[:, 0]
        y0 = dset[:, 1]
        y1 = dset[:, 2]
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y0, sweep_pts)
        np.testing.assert_array_almost_equal(y1, sweep_pts+2)

    def test_variable_sized_return_values_hard_sweep_soft_avg(self):
        """
        Tests a detector that acquires data in chunks of varying sizes
        """
        self.MC.soft_avg(10)
        counter_param = ManualParameter('counter', initial_value=0)

        def return_variable_size_values():
            idx = counter_param() % 3
            counter_param(counter_param()+1)

            if idx == 0:
                return np.arange(0, 7)
            elif idx == 1:
                return np.arange(7, 11)
            elif idx == 2:
                return np.arange(11, 30)

        sweep_pts = np.arange(30)

        d = det.Function_Detector(get_function=return_variable_size_values,
                                  value_names=['Variable size counter'],
                                  detector_control='hard')
        self.MC.set_sweep_function(None_Sweep(sweep_control='hard'))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(d)
        dat = self.MC.run('varying_chunk_size')
        dset = dat["dset"]
        x = dset[:, 0]
        y = dset[:, 1]

        self.assertEqual(np.shape(dset), (len(sweep_pts), 2))
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y, sweep_pts)
        self.assertEqual(self.MC.total_nr_acquired_values, 10*30)

    def test_soft_averages_hard_sweep_1D(self):
        sweep_pts = np.arange(50)
        self.MC.soft_avg(1)
        self.MC.set_sweep_function(None_Sweep(sweep_control='hard'))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Hard(noise=.4))
        noisy_dat = self.MC.run('noisy_dat')
        noisy_dset = noisy_dat["dset"]
        x = noisy_dset[:, 0]
        y = [np.sin(x / np.pi), np.cos(x/np.pi)]
        yn_0 = abs(noisy_dset[:, 1] - y[0])
        yn_1 = abs(noisy_dset[:, 2] - y[1])

        d = self.MC.detector_function
        self.assertEqual(d.times_called, 1)

        self.MC.soft_avg(5000)
        self.MC.set_sweep_function(None_Sweep(sweep_control='hard'))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(d)
        avg_dat = self.MC.run('averaged_dat')
        avg_dset = avg_dat["dset"]
        yavg_0 = abs(avg_dset[:, 1] - y[0])
        yavg_1 = abs(avg_dset[:, 2] - y[1])

        np.testing.assert_array_almost_equal(x, sweep_pts)
        self.assertGreater(np.mean(yn_0), np.mean(yavg_0))
        self.assertGreater(np.mean(yn_1), np.mean(yavg_1))

        np.testing.assert_array_almost_equal(yavg_0, np.zeros(len(x)),
                                             decimal=2)
        np.testing.assert_array_almost_equal(yavg_1, np.zeros(len(x)),
                                             decimal=2)
        self.assertEqual(d.times_called, 5001)

    def test_soft_averages_hard_sweep_2D(self):
        self.MC.soft_avg(1)
        self.MC.live_plot_enabled(False)
        sweep_pts = np.arange(5)
        sweep_pts_2D = np.linspace(5, 10, 5)
        self.MC.set_sweep_function(None_Sweep(sweep_control='hard'))
        self.MC.set_sweep_function_2D(None_Sweep(sweep_control='soft'))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_sweep_points_2D(sweep_pts_2D)
        self.MC.set_detector_function(det.Dummy_Detector_Hard(noise=.2))
        noisy_dat = self.MC.run('2D_hard', mode='2D')
        noisy_dset = noisy_dat["dset"]
        x = noisy_dset[:, 0]
        y = noisy_dset[:, 1]
        z = [np.sin(x / np.pi), np.cos(x/np.pi)]
        z0 = abs(noisy_dset[:, 2] - z[0])
        z1 = abs(noisy_dset[:, 3] - z[1])

        x_tiled = np.tile(sweep_pts, len(sweep_pts_2D))
        y_rep = np.repeat(sweep_pts_2D, len(sweep_pts))
        np.testing.assert_array_almost_equal(x, x_tiled)
        np.testing.assert_array_almost_equal(y, y_rep)

        d = self.MC.detector_function
        self.assertEqual(d.times_called, 5)
        self.MC.set_sweep_function(None_Sweep(sweep_control='hard'))
        self.MC.set_sweep_function_2D(None_Sweep(sweep_control='soft'))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_sweep_points_2D(sweep_pts_2D)
        self.MC.soft_avg(1000)
        avg_dat = self.MC.run('averaged_dat', mode='2D')
        avg_dset = avg_dat["dset"]
        x = avg_dset[:, 0]
        y = avg_dset[:, 1]
        zavg_0 = abs(avg_dset[:, 2] - z[0])
        zavg_1 = abs(avg_dset[:, 3] - z[1])

        np.testing.assert_array_almost_equal(x, x_tiled)
        self.assertGreater(np.mean(z0), np.mean(zavg_0))
        self.assertGreater(np.mean(z1), np.mean(zavg_1))

        np.testing.assert_array_almost_equal(zavg_0, np.zeros(len(x)),
                                             decimal=2)
        np.testing.assert_array_almost_equal(zavg_1, np.zeros(len(x)),
                                             decimal=2)

        self.assertEqual(d.times_called, 5*1000+5)
        self.MC.live_plot_enabled(True)

    def test_soft_sweep_1D_soft_averages(self):
        self.mock_parabola.noise(0)
        self.mock_parabola.x(0)
        self.mock_parabola.y(0)
        self.mock_parabola.z(0)

        sweep_pts = np.linspace(0, 10, 30)
        self.MC.soft_avg(1)
        self.MC.set_sweep_function(self.mock_parabola.x)
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(self.mock_parabola.parabola)
        dat = self.MC.run('1D_soft')
        dset = dat["dset"]
        x = dset[:, 0]
        y_exp = x**2
        y0 = dset[:, 1]
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y0, y_exp, decimal=5)

        sweep_pts = np.linspace(0, 10, 30)
        self.MC.soft_avg(10)
        self.MC.set_sweep_function(self.mock_parabola.x)
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(self.mock_parabola.parabola)
        dat = self.MC.run('1D_soft')
        dset = dat["dset"]
        x = dset[:, 0]
        y_exp = x**2
        y0 = dset[:, 1]
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y0, y_exp, decimal=5)

    def test_adaptive_measurement_nelder_mead(self):
        self.MC.soft_avg(1)
        self.mock_parabola.noise(0)
        self.MC.set_sweep_functions(
            [self.mock_parabola.x, self.mock_parabola.y])
        self.MC.set_adaptive_function_parameters(
            {'adaptive_function': nelder_mead,
             'x0': [-50, -50], 'initial_step': [2.5, 2.5]})
        self.mock_parabola.noise(.5)
        self.MC.set_detector_function(self.mock_parabola.parabola)
        dat = self.MC.run('nelder-mead test', mode='adaptive')
        dset = dat["dset"]
        xf, yf, pf = dset[-1]
        self.assertLess(xf, 0.7)
        self.assertLess(yf, 0.7)
        self.assertLess(pf, 0.7)

    def test_adaptive_measurement_cma(self):
        """
        Example on how to use the cma-es evolutionary algorithm.
        Test contains comments on options that can be used
        """
        # import included in the test to avoid whole suite failing if missing
        import cma

        self.mock_parabola.noise(.01)
        self.MC.set_sweep_functions(
            [self.mock_parabola.x, self.mock_parabola.y,
             self.mock_parabola.z])
        self.MC.set_adaptive_function_parameters(
            {'adaptive_function': cma.fmin,
             'x0': [-5, 5, 5], 'sigma0': 1,
             # options for the CMA algorithm can be found using
             # "cma.CMAOptions()"
             'options': {'maxfevals': 5000,    # maximum function cals
                         # Scaling for individual sigma's
                         'cma_stds': [5, 6, 3],
                         'ftarget': 0.005},     # Target function value
             })
        self.mock_parabola.noise(.5)
        self.MC.set_detector_function(self.mock_parabola.parabola)
        dat = self.MC.run('CMA test', mode='adaptive')
        x_opt = self.MC.adaptive_result[0]
        x_mean = self.MC.adaptive_result[5]

        for i in range(3):
            self.assertLess(x_opt[i], 0.5)
            self.assertLess(x_mean[i], 0.5)

    def test_adaptive_cma_list_of_vals(self):
        """
        This tests
        """
        # import included in the test to avoid whole suite failing if missing
        import cma

        self.mock_parabola.noise(.01)
        self.MC.set_sweep_functions(
            [self.mock_parabola.x, self.mock_parabola.y,
             self.mock_parabola.z])
        self.MC.set_adaptive_function_parameters(
            {'adaptive_function': cma.fmin,
             'x0': [-5, 5, 5], 'sigma0': 1,
             # options for the CMA algorithm can be found using
             # "cma.CMAOptions()"
             'options': {'maxfevals': 5000,    # maximum function cals
                         # Scaling for individual sigma's
                         'cma_stds': [5, 6, 3],
                         'ftarget': 0.005},     # Target function value
             })
        self.mock_parabola.noise(.5)
        self.MC.set_detector_function(self.mock_parabola.parabola_list)
        dat = self.MC.run('CMA test', mode='adaptive')
        x_opt = self.MC.adaptive_result[0]
        x_mean = self.MC.adaptive_result[5]

        for i in range(3):
            self.assertLess(x_opt[i], 0.5)
            self.assertLess(x_mean[i], 0.5)

    def test_adaptive_measurement_SPSA(self):
        self.MC.soft_avg(1)
        self.mock_parabola.noise(0)
        self.mock_parabola.z(0)
        self.MC.set_sweep_functions(
            [self.mock_parabola.x, self.mock_parabola.y])
        self.MC.set_adaptive_function_parameters(
            {'adaptive_function': SPSA,
             'x0': [-50, -50],
             'a': (0.5)*(1+300)**0.602,
             'c': 0.2,
             'alpha': 1.,  # 0.602,
             'gamma': 1./6.,  # 0.101,
             'A': 300,
             'p': 0.5,
             'maxiter': 330})
        self.mock_parabola.noise(.5)
        self.MC.set_detector_function(self.mock_parabola.parabola)
        dat = self.MC.run('SPSA test', mode='adaptive')
        dset = dat["dset"]
        xf, yf, pf = dset[-1]
        self.assertLess(xf, 0.7)
        self.assertLess(yf, 0.7)
        self.assertLess(pf, 0.7)

    def test_adaptive_sampling(self):
        self.MC.soft_avg(1)
        self.mock_parabola.noise(0)
        self.MC.set_sweep_functions(
            [self.mock_parabola.x, self.mock_parabola.y])
        self.MC.set_adaptive_function_parameters({'adaptive_function': adaptive.Learner2D,
                                                  'goal': lambda l: l.npoints > 20*20,
                                                  'bounds': ((-50, +50),
                                                             (-20, +30))})
        self.MC.set_detector_function(self.mock_parabola.parabola)
        dat = self.MC.run('2D adaptive sampling test', mode='adaptive')

    def test_progress_callback(self):

        progress_param = ManualParameter('progress', initial_value=0)

        def set_progress_param_callable(progress):
            progress_param(progress)

        self.MC.on_progress_callback(set_progress_param_callable)

        self.assertEqual(progress_param(), 0)
        sweep_pts = np.linspace(0, 10, 30)
        self.MC.set_sweep_function(None_Sweep())
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Soft())
        dat = self.MC.run('1D_soft')

        self.assertEqual(progress_param(), 100)


    @classmethod
    def tearDownClass(self):
        self.MC.close()
        self.mock_parabola.close()
        del self.station.components['MC']
        del self.station.components['mock_parabola']

    def test_persist_mode(self):
        sweep_pts = np.linspace(0, 10, 5)
        self.MC.persist_mode(True)
        self.MC.set_sweep_function(None_Sweep(sweep_control='hard'))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Hard())
        dat = self.MC.run('1D_hard')
        dset = dat["dset"]
        x = dset[:, 0]
        y = [np.sin(x / np.pi), np.cos(x/np.pi)]
        y0 = dset[:, 1]
        y1 = dset[:, 2]
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y0, y[0])
        np.testing.assert_array_almost_equal(y1, y[1])
        d = self.MC.detector_function
        self.assertEqual(d.times_called, 1)

        persist_dat = self.MC._persist_dat
        x_p = persist_dat[:, 0]
        y0_p = persist_dat[:, 1]
        y1_p = persist_dat[:, 2]
        np.testing.assert_array_almost_equal(x, x_p)
        np.testing.assert_array_almost_equal(y0, y0_p)
        np.testing.assert_array_almost_equal(y1, y1_p)

        self.MC.clear_persitent_plot()
        self.assertEqual(self.MC._persist_dat, None)

    def test_data_resolution(self):
        # This test will fail if the data is saved as 32 bit floats
        sweep_pts = [3e9+1e-3, 3e9+2e-3]
        self.MC.set_sweep_function(None_Sweep())
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Soft())
        dat = self.MC.run('1D_soft')
        x = dat['dset'][:, 0]
        np.testing.assert_array_almost_equal(x, sweep_pts, decimal=5)

    def test_save_exp_metadata(self):
        metadata_dict = {
            'intParam': 1,
            'floatParam': 2.5e-3,
            'strParam': 'spam',
            'listParam': [1, 2, 3, 4],
            'arrayParam': np.array([4e5, 5e5]),
            'dictParam': {'a': 1, 'b': 2},
            'tupleParam': (3, 'c')
        }

        old_a_tools_datadir = a_tools.datadir
        a_tools.datadir = self.MC.datadir()

        sweep_pts = np.linspace(0, 10, 30)
        self.MC.set_sweep_function(None_Sweep())
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Soft())
        self.MC.run('test_exp_metadata', exp_metadata=metadata_dict)
        a = ma.MeasurementAnalysis(label='test_exp_metadata', auto=False)

        a_tools.datadir = old_a_tools_datadir

        loaded_dict = read_dict_from_hdf5(
            {}, a.data_file['Experimental Data']['Experimental Metadata'])

        np.testing.assert_equal(metadata_dict, loaded_dict)
コード例 #6
0
class Test_HDF5(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        self.station = station.Station()
        self.datadir = os.path.join(pq.__path__[0], 'tests', 'test_data')
        self.MC = measurement_control.MeasurementControl(
            'MC', live_plot_enabled=False, verbose=False)
        self.MC.station = self.station
        self.MC.datadir(self.datadir)
        a_tools.datadir = self.datadir
        self.station.add_component(self.MC)

        self.mock_parabola = DummyParHolder('mock_parabola')
        self.station.add_component(self.mock_parabola)
        self.mock_parabola_2 = DummyParHolder('mock_parabola_2')
        self.station.add_component(self.mock_parabola_2)

    @classmethod
    def tearDownClass(self):
        self.MC.close()
        self.mock_parabola.close()
        self.mock_parabola_2.close()

    def test_storing_and_loading_station_snapshot(self):
        """
        Stores and writes a station (instrument) snapshot.
        """

        self.mock_parabola_2.x(1)
        self.mock_parabola_2.y(2.245)
        self.mock_parabola_2.array_like(np.linspace(0, 11, 23))

        snap = self.station.snapshot(update=True)
        data_object = h5d.Data(name='test_object_snap', datadir=self.datadir)
        h5d.write_dict_to_hdf5(snap, data_object)
        data_object.close()
        filepath = data_object.filepath

        new_dict = {}
        opened_hdf5_file = h5py.File(filepath, 'r')
        try:
            h5d.read_dict_from_hdf5(new_dict, opened_hdf5_file)

            self.assertEqual(snap.keys(), new_dict.keys())
            self.assertEqual(snap['instruments'].keys(),
                             new_dict['instruments'].keys())
            mock_parab_pars = snap['instruments']['mock_parabola_2']['parameters']

            self.assertEqual(mock_parab_pars['x']['value'],
                             1)
            self.assertEqual(mock_parab_pars['y']['value'],
                             2.245)
            np.testing.assert_array_equal(
                mock_parab_pars['array_like']['value'],
                np.linspace(0, 11, 23))
            opened_hdf5_file.close()
        except Exception as e:
            opened_hdf5_file.close()
            raise e


    def test_writing_and_reading_dicts_to_hdf5(self):
        """
        Tests dumping some random dictionary to hdf5 and reading back the
        stored values. The input dictionary contains:
            - list of ints
            - list of floats
            - nested dict
            - 1D array
            - 2D array

        """
        test_dict = {
            'list_of_ints': list(np.arange(5)),
            'list_of_floats': list(np.arange(5.1)),
            'some_bool': True,
            'weird_dict': {'a': 5},
            'dataset1': np.linspace(0, 20, 31),
            'dataset2': np.array([[2, 3, 4, 5],
                                  [2, 3, 1, 2]]),
            'list_of_mixed_type': ['hello', 4, 4.2, {'a': 5}, [4, 3]],
            'tuple_of_mixed_type': tuple(['hello', 4, 4.2, {'a': 5}, [4, 3]]),
            'a list of strings': ['my ', 'name ', 'is ', 'earl.'],
            'some_np_bool': np.bool(True),
            'list_of_dicts': [{'a': 5}, {'b': 3}],
            'some_int': 3,
            'some_float': 3.5,
            'some_np_int': np.int(3),
            'some_np_float': np.float(3.5)
        }

        data_object = h5d.Data(name='test_object', datadir=self.datadir)
        h5d.write_dict_to_hdf5(test_dict, data_object)
        data_object.close()
        filepath = data_object.filepath

        new_dict = {}
        opened_hdf5_file = h5py.File(filepath, 'r')
        try:
            h5d.read_dict_from_hdf5(new_dict, opened_hdf5_file)
            # objects are not identical but the string representation should be
            self.assertEqual(test_dict.keys(), new_dict.keys())
            self.assertEqual(test_dict['list_of_ints'], new_dict['list_of_ints'])
            self.assertEqual(test_dict['list_of_floats'],
                             new_dict['list_of_floats'])
            self.assertEqual(test_dict['weird_dict'], new_dict['weird_dict'])
            self.assertEqual(test_dict['some_bool'], new_dict['some_bool'])

            self.assertEqual(test_dict['list_of_dicts'],
                             new_dict['list_of_dicts'])

            self.assertEqual(test_dict['list_of_mixed_type'],
                             new_dict['list_of_mixed_type'])
            self.assertEqual(test_dict['list_of_mixed_type'][0],
                             new_dict['list_of_mixed_type'][0])
            self.assertEqual(test_dict['list_of_mixed_type'][2],
                             new_dict['list_of_mixed_type'][2])

            self.assertEqual(test_dict['tuple_of_mixed_type'],
                             new_dict['tuple_of_mixed_type'])
            self.assertEqual(type(test_dict['tuple_of_mixed_type']),
                             type(new_dict['tuple_of_mixed_type']))
            self.assertEqual(test_dict['tuple_of_mixed_type'][0],
                             new_dict['tuple_of_mixed_type'][0])
            self.assertEqual(test_dict['tuple_of_mixed_type'][2],
                             new_dict['tuple_of_mixed_type'][2])

            self.assertEqual(test_dict['some_np_bool'],
                             new_dict['some_np_bool'])
            self.assertEqual(test_dict['some_int'], new_dict['some_int'])
            self.assertEqual(test_dict['some_np_float'], new_dict['some_np_float'])
            self.assertEqual(test_dict['a list of strings'],
                             new_dict['a list of strings'])
            self.assertEqual(test_dict['a list of strings'][0],
                             new_dict['a list of strings'][0])
            opened_hdf5_file.close()
        except Exception as e:
            opened_hdf5_file.close()
            raise e

    def test_loading_settings_onto_instrument(self):
        """
        Tests storing and reading of parameters.
        Tests for different types of parameters including
        - array
        - float
        - int
        - dict
        - bool
        """
        arr = np.linspace(12, 42, 11)
        self.mock_parabola.array_like(arr)
        self.mock_parabola.x(42.23)
        self.mock_parabola.y(2)
        self.mock_parabola.status(True)
        self.mock_parabola.dict_like({'a': {'b': [2, 3, 5]}})

        self.MC.set_sweep_function(self.mock_parabola.x)
        self.MC.set_sweep_points([0, 1])
        self.MC.set_detector_function(self.mock_parabola.skewed_parabola)
        self.MC.run('test_MC_snapshot_storing')
        self.mock_parabola.array_like(arr+5)
        self.mock_parabola.x(13)
        # Test that these are not the same as before the experiment
        np.testing.assert_array_equal(self.mock_parabola.array_like(),
                                      arr+5)
        self.assertEqual(self.mock_parabola.x(), 13)

        # Now load the settings from the last file
        gen.load_settings_onto_instrument_v2(self.mock_parabola,
                                             label='test_MC_snapshot_storing')

        # Test that these are not the same as before the experiment
        np.testing.assert_array_equal(self.mock_parabola.array_like(),
                                      arr)
        self.assertEqual(self.mock_parabola.x(), 42.23)

        # Loading it into another instrument to test for the other values
        gen.load_settings_onto_instrument_v2(
            self.mock_parabola_2,
            load_from_instr=self.mock_parabola.name,
            label='test_MC_snapshot_storing')
        self.assertEqual(self.mock_parabola_2.y(), 2)
        self.assertEqual(self.mock_parabola_2.status(), True)
        self.assertEqual(self.mock_parabola_2.dict_like(),
                         {'a': {'b': [2, 3, 5]}})
コード例 #7
0
class Test_MeasurementControl(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.station = station.Station()
        self.MC = measurement_control.MeasurementControl(
            "MC", live_plot_enabled=True, verbose=True)
        self.MC.station = self.station
        self.station.add_component(self.MC)

        self.mock_parabola = DummyParHolder("mock_parabola")
        self.station.add_component(self.mock_parabola)

    def setUp(self):
        self.MC.soft_avg(1)

    def test_soft_sweep_1D(self):

        sweep_pts = np.linspace(0, 10, 30)
        self.MC.set_sweep_function(None_Sweep())
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Soft())
        dat = self.MC.run("1D_soft")
        dset = dat["dset"]
        x = dset[:, 0]
        xr = np.arange(len(x)) / 15
        y = np.array([np.sin(xr / np.pi), np.cos(xr / np.pi)])
        y0 = dset[:, 1]
        y1 = dset[:, 2]
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y0, y[0, :])
        np.testing.assert_array_almost_equal(y1, y[1, :])

        # Test that the return dictionary has the right entries
        dat_keys = set([
            "dset",
            "opt_res",
            "opt_res_dset",
            "sweep_parameter_names",
            "sweep_parameter_units",
            "value_names",
            "value_units",
        ])
        self.assertEqual(dat_keys, set(dat.keys()))

        self.assertEqual(dat["sweep_parameter_names"], ["pts"])
        self.assertEqual(dat["sweep_parameter_units"], ["arb. unit"])
        self.assertEqual(dat["value_names"], ["I", "Q"])
        self.assertEqual(dat["value_units"], ["V", "V"])

    def test_soft_sweep_1D_alt_shape(self):
        # This is a generalization of a 1D sweep function where instead of
        # a shape (2,) it has a shape (2,1). This isinconsistent with the
        # N-D hard sweeps. and should be addressed

        sweep_pts = np.linspace(0, 10, 30)
        self.MC.set_sweep_function(None_Sweep())
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Soft_diff_shape())
        dat = self.MC.run("1D_soft")
        dset = dat["dset"]
        x = dset[:, 0]
        xr = np.arange(len(x)) / 15
        y = np.array([np.sin(xr / np.pi), np.cos(xr / np.pi)])
        y0 = dset[:, 1]
        y1 = dset[:, 2]
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y0, y[0, :])
        np.testing.assert_array_almost_equal(y1, y[1, :])

        # Test that the return dictionary has the right entries
        dat_keys = set([
            "dset",
            "opt_res",
            "opt_res_dset",
            "sweep_parameter_names",
            "sweep_parameter_units",
            "value_names",
            "value_units",
        ])
        self.assertEqual(dat_keys, set(dat.keys()))

        self.assertEqual(dat["sweep_parameter_names"], ["pts"])
        self.assertEqual(dat["sweep_parameter_units"], ["arb. unit"])
        self.assertEqual(dat["value_names"], ["I", "Q"])
        self.assertEqual(dat["value_units"], ["V", "V"])

    @unittest.skipIf(True, "This test is currently broken")
    def test_data_location(self):
        sweep_pts = np.linspace(0, 10, 30)
        self.MC.set_sweep_function(None_Sweep())
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Soft())
        self.MC.run("datadir_test_file")
        # raises an error if the file is not found
        ma.MeasurementAnalysis(label="datadir_test_file")

        # change the datadir
        test_dir2 = os.path.abspath(
            os.path.join(os.path.dirname(pq.__file__), os.pardir,
                         "data_test_2"))
        self.MC.datadir(test_dir2)

        sweep_pts = np.linspace(0, 10, 30)
        self.MC.set_sweep_function(None_Sweep())
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Soft())
        self.MC.run("datadir_test_file_2")
        # raises an error if the file is not found
        with self.assertRaises(Exception):
            ma.MeasurementAnalysis(label="datadir_test_file_2")
        ma.a_tools.datadir = test_dir2
        # changing the dir makes it find the file now
        ma.MeasurementAnalysis(label="datadir_test_file_2")
        self.MC.datadir(get_default_datadir())

    def test_hard_sweep_1D(self):
        sweep_pts = np.linspace(0, 10, 5)
        self.MC.set_sweep_function(None_Sweep(sweep_control="hard"))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Hard())
        dat = self.MC.run("1D_hard")
        dset = dat["dset"]
        x = dset[:, 0]
        y = [np.sin(x / np.pi), np.cos(x / np.pi)]
        y0 = dset[:, 1]
        y1 = dset[:, 2]
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y0, y[0])
        np.testing.assert_array_almost_equal(y1, y[1])
        d = self.MC.detector_function
        self.assertEqual(d.times_called, 1)

    def test_soft_sweep_2D(self):
        sweep_pts = np.linspace(0, 10, 30)
        sweep_pts_2D = np.linspace(0, 10, 5)
        self.MC.set_sweep_function(None_Sweep(sweep_control="soft"))
        self.MC.set_sweep_function_2D(None_Sweep(sweep_control="soft"))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_sweep_points_2D(sweep_pts_2D)
        self.MC.set_detector_function(det.Dummy_Detector_Soft())
        dat = self.MC.run("2D_soft", mode="2D")
        dset = dat["dset"]
        x = dset[:, 0]
        y = dset[:, 1]
        xr = np.arange(len(sweep_pts) * len(sweep_pts_2D)) / 15
        z = np.array([np.sin(xr / np.pi), np.cos(xr / np.pi)])
        z0 = dset[:, 2]
        z1 = dset[:, 3]

        x_tiled = np.tile(sweep_pts, len(sweep_pts_2D))
        y_rep = np.repeat(sweep_pts_2D, len(sweep_pts))
        np.testing.assert_array_almost_equal(x, x_tiled)
        np.testing.assert_array_almost_equal(y, y_rep)
        np.testing.assert_array_almost_equal(z0, z[0, :])
        np.testing.assert_array_almost_equal(z1, z[1, :])

    def test_soft_sweep_2D_with_reading_of_set_parameter(self):
        sweep_pts = np.linspace(0, 10, 30)
        sweep_pts_2D = np.linspace(0, 10, 5)
        self.MC.set_sweep_function(
            None_Sweep_With_Parameter_Returned(sweep_control="soft"))
        self.MC.set_sweep_function_2D(
            None_Sweep_With_Parameter_Returned(sweep_control="soft"))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_sweep_points_2D(sweep_pts_2D)
        self.MC.set_detector_function(det.Dummy_Detector_Soft())
        dat = self.MC.run("2D_soft", mode="2D")
        dset = dat["dset"]
        x = dset[:, 0]
        y = dset[:, 1]
        xr = np.arange(len(sweep_pts) * len(sweep_pts_2D)) / 15
        z = np.array([np.sin(xr / np.pi), np.cos(xr / np.pi)])
        z0 = dset[:, 2]
        z1 = dset[:, 3]

        # The +0.1 is to test if the return value is matching
        x_tiled = np.tile(sweep_pts + 0.1, len(sweep_pts_2D))
        y_rep = np.repeat(sweep_pts_2D + 0.1, len(sweep_pts))
        np.testing.assert_array_almost_equal(x, x_tiled)
        np.testing.assert_array_almost_equal(y, y_rep)
        np.testing.assert_array_almost_equal(z0, z[0, :])
        np.testing.assert_array_almost_equal(z1, z[1, :])

    def test_soft_sweep_2D_function_calls(self):
        sweep_pts = np.arange(0, 30, 1)
        sweep_pts_2D = np.arange(0, 5, 1)
        s1 = None_Sweep_idx(sweep_control="soft")
        s2 = None_Sweep_idx(sweep_control="soft")
        self.MC.set_sweep_function(s1)
        self.MC.set_sweep_function_2D(s2)
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_sweep_points_2D(sweep_pts_2D)
        self.MC.set_detector_function(det.Dummy_Detector_Soft())

        self.assertEqual(s1.num_calls, 0)
        self.assertEqual(s2.num_calls, 0)
        self.MC.run("2D_soft", mode="2D")

        # Test that the 2D scan only gets called 5 times (when it changes)
        # The 1D value always changes and as such should always be called
        self.assertEqual(s1.num_calls, 30 * 5)
        self.assertEqual(s2.num_calls, 5)

    def test_hard_sweep_2D(self):
        """
        Hard inner loop, soft outer loop
        """
        sweep_pts = np.linspace(10, 20, 3)
        sweep_pts_2D = np.linspace(0, 10, 5)
        self.MC.live_plot_enabled(False)
        self.MC.set_sweep_function(None_Sweep(sweep_control="hard"))
        self.MC.set_sweep_function_2D(None_Sweep(sweep_control="soft"))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_sweep_points_2D(sweep_pts_2D)
        self.MC.set_detector_function(det.Dummy_Detector_Hard())
        dat = self.MC.run("2D_hard", mode="2D")
        dset = dat["dset"]
        x = dset[:, 0]
        y = dset[:, 1]
        z = self.data = [np.sin(x / np.pi), np.cos(x / np.pi)]
        z0 = dset[:, 2]
        z1 = dset[:, 3]

        x_tiled = np.tile(sweep_pts, len(sweep_pts_2D))
        y_rep = np.repeat(sweep_pts_2D, len(sweep_pts))
        np.testing.assert_array_almost_equal(x, x_tiled)
        np.testing.assert_array_almost_equal(y, y_rep)
        np.testing.assert_array_almost_equal(z0, z[0])
        np.testing.assert_array_almost_equal(z1, z[1])
        d = self.MC.detector_function
        self.assertEqual(d.times_called, 5)

        self.MC.live_plot_enabled(True)

    def test_many_shots_hard_sweep(self):
        """
        Tests acquiring more than the maximum number of shots for a hard
        detector by setting the number of sweep points high
        """
        sweep_pts = np.arange(50)
        self.MC.set_sweep_function(None_Sweep(sweep_control="hard"))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Shots_Detector(max_shots=5))
        dat = self.MC.run("man_shots")
        dset = dat["dset"]
        x = dset[:, 0]
        y = dset[:, 1]

        self.assertEqual(np.shape(dset), (len(sweep_pts), 2))
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y, sweep_pts)

        d = self.MC.detector_function
        self.assertEqual(d.times_called, 10)

    def test_variable_sized_return_values_hard_sweep(self):
        """
        Tests a detector that acquires data in chunks of varying sizes
        """
        self.MC.soft_avg(1)
        counter_param = ManualParameter("counter", initial_value=0)

        def return_variable_size_values():
            idx = counter_param() % 3
            counter_param(counter_param() + 1)

            if idx == 0:
                return np.arange(0, 7)
            elif idx == 1:
                return np.arange(7, 11)
            elif idx == 2:
                return np.arange(11, 30)

        sweep_pts = np.arange(30)

        d = det.Function_Detector(
            get_function=return_variable_size_values,
            value_names=["Variable size counter"],
            detector_control="hard",
        )
        self.MC.set_sweep_function(None_Sweep(sweep_control="hard"))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(d)
        dat = self.MC.run("varying_chunk_size")
        dset = dat["dset"]
        x = dset[:, 0]
        y = dset[:, 1]

        self.assertEqual(np.shape(dset), (len(sweep_pts), 2))
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y, sweep_pts)

        self.assertEqual(self.MC.total_nr_acquired_values, 1 * 30)

    def test_soft_sweep_hard_det_1D(self):
        def mock_func():
            # to also test if the values are set correctly in the sweep
            arr = np.zeros([2, 2])
            arr[0, :] = np.array([self.mock_parabola.x()] * 2)
            arr[1, :] = np.array([self.mock_parabola.x() + 2] * 2)
            return arr

        d = det.Function_Detector(get_function=mock_func,
                                  value_names=["x", "x+2"],
                                  detector_control="hard")
        sweep_pts = np.repeat(np.arange(5), 2)
        self.MC.set_sweep_function(self.mock_parabola.x)
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(d)
        dat = self.MC.run("soft_sweep_hard_det")
        dset = dat["dset"]

        x = dset[:, 0]
        y0 = dset[:, 1]
        y1 = dset[:, 2]
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y0, sweep_pts)
        np.testing.assert_array_almost_equal(y1, sweep_pts + 2)

    def test_variable_sized_return_values_hard_sweep_soft_avg(self):
        """
        Tests a detector that acquires data in chunks of varying sizes
        """
        self.MC.soft_avg(10)
        counter_param = ManualParameter("counter", initial_value=0)

        def return_variable_size_values():
            idx = counter_param() % 3
            counter_param(counter_param() + 1)

            if idx == 0:
                return np.arange(0, 7)
            elif idx == 1:
                return np.arange(7, 11)
            elif idx == 2:
                return np.arange(11, 30)

        sweep_pts = np.arange(30)

        d = det.Function_Detector(
            get_function=return_variable_size_values,
            value_names=["Variable size counter"],
            detector_control="hard",
        )
        self.MC.set_sweep_function(None_Sweep(sweep_control="hard"))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(d)
        dat = self.MC.run("varying_chunk_size")
        dset = dat["dset"]
        x = dset[:, 0]
        y = dset[:, 1]

        self.assertEqual(np.shape(dset), (len(sweep_pts), 2))
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y, sweep_pts)
        self.assertEqual(self.MC.total_nr_acquired_values, 10 * 30)

    def test_soft_averages_hard_sweep_1D(self):
        sweep_pts = np.arange(50)
        self.MC.soft_avg(1)
        self.MC.set_sweep_function(None_Sweep(sweep_control="hard"))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Hard(noise=0.4))
        noisy_dat = self.MC.run("noisy_dat")
        noisy_dset = noisy_dat["dset"]
        x = noisy_dset[:, 0]
        y = [np.sin(x / np.pi), np.cos(x / np.pi)]
        yn_0 = abs(noisy_dset[:, 1] - y[0])
        yn_1 = abs(noisy_dset[:, 2] - y[1])

        d = self.MC.detector_function
        self.assertEqual(d.times_called, 1)

        self.MC.soft_avg(5000)
        self.MC.set_sweep_function(None_Sweep(sweep_control="hard"))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(d)
        avg_dat = self.MC.run("averaged_dat")
        avg_dset = avg_dat["dset"]
        yavg_0 = abs(avg_dset[:, 1] - y[0])
        yavg_1 = abs(avg_dset[:, 2] - y[1])

        np.testing.assert_array_almost_equal(x, sweep_pts)
        self.assertGreater(np.mean(yn_0), np.mean(yavg_0))
        self.assertGreater(np.mean(yn_1), np.mean(yavg_1))

        np.testing.assert_array_almost_equal(yavg_0,
                                             np.zeros(len(x)),
                                             decimal=2)
        np.testing.assert_array_almost_equal(yavg_1,
                                             np.zeros(len(x)),
                                             decimal=2)
        self.assertEqual(d.times_called, 5001)

    def test_soft_averages_hard_sweep_2D(self):
        self.MC.soft_avg(1)
        self.MC.live_plot_enabled(False)
        sweep_pts = np.arange(5)
        sweep_pts_2D = np.linspace(5, 10, 5)
        self.MC.set_sweep_function(None_Sweep(sweep_control="hard"))
        self.MC.set_sweep_function_2D(None_Sweep(sweep_control="soft"))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_sweep_points_2D(sweep_pts_2D)
        self.MC.set_detector_function(det.Dummy_Detector_Hard(noise=0.2))
        noisy_dat = self.MC.run("2D_hard", mode="2D")
        noisy_dset = noisy_dat["dset"]
        x = noisy_dset[:, 0]
        y = noisy_dset[:, 1]
        z = [np.sin(x / np.pi), np.cos(x / np.pi)]
        z0 = abs(noisy_dset[:, 2] - z[0])
        z1 = abs(noisy_dset[:, 3] - z[1])

        x_tiled = np.tile(sweep_pts, len(sweep_pts_2D))
        y_rep = np.repeat(sweep_pts_2D, len(sweep_pts))
        np.testing.assert_array_almost_equal(x, x_tiled)
        np.testing.assert_array_almost_equal(y, y_rep)

        d = self.MC.detector_function
        self.assertEqual(d.times_called, 5)
        self.MC.set_sweep_function(None_Sweep(sweep_control="hard"))
        self.MC.set_sweep_function_2D(None_Sweep(sweep_control="soft"))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_sweep_points_2D(sweep_pts_2D)
        self.MC.soft_avg(1000)
        avg_dat = self.MC.run("averaged_dat", mode="2D")
        avg_dset = avg_dat["dset"]
        x = avg_dset[:, 0]
        y = avg_dset[:, 1]
        zavg_0 = abs(avg_dset[:, 2] - z[0])
        zavg_1 = abs(avg_dset[:, 3] - z[1])

        np.testing.assert_array_almost_equal(x, x_tiled)
        self.assertGreater(np.mean(z0), np.mean(zavg_0))
        self.assertGreater(np.mean(z1), np.mean(zavg_1))

        np.testing.assert_array_almost_equal(zavg_0,
                                             np.zeros(len(x)),
                                             decimal=2)
        np.testing.assert_array_almost_equal(zavg_1,
                                             np.zeros(len(x)),
                                             decimal=2)

        self.assertEqual(d.times_called, 5 * 1000 + 5)
        self.MC.live_plot_enabled(True)

    def test_soft_sweep_1D_soft_averages(self):
        self.mock_parabola.noise(0)
        self.mock_parabola.x(0)
        self.mock_parabola.y(0)
        self.mock_parabola.z(0)

        sweep_pts = np.linspace(0, 10, 30)
        self.MC.soft_avg(1)
        self.MC.set_sweep_function(self.mock_parabola.x)
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(self.mock_parabola.parabola)
        dat = self.MC.run("1D_soft")
        dset = dat["dset"]
        x = dset[:, 0]
        y_exp = x**2
        y0 = dset[:, 1]
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y0, y_exp, decimal=5)

        sweep_pts = np.linspace(0, 10, 30)
        self.MC.soft_avg(10)
        self.MC.set_sweep_function(self.mock_parabola.x)
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(self.mock_parabola.parabola)
        dat = self.MC.run("1D_soft")
        dset = dat["dset"]
        x = dset[:, 0]
        y_exp = x**2
        y0 = dset[:, 1]
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y0, y_exp, decimal=5)

    def test_adaptive_measurement_nelder_mead(self):
        self.MC.soft_avg(1)
        self.mock_parabola.noise(0)
        self.MC.set_sweep_functions(
            [self.mock_parabola.x, self.mock_parabola.y])
        self.MC.set_adaptive_function_parameters({
            "adaptive_function": nelder_mead,
            "x0": [-50, -50],
            "initial_step": [2.5, 2.5],
        })
        self.mock_parabola.noise(0.5)
        self.MC.set_detector_function(self.mock_parabola.parabola)
        dat = self.MC.run("nelder-mead test", mode="adaptive")
        dset = dat["dset"]
        xf, yf, pf = dset[-1]
        self.assertLess(xf, 0.7)
        self.assertLess(yf, 0.7)
        self.assertLess(pf, 0.7)

    def test_adaptive_iter_plot(self):
        """
        Check that the evolution of parameters over iterations is
        plotted correctly
        """
        self.MC.soft_avg(1)
        self.mock_parabola.noise(0)
        self.MC.set_sweep_functions(
            [self.mock_parabola.x, self.mock_parabola.y, self.mock_parabola.z])
        self.MC.set_adaptive_function_parameters({
            "adaptive_function":
            nelder_mead,
            "x0": [-50, -50, -50],
            "initial_step": [2.5, 2.5, 2.5],
        })
        self.mock_parabola.noise(0.5)
        self.MC.set_detector_function(self.mock_parabola.parabola)
        self.MC.run("adaptive params iter plot test", mode="adaptive")
        assert "x" in self.MC.secondary_QtPlot.traces[0]["config"]["ylabel"]
        assert "parabola" in self.MC.secondary_QtPlot.traces[-2]["config"][
            "ylabel"]

    def test_adaptive_measurement_cma(self):
        """
        Example on how to use the cma-es evolutionary algorithm.
        Test contains comments on options that can be used
        """
        # import included in the test to avoid whole suite failing if missing
        import cma

        self.mock_parabola.noise(0.01)
        self.MC.set_sweep_functions(
            [self.mock_parabola.x, self.mock_parabola.y, self.mock_parabola.z])
        self.MC.set_adaptive_function_parameters({
            "adaptive_function": cma.fmin,
            "x0": [-5, 5, 5],
            "sigma0": 1,
            # options for the CMA algorithm can be found using
            # "cma.CMAOptions()"
            "options": {
                "maxfevals": 5000,  # maximum function cals
                # Scaling for individual sigma's
                "cma_stds": [5, 6, 3],
                "ftarget": 0.005,
            },  # Target function value
        })
        self.mock_parabola.noise(0.5)
        self.MC.set_detector_function(self.mock_parabola.parabola)
        dat = self.MC.run("CMA test", mode="adaptive")
        x_opt = self.MC.adaptive_result[0]
        x_mean = self.MC.adaptive_result[5]

        for i in range(3):
            self.assertLess(x_opt[i], 0.5)
            self.assertLess(x_mean[i], 0.5)

    def test_adaptive_cma_list_of_vals(self):
        """
        This tests
        """
        # import included in the test to avoid whole suite failing if missing
        import cma

        self.mock_parabola.noise(0.01)
        self.MC.set_sweep_functions(
            [self.mock_parabola.x, self.mock_parabola.y, self.mock_parabola.z])
        self.MC.set_adaptive_function_parameters({
            "adaptive_function": cma.fmin,
            "x0": [-5, 5, 5],
            "sigma0": 1,
            # options for the CMA algorithm can be found using
            # "cma.CMAOptions()"
            "options": {
                "maxfevals": 5000,  # maximum function cals
                # Scaling for individual sigma's
                "cma_stds": [5, 6, 3],
                "ftarget": 0.005,
            },  # Target function value
        })
        self.mock_parabola.noise(0.5)
        self.MC.set_detector_function(self.mock_parabola.parabola_list)
        dat = self.MC.run("CMA test", mode="adaptive")
        x_opt = self.MC.adaptive_result[0]
        x_mean = self.MC.adaptive_result[5]

        for i in range(3):
            self.assertLess(x_opt[i], 0.5)
            self.assertLess(x_mean[i], 0.5)

    def test_adaptive_measurement_SPSA(self):
        self.MC.soft_avg(1)
        self.mock_parabola.noise(0)
        self.mock_parabola.z(0)
        self.MC.set_sweep_functions(
            [self.mock_parabola.x, self.mock_parabola.y])
        self.MC.set_adaptive_function_parameters({
            "adaptive_function": SPSA,
            "x0": [-50, -50],
            "a": (0.5) * (1 + 300)**0.602,
            "c": 0.2,
            "alpha": 1.0,  # 0.602,
            "gamma": 1.0 / 6.0,  # 0.101,
            "A": 300,
            "p": 0.5,
            "maxiter": 330,
        })
        self.mock_parabola.noise(0.5)
        self.MC.set_detector_function(self.mock_parabola.parabola)
        dat = self.MC.run("SPSA test", mode="adaptive")
        dset = dat["dset"]
        xf, yf, pf = dset[-1]
        self.assertLess(xf, 0.7)
        self.assertLess(yf, 0.7)
        self.assertLess(pf, 0.7)

    def test_adaptive_sampling(self):
        self.MC.soft_avg(1)
        self.mock_parabola.noise(0)
        self.MC.set_sweep_functions(
            [self.mock_parabola.x, self.mock_parabola.y])
        self.MC.set_adaptive_function_parameters({
            "adaptive_function":
            adaptive.Learner2D,
            "goal":
            lambda l: l.npoints > 20 * 20,
            "bounds": ((-50, +50), (-20, +30)),
        })
        self.MC.set_detector_function(self.mock_parabola.parabola)
        dat = self.MC.run("2D adaptive sampling test", mode="adaptive")

    def test_adaptive_X0_x_scale(self):
        self.MC.soft_avg(1)
        self.mock_parabola.noise(0)
        self.MC.set_sweep_functions(
            [self.mock_parabola.x, self.mock_parabola.y])
        self.MC.set_adaptive_function_parameters({
            "adaptive_function":
            adaptive.Learner2D,
            "goal":
            lambda l: l.npoints > 20,
            "bounds": ((-50, +50), (-20, +30)),
            "X0": (-20., 15.),
            "x_scale": (100., 100.)
        })
        self.MC.set_detector_function(self.mock_parabola.parabola)
        dat = self.MC.run("2D adaptive sampling X0 scaling test",
                          mode="adaptive")

        assert self.MC.learner.data[(-2000., 1500.)]

    def test_adaptive_X0s_x_scale(self):
        self.MC.soft_avg(1)
        self.mock_parabola.noise(0)
        self.MC.set_sweep_functions(
            [self.mock_parabola.x, self.mock_parabola.y])
        self.MC.set_adaptive_function_parameters({
            "adaptive_function":
            adaptive.Learner2D,
            "goal":
            lambda l: l.npoints > 20,
            "bounds": ((-50, +50), (-20, +30)),
            "X0": [(-20., 15.), (-19., 16.), (-18., 17.)],
            "x_scale": (100., 100.)
        })
        self.MC.set_detector_function(self.mock_parabola.parabola)
        dat = self.MC.run("2D adaptive sampling X0 scaling test",
                          mode="adaptive")

        assert self.MC.learner.data[(-2000., 1500.)]

    def test_adaptive_x_scale_bounds_2D(self):
        self.MC.soft_avg(1)
        self.mock_parabola.noise(0)
        self.MC.set_sweep_functions(
            [self.mock_parabola.x, self.mock_parabola.y])
        bounds = ((-50, +50), (-20, +30))
        x_scale = (10, 1000.)
        self.MC.set_adaptive_function_parameters({
            "adaptive_function":
            adaptive.Learner2D,
            "goal":
            lambda l: l.npoints > 10 * 10,
            "bounds":
            bounds,
            "x_scale":
            x_scale
        })
        self.MC.set_detector_function(self.mock_parabola.parabola)
        dat = self.MC.run("2D adaptive x_scale bounds 2D test",
                          mode="adaptive")

        l_b = tuple(
            tuple(b for b in b_dim) for b_dim in self.MC.learner.bounds)

        assert l_b == ((-500, +500), (-20000., +30000.))

    def test_adaptive_x_scale_hull(self):
        self.MC.soft_avg(1)
        self.mock_parabola.noise(0)
        self.MC.set_sweep_functions(
            [self.mock_parabola.x, self.mock_parabola.y])
        bounds = np.array([
            [-40, -20],
            [+30, -20],
            [+45, +25],
            [+35, +25],
        ])
        bounds = ConvexHull(bounds)
        x_scale = (100, 0.1)
        self.MC.set_adaptive_function_parameters({
            "adaptive_function":
            adaptive.LearnerND,
            "goal":
            lambda l: l.npoints > 10 * 10,
            "bounds":
            bounds,
            "x_scale":
            x_scale
        })
        self.MC.set_detector_function(self.mock_parabola.parabola)
        dat = self.MC.run("2D adaptive x_scale bounds 2D test",
                          mode="adaptive")

        l_hull = self.MC.learner.bounds

        assert np.all(l_hull.points == np.array([
            [-4000, -2.0],
            [+3000, -2.0],
            [+4500, +2.5],
            [+3500, +2.5],
        ]))

    def test_adaptive_x_scale_bounds_1D(self):
        self.MC.soft_avg(1)
        self.mock_parabola.noise(0)
        self.MC.set_sweep_function(self.mock_parabola.x)
        bounds = (-50., +50)
        x_scale = 10
        self.MC.set_adaptive_function_parameters({
            "adaptive_function":
            adaptive.Learner1D,
            "goal":
            lambda l: l.npoints > 20,
            "bounds":
            bounds,
            "x_scale":
            x_scale
        })
        self.MC.set_detector_function(self.mock_parabola.parabola)
        dat = self.MC.run("2D adaptive x_scale bounds 1D test",
                          mode="adaptive")

        assert tuple(b for b in self.MC.learner.bounds) == (-500., +500)

    def test_simulataneous_1D_adaptive_plus_1D_linear_sweep(self):
        self.MC.soft_avg(1)
        self.mock_parabola.noise(0)
        self.MC.set_sweep_functions(
            [self.mock_parabola.x, self.mock_parabola.y_int])
        loss = mk_minimization_loss_func()
        goal = mk_minimization_goal_func()

        self.MC.set_adaptive_function_parameters({
            "adaptive_function":
            Learner1D_Minimizer,
            "goal":
            lambda l: l.npoints > 15 or goal(l),
            "bounds": (-50., +50.),
            "loss_per_interval":
            loss,
            "extra_dims_sweep_pnts": [x for x in range(-12, 12)]
        })
        self.MC.set_detector_function(self.mock_parabola.parabola_float_int)
        dat = self.MC.run("1D adaptive plus 1D linear sweep test",
                          mode="adaptive")

    @unittest.skip('Skipped due to failure on github CI.')
    def test_plotmon_2D_monkey_patching(self):
        self.MC.soft_avg(1)
        self.mock_parabola.noise(0)
        self.MC.set_sweep_functions(
            [self.mock_parabola.x, self.mock_parabola.y])
        self.MC.set_adaptive_function_parameters({
            "adaptive_function":
            adaptive.Learner2D,
            "goal":
            lambda l: l.npoints > 4 * 4,
            "bounds": ((-50, +50), (-20, +30)),
        })
        saved_unit = self.mock_parabola.parabola.unit
        self.mock_parabola.parabola.unit = "deg"
        self.MC.set_detector_function(self.mock_parabola.parabola)
        dat = self.MC.run("2D adaptive anglemap45", mode="adaptive")
        hist_proxy = self.MC.secondary_QtPlot.traces[0]["plot_object"]["hist"]
        grad_proxy = hist_proxy.gradient
        midle_color = grad_proxy.getLookupTable(3)._getValue()[1]
        assert np.all(midle_color == [254, 229, 234])
        assert hist_proxy.getLevels() == (0.0, 360.0)
        self.mock_parabola.parabola.unit = saved_unit

    def test_adaptive_SKOptLearner(self):
        # NB cool stuff: this can also optimize hyper-parameters
        self.MC.soft_avg(1)
        self.mock_parabola.noise(0.5)
        self.MC.set_sweep_functions(
            [self.mock_parabola.x, self.mock_parabola.y])
        self.MC.set_adaptive_function_parameters({
            "adaptive_function":
            adaptive.SKOptLearner,
            "goal":
            lambda l: l.npoints > 15,
            "dimensions": [(-50.0, +50.0), (-20.0, +30.0)],
            "base_estimator":
            "gp",
            "acq_func":
            "EI",
            "acq_optimizer":
            "lbfgs",
        })
        self.MC.set_detector_function(self.mock_parabola.parabola)
        dat = self.MC.run("2D SKOptLearner adaptive sampling test",
                          mode="adaptive")

    def test_adaptive_SKOptLearner_int(self):
        # Optimize over integer parameters
        self.MC.soft_avg(1)
        self.mock_parabola.noise(0.5)
        self.MC.set_sweep_functions(
            [self.mock_parabola.x_int, self.mock_parabola.y_int])
        self.MC.set_adaptive_function_parameters({
            "adaptive_function":
            adaptive.SKOptLearner,
            "goal":
            lambda l: l.npoints > 15,
            "dimensions": [(-50, +50), (-20, +30)],
            "base_estimator":
            "gp",
            "acq_func":
            "EI",
            "acq_optimizer":
            "lbfgs",
        })
        self.MC.set_detector_function(self.mock_parabola.parabola_int)
        dat = self.MC.run("2D SKOptLearner int parameters", mode="adaptive")

    def test_adaptive_SKOptLearner_list_of_vals(self):
        # NB cool stuff: this can also optimize integers and other
        # hyper-parameters
        self.MC.soft_avg(1)
        self.mock_parabola.noise(0.5)
        self.MC.set_sweep_functions(
            [self.mock_parabola.x, self.mock_parabola.y])
        self.MC.set_adaptive_function_parameters({
            "adaptive_function":
            adaptive.SKOptLearner,
            "goal":
            lambda l: l.npoints > 14,
            "dimensions": [(-50.0, +50.0), (-20.0, +30.0)],
            "base_estimator":
            "gp",
            "acq_func":
            "gp_hedge",
            "acq_optimizer":
            "lbfgs",
        })
        self.MC.set_detector_function(self.mock_parabola.parabola_list)
        dat = self.MC.run("2D SKOptLearner adaptive sampling test multi",
                          mode="adaptive")

    def test_progress_callback(self):

        progress_param = ManualParameter("progress", initial_value=0)

        def set_progress_param_callable(progress):
            progress_param(progress)

        self.MC.on_progress_callback(set_progress_param_callable)

        self.assertEqual(progress_param(), 0)
        sweep_pts = np.linspace(0, 10, 30)
        self.MC.set_sweep_function(None_Sweep())
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Soft())
        dat = self.MC.run("1D_soft")

        self.assertEqual(progress_param(), 100)

    def test_persist_mode(self):
        sweep_pts = np.linspace(0, 10, 5)
        self.MC.persist_mode(True)
        self.MC.set_sweep_function(None_Sweep(sweep_control="hard"))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Hard())
        dat = self.MC.run("1D_hard")
        dset = dat["dset"]
        x = dset[:, 0]
        y = [np.sin(x / np.pi), np.cos(x / np.pi)]
        y0 = dset[:, 1]
        y1 = dset[:, 2]
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y0, y[0])
        np.testing.assert_array_almost_equal(y1, y[1])
        d = self.MC.detector_function
        self.assertEqual(d.times_called, 1)

        persist_dat = self.MC._persist_dat
        x_p = persist_dat[:, 0]
        y0_p = persist_dat[:, 1]
        y1_p = persist_dat[:, 2]
        np.testing.assert_array_almost_equal(x, x_p)
        np.testing.assert_array_almost_equal(y0, y0_p)
        np.testing.assert_array_almost_equal(y1, y1_p)

        self.MC.clear_persitent_plot()
        self.assertEqual(self.MC._persist_dat, None)

    def test_data_resolution(self):
        # This test will fail if the data is saved as 32 bit floats
        sweep_pts = [3e9 + 1e-3, 3e9 + 2e-3]
        self.MC.set_sweep_function(None_Sweep())
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Soft())
        dat = self.MC.run("1D_soft")
        x = dat["dset"][:, 0]
        np.testing.assert_array_almost_equal(x, sweep_pts, decimal=5)

    def test_save_exp_metadata(self):
        metadata_dict = {
            "intParam": 1,
            "floatParam": 2.5e-3,
            "strParam": "spam",
            "listParam": [1, 2, 3, 4],
            "arrayParam": np.array([4e5, 5e5]),
            "dictParam": {
                "a": 1,
                "b": 2
            },
            "tupleParam": (3, "c"),
        }

        old_a_tools_datadir = a_tools.datadir
        a_tools.datadir = self.MC.datadir()

        sweep_pts = np.linspace(0, 10, 30)
        self.MC.set_sweep_function(None_Sweep())
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Soft())
        self.MC.run("test_exp_metadata", exp_metadata=metadata_dict)
        a = ma.MeasurementAnalysis(label="test_exp_metadata", auto=False)

        a_tools.datadir = old_a_tools_datadir

        loaded_dict = read_dict_from_hdf5(
            {}, a.data_file["Experimental Data"]["Experimental Metadata"])

        np.testing.assert_equal(metadata_dict, loaded_dict)

    @classmethod
    def tearDownClass(self):
        self.MC.close()
        self.mock_parabola.close()
        del self.station.components["MC"]
        del self.station.components["mock_parabola"]
コード例 #8
0
class Test_Detectors(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        self.station = station.Station()
        self.MC = measurement_control.MeasurementControl(
            'MC', live_plot_enabled=False, verbose=False)
        self.MC.station = self.station
        self.station.add_component(self.MC)

        self.mock_parabola = DummyParHolder('mock_parabola')
        self.station.add_component(self.mock_parabola)

    def test_function_detector_simple(self):
        def dummy_function(val_a, val_b):
            return val_a

        # Testing input of a simple dict
        d = det.Function_Detector(dummy_function,
                                  value_names=['a'],
                                  value_units=None,
                                  msmt_kw={
                                      'val_a': 5.5,
                                      'val_b': 1
                                  })
        self.assertEqual(d.value_names, ['a'])
        self.assertEqual(d.value_units, ['a.u.'])

        self.MC.set_sweep_function(None_Sweep(sweep_control='soft'))
        self.MC.set_sweep_points(np.linspace(0, 10, 10))
        self.MC.set_detector_function(d)
        dat = self.MC.run()
        dset = dat["dset"]
        np.testing.assert_array_almost_equal(np.ones(10) * 5.5, dset[:, 1])

    def test_function_detector_parameter(self):
        def dummy_function(val_a, val_b):
            return val_a + val_b

        # Testing input of a simple dict

        x = self.mock_parabola.x
        d = det.Function_Detector(dummy_function,
                                  value_names=['xvals+1'],
                                  value_units=['s'],
                                  msmt_kw={
                                      'val_a': x,
                                      'val_b': 1
                                  })
        self.assertEqual(d.value_names, ['xvals+1'])
        self.assertEqual(d.value_units, ['s'])

        xvals = np.linspace(0, 10, 10)
        self.MC.set_sweep_function(self.mock_parabola.x)
        self.MC.set_sweep_points(xvals)
        self.MC.set_detector_function(d)
        dat = self.MC.run()
        dset = dat["dset"]
        np.testing.assert_array_almost_equal(xvals + 1, dset[:, 1])

    def test_function_detector_dict_all_keys(self):
        def dummy_function(val_a, val_b):
            return {'a': val_a, 'b': val_b}

        # Testing input of a simple dict
        d = det.Function_Detector(dummy_function,
                                  value_names=['aa', 'b'],
                                  result_keys=['a', 'b'],
                                  value_units=['s', 's'],
                                  msmt_kw={
                                      'val_a': 5.5,
                                      'val_b': 1
                                  })

        xvals = np.linspace(0, 10, 10)
        self.MC.set_sweep_function(self.mock_parabola.x)
        self.MC.set_sweep_points(xvals)
        self.MC.set_detector_function(d)
        dat = self.MC.run()
        dset = dat["dset"]
        np.testing.assert_array_almost_equal(np.ones(10) * 5.5, dset[:, 1])
        np.testing.assert_array_almost_equal(np.ones(10) * 1, dset[:, 2])
        self.assertEqual(np.shape(dset), (10, 3))

    def test_function_detector_dict_single_key(self):
        def dummy_function(val_a, val_b):
            return {'a': val_a, 'b': val_b}

        # Testing input of a simple dict
        d = det.Function_Detector(dummy_function,
                                  value_names=['aa'],
                                  result_keys=['a'],
                                  value_units=['s'],
                                  msmt_kw={
                                      'val_a': 5.5,
                                      'val_b': 1
                                  })

        xvals = np.linspace(0, 10, 10)
        self.MC.set_sweep_function(self.mock_parabola.x)
        self.MC.set_sweep_points(xvals)
        self.MC.set_detector_function(d)
        dat = self.MC.run()
        dset = dat["dset"]
        np.testing.assert_array_almost_equal(np.ones(10) * 5.5, dset[:, 1])
        self.assertEqual(np.shape(dset), (10, 2))

    def test_UHFQC_state_map(self):
        """
        Tests the statemap method of the UHFQC statistics detector
        """
        d = det.UHFQC_statistics_logging_det
        test_statemap = {'00': '00', '01': '10', '10': '10', '11': '00'}
        sm_arr = d.statemap_to_array(test_statemap)
        exp_sm_arr = np.array([0, 2, 2, 0], dtype=np.uint32)
        np.testing.assert_array_equal(sm_arr, exp_sm_arr)

        invalid_sm = {'01': '10'}
        with self.assertRaises(ValueError):
            d.statemap_to_array(invalid_sm)

    def test_multi_det_basics(self):
        def dummy_function_1(val_a, val_b):
            return val_a

        def dummy_function_2(val_a, val_b):
            return val_a + val_b

        # Testing input of a simple dict
        x = self.mock_parabola.x
        d0 = det.Function_Detector(dummy_function_1,
                                   value_names=['a'],
                                   value_units=['my_unit'],
                                   msmt_kw={
                                       'val_a': x,
                                       'val_b': 1
                                   })
        d1 = det.Function_Detector(dummy_function_2,
                                   value_names=['b'],
                                   value_units=None,
                                   msmt_kw={
                                       'val_a': x,
                                       'val_b': 1
                                   })

        dm = det.Multi_Detector([d0, d1], det_idx_suffix=False)
        self.assertEqual(dm.value_names, ['a', 'b'])
        self.assertEqual(dm.value_units, ['my_unit', 'a.u.'])

        dm_suffix = det.Multi_Detector([d0, d1], det_idx_suffix=True)
        self.assertEqual(dm_suffix.value_names, ['a_det0', 'b_det1'])
        self.assertEqual(dm_suffix.value_units, ['my_unit', 'a.u.'])

        dh = det.Dummy_Detector_Hard()
        with self.assertRaises(ValueError):
            dm = det.Multi_Detector([dh, d0])

    def test_Multi_Detector_soft(self):
        def dummy_function_1(val_a, val_b):
            return val_a, val_b

        def dummy_function_2(val_a, val_b):
            return val_a + val_b

        # Testing input of a simple dict
        x = self.mock_parabola.x
        d0 = det.Function_Detector(dummy_function_1,
                                   value_names=['a', 'b'],
                                   value_units=['my_unit', 'a.u.'],
                                   msmt_kw={
                                       'val_a': x,
                                       'val_b': 1
                                   })
        d1 = det.Function_Detector(dummy_function_2,
                                   value_names=['b'],
                                   value_units=None,
                                   msmt_kw={
                                       'val_a': x,
                                       'val_b': 1
                                   })

        dm = det.Multi_Detector([d0, d1], det_idx_suffix=False)
        self.assertEqual(dm.value_names, ['a', 'b', 'b'])
        self.assertEqual(dm.value_units, ['my_unit', 'a.u.', 'a.u.'])

        dm_suffix = det.Multi_Detector([d0, d1], det_idx_suffix=True)
        self.assertEqual(dm_suffix.value_names, ['a_det0', 'b_det0', 'b_det1'])
        self.assertEqual(dm_suffix.value_units, ['my_unit', 'a.u.', 'a.u.'])

        xvals = np.linspace(0, 10, 10)
        self.MC.set_sweep_function(self.mock_parabola.x)
        self.MC.set_sweep_points(xvals)
        self.MC.set_detector_function(dm)
        dat = self.MC.run("multi_detector")
        dset = dat["dset"]
        np.testing.assert_array_almost_equal(xvals, dset[:, 1])
        np.testing.assert_array_almost_equal(np.ones(len(xvals)), dset[:, 2])
        np.testing.assert_array_almost_equal(xvals + 1, dset[:, 3])

    def test_Multi_Detector_hard(self):
        sweep_pts = np.linspace(0, 10, 5)
        d0 = det.Dummy_Detector_Hard()
        d1 = det.Dummy_Detector_Hard()
        dm = det.Multi_Detector([d0, d1])

        self.MC.set_sweep_function(None_Sweep(sweep_control='hard'))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(dm)
        dat = self.MC.run('Multi_hard')
        dset = dat['dset']
        x = dset[:, 0]
        y = [np.sin(x / np.pi), np.cos(x / np.pi)]
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y[0], dset[:, 1])
        np.testing.assert_array_almost_equal(y[1], dset[:, 2])
        np.testing.assert_array_almost_equal(y[0], dset[:, 3])
        np.testing.assert_array_almost_equal(y[1], dset[:, 4])

    @classmethod
    def tearDownClass(self):
        self.MC.close()
        self.mock_parabola.close()
        del self.station.components['MC']
        del self.station.components['mock_parabola']
コード例 #9
0
class Test_MeasurementControl(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        self.station = station.Station()
        # set up a pulsar with some mock settings for the element
        self.MC = measurement_control.MeasurementControl(
            'MC', live_plot_enabled=False, verbose=False)
        self.MC.station = self.station
        self.station.add_component(self.MC)

        self.mock_parabola = DummyParHolder('mock_parabola')
        self.station.add_component(self.mock_parabola)

    def setUp(self):
        self.MC.soft_avg(1)

    def test_soft_sweep_1D(self):

        sweep_pts = np.linspace(0, 10, 30)
        self.MC.set_sweep_function(None_Sweep())
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Soft())
        dat = self.MC.run('1D_soft')
        x = dat[:, 0]
        xr = np.arange(len(x))/15
        y = np.array([np.sin(xr/np.pi), np.cos(xr/np.pi)])
        y0 = dat[:, 1]
        y1 = dat[:, 2]
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y0, y[0, :])
        np.testing.assert_array_almost_equal(y1, y[1, :])

    def test_hard_sweep_1D(self):
        sweep_pts = np.linspace(0, 10, 5)
        self.MC.set_sweep_function(None_Sweep(sweep_control='hard'))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Hard())
        dat = self.MC.run('1D_hard')
        x = dat[:, 0]
        y = [np.sin(x / np.pi), np.cos(x/np.pi)]
        y0 = dat[:, 1]
        y1 = dat[:, 2]
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y0, y[0])
        np.testing.assert_array_almost_equal(y1, y[1])
        d = self.MC.detector_function
        self.assertEqual(d.times_called, 1)

    def test_soft_sweep_2D(self):
        sweep_pts = np.linspace(0, 10, 30)
        sweep_pts_2D = np.linspace(0, 10, 5)
        self.MC.set_sweep_function(None_Sweep(sweep_control='soft'))
        self.MC.set_sweep_function_2D(None_Sweep(sweep_control='soft'))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_sweep_points_2D(sweep_pts_2D)
        self.MC.set_detector_function(det.Dummy_Detector_Soft())
        dat = self.MC.run('2D_soft', mode='2D')

        x = dat[:, 0]
        y = dat[:, 1]
        xr = np.arange(len(sweep_pts)*len(sweep_pts_2D))/15
        z = np.array([np.sin(xr/np.pi), np.cos(xr/np.pi)])
        z0 = dat[:, 2]
        z1 = dat[:, 3]

        x_tiled = np.tile(sweep_pts, len(sweep_pts_2D))
        y_rep = np.repeat(sweep_pts_2D, len(sweep_pts))
        np.testing.assert_array_almost_equal(x, x_tiled)
        np.testing.assert_array_almost_equal(y, y_rep)
        np.testing.assert_array_almost_equal(z0, z[0, :])
        np.testing.assert_array_almost_equal(z1, z[1, :])

    def test_hard_sweep_2D(self):
        """
        Hard inner loop, soft outer loop
        """
        sweep_pts = np.linspace(10, 20, 3)
        sweep_pts_2D = np.linspace(0, 10, 5)
        self.MC.set_sweep_function(None_Sweep(sweep_control='hard'))
        self.MC.set_sweep_function_2D(None_Sweep(sweep_control='soft'))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_sweep_points_2D(sweep_pts_2D)
        self.MC.set_detector_function(det.Dummy_Detector_Hard())
        dat = self.MC.run('2D_hard', mode='2D')
        x = dat[:, 0]
        y = dat[:, 1]
        z = self.data = [np.sin(x / np.pi), np.cos(x/np.pi)]
        z0 = dat[:, 2]
        z1 = dat[:, 3]

        x_tiled = np.tile(sweep_pts, len(sweep_pts_2D))
        y_rep = np.repeat(sweep_pts_2D, len(sweep_pts))
        np.testing.assert_array_almost_equal(x, x_tiled)
        np.testing.assert_array_almost_equal(y, y_rep)
        np.testing.assert_array_almost_equal(z0, z[0])
        np.testing.assert_array_almost_equal(z1, z[1])
        d = self.MC.detector_function
        self.assertEqual(d.times_called, 5)

    def test_many_shots_hard_sweep(self):
        """
        Tests acquiring more than the maximum number of shots for a hard
        detector by setting the number of sweep points high
        """
        sweep_pts = np.arange(50)
        self.MC.set_sweep_function(None_Sweep(sweep_control='hard'))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Shots_Detector(max_shots=5))
        dat = self.MC.run('man_shots')
        x = dat[:, 0]
        y = dat[:, 1]

        self.assertEqual(np.shape(dat), (len(sweep_pts), 2))
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y, sweep_pts)

        d = self.MC.detector_function
        self.assertEqual(d.times_called, 10)

    def test_soft_averages_hard_sweep_1D(self):
        sweep_pts = np.arange(50)
        self.MC.soft_avg(1)
        self.MC.set_sweep_function(None_Sweep(sweep_control='hard'))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Hard(noise=.4))
        noisy_dat = self.MC.run('noisy_dat')
        x = noisy_dat[:, 0]
        y = [np.sin(x / np.pi), np.cos(x/np.pi)]
        yn_0 = abs(noisy_dat[:, 1] - y[0])
        yn_1 = abs(noisy_dat[:, 2] - y[1])

        d = self.MC.detector_function
        self.assertEqual(d.times_called, 1)

        self.MC.soft_avg(5000)
        self.MC.set_sweep_function(None_Sweep(sweep_control='hard'))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(d)
        avg_dat = self.MC.run('averaged_dat')
        yavg_0 = abs(avg_dat[:, 1] - y[0])
        yavg_1 = abs(avg_dat[:, 2] - y[1])

        np.testing.assert_array_almost_equal(x, sweep_pts)
        self.assertGreater(np.mean(yn_0), np.mean(yavg_0))
        self.assertGreater(np.mean(yn_1), np.mean(yavg_1))

        np.testing.assert_array_almost_equal(yavg_0, np.zeros(len(x)),
                                             decimal=2)
        np.testing.assert_array_almost_equal(yavg_1, np.zeros(len(x)),
                                             decimal=2)
        self.assertEqual(d.times_called, 5001)

    def test_soft_averages_hard_sweep_2D(self):
        self.MC.soft_avg(1)
        sweep_pts = np.arange(5)
        sweep_pts_2D = np.linspace(5, 10, 5)
        self.MC.set_sweep_function(None_Sweep(sweep_control='hard'))
        self.MC.set_sweep_function_2D(None_Sweep(sweep_control='soft'))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_sweep_points_2D(sweep_pts_2D)
        self.MC.set_detector_function(det.Dummy_Detector_Hard(noise=.2))
        noisy_dat = self.MC.run('2D_hard', mode='2D')
        x = noisy_dat[:, 0]
        y = noisy_dat[:, 1]
        z = [np.sin(x / np.pi), np.cos(x/np.pi)]
        z0 = abs(noisy_dat[:, 2] - z[0])
        z1 = abs(noisy_dat[:, 3] - z[1])

        x_tiled = np.tile(sweep_pts, len(sweep_pts_2D))
        y_rep = np.repeat(sweep_pts_2D, len(sweep_pts))
        np.testing.assert_array_almost_equal(x, x_tiled)
        np.testing.assert_array_almost_equal(y, y_rep)

        d = self.MC.detector_function
        self.assertEqual(d.times_called, 5)

        self.MC.set_sweep_function(None_Sweep(sweep_control='hard'))
        self.MC.set_sweep_function_2D(None_Sweep(sweep_control='soft'))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_sweep_points_2D(sweep_pts_2D)
        self.MC.soft_avg(1000)
        avg_dat = self.MC.run('averaged_dat', mode='2D')
        x = avg_dat[:, 0]
        y = avg_dat[:, 1]
        zavg_0 = abs(avg_dat[:, 2] - z[0])
        zavg_1 = abs(avg_dat[:, 3] - z[1])

        np.testing.assert_array_almost_equal(x, x_tiled)
        self.assertGreater(np.mean(z0), np.mean(zavg_0))
        self.assertGreater(np.mean(z1), np.mean(zavg_1))

        np.testing.assert_array_almost_equal(zavg_0, np.zeros(len(x)),
                                             decimal=2)
        np.testing.assert_array_almost_equal(zavg_1, np.zeros(len(x)),
                                             decimal=2)

        self.assertEqual(d.times_called, 5*1000+5)

    def test_soft_sweep_1D_soft_averages(self):
        self.mock_parabola.noise(0)
        self.mock_parabola.x(0)
        self.mock_parabola.y(0)
        self.mock_parabola.z(0)

        sweep_pts = np.linspace(0, 10, 30)
        self.MC.soft_avg(1)
        self.MC.set_sweep_function(self.mock_parabola.x)
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(self.mock_parabola.parabola)
        dat = self.MC.run('1D_soft')
        x = dat[:, 0]
        y_exp = x**2
        y0 = dat[:, 1]
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y0, y_exp, decimal=5)

        sweep_pts = np.linspace(0, 10, 30)
        self.MC.soft_avg(10)
        self.MC.set_sweep_function(self.mock_parabola.x)
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(self.mock_parabola.parabola)
        dat = self.MC.run('1D_soft')
        x = dat[:, 0]
        y_exp = x**2
        y0 = dat[:, 1]
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y0, y_exp, decimal=5)

    def test_adaptive_measurement(self):
        self.MC.soft_avg(1)
        self.mock_parabola.noise(0)
        self.MC.set_sweep_functions(
            [self.mock_parabola.x, self.mock_parabola.y])
        self.MC.set_adaptive_function_parameters(
            {'adaptive_function': nelder_mead,
             'x0': [-50, -50], 'initial_step': [2.5, 2.5]})
        self.mock_parabola.noise(.5)
        self.MC.set_detector_function(self.mock_parabola.parabola)
        dat = self.MC.run('1D test', mode='adaptive')
        xf, yf, pf = dat[-1]
        self.assertLess(xf, 0.7)
        self.assertLess(yf, 0.7)
        self.assertLess(pf, 0.7)

    @classmethod
    def tearDownClass(self):
        self.MC.close()
        self.mock_parabola.close()
        del self.station.components['MC']
        del self.station.components['mock_parabola']

    def test_persist_mode(self):
        sweep_pts = np.linspace(0, 10, 5)
        self.MC.persist_mode(True)
        self.MC.set_sweep_function(None_Sweep(sweep_control='hard'))
        self.MC.set_sweep_points(sweep_pts)
        self.MC.set_detector_function(det.Dummy_Detector_Hard())
        dat = self.MC.run('1D_hard')
        x = dat[:, 0]
        y = [np.sin(x / np.pi), np.cos(x/np.pi)]
        y0 = dat[:, 1]
        y1 = dat[:, 2]
        np.testing.assert_array_almost_equal(x, sweep_pts)
        np.testing.assert_array_almost_equal(y0, y[0])
        np.testing.assert_array_almost_equal(y1, y[1])
        d = self.MC.detector_function
        self.assertEqual(d.times_called, 1)

        persist_dat = self.MC._persist_dat
        x_p = persist_dat[:, 0]
        y0_p = persist_dat[:, 1]
        y1_p = persist_dat[:, 2]
        np.testing.assert_array_almost_equal(x, x_p)
        np.testing.assert_array_almost_equal(y0, y0_p)
        np.testing.assert_array_almost_equal(y1, y1_p)

        self.MC.clear_persitent_plot()
        self.assertEqual(self.MC._persist_dat, None)