tm2d 0.2.0__tar.gz → 0.3.0__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.
- tm2d-0.3.0/PKG-INFO +57 -0
- tm2d-0.3.0/README.md +38 -0
- {tm2d-0.2.0 → tm2d-0.3.0}/pyproject.toml +1 -1
- {tm2d-0.2.0 → tm2d-0.3.0}/tm2d/__init__.py +8 -3
- {tm2d-0.2.0 → tm2d-0.3.0}/tm2d/base_comparator.py +1 -1
- {tm2d-0.2.0 → tm2d-0.3.0}/tm2d/base_results.py +1 -1
- {tm2d-0.2.0 → tm2d-0.3.0}/tm2d/base_template.py +5 -5
- {tm2d-0.2.0 → tm2d-0.3.0}/tm2d/comparators/cross_correlation.py +96 -5
- {tm2d-0.2.0 → tm2d-0.3.0}/tm2d/ctf/ctf.py +33 -1
- {tm2d-0.2.0 → tm2d-0.3.0}/tm2d/ctf/ctf_params.py +3 -170
- tm2d-0.3.0/tm2d/ctf/ctf_set.py +89 -0
- {tm2d-0.2.0 → tm2d-0.3.0}/tm2d/param_set.py +24 -19
- {tm2d-0.2.0 → tm2d-0.3.0}/tm2d/plan.py +41 -30
- {tm2d-0.2.0 → tm2d-0.3.0}/tm2d/results/per_param.py +65 -12
- {tm2d-0.2.0 → tm2d-0.3.0}/tm2d/results/per_pixel.py +36 -19
- {tm2d-0.2.0 → tm2d-0.3.0}/tm2d/templates/atomic.py +61 -47
- {tm2d-0.2.0 → tm2d-0.3.0}/tm2d/templates/density.py +82 -33
- tm2d-0.3.0/tm2d.egg-info/PKG-INFO +57 -0
- tm2d-0.2.0/PKG-INFO +0 -91
- tm2d-0.2.0/README.md +0 -72
- tm2d-0.2.0/tm2d/ctf/ctf_set.py +0 -64
- tm2d-0.2.0/tm2d.egg-info/PKG-INFO +0 -91
- {tm2d-0.2.0 → tm2d-0.3.0}/LICENSE +0 -0
- {tm2d-0.2.0 → tm2d-0.3.0}/setup.cfg +0 -0
- {tm2d-0.2.0 → tm2d-0.3.0}/tm2d.egg-info/SOURCES.txt +0 -0
- {tm2d-0.2.0 → tm2d-0.3.0}/tm2d.egg-info/dependency_links.txt +0 -0
- {tm2d-0.2.0 → tm2d-0.3.0}/tm2d.egg-info/requires.txt +0 -0
- {tm2d-0.2.0 → tm2d-0.3.0}/tm2d.egg-info/top_level.txt +0 -0
tm2d-0.3.0/PKG-INFO
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tm2d
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: A Python module for 2D template matching
|
|
5
|
+
Author-email: Shahar Sandhaus <shahar.sandhaus@berkeley.edu>
|
|
6
|
+
Project-URL: Homepage, https://github.com/sharhar/tm2d
|
|
7
|
+
Project-URL: Issues, https://github.com/sharhar/tm2d/issues
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
12
|
+
Requires-Python: >=3.9
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: setuptools>=59.0
|
|
16
|
+
Requires-Dist: numpy
|
|
17
|
+
Requires-Dist: vkdispatch==0.1.0
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
|
|
20
|
+
# tm2d
|
|
21
|
+
|
|
22
|
+
`tm2d` is a Python package for GPU-accelerated 2D template matching. It contains the core algorithmic pieces: templates, CTF parameter handling, parameter grids, comparators, result containers, and execution plans.
|
|
23
|
+
|
|
24
|
+
Higher-level workflow code, file loading helpers, plotting utilities, and analysis scripts belong in `tm2d-utils` or downstream project code.
|
|
25
|
+
|
|
26
|
+
## Dependencies
|
|
27
|
+
|
|
28
|
+
Runtime dependencies are listed in `pyproject.toml`:
|
|
29
|
+
|
|
30
|
+
- Python >= 3.9
|
|
31
|
+
- `numpy`
|
|
32
|
+
- `vkdispatch==0.1.0`
|
|
33
|
+
- `setuptools>=59.0`
|
|
34
|
+
|
|
35
|
+
A working Vulkan/vkdispatch setup is required to run template matching.
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
From a local checkout:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
cd /path/to/tm2d
|
|
43
|
+
python -m pip install .
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
For development, install in editable mode:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
cd /path/to/tm2d
|
|
50
|
+
python -m pip install -e .
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
To verify the installed package version:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
python -m pip show tm2d
|
|
57
|
+
```
|
tm2d-0.3.0/README.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# tm2d
|
|
2
|
+
|
|
3
|
+
`tm2d` is a Python package for GPU-accelerated 2D template matching. It contains the core algorithmic pieces: templates, CTF parameter handling, parameter grids, comparators, result containers, and execution plans.
|
|
4
|
+
|
|
5
|
+
Higher-level workflow code, file loading helpers, plotting utilities, and analysis scripts belong in `tm2d-utils` or downstream project code.
|
|
6
|
+
|
|
7
|
+
## Dependencies
|
|
8
|
+
|
|
9
|
+
Runtime dependencies are listed in `pyproject.toml`:
|
|
10
|
+
|
|
11
|
+
- Python >= 3.9
|
|
12
|
+
- `numpy`
|
|
13
|
+
- `vkdispatch==0.1.0`
|
|
14
|
+
- `setuptools>=59.0`
|
|
15
|
+
|
|
16
|
+
A working Vulkan/vkdispatch setup is required to run template matching.
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
From a local checkout:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
cd /path/to/tm2d
|
|
24
|
+
python -m pip install .
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
For development, install in editable mode:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
cd /path/to/tm2d
|
|
31
|
+
python -m pip install -e .
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
To verify the installed package version:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
python -m pip show tm2d
|
|
38
|
+
```
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
from .ctf.ctf_params import CTFParams
|
|
2
|
-
from .ctf.ctf_set import CTFSet
|
|
3
|
-
from .ctf.ctf import ctf_filter
|
|
2
|
+
from .ctf.ctf_set import CTFSet, make_ctf_set
|
|
3
|
+
from .ctf.ctf import ctf_filter, apply_ctfs_to_rfft_signals
|
|
4
4
|
|
|
5
5
|
from .templates.atomic import TemplateAtomic
|
|
6
6
|
from .templates.density import TemplateDensity
|
|
@@ -10,12 +10,16 @@ from .comparators.cross_correlation import ComparatorCrossCorrelation
|
|
|
10
10
|
from .results.per_pixel import ResultsPixel
|
|
11
11
|
from .results.per_param import ResultsParam
|
|
12
12
|
|
|
13
|
-
from .
|
|
13
|
+
from .param_set import ParamSet, make_param_set
|
|
14
|
+
|
|
15
|
+
from .plan import Plan, Template, Comparator, Results
|
|
14
16
|
|
|
15
17
|
__all__ = [
|
|
16
18
|
"CTFParams",
|
|
17
19
|
"CTFSet",
|
|
20
|
+
"make_ctf_set",
|
|
18
21
|
"ctf_filter",
|
|
22
|
+
"apply_ctfs_to_rfft_signals"
|
|
19
23
|
"TemplateAtomic",
|
|
20
24
|
"TemplateDensity",
|
|
21
25
|
"ComparatorCrossCorrelation",
|
|
@@ -26,4 +30,5 @@ __all__ = [
|
|
|
26
30
|
"Comparator",
|
|
27
31
|
"Results",
|
|
28
32
|
"ParamSet",
|
|
33
|
+
"make_param_set",
|
|
29
34
|
]
|
|
@@ -11,5 +11,5 @@ class Comparator:
|
|
|
11
11
|
"""
|
|
12
12
|
raise NotImplementedError("set_data must be implemented in a subclass.")
|
|
13
13
|
|
|
14
|
-
def compare_template(self, template: vd.RFFTBuffer, normalize: bool = True) -> vd.RFFTBuffer:
|
|
14
|
+
def compare_template(self, template: vd.RFFTBuffer, normalize: bool = True, output_radius: int = None) -> vd.RFFTBuffer:
|
|
15
15
|
raise NotImplementedError("compare_template must be implemented in a subclass.")
|
|
@@ -5,7 +5,7 @@ class Results:
|
|
|
5
5
|
def __init__(self):
|
|
6
6
|
raise NotImplementedError("Results is an abstract class. Please implement it in a subclass.")
|
|
7
7
|
|
|
8
|
-
def check_comparison(self, comparison_buffer: vd.RFFTBuffer, *
|
|
8
|
+
def check_comparison(self, comparison_buffer: vd.RFFTBuffer, rotation_weights: vc.Var[vc.f32], *indices: vc.Var[vc.i32]):
|
|
9
9
|
raise NotImplementedError("check_comparison must be implemented in a subclass.")
|
|
10
10
|
|
|
11
11
|
def reset(self):
|
|
@@ -48,7 +48,7 @@ class Template:
|
|
|
48
48
|
else:
|
|
49
49
|
assert rotations.shape[1] == 3 , "Rotations must be a 2D array with shape (N, 3)."
|
|
50
50
|
|
|
51
|
-
rotations = self.
|
|
51
|
+
rotations = self.get_rotation_matrices(rotations)
|
|
52
52
|
|
|
53
53
|
return self._make_template(
|
|
54
54
|
rotations=rotations,
|
|
@@ -58,17 +58,17 @@ class Template:
|
|
|
58
58
|
cmd_graph=cmd_graph
|
|
59
59
|
)
|
|
60
60
|
|
|
61
|
-
def
|
|
62
|
-
raise NotImplementedError("
|
|
61
|
+
def _get_rotation_matrices(self, rotations: np.ndarray) -> np.ndarray:
|
|
62
|
+
raise NotImplementedError("get_rotation_matrices must be implemented in a subclass.")
|
|
63
63
|
|
|
64
|
-
def
|
|
64
|
+
def get_rotation_matrices(self, rotations: Union[np.ndarray, Tuple[float, float, float]]) -> np.ndarray:
|
|
65
65
|
if not isinstance(rotations, np.ndarray):
|
|
66
66
|
rotations = np.array(rotations, dtype=np.float32).reshape(1, 3)
|
|
67
67
|
|
|
68
68
|
if rotations.ndim == 1:
|
|
69
69
|
rotations = rotations.reshape(1, 3)
|
|
70
70
|
|
|
71
|
-
return self.
|
|
71
|
+
return self._get_rotation_matrices(rotations)
|
|
72
72
|
|
|
73
73
|
def get_shape(self) -> tuple:
|
|
74
74
|
"""
|
|
@@ -77,7 +77,7 @@ class ComparatorCrossCorrelation(Comparator):
|
|
|
77
77
|
data = np.fft.rfft2(data).astype(np.complex64)
|
|
78
78
|
self.micrographs_buffer.write_fourier(data)
|
|
79
79
|
|
|
80
|
-
def compare_template(self, template_buffer: vd.RFFTBuffer, normalize: bool = True) -> vd.RFFTBuffer:
|
|
80
|
+
def compare_template(self, template_buffer: vd.RFFTBuffer, normalize: bool = True, output_radius: int = None) -> vd.RFFTBuffer:
|
|
81
81
|
assert normalize, "Normalization is mandatory for cross-correlation."
|
|
82
82
|
|
|
83
83
|
correlation_shape = (
|
|
@@ -87,6 +87,7 @@ class ComparatorCrossCorrelation(Comparator):
|
|
|
87
87
|
)
|
|
88
88
|
|
|
89
89
|
correlation_signal = vd.RFFTBuffer(correlation_shape)
|
|
90
|
+
correlation_signal_out = vd.RFFTBuffer(correlation_shape)
|
|
90
91
|
|
|
91
92
|
template_sum = calc_sums(template_buffer)
|
|
92
93
|
|
|
@@ -108,6 +109,16 @@ class ComparatorCrossCorrelation(Comparator):
|
|
|
108
109
|
self.micrographs_buffer.shape[1] * self.micrographs_buffer.shape[2]
|
|
109
110
|
)
|
|
110
111
|
|
|
112
|
+
@vd.map
|
|
113
|
+
def conv_input_map(input_buffer: vc.Buffer[c64]):
|
|
114
|
+
with vc.if_block(vc.all(
|
|
115
|
+
vd.fft.read_op().fft_index >= self.micrographs_buffer.shape[1] // 2 - template_buffer.shape[1]// 2,
|
|
116
|
+
vd.fft.read_op().fft_index < self.micrographs_buffer.shape[1] // 2 + template_buffer.shape[1] // 2)):
|
|
117
|
+
vd.fft.read_op().read_from_buffer(input_buffer)
|
|
118
|
+
with vc.else_block():
|
|
119
|
+
vd.fft.read_op().register.real = 0.0
|
|
120
|
+
vd.fft.read_op().register.imag = 0.0
|
|
121
|
+
|
|
111
122
|
@vd.map
|
|
112
123
|
def convolution_map(kernel_buffer: vc.Buffer[c64]):
|
|
113
124
|
img_val = vd.fft.read_op().register
|
|
@@ -118,11 +129,20 @@ class ComparatorCrossCorrelation(Comparator):
|
|
|
118
129
|
|
|
119
130
|
@vd.map
|
|
120
131
|
def output_map_func(output_buffer: vc.Buffer[c64]):
|
|
121
|
-
|
|
122
|
-
|
|
132
|
+
if output_radius is None:
|
|
133
|
+
out_reg = vd.fft.write_op().register
|
|
134
|
+
output_buffer[vd.fft.write_op().io_index + kernel_offset * template_buffer.shape[0] * vd.fft.mapped_kernel_index()] = out_reg
|
|
135
|
+
return
|
|
136
|
+
|
|
137
|
+
with vc.if_block(vc.any(
|
|
138
|
+
vd.fft.write_op().fft_index < output_radius,
|
|
139
|
+
vd.fft.write_op().fft_index >= self.micrographs_buffer.shape[1] - output_radius)):
|
|
140
|
+
|
|
141
|
+
out_reg = vd.fft.write_op().register
|
|
142
|
+
output_buffer[vd.fft.write_op().io_index + kernel_offset * template_buffer.shape[0] * vd.fft.mapped_kernel_index()] = out_reg
|
|
123
143
|
|
|
124
144
|
vd.fft.convolve(
|
|
125
|
-
|
|
145
|
+
correlation_signal_out,
|
|
126
146
|
correlation_signal,
|
|
127
147
|
self.micrographs_buffer,
|
|
128
148
|
kernel_num=self.micrographs_buffer.shape[0],
|
|
@@ -132,10 +152,81 @@ class ComparatorCrossCorrelation(Comparator):
|
|
|
132
152
|
self.micrographs_buffer.shape[1],
|
|
133
153
|
self.micrographs_buffer.shape[2]
|
|
134
154
|
),
|
|
155
|
+
input_map=conv_input_map,
|
|
135
156
|
kernel_map=convolution_map,
|
|
136
157
|
output_map=output_map_func
|
|
137
158
|
)
|
|
138
159
|
|
|
139
|
-
|
|
160
|
+
if output_radius is None:
|
|
161
|
+
vd.fft.irfft(correlation_signal_out)
|
|
162
|
+
return correlation_signal_out
|
|
163
|
+
|
|
164
|
+
trimmed_shape = (
|
|
165
|
+
correlation_signal.real_shape[0],
|
|
166
|
+
output_radius * 2,
|
|
167
|
+
correlation_signal.real_shape[2]
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
trimmed_shape_2 = (
|
|
171
|
+
correlation_signal.shape[0],
|
|
172
|
+
output_radius * 2,
|
|
173
|
+
correlation_signal.shape[2] * 2
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
trimmed_shape_3 = (
|
|
177
|
+
correlation_signal.shape[0],
|
|
178
|
+
output_radius * 2,
|
|
179
|
+
correlation_signal.shape[2]
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
corr_shape_2 = (
|
|
183
|
+
correlation_signal.shape[0],
|
|
184
|
+
correlation_signal.shape[1],
|
|
185
|
+
correlation_signal.shape[2] * 2,
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
@vd.map
|
|
189
|
+
def irfft_map_input(input_buffer: vc.Buffer[c64]):
|
|
190
|
+
io_index = vd.fft.read_op().io_index
|
|
191
|
+
|
|
192
|
+
with vc.if_block(vd.fft.read_op().fft_index >= (vd.fft.read_op().fft_size // 2) + 1):
|
|
193
|
+
io_index[:] = vd.fft.read_op().r2c_inverse_offset - io_index
|
|
194
|
+
|
|
195
|
+
coord3d = vc.ravel_index(vd.fft.read_op().io_index, trimmed_shape_3).to_register()
|
|
196
|
+
|
|
197
|
+
with vc.if_block(coord3d.y >= output_radius):
|
|
198
|
+
coord3d.y += correlation_signal.shape[1] - output_radius * 2
|
|
199
|
+
|
|
200
|
+
new_io_index = vc.unravel_index(coord3d, correlation_signal.shape).to_register()
|
|
201
|
+
|
|
202
|
+
vd.fft.read_op().register[:] = input_buffer[new_io_index]
|
|
203
|
+
|
|
204
|
+
with vc.if_block(vd.fft.read_op().fft_index >= (vd.fft.read_op().fft_size // 2) + 1):
|
|
205
|
+
vd.fft.read_op().register.imag = -vd.fft.read_op().register.imag
|
|
206
|
+
|
|
207
|
+
@vd.map
|
|
208
|
+
def irfft_map_output(output_buffer: vc.Buffer[c64]):
|
|
209
|
+
coord3d = vc.ravel_index(vd.fft.write_op().io_index, trimmed_shape_2).to_register()
|
|
210
|
+
|
|
211
|
+
with vc.if_block(vc.any(
|
|
212
|
+
coord3d.z < output_radius,
|
|
213
|
+
coord3d.z >= self.micrographs_buffer.real_shape[2] - output_radius)):
|
|
214
|
+
|
|
215
|
+
with vc.if_block(coord3d.y >= output_radius):
|
|
216
|
+
coord3d.y += correlation_signal.shape[1] - output_radius * 2
|
|
217
|
+
|
|
218
|
+
new_io_index = vc.unravel_index(coord3d, corr_shape_2).to_register()
|
|
219
|
+
|
|
220
|
+
vd.fft.write_op().write_to_buffer(output_buffer, io_index=new_io_index)
|
|
221
|
+
|
|
222
|
+
vd.fft.fft(
|
|
223
|
+
correlation_signal,
|
|
224
|
+
correlation_signal_out,
|
|
225
|
+
buffer_shape=trimmed_shape,
|
|
226
|
+
input_map=irfft_map_input,
|
|
227
|
+
output_map=irfft_map_output,
|
|
228
|
+
r2c=True,
|
|
229
|
+
inverse=True
|
|
230
|
+
)
|
|
140
231
|
|
|
141
232
|
return correlation_signal
|
|
@@ -60,7 +60,6 @@ def _zernike_cart(m: int, n: int, r: vc.ShaderVariable, th: vc.ShaderVariable) -
|
|
|
60
60
|
return R * vc.cos(m * th)
|
|
61
61
|
return R * vc.sin(abs(m) * th)
|
|
62
62
|
|
|
63
|
-
|
|
64
63
|
def phase_from_even_zernike(params: CTFParams, Sx_eff: vc.ShaderVariable, Sy_eff: vc.ShaderVariable) -> vc.ShaderVariable:
|
|
65
64
|
psi = vc.new_float_register()
|
|
66
65
|
r = vc.sqrt(Sx_eff * Sx_eff + Sy_eff * Sy_eff).to_register()
|
|
@@ -173,3 +172,36 @@ def ctf_filter(
|
|
|
173
172
|
CTF.imag = 0.0
|
|
174
173
|
|
|
175
174
|
return CTF
|
|
175
|
+
|
|
176
|
+
def apply_ctfs_to_rfft_signals(
|
|
177
|
+
input_value: vc.ShaderVariable,
|
|
178
|
+
registers: list[vc.ShaderVariable],
|
|
179
|
+
indices: list[int],
|
|
180
|
+
ind: vc.ShaderVariable,
|
|
181
|
+
signal_shape: tuple[int, int, int],
|
|
182
|
+
pix_size: vc.ShaderVariable,
|
|
183
|
+
ctf_params: CTFParams,
|
|
184
|
+
args: list[vc.ShaderVariable],
|
|
185
|
+
):
|
|
186
|
+
iind = ind.to_dtype(vc.i32)
|
|
187
|
+
|
|
188
|
+
ipos_2d = vc.new_ivec2_register()
|
|
189
|
+
ipos_2d.x = iind % signal_shape[2]
|
|
190
|
+
ipos_2d.y = ((iind // signal_shape[2]) + signal_shape[1] // 2) % signal_shape[1]
|
|
191
|
+
|
|
192
|
+
pos_2d = ipos_2d.to_dtype(vc.v2).to_register()
|
|
193
|
+
pos_2d.y = pos_2d.y - signal_shape[1] // 2
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
ctf_param_list = ctf_params.assemble_params_list_from_args(args, signal_shape[0])
|
|
198
|
+
|
|
199
|
+
for i in range(len(registers)):
|
|
200
|
+
#index = ind + i * signal_shape[1] * signal_shape[2]
|
|
201
|
+
|
|
202
|
+
registers[i][:] = vc.mult_complex(input_value, ctf_filter(
|
|
203
|
+
signal_shape[1:],
|
|
204
|
+
pos_2d,
|
|
205
|
+
ctf_param_list[indices[i]],
|
|
206
|
+
pix_size
|
|
207
|
+
))
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import dataclasses
|
|
2
2
|
import numpy as np
|
|
3
|
-
import itertools
|
|
4
|
-
from .ctf_set import CTFSet
|
|
5
3
|
|
|
6
4
|
import vkdispatch as vd
|
|
7
5
|
import vkdispatch.codegen as vc
|
|
@@ -191,147 +189,6 @@ class CTFParams:
|
|
|
191
189
|
|
|
192
190
|
return base_obj
|
|
193
191
|
|
|
194
|
-
@classmethod
|
|
195
|
-
def like_titan(cls,
|
|
196
|
-
HT: float = 300e3,
|
|
197
|
-
Cs: float = 4.8e7,
|
|
198
|
-
Cc: float = 7.6e7,
|
|
199
|
-
energy_spread_fwhm: float = 0.9,
|
|
200
|
-
accel_voltage_std: float = 0.07e-6,
|
|
201
|
-
OL_current_std: float = 0.1e-6,
|
|
202
|
-
beam_semiangle: float = 2.5e-6,
|
|
203
|
-
johnson_std: float = 0.37,
|
|
204
|
-
B: float = 0,
|
|
205
|
-
amp_contrast: float = 0.07,
|
|
206
|
-
zernike: float = 0,
|
|
207
|
-
lpp: float = 90,
|
|
208
|
-
NA: float = 0.05,
|
|
209
|
-
f_OL: float = 20e7,
|
|
210
|
-
lpp_rot: float = 18,
|
|
211
|
-
defocus: float = 10000,
|
|
212
|
-
A_mag: float = 0,
|
|
213
|
-
A_ang: float = 0,
|
|
214
|
-
mag_matrix: np.ndarray = None,
|
|
215
|
-
even_zernike: list[float] = None,
|
|
216
|
-
odd_zernike: list[float] = None):
|
|
217
|
-
return cls(
|
|
218
|
-
HT=HT,
|
|
219
|
-
Cs=Cs,
|
|
220
|
-
Cc=Cc,
|
|
221
|
-
energy_spread_fwhm=energy_spread_fwhm,
|
|
222
|
-
accel_voltage_std=accel_voltage_std,
|
|
223
|
-
OL_current_std=OL_current_std,
|
|
224
|
-
beam_semiangle=beam_semiangle,
|
|
225
|
-
johnson_std=johnson_std,
|
|
226
|
-
B=B,
|
|
227
|
-
amp_contrast=amp_contrast,
|
|
228
|
-
zernike=zernike,
|
|
229
|
-
lpp=lpp,
|
|
230
|
-
NA=NA,
|
|
231
|
-
f_OL=f_OL,
|
|
232
|
-
lpp_rot=lpp_rot,
|
|
233
|
-
defocus=defocus,
|
|
234
|
-
A_mag=A_mag,
|
|
235
|
-
A_ang=A_ang,
|
|
236
|
-
mag_matrix=mag_matrix,
|
|
237
|
-
even_zernike=even_zernike,
|
|
238
|
-
odd_zernike=odd_zernike
|
|
239
|
-
)
|
|
240
|
-
|
|
241
|
-
@classmethod
|
|
242
|
-
def like_theia(cls,
|
|
243
|
-
HT: float = 300e3,
|
|
244
|
-
Cs: float = 0,
|
|
245
|
-
Cc: float = 5.1e7,
|
|
246
|
-
energy_spread_fwhm: float = 0.3,
|
|
247
|
-
accel_voltage_std: float = 0.07e-6,
|
|
248
|
-
OL_current_std: float = 0.1e-6,
|
|
249
|
-
beam_semiangle: float = 2.5e-6,
|
|
250
|
-
johnson_std: float = 0.33,
|
|
251
|
-
B: float = 0,
|
|
252
|
-
amp_contrast: float = 0.07,
|
|
253
|
-
zernike: float = 0,
|
|
254
|
-
lpp: float = 90,
|
|
255
|
-
NA: float = 0.05,
|
|
256
|
-
f_OL: float = 14.1e7,
|
|
257
|
-
lpp_rot: float = 0,
|
|
258
|
-
defocus: float = 10000,
|
|
259
|
-
A_mag: float = 0,
|
|
260
|
-
A_ang: float = 0,
|
|
261
|
-
mag_matrix: np.ndarray = None,
|
|
262
|
-
even_zernike: list[float] = None,
|
|
263
|
-
odd_zernike: list[float] = None):
|
|
264
|
-
return cls(
|
|
265
|
-
HT=HT,
|
|
266
|
-
Cs=Cs,
|
|
267
|
-
Cc=Cc,
|
|
268
|
-
energy_spread_fwhm=energy_spread_fwhm,
|
|
269
|
-
accel_voltage_std=accel_voltage_std,
|
|
270
|
-
OL_current_std=OL_current_std,
|
|
271
|
-
beam_semiangle=beam_semiangle,
|
|
272
|
-
johnson_std=johnson_std,
|
|
273
|
-
B=B,
|
|
274
|
-
amp_contrast=amp_contrast,
|
|
275
|
-
zernike=zernike,
|
|
276
|
-
lpp=lpp,
|
|
277
|
-
NA=NA,
|
|
278
|
-
f_OL=f_OL,
|
|
279
|
-
lpp_rot=lpp_rot,
|
|
280
|
-
defocus=defocus,
|
|
281
|
-
A_mag=A_mag,
|
|
282
|
-
A_ang=A_ang,
|
|
283
|
-
mag_matrix=mag_matrix,
|
|
284
|
-
even_zernike=even_zernike,
|
|
285
|
-
odd_zernike=odd_zernike
|
|
286
|
-
)
|
|
287
|
-
|
|
288
|
-
@classmethod
|
|
289
|
-
def like_krios(cls,
|
|
290
|
-
HT: float = 300e3,
|
|
291
|
-
Cs: float = 2.7e7,
|
|
292
|
-
Cc: float = 2.7e7,
|
|
293
|
-
energy_spread_fwhm: float = 0.3,
|
|
294
|
-
accel_voltage_std: float = 0.07e-6,
|
|
295
|
-
OL_current_std: float = 0.1e-6,
|
|
296
|
-
beam_semiangle: float = 2.5e-6,
|
|
297
|
-
johnson_std: float = 0,
|
|
298
|
-
B: float = 0,
|
|
299
|
-
amp_contrast: float = 0.07,
|
|
300
|
-
zernike: float = 0,
|
|
301
|
-
lpp: float = 0,
|
|
302
|
-
NA: float = 0.05,
|
|
303
|
-
f_OL: float = 20e7,
|
|
304
|
-
lpp_rot: float = 0,
|
|
305
|
-
defocus: float = 10000,
|
|
306
|
-
A_mag: float = 0,
|
|
307
|
-
A_ang: float = 0,
|
|
308
|
-
mag_matrix: np.ndarray = None,
|
|
309
|
-
even_zernike: list[float] = None,
|
|
310
|
-
odd_zernike: list[float] = None):
|
|
311
|
-
return cls(
|
|
312
|
-
HT=HT,
|
|
313
|
-
Cs=Cs,
|
|
314
|
-
Cc=Cc,
|
|
315
|
-
energy_spread_fwhm=energy_spread_fwhm,
|
|
316
|
-
accel_voltage_std=accel_voltage_std,
|
|
317
|
-
OL_current_std=OL_current_std,
|
|
318
|
-
beam_semiangle=beam_semiangle,
|
|
319
|
-
johnson_std=johnson_std,
|
|
320
|
-
B=B,
|
|
321
|
-
amp_contrast=amp_contrast,
|
|
322
|
-
zernike=zernike,
|
|
323
|
-
lpp=lpp,
|
|
324
|
-
NA=NA,
|
|
325
|
-
f_OL=f_OL,
|
|
326
|
-
lpp_rot=lpp_rot,
|
|
327
|
-
defocus=defocus,
|
|
328
|
-
A_mag=A_mag,
|
|
329
|
-
A_ang=A_ang,
|
|
330
|
-
mag_matrix=mag_matrix,
|
|
331
|
-
even_zernike=even_zernike,
|
|
332
|
-
odd_zernike=odd_zernike
|
|
333
|
-
)
|
|
334
|
-
|
|
335
192
|
def get_even_coeff(self):
|
|
336
193
|
return (
|
|
337
194
|
self.e_zern_0,
|
|
@@ -407,7 +264,7 @@ class CTFParams:
|
|
|
407
264
|
static_count = 0
|
|
408
265
|
dynamic_count = 0
|
|
409
266
|
static_values = [None] * len(fields)
|
|
410
|
-
|
|
267
|
+
dynamic_indices = []
|
|
411
268
|
|
|
412
269
|
for ii, field in enumerate(fields):
|
|
413
270
|
value = self.__getattribute__(field.name)
|
|
@@ -417,7 +274,7 @@ class CTFParams:
|
|
|
417
274
|
static_count += 1
|
|
418
275
|
else:
|
|
419
276
|
dynamic_count += 1
|
|
420
|
-
|
|
277
|
+
dynamic_indices.append(ii)
|
|
421
278
|
|
|
422
279
|
assert len(args_list) == static_count + dynamic_count * template_count, f"Expected {static_count} + {dynamic_count} * {template_count} args, got {len(args_list)}"
|
|
423
280
|
|
|
@@ -426,37 +283,13 @@ class CTFParams:
|
|
|
426
283
|
result = []
|
|
427
284
|
|
|
428
285
|
for i in range(template_count):
|
|
429
|
-
for ii, dyn_ind in enumerate(
|
|
286
|
+
for ii, dyn_ind in enumerate(dynamic_indices):
|
|
430
287
|
static_values[dyn_ind] = dynamic_args_list[i * dynamic_count + ii]
|
|
431
288
|
|
|
432
289
|
result.append(CTFParams.from_arg_list(*static_values))
|
|
433
290
|
|
|
434
291
|
return result
|
|
435
292
|
|
|
436
|
-
def make_ctf_set(self, **values_dict) -> CTFSet:
|
|
437
|
-
fields = dataclasses.fields(self)
|
|
438
|
-
|
|
439
|
-
dynamic_fields = [field for field in fields if self.__getattribute__(field.name) is None]
|
|
440
|
-
|
|
441
|
-
dynamic_field_names = {field.name for field in dynamic_fields}
|
|
442
|
-
values_dict_keys = set(values_dict.keys())
|
|
443
|
-
if dynamic_field_names != values_dict_keys:
|
|
444
|
-
raise ValueError(f"Dynamic field names {dynamic_field_names} do not match keys in values_dict {values_dict_keys}")
|
|
445
|
-
|
|
446
|
-
dynamic_values = [values_dict[field.name] for field in dynamic_fields]
|
|
447
|
-
|
|
448
|
-
for dyn_val, field in zip(dynamic_values, dynamic_fields):
|
|
449
|
-
assert dyn_val.ndim == 1, f"Dynamic value for field {field.name} must be a 1D array, got {dyn_val.ndim}D array."
|
|
450
|
-
|
|
451
|
-
combinations = list(itertools.product(*dynamic_values))
|
|
452
|
-
combinations_array = np.array(combinations)
|
|
453
|
-
|
|
454
|
-
return CTFSet.from_compiled_params(
|
|
455
|
-
combinations_array=combinations_array,
|
|
456
|
-
field_names=[field.name for field in dynamic_fields],
|
|
457
|
-
lengths=[len(values_dict[field.name]) for field in dynamic_fields]
|
|
458
|
-
)
|
|
459
|
-
|
|
460
293
|
def print(self):
|
|
461
294
|
fields = dataclasses.fields(self)
|
|
462
295
|
max_len = max(len(f.name) for f in fields)
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import dataclasses
|
|
2
|
+
import numpy as np
|
|
3
|
+
import vkdispatch as vd
|
|
4
|
+
import itertools
|
|
5
|
+
|
|
6
|
+
from .ctf_params import CTFParams
|
|
7
|
+
|
|
8
|
+
@dataclasses.dataclass
|
|
9
|
+
class CTFSet:
|
|
10
|
+
ctf_params: CTFParams
|
|
11
|
+
combinations_array: np.ndarray
|
|
12
|
+
field_names: list[str]
|
|
13
|
+
lengths: list[int]
|
|
14
|
+
|
|
15
|
+
def __init__(self,
|
|
16
|
+
ctf_params: CTFParams,
|
|
17
|
+
**values_dict):
|
|
18
|
+
|
|
19
|
+
self.ctf_params = ctf_params
|
|
20
|
+
|
|
21
|
+
fields = dataclasses.fields(ctf_params)
|
|
22
|
+
|
|
23
|
+
dynamic_fields = [field for field in fields if ctf_params.__getattribute__(field.name) is None]
|
|
24
|
+
|
|
25
|
+
dynamic_field_names = {field.name for field in dynamic_fields}
|
|
26
|
+
values_dict_keys = set(values_dict.keys())
|
|
27
|
+
if dynamic_field_names != values_dict_keys:
|
|
28
|
+
raise ValueError(f"Dynamic field names {dynamic_field_names} do not match keys in values_dict {values_dict_keys}")
|
|
29
|
+
|
|
30
|
+
dynamic_values = [values_dict[field.name] for field in dynamic_fields]
|
|
31
|
+
|
|
32
|
+
for dyn_val, field in zip(dynamic_values, dynamic_fields):
|
|
33
|
+
assert dyn_val.ndim == 1, f"Dynamic value for field {field.name} must be a 1D array, got {dyn_val.ndim}D array."
|
|
34
|
+
|
|
35
|
+
combinations = list(itertools.product(*dynamic_values))
|
|
36
|
+
|
|
37
|
+
self.combinations_array = np.array(combinations)
|
|
38
|
+
self.field_names = [field.name for field in dynamic_fields]
|
|
39
|
+
self.lengths = [len(values_dict[field.name]) for field in dynamic_fields]
|
|
40
|
+
|
|
41
|
+
def get_length(self):
|
|
42
|
+
return self.combinations_array.shape[0]
|
|
43
|
+
|
|
44
|
+
def get_field_names(self):
|
|
45
|
+
return self.field_names
|
|
46
|
+
|
|
47
|
+
def get_lengths_list(self):
|
|
48
|
+
return self.lengths
|
|
49
|
+
|
|
50
|
+
def set_ctf_batch(self,
|
|
51
|
+
index_arrays: list[np.ndarray],
|
|
52
|
+
input_array: np.ndarray,
|
|
53
|
+
cmd_graph: vd.CommandGraph,
|
|
54
|
+
ctf_batch_size: int,
|
|
55
|
+
rotations_pixels_batch_size: int,
|
|
56
|
+
rotations_pixels_count: int,
|
|
57
|
+
template_count: int,
|
|
58
|
+
ctf_index: int):
|
|
59
|
+
for batch_id in range(template_count):
|
|
60
|
+
start_index = ctf_index + batch_id * ctf_batch_size
|
|
61
|
+
end_index = min(start_index + ctf_batch_size, self.get_length())
|
|
62
|
+
|
|
63
|
+
last_index = end_index - start_index
|
|
64
|
+
|
|
65
|
+
input_array[:ctf_batch_size] = 0
|
|
66
|
+
index_arrays[batch_id][:ctf_batch_size] = -self.get_length() * rotations_pixels_count
|
|
67
|
+
|
|
68
|
+
if last_index > 0:
|
|
69
|
+
for ii, field in enumerate(self.field_names):
|
|
70
|
+
input_array[:last_index] = self.combinations_array[start_index:end_index, ii]
|
|
71
|
+
cmd_graph.set_var(f"{field}_{batch_id}", np.tile(input_array[:ctf_batch_size], rotations_pixels_batch_size))
|
|
72
|
+
|
|
73
|
+
index_arrays[batch_id][0:last_index] = np.arange(start_index, end_index)
|
|
74
|
+
else:
|
|
75
|
+
for field in self.field_names:
|
|
76
|
+
cmd_graph.set_var(f"{field}_{batch_id}", np.tile(input_array[:ctf_batch_size], rotations_pixels_batch_size))
|
|
77
|
+
|
|
78
|
+
for j in range(1, rotations_pixels_batch_size):
|
|
79
|
+
index_arrays[batch_id][ctf_batch_size*j:ctf_batch_size*(j+1)] = index_arrays[batch_id][:ctf_batch_size] + self.get_length() * j
|
|
80
|
+
|
|
81
|
+
def get_values_at_index(self, index: int):
|
|
82
|
+
return_dict = {}
|
|
83
|
+
for i, field_name in enumerate(self.field_names):
|
|
84
|
+
return_dict[field_name] = self.combinations_array[index, i]
|
|
85
|
+
|
|
86
|
+
return return_dict
|
|
87
|
+
|
|
88
|
+
def make_ctf_set(ctf_params: CTFParams, **values_dict) -> CTFSet:
|
|
89
|
+
return CTFSet(ctf_params, **values_dict)
|