oafuncs 0.0.98.7__py3-none-any.whl → 0.0.98.8__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.
oafuncs/oa_data.py CHANGED
@@ -13,9 +13,7 @@ SystemInfo: Windows 11
13
13
  Python Version: 3.11
14
14
  """
15
15
 
16
- import itertools
17
- import multiprocessing as mp
18
- from concurrent.futures import ThreadPoolExecutor
16
+
19
17
  from typing import Any, List, Union
20
18
 
21
19
  import numpy as np
@@ -24,6 +22,8 @@ import xarray as xr
24
22
  from rich import print
25
23
  from scipy.interpolate import griddata, interp1d
26
24
 
25
+ from oafuncs.oa_tool import PEx
26
+
27
27
  __all__ = ["interp_along_dim", "interp_2d", "ensure_list", "mask_shapefile"]
28
28
 
29
29
 
@@ -115,6 +115,15 @@ def interp_along_dim(
115
115
  return np.apply_along_axis(apply_interp_extrap, interpolation_axis, source_data)
116
116
 
117
117
 
118
+ def _interp_single_worker(*args):
119
+ """
120
+ 用于PEx并行的单slice插值worker,参数为(t, z, source_data, origin_points, target_points, interpolation_method, target_shape)
121
+ """
122
+ data_slice, origin_points, target_points, interpolation_method, target_shape = args
123
+
124
+ return griddata(origin_points, data_slice.ravel(), target_points, method=interpolation_method).reshape(target_shape)
125
+
126
+
118
127
  def interp_2d(
119
128
  target_x_coordinates: Union[np.ndarray, List[float]],
120
129
  target_y_coordinates: Union[np.ndarray, List[float]],
@@ -122,7 +131,6 @@ def interp_2d(
122
131
  source_y_coordinates: Union[np.ndarray, List[float]],
123
132
  source_data: np.ndarray,
124
133
  interpolation_method: str = "linear",
125
- use_parallel: bool = True,
126
134
  ) -> np.ndarray:
127
135
  """
128
136
  Perform 2D interpolation on the last two dimensions of a multi-dimensional array.
