def test_numpy_types(): """ Test that we can save numpy types in the data set """ p = ParamSpecBase(name="p", paramtype="numeric") test_set = qc.new_data_set("test-dataset") test_set.set_interdependencies(InterDependencies_(standalones=(p, ))) test_set.mark_started() idps = InterDependencies_(standalones=(p, )) data_saver = DataSaver(dataset=test_set, write_period=0, interdeps=idps) dtypes = [ np.int8, np.int16, np.int32, np.int64, np.float16, np.float32, np.float64 ] for dtype in dtypes: data_saver.add_result(("p", dtype(2))) data_saver.flush_data_to_database() data = test_set.get_data("p") assert data == [[2] for _ in range(len(dtypes))]
def test_numpy_types(bg_writing): """ Test that we can save numpy types in the data set """ p = ParamSpecBase(name="p", paramtype="numeric") test_set = qc.new_data_set("test-dataset") test_set.set_interdependencies(InterDependencies_(standalones=(p, ))) test_set.mark_started(start_bg_writer=bg_writing) idps = InterDependencies_(standalones=(p, )) data_saver = DataSaver(dataset=test_set, write_period=0, interdeps=idps, write_in_background=bg_writing) dtypes = [ np.int8, np.int16, np.int32, np.int64, np.float16, np.float32, np.float64 ] for dtype in dtypes: data_saver.add_result(("p", dtype(2))) data_saver.flush_data_to_database() test_set.mark_completed() data = test_set.get_parameter_data("p")["p"]["p"] expected_data = np.ones(len(dtypes)) expected_data[:] = 2 np.testing.assert_array_equal(data, expected_data)
def test_saving_numeric_values_as_text(numeric_type): """ Test the saving numeric values into 'text' parameter raises an exception """ p = ParamSpecBase("p", "text") test_set = qc.new_data_set("test-dataset") test_set.set_interdependencies(InterDependencies_(standalones=(p, ))) test_set.mark_started() idps = InterDependencies_(standalones=(p, )) data_saver = DataSaver(dataset=test_set, write_period=0, interdeps=idps) try: value = numeric_type(2) gottype = np.array(value).dtype msg = re.escape(f'Parameter {p.name} is of type ' f'"{p.type}", but got a result of ' f'type {gottype} ({value}).') with pytest.raises(ValueError, match=msg): data_saver.add_result((p.name, value)) finally: data_saver.dataset.conn.close()
def test_string_with_wrong_paramtype_via_datasaver(experiment): """ Test that it is not possible to add a string value for a non-text parameter via DataSaver object """ p = ParamSpecBase("p", "numeric") test_set = qc.new_data_set("test-dataset") idps = InterDependencies_(standalones=(p, )) test_set.set_interdependencies(idps) test_set.mark_started() idps = InterDependencies_(standalones=(p, )) data_saver = DataSaver(dataset=test_set, write_period=0, interdeps=idps, write_in_background=False) try: msg = re.escape('Parameter p is of type "numeric", but got a ' "result of type <U9 (some text).") with pytest.raises(ValueError, match=msg): data_saver.add_result(("p", "some text")) finally: data_saver.dataset.conn.close()
def test_string(experiment): """ Test that we can save text in the data set """ p = ParamSpec("p", "text") test_set = qc.new_data_set("test-dataset") test_set.add_parameter(p) data_saver = DataSaver(dataset=test_set, write_period=0, parameters={"p": p}) data_saver.add_result(("p", "some text")) data_saver.flush_data_to_database() assert test_set.get_data("p") == [["some text"]]
def store_array_to_database(datasaver: DataSaver, array: DataArray) -> int: dims = len(array.shape) if dims == 2: for index1, i in enumerate(array.set_arrays[0]): for index2, j in enumerate(array.set_arrays[1][index1]): datasaver.add_result((array.set_arrays[0].array_id, i), (array.set_arrays[1].array_id, j), (array.array_id, array[index1, index2])) elif dims == 1: for index, i in enumerate(array.set_arrays[0]): datasaver.add_result((array.set_arrays[0].array_id, i), (array.array_id, array[index])) else: raise NotImplementedError( 'The exporter only currently handles 1 and 2 Dimentional data') return datasaver.run_id
def test_string_via_datasaver(experiment): """ Test that we can save text into database via DataSaver API """ p = ParamSpecBase(name="p", paramtype="text") test_set = qc.new_data_set("test-dataset") idps = InterDependencies_(standalones=(p, )) test_set.prepare(snapshot={}, interdeps=idps) idps = InterDependencies_(standalones=(p, )) data_saver = DataSaver(dataset=test_set, write_period=0, interdeps=idps) data_saver.add_result(("p", "some text")) data_saver.flush_data_to_database() assert test_set.get_parameter_data()["p"]["p"] == np.array(["some text"])
def test_string_via_datasaver(experiment): """ Test that we can save text into database via DataSaver API """ p = ParamSpec(name="p", paramtype="text") test_set = qc.new_data_set("test-dataset") test_set.add_parameter(p) test_set.mark_started() idps = InterDependencies_(standalones=(p.base_version(), )) data_saver = DataSaver(dataset=test_set, write_period=0, interdeps=idps) data_saver.add_result(("p", "some text")) data_saver.flush_data_to_database() assert test_set.get_data("p") == [["some text"]]
def test_saving_numeric_values_as_text(numeric_type): """ Test the saving numeric values into 'text' parameter raises an exception """ p = ParamSpec("p", "text") test_set = qc.new_data_set("test-dataset") test_set.add_parameter(p) test_set.mark_started() data_saver = DataSaver( dataset=test_set, write_period=0, parameters={"p": p}) try: msg = f"It is not possible to save a numeric value for parameter " \ f"'{p.name}' because its type class is 'text', not 'numeric'." with pytest.raises(ValueError, match=msg): data_saver.add_result((p.name, numeric_type(2))) finally: data_saver.dataset.conn.close()
def test_string_with_wrong_paramtype_via_datasaver(experiment): """ Test that it is not possible to add a string value for a non-text parameter via DataSaver object """ p = ParamSpec("p", "numeric") test_set = qc.new_data_set("test-dataset") test_set.add_parameter(p) data_saver = DataSaver( dataset=test_set, write_period=0, parameters={"p": p}) try: msg = "It is not possible to save a string value for parameter 'p' " \ "because its type class is 'numeric', not 'text'." with pytest.raises(ValueError, match=msg): data_saver.add_result(("p", "some text")) finally: data_saver.dataset.conn.close()
def test_numpy_types(): """ Test that we can save numpy types in the data set """ p = ParamSpec("p", "numeric") test_set = qc.new_data_set("test-dataset") test_set.add_parameter(p) test_set.mark_started() data_saver = DataSaver( dataset=test_set, write_period=0, parameters={"p": p}) dtypes = [np.int8, np.int16, np.int32, np.int64, np.float16, np.float32, np.float64] for dtype in dtypes: data_saver.add_result(("p", dtype(2))) data_saver.flush_data_to_database() data = test_set.get_data("p") assert data == [[2] for _ in range(len(dtypes))]
def test_duplicated_parameter_raises(): """ Test that passing same parameter multiple times to ``add_result`` raises an exception """ p = ParamSpecBase("p", "text") test_set = qc.new_data_set("test-dataset") test_set.set_interdependencies(InterDependencies_(standalones=(p, ))) test_set.mark_started() idps = InterDependencies_(standalones=(p, )) data_saver = DataSaver(dataset=test_set, write_period=0, interdeps=idps) try: msg = re.escape( "Not all parameter names are unique. Got multiple values for ['p']" ) with pytest.raises(ValueError, match=msg): data_saver.add_result((p.name, 1), (p.name, 1)) finally: data_saver.dataset.mark_completed() data_saver.dataset.conn.close()