def _as_csdm_object(self, data: np.ndarray, method: Method) -> cp.CSDM: """ Converts the simulation data from the given method to a CSDM object. Read `csdmpy <https://csdmpy.readthedocs.io/en/stable/>`_ for details Return: A CSDM object. """ new = cp.new() for dimension in method.spectral_dimensions[::-1]: new.add_dimension(dimension.to_csdm_dimension()) if new.dimensions[-1].origin_offset != 0: new.dimensions[-1].to("ppm", "nmr_frequency_ratio") dependent_variable = { "type": "internal", "quantity_type": "scalar", "numeric_type": "float64", } for index, datum in enumerate(data): if len(datum) == 0: continue dependent_variable["components"] = [datum] if self.config.decompose_spectrum == "spin_system": self._update_name_description_application(dependent_variable, index) new.add_dependent_variable(dependent_variable) new.dependent_variables[-1].encoding = "base64" return new
def test_missing_type(): data = cp.new() dim = { "numeric_type": "float32", "quantity_type": "scalar", "components": [np.arange(10)], } error = "Missing a required `type` key from the dependent variable." with pytest.raises(KeyError, match=".*{0}.*".format(error)): data.add_dependent_variable(dim)
def test_wrong_type(): data = cp.new() dim = { "type": "", "numeric_type": "float32", "quantity_type": "scalar", "components": [np.arange(10)], } error = "is an invalid DependentVariable 'type'" with pytest.raises(ValueError, match=".*{0}.*".format(error)): data.add_dependent_variable(dim)
def setup(): data = cp.new(description="An emoji dataset") x = dict(type="labeled", labels=["🍈", "🍉", "🍋", "🍌", "🥑", "🍍"]) data.add_dimension(x) y = dict( type="internal", numeric_type="float32", quantity_type="scalar", components=[[0.5, 0.25, 1, 2, 1, 0.25]], ) data.add_dependent_variable(y) return data
def test_csdm(): data = cp.new(description="This is a test") # read_only assert data.read_only is False data.read_only = True assert data.read_only is True error = "Expecting an instance of type" with pytest.raises(TypeError, match=".*{0}.*".format(error)): data.read_only = "True" # tags assert data.tags == [] data.tags = ["1", "2", "3"] assert data.tags == ["1", "2", "3"] error = "Expecting an instance of type" with pytest.raises(TypeError, match=".*{0}.*".format(error)): data.tags = "23" # version assert data.version == cp.csdm.CSDM.__latest_CSDM_version__ # geographic_coordinate assert data.geographic_coordinate == {} error = "can't set attribute" with pytest.raises(AttributeError, match=".*{0}.*".format(error)): data.geographic_coordinate = {} # description assert data.description == "This is a test" data.description = "Enough with the tests" assert data.description == "Enough with the tests" error = "Expecting an instance of type" with pytest.raises(TypeError, match=".*{0}.*".format(error)): data.description = {} # application assert data.application == {} data.application = {"csdmpy": "Some day"} assert data.application == {"csdmpy": "Some day"} error = "Expecting an instance of type" with pytest.raises(TypeError, match=".*{0}.*".format(error)): data.application = "Some other day" # filename assert data.filename == "" # data_structure structure = { "csdm": { "version": "1.0", "read_only": True, "tags": ["1", "2", "3"], "description": "Enough with the tests", "dimensions": [], "dependent_variables": [], } } assert data.data_structure == str( json.dumps(structure, ensure_ascii=False, sort_keys=False, indent=2)) assert data.to_dict() == structure
def test_internal_new(): data = cp.new() test_array = np.arange(20).reshape(2, 10) dim = { "type": "internal", "numeric_type": "float32", "quantity_type": "vector_2", "components": test_array, } data.add_dependent_variable(dim) # check type assert data.dependent_variables[0].type == "internal" data.dependent_variables[0].type = "external" assert data.dependent_variables[0].type == "external" error = "is not a valid value. The allowed values are" with pytest.raises(ValueError, match=".*{0}.*".format(error)): data.dependent_variables[0].type = "celestial" # check components assert np.all(data.dependent_variables[0].components == test_array) assert data.dependent_variables[0].numeric_type == "float32" # assign and check components data.dependent_variables[0].components = test_array.astype("int32") + 100 assert np.all(data.dependent_variables[0].components == test_array + 100.0) assert data.dependent_variables[0].numeric_type == "int32" # check name assert data.dependent_variables[0].name == "" data.dependent_variables[0].name = "happy days" assert data.dependent_variables[0].name == "happy days" # check unit assert data.dependent_variables[0].unit == "" error = r"`unit` attribute cannot be modified" with pytest.raises(AttributeError, match=".*{0}.*".format(error)): data.dependent_variables[0].unit = "m/s" # check component_url error = r"DependentVariable' object has no attribute 'component_url" with pytest.raises(AttributeError, match=error): data.dependent_variables[0].component_url # component names assert data.dependent_variables[0].component_labels == ["", ""] data.dependent_variables[0].component_labels = [":)"] assert data.dependent_variables[0].component_labels == [":)", ""] data.dependent_variables[0].component_labels = [] assert data.dependent_variables[0].component_labels == ["", ""] data.dependent_variables[0].component_labels = ["1", "2", "3"] assert data.dependent_variables[0].component_labels == ["1", "2"] data.dependent_variables[0].component_labels[0] = ":(" assert data.dependent_variables[0].component_labels == [":(", "2"] # quantity type assert data.dependent_variables[0].quantity_type == "vector_2" # Need to fix this data.dependent_variables[0].quantity_type = "vector_2" # encoding assert data.dependent_variables[0].encoding == "base64" data.dependent_variables[0].encoding = "none" assert data.dependent_variables[0].encoding == "none" error = "not a valid `encoding` enumeration literal. The allowed values are" with pytest.raises(ValueError, match=".*{0}.*".format(error)): data.dependent_variables[0].encoding = "base16" data.dependent_variables[0].encoding = "raw" assert data.dependent_variables[0].encoding == "raw" # numeric_type assert data.dependent_variables[0].numeric_type == "int32" data.dependent_variables[0].numeric_type = "complex64" assert data.dependent_variables[0].numeric_type == "complex64" assert np.all(data.dependent_variables[0].components == test_array + 100.0) error = "not a valid `numeric_type` enumeration literal. The allowed values are" with pytest.raises(ValueError, match=".*{0}.*".format(error)): data.dependent_variables[0].numeric_type = "complex32" # quantity_name assert data.dependent_variables[0].quantity_name == "dimensionless" error = "`quantity_name` attribute cannot be modified." with pytest.raises(NotImplementedError, match=".*{0}.*".format(error)): data.dependent_variables[0].quantity_name = "time" # description assert data.dependent_variables[0].description == "" data.dependent_variables[0].description = "This is a test" assert data.dependent_variables[0].description == "This is a test" error = "Expecting an instance of type" with pytest.raises(TypeError, match=".*{0}.*".format(error)): data.dependent_variables[0].description = {} # application assert data.dependent_variables[0].application == {} with pytest.raises(TypeError, match=".*{0}.*".format(error)): data.dependent_variables[0].application = "" dependent_variables_dict_1 = [ { "type": "internal", "description": "This is a test", "name": "happy days", "numeric_type": "complex64", "quantity_type": "vector_2", "component_labels": [":(", "2"], "components": [ ["(100+0j), (101+0j), ..., " "(108+0j), (109+0j)"], ["(110+0j), (111+0j), ..., (118+0j), (119+0j)"], ], } ] dict1 = { "csdm": { "version": "1.0", "dimensions": [], "dependent_variables": dependent_variables_dict_1, } } assert data.data_structure == str( json.dumps(dict1, ensure_ascii=False, sort_keys=False, indent=2) ) data.dependent_variables[0].encoding = "base64" dependent_variables_dict_2 = { "type": "internal", "description": "This is a test", "name": "happy days", "numeric_type": "complex64", "quantity_type": "vector_2", "component_labels": [":(", "2"], "encoding": "base64", "components": [ "AADIQgAAAAAAAMpCAAAAAAAAzEIAAAAAAADOQgAAAAAAANBCAAAAAAAA0kIAAAAAAADUQgAAAAAAANZCAAAAAAAA2EIAAAAAAADaQgAAAAA=", "AADcQgAAAAAAAN5CAAAAAAAA4EIAAAAAAADiQgAAAAAAAORCAAAAAAAA5kIAAAAAAADoQgAAAAAAAOpCAAAAAAAA7EIAAAAAAADuQgAAAAA=", ], } assert data.dependent_variables[0].to_dict() == dependent_variables_dict_2
def test_missing_component_url(): data = cp.new() dim = {"type": "external", "numeric_type": "float32", "quantity_type": "scalar"} error = "Missing a required `components_url` key" with pytest.raises(KeyError, match=".*{0}.*".format(error)): data.add_dependent_variable(dim)
def test_external_new(): data = cp.new() dim = { "type": "external", "components_url": ( "https://osu.box.com/shared/static/b967zfl7efcvf471wm9a7tqb74kqomuh.dat" ), "component_labels": ["monotonic"], "name": "Headspace from cinnamon stick", "numeric_type": "float32", "quantity_type": "scalar", } data.add_dependent_variable(dim) # check type assert data.dependent_variables[0].type == "internal" data.dependent_variables[0].type = "external" assert data.dependent_variables[0].type == "external" # check components assert data.dependent_variables[0].numeric_type == "float32" # assign and check components data.dependent_variables[0].numeric_type = "int32" assert data.dependent_variables[0].numeric_type == "int32" assert data.dependent_variables[0].components.dtype == "int32" # check name assert data.dependent_variables[0].name == "Headspace from cinnamon stick" # check unit assert data.dependent_variables[0].unit == "" # check component_url assert data.dependent_variables[0].components_url == ( "https://osu.box.com/shared/static/b967zfl7efcvf471wm9a7tqb74kqomuh.dat" ) # component names assert data.dependent_variables[0].component_labels == ["monotonic"] # quantity type assert data.dependent_variables[0].quantity_type == "scalar" # encoding assert data.dependent_variables[0].encoding == "base64" data.dependent_variables[0].encoding = "raw" assert data.dependent_variables[0].encoding == "raw" # description assert data.dependent_variables[0].description == "" data.dependent_variables[0].description = "This is also a test" assert data.dependent_variables[0].description == "This is also a test" # application assert data.dependent_variables[0].application == {} dict1 = { "csdm": { "version": "1.0", "dimensions": [], "dependent_variables": [ { "type": "internal", "description": "This is also a test", "name": "Headspace from cinnamon stick", "numeric_type": "int32", "quantity_type": "scalar", "component_labels": ["monotonic"], "components": [["48453, 48444, ..., 48040, 48040"]], } ], } } assert data.data_structure == json.dumps( dict1, ensure_ascii=False, sort_keys=False, indent=2 )
def download(): # creating a dynamic csv or file here using `StringIO` file_ = flask.request.args.get("name") nuclei = flask.request.args.get("nuclei") id_ = flask.request.args.get("id") str_io = io.StringIO() print("id", id_) if id_ == "csv": writer = np.asarray([simulator._freq.value, simulator._amp]).T _header_ = (( "\n {0} from file {1}.json\nfrequency / kHz, amplitudes\n") ).format(nuclei, file_) # save file as csv np.savetxt(str_io, writer, fmt="%f", delimiter=",", header=_header_) mem = io.BytesIO() mem.write(str_io.getvalue().encode("utf-8")) mem.seek(0) str_io.close() name_ = "_".join([file_, nuclei]) return flask.send_file( mem, mimetype="text/csv", attachment_filename=f"{name_}.csv", as_attachment=True, ) print(simulator.spectrum) if id_ == "csdf": new = cp.new() dimension = { "type": "linear", "count": simulator._spectrum_c["number_of_points"], "increment": "{0} Hz".format(simulator._spectrum_c["spectral_width"] / simulator._spectrum_c["number_of_points"]), "coordinates_offset": "{0} Hz".format(simulator._spectrum_c["reference_offset"]), "complex_fft": True, } dependent_variable = { "type": "internal", "quantity_type": "scalar", "numeric_type": "float64", "components": [simulator._amp], } new.add_dimension(dimension) new.add_dependent_variable(dependent_variable) new.dependent_variables[0].encoding = "base64" new.save(output_device=str_io) mem = io.BytesIO() mem.write(str_io.getvalue().encode("utf-8")) mem.seek(0) str_io.close() name_ = "_".join([file_, nuclei]) return flask.send_file( mem, mimetype="json", attachment_filename=f"{name_}.csdf", as_attachment=True, )
def test_monotonic_new(): data = cp.new() dim = { "type": "monotonic", "description": "Far far away.", "coordinates": ["1 m", "100 m", "1 km", "1 Gm", "0.25 lyr"], } data.add_dimension(dim) # description assert data.dimensions[0].description == "Far far away." data.dimensions[0].description = "A galaxy far far away." assert data.dimensions[0].description == "A galaxy far far away." error = "Expecting an instance of type" with pytest.raises(TypeError, match=".*{0}.*".format(error)): data.dimensions[0].description = 12 # dimension type assert data.dimensions[0].type == "monotonic" # values assert data.dimensions[0].subtype._values == [ "1 m", "100 m", "1 km", "1 Gm", "0.25 lyr", ] # increment error = "'MonotonicDimension' object has no attribute 'increment'" with pytest.raises(AttributeError, match=".*{0}.*".format(error)): data.dimensions[0].increment # label assert data.dimensions[0].label == "" data.dimensions[0].label = "some string" assert data.dimensions[0].label == "some string" error = "Expecting an instance of type" with pytest.raises(TypeError, match=".*{0}.*".format(error)): data.dimensions[0].label = {} # count assert data.dimensions[0].count == 5 error = "Cannot set count," with pytest.raises(ValueError, match=".*{0}.*".format(error)): data.dimensions[0].count = 12 error = "Expecting an instance of type" with pytest.raises(TypeError, match=".*{0}.*".format(error)): data.dimensions[0].count = "12" # coordinates_offset error = "`MonotonicDimension` has no attribute `coordinates_offset`." with pytest.raises(AttributeError, match=".*{0}.*".format(error)): data.dimensions[0].coordinates_offset # origin offset assert str(data.dimensions[0].origin_offset) == "0.0 m" data.dimensions[0].origin_offset = ScalarQuantity("3.1415 m") assert str(data.dimensions[0].origin_offset) == "3.1415 m" data.dimensions[0].origin_offset = "1 lyr" assert str(data.dimensions[0].origin_offset) == "1.0 lyr" error = "Expecting an instance of type" with pytest.raises(TypeError, match=".*{0}.*".format(error)): data.dimensions[0].origin_offset = {"12 m"} # quantity_name assert data.dimensions[0].quantity_name == "length" error = "This attribute is not yet implemented" with pytest.raises(NotImplementedError, match=".*{0}.*".format(error)): data.dimensions[0].quantity_name = "area/length" # period assert str(data.dimensions[0].period) == "inf m" data.dimensions[0].period = "Infinity m" assert str(data.dimensions[0].period) == "inf m" data.dimensions[0].period = "20 m^2/m" assert str(data.dimensions[0].period) == "20.0 m" data.dimensions[0].period = "(1/0) m^5/m^4" assert str(data.dimensions[0].period) == "inf m" # fft output order error = "'MonotonicDimension' object has no attribute 'complex_fft'" with pytest.raises(AttributeError, match=".*{0}.*".format(error)): data.dimensions[0].complex_fft # coordinates assert np.allclose( data.dimensions[0].coordinates.value, np.asarray([ 1.00000000e00, 1.00000000e02, 1.00000000e03, 1.00000000e09, 2.36518262e15 ]), ) # coordinates assert np.allclose( data.dimensions[0].absolute_coordinates.value, np.asarray([ 9.46073047e15, 9.46073047e15, 9.46073047e15, 9.46073147e15, 1.18259131e16 ]), ) dict1 = { "csdm": { "version": "1.0", "dimensions": [{ "type": "monotonic", "description": "A galaxy far far away.", "coordinates": ["1 m", "100 m", "1 km", "1 Gm", "0.25 lyr"], "origin_offset": "1.0 lyr", "quantity_name": "length", "label": "some string", "reciprocal": { "quantity_name": "wavenumber" }, }], "dependent_variables": [], } } assert data.data_structure == json.dumps(dict1, ensure_ascii=False, sort_keys=False, indent=2) assert data.dimensions[0].to_dict() == dict1["csdm"]["dimensions"][0]
def test_linear_new(): data = cp.new() dim = { "type": "linear", "increment": "10 m/s", "count": 10, "coordinates_offset": "5 m/s", } data.add_dimension(dim) assert data.dimensions[0].type == "linear" error = "can't set attribute" with pytest.raises(AttributeError, match=".*{0}.*".format(error)): data.dimensions[0].type = "monotonic" assert str(data.dimensions[0].increment) == "10.0 m / s" data.dimensions[0].increment = ScalarQuantity("20.0 m / s") assert str(data.dimensions[0].increment) == "20.0 m / s" data.dimensions[0].increment = 20.0 * u.Unit("m / s") assert str(data.dimensions[0].increment) == "20.0 m / s" error = "Expecting an instance of type" with pytest.raises(TypeError, match=".*{0}.*".format(error)): data.dimensions[0].increment = 10 data.dimensions[0].increment = "20/2 m / s" assert str(data.dimensions[0].increment) == "10.0 m / s" assert data.dimensions[0].count == 10 assert data.dimensions[0].application == {} data.dimensions[0].application = {"my_application": {}} assert data.dimensions[0].application == {"my_application": {}} error = "Expecting an instance of type" with pytest.raises(TypeError, match=".*{0}.*".format(error)): data.dimensions[0].application = "my_application" assert str(data.dimensions[0].coordinates_offset) == "5.0 m / s" error = "Expecting an instance of type" with pytest.raises(TypeError, match=".*{0}.*".format(error)): data.dimensions[0].coordinates_offset = 50 data.dimensions[0].coordinates_offset = ScalarQuantity("5.0 m / s") assert str(data.dimensions[0].coordinates_offset) == "5.0 m / s" assert str(data.dimensions[0].origin_offset) == "0.0 m / s" assert data.dimensions[0].quantity_name == "speed" assert str(data.dimensions[0].period) == "inf m / s" assert data.dimensions[0].complex_fft is False assert np.all( data.dimensions[0].coordinates.value == np.arange(10) * 10.0 + 5.0) data.dimensions[0].count = 12 assert data.dimensions[0].count == 12 assert np.all( data.dimensions[0].coordinates.value == np.arange(12) * 10.0 + 5.0) assert np.all( data.dimensions[0].absolute_coordinates.value == np.arange(12) * 10.0 + 5.0) data.dimensions[0].origin_offset = "1 km/s" assert str(data.dimensions[0].origin_offset) == "1.0 km / s" assert np.all( data.dimensions[0].coordinates.value == np.arange(12) * 10.0 + 5.0) test_with = np.arange(12) * 10.0 + 5.0 + 1000.0 assert np.all(data.dimensions[0].absolute_coordinates.value == test_with) data.dimensions[0].increment = "20 m/s" assert str(data.dimensions[0].increment) == "20.0 m / s" assert np.all( data.dimensions[0].coordinates.value == np.arange(12) * 20.0 + 5.0) test_with = np.arange(12) * 20.0 + 5.0 + 1000.0 assert np.all(data.dimensions[0].absolute_coordinates.value == test_with) data.dimensions[0].complex_fft = True assert data.dimensions[0].complex_fft is True assert np.all( data.dimensions[0].coordinates.value == (np.arange(12) - 6) * 20.0 + 5.0) test_with = (np.arange(12) - 6) * 20.0 + 5.0 + 1000.0 assert np.all(data.dimensions[0].absolute_coordinates.value == test_with) dict1 = { "csdm": { "version": "1.0", "dimensions": [{ "type": "linear", "count": 12, "increment": "20.0 m * s^-1", "coordinates_offset": "5.0 m * s^-1", "origin_offset": "1.0 km * s^-1", "quantity_name": "speed", "application": { "my_application": {} }, "complex_fft": True, }], "dependent_variables": [], } } assert data.data_structure == json.dumps(dict1, ensure_ascii=False, sort_keys=False, indent=2) assert data.dimensions[0].to_dict() == dict1["csdm"]["dimensions"][0]