pytme 0.2.0b0__cp311-cp311-macosx_14_0_arm64.whl → 0.2.2__cp311-cp311-macosx_14_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 (52) hide show
  1. pytme-0.2.2.data/scripts/match_template.py +1187 -0
  2. {pytme-0.2.0b0.data → pytme-0.2.2.data}/scripts/postprocess.py +170 -71
  3. {pytme-0.2.0b0.data → pytme-0.2.2.data}/scripts/preprocessor_gui.py +179 -86
  4. pytme-0.2.2.dist-info/METADATA +91 -0
  5. pytme-0.2.2.dist-info/RECORD +74 -0
  6. {pytme-0.2.0b0.dist-info → pytme-0.2.2.dist-info}/WHEEL +1 -1
  7. scripts/extract_candidates.py +126 -87
  8. scripts/match_template.py +596 -209
  9. scripts/match_template_filters.py +571 -223
  10. scripts/postprocess.py +170 -71
  11. scripts/preprocessor_gui.py +179 -86
  12. scripts/refine_matches.py +567 -159
  13. tme/__init__.py +0 -1
  14. tme/__version__.py +1 -1
  15. tme/analyzer.py +627 -855
  16. tme/backends/__init__.py +41 -11
  17. tme/backends/_jax_utils.py +185 -0
  18. tme/backends/cupy_backend.py +120 -225
  19. tme/backends/jax_backend.py +282 -0
  20. tme/backends/matching_backend.py +464 -388
  21. tme/backends/mlx_backend.py +45 -68
  22. tme/backends/npfftw_backend.py +256 -514
  23. tme/backends/pytorch_backend.py +41 -154
  24. tme/density.py +312 -421
  25. tme/extensions.cpython-311-darwin.so +0 -0
  26. tme/matching_data.py +366 -303
  27. tme/matching_exhaustive.py +279 -1521
  28. tme/matching_optimization.py +234 -129
  29. tme/matching_scores.py +884 -0
  30. tme/matching_utils.py +281 -387
  31. tme/memory.py +377 -0
  32. tme/orientations.py +226 -66
  33. tme/parser.py +3 -4
  34. tme/preprocessing/__init__.py +2 -0
  35. tme/preprocessing/_utils.py +217 -0
  36. tme/preprocessing/composable_filter.py +31 -0
  37. tme/preprocessing/compose.py +55 -0
  38. tme/preprocessing/frequency_filters.py +388 -0
  39. tme/preprocessing/tilt_series.py +1011 -0
  40. tme/preprocessor.py +574 -530
  41. tme/structure.py +495 -189
  42. tme/types.py +5 -3
  43. pytme-0.2.0b0.data/scripts/match_template.py +0 -800
  44. pytme-0.2.0b0.dist-info/METADATA +0 -73
  45. pytme-0.2.0b0.dist-info/RECORD +0 -66
  46. tme/helpers.py +0 -881
  47. tme/matching_constrained.py +0 -195
  48. {pytme-0.2.0b0.data → pytme-0.2.2.data}/scripts/estimate_ram_usage.py +0 -0
  49. {pytme-0.2.0b0.data → pytme-0.2.2.data}/scripts/preprocess.py +0 -0
  50. {pytme-0.2.0b0.dist-info → pytme-0.2.2.dist-info}/LICENSE +0 -0
  51. {pytme-0.2.0b0.dist-info → pytme-0.2.2.dist-info}/entry_points.txt +0 -0
  52. {pytme-0.2.0b0.dist-info → pytme-0.2.2.dist-info}/top_level.txt +0 -0
