def test_cache_1d(experiment, DAC, DMM, n_points, bg_writing, channel_array_instrument, setpoints_type): setpoints_param, setpoints_values = _prepare_setpoints_1d( DAC, channel_array_instrument, n_points, setpoints_type) meas = Measurement() meas.register_parameter(setpoints_param) meas_parameters = ( DMM.v1, channel_array_instrument.A.dummy_multi_parameter, channel_array_instrument.A.dummy_scalar_multi_parameter, channel_array_instrument.A.dummy_2d_multi_parameter, channel_array_instrument.A.dummy_2d_multi_parameter_2, channel_array_instrument.A.dummy_array_parameter, channel_array_instrument.A.dummy_complex_array_parameter, channel_array_instrument.A.dummy_complex, channel_array_instrument.A.dummy_parameter_with_setpoints, channel_array_instrument.A.dummy_parameter_with_setpoints_complex, ) channel_array_instrument.A.dummy_start(0) channel_array_instrument.A.dummy_stop(10) channel_array_instrument.A.dummy_n_points(10) for param in meas_parameters: meas.register_parameter(param, setpoints=(setpoints_param, )) with meas.run(write_in_background=bg_writing) as datasaver: dataset = datasaver.dataset _assert_parameter_data_is_identical(dataset.get_parameter_data(), dataset.cache.data()) for i, v in enumerate(setpoints_values): setpoints_param.set(v) meas_vals = [(param, param.get()) for param in meas_parameters[:-2]] meas_vals += expand_setpoints_helper(meas_parameters[-2]) meas_vals += expand_setpoints_helper(meas_parameters[-1]) datasaver.add_result((setpoints_param, v), *meas_vals) datasaver.flush_data_to_database(block=True) data = dataset.cache.data() _assert_parameter_data_is_identical(dataset.get_parameter_data(), data) _assert_parameter_data_is_identical(dataset.get_parameter_data(), dataset.cache.data()) assert dataset.cache._loaded_from_completed_ds is True _assert_parameter_data_is_identical(dataset.get_parameter_data(), dataset.cache.data())
def test_cache_2d(experiment, DAC, DMM, n_points_outer, n_points_inner, bg_writing, channel_array_instrument, in_memory_cache): meas = Measurement() meas.register_parameter(DAC.ch1) meas.register_parameter(DAC.ch2) meas_parameters = (DMM.v1, channel_array_instrument.A.dummy_multi_parameter, channel_array_instrument.A.dummy_scalar_multi_parameter, channel_array_instrument.A.dummy_2d_multi_parameter, channel_array_instrument.A.dummy_2d_multi_parameter_2, channel_array_instrument.A.dummy_array_parameter, channel_array_instrument.A.dummy_complex_array_parameter, channel_array_instrument.A.dummy_complex, channel_array_instrument.A.dummy_parameter_with_setpoints, channel_array_instrument.A.dummy_parameter_with_setpoints_complex, ) channel_array_instrument.A.dummy_start(0) channel_array_instrument.A.dummy_stop(10) channel_array_instrument.A.dummy_n_points(10) for param in meas_parameters: meas.register_parameter(param, setpoints=(DAC.ch1, DAC.ch2)) n_rows_written = 0 with meas.run( write_in_background=bg_writing, in_memory_cache=in_memory_cache) as datasaver: dataset = datasaver.dataset _assert_parameter_data_is_identical(dataset.get_parameter_data(), dataset.cache.data()) for v1 in np.linspace(-1, 1, n_points_outer): for v2 in np.linspace(-1, 1, n_points_inner): DAC.ch1.set(v1) DAC.ch2.set(v2) meas_vals = [(param, param.get()) for param in meas_parameters[:-2]] meas_vals += expand_setpoints_helper(meas_parameters[-2]) meas_vals += expand_setpoints_helper(meas_parameters[-1]) datasaver.add_result((DAC.ch1, v1), (DAC.ch2, v2), *meas_vals) datasaver.flush_data_to_database(block=True) n_rows_written += 1 data = dataset.cache.data() _assert_parameter_data_is_identical(dataset.get_parameter_data(), data) _assert_parameter_data_is_identical(dataset.get_parameter_data(), dataset.cache.data())
def test_expand_setpoints_3d(parameters): n_points_1, n_points_2, n_points_3, \ setpoints_1, setpoints_2, setpoints_3 = parameters param_with_setpoints_3 = ParameterWithSetpoints( 'param_2', get_cmd=lambda: rand(n_points_1(), n_points_2(), n_points_3()), vals=vals.Arrays(shape=(n_points_1, n_points_2, n_points_3))) param_with_setpoints_3.setpoints = (setpoints_1, setpoints_2, setpoints_3) data = expand_setpoints_helper(param_with_setpoints_3) assert len(data) == 4 assert data[0][1].shape == data[1][1].shape assert data[0][1].shape == data[2][1].shape assert data[0][1].shape == data[3][1].shape sp1 = data[0][1] for i in range(sp1.shape[1]): for j in range(sp1.shape[2]): np.testing.assert_array_equal(sp1[:, i, j], np.arange(sp1.shape[0])) sp2 = data[1][1] for i in range(sp2.shape[0]): for j in range(sp2.shape[2]): np.testing.assert_array_equal(sp2[i, :, j], np.arange(sp2.shape[1])) sp3 = data[2][1] for i in range(sp3.shape[0]): for j in range(sp3.shape[1]): np.testing.assert_array_equal(sp3[i, j, :], np.arange(sp3.shape[2]))
def test_expand_setpoints_2d(parameters): n_points_1, n_points_2, n_points_3, \ setpoints_1, setpoints_2, setpoints_3 = parameters param_with_setpoints_2 = ParameterWithSetpoints( 'param_2', get_cmd=lambda: rand(n_points_1(), n_points_2()), vals=vals.Arrays(shape=(n_points_1, n_points_2))) param_with_setpoints_2.setpoints = (setpoints_1, setpoints_2) data = expand_setpoints_helper(param_with_setpoints_2) assert len(data) == 3 assert data[0][1].shape == data[1][1].shape assert data[0][1].shape == data[2][1].shape sp1 = data[0][1] sp2 = data[1][1] # the first set of setpoints should be repeated along the second axis for i in range(sp1.shape[1]): np.testing.assert_array_equal(sp1[:, i], np.arange(sp1.shape[0])) # the second set of setpoints should be repeated along the first axis for i in range(sp2.shape[0]): np.testing.assert_array_equal(sp2[i, :], np.arange(sp1.shape[1]))
def test_expand_setpoints_1c(parameters): """ Test that the setpoints expander helper function works correctly """ n_points_1, n_points_2, n_points_3, \ setpoints_1, setpoints_2, setpoints_3 = parameters param_with_setpoints_1 = ParameterWithSetpoints( 'param_1', get_cmd=lambda: rand(n_points_1()), setpoints=(setpoints_1, ), vals=vals.Arrays(shape=(n_points_1, ))) data = expand_setpoints_helper(param_with_setpoints_1) assert len(data) == 2 assert len(data[0][1]) == len(data[1][1])
def _conditionally_expand_parameter_with_setpoints( self, data: values_type, parameter: ParameterWithSetpoints, parameter_names: Sequence[str], partial_result: res_type) -> Dict[ParamSpecBase, np.ndarray]: local_results = {} setpoint_names = tuple(setpoint.full_name for setpoint in parameter.setpoints) expanded = tuple(setpoint_name in parameter_names for setpoint_name in setpoint_names) if all(expanded): local_results.update(self._unpack_partial_result(partial_result)) elif any(expanded): raise ValueError(f"Some of the setpoints of {parameter.full_name} " "were explicitly given but others were not. " "Either supply all of them or none of them.") else: expanded_partial_result = expand_setpoints_helper(parameter, data) for res in expanded_partial_result: local_results.update(self._unpack_partial_result(res)) return local_results
def test_cache_1d_shape(experiment, DAC, DMM, n_points, bg_writing, channel_array_instrument, setpoints_type, in_memory_cache): setpoints_param, setpoints_values = _prepare_setpoints_1d( DAC, channel_array_instrument, n_points, setpoints_type ) meas = Measurement() meas.register_parameter(setpoints_param) meas_parameters = (DMM.v1, channel_array_instrument.A.dummy_multi_parameter, channel_array_instrument.A.dummy_scalar_multi_parameter, channel_array_instrument.A.dummy_2d_multi_parameter, channel_array_instrument.A.dummy_2d_multi_parameter_2, channel_array_instrument.A.dummy_array_parameter, channel_array_instrument.A.dummy_complex_array_parameter, channel_array_instrument.A.dummy_complex, channel_array_instrument.A.dummy_parameter_with_setpoints, channel_array_instrument.A.dummy_parameter_with_setpoints_complex, ) pws_n_points = 10 channel_array_instrument.A.dummy_start(0) channel_array_instrument.A.dummy_stop(10) channel_array_instrument.A.dummy_n_points(pws_n_points) expected_shapes = { 'dummy_dmm_v1': (n_points, ), 'dummy_channel_inst_ChanA_multi_setpoint_param_this': (n_points, 5), 'dummy_channel_inst_ChanA_multi_setpoint_param_that': (n_points, 5), 'dummy_channel_inst_ChanA_thisparam': (n_points, ), 'dummy_channel_inst_ChanA_thatparam': (n_points, ), 'dummy_channel_inst_ChanA_this': (n_points, 5, 3), 'dummy_channel_inst_ChanA_that': (n_points, 5, 3), 'dummy_channel_inst_ChanA_this_5_3': (n_points, 5, 3), 'dummy_channel_inst_ChanA_this_2_7': (n_points, 2, 7), 'dummy_channel_inst_ChanA_dummy_array_parameter': (n_points, 5), 'dummy_channel_inst_ChanA_dummy_complex_array_parameter': (n_points, 5), 'dummy_channel_inst_ChanA_dummy_complex': (n_points, ), 'dummy_channel_inst_ChanA_dummy_parameter_with_setpoints': (n_points, pws_n_points), 'dummy_channel_inst_ChanA_dummy_parameter_with_setpoints_complex': (n_points, pws_n_points) } for param in meas_parameters: meas.register_parameter(param, setpoints=(setpoints_param,)) meas.set_shapes(detect_shape_of_measurement( meas_parameters, (n_points,)) ) n_points_measured = 0 with meas.run(write_in_background=bg_writing, in_memory_cache=in_memory_cache) as datasaver: dataset = datasaver.dataset _assert_parameter_data_is_identical(dataset.get_parameter_data(), dataset.cache.data()) for i, v in enumerate(setpoints_values): n_points_measured += 1 setpoints_param.set(v) meas_vals = [(param, param.get()) for param in meas_parameters[:-2]] meas_vals += expand_setpoints_helper(meas_parameters[-2]) meas_vals += expand_setpoints_helper(meas_parameters[-1]) datasaver.add_result((setpoints_param, v), *meas_vals) datasaver.flush_data_to_database(block=True) cache_data_trees = dataset.cache.data() param_data_trees = dataset.get_parameter_data() _assert_partial_cache_is_as_expected( cache_data_trees, expected_shapes, n_points_measured, param_data_trees, cache_correct=True ) cache_data_trees = dataset.cache.data() param_data_trees = dataset.get_parameter_data() _assert_completed_cache_is_as_expected(cache_data_trees, param_data_trees, flatten=False)
def test_cache_1d(experiment, DAC, DMM, n_points, bg_writing, channel_array_instrument, setpoints_type, set_shape, in_memory_cache): setpoints_param, setpoints_values = _prepare_setpoints_1d( DAC, channel_array_instrument, n_points, setpoints_type ) meas1 = Measurement() meas1.register_parameter(setpoints_param) meas_parameters1 = ( DMM.v1, channel_array_instrument.A.dummy_multi_parameter, channel_array_instrument.A.dummy_scalar_multi_parameter, channel_array_instrument.A.dummy_2d_multi_parameter, channel_array_instrument.A.dummy_2d_multi_parameter_2, channel_array_instrument.A.dummy_array_parameter, channel_array_instrument.A.dummy_complex_array_parameter, channel_array_instrument.A.dummy_complex, channel_array_instrument.A.dummy_parameter_with_setpoints, channel_array_instrument.A.dummy_parameter_with_setpoints_complex, ) pws_shape_1 = 10 pws_shape_2 = 3 channel_array_instrument.A.dummy_start(0) channel_array_instrument.A.dummy_stop(10) channel_array_instrument.A.dummy_n_points(pws_shape_1) channel_array_instrument.A.dummy_start_2(2) channel_array_instrument.A.dummy_stop_2(7) channel_array_instrument.A.dummy_n_points_2(pws_shape_2) if set_shape: meas1.set_shapes( { DMM.v1.full_name: (n_points,), channel_array_instrument.A.dummy_multi_parameter.full_names[0]: (n_points, 5), channel_array_instrument.A.dummy_multi_parameter.full_names[1]: (n_points, 5), channel_array_instrument.A.dummy_scalar_multi_parameter.full_names[0]: (n_points,), channel_array_instrument.A.dummy_scalar_multi_parameter.full_names[1]: (n_points,), channel_array_instrument.A.dummy_scalar_multi_parameter.full_names[0]: (n_points,), channel_array_instrument.A.dummy_scalar_multi_parameter.full_names[1]: (n_points,), channel_array_instrument.A.dummy_2d_multi_parameter.full_names[0]: (n_points, 5, 3), channel_array_instrument.A.dummy_2d_multi_parameter.full_names[1]: (n_points, 5, 3), channel_array_instrument.A.dummy_2d_multi_parameter_2.full_names[0]: (n_points, 5, 3), channel_array_instrument.A.dummy_2d_multi_parameter_2.full_names[1]: (n_points, 2, 7), channel_array_instrument.A.dummy_array_parameter.full_name: (n_points, 5), channel_array_instrument.A.dummy_complex_array_parameter.full_name: (n_points, 5), channel_array_instrument.A.dummy_complex.full_name: (n_points,), channel_array_instrument.A.dummy_parameter_with_setpoints.full_name: (n_points, pws_shape_1), channel_array_instrument.A.dummy_parameter_with_setpoints_complex.full_name: (n_points, pws_shape_1) } ) for param in meas_parameters1: meas1.register_parameter(param, setpoints=(setpoints_param,)) meas2 = Measurement() meas2.register_parameter(setpoints_param) meas_parameters2 = (channel_array_instrument.A.dummy_parameter_with_setpoints_2d,) if set_shape: meas2.set_shapes( {meas_parameters2[0].full_name: (n_points, pws_shape_1, pws_shape_2)}) for param in meas_parameters2: meas2.register_parameter(param, setpoints=(setpoints_param,)) with meas1.run( write_in_background=bg_writing, in_memory_cache=in_memory_cache ) as datasaver1: with meas2.run( write_in_background=bg_writing, in_memory_cache=in_memory_cache ) as datasaver2: dataset1 = datasaver1.dataset dataset2 = datasaver2.dataset _assert_parameter_data_is_identical(dataset1.get_parameter_data(), dataset1.cache.data()) _assert_parameter_data_is_identical(dataset2.get_parameter_data(), dataset2.cache.data()) for i, v in enumerate(setpoints_values): setpoints_param.set(v) meas_vals1 = [(param, param.get()) for param in meas_parameters1]#[:-2]] meas_vals1 += expand_setpoints_helper(meas_parameters1[-2]) meas_vals1 += expand_setpoints_helper(meas_parameters1[-1]) datasaver1.add_result((setpoints_param, v), *meas_vals1) datasaver1.flush_data_to_database(block=True) meas_vals2 = [(param, param.get()) for param in meas_parameters2] datasaver2.add_result((setpoints_param, v), *meas_vals2) datasaver2.flush_data_to_database(block=True) _assert_parameter_data_is_identical(dataset1.get_parameter_data(), dataset1.cache.data(), shaped_partial=set_shape) _assert_parameter_data_is_identical(dataset2.get_parameter_data(), dataset2.cache.data(), shaped_partial=set_shape) _assert_parameter_data_is_identical(dataset1.get_parameter_data(), dataset1.cache.data()) if in_memory_cache is False: assert dataset1.cache._loaded_from_completed_ds is True assert dataset1.completed is True assert dataset1.cache.live is in_memory_cache _assert_parameter_data_is_identical(dataset2.get_parameter_data(), dataset2.cache.data()) if in_memory_cache is False: assert dataset2.cache._loaded_from_completed_ds is True assert dataset2.completed is True assert dataset1.cache.live is in_memory_cache