Skip to content

Commit

Permalink
[NF] New architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
Helene Toubin committed Jun 9, 2020
1 parent 4374ee2 commit 7c87206
Show file tree
Hide file tree
Showing 16 changed files with 467 additions and 298 deletions.
15 changes: 0 additions & 15 deletions SciDataTool/Classes/DataFreq.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@

# Import all class method
# Try/catch to remove unnecessary dependencies in unused method
try:
from SciDataTool.Methods.DataFreq.get_harmonics import get_harmonics
except ImportError as error:
get_harmonics = error
try:
from SciDataTool.Methods.DataFreq.freq_to_time import freq_to_time
except ImportError as error:
Expand All @@ -24,17 +20,6 @@ class DataFreq(DataND):

VERSION = 1
# Check ImportError to remove unnecessary dependencies in unused method
# cf Methods.DataFreq.get_harmonics
if isinstance(get_harmonics, ImportError):
get_harmonics = property(
fget=lambda x: raise_(
ImportError(
"Can't use DataFreq method get_harmonics: " + str(get_harmonics)
)
)
)
else:
get_harmonics = get_harmonics
# cf Methods.DataFreq.freq_to_time
if isinstance(freq_to_time, ImportError):
freq_to_time = property(
Expand Down
30 changes: 30 additions & 0 deletions SciDataTool/Classes/DataND.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@
from SciDataTool.Methods.DataND.get_magnitude_along import get_magnitude_along
except ImportError as error:
get_magnitude_along = error
try:
from SciDataTool.Methods.DataND.get_phase_along import get_phase_along
except ImportError as error:
get_phase_along = error
try:
from SciDataTool.Methods.DataND.get_harmonics import get_harmonics
except ImportError as error:
get_harmonics = error
from numpy import array, array_equal, squeeze
from SciDataTool.Classes._check import InitUnKnowClassError

Expand Down Expand Up @@ -218,6 +226,28 @@ class DataND(Data):
)
else:
get_magnitude_along = get_magnitude_along
# cf Methods.DataND.get_phase_along
if isinstance(get_phase_along, ImportError):
get_phase_along = property(
fget=lambda x: raise_(
ImportError(
"Can't use DataND method get_phase_along: " + str(get_phase_along)
)
)
)
else:
get_phase_along = get_phase_along
# cf Methods.DataND.get_harmonics
if isinstance(get_harmonics, ImportError):
get_harmonics = property(
fget=lambda x: raise_(
ImportError(
"Can't use DataND method get_harmonics: " + str(get_harmonics)
)
)
)
else:
get_harmonics = get_harmonics
# save method is available in all object
save = save

Expand Down
15 changes: 0 additions & 15 deletions SciDataTool/Classes/DataTime.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@

# Import all class method
# Try/catch to remove unnecessary dependencies in unused method
try:
from SciDataTool.Methods.DataTime.get_harmonics import get_harmonics
except ImportError as error:
get_harmonics = error
try:
from SciDataTool.Methods.DataTime.time_to_freq import time_to_freq
except ImportError as error:
Expand All @@ -24,17 +20,6 @@ class DataTime(DataND):

VERSION = 1
# Check ImportError to remove unnecessary dependencies in unused method
# cf Methods.DataTime.get_harmonics
if isinstance(get_harmonics, ImportError):
get_harmonics = property(
fget=lambda x: raise_(
ImportError(
"Can't use DataTime method get_harmonics: " + str(get_harmonics)
)
)
)
else:
get_harmonics = get_harmonics
# cf Methods.DataTime.time_to_freq
if isinstance(time_to_freq, ImportError):
time_to_freq = property(
Expand Down
2 changes: 1 addition & 1 deletion SciDataTool/Methods/DataND/compare_along.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def compare_along(self, *args, data_list=[], unit="SI", is_norm=False):
)
return_dict[axis] = common_axis_values[axis]
# Return axis and values
return_dict[self.symbol + "_ref"] = values
return_dict[self.symbol] = values
for i, data in enumerate(data_list):
return_dict[data.symbol + "_" + str(i)] = data_values[i]
return return_dict
4 changes: 2 additions & 2 deletions SciDataTool/Methods/DataND/compare_magnitude_along.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def compare_magnitude_along(self, *args, unit="SI", data_list=[], is_norm=False)
data_values = []
return_dict = {}
for data in data_list:
results = data.get_along(args, unit=unit, is_norm=is_norm)
results = data.get_magnitude_along(args, unit=unit, is_norm=is_norm)
data_values.append(results.pop(data.symbol))
data_axis_values.append(results)
# Get the common bases
Expand All @@ -53,7 +53,7 @@ def compare_magnitude_along(self, *args, unit="SI", data_list=[], is_norm=False)
)
return_dict[axis] = common_axis_values[axis]
# Return axis and values
return_dict[self.symbol + "_ref"] = values
return_dict[self.symbol] = values
for i, data in enumerate(data_list):
return_dict[data.symbol + "_" + str(i)] = data_values[i]
return return_dict
4 changes: 2 additions & 2 deletions SciDataTool/Methods/DataND/compare_phase_along.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def compare_phase_along(self, *args, unit="SI", data_list=[], is_norm=False):
data_values = []
return_dict = {}
for data in data_list:
results = data.get_along(args, unit=unit, is_norm=is_norm)
results = data.get_phase_along(args, unit=unit, is_norm=is_norm)
data_values.append(results.pop(data.symbol))
data_axis_values.append(results)
# Get the common bases
Expand All @@ -53,7 +53,7 @@ def compare_phase_along(self, *args, unit="SI", data_list=[], is_norm=False):
)
return_dict[axis] = common_axis_values[axis]
# Return axis and values
return_dict[self.symbol + "_ref"] = values
return_dict[self.symbol] = values
for i, data in enumerate(data_list):
return_dict[data.symbol + "_" + str(i)] = data_values[i]
return return_dict
85 changes: 85 additions & 0 deletions SciDataTool/Methods/DataND/get_harmonics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# -*- coding: utf-8 -*-
from SciDataTool.Functions import AxisError, NormError, UnitError
from SciDataTool.Functions.fft_functions import comp_fft
from SciDataTool.Functions.symmetries import rebuild_symmetries
from SciDataTool.Functions.conversions import convert, to_dB, to_dBA
from SciDataTool.Functions.parser import read_input_strings
from SciDataTool.Functions.interpolations import get_common_base, get_interpolation
from numpy import (
squeeze,
take,
apply_along_axis,
argsort,
negative,
meshgrid,
abs as np_abs,
)


