cohere-core 4.3__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,388 @@
1
+ # #########################################################################
2
+ # Copyright (c) , UChicago Argonne, LLC. All rights reserved. #
3
+ # #
4
+ # See LICENSE file. #
5
+ # #########################################################################
6
+
7
+ from cohere_core.lib.cohlib import cohlib
8
+ import cupy as cp
9
+ import numpy as np
10
+ import cupyx.scipy.stats as stats
11
+ import cupyx.scipy.ndimage as sc
12
+ import cupyx.scipy.special as sp
13
+ import cupyx.scipy.signal as sig
14
+ import cupyx.scipy.fft as fft
15
+
16
+
17
+ class cplib(cohlib):
18
+ @staticmethod
19
+ def array(obj):
20
+ return cp.array(obj)
21
+
22
+ @staticmethod
23
+ def dot(arr1, arr2):
24
+ return cp.dot(arr1, arr2)
25
+
26
+ @staticmethod
27
+ def cross(arr1, arr2):
28
+ return cp.cross(arr1, arr2)
29
+
30
+ @staticmethod
31
+ def set_device(dev_id):
32
+ if dev_id != -1:
33
+ cp.cuda.Device(dev_id).use()
34
+
35
+ @staticmethod
36
+ def set_backend(proc):
37
+ pass
38
+
39
+ @staticmethod
40
+ def to_numpy(arr):
41
+ return cp.asnumpy(arr)
42
+
43
+ @staticmethod
44
+ def from_numpy(arr):
45
+ return cp.array(arr)
46
+
47
+ @staticmethod
48
+ def save(filename, arr):
49
+ cp.save(filename, arr)
50
+
51
+ @staticmethod
52
+ def load(filename):
53
+ return cp.load(filename, allow_pickle=True)
54
+
55
+ @staticmethod
56
+ def dtype(arr):
57
+ return arr.dtype
58
+
59
+ @staticmethod
60
+ def astype(arr, dtype):
61
+ return arr.astype(dtype=dtype)
62
+
63
+ @staticmethod
64
+ def reshape(arr, shape):
65
+ return cp.reshape(arr, shape)
66
+
67
+ @staticmethod
68
+ def size(arr):
69
+ return arr.size
70
+
71
+ @staticmethod
72
+ def next_fast_len(target):
73
+ return fft.next_fast_len(target)
74
+
75
+ @staticmethod
76
+ def hasnan(arr):
77
+ return cp.any(cp.isnan(arr))
78
+
79
+ @staticmethod
80
+ def nan_to_num(arr, **kwargs):
81
+ return cp.nan_to_num(arr, **kwargs)
82
+
83
+ @staticmethod
84
+ def copy(arr):
85
+ return cp.copy(arr)
86
+
87
+ @staticmethod
88
+ def random(shape, **kwargs):
89
+ import time
90
+ import os
91
+
92
+ seed = np.array([time.time() * 10000 * os.getpid(), os.getpid()])
93
+ rs = cp.random.RandomState(seed=seed)
94
+ return cp.random.random(shape, dtype=cp.float32) + 1j * cp.random.random(shape, dtype=cp.float32)
95
+
96
+ @staticmethod
97
+ def roll(arr, sft, axis=None):
98
+ if axis is None:
99
+ axis = list(range(len(sft)))
100
+ if type(sft) != list:
101
+ sft = [sft]
102
+ sft = [int(s) for s in sft]
103
+ return cp.roll(arr, sft, axis=axis)
104
+
105
+ @staticmethod
106
+ def shift(arr, sft):
107
+ return sc.fourier_shift(arr, sft)
108
+
109
+ @staticmethod
110
+ def fftshift(arr):
111
+ return fft.fftshift(arr)
112
+
113
+ @staticmethod
114
+ def ifftshift(arr):
115
+ return fft.ifftshift(arr)
116
+
117
+ @staticmethod
118
+ def fft(arr, norm='forward'):
119
+ return fft.fftn(arr, norm=norm)
120
+
121
+ @staticmethod
122
+ def ifft(arr, norm='forward'):
123
+ return fft.ifftn(arr, norm=norm)
124
+
125
+ @staticmethod
126
+ def fftconvolve(arr1, kernel):
127
+ return sig.fftconvolve(arr1, kernel, mode='same')
128
+
129
+ @staticmethod
130
+ def correlate(arr1, arr2, mode='same', method='fft'):
131
+ return sig.correlate(arr1, arr2, mode, method)
132
+
133
+ @staticmethod
134
+ def where(cond, x, y):
135
+ return cp.where(cond, x, y)
136
+
137
+ @staticmethod
138
+ def dims(arr):
139
+ # get array dimensions
140
+ return arr.shape
141
+
142
+ @staticmethod
143
+ def absolute(arr):
144
+ return cp.absolute(arr)
145
+
146
+ @staticmethod
147
+ def sqrt(arr):
148
+ return cp.sqrt(arr)
149
+
150
+ @staticmethod
151
+ def square(arr):
152
+ return cp.square(arr)
153
+
154
+ @staticmethod
155
+ def sum(arr, axis=None):
156
+ sm = cp.sum(arr, axis)
157
+ # if axis is None:
158
+ # return sm.tolist()
159
+ return sm
160
+
161
+ @staticmethod
162
+ def real(arr):
163
+ return cp.real(arr)
164
+
165
+ @staticmethod
166
+ def imag(arr):
167
+ return cp.imag(arr)
168
+
169
+ @staticmethod
170
+ def amax(arr):
171
+ return cp.amax(arr)
172
+
173
+ @staticmethod
174
+ def amin(arr):
175
+ return cp.amin(arr)
176
+
177
+ @staticmethod
178
+ def argmax(arr, axis=None):
179
+ return cp.argmax(arr, axis)
180
+
181
+ @staticmethod
182
+ def argmin(arr, axis=None):
183
+ return cp.argmin(arr, axis)
184
+
185
+ @staticmethod
186
+ def unravel_index(indices, shape):
187
+ return cp.unravel_index(indices, shape)
188
+
189
+ @staticmethod
190
+ def ravel(arr):
191
+ return cp.ravel(arr)
192
+
193
+ @staticmethod
194
+ def maximum(arr1, arr2):
195
+ return cp.maximum(arr1, arr2)
196
+
197
+ @staticmethod
198
+ def ceil(arr):
199
+ return cp.ceil(arr)
200
+
201
+ @staticmethod
202
+ def fix(arr):
203
+ return cp.fix(arr)
204
+
205
+ @staticmethod
206
+ def round(val):
207
+ return cp.round(val)
208
+
209
+ @staticmethod
210
+ def print(arr, **kwargs):
211
+ print(arr)
212
+
213
+ @staticmethod
214
+ def angle(arr):
215
+ return cp.angle(arr)
216
+
217
+ @staticmethod
218
+ def flip(arr, axis=None):
219
+ return cp.flip(arr, axis)
220
+
221
+ @staticmethod
222
+ def tile(arr, rep):
223
+ return cp.tile(arr, rep)
224
+
225
+ @staticmethod
226
+ def full(shape, fill_value, **kwargs):
227
+ return cp.full(shape, fill_value)
228
+
229
+ @staticmethod
230
+ def expand_dims(arr, axis):
231
+ return cp.expand_dims(arr, axis)
232
+
233
+ @staticmethod
234
+ def squeeze(arr):
235
+ return cp.squeeze(arr)
236
+
237
+ @staticmethod
238
+ def entropy(arr):
239
+ return stats.entropy(arr)
240
+
241
+ @staticmethod
242
+ def gaussian_filter(arr, sigma, **kwargs):
243
+ return sc.gaussian_filter(arr, sigma, **kwargs)
244
+
245
+ @staticmethod
246
+ def median_filter(arr, size, **kwargs):
247
+ return sc.median_filter(arr, size)
248
+
249
+ @staticmethod
250
+ def uniform_filter(arr, size, **kwargs):
251
+ return sc.uniform_filter(arr, size)
252
+
253
+ @staticmethod
254
+ def binary_erosion(arr, **kwargs):
255
+ return sc.binary_erosion(arr, iterations=1)
256
+
257
+ @staticmethod
258
+ def center_of_mass(inarr):
259
+ t = sc.center_of_mass(cp.absolute(inarr))
260
+ return t
261
+
262
+ @staticmethod
263
+ def meshgrid(*xi):
264
+ return cp.meshgrid(*xi)
265
+
266
+ @staticmethod
267
+ def exp(arr):
268
+ return cp.exp(arr)
269
+
270
+ @staticmethod
271
+ def conj(arr):
272
+ return cp.conj(arr)
273
+
274
+ @staticmethod
275
+ def array_equal(arr1, arr2):
276
+ return cp.array_equal(arr1, arr2)
277
+
278
+ @staticmethod
279
+ def cos(arr):
280
+ return cp.cos(arr)
281
+
282
+ @staticmethod
283
+ def linspace(start, stop, num):
284
+ return cp.linspace(start, stop, num)
285
+
286
+ @staticmethod
287
+ def geomspace(start, stop, num):
288
+ return cp.geomspace(start, stop, num)
289
+
290
+ @staticmethod
291
+ def clip(arr, min, max=None):
292
+ return cp.clip(arr, min, max)
293
+
294
+ @staticmethod
295
+ def diff(arr, axis=None, prepend=0):
296
+ return cp.diff(arr, axis=axis, prepend=prepend)
297
+
298
+ @staticmethod
299
+ def gradient(arr, dx=1):
300
+ return cp.gradient(arr, dx)
301
+
302
+ @staticmethod
303
+ def take_along_axis(a, indices, axis):
304
+ return cp.take_along_axis(a, indices, axis)
305
+
306
+ @staticmethod
307
+ def moveaxis(arr, source, dest):
308
+ return cp.moveaxis(arr, source, dest)
309
+
310
+ @staticmethod
311
+ def lstsq(A, B):
312
+ return cp.linalg.lstsq(A, B, rcond=None)
313
+
314
+ @staticmethod
315
+ def zeros(shape):
316
+ return cp.zeros(shape)
317
+
318
+ @staticmethod
319
+ def indices(dims):
320
+ return cp.indices(dims)
321
+
322
+ @staticmethod
323
+ def concatenate(tup, axis=0):
324
+ return cp.concatenate(tup, axis)
325
+
326
+ @staticmethod
327
+ def stack(tup):
328
+ return cp.stack(tup)
329
+
330
+ @staticmethod
331
+ def affine_transform(arr, matrix, order=3, offset=0):
332
+ return sc.affine_transform(arr, matrix, order=order, offset=offset, prefilter=True)
333
+
334
+ @staticmethod
335
+ def pad(arr, padding):
336
+ return cp.pad(arr, padding)
337
+
338
+ @staticmethod
339
+ def histogram2d(arr1, arr2, bins):
340
+ return cp.histogram2d(cp.ravel(arr1), cp.ravel(arr2), bins)[0]
341
+
342
+ # @staticmethod
343
+ # def calc_nmi(hgram):
344
+ # h0 = stats.entropy(cp.sum(hgram, axis=0))
345
+ # h1 = stats.entropy(cp.sum(hgram, axis=1))
346
+ # h01 = stats.entropy(cp.reshape(hgram, -1))
347
+ # return (h0 + h1) / h01
348
+ #
349
+ @staticmethod
350
+ def log(arr):
351
+ return cp.log(arr)
352
+
353
+ @staticmethod
354
+ def log10(arr):
355
+ return cp.log10(arr)
356
+
357
+ @staticmethod
358
+ def xlogy(x, y=None):
359
+ if y is None:
360
+ y = x
361
+ return sp.xlogy(x, y)
362
+
363
+ @staticmethod
364
+ def mean(arr):
365
+ return cp.mean(arr)
366
+
367
+ @staticmethod
368
+ def median(arr):
369
+ return cp.median(arr)
370
+
371
+ # @staticmethod
372
+ # def calc_ehd(hgram):
373
+ # n = hgram.shape[0] * 1j
374
+ # x, y = cp.mgrid[0:1:n, 0:1:n]
375
+ # return cp.sum(hgram * cp.abs(x - y)) / cp.sum(hgram)
376
+ #
377
+ # @staticmethod
378
+ # def integrate_jacobian(jacobian, dx=1):
379
+ # nx, ny, nz, _, _ = jacobian.shape
380
+ # u = cp.zeros((nx, ny, nz, 3))
381
+ # for ax in range(3):
382
+ # u = u + dx * cp.cumsum(jacobian[:, :, :, ax, :], axis=ax)
383
+ # return u
384
+ #
385
+ @staticmethod
386
+ def clean_default_mem():
387
+ cp._default_memory_pool.free_all_blocks()
388
+ cp._default_pinned_memory_pool.free_all_blocks()