tm2d 0.1.7__tar.gz → 0.2.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.1.7/tm2d.egg-info → tm2d-0.2.0}/PKG-INFO +4 -6
- {tm2d-0.1.7 → tm2d-0.2.0}/README.md +1 -1
- {tm2d-0.1.7 → tm2d-0.2.0}/pyproject.toml +7 -8
- tm2d-0.2.0/tm2d/__init__.py +29 -0
- tm2d-0.2.0/tm2d/base_comparator.py +15 -0
- tm2d-0.2.0/tm2d/base_results.py +23 -0
- tm2d-0.2.0/tm2d/base_template.py +78 -0
- tm2d-0.2.0/tm2d/comparators/cross_correlation.py +141 -0
- tm2d-0.2.0/tm2d/ctf/ctf.py +175 -0
- tm2d-0.1.7/tm2d/ctf.py → tm2d-0.2.0/tm2d/ctf/ctf_params.py +218 -248
- tm2d-0.2.0/tm2d/ctf/ctf_set.py +64 -0
- tm2d-0.2.0/tm2d/param_set.py +135 -0
- tm2d-0.2.0/tm2d/plan.py +260 -0
- tm2d-0.2.0/tm2d/results/per_param.py +130 -0
- tm2d-0.2.0/tm2d/results/per_pixel.py +208 -0
- tm2d-0.2.0/tm2d/templates/atomic.py +338 -0
- tm2d-0.2.0/tm2d/templates/density.py +254 -0
- {tm2d-0.1.7 → tm2d-0.2.0/tm2d.egg-info}/PKG-INFO +4 -6
- tm2d-0.2.0/tm2d.egg-info/SOURCES.txt +22 -0
- tm2d-0.2.0/tm2d.egg-info/requires.txt +3 -0
- tm2d-0.1.7/tm2d/__init__.py +0 -30
- tm2d-0.1.7/tm2d/cross_correlation.py +0 -165
- tm2d-0.1.7/tm2d/micrograph.py +0 -109
- tm2d-0.1.7/tm2d/orientation_sampling.py +0 -246
- tm2d-0.1.7/tm2d/plan.py +0 -433
- tm2d-0.1.7/tm2d/plan_standard.py +0 -31
- tm2d-0.1.7/tm2d/results_per_param.py +0 -120
- tm2d-0.1.7/tm2d/results_per_pixel.py +0 -311
- tm2d-0.1.7/tm2d/simulators/image.py +0 -28
- tm2d-0.1.7/tm2d/template_atomic.py +0 -239
- tm2d-0.1.7/tm2d/template_density.py +0 -235
- tm2d-0.1.7/tm2d/utilities/__init__.py +0 -24
- tm2d-0.1.7/tm2d/utilities/data_handler.py +0 -20
- tm2d-0.1.7/tm2d/utilities/fftshift_util.py +0 -15
- tm2d-0.1.7/tm2d/utilities/file_loading.py +0 -25
- tm2d-0.1.7/tm2d/utilities/image_processing.py +0 -105
- tm2d-0.1.7/tm2d/utilities/optics_functions.py +0 -64
- tm2d-0.1.7/tm2d/utilities/rotation_matricies.py +0 -63
- tm2d-0.1.7/tm2d/utilities/signal_normalization.py +0 -31
- tm2d-0.1.7/tm2d/utilities/whitener.py +0 -297
- tm2d-0.1.7/tm2d.egg-info/SOURCES.txt +0 -29
- tm2d-0.1.7/tm2d.egg-info/requires.txt +0 -6
- {tm2d-0.1.7 → tm2d-0.2.0}/LICENSE +0 -0
- {tm2d-0.1.7 → tm2d-0.2.0}/setup.cfg +0 -0
- {tm2d-0.1.7 → tm2d-0.2.0}/tm2d.egg-info/dependency_links.txt +0 -0
- {tm2d-0.1.7 → tm2d-0.2.0}/tm2d.egg-info/top_level.txt +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: tm2d
|
|
3
|
-
Version: 0.
|
|
4
|
-
Summary: A Python module for
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: A Python module for 2D template matching
|
|
5
5
|
Author-email: Shahar Sandhaus <shahar.sandhaus@berkeley.edu>
|
|
6
6
|
Project-URL: Homepage, https://github.com/sharhar/tm2d
|
|
7
7
|
Project-URL: Issues, https://github.com/sharhar/tm2d/issues
|
|
@@ -14,12 +14,10 @@ Description-Content-Type: text/markdown
|
|
|
14
14
|
License-File: LICENSE
|
|
15
15
|
Requires-Dist: setuptools>=59.0
|
|
16
16
|
Requires-Dist: numpy
|
|
17
|
-
Requires-Dist: vkdispatch==0.0
|
|
18
|
-
Provides-Extra: healpy
|
|
19
|
-
Requires-Dist: healpy; extra == "healpy"
|
|
17
|
+
Requires-Dist: vkdispatch==0.1.0
|
|
20
18
|
Dynamic: license-file
|
|
21
19
|
|
|
22
|
-
#
|
|
20
|
+
# tm2d
|
|
23
21
|
A 2D template matcher written using the vkdispatch library.
|
|
24
22
|
|
|
25
23
|
## Installation
|
|
@@ -4,11 +4,11 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "tm2d"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.2.0"
|
|
8
8
|
authors = [
|
|
9
9
|
{ name="Shahar Sandhaus", email="shahar.sandhaus@berkeley.edu" },
|
|
10
10
|
]
|
|
11
|
-
description = "A Python module for
|
|
11
|
+
description = "A Python module for 2D template matching"
|
|
12
12
|
readme = "README.md"
|
|
13
13
|
requires-python = ">=3.9"
|
|
14
14
|
classifiers = [
|
|
@@ -20,7 +20,7 @@ classifiers = [
|
|
|
20
20
|
dependencies = [
|
|
21
21
|
"setuptools>=59.0",
|
|
22
22
|
"numpy",
|
|
23
|
-
"vkdispatch==0.0
|
|
23
|
+
"vkdispatch==0.1.0"
|
|
24
24
|
]
|
|
25
25
|
|
|
26
26
|
[project.urls]
|
|
@@ -30,9 +30,8 @@ Issues = "https://github.com/sharhar/tm2d/issues"
|
|
|
30
30
|
[tool.setuptools]
|
|
31
31
|
packages = [
|
|
32
32
|
"tm2d",
|
|
33
|
-
"tm2d.
|
|
34
|
-
"tm2d.
|
|
33
|
+
"tm2d.templates",
|
|
34
|
+
"tm2d.comparators",
|
|
35
|
+
"tm2d.results",
|
|
36
|
+
"tm2d.ctf"
|
|
35
37
|
]
|
|
36
|
-
|
|
37
|
-
[project.optional-dependencies]
|
|
38
|
-
healpy = ["healpy"]
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from .ctf.ctf_params import CTFParams
|
|
2
|
+
from .ctf.ctf_set import CTFSet
|
|
3
|
+
from .ctf.ctf import ctf_filter
|
|
4
|
+
|
|
5
|
+
from .templates.atomic import TemplateAtomic
|
|
6
|
+
from .templates.density import TemplateDensity
|
|
7
|
+
|
|
8
|
+
from .comparators.cross_correlation import ComparatorCrossCorrelation
|
|
9
|
+
|
|
10
|
+
from .results.per_pixel import ResultsPixel
|
|
11
|
+
from .results.per_param import ResultsParam
|
|
12
|
+
|
|
13
|
+
from .plan import Plan, Template, Comparator, Results, ParamSet
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"CTFParams",
|
|
17
|
+
"CTFSet",
|
|
18
|
+
"ctf_filter",
|
|
19
|
+
"TemplateAtomic",
|
|
20
|
+
"TemplateDensity",
|
|
21
|
+
"ComparatorCrossCorrelation",
|
|
22
|
+
"ResultsPixel",
|
|
23
|
+
"ResultsParam",
|
|
24
|
+
"Plan",
|
|
25
|
+
"Template",
|
|
26
|
+
"Comparator",
|
|
27
|
+
"Results",
|
|
28
|
+
"ParamSet",
|
|
29
|
+
]
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import vkdispatch as vd
|
|
2
|
+
import numpy as np
|
|
3
|
+
|
|
4
|
+
class Comparator:
|
|
5
|
+
def __init__(self):
|
|
6
|
+
raise NotImplementedError("Comparator is an abstract class. Please implement it in a subclass.")
|
|
7
|
+
|
|
8
|
+
def set_data(self, data: np.ndarray):
|
|
9
|
+
"""
|
|
10
|
+
This method should set the data buffer that will be used for comparison.
|
|
11
|
+
"""
|
|
12
|
+
raise NotImplementedError("set_data must be implemented in a subclass.")
|
|
13
|
+
|
|
14
|
+
def compare_template(self, template: vd.RFFTBuffer, normalize: bool = True) -> vd.RFFTBuffer:
|
|
15
|
+
raise NotImplementedError("compare_template must be implemented in a subclass.")
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import vkdispatch as vd
|
|
2
|
+
import vkdispatch.codegen as vc
|
|
3
|
+
|
|
4
|
+
class Results:
|
|
5
|
+
def __init__(self):
|
|
6
|
+
raise NotImplementedError("Results is an abstract class. Please implement it in a subclass.")
|
|
7
|
+
|
|
8
|
+
def check_comparison(self, comparison_buffer: vd.RFFTBuffer, *indicies: vc.Var[vc.i32]):
|
|
9
|
+
raise NotImplementedError("check_comparison must be implemented in a subclass.")
|
|
10
|
+
|
|
11
|
+
def reset(self):
|
|
12
|
+
"""
|
|
13
|
+
Reset the results to their initial state.
|
|
14
|
+
This method should be implemented in subclasses to reset any internal state.
|
|
15
|
+
"""
|
|
16
|
+
raise NotImplementedError("reset must be implemented in a subclass.")
|
|
17
|
+
|
|
18
|
+
def compile_results(self, templates_count: int):
|
|
19
|
+
"""
|
|
20
|
+
Compile the results after all comparisons have been made.
|
|
21
|
+
This method should be implemented in subclasses to compile the results into a final form that can be easily accessed.
|
|
22
|
+
"""
|
|
23
|
+
raise NotImplementedError("compile_results must be implemented in a subclass.")
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import vkdispatch as vd
|
|
2
|
+
import vkdispatch.codegen as vc
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
from typing import Union, List, Tuple
|
|
6
|
+
from .ctf.ctf import CTFParams
|
|
7
|
+
|
|
8
|
+
class Template:
|
|
9
|
+
def __init__(self):
|
|
10
|
+
raise NotImplementedError("Template is an abstract class. Please implement it in a subclass.")
|
|
11
|
+
|
|
12
|
+
def _make_template(self,
|
|
13
|
+
rotations: Union[vc.Var[vc.m4], np.ndarray],
|
|
14
|
+
pixel_size: float,
|
|
15
|
+
ctf_params: CTFParams,
|
|
16
|
+
template_count: int = 1,
|
|
17
|
+
cmd_graph: vd.CommandGraph = None,
|
|
18
|
+
disable_ctf: bool = False) -> vd.RFFTBuffer:
|
|
19
|
+
"""
|
|
20
|
+
This abstract method should return a buffer containing the sampled and filtered template in real space.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
raise NotImplementedError("make_template must be implemented in a subclass.")
|
|
24
|
+
|
|
25
|
+
def make_template(self,
|
|
26
|
+
rotations: Union[vc.Var[vc.m4], List[int], np.ndarray],
|
|
27
|
+
pixel_size: float,
|
|
28
|
+
ctf_params: CTFParams = None,
|
|
29
|
+
template_count: int = 1,
|
|
30
|
+
cmd_graph: vd.CommandGraph = None) -> vd.RFFTBuffer:
|
|
31
|
+
"""
|
|
32
|
+
This abstract method should return a buffer containing the sampled and filtered template in real space.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
if ctf_params is None:
|
|
36
|
+
ctf_params = CTFParams()
|
|
37
|
+
|
|
38
|
+
if isinstance(rotations, list):
|
|
39
|
+
rotations = np.array(rotations, dtype=np.float32)
|
|
40
|
+
|
|
41
|
+
if isinstance(rotations, np.ndarray):
|
|
42
|
+
if rotations.ndim == 1:
|
|
43
|
+
rotations = rotations.reshape(1, 3)
|
|
44
|
+
|
|
45
|
+
if rotations.ndim == 2:
|
|
46
|
+
if rotations.shape[1] == 4 and rotations.shape[0] == 4:
|
|
47
|
+
rotations = rotations.reshape(1, 4, 4)
|
|
48
|
+
else:
|
|
49
|
+
assert rotations.shape[1] == 3 , "Rotations must be a 2D array with shape (N, 3)."
|
|
50
|
+
|
|
51
|
+
rotations = self.get_rotation_matricies(rotations)
|
|
52
|
+
|
|
53
|
+
return self._make_template(
|
|
54
|
+
rotations=rotations,
|
|
55
|
+
pixel_size=pixel_size,
|
|
56
|
+
ctf_params=ctf_params,
|
|
57
|
+
template_count=template_count,
|
|
58
|
+
cmd_graph=cmd_graph
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
def _get_rotation_matricies(self, rotations: np.ndarray) -> np.ndarray:
|
|
62
|
+
raise NotImplementedError("get_rotation_matricies must be implemented in a subclass.")
|
|
63
|
+
|
|
64
|
+
def get_rotation_matricies(self, rotations: Union[np.ndarray, Tuple[float, float, float]]) -> np.ndarray:
|
|
65
|
+
if not isinstance(rotations, np.ndarray):
|
|
66
|
+
rotations = np.array(rotations, dtype=np.float32).reshape(1, 3)
|
|
67
|
+
|
|
68
|
+
if rotations.ndim == 1:
|
|
69
|
+
rotations = rotations.reshape(1, 3)
|
|
70
|
+
|
|
71
|
+
return self._get_rotation_matricies(rotations)
|
|
72
|
+
|
|
73
|
+
def get_shape(self) -> tuple:
|
|
74
|
+
"""
|
|
75
|
+
Return the shape of the template.
|
|
76
|
+
This method should be implemented in subclasses to return the correct shape.
|
|
77
|
+
"""
|
|
78
|
+
raise NotImplementedError("get_shape must be implemented in a subclass.")
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import vkdispatch as vd
|
|
2
|
+
import vkdispatch.codegen as vc
|
|
3
|
+
from vkdispatch.codegen.abbreviations import Buff, f32, c64, v2, Const
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
from ..plan import Comparator
|
|
8
|
+
|
|
9
|
+
@vd.map
|
|
10
|
+
def crop_mapping(in_buff: Buff[f32], sum_buffer: Buff[c64], micrograph_shape: Const[vc.iv2]):
|
|
11
|
+
read_op = vd.fft.read_op()
|
|
12
|
+
|
|
13
|
+
ind = read_op.io_index.to_dtype(vc.i32)
|
|
14
|
+
|
|
15
|
+
read_index_3d = vc.new_ivec3_register()
|
|
16
|
+
read_index_3d.x = ind // (micrograph_shape.x * (micrograph_shape.y + 2))
|
|
17
|
+
read_index_3d.y = (ind // (micrograph_shape.y + 2)) % micrograph_shape.x
|
|
18
|
+
read_index_3d.z = ind % (micrograph_shape.y + 2)
|
|
19
|
+
|
|
20
|
+
read_index_3d.y -= micrograph_shape.x // 2
|
|
21
|
+
read_index_3d.z -= micrograph_shape.y // 2
|
|
22
|
+
|
|
23
|
+
read_index_3d.y += in_buff.shape.y // 2
|
|
24
|
+
read_index_3d.z += in_buff.shape.z - 1
|
|
25
|
+
|
|
26
|
+
with vc.if_block(vc.any(
|
|
27
|
+
read_index_3d.y < 0,
|
|
28
|
+
read_index_3d.y >= in_buff.shape.y,
|
|
29
|
+
read_index_3d.z < 0,
|
|
30
|
+
read_index_3d.z >= (in_buff.shape.z - 1) * 2
|
|
31
|
+
)):
|
|
32
|
+
read_op.register.real = 0.0
|
|
33
|
+
read_op.register.imag = 0.0
|
|
34
|
+
with vc.else_block():
|
|
35
|
+
span_x = in_buff.shape.y * in_buff.shape.z * 2
|
|
36
|
+
span_y = in_buff.shape.z * 2
|
|
37
|
+
|
|
38
|
+
read_index_flat = (read_index_3d.x * span_x + read_index_3d.y * span_y + read_index_3d.z).to_register()
|
|
39
|
+
|
|
40
|
+
read_op.register[:] = sum_buffer[read_index_3d.x] / (in_buff.shape.y * (in_buff.shape.z - 1) * 2)
|
|
41
|
+
read_op.register.imag = vc.sqrt(read_op.register.imag - read_op.register.real * read_op.register.real)
|
|
42
|
+
|
|
43
|
+
read_op.register.real = (in_buff[read_index_flat] - read_op.register.real) / read_op.register.imag
|
|
44
|
+
read_op.register.imag = 0
|
|
45
|
+
|
|
46
|
+
@vd.shader(exec_size=lambda args: args.buf.size)
|
|
47
|
+
def fill_buffer(buf: Buff[c64], val: Const[c64] = 0):
|
|
48
|
+
buf[vc.global_invocation_id().x] = val
|
|
49
|
+
|
|
50
|
+
@vd.reduce.map_reduce(vd.reduce.SubgroupAdd, axes=[1, 2])
|
|
51
|
+
def calc_sums(wave: Buff[c64]) -> v2:
|
|
52
|
+
ind = vd.reduce.mapped_io_index()
|
|
53
|
+
|
|
54
|
+
result = vc.new_vec2_register()
|
|
55
|
+
|
|
56
|
+
with vc.if_block(ind % wave.shape.z < wave.shape.z - 1):
|
|
57
|
+
result.x = wave[ind].real + wave[ind].imag
|
|
58
|
+
result.y = wave[ind].real * wave[ind].real + wave[ind].imag * wave[ind].imag
|
|
59
|
+
|
|
60
|
+
with vc.else_block():
|
|
61
|
+
result.x = 0.0
|
|
62
|
+
result.y = 0.0
|
|
63
|
+
|
|
64
|
+
return result
|
|
65
|
+
|
|
66
|
+
class ComparatorCrossCorrelation(Comparator):
|
|
67
|
+
def __init__(self, shape: tuple, template_shape: tuple):
|
|
68
|
+
assert len(shape) == 3, "Shape must be a 3D tuple (N, H, W) but got: {}".format(shape)
|
|
69
|
+
|
|
70
|
+
self.micrographs_buffer = vd.RFFTBuffer(shape)
|
|
71
|
+
|
|
72
|
+
def set_data(self, data: np.ndarray):
|
|
73
|
+
if data.ndim == 2:
|
|
74
|
+
data = data.reshape((1, *data.shape))
|
|
75
|
+
|
|
76
|
+
assert data.ndim == 3, "Micrographs must be a 3D array (N, H, W)."
|
|
77
|
+
data = np.fft.rfft2(data).astype(np.complex64)
|
|
78
|
+
self.micrographs_buffer.write_fourier(data)
|
|
79
|
+
|
|
80
|
+
def compare_template(self, template_buffer: vd.RFFTBuffer, normalize: bool = True) -> vd.RFFTBuffer:
|
|
81
|
+
assert normalize, "Normalization is mandatory for cross-correlation."
|
|
82
|
+
|
|
83
|
+
correlation_shape = (
|
|
84
|
+
self.micrographs_buffer.real_shape[0] * template_buffer.shape[0],
|
|
85
|
+
self.micrographs_buffer.real_shape[1],
|
|
86
|
+
self.micrographs_buffer.real_shape[2]
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
correlation_signal = vd.RFFTBuffer(correlation_shape)
|
|
90
|
+
|
|
91
|
+
template_sum = calc_sums(template_buffer)
|
|
92
|
+
|
|
93
|
+
vd.fft.fft(
|
|
94
|
+
correlation_signal,
|
|
95
|
+
template_buffer,
|
|
96
|
+
template_sum,
|
|
97
|
+
(self.micrographs_buffer.real_shape[1], self.micrographs_buffer.real_shape[2]),
|
|
98
|
+
buffer_shape=(
|
|
99
|
+
template_buffer.shape[0],
|
|
100
|
+
self.micrographs_buffer.real_shape[1],
|
|
101
|
+
self.micrographs_buffer.real_shape[2]
|
|
102
|
+
),
|
|
103
|
+
input_map=crop_mapping,
|
|
104
|
+
r2c=True
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
kernel_offset = int(
|
|
108
|
+
self.micrographs_buffer.shape[1] * self.micrographs_buffer.shape[2]
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
@vd.map
|
|
112
|
+
def convolution_map(kernel_buffer: vc.Buffer[c64]):
|
|
113
|
+
img_val = vd.fft.read_op().register
|
|
114
|
+
read_register = vc.new_complex64_register()
|
|
115
|
+
|
|
116
|
+
read_register[:] = kernel_buffer[vd.fft.read_op().io_index % kernel_offset + kernel_offset * vd.fft.mapped_kernel_index()]
|
|
117
|
+
img_val[:] = vc.mult_complex(read_register, img_val.conjugate())
|
|
118
|
+
|
|
119
|
+
@vd.map
|
|
120
|
+
def output_map_func(output_buffer: vc.Buffer[c64]):
|
|
121
|
+
out_reg = vd.fft.write_op().register
|
|
122
|
+
output_buffer[vd.fft.write_op().io_index + kernel_offset * template_buffer.shape[0] * vd.fft.mapped_kernel_index()] = out_reg
|
|
123
|
+
|
|
124
|
+
vd.fft.convolve(
|
|
125
|
+
correlation_signal,
|
|
126
|
+
correlation_signal,
|
|
127
|
+
self.micrographs_buffer,
|
|
128
|
+
kernel_num=self.micrographs_buffer.shape[0],
|
|
129
|
+
axis=1,
|
|
130
|
+
buffer_shape=(
|
|
131
|
+
template_buffer.shape[0],
|
|
132
|
+
self.micrographs_buffer.shape[1],
|
|
133
|
+
self.micrographs_buffer.shape[2]
|
|
134
|
+
),
|
|
135
|
+
kernel_map=convolution_map,
|
|
136
|
+
output_map=output_map_func
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
vd.fft.irfft(correlation_signal)
|
|
140
|
+
|
|
141
|
+
return correlation_signal
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import vkdispatch.codegen as vc
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
from .ctf_params import CTFParams
|
|
8
|
+
|
|
9
|
+
E_MASS = 0.511e6 # eV/c^2
|
|
10
|
+
E_COMPTON = 2.42631e-2 # A
|
|
11
|
+
#ALPHA = 0.0072973525693
|
|
12
|
+
#HBAR = 1.0545718001e-34
|
|
13
|
+
#C_REL = 299792458
|
|
14
|
+
|
|
15
|
+
def do_int_check(other):
|
|
16
|
+
return isinstance(other, int) or np.issubdtype(type(other), np.integer)
|
|
17
|
+
|
|
18
|
+
def _even_index_to_mn(i: int) -> tuple[int, int]:
|
|
19
|
+
# Z0^0, Z2^-2, Z2^0, Z2^2, Z4^-4, Z4^-2, Z4^0, ...
|
|
20
|
+
count = 0
|
|
21
|
+
n = 0
|
|
22
|
+
while True:
|
|
23
|
+
for m in range(-n, n + 1, 2):
|
|
24
|
+
if count == i:
|
|
25
|
+
return m, n
|
|
26
|
+
count += 1
|
|
27
|
+
n += 2
|
|
28
|
+
|
|
29
|
+
def _odd_index_to_mn(i: int) -> tuple[int, int]:
|
|
30
|
+
# Z1^-1, Z1^1, Z3^-3, Z3^-1, Z3^1, ...
|
|
31
|
+
count = 0
|
|
32
|
+
n = 1
|
|
33
|
+
while True:
|
|
34
|
+
for m in range(-n, n + 1, 2):
|
|
35
|
+
if count == i:
|
|
36
|
+
return m, n
|
|
37
|
+
count += 1
|
|
38
|
+
n += 2
|
|
39
|
+
|
|
40
|
+
def _zernike_radial(n: int, m: int, r: vc.ShaderVariable) -> vc.ShaderVariable:
|
|
41
|
+
m = abs(m)
|
|
42
|
+
if (n - m) % 2 != 0:
|
|
43
|
+
return 0
|
|
44
|
+
|
|
45
|
+
out = vc.new_float_register()
|
|
46
|
+
for s in range((n - m) // 2 + 1):
|
|
47
|
+
c = ((-1) ** s) * math.factorial(n - s) / (
|
|
48
|
+
math.factorial(s)
|
|
49
|
+
* math.factorial((n + m) // 2 - s)
|
|
50
|
+
* math.factorial((n - m) // 2 - s)
|
|
51
|
+
)
|
|
52
|
+
out += c * r ** (n - 2 * s)
|
|
53
|
+
return out
|
|
54
|
+
|
|
55
|
+
def _zernike_cart(m: int, n: int, r: vc.ShaderVariable, th: vc.ShaderVariable) -> vc.ShaderVariable:
|
|
56
|
+
R = _zernike_radial(n, m, r)
|
|
57
|
+
if m == 0:
|
|
58
|
+
return R
|
|
59
|
+
if m > 0:
|
|
60
|
+
return R * vc.cos(m * th)
|
|
61
|
+
return R * vc.sin(abs(m) * th)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def phase_from_even_zernike(params: CTFParams, Sx_eff: vc.ShaderVariable, Sy_eff: vc.ShaderVariable) -> vc.ShaderVariable:
|
|
65
|
+
psi = vc.new_float_register()
|
|
66
|
+
r = vc.sqrt(Sx_eff * Sx_eff + Sy_eff * Sy_eff).to_register()
|
|
67
|
+
th = vc.atan2(Sy_eff, Sx_eff).to_register()
|
|
68
|
+
for i in range(9):
|
|
69
|
+
with vc.if_block(params.get_even_coeff()[i] != 0.0):
|
|
70
|
+
m, n = _even_index_to_mn(i)
|
|
71
|
+
psi += params.get_even_coeff()[i] * _zernike_cart(m, n, r, th)
|
|
72
|
+
|
|
73
|
+
return psi
|
|
74
|
+
|
|
75
|
+
def phase_from_odd_zernike(params: CTFParams, Sx_eff: vc.ShaderVariable, Sy_eff: vc.ShaderVariable) -> vc.ShaderVariable:
|
|
76
|
+
psi = vc.new_float_register()
|
|
77
|
+
r = vc.sqrt(Sx_eff * Sx_eff + Sy_eff * Sy_eff).to_register()
|
|
78
|
+
th = vc.atan2(Sy_eff, Sx_eff).to_register()
|
|
79
|
+
for i in range(6):
|
|
80
|
+
with vc.if_block(params.get_odd_coeff()[i] != 0.0):
|
|
81
|
+
m, n = _odd_index_to_mn(i)
|
|
82
|
+
psi += params.get_odd_coeff()[i] * _zernike_cart(m, n, r, th)
|
|
83
|
+
|
|
84
|
+
return psi # [rad]
|
|
85
|
+
|
|
86
|
+
def ctf_filter(
|
|
87
|
+
buffer_shape: tuple[int, int],
|
|
88
|
+
pos_2d: vc.ShaderVariable,
|
|
89
|
+
params: CTFParams,
|
|
90
|
+
pixel_size: float,
|
|
91
|
+
disable_B_factor: bool = False):
|
|
92
|
+
vc.comment("Calculating CTF")
|
|
93
|
+
gamma_lorentz = 1 + params.HT/E_MASS
|
|
94
|
+
beta = vc.sqrt(1 - 1/(gamma_lorentz*gamma_lorentz))
|
|
95
|
+
|
|
96
|
+
epsilon = gamma_lorentz/(1 + params.HT/(2*E_MASS))
|
|
97
|
+
|
|
98
|
+
temp1 = epsilon*params.energy_spread_fwhm/( params.HT * (2 * np.sqrt(2 * np.log(2))) )
|
|
99
|
+
temp2 = epsilon*params.accel_voltage_std
|
|
100
|
+
|
|
101
|
+
wlen = E_COMPTON / (gamma_lorentz * beta) # wavelength in Angstroms
|
|
102
|
+
|
|
103
|
+
sigma_f = params.Cc*vc.sqrt(temp1*temp1 + temp2*temp2 + 4*params.OL_current_std*params.OL_current_std)
|
|
104
|
+
|
|
105
|
+
Cs = params.Cs # [A]
|
|
106
|
+
sigma_beta = params.beam_semiangle # [rad]
|
|
107
|
+
johnson_std = params.johnson_std # [A]
|
|
108
|
+
amp_contrast = params.amp_contrast # [dimensionless]
|
|
109
|
+
zernike = params.zernike # [deg]
|
|
110
|
+
lpp = params.lpp # [deg]
|
|
111
|
+
NA = params.NA # [dimensionless]
|
|
112
|
+
f_OL = params.f_OL # [A]
|
|
113
|
+
lpp_rot = params.lpp_rot # [deg]
|
|
114
|
+
|
|
115
|
+
lpp_rot_rad = np.pi / 180 * lpp_rot # [rad]
|
|
116
|
+
A2_mag = params.A_mag # [A]
|
|
117
|
+
A2_ang = params.A_ang # [deg]
|
|
118
|
+
A2_ang_rad = np.pi / 180 * A2_ang # [rad]
|
|
119
|
+
|
|
120
|
+
wave_shape = vc.new_uvec2_register()
|
|
121
|
+
wave_shape.x = buffer_shape[0]
|
|
122
|
+
wave_shape.y = buffer_shape[1] * 2 - 2
|
|
123
|
+
|
|
124
|
+
Sx_raw = (pos_2d.x/(wave_shape.x * pixel_size)).to_register()
|
|
125
|
+
Sy_raw = (pos_2d.y/(wave_shape.y * pixel_size)).to_register()
|
|
126
|
+
|
|
127
|
+
Sx = (params.mag_00 * Sx_raw + params.mag_01 * Sy_raw).to_register()
|
|
128
|
+
Sy = (params.mag_10 * Sx_raw + params.mag_11 * Sy_raw).to_register()
|
|
129
|
+
|
|
130
|
+
psi_even = phase_from_even_zernike(params, Sx, Sy)
|
|
131
|
+
psi_odd = phase_from_odd_zernike(params, Sx, Sy)
|
|
132
|
+
|
|
133
|
+
Sr_2 = (Sx*Sx + Sy*Sy).to_register()
|
|
134
|
+
Sa = (vc.atan2(Sy, Sx) + np.pi/2).to_register()
|
|
135
|
+
|
|
136
|
+
wlen_2 = wlen*wlen
|
|
137
|
+
|
|
138
|
+
CWS = Cs*wlen_2*Sr_2.to_register()
|
|
139
|
+
|
|
140
|
+
defocus = params.defocus
|
|
141
|
+
|
|
142
|
+
gamma = np.pi*wlen*Sr_2*((CWS*0.5) - defocus - A2_mag*vc.cos(2*(Sa - A2_ang_rad))).to_register()
|
|
143
|
+
|
|
144
|
+
# zernike phase plate
|
|
145
|
+
eta_z = -np.pi / 180 * zernike
|
|
146
|
+
|
|
147
|
+
# laser phase plate
|
|
148
|
+
Dx = f_OL * wlen * (Sy * vc.cos(lpp_rot_rad) + Sx * vc.sin(lpp_rot_rad)) # bfp spatial x coord [A]
|
|
149
|
+
Dy = f_OL * wlen * (-Sy * vc.sin(lpp_rot_rad) + Sx * vc.cos(lpp_rot_rad)) # bfp spatial y coord [A]
|
|
150
|
+
w0 = 1.064e4 / (np.pi * NA) # beam waist radius [A]
|
|
151
|
+
zR = w0 / NA # rayleigh range [A]
|
|
152
|
+
w = w0 * vc.sqrt(1 + (Dx / zR)*(Dx / zR)) # waist at Dx [A]
|
|
153
|
+
k = 2 * np.pi / 1.064e4 # wave number [1/A]
|
|
154
|
+
eta_l = (np.pi / 180 * lpp) * (0.5 * (w0 / w) * vc.exp(-2 * Dy*Dy / (w*w)) * (1 + vc.cos(2 * k * Dx)) - 1) # [rad]
|
|
155
|
+
|
|
156
|
+
# total phase plate phase
|
|
157
|
+
eta = eta_z + eta_l
|
|
158
|
+
|
|
159
|
+
sinchi = vc.sin(psi_even + eta + gamma - amp_contrast).to_register()
|
|
160
|
+
E_TC = vc.exp(-0.5 * sigma_f * sigma_f * np.pi * np.pi * wlen_2 * Sr_2 * Sr_2).to_register()
|
|
161
|
+
E_SC = vc.exp(-2 * sigma_beta * sigma_beta * np.pi * np.pi * Sr_2 * (CWS - defocus)*(CWS - defocus)).to_register()
|
|
162
|
+
E_JN = vc.exp(-2 * np.pi * np.pi * johnson_std*johnson_std * Sr_2).to_register()
|
|
163
|
+
E_BF = vc.exp(-0.25 * params.B * Sr_2).to_register() if not disable_B_factor else 1.0
|
|
164
|
+
|
|
165
|
+
CTF_scale = (2 * E_TC * E_SC * E_JN * E_BF * sinchi).to_register() # technically 2 * ctf
|
|
166
|
+
|
|
167
|
+
CTF = vc.complex_from_euler_angle(psi_odd).to_register()
|
|
168
|
+
CTF.real *= CTF_scale
|
|
169
|
+
CTF.imag *= CTF_scale
|
|
170
|
+
|
|
171
|
+
with vc.if_block(vc.all(Sx == 0.0, Sy == 0.0)):
|
|
172
|
+
CTF.real = 0.0
|
|
173
|
+
CTF.imag = 0.0
|
|
174
|
+
|
|
175
|
+
return CTF
|