pytme 0.2.9.post1__tar.gz → 0.3b0__tar.gz

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 (124) hide show
  1. {pytme-0.2.9.post1 → pytme-0.3b0}/PKG-INFO +10 -9
  2. {pytme-0.2.9.post1 → pytme-0.3b0}/pyproject.toml +9 -9
  3. {pytme-0.2.9.post1 → pytme-0.3b0}/pytme.egg-info/SOURCES.txt +6 -3
  4. pytme-0.2.9.post1/scripts/estimate_ram_usage.py → pytme-0.3b0/scripts/estimate_memory_usage.py +16 -33
  5. pytme-0.3b0/scripts/extract_candidates.py +224 -0
  6. {pytme-0.2.9.post1 → pytme-0.3b0}/scripts/match_template.py +224 -223
  7. {pytme-0.2.9.post1 → pytme-0.3b0}/scripts/postprocess.py +283 -163
  8. {pytme-0.2.9.post1 → pytme-0.3b0}/scripts/preprocess.py +11 -8
  9. {pytme-0.2.9.post1 → pytme-0.3b0}/scripts/preprocessor_gui.py +10 -9
  10. pytme-0.3b0/scripts/refine_matches.py +626 -0
  11. {pytme-0.2.9.post1 → pytme-0.3b0}/setup.py +1 -1
  12. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/preprocessing/test_frequency_filters.py +9 -4
  13. pytme-0.3b0/tests/test_analyzer.py +221 -0
  14. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/test_matching_cli.py +85 -30
  15. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/test_matching_exhaustive.py +1 -2
  16. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/test_matching_optimization.py +4 -9
  17. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/test_orientations.py +0 -1
  18. pytme-0.3b0/tme/__version__.py +1 -0
  19. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/analyzer/__init__.py +2 -0
  20. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/analyzer/_utils.py +25 -17
  21. pytme-0.3b0/tme/analyzer/aggregation.py +742 -0
  22. pytme-0.3b0/tme/analyzer/base.py +138 -0
  23. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/analyzer/peaks.py +150 -91
  24. pytme-0.3b0/tme/analyzer/proxy.py +122 -0
  25. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/backends/__init__.py +4 -3
  26. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/backends/_cupy_utils.py +25 -24
  27. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/backends/_jax_utils.py +4 -3
  28. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/backends/cupy_backend.py +4 -13
  29. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/backends/jax_backend.py +6 -8
  30. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/backends/matching_backend.py +4 -3
  31. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/backends/mlx_backend.py +4 -3
  32. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/backends/npfftw_backend.py +7 -5
  33. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/backends/pytorch_backend.py +14 -4
  34. pytme-0.3b0/tme/cli.py +126 -0
  35. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/density.py +4 -3
  36. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/filters/__init__.py +1 -1
  37. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/filters/_utils.py +4 -3
  38. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/filters/bandpass.py +6 -4
  39. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/filters/compose.py +5 -4
  40. pytme-0.3b0/tme/filters/ctf.py +605 -0
  41. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/filters/reconstruction.py +58 -28
  42. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/filters/wedge.py +139 -61
  43. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/filters/whitening.py +36 -36
  44. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/matching_data.py +4 -3
  45. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/matching_exhaustive.py +17 -16
  46. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/matching_optimization.py +5 -4
  47. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/matching_scores.py +4 -3
  48. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/matching_utils.py +41 -3
  49. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/memory.py +4 -3
  50. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/orientations.py +9 -6
  51. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/parser.py +5 -4
  52. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/preprocessor.py +4 -3
  53. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/rotations.py +10 -7
  54. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/structure.py +4 -3
  55. pytme-0.2.9.post1/tests/data/Maps/.DS_Store +0 -0
  56. pytme-0.2.9.post1/tests/data/Structures/.DS_Store +0 -0
  57. pytme-0.2.9.post1/tests/test_analyzer.py +0 -216
  58. pytme-0.2.9.post1/tme/__version__.py +0 -1
  59. pytme-0.2.9.post1/tme/analyzer/aggregation.py +0 -578
  60. pytme-0.2.9.post1/tme/filters/ctf.py +0 -393
  61. {pytme-0.2.9.post1 → pytme-0.3b0}/LICENSE +0 -0
  62. {pytme-0.2.9.post1 → pytme-0.3b0}/MANIFEST.in +0 -0
  63. {pytme-0.2.9.post1 → pytme-0.3b0}/README.md +0 -0
  64. {pytme-0.2.9.post1 → pytme-0.3b0}/scripts/__init__.py +0 -0
  65. {pytme-0.2.9.post1 → pytme-0.3b0}/setup.cfg +0 -0
  66. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/__init__.py +0 -0
  67. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/data/Blurring/blob_width18.npy +0 -0
  68. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/data/Blurring/edgegaussian_sigma3.npy +0 -0
  69. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/data/Blurring/gaussian_sigma2.npy +0 -0
  70. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/data/Blurring/hamming_width6.npy +0 -0
  71. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/data/Blurring/kaiserb_width18.npy +0 -0
  72. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/data/Blurring/localgaussian_sigma0510.npy +0 -0
  73. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/data/Blurring/mean_size5.npy +0 -0
  74. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/data/Blurring/ntree_sigma0510.npy +0 -0
  75. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/data/Blurring/rank_rank3.npy +0 -0
  76. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/data/Maps/emd_8621.mrc.gz +0 -0
  77. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/data/README.md +0 -0
  78. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/data/Raw/em_map.map +0 -0
  79. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/data/Structures/1pdj.cif +0 -0
  80. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/data/Structures/1pdj.pdb +0 -0
  81. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/data/Structures/5khe.cif +0 -0
  82. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/data/Structures/5khe.ent +0 -0
  83. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/data/Structures/5khe.pdb +0 -0
  84. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/data/Structures/5uz4.cif +0 -0
  85. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/preprocessing/__init__.py +0 -0
  86. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/preprocessing/test_compose.py +0 -0
  87. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/preprocessing/test_preprocessor.py +0 -0
  88. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/preprocessing/test_utils.py +0 -0
  89. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/test_backends.py +0 -0
  90. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/test_density.py +0 -0
  91. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/test_extensions.py +0 -0
  92. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/test_matching_data.py +0 -0
  93. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/test_matching_memory.py +0 -0
  94. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/test_matching_utils.py +0 -0
  95. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/test_parser.py +0 -0
  96. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/test_rotations.py +0 -0
  97. {pytme-0.2.9.post1 → pytme-0.3b0}/tests/test_structure.py +0 -0
  98. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/__init__.py +0 -0
  99. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/__init__.py +0 -0
  100. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/c48n309.npy +0 -0
  101. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/c48n527.npy +0 -0
  102. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/c48n9.npy +0 -0
  103. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/c48u1.npy +0 -0
  104. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/c48u1153.npy +0 -0
  105. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/c48u1201.npy +0 -0
  106. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/c48u1641.npy +0 -0
  107. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/c48u181.npy +0 -0
  108. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/c48u2219.npy +0 -0
  109. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/c48u27.npy +0 -0
  110. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/c48u2947.npy +0 -0
  111. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/c48u3733.npy +0 -0
  112. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/c48u4749.npy +0 -0
  113. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/c48u5879.npy +0 -0
  114. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/c48u7111.npy +0 -0
  115. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/c48u815.npy +0 -0
  116. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/c48u83.npy +0 -0
  117. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/c48u8649.npy +0 -0
  118. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/c600v.npy +0 -0
  119. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/c600vc.npy +0 -0
  120. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/metadata.yaml +0 -0
  121. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/quat_to_numpy.py +0 -0
  122. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/data/scattering_factors.pickle +0 -0
  123. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/external/bindings.cpp +0 -0
  124. {pytme-0.2.9.post1 → pytme-0.3b0}/tme/types.py +0 -0