@@ -151,10 +159,6 @@ def interp_2d(
151
159
  >>> result = interp_2d(target_x_coordinates, target_y_coordinates, source_x_coordinates, source_y_coordinates, source_data)
152
160
  >>> print(result.shape) # Expected output: (3, 3)
153
161
  """
154
-
155
- def interp_single(data_slice: np.ndarray, target_points: np.ndarray, origin_points: np.ndarray, method: str) -> np.ndarray:
156
- return griddata(origin_points, data_slice.ravel(), target_points, method=method).reshape(target_y_coordinates.shape)
157
-
158
162
  if len(target_y_coordinates.shape) == 1:
159
163
  target_x_coordinates, target_y_coordinates = np.meshgrid(target_x_coordinates, target_y_coordinates)
160
164
  if len(source_y_coordinates.shape) == 1:
@@ -166,25 +170,30 @@ def interp_2d(
166
170
  target_points = np.column_stack((np.array(target_y_coordinates).ravel(), np.array(target_x_coordinates).ravel()))
167
171
  origin_points = np.column_stack((np.array(source_y_coordinates).ravel(), np.array(source_x_coordinates).ravel()))
168
172
 
169
- if use_parallel:
170
- with ThreadPoolExecutor(max_workers=mp.cpu_count() - 2) as executor:
171
- if len(source_data.shape) == 2:
172
- interpolated_data = list(executor.map(interp_single, [source_data], [target_points], [origin_points], [interpolation_method]))
173
- elif len(source_data.shape) == 3:
174
- interpolated_data = list(executor.map(interp_single, [source_data[i] for i in range(source_data.shape[0])], [target_points] * source_data.shape[0], [origin_points] * source_data.shape[0], [interpolation_method] * source_data.shape[0]))
175
- elif len(source_data.shape) == 4:
176
- index_combinations = list(itertools.product(range(source_data.shape[0]), range(source_data.shape[1])))
177
- interpolated_data = list(executor.map(interp_single, [source_data[i, j] for i, j in index_combinations], [target_points] * len(index_combinations), [origin_points] * len(index_combinations), [interpolation_method] * len(index_combinations)))
178
- interpolated_data = np.array(interpolated_data).reshape(source_data.shape[0], source_data.shape[1], *target_y_coordinates.shape)
179
- else:
180
- if len(source_data.shape) == 2:
181
- interpolated_data = interp_single(source_data, target_points, origin_points, interpolation_method)
182
- elif len(source_data.shape) == 3:
183
- interpolated_data = np.stack([interp_single(source_data[i], target_points, origin_points, interpolation_method) for i in range(source_data.shape[0])])
184
- elif len(source_data.shape) == 4:
185
- interpolated_data = np.stack([np.stack([interp_single(source_data[i, j], target_points, origin_points, interpolation_method) for j in range(source_data.shape[1])]) for i in range(source_data.shape[0])])
186
-
187
- return np.squeeze(np.array(interpolated_data))
173
+ data_dims = len(source_data.shape)
174
+ # Ensure source_data is 4D for consistent processing (t, z, y, x)
175
+ if data_dims < 2:
176
+ raise ValueError(f"[red]Source data must have at least 2 dimensions, but got {data_dims}.[/red]")
177
+ elif data_dims > 4:
178
+ # Or handle cases with more than 4 dimensions if necessary
179
+ raise ValueError(f"[red]Source data has {data_dims} dimensions, but this function currently supports only up to 4.[/red]")
180
+
181
+ # Reshape to 4D by adding leading dimensions of size 1 if needed
182
+ num_dims_to_add = 4 - data_dims
183
+ new_shape = (1,) * num_dims_to_add + source_data.shape
184
+ new_src_data = source_data.reshape(new_shape)
185
+
186
+ t, z, _, _ = new_src_data.shape
187
+
188
+ paras = []
189
+ target_shape = target_y_coordinates.shape
190
+ for t_index in range(t):
191
+ for z_index in range(z):
192
+ paras.append((new_src_data[t_index, z_index], origin_points, target_points, interpolation_method, target_shape))
193
+ excutor = PEx()
194
+ result = excutor.run(_interp_single_worker, paras)
195
+
196
+ return np.squeeze(np.array(result))
188
197
 
189
198
 
190
199
  def mask_shapefile(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: oafuncs
3
- Version: 0.0.98.7
3
+ Version: 0.0.98.8
4
4
  Summary: Oceanic and Atmospheric Functions
5
5
  Home-page: https://github.com/Industry-Pays/OAFuncs
6
6
  Author: Kun Liu
@@ -1,6 +1,6 @@
1
1
  oafuncs/__init__.py,sha256=T_-VtnWWllV3Q91twT5Yt2sUapeA051QbPNnBxmg9nw,1456
2
2
  oafuncs/oa_cmap.py,sha256=DimWT4Bg7uE5Lx8hSw1REp7whpsR2pFRStAwk1cowEM,11494
3
- oafuncs/oa_data.py,sha256=0AbQ8_7vf9ecZaui6hmUjubkWRJxs4TGcdhJaPdbmP8,10958
3
+ oafuncs/oa_data.py,sha256=7heyoFOBt_xqe0YSiUdO6tOpmySm0FuG1fHSoAO1NJI,10271
4
4
  oafuncs/oa_date.py,sha256=KqU-bHtC74hYsf6VgiA3i2vI__q_toOVR-whFy4cYP8,5523
5
5
  oafuncs/oa_draw.py,sha256=Wj2QBgyIPpV_dxaDrH10jqj_puK9ZM9rd-si-3VrsrE,17631
6
6
  oafuncs/oa_file.py,sha256=goF5iRXJFFCIKhIjlkCnYYt0EYlJb_4r8AeYNZ0-SOk,16209
@@ -37,8 +37,8 @@ oafuncs/oa_sign/__init__.py,sha256=QKqTFrJDFK40C5uvk48GlRRbGFzO40rgkYwu6dYxatM,5
37
37
  oafuncs/oa_sign/meteorological.py,sha256=8091SHo2L8kl4dCFmmSH5NGVHDku5i5lSiLEG5DLnOQ,6489
38
38
  oafuncs/oa_sign/ocean.py,sha256=xrW-rWD7xBWsB5PuCyEwQ1Q_RDKq2KCLz-LOONHgldU,5932
39
39
  oafuncs/oa_sign/scientific.py,sha256=a4JxOBgm9vzNZKpJ_GQIQf7cokkraV5nh23HGbmTYKw,5064
40
- oafuncs-0.0.98.7.dist-info/licenses/LICENSE.txt,sha256=rMtLpVg8sKiSlwClfR9w_Dd_5WubTQgoOzE2PDFxzs4,1074
41
- oafuncs-0.0.98.7.dist-info/METADATA,sha256=HotYRm4-mVHvJqg_pe71FgQpZ7mYKjd6WGr2PBbIeJY,4272
42
- oafuncs-0.0.98.7.dist-info/WHEEL,sha256=lTU6B6eIfYoiQJTZNc-fyaR6BpL6ehTzU3xGYxn2n8k,91
43
- oafuncs-0.0.98.7.dist-info/top_level.txt,sha256=bgC35QkXbN4EmPHEveg_xGIZ5i9NNPYWqtJqaKqTPsQ,8
44
- oafuncs-0.0.98.7.dist-info/RECORD,,
40
+ oafuncs-0.0.98.8.dist-info/licenses/LICENSE.txt,sha256=rMtLpVg8sKiSlwClfR9w_Dd_5WubTQgoOzE2PDFxzs4,1074
41
+ oafuncs-0.0.98.8.dist-info/METADATA,sha256=Le6ieydYuvZciK8CaFHPBUy47T3geyA48Z6nzSbLMwQ,4272
42
+ oafuncs-0.0.98.8.dist-info/WHEEL,sha256=lTU6B6eIfYoiQJTZNc-fyaR6BpL6ehTzU3xGYxn2n8k,91
43
+ oafuncs-0.0.98.8.dist-info/top_level.txt,sha256=bgC35QkXbN4EmPHEveg_xGIZ5i9NNPYWqtJqaKqTPsQ,8
44
+ oafuncs-0.0.98.8.dist-info/RECORD,,