pytdo 0.2.2.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ from typing import TypedDict
2
+ from ..config import Config
3
+
4
+
5
+ class MetadataProcessor(TypedDict):
6
+ temperature: float
7
+ fs_pickup: float
8
+ fs_signal: float
9
+
10
+
11
+ class tTDOProcessor:
12
+ cfg: Config
13
+ _meas_name: str
14
+ _tdo_name: str
15
+ _tdo_det_inc_name: str
16
+ _tdo_det_dec_name: str
17
+ _tdo_inv_inc_name: str
18
+ _tdo_inv_dec_name: str
19
+ npoints_raw: int
20
+ inds_inc: slice
21
+ inds_dec: slice
22
+ is_cropped: dict[str, bool]
pytdo/py.typed ADDED
File without changes
pytdo/sp.py ADDED
@@ -0,0 +1,267 @@
1
+ """Signal processing module."""
2
+
3
+ import numpy as np
4
+
5
+
6
+ def _find_barycenters_fast(
7
+ fxx: np.ndarray, sxx: np.ndarray, freq_window: float
8
+ ) -> np.ndarray:
9
+ max_f_idx = np.argmax(sxx, axis=0)
10
+ bary_win_size = int(freq_window / np.mean(np.diff(fxx)))
11
+ # sxx is nfreqs x ntimes
12
+ # Create indices for all freq-windows at each time so dimensions :
13
+ # winsize x ntimes
14
+ # Indices are -30:30 centered on each max freq
15
+ indices = (
16
+ np.arange(-bary_win_size, bary_win_size + 1)[..., np.newaxis]
17
+ + max_f_idx[np.newaxis, ...]
18
+ )
19
+ # Make sure indices are in range
20
+ indices = np.clip(indices, 0, sxx.shape[0] - 1)
21
+ # Extract windows for all columns. We need cols_indexer to select all columns of
22
+ # the spectrogram and pair them with the frequency indices
23
+ cols_indexer = np.arange(sxx.shape[1])
24
+ sxx_windows = sxx[indices, cols_indexer]
25
+ fxx_windows = fxx[indices]
26
+ # Compute barycenters
27
+ sum_weight_f = np.sum(fxx_windows * sxx_windows, axis=0)
28
+ sum_weight = np.sum(sxx_windows, axis=0)
29
+
30
+ return sum_weight_f / sum_weight
31
+
32
+
33
+ def _find_barycenters_slow(
34
+ fxx: np.ndarray, sxx: np.ndarray, freq_window: float
35
+ ) -> np.ndarray:
36
+ max_f_idx = np.argmax(sxx, axis=0)
37
+ bary_win_size = int(freq_window / np.mean(np.diff(fxx)))
38
+ barycenters = np.zeros(max_f_idx.size)
39
+ for idx in range(max_f_idx.size):
40
+ indexer = slice(max_f_idx[idx] - bary_win_size, max_f_idx[idx] + bary_win_size)
41
+ sum_weight_f = np.sum(fxx[indexer] * sxx[indexer, idx])
42
+ sum_weight = np.sum(sxx[indexer, idx])
43
+ barycenters[idx] = sum_weight_f / sum_weight
44
+
45
+ return barycenters
46
+
47
+
48
+ def find_barycenters(
49
+ fxx: np.ndarray, sxx: np.ndarray, freq_window: float, fast: bool = True
50
+ ) -> np.ndarray:
51
+ """
52
+ Compute the barycenters of the maximum frequencies over time in a spectrogram.
53
+
54
+ It uses the spectrogram from `scipy.signal.spectrogram()`.
55
+
56
+ Parameters
57
+ ----------
58
+ fxx : np.ndarray
59
+ Array with the frequencies of the spectrogram, as returned by
60
+ `scipy.signal.spectrogram()`.
61
+ sxx : np.ndarray
62
+ Spectrogram as returned by `scipy.signal.spectrogram()`.
63
+ freq_window : float
64
+ Frequency-window size around the frequency maxima, in the same units as `fxx`.
65
+ fast : bool, optional
66
+ Whether to use the vectorized version of the algorithm, default is True.
67
+ """
68
+ if fast:
69
+ barycenters = _find_barycenters_fast(fxx, sxx, freq_window)
70
+ else:
71
+ barycenters = _find_barycenters_slow(fxx, sxx, freq_window)
72
+
73
+ return barycenters
74
+
75
+
76
+ def find_nearest_index(array: np.ndarray, value: float) -> int:
77
+ """
78
+ Find the index of the nearest value in `array` to the input `value`.
79
+
80
+ Parameters
81
+ ----------
82
+ array : np.ndarray
83
+ Array from which the index is extracted from.
84
+ value : float
85
+ Value to find in `array`.
86
+ """
87
+ return (np.abs(array - value)).argmin()
88
+
89
+
90
+ def get_up_down_indices(x: np.ndarray) -> tuple[slice, slice]:
91
+ """
92
+ Get indices in `y` for increasing and decreasing `x`.
93
+
94
+ Returns 2 slices, the first indexes `y` in increasing `x`, the second in decreasing
95
+ `x`. `x` should be a vector that increases, reach a maximum, then decreases.
96
+
97
+ Parameters
98
+ ----------
99
+ x : np.ndarray
100
+ Should be 1D.
101
+
102
+ Returns
103
+ -------
104
+ slice_inc, slice_dec : slice
105
+ """
106
+ idx_at_xmax = np.argmax(x)
107
+ return (slice(0, idx_at_xmax), slice(-1, idx_at_xmax - 1, -1))
108
+
109
+
110
+ def split_up_down(
111
+ x: np.ndarray, y: np.ndarray
112
+ ) -> tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
113
+ """
114
+ Split `y` in two arrays based on if `x` is increasing or decreasing.
115
+
116
+ `x` should be a vector that increases, reaches a maximum and then decreases (e.g. a
117
+ magnetic field).
118
+ One array corresponds to increasing `x`, the othe corresponds to decreasing `x`. The
119
+ arrays are sorted and the `x` arrays are also returned.
120
+
121
+ Parameters
122
+ ----------
123
+ x, y : np.ndarray
124
+ 1D vectors.
125
+
126
+ Returns
127
+ -------
128
+ x_inc, y_inc : np.ndarray
129
+ `x` and `y` when `x` increases.
130
+ x_dec, y_dec : np.ndarray
131
+ `x` and `y` when `x` decreases.
132
+ """
133
+ s_inc, s_dec = get_up_down_indices(x, y)
134
+ return (x[s_inc], y[s_inc], x[s_dec], y[s_dec])
135
+
136
+
137
+ def fit_poly(
138
+ field: np.ndarray,
139
+ sig: np.ndarray,
140
+ boundary1: float,
141
+ boundary2: float,
142
+ poly_deg: int,
143
+ ) -> tuple[np.ndarray, np.ndarray]:
144
+ """
145
+ Polynomial fit of `sig(field)`, between boundaries.
146
+
147
+ Returns the resulting polynome evaluated at `field` along with the detrended signal.
148
+
149
+ Parameters
150
+ ----------
151
+ field : np.ndarray
152
+ Magnetic field vector (x).
153
+ sig : np.ndarray
154
+ Signal vector (y).
155
+ poly_boundary1, poly_boundary2 : float
156
+ Do the fit only in `field` comprised in `[poly_boundary1, poly_boundary2]`.
157
+ poly_deg : int
158
+ Polynome degree.
159
+
160
+ Returns
161
+ -------
162
+ poly : np.ndarray
163
+ Polynome evaluated at `field`.
164
+ sig_detrend : np.ndarray
165
+ `sig` subtracted
166
+ """
167
+ idx_bmin = find_nearest_index(field, boundary1)
168
+ idx_bmax = find_nearest_index(field, boundary2)
169
+
170
+ fit = np.polynomial.Polynomial.fit(
171
+ field[idx_bmin:idx_bmax],
172
+ sig[idx_bmin:idx_bmax],
173
+ poly_deg,
174
+ )
175
+ res = fit(field)
176
+ return res, sig - res
177
+
178
+
179
+ def interpolate_inverse(
180
+ x: np.ndarray, y: np.ndarray, boundary1: float, boundary2: float, npoints: int
181
+ ) -> tuple[np.ndarray, np.ndarray]:
182
+ """
183
+ Oversample `y` in `1/x` in the given range.
184
+
185
+ Parameters
186
+ ----------
187
+ x : np.ndarray
188
+ Points at which `y` is evaluated.
189
+ y : np.ndarray
190
+ Signal to oversample.
191
+ boundary1, boundary2 : float
192
+ Define the range to select and oversample, before inversion.
193
+ npoints : int
194
+ Number of points of the resulting vectors.
195
+
196
+ Returns
197
+ -------
198
+ x_inverse_oversample : np.ndarray
199
+ `1/x` oversampled in `[1/boundary2, 1/boundary1]`.
200
+ y_oversample : np.ndarray
201
+ `y` oversampled in `[1/boundary2, 1/boundary1]`.
202
+ """
203
+ # Create the oversampled time vector
204
+ x_inverse_oversample = np.linspace(
205
+ 1 / boundary2, 1.0 / boundary1, npoints, endpoint=False
206
+ )
207
+ # Interpolate
208
+ y_oversample = np.interp(1 / x_inverse_oversample, x, y)
209
+
210
+ return x_inverse_oversample, y_oversample
211
+
212
+
213
+ def fourier_transform(
214
+ a: np.ndarray, d: float, pad_mult: int = 1
215
+ ) -> tuple[np.ndarray, np.ndarray]:
216
+ """
217
+ Fourier transform for 1D real signal, with extra padding.
218
+
219
+ Parameters
220
+ ----------
221
+ a : np.ndarray
222
+ Input array.
223
+ d : float
224
+ Sample spacing.
225
+ pad_mult : int, optional
226
+ Multiplier of signal size used for padding. Default is 1.
227
+
228
+ Returns
229
+ -------
230
+ freq : np.ndarray
231
+ Frequencies.
232
+ X : np.ndarray
233
+ Magnitude of the fourier transform.
234
+ """
235
+ n = a.size * pad_mult
236
+ X = np.fft.rfft(np.hamming(a.size) * a, n=n)[:-1]
237
+ freq = np.fft.rfftfreq(n, d=d)[:-1]
238
+ X = np.abs(X) * 2 / n
239
+ return freq, X
240
+
241
+
242
+ def collate_arrays(to_save: list[np.ndarray]):
243
+ """
244
+ Store 1D vectors as columns in a 2D arrays.
245
+
246
+ Dimension mismatches are filled with trailing NaNs.
247
+
248
+ Parameters
249
+ ----------
250
+ to_save : list[np.ndarray]
251
+ List of arrays to save in the same file.
252
+
253
+ Returns
254
+ -------
255
+ a : np.ndarray
256
+ The array with all the vectors, with NaNs to fill shorter vectors.
257
+ """
258
+ # Build the array
259
+ ncolumns = len(to_save)
260
+ nrows = max([a.size for a in to_save])
261
+ a_out = np.full((nrows, ncolumns), np.nan)
262
+
263
+ # Fill the array
264
+ for idx_col, a in enumerate(to_save):
265
+ a_out[: a.size, idx_col] = a
266
+
267
+ return a_out
@@ -0,0 +1,101 @@
1
+ Metadata-Version: 2.3
2
+ Name: pytdo
3
+ Version: 0.2.2.2
4
+ Summary: Add your description here
5
+ Author: Guillaume Le Goc
6
+ Author-email: Guillaume Le Goc <guillaume.le-goc@lncmi.cnrs.fr>
7
+ Requires-Dist: matplotlib>=3.10.7
8
+ Requires-Dist: numpy>=2.3.5
9
+ Requires-Dist: rich>=14.2.0
10
+ Requires-Dist: scipy>=1.16.3
11
+ Requires-Dist: pydantic>=2.12.4
12
+ Requires-Dist: pyqt6>=6.10.0
13
+ Requires-Dist: pyuson>=0.7.0
14
+ Requires-Python: >=3.12
15
+ Description-Content-Type: text/markdown
16
+
17
+ # `pytdo`
18
+
19
+ [![Latest Release](https://gitlab.in2p3.fr/himagnetos/pytdo/-/badges/release.svg)](https://gitlab.in2p3.fr/himagnetos/pytdo/-/releases)
20
+ [![PyPI](https://img.shields.io/pypi/v/pytdo.svg)](https://pypi.org/project/pytdo/)
21
+
22
+ ## Description
23
+ `pytdo` provides a Python API to read and analyse tunnel diode oscillator experiments on samples exposed to a high magnetic field. It is bundled with a graphical user interface written with PyQt.
24
+
25
+ ## Installation
26
+
27
+ ### As an app
28
+ If you plan to just use the graphical user interface, consider installing `pytdo` as a tool managed by uv, a modern Python package manager.
29
+
30
+ To do so, install uv : https://docs.astral.sh/uv/getting-started/installation/.
31
+
32
+ Then, from the command line (PowerShell in Windows), install `pytdo` with uv :
33
+ ```
34
+ uv tool install pytdo
35
+ ```
36
+
37
+ `pytdo` will be installed along its dependencies in an isolated environment with no risks to mess up with your current tools and Python versions.
38
+
39
+ Then, launch the GUI by running `pytdo` from the command line.
40
+
41
+ To update, run : `uv tool upgrade pytdo`.
42
+
43
+ ### As a library
44
+ Installing `pytdo` as a library will allow you to use it from scripts and Jupyter notebooks (e.g. you will be able to import it with `import pytdo`).
45
+ Note that you can re-use the same environment than the one for [`pyuson`](https://gitlab.in2p3.fr/himagnetos/pyuson).
46
+
47
+ 1. Create a Python environment :
48
+
49
+ With `conda` :
50
+ ```
51
+ conda create -n pytdo-env -c conda-forge python=3.12
52
+ ```
53
+
54
+ You may replace the environment name `pytdo-env` with whatever you want.
55
+
56
+ Activate the environment with :
57
+ ```
58
+ conda activate pytdo-env
59
+ ```
60
+
61
+ 2. Install the `pytdo` package :
62
+
63
+ With `pip`, with the `pytdo-env` activated :
64
+ ```
65
+ pip install pytdo
66
+ ```
67
+
68
+ #### Updates
69
+ To upgrade `pytdo` to the latest version, simply run from the activated environment :
70
+ ```
71
+ pip install pytdo --upgrade
72
+ ```
73
+
74
+ #### Manual download
75
+ 1. Download the repository and extract it on your computer (or clone it).
76
+ 2. Create and activate a conda environment as shown above.
77
+ 3. Install the extracted repository with `pip` :
78
+ ```
79
+ pip install "/path/to/pytdo-main"
80
+ ```
81
+
82
+ Alternatively, if you wish to make modification to the source code so that your change is reflected immediately, use the editable mode :
83
+ ```
84
+ pip install -e /path/to/pytdo-main
85
+ ```
86
+
87
+ ## Usage
88
+ You can use this package through the graphical user interface, or directly from Python prompt, script or Jupyter notebook using the `Processor` classes. All this methods rely on a TOML configuration file that specifies the experiment parameters and analysis settings.
89
+
90
+ A template configuration file is provided in the `configs` folder. Copy-paste and edit it according to your need. Each entry of the file is commented so it should be somewhat straightforward to configure. Then :
91
+
92
+ - Run the graphical interface with `python -m pytdo` from a terminal with the `pyuson-env` environment activated. Drag & drop the configuration file in the window, load the data, extract the TDO signal and interactively choose the field-window in which the signal is detrended.
93
+ - Write your own script using the library :
94
+ ```python
95
+ import pytdo
96
+
97
+ cfg_filename = "/path/to/your/config.toml"
98
+ r = pytdo.TDOProcessor(cfg_filename)
99
+ r.extract_tdo().remove_background().fft_inverse()
100
+ r.save_results_csv()
101
+ ```
@@ -0,0 +1,24 @@
1
+ pytdo/__init__.py,sha256=ODvkpsOLn2JAOFH67Wt7KUwTu2HQFGU8JIwtNqSBRgo,1062
2
+ pytdo/__main__.py,sha256=N9_neeztCtpt57lGth4Sh76665su9qFcuD4j2o2En2I,186
3
+ pytdo/assets/config_default.toml,sha256=LGHK3N3kY7s9j_e_dw1msvZs8t2SkmdJYbVFu35UKFU,3009
4
+ pytdo/config/__init__.py,sha256=LhUuYBPTFz1SRTEZ4stATkxwqi23mSXqL8DocsvmPXQ,105
5
+ pytdo/config/_config.py,sha256=nP5hM2BRQUjhhe0Azpx2u-1hP-IpXYHySmNuEzwJvVo,743
6
+ pytdo/config/_types.py,sha256=IpkyM9Xo5SkITK8Iwha0ueY0bIn98VjQt0kGMCLj_js,307
7
+ pytdo/config/models.py,sha256=rvlcHUeYM-jyM9VqyoKLm4hmsfcoSGtXdslmFLJZqKw,1338
8
+ pytdo/gui/__init__.py,sha256=LOIHr2l7b-GSo5bxgfiW5MRy21beTADfWknlKBQoeyU,396
9
+ pytdo/gui/_core.py,sha256=8HVR127q5QSLG0BvuhnxsHWzm0rhDdp0Dc_YP-jIr60,17808
10
+ pytdo/gui/_workers.py,sha256=lxYnJYGpyspPQ6lwCU4O9BKROKs_YOa5BKr-I-swaV4,2373
11
+ pytdo/gui/widgets/__init__.py,sha256=0Ijv9K-cosBpSiPz8V7nmWTf2WhMOfF3wr3pVNCVyMw,301
12
+ pytdo/gui/widgets/_configuration_tab.py,sha256=I9ODZed7OkQv3FBq6hqHm5gVt1SmIR4i2425dTHi26U,636
13
+ pytdo/gui/widgets/_graphs.py,sha256=cDClog4CplME_GEljfpt68Dje-Aw120yexcvYbFklDA,8270
14
+ pytdo/gui/widgets/_main_buttons.py,sha256=yeqA7gTVrKccmXxXlH1SSemxY1SY6XSGnImrX8q-ipE,2695
15
+ pytdo/gui/widgets/_param_content.py,sha256=5nsjDLt5fo8G8iK9kha0CVpm1UCVW-NZO4jnjZ1JW2U,3042
16
+ pytdo/processors/__init__.py,sha256=jGbDv24Em0wmPBnoa4HTzUWahDvOUTQ8g_q-ZWO8AL8,89
17
+ pytdo/processors/_tdo.py,sha256=bQUyWB5-PU22La2xephfk3mp3KL8LYxVs1Zb9qBMmPE,35036
18
+ pytdo/processors/_types.py,sha256=Izg05yabP9LuNydB8cyCrF5IMzFVZobQ4qKzwv9O2_M,439
19
+ pytdo/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
20
+ pytdo/sp.py,sha256=mHmNVIpyBXSmg_F3MnJhlcgemi36HhudMKiZYlktnZA,7670
21
+ pytdo-0.2.2.2.dist-info/WHEEL,sha256=xDCZ-UyfvkGuEHPeI7BcJzYKIZzdqN8A8o1M5Om8IyA,79
22
+ pytdo-0.2.2.2.dist-info/entry_points.txt,sha256=zLbRwmNJK8689D5YzFJHPipByYV-4W5MFBIXZTmapZU,43
23
+ pytdo-0.2.2.2.dist-info/METADATA,sha256=SScxZJLc2wTlbHB8-VwCUzyOOwTyhJn4W5F5zpb09Lk,3789
24
+ pytdo-0.2.2.2.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: uv 0.9.17
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,3 @@
1
+ [gui_scripts]
2
+ pytdo = pytdo:__main__.main
3
+