pytme 0.2.9__cp311-cp311-macosx_15_0_arm64.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.
Files changed (119) hide show
  1. pytme-0.2.9.data/scripts/estimate_ram_usage.py +97 -0
  2. pytme-0.2.9.data/scripts/match_template.py +1135 -0
  3. pytme-0.2.9.data/scripts/postprocess.py +622 -0
  4. pytme-0.2.9.data/scripts/preprocess.py +209 -0
  5. pytme-0.2.9.data/scripts/preprocessor_gui.py +1227 -0
  6. pytme-0.2.9.dist-info/METADATA +95 -0
  7. pytme-0.2.9.dist-info/RECORD +119 -0
  8. pytme-0.2.9.dist-info/WHEEL +5 -0
  9. pytme-0.2.9.dist-info/entry_points.txt +6 -0
  10. pytme-0.2.9.dist-info/licenses/LICENSE +153 -0
  11. pytme-0.2.9.dist-info/top_level.txt +3 -0
  12. scripts/__init__.py +0 -0
  13. scripts/estimate_ram_usage.py +97 -0
  14. scripts/match_template.py +1135 -0
  15. scripts/postprocess.py +622 -0
  16. scripts/preprocess.py +209 -0
  17. scripts/preprocessor_gui.py +1227 -0
  18. tests/__init__.py +0 -0
  19. tests/data/Blurring/blob_width18.npy +0 -0
  20. tests/data/Blurring/edgegaussian_sigma3.npy +0 -0
  21. tests/data/Blurring/gaussian_sigma2.npy +0 -0
  22. tests/data/Blurring/hamming_width6.npy +0 -0
  23. tests/data/Blurring/kaiserb_width18.npy +0 -0
  24. tests/data/Blurring/localgaussian_sigma0510.npy +0 -0
  25. tests/data/Blurring/mean_size5.npy +0 -0
  26. tests/data/Blurring/ntree_sigma0510.npy +0 -0
  27. tests/data/Blurring/rank_rank3.npy +0 -0
  28. tests/data/Maps/.DS_Store +0 -0
  29. tests/data/Maps/emd_8621.mrc.gz +0 -0
  30. tests/data/README.md +2 -0
  31. tests/data/Raw/em_map.map +0 -0
  32. tests/data/Structures/.DS_Store +0 -0
  33. tests/data/Structures/1pdj.cif +3339 -0
  34. tests/data/Structures/1pdj.pdb +1429 -0
  35. tests/data/Structures/5khe.cif +3685 -0
  36. tests/data/Structures/5khe.ent +2210 -0
  37. tests/data/Structures/5khe.pdb +2210 -0
  38. tests/data/Structures/5uz4.cif +70548 -0
  39. tests/preprocessing/__init__.py +0 -0
  40. tests/preprocessing/test_compose.py +76 -0
  41. tests/preprocessing/test_frequency_filters.py +178 -0
  42. tests/preprocessing/test_preprocessor.py +136 -0
  43. tests/preprocessing/test_utils.py +79 -0
  44. tests/test_analyzer.py +216 -0
  45. tests/test_backends.py +446 -0
  46. tests/test_density.py +503 -0
  47. tests/test_extensions.py +130 -0
  48. tests/test_matching_cli.py +283 -0
  49. tests/test_matching_data.py +162 -0
  50. tests/test_matching_exhaustive.py +124 -0
  51. tests/test_matching_memory.py +30 -0
  52. tests/test_matching_optimization.py +226 -0
  53. tests/test_matching_utils.py +189 -0
  54. tests/test_orientations.py +175 -0
  55. tests/test_parser.py +33 -0
  56. tests/test_rotations.py +153 -0
  57. tests/test_structure.py +247 -0
  58. tme/__init__.py +6 -0
  59. tme/__version__.py +1 -0
  60. tme/analyzer/__init__.py +2 -0
  61. tme/analyzer/_utils.py +186 -0
  62. tme/analyzer/aggregation.py +577 -0
  63. tme/analyzer/peaks.py +953 -0
  64. tme/backends/__init__.py +171 -0
  65. tme/backends/_cupy_utils.py +734 -0
  66. tme/backends/_jax_utils.py +188 -0
  67. tme/backends/cupy_backend.py +294 -0
  68. tme/backends/jax_backend.py +314 -0
  69. tme/backends/matching_backend.py +1270 -0
  70. tme/backends/mlx_backend.py +241 -0
  71. tme/backends/npfftw_backend.py +583 -0
  72. tme/backends/pytorch_backend.py +430 -0
  73. tme/data/__init__.py +0 -0
  74. tme/data/c48n309.npy +0 -0
  75. tme/data/c48n527.npy +0 -0
  76. tme/data/c48n9.npy +0 -0
  77. tme/data/c48u1.npy +0 -0
  78. tme/data/c48u1153.npy +0 -0
  79. tme/data/c48u1201.npy +0 -0
  80. tme/data/c48u1641.npy +0 -0
  81. tme/data/c48u181.npy +0 -0
  82. tme/data/c48u2219.npy +0 -0
  83. tme/data/c48u27.npy +0 -0
  84. tme/data/c48u2947.npy +0 -0
  85. tme/data/c48u3733.npy +0 -0
  86. tme/data/c48u4749.npy +0 -0
  87. tme/data/c48u5879.npy +0 -0
  88. tme/data/c48u7111.npy +0 -0
  89. tme/data/c48u815.npy +0 -0
  90. tme/data/c48u83.npy +0 -0
  91. tme/data/c48u8649.npy +0 -0
  92. tme/data/c600v.npy +0 -0
  93. tme/data/c600vc.npy +0 -0
  94. tme/data/metadata.yaml +80 -0
  95. tme/data/quat_to_numpy.py +42 -0
  96. tme/data/scattering_factors.pickle +0 -0
  97. tme/density.py +2263 -0
  98. tme/extensions.cpython-311-darwin.so +0 -0
  99. tme/external/bindings.cpp +332 -0
  100. tme/filters/__init__.py +6 -0
  101. tme/filters/_utils.py +311 -0
  102. tme/filters/bandpass.py +230 -0
  103. tme/filters/compose.py +81 -0
  104. tme/filters/ctf.py +393 -0
  105. tme/filters/reconstruction.py +160 -0
  106. tme/filters/wedge.py +542 -0
  107. tme/filters/whitening.py +191 -0
  108. tme/matching_data.py +863 -0
  109. tme/matching_exhaustive.py +497 -0
  110. tme/matching_optimization.py +1311 -0
  111. tme/matching_scores.py +1183 -0
  112. tme/matching_utils.py +1188 -0
  113. tme/memory.py +337 -0
  114. tme/orientations.py +598 -0
  115. tme/parser.py +685 -0
  116. tme/preprocessor.py +1329 -0
  117. tme/rotations.py +350 -0
  118. tme/structure.py +1864 -0
  119. tme/types.py +13 -0