@@ -1,10 +1,10 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pytme
3
- Version: 0.2.9.post1
3
+ Version: 0.3b0
4
4
  Summary: Python Template Matching Engine
5
5
  Author: Valentin Maurer
6
6
  Author-email: Valentin Maurer <valentin.maurer@embl-hamburg.de>
7
- License: Proprietary
7
+ License-Expression: GPL-2.0-only
8
8
  Project-URL: Homepage, https://github.com/KosinskiLab/pyTME
9
9
  Classifier: Programming Language :: Python :: 3
10
10
  Classifier: Operating System :: OS Independent
@@ -12,7 +12,7 @@ Requires-Python: >=3.11
12
12
  Description-Content-Type: text/markdown
13
13
  License-File: LICENSE
14
14
  Requires-Dist: mrcfile>=1.4.3
15
- Requires-Dist: numpy<2.0,>=1.22.2
15
+ Requires-Dist: numpy>=1.22.2
16
16
  Requires-Dist: scipy>=1.9.1
17
17
  Requires-Dist: pyfftw>=0.13.1
18
18
  Requires-Dist: pytest>=6.2.5
@@ -25,19 +25,20 @@ Requires-Dist: h5py
25
25
  Requires-Dist: importlib_resources
26
26
  Requires-Dist: joblib
