oafuncs 0.0.89__py2.py3-none-any.whl → 0.0.91__py2.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.
Binary file
oafuncs/oa_data.py CHANGED
@@ -15,13 +15,14 @@ Python Version: 3.11
15
15
 
16
16
  import itertools
17
17
  import multiprocessing as mp
18
- from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
18
+ from concurrent.futures import ThreadPoolExecutor
19
19
 
20
20
  import numpy as np
21
- from scipy.interpolate import griddata
22
21
  from rich import print
22
+ from scipy.interpolate import griddata
23
23
 
24
- __all__ = ["interp_2d","ParallelExecutor"]
24
+
25
+ __all__ = ["interp_2d"]
25
26
 
26
27
 
27
28
  def interp_2d(target_x, target_y, origin_x, origin_y, data, method="linear", parallel=True):
@@ -91,70 +92,7 @@ def interp_2d(target_x, target_y, origin_x, origin_y, data, method="linear", par
91
92
  return np.array(interpolated_data)
92
93
 
93
94
 
94
- class ParallelExecutor:
95
- """
96
- 通用并行计算类,支持多进程和多线程模式。
97
-
98
- 使用说明:
99
- 1. 创建实例时选择模式:
100
- - mode="process" 使用多进程(适合 CPU 密集型任务)。
101
- - mode="thread" 使用多线程(适合 IO 密集型任务)。
102
-
103
- 2. 调用 run 方法:
104
- - 参数 func:需要并行执行的函数。
105
- - 参数 param_list:参数列表,每个元素是传递给 func 的参数元组。
106
-
107
- 示例:
108
- # 示例 1:计算平方
109
- def compute_square(x):
110
- return x * x
111
-
112
- params = [(i,) for i in range(10)]
113
- executor = ParallelExecutor(mode="process", max_workers=4)
114
- results = executor.run(compute_square, params)
115
- print("Results:", results)
116
-
117
- # 示例 2:计算两数之和
118
- def compute_sum(a, b):
119
- return a + b
120
-
121
- params = [(1, 2), (3, 4), (5, 6)]
122
- executor = ParallelExecutor(mode="thread", max_workers=2)
123
- results = executor.run(compute_sum, params)
124
- print("Results:", results)
125
-
126
- 参数:
127
- mode (str): 并行模式,"process" 表示多进程,"thread" 表示多线程。
128
- max_workers (int): 最大并行工作数,默认为 CPU 核心数减 2。
129
- """
130
-
131
- def __init__(self, mode="process", max_workers=mp.cpu_count() - 2):
132
- self.mode = mode
133
- self.max_workers = max_workers
134
- self.executor = ProcessPoolExecutor if mode == "process" else ThreadPoolExecutor
135
-
136
- def run(self, func, param_list):
137
- """
138
- 并行运行指定函数,并确保结果顺序与输入参数顺序一致。
139
95
 
140
- 参数:
141
- func (callable): 需要并行执行的函数。
142
- param_list (list): 参数列表,每个元素是传递给 func 的参数元组。
143
-
144
- 返回:
145
- results (list): 按输入顺序返回的结果。
146
- """
147
- results = [None] * len(param_list) # 预分配结果数组
148
-
149
- with self.executor(max_workers=self.max_workers) as executor:
150
- # 提交任务并保存其索引
151
- future_to_index = {executor.submit(func, *params): idx for idx, params in enumerate(param_list)}
152
-
153
- for future in future_to_index:
154
- idx = future_to_index[future] # 获取原始索引
155
- results[idx] = future.result() # 将结果存放到对应位置
156
-
157
- return results
158
96
 
159
97
 
160
98
  # ---------------------------------------------------------------------------------- not used below ----------------------------------------------------------------------------------
@@ -203,7 +141,7 @@ def interp_2d_20241213(target_x, target_y, origin_x, origin_y, data, method="lin
203
141
  for i in range(dims[0]):
204
142
  dt = griddata(origin_points, np.ravel(data[i, :, :]), target_points, method=method)
205
143
  interpolated_data.append(np.reshape(dt, target_y.shape))
206
- print(f"Interpolating {i+1}/{dims[0]}...")
144
+ print(f"Interpolating {i + 1}/{dims[0]}...")
207
145
  interpolated_data = np.array(interpolated_data)
208
146
  elif len_dims == 4:
209
147
  interpolated_data = []
@@ -212,7 +150,7 @@ def interp_2d_20241213(target_x, target_y, origin_x, origin_y, data, method="lin
212
150
  for j in range(dims[1]):
213
151
  dt = griddata(origin_points, np.ravel(data[i, j, :, :]), target_points, method=method)
214
152
  interpolated_data[i].append(np.reshape(dt, target_y.shape))
215
- print(f"\rInterpolating {i*dims[1]+j+1}/{dims[0]*dims[1]}...", end="")
153
+ print(f"\rInterpolating {i * dims[1] + j + 1}/{dims[0] * dims[1]}...", end="")
216
154
  print("\n")
217
155
  interpolated_data = np.array(interpolated_data)
218
156
 
@@ -270,7 +208,7 @@ def interp_2d_parallel_20241213(target_x, target_y, origin_x, origin_y, data, me
270
208
 
271
209
  # 使用多线程进行插值
272
210
  with ThreadPoolExecutor(max_workers=mp.cpu_count() - 2) as executor:
273
- print(f"Using {mp.cpu_count()-2} threads...")
211
+ print(f"Using {mp.cpu_count() - 2} threads...")
274
212
  if len_dims == 2:
275
213
  interpolated_data = list(executor.map(interp_single2d, [target_y], [target_x], [origin_y], [origin_x], [data], [method]))
276
214
  elif len_dims == 3:
@@ -296,23 +234,12 @@ def interp_2d_parallel_20241213(target_x, target_y, origin_x, origin_y, data, me
296
234
  return interpolated_data
297
235
 
298
236
 
299
- def _test_sum(a,b):
300
- return a+b
237
+ def _test_sum(a, b):
238
+ return a + b
301
239
 
302
240
 
303
241
  if __name__ == "__main__":
304
- # 参数列表:每个参数是元组
305
- params_list = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]
306
-
307
- # 创建并行执行器
308
- executor = ParallelExecutor()
309
-
310
- # 并行运行
311
- results = executor.run(_test_sum, params_list)
312
242
 
313
- # 验证结果顺序
314
- print("Params:", params_list)
315
- print("Results:", results)
316
243
  pass
317
244
  """ import time
318
245
 
@@ -19,3 +19,4 @@ Python Version: 3.11
19
19
  from .hycom_3hourly import *
20
20
  from .literature import *
21
21
  from .user_agent import *
22
+ from .idm import *