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_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_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=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_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'])
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_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_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_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)
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)
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_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_Mock_Detector(self): x = np.linspace(0, 20, 31) y = x**2 d = det.Mock_Detector(value_names=['val'], value_units=['s'], detector_control='soft', mock_values=y) self.MC.set_sweep_function(None_Sweep(sweep_control='soft')) self.MC.set_sweep_points(x) self.MC.set_detector_function(d) dat = self.MC.run('Mock_detector') xm = dat['dset'][:, 0] ym = dat['dset'][:, 1] assert (x == xm).all() assert (y == ym).all()
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_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}) assert d.value_names == ['a'] assert 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) np.seterr() dat = self.MC.run()
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_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])
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_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])