27
27
  Provides-Extra: cupy
28
- Requires-Dist: cupy-cuda12x; extra == "cupy"
29
- Requires-Dist: voltools==0.6.0; extra == "cupy"
28
+ Requires-Dist: cupy-cuda12x>=13.0.0; extra == "cupy"
30
29
  Provides-Extra: pytorch
31
30
  Requires-Dist: torch; extra == "pytorch"
32
31
  Requires-Dist: torchvision; extra == "pytorch"
33
32
  Provides-Extra: jax
34
- Requires-Dist: jax; extra == "jax"
33
+ Requires-Dist: jax[cuda12]; extra == "jax"
35
34
  Requires-Dist: jaxlib; extra == "jax"
35
+ Provides-Extra: jax-cpu
36
+ Requires-Dist: jax; extra == "jax-cpu"
37
+ Requires-Dist: jaxlib; extra == "jax-cpu"
36
38
  Provides-Extra: all
37
- Requires-Dist: cupy; extra == "all"
38
- Requires-Dist: voltools==0.6.0; extra == "all"
39
+ Requires-Dist: cupy-cuda12x>=13.0.0; extra == "all"
39
40
  Requires-Dist: torch; extra == "all"
40
- Requires-Dist: jax; extra == "all"
41
+ Requires-Dist: jax[cuda12]; extra == "all"
41
42
  Requires-Dist: jaxlib; extra == "all"
42
43
  Requires-Dist: tifffile; extra == "all"
43
44
  Dynamic: author
@@ -7,13 +7,13 @@ name="pytme"
7
7
  authors = [
8
8
  { name = "Valentin Maurer", email = "valentin.maurer@embl-hamburg.de" },
9
9
  ]
10
- version="0.2.9.post1"
10
+ version="0.3.b0"
11
11
  description="Python Template Matching Engine"
12
12
  readme="README.md"
13
13
  requires-python = ">=3.11"
