def test_get_data_by_id_order(dataset): """ Test if the values of the setpoints/dependent parameters is dependent on the order of the `depends_on` value. This sounds far fetch but was actually the case before #1250. """ indepA = ParamSpec('indep1', "numeric") indepB = ParamSpec('indep2', "numeric") depAB = ParamSpec('depAB', "numeric", depends_on=[indepA, indepB]) depBA = ParamSpec('depBA', "numeric", depends_on=[indepB, indepA]) dataset.add_parameters([indepA, indepB, depAB, depBA]) dataset.add_result({'depAB': 12, 'indep2': 2, 'indep1': 1}) dataset.add_result({'depBA': 21, 'indep2': 2, 'indep1': 1}) dataset.mark_complete() data = get_data_by_id(dataset.run_id) data_dict = {el['name']: el['data'] for el in data[0]} assert data_dict['indep1'] == 1 assert data_dict['indep2'] == 2 data_dict = {el['name']: el['data'] for el in data[1]} assert data_dict['indep1'] == 1 assert data_dict['indep2'] == 2
def test_get_data_by_id_order(dataset): """ Test that the added values of setpoints end up associated with the correct setpoint parameter, irrespective of the ordering of those setpoint parameters """ indepA = ParamSpecBase('indep1', "numeric") indepB = ParamSpecBase('indep2', "numeric") depAB = ParamSpecBase('depAB', "numeric") depBA = ParamSpecBase('depBA', "numeric") idps = InterDependencies_( dependencies={depAB: (indepA, indepB), depBA: (indepB, indepA)}) dataset.set_interdependencies(idps) dataset.mark_started() dataset.add_result({'depAB': 12, 'indep2': 2, 'indep1': 1}) dataset.add_result({'depBA': 21, 'indep2': 2, 'indep1': 1}) dataset.mark_completed() data = get_data_by_id(dataset.run_id) data_dict = {el['name']: el['data'] for el in data[0]} assert data_dict['indep1'] == 1 assert data_dict['indep2'] == 2 data_dict = {el['name']: el['data'] for el in data[1]} assert data_dict['indep1'] == 1 assert data_dict['indep2'] == 2
def test_modify_results(dataset): xparam = ParamSpec("x", "numeric") dataset.add_parameter(xparam) dataset.add_result({'x': 0}) dataset.add_result({'x': 1}) pytest.deprecated_call(dataset.modify_results, 0, [{'x': [10]}]) assert [[10], [1]] == dataset.get_data(xparam) pytest.deprecated_call(dataset.modify_results, 1, [{'x': [14]}]) assert [[10], [14]] == dataset.get_data(xparam) with pytest.raises(RuntimeError, match='Rolling back due to unhandled exception'): # not sure calling `modify_results` like this is correct, anyway it # is difficult to find out what the call signature for multiple # results is supposed to look like... pytest.deprecated_call(dataset.modify_results, 0, [{ 'x': [5] }, { 'x': [6] }]) assert [[5], [6]] == dataset.get_data(xparam) pytest.xfail('modify_results does not seem to work for cases where ' 'multiple values of multiple parameters need to be changed. ' 'Anyway, the signature needs to be revisited, ' 'and consequently the correct behavior needs to be ' 'implemented and covered with tests.')
def test_modify_result(dataset): xparam = ParamSpec("x", "numeric", label="x parameter", unit='V') yparam = ParamSpec("y", 'numeric', label='y parameter', unit='Hz', depends_on=[xparam]) zparam = ParamSpec("z", 'array', label='z parameter', unit='sqrt(Hz)', depends_on=[xparam]) dataset.add_parameter(xparam) dataset.add_parameter(yparam) dataset.add_parameter(zparam) xdata = 0 ydata = 1 zdata = np.linspace(0, 1, 100) dataset.add_result({'x': 0, 'y': 1, 'z': zdata}) shadow_ds = make_shadow_dataset(dataset) try: assert dataset.get_data('x')[0][0] == xdata assert dataset.get_data('y')[0][0] == ydata assert (dataset.get_data('z')[0][0] == zdata).all() assert shadow_ds.get_data('x')[0][0] == xdata assert shadow_ds.get_data('y')[0][0] == ydata assert (shadow_ds.get_data('z')[0][0] == zdata).all() with pytest.raises(ValueError): pytest.deprecated_call(dataset.modify_result, 0, {' x': 1}) xdata = 1 ydata = 12 zdata = np.linspace(0, 1, 99) pytest.deprecated_call(dataset.modify_result, 0, {'x': xdata}) assert dataset.get_data('x')[0][0] == xdata assert shadow_ds.get_data('x')[0][0] == xdata pytest.deprecated_call(dataset.modify_result, 0, {'y': ydata}) assert dataset.get_data('y')[0][0] == ydata assert shadow_ds.get_data('y')[0][0] == ydata pytest.deprecated_call(dataset.modify_result, 0, {'z': zdata}) assert (dataset.get_data('z')[0][0] == zdata).all() assert (shadow_ds.get_data('z')[0][0] == zdata).all() dataset.mark_complete() with pytest.raises(CompletedError): pytest.deprecated_call(dataset.modify_result, 0, {'x': 2}) finally: shadow_ds.conn.close()
def ds_with_vals(self, dataset): """ This fixture creates a DataSet with values that is to be used by all the tests in this class """ dataset.add_parameter(self.x) for xv in self.xvals: dataset.add_result({self.x.name: xv}) return dataset
def ds_with_vals(self, dataset): """ This fixture creates a DataSet with values that is to be used by all the tests in this class """ idps = InterDependencies_(standalones=(self.x, )) dataset.set_interdependencies(idps) dataset.mark_started() for xv in self.xvals: dataset.add_result({self.x.name: xv}) return dataset
def test_modify_result(): dataset = new_data_set("test_modify_result") xparam = ParamSpec("x", "numeric", label="x parameter", unit='V') yparam = ParamSpec("y", 'numeric', label='y parameter', unit='Hz', depends_on=[xparam]) zparam = ParamSpec("z", 'array', label='z parameter', unit='sqrt(Hz)', depends_on=[xparam]) dataset.add_parameter(xparam) dataset.add_parameter(yparam) dataset.add_parameter(zparam) xdata = 0 ydata = 1 zdata = np.linspace(0, 1, 100) dataset.add_result({'x': 0, 'y': 1, 'z': zdata}) assert dataset.get_data('x')[0][0] == xdata assert dataset.get_data('y')[0][0] == ydata assert (dataset.get_data('z')[0][0] == zdata).all() with pytest.raises(ValueError): dataset.modify_result(0, {' x': 1}) xdata = 1 ydata = 12 zdata = np.linspace(0, 1, 99) dataset.modify_result(0, {'x': xdata}) assert dataset.get_data('x')[0][0] == xdata dataset.modify_result(0, {'y': ydata}) assert dataset.get_data('y')[0][0] == ydata dataset.modify_result(0, {'z': zdata}) assert (dataset.get_data('z')[0][0] == zdata).all() dataset.mark_complete() with pytest.raises(CompletedError): dataset.modify_result(0, {'x': 2})
def test_basic_subscription(dataset, basic_subscriber): xparam = ParamSpec(name='x', paramtype='numeric', label='x parameter', unit='V') yparam = ParamSpec(name='y', paramtype='numeric', label='y parameter', unit='Hz', depends_on=[xparam]) dataset.add_parameter(xparam) dataset.add_parameter(yparam) sub_id = dataset.subscribe(basic_subscriber, min_wait=0, min_count=1, state={}) assert len(dataset.subscribers) == 1 assert list(dataset.subscribers.keys()) == [sub_id] expected_state = {} for x in range(10): y = -x**2 dataset.add_result({'x': x, 'y': y}) expected_state[x + 1] = [(x, y)] @retry_until_does_not_throw(exception_class_to_expect=AssertionError, delay=0, tries=10) def assert_expected_state(): assert dataset.subscribers[sub_id].state == expected_state assert_expected_state() dataset.unsubscribe(sub_id) assert len(dataset.subscribers) == 0 assert list(dataset.subscribers.keys()) == [] # Ensure the trigger for the subscriber have been removed from the database get_triggers_sql = "SELECT * FROM sqlite_master WHERE TYPE = 'trigger';" triggers = atomic_transaction(dataset.conn, get_triggers_sql).fetchall() assert len(triggers) == 0
def test_basic_subscription(dataset, basic_subscriber): xparam = ParamSpec(name='x', paramtype='numeric', label='x parameter', unit='V') yparam = ParamSpec(name='y', paramtype='numeric', label='y parameter', unit='Hz', depends_on=[xparam]) dataset.add_parameter(xparam) dataset.add_parameter(yparam) sub_id = dataset.subscribe(basic_subscriber, min_wait=0, min_count=1, state={}) assert len(dataset.subscribers) == 1 assert list(dataset.subscribers.keys()) == [sub_id] expected_state = {} for x in range(10): y = -x**2 dataset.add_result({'x': x, 'y': y}) expected_state[x+1] = [(x, y)] assert dataset.subscribers[sub_id].state == expected_state
def test_subscription_from_config(dataset, basic_subscriber): """ This test is similar to `test_basic_subscription`, with the only difference that another subscriber from a config file is added. """ # This string represents the config file in the home directory: config = """ { "subscription":{ "subscribers":{ "test_subscriber":{ "factory": "qcodes.tests.dataset.test_subscribing.MockSubscriber", "factory_kwargs":{ "lg": false }, "subscription_kwargs":{ "min_wait": 0, "min_count": 1, "callback_kwargs": {} } } } } } """ # This little dance around the db_location is due to the fact that the # dataset fixture creates a dataset in a db in a temporary directory. # Therefore we need to 'backup' the path to the db when using the # default configuration. db_location = qcodes.config.core.db_location with default_config(user_config=config): qcodes.config.core.db_location = db_location assert 'test_subscriber' in qcodes.config.subscription.subscribers xparam = ParamSpecBase(name='x', paramtype='numeric', label='x parameter', unit='V') yparam = ParamSpecBase(name='y', paramtype='numeric', label='y parameter', unit='Hz') idps = InterDependencies_(dependencies={yparam: (xparam,)}) dataset.set_interdependencies(idps) dataset.mark_started() sub_id = dataset.subscribe(basic_subscriber, min_wait=0, min_count=1, state={}) sub_id_c = dataset.subscribe_from_config('test_subscriber') assert len(dataset.subscribers) == 2 assert list(dataset.subscribers.keys()) == [sub_id, sub_id_c] expected_state = {} # Here we are only testing 2 to reduce the CI time for x in range(2): y = -x**2 dataset.add_result({'x': x, 'y': y}) expected_state[x+1] = [(x, y)] @retry_until_does_not_throw( exception_class_to_expect=AssertionError, delay=0, tries=10) def assert_expected_state(): assert dataset.subscribers[sub_id].state == expected_state assert dataset.subscribers[sub_id_c].state == expected_state assert_expected_state()