From 3f34d9d16c8c1dbb054fe911293d38cad7cfb528 Mon Sep 17 00:00:00 2001 From: EmileDvs <58598539+EmileDvs@users.noreply.github.com> Date: Mon, 25 Oct 2021 10:49:01 +0200 Subject: [PATCH 01/10] [WiP] debug get_xyz_along including periodicity and if there is only radial component --- SciDataTool/Functions/conversions.py | 5 ++-- .../Methods/VectorField/get_xyz_along.py | 27 +++++++++++++------ SciDataTool/Methods/VectorField/to_xyz.py | 4 ++- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/SciDataTool/Functions/conversions.py b/SciDataTool/Functions/conversions.py index 846b29dd..15851021 100644 --- a/SciDataTool/Functions/conversions.py +++ b/SciDataTool/Functions/conversions.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from SciDataTool.Functions import UnitError from SciDataTool.Functions.fft_functions import ( comp_fft_freqs, @@ -281,7 +280,7 @@ def to_dB(values, unit, ref_value=1.0): try: convert(values, unit, "W") values_dB = 10.0 * where(mask, log10(values / ref_value, where=mask), 0) - except: + except Exception: values_dB = 20.0 * where(mask, log10(values / ref_value, where=mask), 0) return values_dB @@ -410,7 +409,7 @@ def dB_to_dBA(values, freqs, noct=None): try: values += Aweight return values - except: + except Exception: raise UnitError("ERROR: dBA conversion only available for 1D fft") diff --git a/SciDataTool/Methods/VectorField/get_xyz_along.py b/SciDataTool/Methods/VectorField/get_xyz_along.py index 77cc3678..e30558f7 100644 --- a/SciDataTool/Methods/VectorField/get_xyz_along.py +++ b/SciDataTool/Methods/VectorField/get_xyz_along.py @@ -28,7 +28,7 @@ def get_xyz_along(self, *args, unit="SI", is_norm=False, axis_data=[], is_squeez if len(args) == 1 and type(args[0]) == tuple: args = args[0] # if called from another script with *args - if "radial" in self.components.keys() and "tangential" in self.components.keys(): + if "radial" in self.components.keys() or "tangential" in self.components.keys(): # Extract first along whole "angle" axis new_args = [arg for arg in args] string = [s for s in args if "angle" in s] @@ -42,15 +42,26 @@ def get_xyz_along(self, *args, unit="SI", is_norm=False, axis_data=[], is_squeez is_norm=is_norm, axis_data=axis_data, ) - Dataphi = self.components["tangential"].get_data_along( - *new_args, - unit=unit, - is_norm=is_norm, - axis_data=axis_data, - ) field_r = Datar.values - field_c = Dataphi.values shape = field_r.shape + if "tangential" in self.components.keys(): + Dataphi = self.components["tangential"].get_data_along( + *new_args, + unit=unit, + is_norm=is_norm, + axis_data=axis_data, + ) + field_c = Dataphi.values + else: + field_c = zeros(shape, dtype=field_r.dtype) + Dataphi = type(Datar)( + name=Datar.name, + unit=Datar.unit, + symbol=Datar.symbol, + axes=Datar.axes, + values=field_c, + ) + phi = Datar.get_axes("angle")[0].get_values() # Convert to cylindrical coordinates (field_x, field_y) = pol2cart(field_r, field_c, phi) diff --git a/SciDataTool/Methods/VectorField/to_xyz.py b/SciDataTool/Methods/VectorField/to_xyz.py index f9f6146b..e20ad6fe 100644 --- a/SciDataTool/Methods/VectorField/to_xyz.py +++ b/SciDataTool/Methods/VectorField/to_xyz.py @@ -21,7 +21,9 @@ def to_xyz(self): else: # Coordinate transform - arg_list = [axis.name for axis in self.components["radial"].axes] + arg_list = [ + axis.name + "[smallestperiod]" for axis in self.components["radial"].axes + ] result = self.get_xyz_along(*arg_list) # Store in new VectorField comp_dict = dict() From 16e29f57745433732dfa90db561b0b8f20562dcb Mon Sep 17 00:00:00 2001 From: helene-t Date: Mon, 25 Oct 2021 11:20:57 +0200 Subject: [PATCH 02/10] [BC] take symmetries into account + robustness to z axis in pol2cart + robustness to no tangential comp --- SciDataTool/Functions/conversions.py | 25 +++++-- SciDataTool/Methods/RequestedAxis/get_axis.py | 16 ++--- .../Methods/VectorField/get_rphiz_along.py | 11 +-- .../Methods/VectorField/get_xyz_along.py | 14 ++-- SciDataTool/Methods/VectorField/to_rphiz.py | 4 +- SciDataTool/Methods/VectorField/to_xyz.py | 2 +- Tests/Validation/test_vectfield.py | 69 ++++++++++++++++++- 7 files changed, 115 insertions(+), 26 deletions(-) diff --git a/SciDataTool/Functions/conversions.py b/SciDataTool/Functions/conversions.py index 15851021..8b8788f9 100644 --- a/SciDataTool/Functions/conversions.py +++ b/SciDataTool/Functions/conversions.py @@ -27,6 +27,7 @@ take, zeros, floor, + ones, ) # List of the unit symbols, their normalizing value and their dimensions "MLTTempAngleCurrent" @@ -606,8 +607,16 @@ def cart2pol(field_x, field_y, phi): cos_phi = cos(phi) sin_phi = sin(phi) - field_r = cos_phi * field_x + sin_phi * field_y - field_phi = -sin_phi * field_x + cos_phi * field_y + dim_array = ones((1, field_x.ndim), int).ravel() + dim_array[1] = -1 + + # Reshape b with dim_array and perform elementwise multiplication with + # broadcasting along the singleton dimensions for the final output + cos_phi_reshaped = cos_phi.reshape(dim_array) + sin_phi_reshaped = sin_phi.reshape(dim_array) + + field_r = field_x * cos_phi_reshaped + field_y * sin_phi_reshaped + field_phi = -field_x * sin_phi_reshaped + field_y * cos_phi_reshaped return (field_r, field_phi) @@ -616,7 +625,15 @@ def pol2cart(field_r, field_phi, phi): cos_phi = cos(phi) sin_phi = sin(phi) - field_x = cos_phi * field_r - sin_phi * field_phi - field_y = sin_phi * field_r + cos_phi * field_phi + dim_array = ones((1, field_r.ndim), int).ravel() + dim_array[1] = -1 + + # Reshape b with dim_array and perform elementwise multiplication with + # broadcasting along the singleton dimensions for the final output + cos_phi_reshaped = cos_phi.reshape(dim_array) + sin_phi_reshaped = sin_phi.reshape(dim_array) + + field_x = field_r * cos_phi_reshaped - field_phi * sin_phi_reshaped + field_y = field_r * sin_phi_reshaped + field_phi * cos_phi_reshaped return (field_x, field_y) diff --git a/SciDataTool/Methods/RequestedAxis/get_axis.py b/SciDataTool/Methods/RequestedAxis/get_axis.py index 3128da25..5bd4dca2 100644 --- a/SciDataTool/Methods/RequestedAxis/get_axis.py +++ b/SciDataTool/Methods/RequestedAxis/get_axis.py @@ -61,14 +61,14 @@ def get_axis(self, axis, is_real): is_antiperiod = False self.extension = "smallestperiod" elif self.extension == "smallestperiod": - if isinstance(axis, DataPattern): - raise AxisError( - "[smallestperiod] cannot be called with DataPattern axis" - ) - else: - is_smallestperiod = True - is_oneperiod = False - is_antiperiod = False + # if isinstance(axis, DataPattern): + # raise AxisError( + # "[smallestperiod] cannot be called with DataPattern axis" + # ) + # else: + is_smallestperiod = True + is_oneperiod = False + is_antiperiod = False elif self.extension == "antiperiod": if isinstance(axis, DataPattern): raise AxisError("[antiperiod] cannot be called with DataPattern axis") diff --git a/SciDataTool/Methods/VectorField/get_rphiz_along.py b/SciDataTool/Methods/VectorField/get_rphiz_along.py index db25eb21..9caa59a6 100644 --- a/SciDataTool/Methods/VectorField/get_rphiz_along.py +++ b/SciDataTool/Methods/VectorField/get_rphiz_along.py @@ -34,9 +34,9 @@ def get_rphiz_along( # Extract first along whole "angle" axis new_args = [arg for arg in args] string = [s for s in args if "angle" in s] - if string != []: + if string != [] and "smallestperiod" not in string[0]: new_args[args.index(string[0])] = "angle" - else: + elif string == []: new_args.extend(["angle"]) Datax = self.components["comp_x"].get_data_along( *new_args, @@ -53,7 +53,10 @@ def get_rphiz_along( field_x = Datax.values field_y = Datay.values shape = field_x.shape - phi = Datax.get_axes("angle")[0].get_values() + if "smallestperiod" in string[0]: + phi = Datax.get_axes("angle")[0].get_values(is_smallestperiod=True) + else: + phi = Datax.get_axes("angle")[0].get_values() # Convert to cylindrical coordinates (field_r, field_t) = cart2pol(field_x, field_y, phi) # Extract second time with true args @@ -77,7 +80,7 @@ def get_rphiz_along( axis_data=axis_data, is_squeeze=is_squeeze, ) - field_t = resultt[self.components["tangential"].symbol] + field_c = resultt[self.components["tangential"].symbol] # Delete temporary Data objects del self.components["radial"] del self.components["tangential"] diff --git a/SciDataTool/Methods/VectorField/get_xyz_along.py b/SciDataTool/Methods/VectorField/get_xyz_along.py index e30558f7..bc1aa87f 100644 --- a/SciDataTool/Methods/VectorField/get_xyz_along.py +++ b/SciDataTool/Methods/VectorField/get_xyz_along.py @@ -29,12 +29,12 @@ def get_xyz_along(self, *args, unit="SI", is_norm=False, axis_data=[], is_squeez args = args[0] # if called from another script with *args if "radial" in self.components.keys() or "tangential" in self.components.keys(): - # Extract first along whole "angle" axis + # Extract first along whole or smallest period "angle" axis new_args = [arg for arg in args] string = [s for s in args if "angle" in s] - if string != []: + if string != [] and "smallestperiod" not in string[0]: new_args[args.index(string[0])] = "angle" - else: + elif string == []: new_args.extend(["angle"]) Datar = self.components["radial"].get_data_along( *new_args, @@ -61,12 +61,14 @@ def get_xyz_along(self, *args, unit="SI", is_norm=False, axis_data=[], is_squeez axes=Datar.axes, values=field_c, ) - - phi = Datar.get_axes("angle")[0].get_values() + if "smallestperiod" in string[0]: + phi = Datar.get_axes("angle")[0].get_values(is_smallestperiod=True) + else: + phi = Datar.get_axes("angle")[0].get_values() # Convert to cylindrical coordinates (field_x, field_y) = pol2cart(field_r, field_c, phi) # Extract second time with true args - if "angle" not in args: + if "angle" not in args and "angle[smallestperiod]": Datar.values = field_x Dataphi.values = field_y self.components["comp_x"] = Datar diff --git a/SciDataTool/Methods/VectorField/to_rphiz.py b/SciDataTool/Methods/VectorField/to_rphiz.py index df69f529..05e80250 100644 --- a/SciDataTool/Methods/VectorField/to_rphiz.py +++ b/SciDataTool/Methods/VectorField/to_rphiz.py @@ -21,7 +21,9 @@ def to_rphiz(self): else: # Coordinate transform - arg_list = [axis.name for axis in self.components["comp_x"].axes] + arg_list = [ + axis.name + "[smallestperiod]" for axis in self.components["comp_x"].axes + ] result = self.get_rphiz_along(*arg_list) # Store in new VectorField comp_dict = dict() diff --git a/SciDataTool/Methods/VectorField/to_xyz.py b/SciDataTool/Methods/VectorField/to_xyz.py index e20ad6fe..e08b4b52 100644 --- a/SciDataTool/Methods/VectorField/to_xyz.py +++ b/SciDataTool/Methods/VectorField/to_xyz.py @@ -24,7 +24,7 @@ def to_xyz(self): arg_list = [ axis.name + "[smallestperiod]" for axis in self.components["radial"].axes ] - result = self.get_xyz_along(*arg_list) + result = self.get_xyz_along(*arg_list, is_squeeze=False) # Store in new VectorField comp_dict = dict() diff --git a/Tests/Validation/test_vectfield.py b/Tests/Validation/test_vectfield.py index b98576f4..6e9ba716 100644 --- a/Tests/Validation/test_vectfield.py +++ b/Tests/Validation/test_vectfield.py @@ -1,5 +1,12 @@ import pytest -from SciDataTool import DataTime, Data1D, DataLinspace, VectorField, Norm_ref +from SciDataTool import ( + DataTime, + Data1D, + DataLinspace, + VectorField, + Norm_ref, + DataPattern, +) import numpy as np from numpy.testing import assert_array_almost_equal @@ -171,5 +178,63 @@ def test_xyz_rphiz(): ) +@pytest.mark.validation +def test_xyz_sym(): + f = 50 + Time = DataLinspace( + name="time", + unit="s", + initial=0, + final=1 / f, + number=10, + include_endpoint=False, + symmetries={"period": 5}, + ) + Angle = DataLinspace( + name="angle", + unit="rad", + initial=0, + final=2 * np.pi / 4, + number=20, + include_endpoint=False, + symmetries={"antiperiod": 4}, + ) + Slice = DataPattern( + name="z", + unit="m", + values=[-0.5], + values_whole=[-0.5, 0.5], + rebuild_indices=[0, 0], + ) + field = np.zeros((10, 20, 1)) + at, ta = np.meshgrid( + Angle.get_values(is_smallestperiod=True), + Time.get_values(is_smallestperiod=True), + ) + field[:, :, 0] = 5 * np.cos(2 * np.pi * f * ta + 3 * at) + Field_r = DataTime( + name="Radial field", + symbol="X_r", + unit="m", + normalizations={"ref": Norm_ref(ref=2e-5)}, + axes=[Time, Angle, Slice], + values=field, + ) + VecField = VectorField( + name="Example field", + symbol="X", + components={"radial": Field_r}, + ) + + VecField_xyz = VecField.to_xyz() + assert VecField_xyz.components["comp_x"].values.shape == (10, 20, 1) + + VecField_rphiz = VecField_xyz.to_rphiz() + assert VecField_rphiz.compare(VecField) == [ + "len(selfcomponents)" + ] # Now contains tangential comp + assert VecField_rphiz.components["radial"].compare(VecField.components["radial"]) + + if __name__ == "__main__": - test_xyz_rphiz() + test_xyz_sym() From 7c552ddb82cede58cd745b21277f2b831ef9341e Mon Sep 17 00:00:00 2001 From: helene-t Date: Mon, 25 Oct 2021 11:34:18 +0200 Subject: [PATCH 03/10] [NF] new get_vectorfield_along method (calls get_data_along on each component) --- SciDataTool/Classes/Class_Dict.json | 1 + SciDataTool/Classes/VectorField.py | 17 +++++++ .../Generator/ClassesRef/VectorField.csv | 1 + SciDataTool/Methods/DataND/get_data_along.py | 4 +- .../VectorField/get_vectorfield_along.py | 26 ++++++++++ Tests/Validation/test_vectfield.py | 51 ++++++++++++++++++- 6 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 SciDataTool/Methods/VectorField/get_vectorfield_along.py diff --git a/SciDataTool/Classes/Class_Dict.json b/SciDataTool/Classes/Class_Dict.json index ac458466..494340a4 100644 --- a/SciDataTool/Classes/Class_Dict.json +++ b/SciDataTool/Classes/Class_Dict.json @@ -777,6 +777,7 @@ "get_mag_xyz_along", "get_rphiz_along", "get_xyz_along", + "get_vectorfield_along", "plot_2D_Data", "plot_2D_Data_Animated", "plot_3D_Data", diff --git a/SciDataTool/Classes/VectorField.py b/SciDataTool/Classes/VectorField.py index 307f39c6..49754e94 100644 --- a/SciDataTool/Classes/VectorField.py +++ b/SciDataTool/Classes/VectorField.py @@ -55,6 +55,11 @@ except ImportError as error: get_xyz_along = error +try: + from ..Methods.VectorField.get_vectorfield_along import get_vectorfield_along +except ImportError as error: + get_vectorfield_along = error + try: from ..Methods.VectorField.plot_2D_Data import plot_2D_Data except ImportError as error: @@ -186,6 +191,18 @@ class VectorField(FrozenClass): ) else: get_xyz_along = get_xyz_along + # cf Methods.VectorField.get_vectorfield_along + if isinstance(get_vectorfield_along, ImportError): + get_vectorfield_along = property( + fget=lambda x: raise_( + ImportError( + "Can't use VectorField method get_vectorfield_along: " + + str(get_vectorfield_along) + ) + ) + ) + else: + get_vectorfield_along = get_vectorfield_along # cf Methods.VectorField.plot_2D_Data if isinstance(plot_2D_Data, ImportError): plot_2D_Data = property( diff --git a/SciDataTool/Generator/ClassesRef/VectorField.csv b/SciDataTool/Generator/ClassesRef/VectorField.csv index 79450b31..f9a6a8b3 100644 --- a/SciDataTool/Generator/ClassesRef/VectorField.csv +++ b/SciDataTool/Generator/ClassesRef/VectorField.csv @@ -7,6 +7,7 @@ components,,Dict of the components,,{SciDataTool.Classes.DataND},-1,,,,,,get_har ,,,,,,,,,,,get_mag_xyz_along,,,, ,,,,,,,,,,,get_rphiz_along,,,, ,,,,,,,,,,,get_xyz_along,,,, +,,,,,,,,,,,get_vectorfield_along,,,, ,,,,,,,,,,,plot_2D_Data,,,, ,,,,,,,,,,,plot_2D_Data_Animated,,,, ,,,,,,,,,,,plot_3D_Data,,,, diff --git a/SciDataTool/Methods/DataND/get_data_along.py b/SciDataTool/Methods/DataND/get_data_along.py index f31e57c1..33603515 100644 --- a/SciDataTool/Methods/DataND/get_data_along.py +++ b/SciDataTool/Methods/DataND/get_data_along.py @@ -22,7 +22,9 @@ def get_data_along(self, *args, unit="SI", is_norm=False, axis_data=[]): a DataND object """ - results = self.get_along(*args, is_squeeze=False) + results = self.get_along( + *args, is_squeeze=False, unit=unit, is_norm=is_norm, axis_data=axis_data + ) values = results.pop(self.symbol) del results["axes_dict_other"] axes_list = results.pop("axes_list") diff --git a/SciDataTool/Methods/VectorField/get_vectorfield_along.py b/SciDataTool/Methods/VectorField/get_vectorfield_along.py new file mode 100644 index 00000000..c03e9370 --- /dev/null +++ b/SciDataTool/Methods/VectorField/get_vectorfield_along.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- + + +def get_vectorfield_along(self, *args, unit="SI", is_norm=False, axis_data=[]): + """Returns the sliced or interpolated version of the data, using conversions and symmetries if needed. + Parameters + ---------- + self : VectorField + a VectorField object + Returns + ------- + a VectorField object + """ + + # Dynamic import to avoid loop + module = __import__("SciDataTool.Classes.VectorField", fromlist=["VectorField"]) + VectorField = getattr(module, "VectorField") + + comp_dict = dict() + + for comp in self.components: # Call get_data_along on each component + comp_dict[comp] = self.components[comp].get_data_along( + *args, unit=unit, is_norm=is_norm, axis_data=axis_data + ) + + return VectorField(name=self.name, symbol=self.symbol, components=comp_dict) diff --git a/Tests/Validation/test_vectfield.py b/Tests/Validation/test_vectfield.py index 6e9ba716..445c1dd0 100644 --- a/Tests/Validation/test_vectfield.py +++ b/Tests/Validation/test_vectfield.py @@ -236,5 +236,54 @@ def test_xyz_sym(): assert VecField_rphiz.components["radial"].compare(VecField.components["radial"]) +@pytest.mark.validation +def test_get_vectorfield(): + f = 50 + Time = DataLinspace( + name="time", + unit="s", + initial=0, + final=1 / f, + number=10, + include_endpoint=False, + ) + Angle = DataLinspace( + name="angle", + unit="rad", + initial=0, + final=2 * np.pi, + number=20, + include_endpoint=False, + ) + ta, at = np.meshgrid(Time.get_values(), Angle.get_values()) + field = 5 * np.cos(2 * np.pi * f * ta + 3 * at) + Field_r = DataTime( + name="Radial field", + symbol="X_r", + unit="m", + normalizations={"ref": Norm_ref(ref=2e-5)}, + axes=[Time, Angle], + values=field.T, + ) + Field_t = DataTime( + name="Tangential field", + symbol="X_t", + unit="m", + normalizations={"ref": Norm_ref(ref=2e-5)}, + axes=[Time, Angle], + values=-field.T, + ) + VecField = VectorField( + name="Example field", + symbol="X", + components={"radial": Field_r, "tangential": Field_t}, + ) + VecField_sliced = VecField.get_vectorfield_along("time", "angle[0]") + assert_array_almost_equal( + VecField_sliced.components["radial"].values[:, 0], + VecField.components["radial"].values[:, 0], + ) + + if __name__ == "__main__": - test_xyz_sym() + test_get_vectorfield() From 4d792bdb722ef6fc04e8b7cf162b558558f9346a Mon Sep 17 00:00:00 2001 From: helene-t Date: Mon, 25 Oct 2021 12:29:21 +0200 Subject: [PATCH 04/10] [CC] "x-axis" in component name --- SciDataTool/Methods/VectorField/to_rphiz.py | 4 ++-- SciDataTool/Methods/VectorField/to_xyz.py | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/SciDataTool/Methods/VectorField/to_rphiz.py b/SciDataTool/Methods/VectorField/to_rphiz.py index 05e80250..8c181721 100644 --- a/SciDataTool/Methods/VectorField/to_rphiz.py +++ b/SciDataTool/Methods/VectorField/to_rphiz.py @@ -30,7 +30,7 @@ def to_rphiz(self): Comp_r = self.components["comp_x"].copy() Comp_r.name = "Radial " + self.components["comp_x"].name.lower().replace( - " along x", "" + " along x-axis", "" ) Comp_r.symbol = self.components["comp_x"].symbol.replace("_x", "_r") Comp_r.values = result["radial"] @@ -38,7 +38,7 @@ def to_rphiz(self): Comp_t = self.components["comp_x"].copy() Comp_t.name = "Tangential " + self.components["comp_x"].name.lower().replace( - " along x", "" + " along x-axis", "" ) Comp_t.symbol = self.components["comp_x"].symbol.replace("_r", "_y") Comp_t.values = result["tangential"] diff --git a/SciDataTool/Methods/VectorField/to_xyz.py b/SciDataTool/Methods/VectorField/to_xyz.py index e08b4b52..01869f26 100644 --- a/SciDataTool/Methods/VectorField/to_xyz.py +++ b/SciDataTool/Methods/VectorField/to_xyz.py @@ -30,7 +30,8 @@ def to_xyz(self): Comp_x = self.components["radial"].copy() Comp_x.name = ( - self.components["radial"].name.lower().replace("radial ", "") + " along x" + self.components["radial"].name.lower().replace("radial ", "") + + " along x-axis" ) Comp_x.symbol = self.components["radial"].symbol.replace("_r", "_x") Comp_x.values = result["comp_x"] @@ -38,7 +39,8 @@ def to_xyz(self): Comp_y = self.components["radial"].copy() Comp_y.name = ( - self.components["radial"].name.lower().replace("radial ", "") + " along y" + self.components["radial"].name.lower().replace("radial ", "") + + " along y-axis" ) Comp_y.symbol = self.components["radial"].symbol.replace("_r", "_y") Comp_y.values = result["comp_y"] From da84bafe9922bfe5582fab65e2335c427c951940 Mon Sep 17 00:00:00 2001 From: helene-t Date: Mon, 25 Oct 2021 15:59:26 +0200 Subject: [PATCH 05/10] [NF] new method to compute frequencies from speed + order axes --- SciDataTool/Classes/Class_Dict.json | 5 +- SciDataTool/Classes/DataND.py | 40 ++++++++++------ SciDataTool/Generator/ClassesRef/DataND.csv | 3 +- SciDataTool/Methods/DataND/get_freqs.py | 46 +++++++++++++++++++ .../Methods/DataND/get_magnitude_along.py | 26 ++++++++--- Tests/Validation/test_convert.py | 42 +++++++++++++++-- 6 files changed, 136 insertions(+), 26 deletions(-) create mode 100644 SciDataTool/Methods/DataND/get_freqs.py diff --git a/SciDataTool/Classes/Class_Dict.json b/SciDataTool/Classes/Class_Dict.json index 494340a4..e33527fe 100644 --- a/SciDataTool/Classes/Class_Dict.json +++ b/SciDataTool/Classes/Class_Dict.json @@ -248,6 +248,7 @@ "get_axes", "get_data_along", "get_field", + "get_freqs", "get_harmonics", "get_magnitude_along", "get_phase_along", @@ -258,8 +259,8 @@ "plot_3D_Data", "rebuild_symmetries", "set_Ftparameters", - "orthogonal_mp", - "summing" + "summing", + "orthogonal_mp" ], "mother": "Data", "name": "DataND", diff --git a/SciDataTool/Classes/DataND.py b/SciDataTool/Classes/DataND.py index 40afed19..3861a1cd 100644 --- a/SciDataTool/Classes/DataND.py +++ b/SciDataTool/Classes/DataND.py @@ -85,6 +85,11 @@ except ImportError as error: get_field = error +try: + from ..Methods.DataND.get_freqs import get_freqs +except ImportError as error: + get_freqs = error + try: from ..Methods.DataND.get_harmonics import get_harmonics except ImportError as error: @@ -136,14 +141,14 @@ set_Ftparameters = error try: - from ..Methods.DataND.orthogonal_mp import orthogonal_mp + from ..Methods.DataND.summing import summing except ImportError as error: - orthogonal_mp = error + summing = error try: - from ..Methods.DataND.summing import summing + from ..Methods.DataND.orthogonal_mp import orthogonal_mp except ImportError as error: - summing = error + orthogonal_mp = error from numpy import array, array_equal @@ -300,6 +305,15 @@ class DataND(Data): ) else: get_field = get_field + # cf Methods.DataND.get_freqs + if isinstance(get_freqs, ImportError): + get_freqs = property( + fget=lambda x: raise_( + ImportError("Can't use DataND method get_freqs: " + str(get_freqs)) + ) + ) + else: + get_freqs = get_freqs # cf Methods.DataND.get_harmonics if isinstance(get_harmonics, ImportError): get_harmonics = property( @@ -409,6 +423,15 @@ class DataND(Data): ) else: set_Ftparameters = set_Ftparameters + # cf Methods.DataND.summing + if isinstance(summing, ImportError): + summing = property( + fget=lambda x: raise_( + ImportError("Can't use DataND method summing: " + str(summing)) + ) + ) + else: + summing = summing # cf Methods.DataND.orthogonal_mp if isinstance(orthogonal_mp, ImportError): orthogonal_mp = property( @@ -420,15 +443,6 @@ class DataND(Data): ) else: orthogonal_mp = orthogonal_mp - # cf Methods.DataND.summing - if isinstance(summing, ImportError): - summing = property( - fget=lambda x: raise_( - ImportError("Can't use DataND method summing: " + str(summing)) - ) - ) - else: - summing = summing # save and copy methods are available in all object save = save copy = copy diff --git a/SciDataTool/Generator/ClassesRef/DataND.csv b/SciDataTool/Generator/ClassesRef/DataND.csv index e4c885ab..1310e4ac 100644 --- a/SciDataTool/Generator/ClassesRef/DataND.csv +++ b/SciDataTool/Generator/ClassesRef/DataND.csv @@ -13,6 +13,7 @@ is_real,,To indicate if the signal is real (use only positive frequencies),,bool ,,,,,,,,,,,get_axes,,,, ,,,,,,,,,,,get_data_along,,,, ,,,,,,,,,,,get_field,,,, +,,,,,,,,,,,get_freqs,,,, ,,,,,,,,,,,get_harmonics,,,, ,,,,,,,,,,,get_magnitude_along,,,, ,,,,,,,,,,,get_phase_along,,,, @@ -23,5 +24,5 @@ is_real,,To indicate if the signal is real (use only positive frequencies),,bool ,,,,,,,,,,,plot_3D_Data,,,, ,,,,,,,,,,,rebuild_symmetries,,,, ,,,,,,,,,,,set_Ftparameters,,,, -,,,,,,,,,,,orthogonal_mp,,,, ,,,,,,,,,,,summing,,,, +,,,,,,,,,,,orthogonal_mp,,,, diff --git a/SciDataTool/Methods/DataND/get_freqs.py b/SciDataTool/Methods/DataND/get_freqs.py new file mode 100644 index 00000000..5b93a1e1 --- /dev/null +++ b/SciDataTool/Methods/DataND/get_freqs.py @@ -0,0 +1,46 @@ +from numpy import meshgrid, array + +from SciDataTool.Functions import AxisError + + +def get_freqs(self): + """Returns the frequency vector or array, if available. + Parameters + ---------- + self: Data + a Data object + Returns + ------- + freqs : frequency vector or array + """ + + axis_names = [axis.name for axis in self.axes] + + index = None + operation = None + index_speed = None + index_order = None + + if "freqs" in axis_names: + index = axis_names.index("freqs") + elif "frequency" in axis_names: + index = axis_names.index("frequency") + elif "time" in axis_names: + index = axis_names.index("frequency") + operation = "time_to_freqs" + elif "speed" in axis_names and "order" in axis_names: + index_speed = axis_names.index("speed") + index_order = axis_names.index("order") + else: + raise AxisError("Cannot compute frequencies from available axes") + + if index is not None: + freqs = self.get_axes()[index].get_values(operation=operation) + else: # f = speed * 60 / order + speed = self.get_axes()[index_speed].get_values() + order_strings = self.get_axes()[index_order].get_values().tolist() + orders = array([int(s.split(" ")[0].replace("H", "")) for s in order_strings]) + os, so = meshgrid(orders, speed) + freqs = so * 60 / os + + return freqs diff --git a/SciDataTool/Methods/DataND/get_magnitude_along.py b/SciDataTool/Methods/DataND/get_magnitude_along.py index 2b1b405a..581690b6 100644 --- a/SciDataTool/Methods/DataND/get_magnitude_along.py +++ b/SciDataTool/Methods/DataND/get_magnitude_along.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- from SciDataTool.Functions import NormError, UnitError from SciDataTool.Functions.conversions import convert, to_dB, to_dBA, to_noct -from numpy import apply_along_axis, abs as np_abs +from numpy import apply_along_axis, apply_over_axes, abs as np_abs, meshgrid def get_magnitude_along( @@ -62,19 +62,33 @@ def get_magnitude_along( for axis in return_dict["axes_list"]: if axis.name == "freqs" or axis.corr_name == "freqs": index = axis.index - if return_dict["axes_list"][index].corr_values is not None and return_dict[ - "axes_list" - ][index].unit not in ["SI", return_dict["axes_list"][index].corr_unit]: - freqs = return_dict["axes_list"][index].corr_values else: - freqs = return_dict["freqs"] + if return_dict["axes_list"][ + index + ].corr_values is not None and return_dict["axes_list"][ + index + ].unit not in [ + "SI", + return_dict["axes_list"][index].corr_unit, + ]: + freqs = return_dict["axes_list"][index].corr_values + else: + freqs = return_dict["freqs"] values = apply_along_axis( to_dBA, index, values, freqs, self.unit, ref_value ) + elif "speed" in return_dict and "order" in return_dict: + freqs = self.get_freqs() + freqs = freqs.ravel("C") + shape = values.shape + values = values.reshape(freqs.shape + shape[2:]) + values = apply_along_axis(to_dBA, 0, values, freqs, self.unit, ref_value) + values = values.reshape(shape) else: raise UnitError( "ERROR: dBA conversion only available for fft with frequencies" ) + elif unit in self.normalizations: values = self.normalizations.get(unit).normalize(values) else: diff --git a/Tests/Validation/test_convert.py b/Tests/Validation/test_convert.py index 5eafa618..476286b3 100644 --- a/Tests/Validation/test_convert.py +++ b/Tests/Validation/test_convert.py @@ -252,12 +252,46 @@ def test_dba(): result_elec_order = Field.get_along("freqs->elec_order", unit="dBA") assert_array_almost_equal(result_Hz["X"], result_elec_order["X"]) - result_Hz = Field.get_magnitude_along("freqs", unit="dBA") - result_elec_order = Field.get_magnitude_along("freqs->elec_order", unit="dBA") - assert_array_almost_equal(result_Hz["X"], result_elec_order["X"]) + +@pytest.mark.validation +def test_dba_speed_order(): + Speed = DataLinspace( + name="speed", + unit="m/s", + initial=0, + final=10000, + number=11, + include_endpoint=True, + ) + Order = Data1D( + name="order", + unit="", + values=["H" + str(i) for i in range(20)], + ) + Loadcase = DataLinspace( + name="loadcases", + unit="", + initial=0, + final=5, + number=6, + include_endpoint=True, + ) + field = 600000 * np.ones((11, 20, 6)) + Field = DataFreq( + name="noise", + symbol="X", + unit="Pa", + values=field, + axes=[Speed, Order, Loadcase], + normalizations={"ref": Norm_ref(ref=2e-5)}, + ) + result = Field.get_magnitude_along("speed", "order", unit="dBA") + assert result["X"].shape == (11, 20) + result = Field.get_magnitude_along("speed", "order", "loadcases", unit="dBA") + assert result["X"].shape == (11, 20, 6) if __name__ == "__main__": - test_units_fft() + test_dba_speed_order() # test_norm() print("Done") \ No newline at end of file From 2f103b48cf9877c36a6528c6716a2e44ddb731dc Mon Sep 17 00:00:00 2001 From: EmileDvs <58598539+EmileDvs@users.noreply.github.com> Date: Mon, 25 Oct 2021 16:11:16 +0200 Subject: [PATCH 06/10] [BC] Don't squeeze values in case of integrate() --- SciDataTool/Functions/conversions.py | 4 ++-- SciDataTool/Functions/derivation_integration.py | 4 ++-- SciDataTool/Methods/VectorField/to_xyz.py | 1 + 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/SciDataTool/Functions/conversions.py b/SciDataTool/Functions/conversions.py index 8b8788f9..38becb78 100644 --- a/SciDataTool/Functions/conversions.py +++ b/SciDataTool/Functions/conversions.py @@ -102,10 +102,10 @@ def get_dim_prefix(unit_str): if prefix_str in unit_prefixes.keys(): prefix = (unit_prefixes.get(prefix_str) * key[1]) ** p else: - raise UnitError("ERROR: Prefix " + prefix_str + " unknown") + raise UnitError("Prefix " + prefix_str + " unknown") break if not dim: - raise UnitError("ERROR: Unit " + unit_str + " unknown") + raise UnitError("Unit " + unit_str + " unknown") return (dim, prefix) diff --git a/SciDataTool/Functions/derivation_integration.py b/SciDataTool/Functions/derivation_integration.py index df94e3e9..f25540a0 100644 --- a/SciDataTool/Functions/derivation_integration.py +++ b/SciDataTool/Functions/derivation_integration.py @@ -145,8 +145,8 @@ def integrate(values, ax_val, index, Nper, is_aper, is_phys, is_mean=False): values_full[-1, ...] = values[0, ...] # Integrate along axis values = Nper * np.trapz(values_full, x=ax_full, axis=0) - # Get N first values and swap axes back to origin - values = np.swapaxes(values, 0, index - 1) + # Readd first dim and swap axes back to origin + values = np.swapaxes(values[None, ...], 0, index) if is_mean: # Taking mean value by dividing by integration interval diff --git a/SciDataTool/Methods/VectorField/to_xyz.py b/SciDataTool/Methods/VectorField/to_xyz.py index 01869f26..3a7e794e 100644 --- a/SciDataTool/Methods/VectorField/to_xyz.py +++ b/SciDataTool/Methods/VectorField/to_xyz.py @@ -24,6 +24,7 @@ def to_xyz(self): arg_list = [ axis.name + "[smallestperiod]" for axis in self.components["radial"].axes ] + #TODO: arg_list = [axis.name for axis in self.components["radial"].axes] result = self.get_xyz_along(*arg_list, is_squeeze=False) # Store in new VectorField comp_dict = dict() From b5d146979797bbfcca86b1a8841a81e28e5aa920 Mon Sep 17 00:00:00 2001 From: helene-t Date: Mon, 25 Oct 2021 17:36:20 +0200 Subject: [PATCH 07/10] [BC] fix problems with fft axes --- SciDataTool/Methods/DataND/get_data_along.py | 5 ++++- SciDataTool/Methods/VectorField/get_rphiz_along.py | 11 +++++++---- SciDataTool/Methods/VectorField/get_xyz_along.py | 9 ++++++--- SciDataTool/Methods/VectorField/to_rphiz.py | 5 ++++- SciDataTool/Methods/VectorField/to_xyz.py | 7 +++++-- 5 files changed, 26 insertions(+), 11 deletions(-) diff --git a/SciDataTool/Methods/DataND/get_data_along.py b/SciDataTool/Methods/DataND/get_data_along.py index 33603515..4f2eadcb 100644 --- a/SciDataTool/Methods/DataND/get_data_along.py +++ b/SciDataTool/Methods/DataND/get_data_along.py @@ -66,7 +66,10 @@ def get_data_along(self, *args, unit="SI", is_norm=False, axis_data=[]): axis_values = results[axis_name] unit = rev_axes_dict[axis_name][2] # Update symmetries - if "smallestperiod" in args[index]: + if "smallestperiod" in args[index] or args[index] in [ + "freqs", + "wavenumber", + ]: symmetries = self.axes[index].symmetries else: symmetries = dict() diff --git a/SciDataTool/Methods/VectorField/get_rphiz_along.py b/SciDataTool/Methods/VectorField/get_rphiz_along.py index 9caa59a6..003092e2 100644 --- a/SciDataTool/Methods/VectorField/get_rphiz_along.py +++ b/SciDataTool/Methods/VectorField/get_rphiz_along.py @@ -31,13 +31,16 @@ def get_rphiz_along( args = args[0] # if called from another script with *args if "comp_x" in self.components.keys() and "comp_y" in self.components.keys(): - # Extract first along whole "angle" axis + # Extract first along whole or smallest period "angle" axis new_args = [arg for arg in args] string = [s for s in args if "angle" in s] if string != [] and "smallestperiod" not in string[0]: new_args[args.index(string[0])] = "angle" elif string == []: - new_args.extend(["angle"]) + if "wavenumber" in args: + new_args[args.index("wavenumber")] = "angle" + else: + new_args.extend(["angle"]) Datax = self.components["comp_x"].get_data_along( *new_args, unit=unit, @@ -53,14 +56,14 @@ def get_rphiz_along( field_x = Datax.values field_y = Datay.values shape = field_x.shape - if "smallestperiod" in string[0]: + if string != [] and "smallestperiod" in string[0]: phi = Datax.get_axes("angle")[0].get_values(is_smallestperiod=True) else: phi = Datax.get_axes("angle")[0].get_values() # Convert to cylindrical coordinates (field_r, field_t) = cart2pol(field_x, field_y, phi) # Extract second time with true args - if "angle" not in args: + if "angle" not in args and "angle[smallestperiod]" not in args: Datax.values = field_r Datay.values = field_t self.components["radial"] = Datax diff --git a/SciDataTool/Methods/VectorField/get_xyz_along.py b/SciDataTool/Methods/VectorField/get_xyz_along.py index bc1aa87f..8934ec31 100644 --- a/SciDataTool/Methods/VectorField/get_xyz_along.py +++ b/SciDataTool/Methods/VectorField/get_xyz_along.py @@ -35,7 +35,10 @@ def get_xyz_along(self, *args, unit="SI", is_norm=False, axis_data=[], is_squeez if string != [] and "smallestperiod" not in string[0]: new_args[args.index(string[0])] = "angle" elif string == []: - new_args.extend(["angle"]) + if "wavenumber" in args: + new_args[args.index("wavenumber")] = "angle" + else: + new_args.extend(["angle"]) Datar = self.components["radial"].get_data_along( *new_args, unit=unit, @@ -61,14 +64,14 @@ def get_xyz_along(self, *args, unit="SI", is_norm=False, axis_data=[], is_squeez axes=Datar.axes, values=field_c, ) - if "smallestperiod" in string[0]: + if string != [] and "smallestperiod" in string[0]: phi = Datar.get_axes("angle")[0].get_values(is_smallestperiod=True) else: phi = Datar.get_axes("angle")[0].get_values() # Convert to cylindrical coordinates (field_x, field_y) = pol2cart(field_r, field_c, phi) # Extract second time with true args - if "angle" not in args and "angle[smallestperiod]": + if "angle" not in args and "angle[smallestperiod]" not in args: Datar.values = field_x Dataphi.values = field_y self.components["comp_x"] = Datar diff --git a/SciDataTool/Methods/VectorField/to_rphiz.py b/SciDataTool/Methods/VectorField/to_rphiz.py index 8c181721..664f0605 100644 --- a/SciDataTool/Methods/VectorField/to_rphiz.py +++ b/SciDataTool/Methods/VectorField/to_rphiz.py @@ -22,7 +22,10 @@ def to_rphiz(self): else: # Coordinate transform arg_list = [ - axis.name + "[smallestperiod]" for axis in self.components["comp_x"].axes + axis.name + if axis.name in ["freqs", "wavenumber"] + else axis.name + "[smallestperiod]" + for axis in self.components["radial"].axes ] result = self.get_rphiz_along(*arg_list) # Store in new VectorField diff --git a/SciDataTool/Methods/VectorField/to_xyz.py b/SciDataTool/Methods/VectorField/to_xyz.py index 3a7e794e..7aac068e 100644 --- a/SciDataTool/Methods/VectorField/to_xyz.py +++ b/SciDataTool/Methods/VectorField/to_xyz.py @@ -22,9 +22,12 @@ def to_xyz(self): else: # Coordinate transform arg_list = [ - axis.name + "[smallestperiod]" for axis in self.components["radial"].axes + axis.name + if axis.name in ["freqs", "wavenumber"] + else axis.name + "[smallestperiod]" + for axis in self.components["radial"].axes ] - #TODO: arg_list = [axis.name for axis in self.components["radial"].axes] + # TODO: arg_list = [axis.name for axis in self.components["radial"].axes] result = self.get_xyz_along(*arg_list, is_squeeze=False) # Store in new VectorField comp_dict = dict() From e88288e77731d0cbbcc60153a0dea31e90f380ed Mon Sep 17 00:00:00 2001 From: helene-t Date: Tue, 26 Oct 2021 09:49:46 +0200 Subject: [PATCH 08/10] [CC] code cleaning --- SciDataTool/Classes/_check.py | 2 - SciDataTool/Classes/_frozen.py | 1 - SciDataTool/Functions/Plot/init_fig.py | 1 - SciDataTool/Functions/Plot/plot_2D.py | 7 - SciDataTool/Functions/Plot/plot_3D.py | 2 - SciDataTool/Functions/Plot/plot_4D.py | 2 - SciDataTool/Functions/__init__.py | 1 - SciDataTool/Functions/conversions.py | 5 +- SciDataTool/Functions/fft_functions.py | 152 ++++++++---------- SciDataTool/Functions/interpolations.py | 3 - SciDataTool/Functions/load.py | 11 +- SciDataTool/Functions/parser.py | 2 +- SciDataTool/Functions/save.py | 1 - SciDataTool/Functions/symmetries.py | 9 +- SciDataTool/Methods/Data1D/get_length.py | 3 +- SciDataTool/Methods/Data1D/get_periodicity.py | 3 - SciDataTool/Methods/Data1D/get_values.py | 2 +- SciDataTool/Methods/Data1D/has_period.py | 3 - SciDataTool/Methods/DataFreq/freq_to_time.py | 3 +- .../Methods/DataLinspace/get_length.py | 3 +- .../Methods/DataLinspace/get_periodicity.py | 3 - .../Methods/DataLinspace/get_values.py | 2 +- .../Methods/DataLinspace/has_period.py | 3 - SciDataTool/Methods/DataND/_set_values.py | 1 - SciDataTool/Methods/DataND/comp_axes.py | 5 +- SciDataTool/Methods/DataND/compare_along.py | 2 - .../Methods/DataND/compare_magnitude_along.py | 2 - .../Methods/DataND/compare_phase_along.py | 2 - SciDataTool/Methods/DataND/export_along.py | 7 +- SciDataTool/Methods/DataND/extract_slices.py | 1 - .../Methods/DataND/extract_slices_fft.py | 1 - SciDataTool/Methods/DataND/get_along.py | 1 - SciDataTool/Methods/DataND/get_axes.py | 3 - SciDataTool/Methods/DataND/get_field.py | 11 +- SciDataTool/Methods/DataND/get_harmonics.py | 1 - .../Methods/DataND/get_magnitude_along.py | 11 +- SciDataTool/Methods/DataND/get_phase_along.py | 5 +- SciDataTool/Methods/DataND/has_period.py | 3 - SciDataTool/Methods/DataND/interpolate.py | 2 - SciDataTool/Methods/DataND/orthogonal_mp.py | 6 - SciDataTool/Methods/DataND/plot_2D_Data.py | 3 - SciDataTool/Methods/DataND/plot_3D_Data.py | 2 - .../Methods/DataND/rebuild_symmetries.py | 1 - SciDataTool/Methods/DataPattern/get_length.py | 4 - SciDataTool/Methods/DataPattern/has_period.py | 3 - SciDataTool/Methods/DataTime/time_to_freq.py | 3 +- .../Methods/VectorField/freq_to_time.py | 3 - SciDataTool/Methods/VectorField/get_axes.py | 3 - .../VectorField/get_harm_rphiz_along.py | 4 +- .../VectorField/get_mag_rphiz_along.py | 4 +- .../Methods/VectorField/get_mag_xyz_along.py | 2 - .../Methods/VectorField/get_rphiz_along.py | 2 - .../VectorField/get_vectorfield_along.py | 3 - .../Methods/VectorField/get_xyz_along.py | 4 +- .../Methods/VectorField/time_to_freq.py | 3 - SciDataTool/Methods/VectorField/to_rphiz.py | 3 - SciDataTool/Methods/VectorField/to_xyz.py | 4 - SciDataTool/__init__.py | 1 - Tests/Validation/test_omp.py | 76 ++++----- Tests/__init__.py | 3 +- Tests/find.py | 2 - 61 files changed, 130 insertions(+), 291 deletions(-) diff --git a/SciDataTool/Classes/_check.py b/SciDataTool/Classes/_check.py index 78cc19e5..bb31461a 100644 --- a/SciDataTool/Classes/_check.py +++ b/SciDataTool/Classes/_check.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - from numpy import array, empty, int32, squeeze, expand_dims diff --git a/SciDataTool/Classes/_frozen.py b/SciDataTool/Classes/_frozen.py index 87037a22..3403bcba 100644 --- a/SciDataTool/Classes/_frozen.py +++ b/SciDataTool/Classes/_frozen.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from numpy import array_equal, ndarray diff --git a/SciDataTool/Functions/Plot/init_fig.py b/SciDataTool/Functions/Plot/init_fig.py index 049e318b..6fdd92ca 100644 --- a/SciDataTool/Functions/Plot/init_fig.py +++ b/SciDataTool/Functions/Plot/init_fig.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from matplotlib.pyplot import subplots import io import pickle diff --git a/SciDataTool/Functions/Plot/plot_2D.py b/SciDataTool/Functions/Plot/plot_2D.py index 506c940e..9d333de7 100644 --- a/SciDataTool/Functions/Plot/plot_2D.py +++ b/SciDataTool/Functions/Plot/plot_2D.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- - -from itertools import repeat - import matplotlib.pyplot as plt from numpy import ( @@ -9,13 +5,10 @@ argmin, abs, arange, - squeeze, - split, ndarray, cumsum, zeros, shape, - max as np_max, ) from SciDataTool.Functions.Plot.init_fig import init_fig diff --git a/SciDataTool/Functions/Plot/plot_3D.py b/SciDataTool/Functions/Plot/plot_3D.py index d46fb771..b0b4d9e1 100644 --- a/SciDataTool/Functions/Plot/plot_3D.py +++ b/SciDataTool/Functions/Plot/plot_3D.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - from numpy import min as np_min, max as np_max, abs as np_abs, log10 import matplotlib.pyplot as plt diff --git a/SciDataTool/Functions/Plot/plot_4D.py b/SciDataTool/Functions/Plot/plot_4D.py index a96f0d1e..34a87bf5 100644 --- a/SciDataTool/Functions/Plot/plot_4D.py +++ b/SciDataTool/Functions/Plot/plot_4D.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - from numpy import log10, abs as np_abs, max as np_max, NaN, zeros_like import matplotlib.pyplot as plt diff --git a/SciDataTool/Functions/__init__.py b/SciDataTool/Functions/__init__.py index 86a17ce3..17346d89 100644 --- a/SciDataTool/Functions/__init__.py +++ b/SciDataTool/Functions/__init__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- class FTError(Exception): """Raised when there is an error within the Fourier Transform methods (Data Object)""" diff --git a/SciDataTool/Functions/conversions.py b/SciDataTool/Functions/conversions.py index 38becb78..6de552dd 100644 --- a/SciDataTool/Functions/conversions.py +++ b/SciDataTool/Functions/conversions.py @@ -248,7 +248,7 @@ def convert(values, unit1, unit2): dim2 = [i - j for i, j in zip(dim2_num, dim2_denom)] if dim1 != dim2: raise UnitError( - "ERROR: Units " + unit1_save + " and " + unit2_save + " do not match" + "Units " + unit1_save + " and " + unit2_save + " do not match" ) else: return ( @@ -407,11 +407,8 @@ def dB_to_dBA(values, freqs, noct=None): ) Aweight = 2.0 + 20.0 * log10(RA) Aweight[isnan(Aweight)] = -100 # replacing NaN by -100 dB - try: values += Aweight return values - except Exception: - raise UnitError("ERROR: dBA conversion only available for 1D fft") def to_noct(values, freqs, noct=3): diff --git a/SciDataTool/Functions/fft_functions.py b/SciDataTool/Functions/fft_functions.py index 0b4706a1..016dc114 100644 --- a/SciDataTool/Functions/fft_functions.py +++ b/SciDataTool/Functions/fft_functions.py @@ -1,33 +1,21 @@ -# -*- coding: utf-8 -*- -from numpy import mean, hanning, linspace, where, isclose, apply_along_axis, around +from numpy import linspace, where, isclose, around from numpy.fft import ( - fft, fftshift, - ifft, ifftshift, rfftn, irfftn, fftn, ifftn, - rfftfreq, ) from numpy import ( array, pi, - zeros, exp, ceil, - iscomplex, - concatenate, - conjugate, - flip, isin, - append, take, insert, delete, - abs as np_abs, - angle as np_angle, allclose, real, ) @@ -455,80 +443,80 @@ def comp_ifftn(values, axes_list, is_real=True): return values_IFT -def comp_magnitude(values): - """Computes the magnitude of the Fourier Transform - Parameters - ---------- - values: ndarray - ndarray of the field - Returns - ------- - Magnitude of the Fourier Transform - """ - return np_abs(_comp_fft(values)) +# def comp_magnitude(values): +# """Computes the magnitude of the Fourier Transform +# Parameters +# ---------- +# values: ndarray +# ndarray of the field +# Returns +# ------- +# Magnitude of the Fourier Transform +# """ +# return np_abs(_comp_fft(values)) # return np_abs(comp_stft_average(values)) -def comp_phase(values): - """Computes the phase of the Fourier Transform - Parameters - ---------- - values: ndarray - ndarray of the field - Returns - ------- - Phase of the Fourier Transform - """ - return np_angle(_comp_fft(values)) - - -def comp_stft_average(values): - """Computes the average of the Short-Time Fourier Transform - Parameters - ---------- - values: ndarray - ndarray of the field - Returns - ------- - Average of the Short-Time Fourier Transform - """ - # To do - nperseg = 3200 - noverlap = int(nperseg * 0.75) - f, t, Zxx = stft( - values, fs=48000, window="hann", nperseg=nperseg, noverlap=noverlap - ) - window_size = nperseg / len(values) - values = mean(Zxx, axis=1) / (0.5) - # values = 2.0 * mean(Zxx, axis=1) - print(values.shape) - return f, np_abs(values) +# def comp_phase(values): +# """Computes the phase of the Fourier Transform +# Parameters +# ---------- +# values: ndarray +# ndarray of the field +# Returns +# ------- +# Phase of the Fourier Transform +# """ +# return np_angle(_comp_fft(values)) -def comp_fft_average(values): - """Computes the average of the Short-Time Fourier Transform - Parameters - ---------- - values: ndarray - ndarray of the field - Returns - ------- - Average of the Short-Time Fourier Transform - """ - # To do - nperseg = 3200 - noverlap = int(nperseg * 0.75) - step = nperseg - noverlap - N_tot = len(values) - nwindows = int(N_tot / (2.0 * step)) - values_fft = zeros(nperseg, dtype="complex128") - for i in range(nwindows): - values_fft += _comp_fft( - values[i * step : nperseg + i * step] * hanning(nperseg) - ) - values = values_fft[int(nperseg / 2) :] / nwindows - f = linspace(0, int(N_tot / 2), int(nperseg / 2)) - return f, np_abs(values) +# def comp_stft_average(values): +# """Computes the average of the Short-Time Fourier Transform +# Parameters +# ---------- +# values: ndarray +# ndarray of the field +# Returns +# ------- +# Average of the Short-Time Fourier Transform +# """ +# # To do +# nperseg = 3200 +# noverlap = int(nperseg * 0.75) +# f, t, Zxx = stft( +# values, fs=48000, window="hann", nperseg=nperseg, noverlap=noverlap +# ) +# window_size = nperseg / len(values) +# values = mean(Zxx, axis=1) / (0.5) +# # values = 2.0 * mean(Zxx, axis=1) +# print(values.shape) +# return f, np_abs(values) + + +# def comp_fft_average(values): +# """Computes the average of the Short-Time Fourier Transform +# Parameters +# ---------- +# values: ndarray +# ndarray of the field +# Returns +# ------- +# Average of the Short-Time Fourier Transform +# """ +# # To do +# nperseg = 3200 +# noverlap = int(nperseg * 0.75) +# step = nperseg - noverlap +# N_tot = len(values) +# nwindows = int(N_tot / (2.0 * step)) +# values_fft = zeros(nperseg, dtype="complex128") +# for i in range(nwindows): +# values_fft += _comp_fft( +# values[i * step : nperseg + i * step] * hanning(nperseg) +# ) +# values = values_fft[int(nperseg / 2) :] / nwindows +# f = linspace(0, int(N_tot / 2), int(nperseg / 2)) +# return f, np_abs(values) def rect_window(f, M, dt): diff --git a/SciDataTool/Functions/interpolations.py b/SciDataTool/Functions/interpolations.py index 4488b1f6..2b78fbac 100644 --- a/SciDataTool/Functions/interpolations.py +++ b/SciDataTool/Functions/interpolations.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from numpy import ( array, linspace, @@ -6,10 +5,8 @@ take, isclose, isin, - around, all, abs as np_abs, - interp, where, zeros, ) diff --git a/SciDataTool/Functions/load.py b/SciDataTool/Functions/load.py index c86d6fbd..d79d61a5 100644 --- a/SciDataTool/Functions/load.py +++ b/SciDataTool/Functions/load.py @@ -1,12 +1,9 @@ -# -*- coding: utf-8 -*- - from os.path import isdir, join from os import walk, getcwd, chdir -from .Load.load_json import load_json -from .Load.load_hdf5 import load_hdf5 -from .Load.load_pkl import load_pkl -from .Load.import_class import import_class -from ..Classes._check import InitUnKnowClassError +from SciDataTool.Functions.Load.load_json import load_json +from SciDataTool.Functions.Load.load_hdf5 import load_hdf5 +from SciDataTool.Functions.Load.load_pkl import load_pkl +from SciDataTool.Functions.Load.import_class import import_class def init_data(obj, file_path): diff --git a/SciDataTool/Functions/parser.py b/SciDataTool/Functions/parser.py index 9391a474..ad8015cf 100644 --- a/SciDataTool/Functions/parser.py +++ b/SciDataTool/Functions/parser.py @@ -102,7 +102,7 @@ def read_input_strings(args, axis_data): try: input_data = axis_data[name] except Exception: - raise AxisError("ERROR: No axis_data provided") + raise AxisError("No axis_data provided") # Detect above elif ">" in axis_str: elems = axis_str.split(">") diff --git a/SciDataTool/Functions/save.py b/SciDataTool/Functions/save.py index 416c1690..46036b0f 100644 --- a/SciDataTool/Functions/save.py +++ b/SciDataTool/Functions/save.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from json import dump from os.path import join, basename, isdir diff --git a/SciDataTool/Functions/symmetries.py b/SciDataTool/Functions/symmetries.py index 3bdb8152..068be3a9 100644 --- a/SciDataTool/Functions/symmetries.py +++ b/SciDataTool/Functions/symmetries.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from SciDataTool.Functions import AxisError from numpy import tile, concatenate, negative, ones, append @@ -50,9 +49,7 @@ def rebuild_symmetries_axis(values, symmetries): values_new, values_new[-1] + symmetries["delta"] ) else: - raise AxisError( - "ERROR: must provide delta for symmetries with one sample" - ) + raise AxisError("must provide delta for symmetries with one sample") else: values_new = concatenate( ( @@ -68,9 +65,7 @@ def rebuild_symmetries_axis(values, symmetries): values_new, values_new[-1] + symmetries["delta"] ) else: - raise AxisError( - "ERROR: must provide delta for symmetries with one sample" - ) + raise AxisError("must provide delta for symmetries with one sample") else: values_new = concatenate( ( diff --git a/SciDataTool/Methods/Data1D/get_length.py b/SciDataTool/Methods/Data1D/get_length.py index c5cf9d71..549d243e 100644 --- a/SciDataTool/Methods/Data1D/get_length.py +++ b/SciDataTool/Methods/Data1D/get_length.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from SciDataTool.Functions import AxisError @@ -24,7 +23,7 @@ def get_length(self, is_oneperiod=False, is_antiperiod=False, is_smallestperiod= if "antiperiod" in self.symmetries: return N else: - raise AxisError("ERROR: axis has no antiperiodicity") + raise AxisError("axis has no antiperiodicity") elif is_oneperiod: if "antiperiod" in self.symmetries: return N * 2 diff --git a/SciDataTool/Methods/Data1D/get_periodicity.py b/SciDataTool/Methods/Data1D/get_periodicity.py index db2c5421..db9b0f0a 100644 --- a/SciDataTool/Methods/Data1D/get_periodicity.py +++ b/SciDataTool/Methods/Data1D/get_periodicity.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- - - def get_periodicity(self): """Gives periodicity of the axis. Parameters diff --git a/SciDataTool/Methods/Data1D/get_values.py b/SciDataTool/Methods/Data1D/get_values.py index c01c707f..6404eb5d 100644 --- a/SciDataTool/Methods/Data1D/get_values.py +++ b/SciDataTool/Methods/Data1D/get_values.py @@ -62,7 +62,7 @@ def get_values( if "antiperiod" in self.symmetries: pass else: - raise AxisError("ERROR: axis has no antiperiodicity") + raise AxisError("axis has no antiperiodicity") elif is_oneperiod: if "antiperiod" in self.symmetries: nper = self.symmetries["antiperiod"] diff --git a/SciDataTool/Methods/Data1D/has_period.py b/SciDataTool/Methods/Data1D/has_period.py index 11b88c0c..d1fcc0b4 100644 --- a/SciDataTool/Methods/Data1D/has_period.py +++ b/SciDataTool/Methods/Data1D/has_period.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- - - def has_period(self): """Indicates if an axis has symmetries. Parameters diff --git a/SciDataTool/Methods/DataFreq/freq_to_time.py b/SciDataTool/Methods/DataFreq/freq_to_time.py index 5eed4bcf..e344cb63 100644 --- a/SciDataTool/Methods/DataFreq/freq_to_time.py +++ b/SciDataTool/Methods/DataFreq/freq_to_time.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from SciDataTool import Data1D from SciDataTool.Functions import AxisError @@ -35,7 +34,7 @@ def freq_to_time(self): axes_str.append(axis_str) if axes_str == [axis.name for axis in self.axes]: raise AxisError( - "ERROR: No available axis is compatible with fft (should be time or angle)" + "No available axis is compatible with fft (should be time or angle)" ) else: results = self.get_along(*axes_str) diff --git a/SciDataTool/Methods/DataLinspace/get_length.py b/SciDataTool/Methods/DataLinspace/get_length.py index cdbfc8bc..7c59dfd1 100644 --- a/SciDataTool/Methods/DataLinspace/get_length.py +++ b/SciDataTool/Methods/DataLinspace/get_length.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from SciDataTool.Functions import AxisError @@ -29,7 +28,7 @@ def get_length(self, is_oneperiod=False, is_antiperiod=False, is_smallestperiod= if "antiperiod" in self.symmetries: return N else: - raise AxisError("ERROR: axis has no antiperiodicity") + raise AxisError("axis has no antiperiodicity") elif is_oneperiod: if "antiperiod" in self.symmetries: return N * 2 diff --git a/SciDataTool/Methods/DataLinspace/get_periodicity.py b/SciDataTool/Methods/DataLinspace/get_periodicity.py index 89e4a91e..6e910493 100644 --- a/SciDataTool/Methods/DataLinspace/get_periodicity.py +++ b/SciDataTool/Methods/DataLinspace/get_periodicity.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- - - def get_periodicity(self): """Gives periodicity of the axis. Parameters diff --git a/SciDataTool/Methods/DataLinspace/get_values.py b/SciDataTool/Methods/DataLinspace/get_values.py index f5ccedc3..b4f96cb9 100644 --- a/SciDataTool/Methods/DataLinspace/get_values.py +++ b/SciDataTool/Methods/DataLinspace/get_values.py @@ -73,7 +73,7 @@ def get_values( if "antiperiod" in self.symmetries: pass else: - raise AxisError("ERROR: axis has no antiperiodicity") + raise AxisError("axis has no antiperiodicity") elif is_oneperiod: if "antiperiod" in self.symmetries: nper = self.symmetries["antiperiod"] diff --git a/SciDataTool/Methods/DataLinspace/has_period.py b/SciDataTool/Methods/DataLinspace/has_period.py index b3953b22..04bc2245 100644 --- a/SciDataTool/Methods/DataLinspace/has_period.py +++ b/SciDataTool/Methods/DataLinspace/has_period.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- - - def has_period(self): """Indicates if an axis has symmetries. Parameters diff --git a/SciDataTool/Methods/DataND/_set_values.py b/SciDataTool/Methods/DataND/_set_values.py index c69e9f4e..d6ce2211 100644 --- a/SciDataTool/Methods/DataND/_set_values.py +++ b/SciDataTool/Methods/DataND/_set_values.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from SciDataTool.Classes._check import check_dimensions, check_var from numpy import squeeze, array diff --git a/SciDataTool/Methods/DataND/comp_axes.py b/SciDataTool/Methods/DataND/comp_axes.py index 8f5b9e07..148f6012 100644 --- a/SciDataTool/Methods/DataND/comp_axes.py +++ b/SciDataTool/Methods/DataND/comp_axes.py @@ -1,7 +1,4 @@ -# -*- coding: utf-8 -*- -from SciDataTool.Functions import AxisError, axes_dict, rev_axes_dict - -from numpy import inf +from SciDataTool.Functions import axes_dict, rev_axes_dict def comp_axes(self, axes_list): diff --git a/SciDataTool/Methods/DataND/compare_along.py b/SciDataTool/Methods/DataND/compare_along.py index 64e74837..18cd2dde 100644 --- a/SciDataTool/Methods/DataND/compare_along.py +++ b/SciDataTool/Methods/DataND/compare_along.py @@ -1,6 +1,4 @@ -# -*- coding: utf-8 -*- from SciDataTool.Functions.interpolations import get_common_base, get_interpolation -from numpy import squeeze, array def compare_along(self, *args, data_list=[], unit="SI", is_norm=False): diff --git a/SciDataTool/Methods/DataND/compare_magnitude_along.py b/SciDataTool/Methods/DataND/compare_magnitude_along.py index 96f149ad..4fd0ea91 100644 --- a/SciDataTool/Methods/DataND/compare_magnitude_along.py +++ b/SciDataTool/Methods/DataND/compare_magnitude_along.py @@ -1,6 +1,4 @@ -# -*- coding: utf-8 -*- from SciDataTool.Functions.interpolations import get_common_base, get_interpolation -from numpy import squeeze def compare_magnitude_along(self, *args, unit="SI", data_list=[], is_norm=False): diff --git a/SciDataTool/Methods/DataND/compare_phase_along.py b/SciDataTool/Methods/DataND/compare_phase_along.py index 54d4a6bd..bf45c0d9 100644 --- a/SciDataTool/Methods/DataND/compare_phase_along.py +++ b/SciDataTool/Methods/DataND/compare_phase_along.py @@ -1,6 +1,4 @@ -# -*- coding: utf-8 -*- from SciDataTool.Functions.interpolations import get_common_base, get_interpolation -from numpy import squeeze def compare_phase_along(self, *args, unit="SI", data_list=[], is_norm=False): diff --git a/SciDataTool/Methods/DataND/export_along.py b/SciDataTool/Methods/DataND/export_along.py index 8dcdd8ab..b3f87cbb 100644 --- a/SciDataTool/Methods/DataND/export_along.py +++ b/SciDataTool/Methods/DataND/export_along.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from SciDataTool.Functions.Plot import unit_dict import csv @@ -91,12 +90,12 @@ def export_along( nfiles = len(axes_list_new[2].values) elif len(axes_list_new) == 3: raise Exception( - "ERROR: cannot export more than 2 dimensions in single csv file. Activate is_multiple_files to write in multiple csv files." + "cannot export more than 2 dimensions in single csv file. Activate is_multiple_files to write in multiple csv files." ) elif len(axes_list_new) < 3: nfiles = 1 else: - raise Exception("ERROR: cannot export more than 3 dimensions in csv file") + raise Exception("cannot export more than 3 dimensions in csv file") for i in range(nfiles): if nfiles > 1: @@ -161,4 +160,4 @@ def export_along( csvWriter.writerows(matrix) else: - raise Exception("ERROR: export format not supported") + raise Exception("export format not supported") diff --git a/SciDataTool/Methods/DataND/extract_slices.py b/SciDataTool/Methods/DataND/extract_slices.py index 151155c2..44fccece 100644 --- a/SciDataTool/Methods/DataND/extract_slices.py +++ b/SciDataTool/Methods/DataND/extract_slices.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from numpy import take diff --git a/SciDataTool/Methods/DataND/extract_slices_fft.py b/SciDataTool/Methods/DataND/extract_slices_fft.py index a396e0b3..e2662ed1 100644 --- a/SciDataTool/Methods/DataND/extract_slices_fft.py +++ b/SciDataTool/Methods/DataND/extract_slices_fft.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from numpy import take diff --git a/SciDataTool/Methods/DataND/get_along.py b/SciDataTool/Methods/DataND/get_along.py index 448231cf..328ba358 100644 --- a/SciDataTool/Methods/DataND/get_along.py +++ b/SciDataTool/Methods/DataND/get_along.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from SciDataTool.Functions.parser import read_input_strings from SciDataTool.Functions.fft_functions import comp_fftn, comp_ifftn diff --git a/SciDataTool/Methods/DataND/get_axes.py b/SciDataTool/Methods/DataND/get_axes.py index 4e4cb97d..6b4362cd 100644 --- a/SciDataTool/Methods/DataND/get_axes.py +++ b/SciDataTool/Methods/DataND/get_axes.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- - - def get_axes(self, *args): """Returns the list of axes. Parameters diff --git a/SciDataTool/Methods/DataND/get_field.py b/SciDataTool/Methods/DataND/get_field.py index b63e3800..2f3a6f37 100644 --- a/SciDataTool/Methods/DataND/get_field.py +++ b/SciDataTool/Methods/DataND/get_field.py @@ -1,13 +1,4 @@ -# -*- coding: utf-8 -*- -from numpy import ( - sum as np_sum, - mean as np_mean, - sqrt, - trapz, - take, - min as np_min, - max as np_max, -) +from numpy import take from SciDataTool.Functions.symmetries import rebuild_symmetries diff --git a/SciDataTool/Methods/DataND/get_harmonics.py b/SciDataTool/Methods/DataND/get_harmonics.py index e7f0d57e..dbf42cf5 100644 --- a/SciDataTool/Methods/DataND/get_harmonics.py +++ b/SciDataTool/Methods/DataND/get_harmonics.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from numpy import ( argsort, negative, diff --git a/SciDataTool/Methods/DataND/get_magnitude_along.py b/SciDataTool/Methods/DataND/get_magnitude_along.py index 581690b6..c6b50768 100644 --- a/SciDataTool/Methods/DataND/get_magnitude_along.py +++ b/SciDataTool/Methods/DataND/get_magnitude_along.py @@ -1,7 +1,6 @@ -# -*- coding: utf-8 -*- from SciDataTool.Functions import NormError, UnitError from SciDataTool.Functions.conversions import convert, to_dB, to_dBA, to_noct -from numpy import apply_along_axis, apply_over_axes, abs as np_abs, meshgrid +from numpy import apply_along_axis def get_magnitude_along( @@ -46,9 +45,7 @@ def get_magnitude_along( try: values = self.normalizations["ref"].normalize(values) except: - raise NormError( - "ERROR: Reference value not specified for normalization" - ) + raise NormError("Reference value not specified for normalization") elif unit == "dB": ref_value = 1.0 if "ref" in self.normalizations: @@ -85,9 +82,7 @@ def get_magnitude_along( values = apply_along_axis(to_dBA, 0, values, freqs, self.unit, ref_value) values = values.reshape(shape) else: - raise UnitError( - "ERROR: dBA conversion only available for fft with frequencies" - ) + raise UnitError("dBA conversion only available for fft with frequencies") elif unit in self.normalizations: values = self.normalizations.get(unit).normalize(values) diff --git a/SciDataTool/Methods/DataND/get_phase_along.py b/SciDataTool/Methods/DataND/get_phase_along.py index 1f33be46..732b0f22 100644 --- a/SciDataTool/Methods/DataND/get_phase_along.py +++ b/SciDataTool/Methods/DataND/get_phase_along.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from SciDataTool.Functions import NormError from SciDataTool.Functions.conversions import convert from numpy import angle as np_angle @@ -34,9 +33,7 @@ def get_phase_along(self, *args, unit="SI", is_norm=False, axis_data=[]): try: values = self.normalizations["ref"].normalize(values) except: - raise NormError( - "ERROR: Reference value not specified for normalization" - ) + raise NormError("Reference value not specified for normalization") elif unit == "°": values = convert(values, "rad", "°") elif unit in self.normalizations: diff --git a/SciDataTool/Methods/DataND/has_period.py b/SciDataTool/Methods/DataND/has_period.py index 4dc91da4..e4e5b190 100644 --- a/SciDataTool/Methods/DataND/has_period.py +++ b/SciDataTool/Methods/DataND/has_period.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- - - def has_period(self): """Indicates if a field has symmetries. Parameters diff --git a/SciDataTool/Methods/DataND/interpolate.py b/SciDataTool/Methods/DataND/interpolate.py index 4c1a9906..046ffcde 100644 --- a/SciDataTool/Methods/DataND/interpolate.py +++ b/SciDataTool/Methods/DataND/interpolate.py @@ -1,9 +1,7 @@ -# -*- coding: utf-8 -*- from SciDataTool.Functions.interpolations import ( get_interpolation, get_interpolation_step, ) -from numpy import apply_along_axis, squeeze def interpolate(self, values, axes_list): diff --git a/SciDataTool/Methods/DataND/orthogonal_mp.py b/SciDataTool/Methods/DataND/orthogonal_mp.py index e6f5bb0c..ab82eda4 100644 --- a/SciDataTool/Methods/DataND/orthogonal_mp.py +++ b/SciDataTool/Methods/DataND/orthogonal_mp.py @@ -1,9 +1,3 @@ -# -*- coding: utf-8 -*- - -import numpy as np - -from typing import List -from numpy import ndarray from SciDataTool.Classes.Data1D import Data1D from SciDataTool.Functions.omp import omp, comp_undersampled_axe diff --git a/SciDataTool/Methods/DataND/plot_2D_Data.py b/SciDataTool/Methods/DataND/plot_2D_Data.py index fe8b83ca..27640b1b 100644 --- a/SciDataTool/Methods/DataND/plot_2D_Data.py +++ b/SciDataTool/Methods/DataND/plot_2D_Data.py @@ -1,12 +1,9 @@ -# -*- coding: utf-8 -*- - from SciDataTool.Functions.Plot.plot_2D import plot_2D from SciDataTool.Functions.Plot import ( unit_dict, norm_dict, axes_dict, COLORS, - LINESTYLES, ) from numpy import ( squeeze, diff --git a/SciDataTool/Methods/DataND/plot_3D_Data.py b/SciDataTool/Methods/DataND/plot_3D_Data.py index 618cb014..83c8c9e4 100644 --- a/SciDataTool/Methods/DataND/plot_3D_Data.py +++ b/SciDataTool/Methods/DataND/plot_3D_Data.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - from SciDataTool.Functions.Plot.plot_4D import plot_4D from SciDataTool.Functions.Plot.plot_3D import plot_3D from SciDataTool.Functions.Plot import unit_dict, norm_dict, axes_dict diff --git a/SciDataTool/Methods/DataND/rebuild_symmetries.py b/SciDataTool/Methods/DataND/rebuild_symmetries.py index f63947d7..f14dc09a 100644 --- a/SciDataTool/Methods/DataND/rebuild_symmetries.py +++ b/SciDataTool/Methods/DataND/rebuild_symmetries.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from SciDataTool.Functions.symmetries import ( rebuild_symmetries as rebuild_symmetries_fct, ) diff --git a/SciDataTool/Methods/DataPattern/get_length.py b/SciDataTool/Methods/DataPattern/get_length.py index fa9ca6d6..e7f13f12 100644 --- a/SciDataTool/Methods/DataPattern/get_length.py +++ b/SciDataTool/Methods/DataPattern/get_length.py @@ -1,7 +1,3 @@ -# -*- coding: utf-8 -*- -from SciDataTool.Functions import AxisError - - def get_length(self, is_pattern=False, is_smallestperiod=False): """Returns the length of the axis taking symmetries into account. Parameters diff --git a/SciDataTool/Methods/DataPattern/has_period.py b/SciDataTool/Methods/DataPattern/has_period.py index b5afec82..d99ce18e 100644 --- a/SciDataTool/Methods/DataPattern/has_period.py +++ b/SciDataTool/Methods/DataPattern/has_period.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- - - def has_period(self): """Indicates if an axis has symmetries. Parameters diff --git a/SciDataTool/Methods/DataTime/time_to_freq.py b/SciDataTool/Methods/DataTime/time_to_freq.py index 5f0a5fdd..8dc4da1a 100644 --- a/SciDataTool/Methods/DataTime/time_to_freq.py +++ b/SciDataTool/Methods/DataTime/time_to_freq.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from SciDataTool import Data1D from SciDataTool.Functions import AxisError @@ -35,7 +34,7 @@ def time_to_freq(self): axes_str.append(axis_str) if axes_str == [axis.name for axis in self.axes]: raise AxisError( - "ERROR: No available axis is compatible with fft (should be time or angle)" + "No available axis is compatible with fft (should be time or angle)" ) else: results = self.get_along(*axes_str) diff --git a/SciDataTool/Methods/VectorField/freq_to_time.py b/SciDataTool/Methods/VectorField/freq_to_time.py index c6f3e767..0bbed3e0 100644 --- a/SciDataTool/Methods/VectorField/freq_to_time.py +++ b/SciDataTool/Methods/VectorField/freq_to_time.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- - - def freq_to_time(self): """Performs the Fourier Transform and stores the resulting field in a VectorField of DataTime objects. Parameters diff --git a/SciDataTool/Methods/VectorField/get_axes.py b/SciDataTool/Methods/VectorField/get_axes.py index ed56e316..61efefec 100644 --- a/SciDataTool/Methods/VectorField/get_axes.py +++ b/SciDataTool/Methods/VectorField/get_axes.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- - - def get_axes(self, *args): """Returns the list of axes. Parameters diff --git a/SciDataTool/Methods/VectorField/get_harm_rphiz_along.py b/SciDataTool/Methods/VectorField/get_harm_rphiz_along.py index 12ef0183..0e226d20 100644 --- a/SciDataTool/Methods/VectorField/get_harm_rphiz_along.py +++ b/SciDataTool/Methods/VectorField/get_harm_rphiz_along.py @@ -1,8 +1,6 @@ -# -*- coding: utf-8 -*- - from numpy import zeros -from SciDataTool.Functions.conversions import xy_to_rphi, cart2pol +from SciDataTool.Functions.conversions import cart2pol from SciDataTool.Functions import AxisError diff --git a/SciDataTool/Methods/VectorField/get_mag_rphiz_along.py b/SciDataTool/Methods/VectorField/get_mag_rphiz_along.py index ec40fff4..6dc0444f 100644 --- a/SciDataTool/Methods/VectorField/get_mag_rphiz_along.py +++ b/SciDataTool/Methods/VectorField/get_mag_rphiz_along.py @@ -1,8 +1,6 @@ -# -*- coding: utf-8 -*- - from numpy import zeros -from SciDataTool.Functions.conversions import xy_to_rphi, cart2pol +from SciDataTool.Functions.conversions import cart2pol from SciDataTool.Functions import AxisError diff --git a/SciDataTool/Methods/VectorField/get_mag_xyz_along.py b/SciDataTool/Methods/VectorField/get_mag_xyz_along.py index 5a9362ff..92fb5ab5 100644 --- a/SciDataTool/Methods/VectorField/get_mag_xyz_along.py +++ b/SciDataTool/Methods/VectorField/get_mag_xyz_along.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - from numpy import zeros from SciDataTool.Functions.conversions import pol2cart diff --git a/SciDataTool/Methods/VectorField/get_rphiz_along.py b/SciDataTool/Methods/VectorField/get_rphiz_along.py index 003092e2..0aa14553 100644 --- a/SciDataTool/Methods/VectorField/get_rphiz_along.py +++ b/SciDataTool/Methods/VectorField/get_rphiz_along.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - from numpy import zeros from SciDataTool.Functions.conversions import cart2pol diff --git a/SciDataTool/Methods/VectorField/get_vectorfield_along.py b/SciDataTool/Methods/VectorField/get_vectorfield_along.py index c03e9370..d2750327 100644 --- a/SciDataTool/Methods/VectorField/get_vectorfield_along.py +++ b/SciDataTool/Methods/VectorField/get_vectorfield_along.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- - - def get_vectorfield_along(self, *args, unit="SI", is_norm=False, axis_data=[]): """Returns the sliced or interpolated version of the data, using conversions and symmetries if needed. Parameters diff --git a/SciDataTool/Methods/VectorField/get_xyz_along.py b/SciDataTool/Methods/VectorField/get_xyz_along.py index 8934ec31..78b00e66 100644 --- a/SciDataTool/Methods/VectorField/get_xyz_along.py +++ b/SciDataTool/Methods/VectorField/get_xyz_along.py @@ -1,6 +1,4 @@ -# -*- coding: utf-8 -*- - -from numpy import zeros, ndarray +from numpy import zeros from SciDataTool.Functions.conversions import pol2cart from SciDataTool.Functions import AxisError diff --git a/SciDataTool/Methods/VectorField/time_to_freq.py b/SciDataTool/Methods/VectorField/time_to_freq.py index 3748ad50..faebcada 100644 --- a/SciDataTool/Methods/VectorField/time_to_freq.py +++ b/SciDataTool/Methods/VectorField/time_to_freq.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- - - def time_to_freq(self): """Performs the Fourier Transform and stores the resulting field in a VectorField of DataFreq objects. Parameters diff --git a/SciDataTool/Methods/VectorField/to_rphiz.py b/SciDataTool/Methods/VectorField/to_rphiz.py index 664f0605..36d9b114 100644 --- a/SciDataTool/Methods/VectorField/to_rphiz.py +++ b/SciDataTool/Methods/VectorField/to_rphiz.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- - - def to_rphiz(self): """Performs the corrdinate change and stores the resulting field in a VectorField object. Parameters diff --git a/SciDataTool/Methods/VectorField/to_xyz.py b/SciDataTool/Methods/VectorField/to_xyz.py index 7aac068e..027ac91f 100644 --- a/SciDataTool/Methods/VectorField/to_xyz.py +++ b/SciDataTool/Methods/VectorField/to_xyz.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- - - def to_xyz(self): """Performs the corrdinate change and stores the resulting field in a VectorField object. Parameters @@ -27,7 +24,6 @@ def to_xyz(self): else axis.name + "[smallestperiod]" for axis in self.components["radial"].axes ] - # TODO: arg_list = [axis.name for axis in self.components["radial"].axes] result = self.get_xyz_along(*arg_list, is_squeeze=False) # Store in new VectorField comp_dict = dict() diff --git a/SciDataTool/__init__.py b/SciDataTool/__init__.py index 4529f36a..03fc5254 100644 --- a/SciDataTool/__init__.py +++ b/SciDataTool/__init__.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- from SciDataTool.Classes.Data import Data from SciDataTool.Classes.Data1D import Data1D from SciDataTool.Classes.DataFreq import DataFreq diff --git a/Tests/Validation/test_omp.py b/Tests/Validation/test_omp.py index 3faaead7..c0f108bb 100644 --- a/Tests/Validation/test_omp.py +++ b/Tests/Validation/test_omp.py @@ -1,15 +1,10 @@ -# -*- coding: utf-8 -*- - import pytest import numpy as np -from numpy import ndarray, sin, linspace, asarray, meshgrid, cos -from math import floor, pi - from SciDataTool.Classes.Data1D import Data1D from SciDataTool.Classes.DataTime import DataTime -from SciDataTool.Classes.DataND import DataND -from SciDataTool.Functions.omp import comp_undersampling, omp, comp_undersampled_axe +from SciDataTool.Functions.omp import comp_undersampling, comp_undersampled_axe + @pytest.mark.validation def test_omp_SMV(): @@ -17,7 +12,7 @@ def test_omp_SMV(): Test the recovery of a sparse undersampled signal in the SMV (1D) situation """ - def f_1d(t: ndarray) -> ndarray: + def f_1d(t: np.ndarray) -> np.ndarray: """ Create a 1D function with the following Fourier transform coefficients: - 2 at 0 Hz @@ -27,24 +22,20 @@ def f_1d(t: ndarray) -> ndarray: return ( 2 - + 3 * sin(5 * 2 * pi * t) - + 4 * sin(12 * 2 * pi * t) - + 1 * sin(20 * 2 * pi * t) + + 3 * np.sin(5 * 2 * np.pi * t) + + 4 * np.sin(12 * 2 * np.pi * t) + + 1 * np.sin(20 * 2 * np.pi * t) ) # Define a time vector n = 200 - time = linspace(0,1,n,endpoint=False) + time = np.linspace(0, 1, n, endpoint=False) Time = Data1D(name="time", unit="s", values=time) # Compute the signal the signal signal = f_1d(Time.values) Field = DataTime( - name="field", - symbol="X", - axes=[Time], - values=signal, - unit="dimless" + name="field", symbol="X", axes=[Time], values=signal, unit="dimless" ) # Randomly choose observations of the signal @@ -67,7 +58,7 @@ def f_1d(t: ndarray) -> ndarray: # Recover the signal with the OMP # The parameter n_coefs is the number of atoms of the dictionary used to recover the signal - Field_recover = Field_under.orthogonal_mp(Time,n_coefs=10) + Field_recover = Field_under.orthogonal_mp(Time, n_coefs=10) signal_recover = Field_recover.values # Check that the result match the signal @@ -86,7 +77,7 @@ def test_comp_undersampled_axe(): """ n = 100 - time = linspace(0,1,n,endpoint=False) + time = np.linspace(0, 1, n, endpoint=False) Time = Data1D( name="time", @@ -98,7 +89,7 @@ def test_comp_undersampled_axe(): K = 0.5 M, Time_under = comp_undersampling(K, Time, seed=10) - np.testing.assert_array_equal(M,comp_undersampled_axe(Time,Time_under)) + np.testing.assert_array_equal(M, comp_undersampled_axe(Time, Time_under)) @pytest.mark.validation @@ -108,7 +99,7 @@ def test_omp_dataND(): using the method DataND.orthogonal_mp """ - def f_2d(theta: ndarray, t: ndarray) -> ndarray: + def f_2d(theta: np.ndarray, t: np.ndarray) -> np.ndarray: """ Create a 2D function with the following Fourier transform coefficients: - 1 at 6 Hz @@ -119,25 +110,27 @@ def f_2d(theta: ndarray, t: ndarray) -> ndarray: """ return ( - 0 - + 2 * cos(6 * 2 * pi * t + 20 * theta/360) - + 1 * cos(10 * 2 * pi * t + 40 * theta/360) + 0 + + 2 * np.cos(6 * 2 * np.pi * t + 20 * theta / 360) + + 1 * np.cos(10 * 2 * np.pi * t + 40 * theta / 360) ) - + # Define the Time and Angle vector n = 200 - Time = Data1D(name="time", unit="s", values=linspace(0,1,n,endpoint=False)) - Angle = Data1D(name="angle", unit="{°}", values=linspace(0,40,40,endpoint=False)) + Time = Data1D(name="time", unit="s", values=np.linspace(0, 1, n, endpoint=False)) + Angle = Data1D( + name="angle", unit="{°}", values=np.linspace(0, 40, 40, endpoint=False) + ) # Compute a grid of the space and the resulting field - time_coord, angle_coord = meshgrid(Time.get_values(), Angle.get_values()) + time_coord, angle_coord = np.meshgrid(Time.get_values(), Angle.get_values()) field = f_2d(angle_coord, time_coord).T Field = DataTime( name="Field", symbol="X", unit="dimless", - axes=[Time,Angle], + axes=[Time, Angle], values=field, ) @@ -146,14 +139,16 @@ def f_2d(theta: ndarray, t: ndarray) -> ndarray: M, Time_under = comp_undersampling(K, Time, seed=8) # Compute a new grid and the resulting field - time_under_coord, angle_under_coord = meshgrid(Time_under.get_values(), Angle.get_values()) - field_under = f_2d(angle_under_coord,time_under_coord).T + time_under_coord, angle_under_coord = np.meshgrid( + Time_under.get_values(), Angle.get_values() + ) + field_under = f_2d(angle_under_coord, time_under_coord).T Field_under = DataTime( name="Field", symbol="X", unit="dimless", - axes=[Time_under,Angle], + axes=[Time_under, Angle], values=field_under, ) @@ -169,6 +164,7 @@ def f_2d(theta: ndarray, t: ndarray) -> ndarray: atol=1e-1, ) + @pytest.mark.validation def test_comp_undersampling(): """ @@ -177,23 +173,17 @@ def test_comp_undersampling(): Time = Data1D( name="time", unit="s", - values=linspace(0,1,100), + values=np.linspace(0, 1, 100), ) K = 0.5 - [M, Time_undersampled] = comp_undersampling(K,Time) + [M, Time_undersampled] = comp_undersampling(K, Time) assert len(M) == len(Time_undersampled.values) - np.testing.assert_array_equal(M,comp_undersampled_axe(Time, Time_undersampled)) + np.testing.assert_array_equal(M, comp_undersampled_axe(Time, Time_undersampled)) K = 1.0 - [M, Time_undersampled] = comp_undersampling(K,Time) + [M, Time_undersampled] = comp_undersampling(K, Time) assert len(M) == len(Time_undersampled.values) - np.testing.assert_array_equal(M,comp_undersampled_axe(Time, Time_undersampled)) - - - - - - + np.testing.assert_array_equal(M, comp_undersampled_axe(Time, Time_undersampled)) diff --git a/Tests/__init__.py b/Tests/__init__.py index 808fd857..226d591b 100644 --- a/Tests/__init__.py +++ b/Tests/__init__.py @@ -1,6 +1,5 @@ -# -*- coding: utf-8 -*- import logging -from os.path import normpath, join, abspath, dirname, isdir +from os.path import join, abspath, dirname, isdir from os import makedirs from shutil import rmtree from matplotlib import use diff --git a/Tests/find.py b/Tests/find.py index 3ce31e65..6d08ec8a 100644 --- a/Tests/find.py +++ b/Tests/find.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - from numpy.random import random_sample, ranf PYTHON_TYPE = ["float", "int", "str", "bool", "complex"] From 5dfe21f287da89c5467fd872c0e5b5efa4f79443 Mon Sep 17 00:00:00 2001 From: helene-t Date: Tue, 26 Oct 2021 09:55:37 +0200 Subject: [PATCH 09/10] [BC] fixed typos --- SciDataTool/Methods/VectorField/get_rphiz_along.py | 4 ++-- SciDataTool/Methods/VectorField/to_rphiz.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/SciDataTool/Methods/VectorField/get_rphiz_along.py b/SciDataTool/Methods/VectorField/get_rphiz_along.py index 0aa14553..3a733a8f 100644 --- a/SciDataTool/Methods/VectorField/get_rphiz_along.py +++ b/SciDataTool/Methods/VectorField/get_rphiz_along.py @@ -59,11 +59,11 @@ def get_rphiz_along( else: phi = Datax.get_axes("angle")[0].get_values() # Convert to cylindrical coordinates - (field_r, field_t) = cart2pol(field_x, field_y, phi) + (field_r, field_c) = cart2pol(field_x, field_y, phi) # Extract second time with true args if "angle" not in args and "angle[smallestperiod]" not in args: Datax.values = field_r - Datay.values = field_t + Datay.values = field_c self.components["radial"] = Datax self.components["tangential"] = Datay resultr = self.components["radial"].get_along( diff --git a/SciDataTool/Methods/VectorField/to_rphiz.py b/SciDataTool/Methods/VectorField/to_rphiz.py index 36d9b114..1a16a85c 100644 --- a/SciDataTool/Methods/VectorField/to_rphiz.py +++ b/SciDataTool/Methods/VectorField/to_rphiz.py @@ -22,7 +22,7 @@ def to_rphiz(self): axis.name if axis.name in ["freqs", "wavenumber"] else axis.name + "[smallestperiod]" - for axis in self.components["radial"].axes + for axis in self.components["comp_x"].axes ] result = self.get_rphiz_along(*arg_list) # Store in new VectorField From 425e3b565b6a0757c974a70fc2fee894c399baec Mon Sep 17 00:00:00 2001 From: helene-t Date: Tue, 26 Oct 2021 10:20:52 +0200 Subject: [PATCH 10/10] [BC] fixed bugs in unit actualization for derivate/integrate --- SciDataTool/Functions/conversions.py | 6 ++++++ SciDataTool/Functions/omp.py | 2 -- SciDataTool/Methods/DataND/get_data_along.py | 4 ++-- Tests/Validation/test_get_data_along.py | 11 +++++++++-- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/SciDataTool/Functions/conversions.py b/SciDataTool/Functions/conversions.py index 6de552dd..4aba5f44 100644 --- a/SciDataTool/Functions/conversions.py +++ b/SciDataTool/Functions/conversions.py @@ -110,8 +110,11 @@ def get_dim_prefix(unit_str): def get_unit_derivate(unit_str, axis_unit): + unit_str = unit_str.replace("*", "").replace(" ", "").replace("^", "") if axis_unit == "Hz": axis_unit = "s" + if axis_unit == "rad": + axis_unit = "m" p = 0 if "/" in unit_str: unit_num = unit_str.split("/")[0] @@ -153,8 +156,11 @@ def get_unit_derivate(unit_str, axis_unit): def get_unit_integrate(unit_str, axis_unit): + unit_str = unit_str.replace("*", "").replace(" ", "").replace("^", "") if axis_unit == "Hz": axis_unit = "s" + if axis_unit == "rad": + axis_unit = "m" p = 0 if "/" in unit_str: unit_num = unit_str.split("/")[0] diff --git a/SciDataTool/Functions/omp.py b/SciDataTool/Functions/omp.py index e45ae889..d73e8c54 100644 --- a/SciDataTool/Functions/omp.py +++ b/SciDataTool/Functions/omp.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - from SciDataTool.Classes.Data1D import Data1D import numpy as np diff --git a/SciDataTool/Methods/DataND/get_data_along.py b/SciDataTool/Methods/DataND/get_data_along.py index 4f2eadcb..2197800f 100644 --- a/SciDataTool/Methods/DataND/get_data_along.py +++ b/SciDataTool/Methods/DataND/get_data_along.py @@ -87,9 +87,9 @@ def get_data_along(self, *args, unit="SI", is_norm=False, axis_data=[]): unit = self.unit for axis in axes_list: if axis.extension in ["antiderivate", "integrate"]: - unit = get_unit_integrate(self.unit, axis.corr_unit) + unit = get_unit_integrate(unit, axis.corr_unit) elif axis.extension == "derivate": - unit = get_unit_derivate(self.unit, axis.corr_unit) + unit = get_unit_derivate(unit, axis.corr_unit) return DataClass( name=self.name, diff --git a/Tests/Validation/test_get_data_along.py b/Tests/Validation/test_get_data_along.py index 23132eff..1b4696cf 100644 --- a/Tests/Validation/test_get_data_along.py +++ b/Tests/Validation/test_get_data_along.py @@ -183,6 +183,13 @@ def test_get_data_along_integrate(): assert_array_almost_equal(Field_rms.values, A / np.sqrt(2), decimal=15) assert Field_rms.unit == "m" + Field.unit = "N/m^2" + Time.unit = "m" + Field_int = Field.get_data_along("time=integrate", "angle=integrate") + assert Field_int.unit == "N" + Time.unit = "s" + Field.unit = "m" + # Anti-periodic signal Time0 = Time.get_axis_periodic(Nper=1, is_aper=True) ta0, at0 = np.meshgrid(Time0.get_values(is_smallestperiod=True), Angle.get_values()) @@ -310,7 +317,7 @@ def test_get_data_along_antiderivate(): # Angle derivation Field_anti_a = Field.get_data_along("time", "angle=antiderivate") - assert Field_anti_a.unit == "mrad", "wrong unit: " + Field_anti_a.unit + assert Field_anti_a.unit == "m2", "wrong unit: " + Field_anti_a.unit field_anti_a_check = Field_anti_a.values field_anti_a_ref = 5 / 3 * np.sin(2 * np.pi * f * ta.T + 3 * at.T) assert_array_almost_equal(field_anti_a_check, field_anti_a_ref, decimal=3) @@ -374,7 +381,7 @@ def test_get_data_along_derivate(): # Angle derivation Field_diff_a = Field.get_data_along("time", "angle=derivate") - assert Field_diff_a.unit == "m/rad" + assert Field_diff_a.unit == "" field_diff_a_check = Field_diff_a.values field_diff_a_ref = -5 * 3 * np.sin(2 * np.pi * f * ta.T + 3 * at.T) assert_array_almost_equal(field_diff_a_check, field_diff_a_ref, decimal=1)