@@ -0,0 +1,1270 @@
1
+ """ Strategy pattern to allow for flexible array / FFT backends.
2
+
3
+ Copyright (c) 2023 European Molecular Biology Laboratory
4
+
5
+ Author: Valentin Maurer <valentin.maurer@embl-hamburg.de>
6
+ """
7
+
8
+ from abc import ABC, abstractmethod
9
+ from multiprocessing import shared_memory
10
+ from typing import Tuple, Callable, List, Any, Union, Optional, Generator, Dict
11
+
12
+ from ..types import BackendArray, NDArray, Scalar, shm_type
13
+
14
+
15
+ def _create_metafunction(func_name: str) -> Callable:
16
+ """
17
+ Returns a wrapper of ``self._array_backend.func_name``.
18
+ """
19
+
20
+ def metafunction(self, *args, **kwargs) -> Any:
21
+ backend_func = getattr(self._array_backend, func_name)
22
+ return backend_func(*args, **kwargs)
23
+
24
+ return metafunction
25
+
26
+
27
+ class MatchingBackend(ABC):
28
+ """
29
+ A strategy class for template matching backends.
30
+
31
+ This class provides an interface to enable users to swap between different
32
+ array and fft implementations while preserving the remaining functionalities
33
+ of the API. The objective is to maintain a numpy like interface that generalizes
34
+ across various backends.
35
+
36
+ By delegating attribute access to the provided backend, users can access
37
+ functions/methods of the backend as if they were directly part of this class.
38
+
39
+ Parameters
40
+ ----------
41
+ array_backend : object
42
+ The backend object providing array functionalities.
43
+ float_dtype : type
44
+ Data type of float array instances, e.g. np.float32.
45
+ complex_dtype : type
46
+ Data type of complex array instances, e.g. np.complex64.
47
+ int_dtype : type
48
+ Data type of integer array instances, e.g. np.int32.
49
+ overflow_safe_dtype : type
50
+ Data type than can be used in reduction operations to avoid overflows.
51
+
52
+ Attributes
53
+ ----------
54
+ _array_backend : object
55
+ The backend object providing array functionalities.
56
+ _float_dtype : type
57
+ Data type of float array instances, e.g. np.float32.
58
+ _complex_dtype : type
59
+ Data type of complex array instances, e.g. np.complex64.
60
+ _int_dtype : type
61
+ Data type of integer array instances, e.g. np.int32.
62
+ _overflow_safe_dtype : type
63
+ Data type than can be used in reduction operations to avoid overflows.
64
+ _fundamental_dtypes : Dict
65
+ Maps int, float and cmoplex python types to backend specific data types.
66
+
67
+ Examples
68
+ --------
69
+ >>> import numpy as np
70
+ >>> from tme.backends import NumpyFFTWBackend
71
+ >>> backend = NumpyFFTWBackend(
72
+ >>> array_backend=np,
73
+ >>> float_dtype=np.float32,
74
+ >>> complex_dtype=np.complex64,
75
+ >>> int_dtype=np.int32
76
+ >>> )
77
+ >>> arr = backend.array([1, 2, 3])
78
+ >>> print(arr)
79
+ [1 2 3]
80
+
81
+ Notes
82
+ -----
83
+ Developers should be aware of potential naming conflicts between methods and
84
+ attributes of this class and those of the provided backend.
85
+ """
86
+
87
+ def __init__(
88
+ self,
89
+ array_backend,
90
+ float_dtype: type,
91
+ complex_dtype: type,
92
+ int_dtype: type,
93
+ overflow_safe_dtype: type,
94
+ ):
95
+ self._array_backend = array_backend
96
+ self._float_dtype = float_dtype
97
+ self._complex_dtype = complex_dtype
98
+ self._int_dtype = int_dtype
99
+ self._overflow_safe_dtype = overflow_safe_dtype
100
+
101
+ self._fundamental_dtypes = {
102
+ int: self._int_dtype,
103
+ float: self._float_dtype,
104
+ complex: self._complex_dtype,
105
+ }
106
+
107
+ def __getattr__(self, name: str):
108
+ """
109
+ Delegate attribute access to the underlying backend.
110
+
111
+ Parameters
112
+ ----------
113
+ name : str
114
+ The name of the attribute to access.
115
+
116
+ Returns
117
+ -------
118
+ attribute
119
+ The attribute from the underlying backend.
120
+
121
+ Raises
122
+ ------
123
+ AttributeError
124
+ If the attribute is not found in the backend.
125
+ """
126
+ return getattr(self._array_backend, name)
127
+
128
+ def __dir__(self) -> List:
129
+ """
130
+ Return a list of attributes available in this object,
131
+ including those from the backend.
132
+
133
+ Returns
134
+ -------
135
+ list
136
+ Sorted list of attributes.
137
+ """
138
+ base_attributes = []
139
+ base_attributes.extend(dir(self.__class__))
140
+ base_attributes.extend(self.__dict__.keys())
141
+ base_attributes.extend(dir(self._array_backend))
142
+ return sorted(base_attributes)
143
+
144
+ @abstractmethod
145
+ def to_backend_array(self, arr: NDArray) -> BackendArray:
146
+ """
147
+ Convert a numpy array instance to backend array type.
148
+
149
+ Parameters
150
+ ----------
151
+ arr : NDArray
152
+ The numpy array instance to be converted.
153
+
154
+ Returns
155
+ -------
156
+ BackendArray
157
+ An array of the specified backend.
158
+
159
+ See Also
160
+ --------
161
+ :py:meth:`MatchingBackend.to_cpu_array`
162
+ :py:meth:`MatchingBackend.to_numpy_array`
163
+ """
164
+
165
+ @abstractmethod
166
+ def to_numpy_array(self, arr: BackendArray) -> NDArray:
167
+ """
168
+ Convert an array of given backend to a numpy array.
169
+
170
+ Parameters
171
+ ----------
172
+ arr : BackendArray
173
+ The array instance to be converted.
174
+
175
+ Returns
176
+ -------
177
+ NDArray
178
+ The numpy array equivalent of arr.
179
+
180
+ See Also
181
+ --------
182
+ :py:meth:`MatchingBackend.to_cpu_array`
183
+ :py:meth:`MatchingBackend.to_backend_array`
184
+ """
185
+
186
+ @abstractmethod
187
+ def to_cpu_array(self, arr: BackendArray) -> BackendArray:
188
+ """
189
+ Convert an array of a given backend to a CPU array of that backend.
190
+
191
+ Parameters
192
+ ----------
193
+ arr : BackendArray
194
+ The array instance to be converted.
195
+
196
+ Returns
197
+ -------
198
+ BackendArray
199
+ The CPU array equivalent of arr.
200
+
201
+ See Also
202
+ --------
203
+ :py:meth:`MatchingBackend.to_numpy_array`
204
+ :py:meth:`MatchingBackend.to_backend_array`
205
+ """
206
+
207
+ def get_fundamental_dtype(self, arr: BackendArray) -> type:
208
+ """
209
+ Given an array instance, returns the corresponding fundamental python type,
210
+ i.e., int, float or complex.
211
+
212
+ Parameters
213
+ ----------
214
+ arr : BackendArray
215
+ Input data.
216
+
217
+ Returns
218
+ -------
219
+ type
220
+ Data type.
221
+ """
222
+
223
+ @abstractmethod
224
+ def free_cache(self):
225
+ """
226
+ Free cached objects allocated by backend.
227
+ """
228
+
229
+ @abstractmethod
230
+ def add(
231
+ self, arr1: BackendArray, arr2: BackendArray, out: BackendArray = None
232
+ ) -> BackendArray:
233
+ """
234
+ Element-wise addition of arrays.
235
+
236
+ Parameters
237
+ ----------
238
+ arr1 : BackendArray
239
+ Input array.
240
+ arr2 : BackendArray
241
+ Input array.
242
+ out : BackendArray, optional
243
+ Output array to write the result to. Returns a new array by default.
244
+
245
+ Returns
246
+ -------
247
+ BackendArray
248
+ Element-wise sum of the input arrays.
249
+ """
250
+
251
+ @abstractmethod
252
+ def subtract(
253
+ self, arr1: BackendArray, arr2: BackendArray, out: BackendArray = None
254
+ ) -> BackendArray:
255
+ """
256
+ Element-wise subtraction of arrays.
257
+
258
+ Parameters
259
+ ----------
260
+ arr1 : BackendArray
261
+ The minuend array.
262
+ arr2 : BackendArray
263
+ The subtrahend array.
264
+ out : BackendArray, optional
265
+ Output array to write the result to. Returns a new array by default.
266
+
267
+ Returns
268
+ -------
269
+ BackendArray
270
+ Element-wise difference of the input arrays.
271
+ """
272
+
273
+ @abstractmethod
274
+ def multiply(
275
+ self, arr1: BackendArray, arr2: BackendArray, out: BackendArray = None
276
+ ) -> BackendArray:
277
+ """
278
+ Element-wise multiplication of arrays.
279
+
280
+ Parameters
281
+ ----------
282
+ arr1 : BackendArray
283
+ Input array.
284
+ arr2 : BackendArray
285
+ Input array.
286
+ out : BackendArray, optional
287
+ Output array to write the result to. Returns a new array by default.
288
+
289
+ Returns
290
+ -------
291
+ BackendArray
292
+ Element-wise product of the input arrays.
293
+ """
294
+
295
+ @abstractmethod
296
+ def divide(
297
+ self, arr1: BackendArray, arr2: BackendArray, out: BackendArray = None
298
+ ) -> BackendArray:
299
+ """
300
+ Element-wise division of arrays.
301
+
302
+ Parameters
303
+ ----------
304
+ arr1 : BackendArray
305
+ The dividend array.
306
+ arr2 : BackendArray
307
+ The divisor array.
308
+ out : BackendArray, optional
309
+ Output array to write the result to. Returns a new array by default.
310
+
311
+ Returns
312
+ -------
313
+ BackendArray
314
+ Element-wise quotient of the input arrays.
315
+ """
316
+
317
+ @abstractmethod
318
+ def mod(
319
+ self, arr1: BackendArray, arr2: BackendArray, out: BackendArray = None
320
+ ) -> BackendArray:
321
+ """
322
+ Element-wise modulus of arrays.
323
+
324
+ Parameters
325
+ ----------
326
+ arr1 : BackendArray
327
+ The dividend array.
328
+ arr2 : BackendArray
329
+ The divisor array.
330
+ out : BackendArray, optional
331
+ Output array to write the result to. Returns a new array by default.
332
+
333
+ Returns
334
+ -------
335
+ BackendArray
336
+ Element-wise modulus of the input arrays.
337
+ """
338
+
339
+ @abstractmethod
340
+ def einsum(
341
+ self, arr1: BackendArray, arr2: BackendArray, out: BackendArray = None
342
+ ) -> BackendArray:
343
+ """
344
+ Compute the einstein notation based summation.
345
+
346
+ Parameters
347
+ ----------
348
+ subscripts : str
349
+ Specifies the subscripts for summation (see :obj:`numpy.einsum`).
350
+ arr1, arr2 : BackendArray
351
+ Input data.
352
+ out : BackendArray, optional
353
+ Output array to write the result to. Returns a new array by default.
354
+
355
+ Returns
356
+ -------
357
+ BackendArray
358
+ Einsum of input arrays.
359
+ """
360
+
361
+ @abstractmethod
362
+ def sum(
363
+ self, arr: BackendArray, axis: Tuple[int] = None
364
+ ) -> Union[BackendArray, Scalar]:
365
+ """
366
+ Compute the sum of array elements.
367
+
368
+ Parameters
369
+ ----------
370
+ arr : BackendArray
371
+ Input data.
372
+ axis : int or tuple of ints, optional
373
+ Axis or axes to perform the operation on. Default is all.
374
+
375
+ Returns
376
+ -------
377
+ Union[BackendArray, Scalar]
378
+ Sum of ``arr``.
379
+ """
380
+
381
+ @abstractmethod
382
+ def mean(
383
+ self, arr: BackendArray, axis: Tuple[int] = None
384
+ ) -> Union[BackendArray, Scalar]:
385
+ """
386
+ Compute the mean of array elements.
387
+
388
+ Parameters
389
+ ----------
390
+ arr : BackendArray
391
+ Input data.
392
+ axis : int or tuple of ints, optional
393
+ Axis or axes to perform the operation on. Default is all.
394
+
395
+ Returns
396
+ -------
397
+ Union[BackendArray, Scalar]
398
+ Mean of ``arr``.
399
+ """
400
+
401
+ @abstractmethod
402
+ def std(
403
+ self, arr: BackendArray, axis: Tuple[int] = None
404
+ ) -> Union[BackendArray, Scalar]:
405
+ """
406
+ Compute the standad deviation of array elements.
407
+
408
+ Parameters
409
+ ----------
410
+ arr : BackendArray
411
+ Input data.
412
+ axis : int or tuple of ints, optional
413
+ Axis or axes to perform the operation on. Default is all.
414
+
415
+ Returns
416
+ -------
417
+ Union[BackendArray, Scalar]
418
+ Standard deviation of ``arr``.
419
+ """
420
+
421
+ @abstractmethod
422
+ def max(
423
+ self, arr: BackendArray, axis: Tuple[int] = None
424
+ ) -> Union[BackendArray, Scalar]:
425
+ """
426
+ Compute the maximum of array elements.
427
+
428
+ Parameters
429
+ ----------
430
+ arr : BackendArray
431
+ Input data.
432
+ axis : int or tuple of ints, optional
433
+ Axis or axes to perform the operation on. Default is all.
434
+
435
+ Returns
436
+ -------
437
+ Union[BackendArray, Scalar]
438
+ Maximum of ``arr``.
439
+ """
440
+
441
+ @abstractmethod
442
+ def min(
443
+ self, arr: BackendArray, axis: Tuple[int] = None
444
+ ) -> Union[BackendArray, Scalar]:
445
+ """
446
+ Compute the minimum of array elements.
447
+
448
+ Parameters
449
+ ----------
450
+ arr : BackendArray
451
+ Input data.
452
+ axis : int or tuple of ints, optional
453
+ Axis or axes to perform the operation on. Default is all.
454
+
455
+ Returns
456
+ -------
457
+ Union[BackendArray, Scalar]
458
+ Minimum of ``arr``.
459
+ """
460
+
461
+ @abstractmethod
462
+ def maximum(
463
+ self, arr1: BackendArray, arr2: BackendArray, out: BackendArray = None
464
+ ) -> BackendArray:
465
+ """
466
+ Compute the element wise maximum of arr1 and arr2.
467
+
468
+ Parameters
469
+ ----------
470
+ arr1, arr2 : BackendArray
471
+ Input data.
472
+ out : BackendArray, optional
473
+ Output array to write the result to. Returns a new array by default.
474
+
475
+ Returns
476
+ -------
477
+ BackendArray
478
+ Element wise maximum of ``arr1`` and ``arr2``.
479
+ """
480
+
481
+ @abstractmethod
482
+ def minimum(
483
+ self, arr1: BackendArray, arr2: BackendArray, out: BackendArray = None
484
+ ) -> BackendArray:
485
+ """
486
+ Compute the element wise minimum of arr1 and arr2.
487
+
488
+ Parameters
489
+ ----------
490
+ arr1, arr2 : BackendArray
491
+ Input data.
492
+ out : BackendArray, optional
493
+ Output array to write the result to. Returns a new array by default.
494
+
495
+ Returns
496
+ -------
497
+ BackendArray
498
+ Element wise minimum of arr1 and arr2.
499
+ """
500
+
501
+ @abstractmethod
502
+ def sqrt(self, arr: BackendArray, out: BackendArray = None) -> BackendArray:
503
+ """
504
+ Compute the square root of array elements.
505
+
506
+ Parameters
507
+ ----------
508
+ arr : BackendArray
509
+ Input data.
510
+ out : BackendArray, optional
511
+ Output array to write the result to. Returns a new array by default.
512
+
513
+ Returns
514
+ -------
515
+ BackendArray
516
+ Square root of ``arr``.
517
+ """
518
+
519
+ @abstractmethod
520
+ def square(self, arr: BackendArray, out: BackendArray = None) -> BackendArray:
521
+ """
522
+ Compute the square of array elements.
523
+
524
+ Parameters
525
+ ----------
526
+ arr : BackendArray
527
+ Input data.
528
+ out : BackendArray, optional
529
+ Output array to write the result to. Returns a new array by default.
530
+
531
+ Returns
532
+ -------
533
+ BackendArray
534
+ Square of ``arr``.
535
+ """
536
+
537
+ @abstractmethod
538
+ def abs(self, arr: BackendArray, out: BackendArray = None) -> BackendArray:
539
+ """
540
+ Compute the absolute of array elements.
541
+
542
+ Parameters
543
+ ----------
544
+ arr : BackendArray
545
+ Input data.
546
+ out : BackendArray, optional
547
+ Output array to write the result to. Returns a new array by default.
548
+
549
+ Returns
550
+ -------
551
+ BackendArray
552
+ Absolute value of ``arr``.
553
+ """
554
+
555
+ @abstractmethod
556
+ def transpose(self, arr: BackendArray) -> BackendArray:
557
+ """
558
+ Compute the transpose of arr.
559
+
560
+ Parameters
561
+ ----------
562
+ arr : BackendArray
563
+ Input data.
564
+
565
+ Returns
566
+ -------
567
+ BackendArray
568
+ Transpose of ``arr``.
569
+ """
570
+
571
+ def power(
572
+ self,
573
+ arr: BackendArray = None,
574
+ power: BackendArray = None,
575
+ out: BackendArray = None,
576
+ *args,
577
+ **kwargs,
578
+ ) -> BackendArray:
579
+ """
580
+ Compute the n-th power of an array.
581
+
582
+ Parameters
583
+ ----------
584
+ arr : BackendArray
585
+ Input data.
586
+ power : BackendArray
587
+ Power to raise ``arr`` to.
588
+ arr : BackendArray
589
+ Output array to write the result to. Returns a new array by default.
590
+
591
+ Returns
592
+ -------
593
+ BackendArray
594
+ N-th power of ``arr``.
595
+ """
596
+
597
+ def tobytes(self, arr: BackendArray) -> str:
598
+ """
599
+ Compute the bytestring representation of arr.
600
+
601
+ Parameters
602
+ ----------
603
+ arr : BackendArray
604
+ Input data.
605
+
606
+ Returns
607
+ -------
608
+ str
609
+ Bytestring representation of ``arr``.
610
+ """
611
+
612
+ @abstractmethod
613
+ def size(self, arr: BackendArray) -> int:
614
+ """
615
+ Compute the number of elements of arr.
616
+
617
+ Parameters
618
+ ----------
619
+ arr : BackendArray
620
+ Input data.
621
+
622
+ Returns
623
+ -------
624
+ int
625
+ Number of elements in ``arr``.
626
+ """
627
+
628
+ @abstractmethod
629
+ def fill(self, arr: BackendArray, value: Scalar) -> None:
630
+ """
631
+ Fills ``arr`` in-place with a given value.
632
+
633
+ Parameters
634
+ ----------
635
+ arr : BackendArray
636
+ Input data.
637
+ value : Scalar
638
+ The value to fill the array with.
639
+ """
640
+
641
+ @abstractmethod
642
+ def zeros(self, shape: Tuple[int], dtype: type) -> BackendArray:
643
+ """
644
+ Returns an aligned array of zeros with specified shape and dtype.
645
+
646
+ Parameters
647
+ ----------
648
+ shape : tuple of ints.
649
+ Desired shape for the array.
650
+ dtype : type
651
+ Desired data type for the array.
652
+
653
+ Returns
654
+ -------
655
+ BackendArray
656
+ Byte-aligned array of zeros with specified shape and dtype.
657
+ """
658
+
659
+ @abstractmethod
660
+ def full(self, shape: Tuple[int], dtype: type, fill_value: Scalar) -> BackendArray:
661
+ """
662
+ Returns an array filled with fill_value of specified shape and dtype.
663
+
664
+ Parameters
665
+ ----------
666
+ shape : tuple of ints.
667
+ Desired shape for the array.
668
+ dtype : type
669
+ Desired data type for the array.
670
+
671
+ Returns
672
+ -------
673
+ BackendArray
674
+ Byte-aligned array of zeros with specified shape and dtype.
675
+ """
676
+
677
+ @abstractmethod
678
+ def eps(self, dtype: type) -> Scalar:
679
+ """
680
+ Returns the minimal difference representable by dtype.
681
+
682
+ Parameters
683
+ ----------
684
+ dtype : type
685
+ Data type for which eps should be returned.
686
+
687
+ Returns
688
+ -------
689
+ Scalar
690
+ The eps for the given data type.
691
+ """
692
+
693
+ @abstractmethod
694
+ def datatype_bytes(self, dtype: type) -> int:
695
+ """
696
+ Return the number of bytes occupied by a given datatype.
697
+
698
+ Parameters
699
+ ----------
700
+ dtype : type
701
+ Data type to determine the bytesize of.
702
+
703
+ Returns
704
+ -------
705
+ int
706
+ Number of bytes occupied by the datatype.
707
+ """
708
+
709
+ @abstractmethod
710
+ def clip(
711
+ self, arr: BackendArray, a_min: Scalar, a_max: Scalar, out: BackendArray = None
712
+ ) -> BackendArray:
713
+ """
714
+ Clip elements of arr.
715
+
716
+ Parameters
717
+ ----------
718
+ arr : BackendArray
719
+ Input data.
720
+ a_min : Scalar
721
+ Lower bound.
722
+ a_max : Scalar
723
+ Upper bound.
724
+ out : BackendArray, optional
725
+ Output array to write the result to. Returns a new array by default.
726
+
727
+ Returns
728
+ -------
729
+ BackendArray
730
+ Clipped ``arr``.
731
+ """
732
+
733
+ @abstractmethod
734
+ def astype(arr: BackendArray, dtype: type) -> BackendArray:
735
+ """
736
+ Change the datatype of arr.
737
+
738
+ Parameters
739
+ ----------
740
+ arr : BackendArray
741
+ Input data.
742
+ dtype : type
743
+ Target data type.
744
+
745
+ Returns
746
+ -------
747
+ BackendArray
748
+ Freshly allocated array containing the data of ``arr`` in ``dtype``.
749
+ """
750
+
751
+ @abstractmethod
752
+ def at(arr, idx, value) -> NDArray:
753
+ """
754
+ Assign value to arr at idx, for compatibility with immutable array structures
755
+ such as jax.
756
+
757
+ Parameters
758
+ ----------
759
+ arr : BackendArray
760
+ Input data.
761
+ idx : BackendArray
762
+ Indices to change.
763
+ value: BackendArray
764
+ Values to assign to arr at idx.
765
+
766
+ Returns
767
+ -------
768
+ BackendArray
769
+ Modified input data.
770
+ """
771
+
772
+ @abstractmethod
773
+ def arange(
774
+ self, stop: Scalar, start: Scalar = 0, step: Scalar = 1, *args, **kwargs
775
+ ) -> BackendArray:
776
+ """
777
+ Arange values in evenly spaced interval.
778
+
779
+ Parameters
780
+ ----------
781
+ stop : Scalar
782
+ End of the interval.
783
+ start : Scalar
784
+ Start of the interval, zero by default.
785
+ step : Scalar
786
+ Interval step size, one by default.
787
+
788
+ Returns
789
+ -------
790
+ BackendArray
791
+ Array of evenly spaced values in specified interval.
792
+ """
793
+
794
+ def stack(self, *args, **kwargs) -> BackendArray:
795
+ """
796
+ Join a sequence of objects along a new axis.
797
+
798
+ Parameters
799
+ ----------
800
+ arr : BackendArray
801
+ Sequence of arrays.
802
+ axis : int, optional
803
+ Axis along which to stack the input arrays.
804
+
805
+ Returns
806
+ -------
807
+ BackendArray
808
+ Stacked input data.
809
+ """
810
+
811
+ @abstractmethod
812
+ def concatenate(self, *args, **kwargs) -> BackendArray:
813
+ """
814
+ Join a sequence of objects along an existing axis.
815
+
816
+ Parameters
817
+ ----------
818
+ arr : BackendArray
819
+ Sequence of arrays.
820
+ axis : int
821
+ Axis along which to stack the input arrays.
822
+
823
+ Returns
824
+ -------
825
+ BackendArray
826
+ Concatenated input data.
827
+ """
828
+
829
+ @abstractmethod
830
+ def repeat(self, *args, **kwargs) -> BackendArray:
831
+ """
832
+ Repeat each array element a specified number of times.
833
+
834
+ Parameters
835
+ ----------
836
+ arr : BackendArray
837
+ Input data.
838
+ repeats : int or tuple of ints
839
+ Number of each repetitions along axis.
840
+
841
+ Returns
842
+ -------
843
+ BackendArray
844
+ Repeated ``arr``.
845
+ """
846
+
847
+ @abstractmethod
848
+ def topk_indices(self, arr: NDArray, k: int) -> BackendArray:
849
+ """
850
+ Determinces the indices of largest elements.
851
+
852
+ Parameters
853
+ ----------
854
+ arr : BackendArray
855
+ Input data.
856
+ k : int
857
+ Number of maxima to determine.
858
+
859
+ Returns
860
+ -------
861
+ BackendArray
862
+ Indices of ``k`` largest elements in ``arr``.
863
+ """
864
+
865
+ def indices(self, *args, **kwargs) -> BackendArray:
866
+ """
867
+ Creates an array representing the index grid of an input.
868
+
869
+ Returns
870
+ -------
871
+ BackendArray
872
+ The index grid.
873
+ """
874
+
875
+ @abstractmethod
876
+ def roll(self, *args, **kwargs) -> BackendArray:
877
+ """
878
+ Roll array elements along a specified axis.
879
+
880
+ Parameters
881
+ ----------
882
+ a : BackendArray
883
+ Input data.
884
+ shift : int or tuple of ints, optional
885
+ Shift along each axis.
886
+
887
+ Returns
888
+ -------
889
+ BackendArray
890
+ Array with elements rolled.
891
+ """
892
+
893
+ @abstractmethod
894
+ def where(condition, *args) -> BackendArray:
895
+ """
896
+ Return elements from input depending on ``condition``.
897
+
898
+ Parameters
899
+ ----------
900
+ condition : BackendArray
901
+ Binary condition array.
902
+ *args : BackendArray
903
+ Values to choose from.
904
+
905
+ Returns
906
+ -------
907
+ BackendArray
908
+ Array of elements according to ``condition``.
909
+ """
910
+
911
+ @abstractmethod
912
+ def unique(
913
+ self,
914
+ arr: BackendArray,
915
+ return_index: bool = False,
916
+ return_inverse: bool = False,
917
+ return_counts: bool = False,
918
+ axis: Tuple[int] = None,
919
+ *args,
920
+ **kwargs,
921
+ ) -> Tuple[BackendArray]:
922
+ """
923
+ Find the unique elements of an array.
924
+
925
+ Parameters
926
+ ----------
927
+ arr : BackendArray
928
+ Input data.
929
+ return_index : bool, optional
930
+ Return indices that resulted in unique array, False by default.
931
+ return_inverse : bool, optional
932
+ Return indices to reconstruct the input, False by default.
933
+ return_counts : bool, optional
934
+ Return number of occurences of each unique element, False by default.
935
+ axis : int or tuple of ints, optional
936
+ Axis or axes to perform the operation on. Default is all.
937
+
938
+ Returns
939
+ -------
940
+ BackendArray or tuple of BackendArray
941
+ If `return_index`, `return_inverse`, and `return_counts` keyword
942
+ arguments are all False (the default), this will be an BackendArray object
943
+ of the sorted unique values. Otherwise, it's a tuple with one
944
+ or more arrays as specified by those keyword arguments.
945
+ """
946
+
947
+ @abstractmethod
948
+ def argsort(self, *args, **kwargs) -> BackendArray:
949
+ """
950
+ Compute the indices to sort a given input array.
951
+
952
+ Parameters
953
+ ----------
954
+ arr : BackendArray
955
+ Input array.
956
+ dtype : type
957
+ Target data type.
958
+
959
+ Returns
960
+ -------
961
+ BackendArray
962
+ Indices that would sort the input data.
963
+ """
964
+
965
+ @abstractmethod
966
+ def unravel_index(
967
+ self, indices: BackendArray, shape: Tuple[int]
968
+ ) -> Tuple[BackendArray]:
969
+ """
970
+ Convert flat index to array indices.
971
+
972
+ Parameters
973
+ ----------
974
+ indices : BackendArray
975
+ Input data.
976
+ shape : tuple of ints
977
+ Shape of the array used for unraveling.
978
+
979
+ Returns
980
+ -------
981
+ BackendArray
982
+ Array indices.
983
+ """
984
+
985
+ @abstractmethod
986
+ def tril_indices(self, *args, **kwargs) -> BackendArray:
987
+ """
988
+ Compute indices of upper triangular matrix
989
+
990
+ Parameters
991
+ ----------
992
+ arr : BackendArray
993
+ Input array.
994
+ dtype : type
995
+ Target data type.
996
+
997
+ Returns
998
+ -------
999
+ BackendArray
1000
+ Flipped version of arr.
1001
+ """
1002
+
1003
+ @abstractmethod
1004
+ def max_filter_coordinates(
1005
+ self, score_space: BackendArray, min_distance: Tuple[int]
1006
+ ) -> BackendArray:
1007
+ """
1008
+ Identifies local maxima in score_space separated by min_distance.
1009
+
1010
+ Parameters
1011
+ ----------
1012
+ score_space : BackendArray
1013
+ Input score space.
1014
+ min_distance : tuple of ints
1015
+ Minimum distance along each array axis.
1016
+
1017
+ Returns
1018
+ -------
1019
+ BackendArray
1020
+ Identified local maxima.
1021
+ """
1022
+
1023
+ @abstractmethod
1024
+ def from_sharedarr(
1025
+ self, shape: Tuple[int], dtype: str, shm: shared_memory.SharedMemory
1026
+ ) -> BackendArray:
1027
+ """
1028
+ Returns an array of given shape and dtype from shared memory location.
1029
+
1030
+ Parameters
1031
+ ----------
1032
+ shape : tuple
1033
+ Tuple of integers specifying the shape of the array.
1034
+ dtype : str
1035
+ String specifying the dtype of the array.
1036
+ shm : shared_memory.SharedMemory
1037
+ Shared memory object where the array is stored.
1038
+
1039
+ Returns
1040
+ -------
1041
+ BackendArray
1042
+ Array of the specified shape and dtype from the shared memory location.
1043
+ """
1044
+
1045
+ @abstractmethod
1046
+ def to_sharedarr(self, arr: type, shared_memory_handler: type = None) -> shm_type:
1047
+ """
1048
+ Converts an array to an object shared in memory. The output of this function
1049
+ will only be passed to :py:meth:`from_sharedarr`, hence the return values can
1050
+ be modified in particular backends to match the expected input data.
1051
+
1052
+ Parameters
1053
+ ----------
1054
+ arr : BackendArray
1055
+ Numpy array to convert.
1056
+ shared_memory_handler : type, optional
1057
+ The type of shared memory handler. Default is None.
1058
+
1059
+ Returns
1060
+ -------
1061
+ Tuple[shared_memory.SharedMemory, tuple of ints, dtype]
1062
+ The shared memory object containing the numpy array, its shape and dtype.
1063
+ """
1064
+
1065
+ @abstractmethod
1066
+ def topleft_pad(
1067
+ self, arr: BackendArray, shape: Tuple[int], padval: int = 0
1068
+ ) -> BackendArray:
1069
+ """
1070
+ Returns an array that has been padded to a specified shape with a padding
1071
+ value at the top-left corner.
1072
+
1073
+ Parameters
1074
+ ----------
1075
+ arr : BackendArray
1076
+ Input array to be padded.
1077
+ shape : Tuple[int]
1078
+ Desired shape for the output array.
1079
+ padval : int, optional
1080
+ Value to use for padding, default is 0.
1081
+
1082
+ Returns
1083
+ -------
1084
+ BackendArray
1085
+ Array that has been padded to the specified shape.
1086
+ """
1087
+
1088
+ @abstractmethod
1089
+ def build_fft(
1090
+ self,
1091
+ fwd_shape: Tuple[int],
1092
+ inv_shape: Tuple[int],
1093
+ real_dtype: type,
1094
+ cmpl_dtype: type,
1095
+ inv_output_shape: Tuple[int] = None,
1096
+ temp_fwd: NDArray = None,
1097
+ temp_inv: NDArray = None,
1098
+ fwd_axes: Tuple[int] = None,
1099
+ inv_axes: Tuple[int] = None,
1100
+ fftargs: Dict = {},
1101
+ ) -> Tuple[Callable, Callable]:
1102
+ """
1103
+ Build forward and inverse real fourier transform functions. The returned
1104
+ callables have two parameters ``arr`` and ``out`` which correspond to the
1105
+ input and output of the Fourier transform. The methods return the output
1106
+ of the respective function call, regardless of ``out`` being provided or not,
1107
+ analogous to most numpy functions.
1108
+
1109
+ Parameters
1110
+ ----------
1111
+ fwd_shape : tuple
1112
+ Input shape for the forward Fourier transform.
1113
+ (see `compute_convolution_shapes`).
1114
+ inv_shape : tuple
1115
+ Input shape for the inverse Fourier transform.
1116
+ real_dtype : dtype
1117
+ Data type of the forward Fourier transform.
1118
+ complex_dtype : dtype
1119
+ Data type of the inverse Fourier transform.
1120
+ inv_output_shape : tuple, optional
1121
+ Output shape of the inverse Fourier transform. By default fast_shape.
1122
+ fftargs : dict, optional
1123
+ Dictionary passed to pyFFTW builders.
1124
+ temp_fwd : NDArray, optional
1125
+ Temporary array to build the forward transform. Superseeds shape defined by
1126
+ fwd_shape if provided.
1127
+ temp_inv : NDArray, optional
1128
+ Temporary array to build the inverse transform. Superseeds shape defined by
1129
+ inv_shape if provided.
1130
+ fwd_axes : tuple of int
1131
+ Axes to perform the forward Fourier transform over.
1132
+ inv_axes : tuple of int
1133
+ Axes to perform the inverse Fourier transform over.
1134
+
1135
+ Returns
1136
+ -------
1137
+ tuple
1138
+ Tuple of callables for forward and inverse real Fourier transform.
1139
+ """
1140
+
1141
+ def extract_center(self, arr: BackendArray, newshape: Tuple[int]) -> BackendArray:
1142
+ """
1143
+ Extract the centered portion of an array based on a new shape.
1144
+
1145
+ Parameters
1146
+ ----------
1147
+ arr : BackendArray
1148
+ Input data.
1149
+ newshape : tuple
1150
+ Desired shape for the central portion.
1151
+
1152
+ Returns
1153
+ -------
1154
+ BackendArray
1155
+ Central portion of the array with shape ``newshape``.
1156
+ """
1157
+
1158
+ @abstractmethod
1159
+ def compute_convolution_shapes(
1160
+ self, arr1_shape: Tuple[int], arr2_shape: Tuple[int]
1161
+ ) -> Tuple[List[int], List[int], List[int]]:
1162
+ """
1163
+ Computes regular, optimized and fourier convolution shape.
1164
+
1165
+ Parameters
1166
+ ----------
1167
+ arr1_shape : tuple of int
1168
+ Tuple of integers corresponding to array1 shape.
1169
+ arr2_shape : tuple of int
1170
+ Tuple of integers corresponding to array2 shape.
1171
+
1172
+ Returns
1173
+ -------
1174
+ tuple
1175
+ Tuple with regular convolution shape, convolution shape optimized for faster
1176
+ fourier transform, shape of the forward fourier transform
1177
+ (see :py:meth:`build_fft`).
1178
+ """
1179
+
1180
+ @abstractmethod
1181
+ def rigid_transform(
1182
+ self,
1183
+ arr: BackendArray,
1184
+ rotation_matrix: BackendArray,
1185
+ arr_mask: Optional[BackendArray] = None,
1186
+ translation: Optional[BackendArray] = None,
1187
+ use_geometric_center: bool = True,
1188
+ out: Optional[BackendArray] = None,
1189
+ out_mask: Optional[BackendArray] = None,
1190
+ order: int = 3,
1191
+ **kwargs,
1192
+ ) -> Tuple[BackendArray, Optional[BackendArray]]:
1193
+ """
1194
+ Performs a rigid transformation.
1195
+
1196
+ Parameters
1197
+ ----------
1198
+ arr : BackendArray
1199
+ The input array to be rotated.
1200
+ arr_mask : BackendArray, optional
1201
+ The mask of `arr` that will be equivalently rotated.
1202
+ rotation_matrix : BackendArray
1203
+ The rotation matrix to apply (d, d).
1204
+ translation : BackendArray
1205
+ The translation to apply (d,).
1206
+ use_geometric_center : bool, optional
1207
+ Whether rotation should be performed over the center of mass.
1208
+ out : BackendArray, optional
1209
+ Location into which the rotation of ``arr`` is written.
1210
+ out_mask : BackendArray, optional
1211
+ Location into which the rotation of ``arr_mask`` is written.
1212
+ order : int, optional
1213
+ Interpolation order, one is linear and three is cubic. Specific
1214
+ meaning depends on backend.
1215
+ kwargs : dict, optional
1216
+ Keyword arguments relevant to particular backend implementations.
1217
+
1218
+ Returns
1219
+ -------
1220
+ out, out_mask : BackendArray or None
1221
+ The rotated arrays.
1222
+ """
1223
+
1224
+ @abstractmethod
1225
+ def get_available_memory(self) -> int:
1226
+ """
1227
+ Returns the available memory available for computations in bytes. For CPU
1228
+ operations this corresponds to available RAM. For GPU operations the function
1229
+ is expected to return the available GPU memory.
1230
+ """
1231
+
1232
+ @abstractmethod
1233
+ def reverse(arr: BackendArray) -> BackendArray:
1234
+ """
1235
+ Reverse the order of elements in an array along all its axes.
1236
+
1237
+ Parameters
1238
+ ----------
1239
+ arr : NDArray
1240
+ Input array.
1241
+ axis : tuple of int
1242
+ Axis to reverse, all by default.
1243
+
1244
+ Returns
1245
+ -------
1246
+ NDArray
1247
+ Reversed array.
1248
+ """
1249
+
1250
+ @abstractmethod
1251
+ def set_device(device_index: int) -> Generator:
1252
+ """
1253
+ Context manager that sets active compute device device for operations.
1254
+
1255
+ Parameters
1256
+ ----------
1257
+ device_index : int
1258
+ Index of the device to be set as active.
1259
+ """
1260
+
1261
+ @abstractmethod
1262
+ def device_count() -> int:
1263
+ """
1264
+ Return the number of available compute devices considered by the backend.
1265
+
1266
+ Returns
1267
+ -------
1268
+ int
1269
+ Number of available devices.
1270
+ """