Skip to content

Commit 050da51

Browse files
committed
Reorganize functions in spectrum.py
1 parent 99c202b commit 050da51

File tree

1 file changed

+50
-42
lines changed

1 file changed

+50
-42
lines changed

sourcespec/spectrum.py

+50-42
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,6 @@
2424
import numpy as np
2525

2626

27-
# Set default YAML representer for unsupported types
28-
def _default_yaml_representer(dumper, data):
29-
return dumper.represent_scalar('tag:yaml.org,2002:str', str(data))
30-
# flake8: noqa
31-
yaml.representer.SafeRepresenter.add_representer(
32-
None, _default_yaml_representer)
33-
34-
35-
def _normalize_value_for_yaml(value):
36-
"""
37-
Normalize a value to a type supported by YAML serialization.
38-
39-
:param value: The value to normalize.
40-
:return: A dictionary, a float or the original value.
41-
"""
42-
if hasattr(value, 'items'):
43-
return {
44-
key: _normalize_value_for_yaml(val) for key, val in value.items()
45-
}
46-
# check if value is numeric
47-
with contextlib.suppress(TypeError, ValueError):
48-
return float(value)
49-
return value
50-
51-
5227
def signal_fft(signal, delta):
5328
"""
5429
Compute the complex Fourier transform of a signal.
@@ -94,23 +69,6 @@ def __deepcopy__(self, memo):
9469
return new_dict
9570

9671

97-
def _n_significant_digits(x):
98-
"""
99-
Helper function to compute the number of significant digits of a number.
100-
101-
- If the number is greater than 1, the number of significant digits is
102-
zero.
103-
- If the number is less than 1, the number of significant digits is
104-
the number of digits after the decimal point.
105-
- If the number is zero, the number of significant digits is zero.
106-
"""
107-
try:
108-
x = math.fabs(x)
109-
except TypeError as e:
110-
raise ValueError('x must be a number') from e
111-
return 0 if x == 0 or x > 1 else -int(math.floor(math.log10(x)))
112-
113-
11472
class Spectrum():
11573
"""
11674
A class to handle amplitude spectra.
@@ -584,6 +542,56 @@ def write(self, filename, format='HDF5'):
584542
raise ValueError(f'Unsupported format: {format}')
585543

586544

545+
# ---- Reading/writing functions and helper functions ----
546+
547+
def _n_significant_digits(x):
548+
"""
549+
Helper function to compute the number of significant digits of a number.
550+
551+
- If the number is greater than 1, the number of significant digits is
552+
zero.
553+
- If the number is less than 1, the number of significant digits is
554+
the number of digits after the decimal point.
555+
- If the number is zero, the number of significant digits is zero.
556+
"""
557+
try:
558+
x = math.fabs(x)
559+
except TypeError as e:
560+
raise ValueError('x must be a number') from e
561+
return 0 if x == 0 or x > 1 else -int(math.floor(math.log10(x)))
562+
563+
564+
def _default_yaml_representer(dumper, data):
565+
"""
566+
Default YAML representer for unsupported types.
567+
568+
:param dumper: The YAML dumper.
569+
:param data: The data to represent.
570+
:return: The YAML representation of the data.
571+
"""
572+
return dumper.represent_scalar('tag:yaml.org,2002:str', str(data))
573+
# flake8: noqa
574+
yaml.representer.SafeRepresenter.add_representer(
575+
None, _default_yaml_representer)
576+
577+
578+
def _normalize_value_for_yaml(value):
579+
"""
580+
Normalize a value to a type supported by YAML serialization.
581+
582+
:param value: The value to normalize.
583+
:return: A dictionary, a float or the original value.
584+
"""
585+
if hasattr(value, 'items'):
586+
return {
587+
key: _normalize_value_for_yaml(val) for key, val in value.items()
588+
}
589+
# check if value is numeric
590+
with contextlib.suppress(TypeError, ValueError):
591+
return float(value)
592+
return value
593+
594+
587595
def _read_spectrum_from_hdf5_group(group):
588596
"""
589597
Read a Spectrum object from an HDF5 group.

0 commit comments

Comments
 (0)