14
14
  dependencies=[
15
15
  "mrcfile>=1.4.3",
16
- "numpy>=1.22.2,<2.0",
16
+ "numpy>=1.22.2",
17
17
  "scipy>=1.9.1",
18
18
  "pyfftw>=0.13.1",
19
19
  "pytest>=6.2.5",
@@ -26,28 +26,28 @@ dependencies=[
26
26
  "importlib_resources",
27
27
  "joblib"
28
28
  ]
29
- license = {text = "Proprietary"}
29
+ license = "GPL-2.0-only"
30
30
  classifiers = [
31
31
  "Programming Language :: Python :: 3",
32
32
  "Operating System :: OS Independent",
33
33
  ]
34
34
 
35
35
  [project.optional-dependencies]
36
- cupy = ["cupy-cuda12x", "voltools==0.6.0"]
36
+ cupy = ["cupy-cuda12x>=13.0.0"]
37
37
  pytorch = ["torch", "torchvision"]
38
- jax = ["jax", "jaxlib"]
38
+ jax = ["jax[cuda12]", "jaxlib"]
39
+ jax_cpu = ["jax", "jaxlib"]
39
40
  all = [
40
- "cupy",
41
- "voltools==0.6.0",
41
+ "cupy-cuda12x>=13.0.0",
42
42
  "torch",
43
- "jax",
43
+ "jax[cuda12]",
44
44
  "jaxlib",
45
45
  "tifffile"
46
46
  ]
47
47
 
48
48
  [project.scripts]
49
49
  match_template = "scripts:match_template.main"
50
- estimate_ram_usage = "scripts:estimate_ram_usage.main"
50
+ estimate_memory_usage = "scripts:estimate_memory_usage.main"
51
51
  preprocessor_gui = "scripts:preprocessor_gui.main"
52
52
  preprocess = "scripts:preprocess.main"
53
53
  postprocess = "scripts:postprocess.main"
@@ -4,11 +4,13 @@ README.md
4
4
  pyproject.toml
5
5
  setup.py
6
6
  scripts/__init__.py
7
- scripts/estimate_ram_usage.py
7
+ scripts/estimate_memory_usage.py
8
+ scripts/extract_candidates.py
8
9
  scripts/match_template.py
9
10
  scripts/postprocess.py
10
11
  scripts/preprocess.py
11
12
  scripts/preprocessor_gui.py
13
+ scripts/refine_matches.py
12
14
  tests/__init__.py
13
15
  tests/test_analyzer.py
14
16
  tests/test_backends.py
@@ -34,10 +36,8 @@ tests/data/Blurring/localgaussian_sigma0510.npy
34
36
  tests/data/Blurring/mean_size5.npy
35
37
  tests/data/Blurring/ntree_sigma0510.npy
36
38
  tests/data/Blurring/rank_rank3.npy
37
- tests/data/Maps/.DS_Store
38
39
  tests/data/Maps/emd_8621.mrc.gz
39
40
  tests/data/Raw/em_map.map
40
- tests/data/Structures/.DS_Store
41
41
  tests/data/Structures/1pdj.cif
42
42
  tests/data/Structures/1pdj.pdb
43
43
  tests/data/Structures/5khe.cif
@@ -51,6 +51,7 @@ tests/preprocessing/test_preprocessor.py
51
51
  tests/preprocessing/test_utils.py
52
52
  tme/__init__.py
53
53
  tme/__version__.py
54
+ tme/cli.py
54
55
  tme/density.py
55
56
  tme/matching_data.py
56
57
  tme/matching_exhaustive.py
@@ -67,7 +68,9 @@ tme/types.py
67
68
  tme/analyzer/__init__.py
68
69
  tme/analyzer/_utils.py
69
70
  tme/analyzer/aggregation.py
71
+ tme/analyzer/base.py
70
72
  tme/analyzer/peaks.py
73
+ tme/analyzer/proxy.py
71
74
  tme/backends/__init__.py
72
75
  tme/backends/_cupy_utils.py
73
76
  tme/backends/_jax_utils.py
@@ -1,20 +1,21 @@
1
1
  #!python3
2
- """ Estimate RAM requirements for template matching jobs.
2
+ """Estimate memory requirements for template matching jobs.
3
3
 
4
- Copyright (c) 2023 European Molecular Biology Laboratory
4
+ Copyright (c) 2023 European Molecular Biology Laboratory
5
5
 
6
- Author: Valentin Maurer <valentin.maurer@embl-hamburg.de>
6
+ Author: Valentin Maurer <valentin.maurer@embl-hamburg.de>
7
7
  """
8
8
  import numpy as np
9
9
  import argparse
10
10
  from tme import Density
11
- from tme.matching_utils import estimate_ram_usage
11
+ from tme.memory import estimate_memory_usage
12
12
  from tme.matching_exhaustive import MATCHING_EXHAUSTIVE_REGISTER
13
13
 
14
14
 
15
15
  def parse_args():
16
16
  parser = argparse.ArgumentParser(
17
- description="Estimate RAM usage for template matching."
17
+ description="Estimate memory usage for template matching.",
18
+ formatter_class=argparse.ArgumentDefaultsHelpFormatter,
18
19
  )
19
20
  parser.add_argument(
20
21
  "-m",
@@ -32,14 +33,9 @@ def parse_args():
32
33
  required=True,
33
34
  help="Path to a template in PDB/MMCIF or CCP4/MRC format.",
34
35
  )
35
- parser.add_argument(
36
- "--matching_method",
37
- required=False,
38
- default=None,
39
- help="Analyzer method to use.",
40
- )
41
36
  parser.add_argument(
42
37
  "-s",
38
+ "--score",
43
39
  dest="score",
44
40
  type=str,
45
41
  default="FLCSphericalMask",
@@ -50,21 +46,12 @@ def parse_args():
50
46
  "--ncores", type=int, help="Number of cores for parallelization.", required=True
51
47
  )
52
48
  parser.add_argument(
53
- "--no_edge_padding",
54
- dest="no_edge_padding",
49
+ "--pad_edges",
50
+ dest="pad_edges",
55
51
  action="store_true",
56
52
  default=False,
57
- help="Whether to pad the edges of the target. This is useful, if the target"
58
- " has a well defined bounding box, e.g. a density map.",
59
- )
60
- parser.add_argument(
61
- "--no_fourier_padding",
62
- dest="no_fourier_padding",
63
- action="store_true",
64
- default=False,
65
- help="Whether input arrays should be zero-padded to the full convolution shape"
66
- " for numerical stability. When working with very large targets such as"
67
- " tomograms it is safe to use this flag and benefit from the performance gain.",
53
+ help="Whether to pad the edges of the target. Useful if the target does not "
54
+ "a well-defined bounding box. Defaults to True if splitting is required.",
68
55
  )
69
56
  args = parser.parse_args()
70
57
  return args
@@ -72,19 +59,15 @@ def parse_args():
72
59
 
73
60
  def main():
74
61
  args = parse_args()
75
- target = Density.from_file(args.target)
76
- template = Density.from_file(args.template)
77
-
78
- target_box = target.shape
79
- if not args.no_edge_padding:
80
- target_box = np.add(target_box, template.shape)
62
+ target = Density.from_file(args.target, use_memmap=True)
63
+ template = Density.from_file(args.template, use_memmap=True)
81
64
 
82
65
  template_box = template.shape
83
- if args.no_fourier_padding:
66
+ if not args.pad_edges:
84
67
  template_box = np.ones(len(template_box), dtype=int)
85
68
 
86
- result = estimate_ram_usage(
87
- shape1=target_box,
69
+ result = estimate_memory_usage(
70
+ shape1=target.shape,
88
71
  shape2=template_box,
89
72
  matching_method=args.score,
90
73
  ncores=args.ncores,
@@ -0,0 +1,224 @@
1
+ #!python3
2
+ """ Prepare orientations stack for refinement.
3
+
4
+ Copyright (c) 2023 European Molecular Biology Laboratory
5
+
6
+ Author: Valentin Maurer <valentin.maurer@embl-hamburg.de>
7
+ """
8
+ import argparse
9
+ from os.path import splitext
10
+
11
+ import numpy as np
12
+
13
+ from tme import Density, Orientations
14
+ from tme.matching_utils import (
15
+ generate_tempfile_name,
16
+ rotation_aligning_vectors,
17
+ euler_from_rotationmatrix,
18
+ euler_to_rotationmatrix,
19
+ )
20
+
21
+
22
+ class ProgressBar:
23
+ """
24
+ ASCII progress bar.
25
+ """
26
+
27
+ def __init__(self, message: str, nchars: int, total: int):
28
+ self._size = nchars - len(message) - (len(str(total))+2) * 2
29
+ self._message = message
30
+ self._total = total
31
+
32
+ def update(self, cur):
33
+ x = int(cur * self._size / self._total)
34
+ print(
35
+ "%s[%s%s] %i/%i\r"
36
+ % (self._message, "#" * x, "." * (self._size - x), cur, self._total),
37
+ end="",
38
+ )
39
+
40
+
41
+ def parse_args():
42
+ parser = argparse.ArgumentParser(
43
+ description="Extract matching candidates for further refinement.",
44
+ formatter_class=argparse.ArgumentDefaultsHelpFormatter,
45
+ )
46
+
47
+ io_group = parser.add_argument_group("Input / Output")
48
+ io_group.add_argument(
49
+ "--target",
50
+ required=True,
51
+ type=str,
52
+ help="Extract candidates from this target.",
53
+ )
54
+ io_group.add_argument(
55
+ "--orientations",
56
+ required=True,
57
+ type=str,
58
+ help="Path to file generated by postprocess.py using output_format orientations.",
59
+ )
60
+ io_group.add_argument(
61
+ "--orientations_sampling",
62
+ required=False,
63
+ type=float,
64
+ default=1.0,
65
+ help="Factor to map candidate coordinates onto the target. Only relevant if "
66
+ "target sampling rate differs from candidate orientation sampling rate.",
67
+ )
68
+ io_group.add_argument(
69
+ "-o",
70
+ "--output_file",
71
+ required=True,
72
+ type=str,
73
+ help="Path to write output H5 file.",
74
+ )
75
+
76
+ alignment_group = parser.add_argument_group("Alignment")
77
+ alignment_group.add_argument(
78
+ "--align_orientations",
79
+ action="store_true",
80
+ required=False,
81
+ help="Whether to align extracted orientations based on their angles. Allows "
82
+ "for efficient subsequent sampling of cone angles.",
83
+ )
84
+ alignment_group.add_argument(
85
+ "--angles_are_vector",
86
+ action="store_true",
87
+ required=False,
88
+ help="Considers euler_z euler_y, euler_x as vector that will be rotated to align "
89
+ "with the z-axis (1,0,0). Only considered when --align_orientations is set.",
90
+ )
91
+ alignment_group.add_argument(
92
+ "--interpolation_order",
93
+ dest="interpolation_order",
94
+ required=False,
95
+ type=int,
96
+ default=1,
97
+ help="Interpolation order for alignment, less than zero is no interpolation.",
98
+ )
99
+
100
+ extraction_group = parser.add_argument_group("Extraction")
101
+ extraction_group.add_argument(
102
+ "--box_size",
103
+ required=False,
104
+ type=int,
105
+ help="Box size for extraction, defaults to two times the template.",
106
+ )
107
+ extraction_group.add_argument(
108
+ "--translation_uncertainty",
109
+ required=False,
110
+ type=int,
111
+ help="Sets box size for extraction to template box plus this value.",
112
+ )
113
+ extraction_group.add_argument(
114
+ "--keep_out_of_box",
115
+ action="store_true",
116
+ required=False,
117
+ help="Whether to keep orientations that fall outside the box. If the "
118
+ "orientations are sensible, it is safe to pass this flag.",
119
+ )
120
+
121
+ args = parser.parse_args()
122
+
123
+ return args
124
+
125
+
126
+ def main():
127
+ args = parse_args()
128
+ orientations = Orientations.from_file(args.orientations)
129
+ orientations.translations = np.divide(
130
+ orientations.translations, args.orientations_sampling
131
+ )
132
+
133
+ target = Density.from_file(args.target, use_memmap=True)
134
+
135
+ box_size = np.array(args.box_size)
136
+ box_size = np.repeat(box_size, target.data.ndim // box_size.size).astype(int)
137
+
138
+ extraction_shape = np.copy(box_size)
139
+ if args.align_orientations:
140
+ extraction_shape[:] = int(np.linalg.norm(box_size) + 1)
141
+
142
+ orientations, cand_slices, obs_slices = orientations.get_extraction_slices(
143
+ target_shape=target.shape,
144
+ extraction_shape=extraction_shape,
145
+ drop_out_of_box=not args.keep_out_of_box,
146
+ return_orientations=True,
147
+ )
148
+
149
+ if args.align_orientations:
150
+ for index in range(orientations.rotations.shape[0]):
151
+ rotation_matrix = euler_to_rotationmatrix(orientations.rotations[index])
152
+ rotation_matrix = np.linalg.inv(rotation_matrix)
153
+ if args.angles_are_vector:
154
+ rotation_matrix = rotation_aligning_vectors(
155
+ orientations.rotations[index], target_vector=(1, 0, 0)
156
+ )
157
+ orientations.rotations[index] = euler_from_rotationmatrix(rotation_matrix)
158
+
159
+ filename = generate_tempfile_name()
160
+ output_dtype = target.data.dtype
161
+ if args.align_orientations is not None:
162
+ output_dtype = np.float32
163
+
164
+ target.data = target.data.astype(output_dtype)
165
+
166
+ dens = Density(
167
+ np.memmap(
168
+ filename,
169
+ mode="w+",
170
+ shape=(len(obs_slices), *box_size),
171
+ dtype=output_dtype,
172
+ ),
173
+ sampling_rate=(1, *target.sampling_rate),
174
+ origin=(0, *target.origin),
175
+ )
176
+ dens.data[:] = target.metadata["mean"]
177
+
178
+ data_subset = np.zeros(extraction_shape, dtype=target.data.dtype)
179
+ pbar = ProgressBar(message="Aligning ", nchars=80, total=len(obs_slices))
180
+ for index, (obs_slice, cand_slice) in enumerate(zip(obs_slices, cand_slices)):
181
+ pbar.update(index + 1)
182
+
183
+ data_subset.fill(0)
184
+ data_subset[cand_slice] = target.data[obs_slice]
185
+ target_subset = Density(
186
+ data_subset,
187
+ sampling_rate=target.sampling_rate,
188
+ origin=target.origin,
189
+ )
190
+
191
+ if args.align_orientations:
192
+ rotation_matrix = euler_to_rotationmatrix(orientations.rotations[index])
193
+ target_subset = target_subset.rigid_transform(
194
+ rotation_matrix=rotation_matrix,
195
+ use_geometric_center=True,
196
+ order=args.interpolation_order,
197
+ )
198
+ target_subset.pad(box_size, center=True)
199
+
200
+ # target_value = target.data[tuple(orientations.translations[index].astype(int))]
201
+ # center = np.divide(target_subset.data.shape, 2).astype(int)
202
+ # print(np.where(target_subset.data == target_value), center)
203
+ # print(target_subset.data[tuple(center.astype(int))],
204
+ # target_value,
205
+ # target_subset.data[tuple(center.astype(int))] == target_value
206
+ # )
207
+
208
+ dens.data[index] = target_subset.data
209
+ print("")
210
+
211
+ target_meta = {
212
+ k: v for k, v in target.metadata.items() if k in ("mean", "max", "min", "std")
213
+ }
214
+ dens.metadata.update(target_meta)
215
+ dens.metadata["batch_dimension"] = (0,)
216
+ dens.metadata["normals"] = orientations.rotations
217
+
218
+ dens.to_file(args.output_file)
219
+ orientations.to_file(
220
+ f"{splitext(args.output_file)[0]}_aligned.tsv", file_format="text"
221
+ )
222
+
223
+ if __name__ == "__main__":
224
+ main()