Skip to content

Commit 90906c9

Browse files
committed
add draft of phase_flip to MicrographSource
[skip ci]
1 parent 5d2fe6a commit 90906c9

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

src/aspire/source/micrograph.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from aspire.source import Simulation
1212
from aspire.source.image import _ImageAccessor
1313
from aspire.storage import StarFile
14-
from aspire.utils import Random, grid_2d, rename_with_timestamp
14+
from aspire.utils import Random, grid_2d, rename_with_timestamp, trange
1515
from aspire.volume import Volume
1616

1717
logger = logging.getLogger(__name__)
@@ -121,6 +121,42 @@ def _images(self, indices):
121121
:return: An `Image` object representing the micrographs for `indices`.
122122
"""
123123

124+
def phase_flip(self, filters):
125+
"""
126+
Perform phase flip on micrographs in the source object using CTF information.
127+
If no CTFFilters exist this will emit a warning and otherwise no-op.
128+
"""
129+
130+
logger.info("Perform phase flip on source object")
131+
filters = list(filters) # unpack any generators
132+
133+
if len(filters) >= 1:
134+
assert len(filters) == self.micrograph_count
135+
136+
logger.info("Phaseflipping")
137+
phase_flipped_micrographs = np.empty(
138+
(self.micrograph_count, *self.micrograph_size), dtype=self.dtype
139+
)
140+
for i in trange(self.micrograph_count, desc=f"Phaseflipping micrograph"):
141+
# micrograph = self.images[i]
142+
# f = filters[i].sign
143+
# ... = micrograph.filter(f)
144+
phase_flipped_micrographs[i] = self.images[i].filter(filters[i].sign)
145+
146+
return ArrayMicrographSource(
147+
micrographs=phase_flipped_micrographs, pixel_size=self.pixel_size
148+
)
149+
150+
else:
151+
# No CTF filters found
152+
logger.warning(
153+
"No Filters found."
154+
" `phase_flip` is a no-op without Filters."
155+
" Confirm you have correctly populated CTFFilters."
156+
)
157+
158+
return self
159+
124160

125161
class ArrayMicrographSource(MicrographSource):
126162
def __init__(self, micrographs, dtype=None, pixel_size=None):
@@ -287,7 +323,7 @@ def _images(self, indices):
287323

288324
# Continually compare with initial pixel_size
289325
if _pixel_size is not None and _pixel_size != self.pixel_size:
290-
msg = f"Mismatched pixel size. {micrograph.pixel_size} angstroms defined in {self.micrograph_files[ind]}, but provided {self.pixel_size} angstroms."
326+
msg = f"Mismatched pixel size. {_pixel_size} angstroms defined in {self.micrograph_files[ind]}, but provided {self.pixel_size} angstroms."
291327
warnings.warn(msg, UserWarning, stacklevel=2)
292328

293329
# Assign to array, implicitly performs casting to dtype

0 commit comments

Comments
 (0)