pyopenrivercam 0.8.11__py3-none-any.whl → 0.9.0__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.
@@ -1,331 +0,0 @@
1
- """PIV processing wrappers for OpenPIV."""
2
-
3
- from typing import List, Optional, Tuple, Union
4
-
5
- import numpy as np
6
- import openpiv.pyprocess
7
- import openpiv.tools
8
- import xarray as xr
9
-
10
- __all__ = [
11
- "get_openpiv",
12
- "piv",
13
- ]
14
-
15
-
16
- def get_openpiv(frames, y, x, dt, **kwargs):
17
- """Compute time-resolved Particle Image Velocimetry (PIV) using Fast Fourier Transform (FFT) within OpenPIV.
18
-
19
- Calculates velocity using the OpenPIV algorithms by processing sequential frames
20
- from a dataset and returning the velocity components, signal-to-noise ratio, and
21
- correlation values. The function shifts frames in time and applies the PIV algorithm
22
- to compute flow fields over the specified spatial axes.
23
-
24
- Parameters
25
- ----------
26
- frames : xarray.Dataset
27
- The input dataset containing time-dependent frames with coordinates.
28
- y : array-like
29
- The spatial coordinates along the y-axis where the outputs should be interpolated.
30
- x : array-like
31
- The spatial coordinates along the x-axis where the outputs should be interpolated.
32
- dt : float
33
- The time step between consecutive frames (used to go from per-frame to per-second displacement).
34
- **kwargs : dict
35
- Additional keyword arguments to be passed to the PIV function.
36
-
37
- Returns
38
- -------
39
- xarray.Dataset
40
- A dataset containing computed velocity components `v_x` and `v_y`,
41
- signal-to-noise ratios `s2n`, and correlation values `corr`. The dataset
42
- includes updated x and y coordinates representing the flow field grid.
43
-
44
- """
45
- # first get rid of coordinates that need to be recalculated
46
- coords_drop = list(set(frames.coords) - set(frames.dims))
47
- frames = frames.drop_vars(coords_drop)
48
- # get frames and shifted frames in time
49
- frames1 = frames.shift(time=1)[1:].chunk({"time": 1})
50
- frames2 = frames[1:].chunk({"time": 1})
51
- # retrieve all data arrays
52
- v_x, v_y, s2n, corr = xr.apply_ufunc(
53
- piv,
54
- frames1,
55
- frames2,
56
- dt,
57
- kwargs=kwargs,
58
- input_core_dims=[["y", "x"], ["y", "x"], []],
59
- output_core_dims=[["new_y", "new_x"]] * 4,
60
- dask_gufunc_kwargs={
61
- "output_sizes": {"new_y": len(y), "new_x": len(x)},
62
- },
63
- output_dtypes=[np.float32] * 4,
64
- vectorize=True,
65
- keep_attrs=True,
66
- dask="parallelized",
67
- )
68
- # merge all DataArrays in one Dataset
69
- ds = xr.merge([v_x.rename("v_x"), v_y.rename("v_y"), s2n.rename("s2n"), corr.rename("corr")]).rename(
70
- {"new_x": "x", "new_y": "y"}
71
- )
72
- # add y and x-axis values
73
- ds["y"] = y
74
- ds["x"] = x
75
- return ds
76
-
77
-
78
- def piv(
79
- frame_a,
80
- frame_b,
81
- dt,
82
- res_x=0.01,
83
- res_y=0.01,
84
- search_area_size=30,
85
- window_size=None,
86
- overlap=None,
87
- **kwargs,
88
- ):
89
- """Perform PIV analysis on two sequential frames following keyword arguments from openpiv.
90
-
91
- This function also computes the correlations per interrogation window, so that poorly correlated values can be
92
- filtered out. Furthermore, the resolution is used to convert pixel per second velocity estimates, into meter per
93
- second velocity estimates. The centre of search area columns and rows are also returned so that a georeferenced
94
- grid can be written from the results.
95
-
96
- Note: Typical openpiv kwargs are for instance
97
- window_size=60, overlap=30, search_area_size=60, dt=1./25
98
-
99
- Parameters
100
- ----------
101
- frame_a: np.ndarray (2D)
102
- first frame
103
- frame_b: np.ndarray (2D)
104
- second frame
105
- dt : float
106
- time resolution in seconds.
107
- res_x: float, optional
108
- resolution of x-dir pixels in a user-defined unit per pixel (e.g. m pixel-1) Default: 0.01
109
- res_y: float, optional
110
- resolution of y-dir pixels in a user-defined unit per pixel (e.g. m pixel-1) Default: 0.01
111
- search_area_size: int, optional
112
- length of subsetted matrix to search for correlations (default: 30)
113
- window_size: int, optional
114
- size of interrogation window in amount of pixels. If not set, it is set equal to search_area_size
115
- (default: None).
116
- overlap: int, optional
117
- length of overlap between interrogation windows. If not set, this defaults to 50% of the window_size parameter
118
- (default: None).
119
- **kwargs: dict
120
- keyword arguments related to openpiv. See openpiv manual for further information
121
-
122
- Returns
123
- -------
124
- v_x: np.ndarray(2D)
125
- raw x-dir velocities [m s-1] in interrogation windows (requires filtering to get valid velocities)
126
- v_y: np.ndarray (2D)
127
- raw y-dir velocities [m s-1] in interrogation windows (requires filtering to get valid velocities)
128
- s2n: np.ndarray (2D)
129
- signal to noise ratio, measured as maximum correlation found divided by the mean correlation
130
- (method="peak2mean") or second to maximum correlation (method="peak2peak") found within search area
131
- corr: np.ndarray (2D)
132
- correlation values in interrogation windows
133
-
134
- """
135
- window_size = search_area_size if window_size is None else window_size
136
- overlap = int(round(window_size) / 2) if overlap is None else overlap
137
- # modified version of extended_search_area_piv to accomodate exporting corr
138
- v_x, v_y, s2n, corr = extended_search_area_piv(
139
- frame_a, frame_b, dt=dt, search_area_size=search_area_size, overlap=overlap, window_size=window_size, **kwargs
140
- )
141
- return v_x * res_x, v_y * res_y, s2n, corr
142
-
143
-
144
- def extended_search_area_piv(
145
- frame_a: np.ndarray,
146
- frame_b: np.ndarray,
147
- window_size: int,
148
- overlap: int = 0,
149
- dt: float = 1.0,
150
- search_area_size: Optional[Union[Tuple[int, int], List[int], int]] = None,
151
- correlation_method: str = "circular",
152
- subpixel_method: str = "gaussian",
153
- sig2noise_method: Optional[str] = "peak2mean",
154
- width: int = 2,
155
- normalized_correlation: bool = True,
156
- use_vectorized: bool = False,
157
- ) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
158
- """Perform PIV cross-correlation analysis.
159
-
160
- Extended area search can be used to increased dynamic range. The search region
161
- in the second frame is larger than the interrogation window size in the
162
- first frame. For Cython implementation see
163
- openpiv.process.extended_search_area_piv
164
-
165
- This is a pure python implementation of the standard PIV cross-correlation
166
- algorithm. It is a zero order displacement predictor, and no iterative
167
- process is performed.
168
-
169
- Parameters
170
- ----------
171
- frame_a : 2d np.ndarray
172
- an two dimensions array of integers containing grey levels of
173
- the first frame.
174
-
175
- frame_b : 2d np.ndarray
176
- an two dimensions array of integers containing grey levels of
177
- the second frame.
178
-
179
- window_size : int
180
- the size of the (square) interrogation window, [default: 32 pix].
181
-
182
- overlap : int
183
- the number of pixels by which two adjacent windows overlap
184
- [default: 16 pix].
185
-
186
- dt : float
187
- the time delay separating the two frames [default: 1.0].
188
-
189
- correlation_method : string
190
- one of the two methods implemented: 'circular' or 'linear',
191
- default: 'circular', it's faster, without zero-padding
192
- 'linear' requires also normalized_correlation = True (see below)
193
-
194
- subpixel_method : string
195
- one of the following methods to estimate subpixel location of the
196
- peak:
197
- 'centroid' [replaces default if correlation map is negative],
198
- 'gaussian' [default if correlation map is positive],
199
- 'parabolic'.
200
-
201
- sig2noise_method : string
202
- defines the method of signal-to-noise-ratio measure,
203
- ('peak2peak' or 'peak2mean'. If None, no measure is performed.)
204
-
205
- width : int
206
- the half size of the region around the first
207
- correlation peak to ignore for finding the second
208
- peak. [default: 2]. Only used if ``sig2noise_method==peak2peak``.
209
-
210
- search_area_size : int
211
- the size of the interrogation window in the second frame,
212
- default is the same interrogation window size and it is a
213
- fallback to the simplest FFT based PIV
214
-
215
- normalized_correlation: bool
216
- if True, then the image intensity will be modified by removing
217
- the mean, dividing by the standard deviation and
218
- the correlation map will be normalized. It's slower but could be
219
- more robust
220
-
221
- use_vectorized : bool
222
- If set, vectorization is used to speed up analysis.
223
-
224
- Returns
225
- -------
226
- u : 2d np.ndarray
227
- a two dimensional array containing the u velocity component,
228
- in pixels/seconds.
229
-
230
- v : 2d np.ndarray
231
- a two dimensional array containing the v velocity component,
232
- in pixels/seconds.
233
-
234
- sig2noise : 2d np.ndarray ( optional: only if sig2noise_method != None )
235
- a two dimensional array the signal to noise ratio for each
236
- window pair.
237
-
238
- corr : 2d np.ndarray
239
- a two dimensional array with the maximum correlation values found in each interrogation window.
240
-
241
- The implementation of the one-step direct correlation with different
242
- size of the interrogation window and the search area. The increased
243
- size of the search areas cope with the problem of loss of pairs due
244
- to in-plane motion, allowing for a smaller interrogation window size,
245
- without increasing the number of outlier vectors.
246
-
247
- See:
248
-
249
- Particle-Imaging Techniques for Experimental Fluid Mechanics
250
-
251
- Annual Review of Fluid Mechanics
252
- Vol. 23: 261-304 (Volume publication date January 1991)
253
- DOI: 10.1146/annurev.fl.23.010191.001401
254
-
255
- originally implemented in process.pyx in Cython and converted to
256
- a NumPy vectorized solution in pyprocess.py
257
-
258
- """
259
- if search_area_size is not None:
260
- if isinstance(search_area_size, tuple) == False and isinstance(search_area_size, list) == False:
261
- search_area_size = [search_area_size, search_area_size]
262
- if isinstance(window_size, tuple) == False and isinstance(window_size, list) == False:
263
- window_size = [window_size, window_size]
264
- if isinstance(overlap, tuple) == False and isinstance(overlap, list) == False:
265
- overlap = [overlap, overlap]
266
-
267
- # check the inputs for validity
268
- search_area_size = window_size if search_area_size is None else search_area_size
269
-
270
- if overlap[0] >= window_size[0] or overlap[1] >= window_size[1]:
271
- raise ValueError("Overlap has to be smaller than the window_size")
272
-
273
- if search_area_size[0] < window_size[0] or search_area_size[1] < window_size[1]:
274
- raise ValueError("Search size cannot be smaller than the window_size")
275
-
276
- if (window_size[1] > frame_a.shape[0]) or (window_size[0] > frame_a.shape[1]):
277
- raise ValueError("window size cannot be larger than the image")
278
-
279
- # get field shape
280
- n_rows, n_cols = openpiv.pyprocess.get_field_shape(frame_a.shape, search_area_size, overlap)
281
-
282
- # We implement the new vectorized code
283
- aa = openpiv.pyprocess.sliding_window_array(frame_a, search_area_size, overlap)
284
- bb = openpiv.pyprocess.sliding_window_array(frame_b, search_area_size, overlap)
285
-
286
- # for the case of extended seearch, the window size is smaller than
287
- # the search_area_size. In order to keep it all vectorized the
288
- # approach is to use the interrogation window in both
289
- # frames of the same size of search_area_asize,
290
- # but mask out the region around
291
- # the interrogation window in the frame A
292
-
293
- if search_area_size > window_size:
294
- # before masking with zeros we need to remove
295
- # edges
296
-
297
- aa = openpiv.pyprocess.normalize_intensity(aa)
298
- bb = openpiv.pyprocess.normalize_intensity(bb)
299
-
300
- mask = np.zeros((search_area_size[0], search_area_size[1])).astype(aa.dtype)
301
- pady = int((search_area_size[0] - window_size[0]) / 2)
302
- padx = int((search_area_size[1] - window_size[1]) / 2)
303
- mask[slice(pady, search_area_size[0] - pady), slice(padx, search_area_size[1] - padx)] = 1
304
- mask = np.broadcast_to(mask, aa.shape)
305
- aa *= mask
306
-
307
- corr = openpiv.pyprocess.fft_correlate_images(
308
- aa, bb, correlation_method=correlation_method, normalized_correlation=normalized_correlation
309
- )
310
- if use_vectorized == True:
311
- u, v = openpiv.pyprocess.vectorized_correlation_to_displacements(
312
- corr, n_rows, n_cols, subpixel_method=subpixel_method
313
- )
314
- else:
315
- u, v = openpiv.pyprocess.correlation_to_displacement(corr, n_rows, n_cols, subpixel_method=subpixel_method)
316
-
317
- # return output depending if user wanted sig2noise information
318
- sig2noise = np.zeros_like(u) * np.nan
319
- if sig2noise_method is not None:
320
- if use_vectorized == True:
321
- sig2noise = openpiv.pyprocess.vectorized_sig2noise_ratio(
322
- corr, sig2noise_method=sig2noise_method, width=width
323
- )
324
- else:
325
- sig2noise = openpiv.pyprocess.sig2noise_ratio(corr, sig2noise_method=sig2noise_method, width=width)
326
-
327
- sig2noise = sig2noise.reshape(n_rows, n_cols)
328
- # extended code for exporting the maximum found value for corr
329
- corr = corr.max(axis=-1).max(axis=-1).reshape((n_rows, n_cols))
330
-
331
- return u / dt, v / dt, sig2noise, corr