|
24 | 24 | import numpy as np
|
25 | 25 |
|
26 | 26 |
|
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 |
| - |
52 | 27 | def signal_fft(signal, delta):
|
53 | 28 | """
|
54 | 29 | Compute the complex Fourier transform of a signal.
|
@@ -94,23 +69,6 @@ def __deepcopy__(self, memo):
|
94 | 69 | return new_dict
|
95 | 70 |
|
96 | 71 |
|
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 |
| - |
114 | 72 | class Spectrum():
|
115 | 73 | """
|
116 | 74 | A class to handle amplitude spectra.
|
@@ -584,6 +542,56 @@ def write(self, filename, format='HDF5'):
|
584 | 542 | raise ValueError(f'Unsupported format: {format}')
|
585 | 543 |
|
586 | 544 |
|
| 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 | + |
587 | 595 | def _read_spectrum_from_hdf5_group(group):
|
588 | 596 | """
|
589 | 597 | Read a Spectrum object from an HDF5 group.
|
|
0 commit comments