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_numpy_nan(dataset): parameter_m = ParamSpec("m", "numeric") dataset.add_parameters([parameter_m]) data_dict = [{"m": value} for value in [0.0, np.nan, 1.0]] dataset.add_results(data_dict) retrieved = dataset.get_data("m") assert np.isnan(retrieved[1])
def test_numpy_nan(dataset): parameter_m = ParamSpecBase("m", "numeric") idps = InterDependencies_(standalones=(parameter_m, )) dataset.set_interdependencies(idps) dataset.mark_started() data_dict = [{"m": value} for value in [0.0, np.nan, 1.0]] dataset.add_results(data_dict) retrieved = dataset.get_data("m") assert np.isnan(retrieved[1])
def test_numpy_inf(dataset): """ Test that we can insert and retrieve numpy inf in the data set """ parameter_m = ParamSpec("m", "numeric") dataset.add_parameter(parameter_m) dataset.mark_started() data_dict = [{"m": value} for value in [-np.inf, np.inf]] dataset.add_results(data_dict) retrieved = dataset.get_data("m") assert np.isinf(retrieved).all()
def test_numpy_floats(dataset): """ Test that we can insert numpy floats in the data set """ float_param = ParamSpec('y', 'numeric') dataset.add_parameters([float_param]) numpy_floats = [np.float, np.float16, np.float32, np.float64] results = [{"y": tp(1.2)} for tp in numpy_floats] dataset.add_results(results) expected_result = [[tp(1.2)] for tp in numpy_floats] assert np.allclose(dataset.get_data("y"), expected_result, atol=1E-8)
def test_numpy_inf(dataset): """ Test that we can insert and retrieve numpy inf in the data set """ parameter_m = ParamSpecBase("m", "numeric") idps = InterDependencies_(standalones=(parameter_m, )) dataset.set_interdependencies(idps) dataset.mark_started() data_dict = [{"m": value} for value in [-np.inf, np.inf]] dataset.add_results(data_dict) retrieved = dataset.get_data("m") assert np.isinf(retrieved).all()
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 test_numpy_floats(dataset): """ Test that we can insert numpy floats in the data set """ float_param = ParamSpecBase('y', 'numeric') idps = InterDependencies_(standalones=(float_param, )) dataset.set_interdependencies(idps) dataset.mark_started() numpy_floats = [np.float, np.float16, np.float32, np.float64] results = [{"y": tp(1.2)} for tp in numpy_floats] dataset.add_results(results) expected_result = [[tp(1.2)] for tp in numpy_floats] assert np.allclose(dataset.get_data("y"), expected_result, atol=1E-8)
def test_numpy_ints(dataset): """ Test that we can insert numpy integers in the data set """ xparam = ParamSpec('x', 'numeric') dataset.add_parameters([xparam]) numpy_ints = [ np.int, np.int8, np.int16, np.int32, np.int64, np.uint, np.uint8, np.uint16, np.uint32, np.uint64 ] results = [{"x": tp(1)} for tp in numpy_ints] dataset.add_results(results) expected_result = len(numpy_ints) * [[1]] assert dataset.get_data("x") == expected_result
def test_numpy_ints(dataset): """ Test that we can insert numpy integers in the data set """ xparam = ParamSpecBase('x', 'numeric') idps = InterDependencies_(standalones=(xparam, )) dataset.set_interdependencies(idps) dataset.mark_started() numpy_ints = [ np.int, np.int8, np.int16, np.int32, np.int64, np.uint, np.uint8, np.uint16, np.uint32, np.uint64 ] results = [{"x": tp(1)} for tp in numpy_ints] dataset.add_results(results) expected_result = len(numpy_ints) * [[1]] assert dataset.get_data("x") == expected_result
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_add_parameter_values(dataset): n = 2 m = n + 1 xparam = ParamSpec('x', 'numeric') dataset.add_parameter(xparam) x_results = [{'x': x} for x in range(n)] dataset.add_results(x_results) yparam = ParamSpec("y", "numeric") match_str = f'Need to have {n} values but got {m}.' match_str = re.escape(match_str) with pytest.raises(ValueError, match=match_str): pytest.deprecated_call(dataset.add_parameter_values, yparam, [y for y in range(m)]) yvals = [y for y in range(n)] # Unlike what the docstring of the method suggests, # `add_parameter_values` does NOT add a new parameter and values for it # "NEXT TO the columns of values of existing parameters". # # In other words, if the initial state of the table is: # # | x | # -------- # | 1 | # | 2 | # # then the state of the table after calling `add_parameter_values` is # going to be: # # | x | y | # --------------- # | 1 | NULL | # | 2 | NULL | # | NULL | 25 | # | NULL | 42 | # # while the docstring suggests the following state: # # | x | y | # --------------- # | 1 | 25 | # | 2 | 42 | # y_expected = [[None]] * n + [[y] for y in yvals] pytest.deprecated_call(dataset.add_parameter_values, yparam, yvals) shadow_ds = make_shadow_dataset(dataset) try: assert y_expected == dataset.get_data(yparam) assert y_expected == shadow_ds.get_data(yparam) dataset.mark_complete() # and now let's test that dataset's connection does not commit anymore # when `atomic` is used dataset.add_results([{yparam.name: -2}]) y_expected_2 = y_expected + [[-2]] assert y_expected_2 == dataset.get_data(yparam) assert y_expected_2 == shadow_ds.get_data(yparam) finally: shadow_ds.conn.close()