@@ -1,800 +0,0 @@
1
- #!python
2
- """ CLI interface for basic pyTME template matching functions.
3
-
4
- Copyright (c) 2023 European Molecular Biology Laboratory
5
-
6
- Author: Valentin Maurer <valentin.maurer@embl-hamburg.de>
7
- """
8
- import os
9
- import argparse
10
- import warnings
11
- import importlib.util
12
- from sys import exit
13
- from time import time
14
- from copy import deepcopy
15
- from os.path import abspath
16
-
17
- import numpy as np
18
-
19
- from tme import Density, Preprocessor, __version__
20
- from tme.matching_utils import (
21
- get_rotation_matrices,
22
- compute_parallelization_schedule,
23
- euler_from_rotationmatrix,
24
- scramble_phases,
25
- generate_tempfile_name,
26
- write_pickle,
27
- )
28
- from tme.matching_exhaustive import scan_subsets, MATCHING_EXHAUSTIVE_REGISTER
29
- from tme.matching_data import MatchingData
30
- from tme.analyzer import (
31
- MaxScoreOverRotations,
32
- PeakCallerMaximumFilter,
33
- )
34
- from tme.backends import backend
35
-
36
-
37
- def get_func_fullname(func) -> str:
38
- """Returns the full name of the given function, including its module."""
39
- return f"<function '{func.__module__}.{func.__name__}'>"
40
-
41
-
42
- def print_block(name: str, data: dict, label_width=20) -> None:
43
- """Prints a formatted block of information."""
44
- print(f"\n> {name}")
45
- for key, value in data.items():
46
- formatted_value = str(value)
47
- print(f" - {key + ':':<{label_width}} {formatted_value}")
48
-
49
-
50
- def print_entry() -> None:
51
- width = 80
52
- text = f" pyTME v{__version__} "
53
- padding_total = width - len(text) - 2
54
- padding_left = padding_total // 2
55
- padding_right = padding_total - padding_left
56
-
57
- print("*" * width)
58
- print(f"*{ ' ' * padding_left }{text}{ ' ' * padding_right }*")
59
- print("*" * width)
60
-
61
-
62
- def check_positive(value):
63
- ivalue = float(value)
64
- if ivalue <= 0:
65
- raise argparse.ArgumentTypeError("%s is an invalid positive float." % value)
66
- return ivalue
67
-
68
-
69
- def load_and_validate_mask(mask_target: "Density", mask_path: str, **kwargs):
70
- """
71
- Loadsa mask in CCP4/MRC format and assess whether the sampling_rate
72
- and shape matches its target.
73
-
74
- Parameters
75
- ----------
76
- mask_target : Density
77
- Object the mask should be applied to
78
- mask_path : str
79
- Path to the mask in CCP4/MRC format.
80
- kwargs : dict, optional
81
- Keyword arguments passed to :py:meth:`tme.density.Density.from_file`.
82
- Raise
83
- -----
84
- ValueError
85
- If shape or sampling rate do not match between mask_target and mask
86
-
87
- Returns
88
- -------
89
- Density
90
- A density instance if the mask was validated and loaded otherwise None
91
- """
92
- mask = mask_path
93
- if mask is not None:
94
- mask = Density.from_file(mask, **kwargs)
95
- mask.origin = deepcopy(mask_target.origin)
96
- if not np.allclose(mask.shape, mask_target.shape):
97
- raise ValueError(
98
- f"Expected shape of {mask_path} was {mask_target.shape},"
99
- f" got f{mask.shape}"
100
- )
101
- if not np.allclose(mask.sampling_rate, mask_target.sampling_rate):
102
- raise ValueError(
103
- f"Expected sampling_rate of {mask_path} was {mask_target.sampling_rate}"
104
- f", got f{mask.sampling_rate}"
105
- )
106
- return mask
107
-
108
-
109
- def crop_data(data: Density, cutoff: float, data_mask: Density = None) -> bool:
110
- """
111
- Crop the provided data and mask to a smaller box based on a cutoff value.
112
-
113
- Parameters
114
- ----------
115
- data : Density
116
- The data that should be cropped.
117
- cutoff : float
118
- The threshold value to determine which parts of the data should be kept.
119
- data_mask : Density, optional
120
- A mask for the data that should be cropped.
121
-
122
- Returns
123
- -------
124
- bool
125
- Returns True if the data was adjusted (cropped), otherwise returns False.
126
-
127
- Notes
128
- -----
129
- Cropping is performed in place.
130
- """
131
- if cutoff is None:
132
- return False
133
-
134
- box = data.trim_box(cutoff=cutoff)
135
- box_mask = box
136
- if data_mask is not None:
137
- box_mask = data_mask.trim_box(cutoff=cutoff)
138
- box = tuple(
139
- slice(min(arr.start, mask.start), max(arr.stop, mask.stop))
140
- for arr, mask in zip(box, box_mask)
141
- )
142
- if box == tuple(slice(0, x) for x in data.shape):
143
- return False
144
-
145
- data.adjust_box(box)
146
-
147
- if data_mask:
148
- data_mask.adjust_box(box)
149
-
150
- return True
151
-
152
-
153
- def parse_args():
154
- parser = argparse.ArgumentParser(description="Perform template matching.")
155
-
156
- io_group = parser.add_argument_group("Input / Output")
157
- io_group.add_argument(
158
- "-m",
159
- "--target",
160
- dest="target",
161
- type=str,
162
- required=True,
163
- help="Path to a target in CCP4/MRC, EM, H5 or another format supported by "
164
- "tme.density.Density.from_file "
165
- "https://kosinskilab.github.io/pyTME/reference/api/tme.density.Density.from_file.html",
166
- )
167
- io_group.add_argument(
168
- "--target_mask",
169
- dest="target_mask",
170
- type=str,
171
- required=False,
172
- help="Path to a mask for the target in a supported format (see target).",
173
- )
174
- io_group.add_argument(
175
- "-i",
176
- "--template",
177
- dest="template",
178
- type=str,
179
- required=True,
180
- help="Path to a template in PDB/MMCIF or other supported formats (see target).",
181
- )
182
- io_group.add_argument(
183
- "--template_mask",
184
- dest="template_mask",
185
- type=str,
186
- required=False,
187
- help="Path to a mask for the template in a supported format (see target).",
188
- )
189
- io_group.add_argument(
190
- "-o",
191
- "--output",
192
- dest="output",
193
- type=str,
194
- required=False,
195
- default="output.pickle",
196
- help="Path to the output pickle file.",
197
- )
198
- io_group.add_argument(
199
- "--invert_target_contrast",
200
- dest="invert_target_contrast",
201
- action="store_true",
202
- default=False,
203
- help="Invert the target's contrast and rescale linearly between zero and one. "
204
- "This option is intended for targets where templates to-be-matched have "
205
- "negative values, e.g. tomograms.",
206
- )
207
- io_group.add_argument(
208
- "--scramble_phases",
209
- dest="scramble_phases",
210
- action="store_true",
211
- default=False,
212
- help="Phase scramble the template to generate a noise score background.",
213
- )
214
-
215
- scoring_group = parser.add_argument_group("Scoring")
216
- scoring_group.add_argument(
217
- "-s",
218
- dest="score",
219
- type=str,
220
- default="FLCSphericalMask",
221
- choices=list(MATCHING_EXHAUSTIVE_REGISTER.keys()),
222
- help="Template matching scoring function.",
223
- )
224
- scoring_group.add_argument(
225
- "-p",
226
- dest="peak_calling",
227
- action="store_true",
228
- default=False,
229
- help="Perform peak calling instead of score aggregation.",
230
- )
231
- scoring_group.add_argument(
232
- "-a",
233
- dest="angular_sampling",
234
- type=check_positive,
235
- default=40.0,
236
- help="Angular sampling rate for template matching. "
237
- "A lower number yields more rotations. Values >= 180 sample only the identity.",
238
- )
239
-
240
-
241
- computation_group = parser.add_argument_group("Computation")
242
- computation_group.add_argument(
243
- "-n",
244
- dest="cores",
245
- required=False,
246
- type=int,
247
- default=4,
248
- help="Number of cores used for template matching.",
249
- )
250
- computation_group.add_argument(
251
- "--use_gpu",
252
- dest="use_gpu",
253
- action="store_true",
254
- default=False,
255
- help="Whether to perform computations on the GPU.",
256
- )
257
- computation_group.add_argument(
258
- "--gpu_indices",
259
- dest="gpu_indices",
260
- type=str,
261
- default=None,
262
- help="Comma-separated list of GPU indices to use. For example,"
263
- " 0,1 for the first and second GPU. Only used if --use_gpu is set."
264
- " If not provided but --use_gpu is set, CUDA_VISIBLE_DEVICES will"
265
- " be respected.",
266
- )
267
- computation_group.add_argument(
268
- "-r",
269
- "--ram",
270
- dest="memory",
271
- required=False,
272
- type=int,
273
- default=None,
274
- help="Amount of memory that can be used in bytes.",
275
- )
276
- computation_group.add_argument(
277
- "--memory_scaling",
278
- dest="memory_scaling",
279
- required=False,
280
- type=float,
281
- default=0.85,
282
- help="Fraction of available memory that can be used. Defaults to 0.85 and is "
283
- "ignored if --ram is set",
284
- )
285
- computation_group.add_argument(
286
- "--use_mixed_precision",
287
- dest="use_mixed_precision",
288
- action="store_true",
289
- default=False,
290
- help="Use float16 for real values operations where possible.",
291
- )
292
- computation_group.add_argument(
293
- "--use_memmap",
294
- dest="use_memmap",
295
- action="store_true",
296
- default=False,
297
- help="Use memmaps to offload large data objects to disk. "
298
- "Particularly useful for large inputs in combination with --use_gpu.",
299
- )
300
- computation_group.add_argument(
301
- "--temp_directory",
302
- dest="temp_directory",
303
- default=None,
304
- help="Directory for temporary objects. Faster I/O improves runtime.",
305
- )
306
-
307
- filter_group = parser.add_argument_group("Filters")
308
- filter_group.add_argument(
309
- "--gaussian_sigma",
310
- dest="gaussian_sigma",
311
- type=float,
312
- required=False,
313
- help="Sigma parameter for Gaussian filtering the template.",
314
- )
315
- filter_group.add_argument(
316
- "--bandpass_band",
317
- dest="bandpass_band",
318
- type=str,
319
- required=False,
320
- help="Comma separated start and stop frequency for bandpass filtering the"
321
- " template, e.g. 0.1, 0.5",
322
- )
323
- filter_group.add_argument(
324
- "--bandpass_smooth",
325
- dest="bandpass_smooth",
326
- type=float,
327
- required=False,
328
- default=None,
329
- help="Sigma smooth parameter for the bandpass filter.",
330
- )
331
- filter_group.add_argument(
332
- "--tilt_range",
333
- dest="tilt_range",
334
- type=str,
335
- required=False,
336
- help="Comma separated start and stop stage tilt angle, e.g. '50,45'. Used"
337
- " to create a wedge mask to be applied to the template.",
338
- )
339
- filter_group.add_argument(
340
- "--tilt_step",
341
- dest="tilt_step",
342
- type=float,
343
- required=False,
344
- default=None,
345
- help="Step size between tilts. e.g. '5'. When set the wedge mask"
346
- " reflects the individual tilts, otherwise a continuous mask is used.",
347
- )
348
- filter_group.add_argument(
349
- "--wedge_axes",
350
- dest="wedge_axes",
351
- type=str,
352
- required=False,
353
- default="0,2",
354
- help="Axis index of wedge opening and tilt axis, e.g. 0,2 for a wedge that is open in"
355
- " z and tilted over x.",
356
- )
357
- filter_group.add_argument(
358
- "--wedge_smooth",
359
- dest="wedge_smooth",
360
- type=float,
361
- required=False,
362
- default=None,
363
- help="Sigma smooth parameter for the wedge mask.",
364
- )
365
-
366
- performance_group = parser.add_argument_group("Performance")
367
- performance_group.add_argument(
368
- "--cutoff_target",
369
- dest="cutoff_target",
370
- type=float,
371
- required=False,
372
- default=None,
373
- help="Target contour level (used for cropping).",
374
- )
375
- performance_group.add_argument(
376
- "--cutoff_template",
377
- dest="cutoff_template",
378
- type=float,
379
- required=False,
380
- default=None,
381
- help="Template contour level (used for cropping).",
382
- )
383
- performance_group.add_argument(
384
- "--no_centering",
385
- dest="no_centering",
386
- action="store_true",
387
- help="Assumes the template is already centered and omits centering.",
388
- )
389
- performance_group.add_argument(
390
- "--no_edge_padding",
391
- dest="no_edge_padding",
392
- action="store_true",
393
- default=False,
394
- help="Whether to not pad the edges of the target. Can be set if the target"
395
- " has a well defined bounding box, e.g. a masked reconstruction.",
396
- )
397
- performance_group.add_argument(
398
- "--no_fourier_padding",
399
- dest="no_fourier_padding",
400
- action="store_true",
401
- default=False,
402
- help="Whether input arrays should not be zero-padded to full convolution shape "
403
- "for numerical stability. When working with very large targets, e.g. tomograms, "
404
- "it is safe to use this flag and benefit from the performance gain.",
405
- )
406
- performance_group.add_argument(
407
- "--interpolation_order",
408
- dest="interpolation_order",
409
- required=False,
410
- type=int,
411
- default=3,
412
- help="Spline interpolation used for template rotations. If less than zero "
413
- "no interpolation is performed.",
414
- )
415
-
416
- analyzer_group = parser.add_argument_group("Analyzer")
417
- analyzer_group.add_argument(
418
- "--score_threshold",
419
- dest="score_threshold",
420
- required=False,
421
- type=float,
422
- default=0,
423
- help="Minimum template matching scores to consider for analysis.",
424
- )
425
-
426
- args = parser.parse_args()
427
-
428
- if args.interpolation_order < 0:
429
- args.interpolation_order = None
430
-
431
- if args.temp_directory is None:
432
- default = abspath(".")
433
- if os.environ.get("TMPDIR", None) is not None:
434
- default = os.environ.get("TMPDIR")
435
- args.temp_directory = default
436
-
437
- os.environ["TMPDIR"] = args.temp_directory
438
-
439
- args.pad_target_edges = not args.no_edge_padding
440
- args.pad_fourier = not args.no_fourier_padding
441
-
442
- if args.score not in MATCHING_EXHAUSTIVE_REGISTER:
443
- raise ValueError(
444
- f"score has to be one of {', '.join(MATCHING_EXHAUSTIVE_REGISTER.keys())}"
445
- )
446
-
447
- gpu_devices = os.environ.get("CUDA_VISIBLE_DEVICES", None)
448
- if args.gpu_indices is not None:
449
- os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu_indices
450
-
451
- if args.use_gpu:
452
- gpu_devices = os.environ.get("CUDA_VISIBLE_DEVICES", None)
453
- if gpu_devices is None:
454
- print(
455
- "No GPU indices provided and CUDA_VISIBLE_DEVICES is not set.",
456
- "Assuming device 0.",
457
- )
458
- os.environ["CUDA_VISIBLE_DEVICES"] = "0"
459
- args.gpu_indices = [
460
- int(x) for x in os.environ["CUDA_VISIBLE_DEVICES"].split(",")
461
- ]
462
-
463
- return args
464
-
465
-
466
- def main():
467
- args = parse_args()
468
- print_entry()
469
-
470
- target = Density.from_file(args.target, use_memmap=True)
471
-
472
- try:
473
- template = Density.from_file(args.template)
474
- except Exception:
475
- template = Density.from_structure(
476
- filename_or_structure=args.template,
477
- sampling_rate=target.sampling_rate,
478
- )
479
-
480
- if not np.allclose(target.sampling_rate, template.sampling_rate):
481
- print(
482
- f"Resampling template to {target.sampling_rate}. "
483
- "Consider providing a template with the same sampling rate as the target."
484
- )
485
- template = template.resample(target.sampling_rate, order=3)
486
-
487
- template_mask = load_and_validate_mask(
488
- mask_target=template, mask_path=args.template_mask
489
- )
490
- target_mask = load_and_validate_mask(
491
- mask_target=target, mask_path=args.target_mask, use_memmap=True
492
- )
493
-
494
- initial_shape = target.shape
495
- is_cropped = crop_data(
496
- data=target, data_mask=target_mask, cutoff=args.cutoff_target
497
- )
498
- print_block(
499
- name="Target",
500
- data={
501
- "Initial Shape": initial_shape,
502
- "Sampling Rate": tuple(np.round(target.sampling_rate, 2)),
503
- "Final Shape": target.shape,
504
- },
505
- )
506
- if is_cropped:
507
- args.target = generate_tempfile_name(suffix=".mrc")
508
- target.to_file(args.target)
509
-
510
- if target_mask:
511
- args.target_mask = generate_tempfile_name(suffix=".mrc")
512
- target_mask.to_file(args.target_mask)
513
-
514
- if target_mask:
515
- print_block(
516
- name="Target Mask",
517
- data={
518
- "Initial Shape": initial_shape,
519
- "Sampling Rate": tuple(np.round(target_mask.sampling_rate, 2)),
520
- "Final Shape": target_mask.shape,
521
- },
522
- )
523
-
524
- initial_shape = template.shape
525
- _ = crop_data(data=template, data_mask=template_mask, cutoff=args.cutoff_template)
526
-
527
- translation = np.zeros(len(template.shape), dtype=np.float32)
528
- if not args.no_centering:
529
- template, translation = template.centered(0)
530
- print_block(
531
- name="Template",
532
- data={
533
- "Initial Shape": initial_shape,
534
- "Sampling Rate": tuple(np.round(template.sampling_rate, 2)),
535
- "Final Shape": template.shape,
536
- },
537
- )
538
-
539
- template_filter = {}
540
- if args.gaussian_sigma is not None:
541
- template.data = Preprocessor().gaussian_filter(
542
- sigma=args.gaussian_sigma, template=template.data
543
- )
544
-
545
- if args.bandpass_band is not None:
546
- bandpass_start, bandpass_stop = [
547
- float(x) for x in args.bandpass_band.split(",")
548
- ]
549
- if args.bandpass_smooth is None:
550
- args.bandpass_smooth = 0
551
-
552
- template_filter["bandpass_mask"] = {
553
- "minimum_frequency": bandpass_start,
554
- "maximum_frequency": bandpass_stop,
555
- "gaussian_sigma": args.bandpass_smooth,
556
- }
557
-
558
- if args.tilt_range is not None:
559
- args.wedge_smooth if args.wedge_smooth is not None else 0
560
- tilt_start, tilt_stop = [float(x) for x in args.tilt_range.split(",")]
561
- opening_axis, tilt_axis = [int(x) for x in args.wedge_axes.split(",")]
562
-
563
- if args.tilt_step is not None:
564
- template_filter["step_wedge_mask"] = {
565
- "start_tilt": tilt_start,
566
- "stop_tilt": tilt_stop,
567
- "tilt_step": args.tilt_step,
568
- "sigma": args.wedge_smooth,
569
- "opening_axis": opening_axis,
570
- "tilt_axis": tilt_axis,
571
- "omit_negative_frequencies": True,
572
- }
573
- else:
574
- template_filter["continuous_wedge_mask"] = {
575
- "start_tilt": tilt_start,
576
- "stop_tilt": tilt_stop,
577
- "tilt_axis": tilt_axis,
578
- "opening_axis": opening_axis,
579
- "infinite_plane": True,
580
- "sigma": args.wedge_smooth,
581
- "omit_negative_frequencies": True,
582
- }
583
-
584
- if template_mask is None:
585
- template_mask = template.empty
586
- if not args.no_centering:
587
- enclosing_box = template.minimum_enclosing_box(
588
- 0, use_geometric_center=False
589
- )
590
- template_mask.adjust_box(enclosing_box)
591
-
592
- template_mask.data[:] = 1
593
- translation = np.zeros_like(translation)
594
-
595
- template_mask.pad(template.shape, center=False)
596
- origin_translation = np.divide(
597
- np.subtract(template.origin, template_mask.origin), template.sampling_rate
598
- )
599
- translation = np.add(translation, origin_translation)
600
-
601
- template_mask = template_mask.rigid_transform(
602
- rotation_matrix=np.eye(template_mask.data.ndim),
603
- translation=-translation,
604
- order=1,
605
- )
606
- template_mask.origin = template.origin.copy()
607
- print_block(
608
- name="Template Mask",
609
- data={
610
- "Inital Shape": initial_shape,
611
- "Sampling Rate": tuple(np.round(template_mask.sampling_rate, 2)),
612
- "Final Shape": template_mask.shape,
613
- },
614
- )
615
- print("\n" + "-" * 80)
616
-
617
- if args.scramble_phases:
618
- template.data = scramble_phases(
619
- template.data, noise_proportion=1.0, normalize_power=True
620
- )
621
-
622
- available_memory = backend.get_available_memory()
623
- if args.use_gpu:
624
- args.cores = len(args.gpu_indices)
625
- has_torch = importlib.util.find_spec("torch") is not None
626
- has_cupy = importlib.util.find_spec("cupy") is not None
627
-
628
- if not has_torch and not has_cupy:
629
- raise ValueError(
630
- "Found neither CuPy nor PyTorch installation. You need to install"
631
- " either to enable GPU support."
632
- )
633
-
634
- if args.peak_calling:
635
- preferred_backend = "pytorch"
636
- if not has_torch:
637
- preferred_backend = "cupy"
638
- backend.change_backend(backend_name=preferred_backend, device="cuda")
639
- else:
640
- preferred_backend = "cupy"
641
- if not has_cupy:
642
- preferred_backend = "pytorch"
643
- backend.change_backend(backend_name=preferred_backend, device="cuda")
644
- if args.use_mixed_precision and preferred_backend == "pytorch":
645
- raise NotImplementedError(
646
- "pytorch backend does not yet support mixed precision."
647
- " Consider installing CuPy to enable this feature."
648
- )
649
- elif args.use_mixed_precision:
650
- backend.change_backend(
651
- backend_name="cupy",
652
- default_dtype=backend._array_backend.float16,
653
- complex_dtype=backend._array_backend.complex64,
654
- default_dtype_int=backend._array_backend.int16,
655
- )
656
- available_memory = backend.get_available_memory() * args.cores
657
- if preferred_backend == "pytorch" and args.interpolation_order == 3:
658
- args.interpolation_order = 1
659
-
660
- if args.memory is None:
661
- args.memory = int(args.memory_scaling * available_memory)
662
-
663
- target_padding = np.zeros_like(template.shape)
664
- if args.pad_target_edges:
665
- target_padding = template.shape
666
-
667
- template_box = template.shape
668
- if not args.pad_fourier:
669
- template_box = np.ones(len(template_box), dtype=int)
670
-
671
- callback_class = MaxScoreOverRotations
672
- if args.peak_calling:
673
- callback_class = PeakCallerMaximumFilter
674
-
675
- splits, schedule = compute_parallelization_schedule(
676
- shape1=target.shape,
677
- shape2=template_box,
678
- shape1_padding=target_padding,
679
- max_cores=args.cores,
680
- max_ram=args.memory,
681
- split_only_outer=args.use_gpu,
682
- matching_method=args.score,
683
- analyzer_method=callback_class.__name__,
684
- backend=backend._backend_name,
685
- float_nbytes=backend.datatype_bytes(backend._default_dtype),
686
- complex_nbytes=backend.datatype_bytes(backend._complex_dtype),
687
- integer_nbytes=backend.datatype_bytes(backend._default_dtype_int),
688
- )
689
-
690
- if splits is None:
691
- print(
692
- "Found no suitable parallelization schedule. Consider increasing"
693
- " available RAM or decreasing number of cores."
694
- )
695
- exit(-1)
696
-
697
- analyzer_args = {
698
- "score_threshold": args.score_threshold,
699
- "number_of_peaks": 1000,
700
- "convolution_mode": "valid",
701
- "use_memmap": args.use_memmap,
702
- }
703
-
704
- matching_setup, matching_score = MATCHING_EXHAUSTIVE_REGISTER[args.score]
705
- matching_data = MatchingData(target=target, template=template.data)
706
- matching_data.rotations = get_rotation_matrices(
707
- angular_sampling=args.angular_sampling, dim=target.data.ndim
708
- )
709
- if args.angular_sampling >= 180:
710
- ndim = target.data.ndim
711
- matching_data.rotations = np.eye(ndim).reshape(1, ndim, ndim)
712
-
713
- matching_data.template_filter = template_filter
714
- matching_data._invert_target = args.invert_target_contrast
715
- if target_mask is not None:
716
- matching_data.target_mask = target_mask
717
- if template_mask is not None:
718
- matching_data.template_mask = template_mask.data
719
-
720
- n_splits = np.prod(list(splits.values()))
721
- target_split = ", ".join(
722
- [":".join([str(x) for x in axis]) for axis in splits.items()]
723
- )
724
- gpus_used = 0 if args.gpu_indices is None else len(args.gpu_indices)
725
- options = {
726
- "CPU Cores": args.cores,
727
- "Run on GPU": f"{args.use_gpu} [N={gpus_used}]",
728
- "Use Mixed Precision": args.use_mixed_precision,
729
- "Assigned Memory [MB]": f"{args.memory // 1e6} [out of {available_memory//1e6}]",
730
- "Temporary Directory": args.temp_directory,
731
- "Extend Fourier Grid": not args.no_fourier_padding,
732
- "Extend Target Edges": not args.no_edge_padding,
733
- "Interpolation Order": args.interpolation_order,
734
- "Score": f"{args.score}",
735
- "Setup Function": f"{get_func_fullname(matching_setup)}",
736
- "Scoring Function": f"{get_func_fullname(matching_score)}",
737
- "Angular Sampling": f"{args.angular_sampling}"
738
- f" [{matching_data.rotations.shape[0]} rotations]",
739
- "Scramble Template": args.scramble_phases,
740
- "Target Splits": f"{target_split} [N={n_splits}]",
741
- }
742
-
743
- print_block(
744
- name="Template Matching Options",
745
- data=options,
746
- label_width=max(len(key) for key in options.keys()) + 2,
747
- )
748
-
749
- options = {"Analyzer": callback_class, **analyzer_args}
750
- print_block(
751
- name="Score Analysis Options",
752
- data=options,
753
- label_width=max(len(key) for key in options.keys()) + 2,
754
- )
755
- print("\n" + "-" * 80)
756
-
757
- outer_jobs = f"{schedule[0]} job{'s' if schedule[0] > 1 else ''}"
758
- inner_jobs = f"{schedule[1]} core{'s' if schedule[1] > 1 else ''}"
759
- n_splits = f"{n_splits} split{'s' if n_splits > 1 else ''}"
760
- print(f"\nDistributing {n_splits} on {outer_jobs} each using {inner_jobs}.")
761
-
762
- start = time()
763
- print("Running Template Matching. This might take a while ...")
764
- candidates = scan_subsets(
765
- matching_data=matching_data,
766
- job_schedule=schedule,
767
- matching_score=matching_score,
768
- matching_setup=matching_setup,
769
- callback_class=callback_class,
770
- callback_class_args=analyzer_args,
771
- target_splits=splits,
772
- pad_target_edges=args.pad_target_edges,
773
- pad_fourier=args.pad_fourier,
774
- interpolation_order=args.interpolation_order,
775
- )
776
-
777
- candidates = list(candidates) if candidates is not None else []
778
- if callback_class == MaxScoreOverRotations:
779
- if target_mask is not None and args.score != "MCC":
780
- candidates[0] *= target_mask.data
781
- with warnings.catch_warnings():
782
- warnings.simplefilter("ignore", category=UserWarning)
783
- candidates[3] = {
784
- x: euler_from_rotationmatrix(
785
- np.frombuffer(i, dtype=matching_data.rotations.dtype).reshape(
786
- candidates[0].ndim, candidates[0].ndim
787
- )
788
- )
789
- for i, x in candidates[3].items()
790
- }
791
-
792
- candidates.append((target.origin, template.origin, target.sampling_rate, args))
793
- write_pickle(data=candidates, filename=args.output)
794
-
795
- runtime = time() - start
796
- print(f"\nRuntime real: {runtime:.3f}s user: {(runtime * args.cores):.3f}s.")
797
-
798
-
799
- if __name__ == "__main__":
800
- main()