|
11 | 11 | from aspire.source import Simulation |
12 | 12 | from aspire.source.image import _ImageAccessor |
13 | 13 | 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 |
15 | 15 | from aspire.volume import Volume |
16 | 16 |
|
17 | 17 | logger = logging.getLogger(__name__) |
@@ -121,6 +121,42 @@ def _images(self, indices): |
121 | 121 | :return: An `Image` object representing the micrographs for `indices`. |
122 | 122 | """ |
123 | 123 |
|
| 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 | + |
124 | 160 |
|
125 | 161 | class ArrayMicrographSource(MicrographSource): |
126 | 162 | def __init__(self, micrographs, dtype=None, pixel_size=None): |
@@ -287,7 +323,7 @@ def _images(self, indices): |
287 | 323 |
|
288 | 324 | # Continually compare with initial pixel_size |
289 | 325 | 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." |
291 | 327 | warnings.warn(msg, UserWarning, stacklevel=2) |
292 | 328 |
|
293 | 329 | # Assign to array, implicitly performs casting to dtype |
|
0 commit comments