foscat 3.7.3__py3-none-any.whl → 3.8.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.
foscat/BkNumpy.py ADDED
@@ -0,0 +1,374 @@
1
+ import foscat.BkBase as BackendBase
2
+ import numpy as np
3
+
4
+ class BkNumpy(BackendBase.BackendBase):
5
+
6
+ def __init__(self, *args, **kwargs):
7
+ # Impose que use_2D=True pour la classe scat
8
+ super().__init__(name='tensorflow', *args, **kwargs)
9
+
10
+ # ===========================================================================
11
+ # INIT
12
+
13
+ self.backend = np
14
+ import scipy as scipy
15
+
16
+ self.scipy = scipy
17
+
18
+ self.float64 = self.backend.float64
19
+ self.float32 = self.backend.float32
20
+ self.int64 = self.backend.int64
21
+ self.int32 = self.backend.int32
22
+ self.complex64 = self.backend.complex128
23
+ self.complex128 = self.backend.complex64
24
+
25
+ if self.all_type == "float32":
26
+ self.all_bk_type = self.backend.float32
27
+ self.all_cbk_type = self.backend.complex64
28
+ else:
29
+ if self.all_type == "float64":
30
+ self.all_type = "float64"
31
+ self.all_bk_type = self.backend.float64
32
+ self.all_cbk_type = self.backend.complex128
33
+ else:
34
+ print("ERROR INIT FOCUS ", all_type, " should be float32 or float64")
35
+ return None
36
+
37
+ # ===========================================================================
38
+ # INIT
39
+
40
+ gpus = []
41
+ gpuname = "CPU:0"
42
+ self.gpulist = {}
43
+ self.gpulist[0] = gpuname
44
+ self.ngpu = 1
45
+
46
+ # ---------------------------------------------−---------
47
+ # -- BACKEND DEFINITION --
48
+ # ---------------------------------------------−---------
49
+ def bk_SparseTensor(self, indice, w, dense_shape=[]):
50
+ return self.scipy.sparse.coo_matrix(
51
+ (w, (indice[:, 0], indice[:, 1])), shape=dense_shape
52
+ )
53
+
54
+ def bk_stack(self, list, axis=0):
55
+ return self.backend.stack(list, axis=axis)
56
+
57
+ def bk_sparse_dense_matmul(self, smat, mat):
58
+ return smat.dot(mat)
59
+
60
+ def conv2d(self, x, w, strides=[1, 1, 1, 1], padding="SAME"):
61
+ res = np.zeros(
62
+ [x.shape[0], x.shape[1], x.shape[2], w.shape[3]], dtype=x.dtype
63
+ )
64
+ for k in range(w.shape[2]):
65
+ for l_orient in range(w.shape[3]):
66
+ for j in range(res.shape[0]):
67
+ tmp = self.scipy.signal.convolve2d(
68
+ x[j, :, :, k],
69
+ w[:, :, k, l_orient],
70
+ mode="same",
71
+ boundary="symm",
72
+ )
73
+ res[j, :, :, l_orient] += tmp
74
+ del tmp
75
+ return res
76
+
77
+ def conv1d(self, x, w, strides=[1, 1, 1], padding="SAME"):
78
+ res = np.zeros([x.shape[0], x.shape[1], w.shape[2]], dtype=x.dtype)
79
+ for k in range(w.shape[2]):
80
+ for j in range(res.shape[0]):
81
+ tmp = self.scipy.signal.convolve1d(
82
+ x[j, :, k], w[:, k], mode="same", boundary="symm"
83
+ )
84
+ res[j, :, :] += tmp
85
+ del tmp
86
+ return res
87
+
88
+ def bk_threshold(self, x, threshold, greater=True):
89
+
90
+ return (x > threshold) * x
91
+
92
+ def bk_maximum(self, x1, x2):
93
+ return x1 * (x1 > x2) + x2 * (x2 > x1)
94
+
95
+ def bk_device(self, device_name):
96
+ return self.backend.device(device_name)
97
+
98
+ def bk_ones(self, shape, dtype=None):
99
+ if dtype is None:
100
+ dtype = self.all_type
101
+ return self.backend.ones(shape, dtype=dtype)
102
+
103
+ def bk_conv1d(self, x, w):
104
+ res = np.zeros([x.shape[0], x.shape[1], w.shape[1]], dtype=x.dtype)
105
+ for k in range(w.shape[1]):
106
+ for l_orient in range(w.shape[2]):
107
+ res[:, :, l_orient] += self.scipy.ndimage.convolve1d(
108
+ x[:, :, k], w[:, k, l_orient], axis=1, mode="constant", cval=0.0
109
+ )
110
+ return res
111
+
112
+ def bk_flattenR(self, x):
113
+ if self.bk_is_complex(x):
114
+ return np.concatenate([x.real.flatten(), x.imag.flatten()], 0)
115
+ else:
116
+ return x.flatten()
117
+
118
+
119
+ def bk_flatten(self, x):
120
+ return x.flatten()
121
+
122
+ def bk_flatten(self, x):
123
+ return x.flatten()
124
+
125
+ def bk_size(self, x):
126
+ return x.size
127
+
128
+ def bk_resize_image(self, x, shape):
129
+ return self.bk_cast(self.backend.image.resize(x, shape, method="bilinear"))
130
+
131
+ def bk_L1(self, x):
132
+ return self.backend.sign(x) * self.backend.sqrt(self.backend.sign(x) * x)
133
+
134
+ def bk_square_comp(self, x):
135
+ return x * x
136
+
137
+ def bk_reduce_sum(self, data, axis=None):
138
+
139
+ if axis is None:
140
+ return np.sum(data)
141
+ else:
142
+ return np.sum(data, axis)
143
+
144
+ # ---------------------------------------------−---------
145
+ # return a tensor size
146
+
147
+ def bk_size(self, data):
148
+ return data.size
149
+
150
+
151
+ def bk_reduce_mean(self, data, axis=None):
152
+
153
+ if axis is None:
154
+ return np.mean(data)
155
+ else:
156
+ return np.mean(data, axis)
157
+
158
+ def bk_reduce_min(self, data, axis=None):
159
+
160
+ if axis is None:
161
+ return np.min(data)
162
+ else:
163
+ return np.min(data, axis)
164
+
165
+ def bk_random_seed(self, value):
166
+
167
+ return np.random.seed(value)
168
+
169
+ def bk_random_uniform(self, shape):
170
+
171
+ return np.random.rand(shape)
172
+
173
+ def bk_reduce_std(self, data, axis=None):
174
+ if axis is None:
175
+ r = np.std(data)
176
+ return self.bk_complex(r, 0 * r)
177
+ else:
178
+ r = np.std(data, axis)
179
+ if self.bk_is_complex(data):
180
+ return self.bk_complex(r, 0 * r)
181
+ else:
182
+ return r
183
+
184
+ def bk_sqrt(self, data):
185
+
186
+ return self.backend.sqrt(self.backend.abs(data))
187
+
188
+ def bk_abs(self, data):
189
+ return self.backend.abs(data)
190
+
191
+ def bk_is_complex(self, data):
192
+
193
+ return data.dtype == "complex64" or data.dtype == "complex128"
194
+
195
+ def bk_distcomp(self, data):
196
+ if self.bk_is_complex(data):
197
+ res = self.bk_square(self.bk_real(data)) + self.bk_square(
198
+ self.bk_imag(data)
199
+ )
200
+ return res
201
+ else:
202
+ return self.bk_square(data)
203
+
204
+ def bk_norm(self, data):
205
+ if self.bk_is_complex(data):
206
+ res = self.bk_square(self.bk_real(data)) + self.bk_square(
207
+ self.bk_imag(data)
208
+ )
209
+ return self.bk_sqrt(res)
210
+
211
+ else:
212
+ return self.bk_abs(data)
213
+
214
+ def bk_square(self, data):
215
+
216
+ return data * data
217
+
218
+ def bk_log(self, data):
219
+ return np.log(data)
220
+
221
+ def bk_matmul(self, a, b):
222
+ return np.dot(a, b)
223
+
224
+ def bk_tensor(self, data):
225
+ return data
226
+
227
+ def bk_shape_tensor(self, shape):
228
+ return np.zeros(shape)
229
+
230
+ def bk_complex(self, real, imag):
231
+ return real + 1j * imag
232
+
233
+ def bk_exp(self, data):
234
+
235
+ return self.backend.exp(data)
236
+
237
+ def bk_min(self, data):
238
+
239
+ return self.backend.reduce_min(data)
240
+
241
+ def bk_argmin(self, data):
242
+
243
+ return self.backend.argmin(data)
244
+
245
+ def bk_tanh(self, data):
246
+
247
+ return self.backend.math.tanh(data)
248
+
249
+ def bk_max(self, data):
250
+
251
+ return self.backend.reduce_max(data)
252
+
253
+ def bk_argmax(self, data):
254
+
255
+ return self.backend.argmax(data)
256
+
257
+ def bk_reshape(self, data, shape):
258
+
259
+ return self.backend.reshape(data, shape)
260
+
261
+ def bk_repeat(self, data, nn, axis=0):
262
+ return self.backend.repeat(data, nn, axis=axis)
263
+
264
+ def bk_tile(self, data, nn, axis=0):
265
+
266
+ return self.backend.tile(data, nn)
267
+
268
+ def bk_roll(self, data, nn, axis=0):
269
+ return self.backend.roll(data, nn, axis=axis)
270
+
271
+ def bk_expand_dims(self, data, axis=0):
272
+ return np.expand_dims(data, axis)
273
+
274
+ def bk_transpose(self, data, thelist):
275
+ return np.transpose(data, thelist)
276
+
277
+ def bk_concat(self, data, axis=None):
278
+
279
+ if axis is None:
280
+ return np.concatenate(data, axis=0)
281
+ else:
282
+ return np.concatenate(data, axis=axis)
283
+
284
+ def bk_zeros(self, shape, dtype=None):
285
+ return np.zeros(shape, dtype=dtype)
286
+
287
+ def bk_gather(self, data, idx):
288
+ return data[idx]
289
+
290
+ def bk_reverse(self, data, axis=0):
291
+ return np.reverse(data, axis=axis)
292
+
293
+ def bk_fft(self, data):
294
+ return self.backend.fft.fft(data)
295
+
296
+ def bk_fftn(self, data,dim=None):
297
+ return self.backend.fft.fftn(data)
298
+
299
+ def bk_ifftn(self, data,dim=None,norm=None):
300
+ return self.backend.fft.ifftn(data)
301
+
302
+ def bk_rfft(self, data):
303
+ return self.backend.fft.rfft(data)
304
+
305
+ def bk_irfft(self, data):
306
+ return self.backend.fft.irfft(data)
307
+
308
+ def bk_conjugate(self, data):
309
+
310
+ return data.conjugate()
311
+
312
+ def bk_real(self, data):
313
+ return data.real
314
+
315
+ def bk_imag(self, data):
316
+ return data.imag
317
+
318
+ def bk_relu(self, x):
319
+ return (x > 0) * x
320
+
321
+ def bk_clip_by_value(self, x,xmin,xmax):
322
+ return self.backend.clip(x,xmin,xmax)
323
+
324
+ def bk_cast(self, x):
325
+ if isinstance(x, np.float64):
326
+ if self.all_bk_type == "float32":
327
+ return np.float32(x)
328
+ else:
329
+ return x
330
+ if isinstance(x, np.float32):
331
+ if self.all_bk_type == "float64":
332
+ return np.float64(x)
333
+ else:
334
+ return x
335
+ if isinstance(x, np.complex128):
336
+ if self.all_bk_type == "float32":
337
+ return np.complex64(x)
338
+ else:
339
+ return x
340
+ if isinstance(x, np.complex64):
341
+ if self.all_bk_type == "float64":
342
+ return np.complex128(x)
343
+ else:
344
+ return x
345
+
346
+ if isinstance(x, np.int32) or isinstance(x, np.int64) or isinstance(x, int):
347
+ if self.all_bk_type == "float64":
348
+ return np.float64(x)
349
+ else:
350
+ return np.float32(x)
351
+
352
+ if self.bk_is_complex(x):
353
+ out_type = self.all_cbk_type
354
+ else:
355
+ out_type = self.all_bk_type
356
+
357
+ return x.astype(out_type)
358
+
359
+ def bk_variable(self,x):
360
+
361
+ return self.bk_cast(x)
362
+
363
+ def bk_assign(self,x,y):
364
+ x=y
365
+
366
+ def bk_constant(self,x):
367
+
368
+ return self.bk_cast(x)
369
+
370
+ def bk_empty(self,list):
371
+ return self.backend.empty(list)
372
+
373
+ def to_numpy(self,x):
374
+ return x