Skip to content

Commit

Permalink
Reorganize functions in spectrum.py
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiodsf committed Mar 26, 2024
1 parent 99c202b commit 050da51
Showing 1 changed file with 50 additions and 42 deletions.
92 changes: 50 additions & 42 deletions sourcespec/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,6 @@
import numpy as np


# Set default YAML representer for unsupported types
def _default_yaml_representer(dumper, data):
return dumper.represent_scalar('tag:yaml.org,2002:str', str(data))
# flake8: noqa
yaml.representer.SafeRepresenter.add_representer(
None, _default_yaml_representer)


def _normalize_value_for_yaml(value):
"""
Normalize a value to a type supported by YAML serialization.
:param value: The value to normalize.
:return: A dictionary, a float or the original value.
"""
if hasattr(value, 'items'):
return {
key: _normalize_value_for_yaml(val) for key, val in value.items()
}
# check if value is numeric
with contextlib.suppress(TypeError, ValueError):
return float(value)
return value


def signal_fft(signal, delta):
"""
Compute the complex Fourier transform of a signal.
Expand Down Expand Up @@ -94,23 +69,6 @@ def __deepcopy__(self, memo):
return new_dict


def _n_significant_digits(x):
"""
Helper function to compute the number of significant digits of a number.
- If the number is greater than 1, the number of significant digits is
zero.
- If the number is less than 1, the number of significant digits is
the number of digits after the decimal point.
- If the number is zero, the number of significant digits is zero.
"""
try:
x = math.fabs(x)
except TypeError as e:
raise ValueError('x must be a number') from e
return 0 if x == 0 or x > 1 else -int(math.floor(math.log10(x)))


class Spectrum():
"""
A class to handle amplitude spectra.
Expand Down Expand Up @@ -584,6 +542,56 @@ def write(self, filename, format='HDF5'):
raise ValueError(f'Unsupported format: {format}')


# ---- Reading/writing functions and helper functions ----

def _n_significant_digits(x):
"""
Helper function to compute the number of significant digits of a number.
- If the number is greater than 1, the number of significant digits is
zero.
- If the number is less than 1, the number of significant digits is
the number of digits after the decimal point.
- If the number is zero, the number of significant digits is zero.
"""
try:
x = math.fabs(x)
except TypeError as e:
raise ValueError('x must be a number') from e
return 0 if x == 0 or x > 1 else -int(math.floor(math.log10(x)))


def _default_yaml_representer(dumper, data):
"""
Default YAML representer for unsupported types.
:param dumper: The YAML dumper.
:param data: The data to represent.
:return: The YAML representation of the data.
"""
return dumper.represent_scalar('tag:yaml.org,2002:str', str(data))
# flake8: noqa
yaml.representer.SafeRepresenter.add_representer(
None, _default_yaml_representer)


def _normalize_value_for_yaml(value):
"""
Normalize a value to a type supported by YAML serialization.
:param value: The value to normalize.
:return: A dictionary, a float or the original value.
"""
if hasattr(value, 'items'):
return {
key: _normalize_value_for_yaml(val) for key, val in value.items()
}
# check if value is numeric
with contextlib.suppress(TypeError, ValueError):
return float(value)
return value


def _read_spectrum_from_hdf5_group(group):
"""
Read a Spectrum object from an HDF5 group.
Expand Down

0 comments on commit 050da51

Please sign in to comment.