def get_harmonics(self, N_harm, *args, unit="SI", is_norm=False, is_flat=False):
"""Returns the complex Fourier Transform of the field, using conversions and symmetries if needed.
Parameters
----------
self: Data
a Data object
N_harm: int
Number of largest harmonics to be extracted
args: list
Axes names, ranges and units
unit: str
Unit demanded by the user ("SI" by default)
is_norm: bool
Boolean indicating if the field must be normalized (False by default)
is_flat: bool
Boolean if the output data remains flattened (for 2D cases)
Returns
-------
list of 1Darray of axes values, ndarray of magnitude of FT
"""
if len(args) == 1 and type(args[0]) == tuple:
args = args[0] # if called from another script with *args
return_dict = self.get_magnitude_along(args, unit=unit, is_norm=is_norm)
values = return_dict[self.symbol]

# 2D case
if "freqs" in return_dict and "wavenumber" in return_dict:
r = return_dict["wavenumber"]
f = return_dict["freqs"]
# Flatten the data
values_flat = values.flatten()
R, F = meshgrid(r, f)
f = F.flatten()
r = R.flatten()
# Get the N_harm largest peaks
indices = argsort(negative(values_flat))
indices = indices[:N_harm]
values = values_flat[indices]
f = f[indices]
r = r[indices]
if len(values.shape) == 2 and not is_flat:
f.reshape((N_harm, N_harm))
r.reshape((N_harm, N_harm))
values.reshape((N_harm, N_harm))
return_dict["freqs"] = f
return_dict["wavenumber"] = r
return_dict[self.symbol] = values

# 1D cases
elif "freqs" in return_dict:
f = return_dict["freqs"]
indices = argsort(negative(values))
indices = indices[:N_harm]
f = f[indices]
values = values[indices]
return_dict["freqs"] = f
return_dict[self.symbol] = values
elif "wavenumber" in return_dict:
r = return_dict["wavenumber"]
indices = argsort(negative(values))
indices = indices[:N_harm]
r = r[indices]
values = values[indices]
return_dict["wavenumber"] = r
return_dict[self.symbol] = values

return return_dict
6 changes: 2 additions & 4 deletions SciDataTool/Methods/DataND/get_magnitude_along.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# -*- coding: utf-8 -*-
from SciDataTool.Functions import NormError, UnitError
from SciDataTool.Functions.fft_functions import comp_magnitude
from SciDataTool.Functions.symmetries import rebuild_symmetries
from SciDataTool.Functions.conversions import convert, to_dB, to_dBA
from SciDataTool.Functions.parser import read_input_strings
from SciDataTool.Functions.interpolations import get_common_base, get_interpolation
from numpy import abs as np_abs


Expand All @@ -26,6 +22,8 @@ def get_magnitude_along(self, *args, unit="SI", is_norm=False, axis_data=[]):
-------
list of 1Darray of axes values, ndarray of magnitude values
"""
if len(args) == 1 and type(args[0]) == tuple:
args = args[0] # if called from another script with *args
return_dict = self.get_along(args, axis_data=axis_data)
values = return_dict[self.symbol]
# Compute magnitude
Expand Down
47 changes: 47 additions & 0 deletions SciDataTool/Methods/DataND/get_phase_along.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
from SciDataTool.Functions import NormError
from SciDataTool.Functions.conversions import convert
from numpy import angle as np_angle


def get_phase_along(self, *args, unit="SI", is_norm=False, axis_data=[]):
"""Returns the ndarray of the magnitude of the FT, using conversions and symmetries if needed.
Parameters
----------
self: Data
a Data object
*args: list of strings
List of axes requested by the user, their units and values (optional)
unit: str
Unit requested by the user ("SI" by default)
is_norm: bool
Boolean indicating if the field must be normalized (False by default)
axis_data: list
list of ndarray corresponding to user-input data
Returns
-------
list of 1Darray of axes values, ndarray of magnitude values
"""
if len(args) == 1 and type(args[0]) == tuple:
args = args[0] # if called from another script with *args
return_dict = self.get_along(args, axis_data=axis_data)
values = return_dict[self.symbol]
# Compute magnitude
values = np_angle(values)
# Convert into right unit (apart because of degree conversion)
if unit == self.unit or unit == "SI":
if is_norm:
try:
values = values / self.normalizations.get("ref")
except:
raise NormError(
"ERROR: Reference value not specified for normalization"
)
elif unit == "°":
values = convert(values, "rad", "°")
elif unit in self.normalizations:
values = values / self.normalizations.get(unit)
else:
values = convert(values, self.unit, unit)
return_dict[self.symbol] = values
return return_dict
Loading

0 comments on commit 7c87206

Please sign in to comment.