|
2 | 2 | import functools
|
3 | 3 | import logging
|
4 | 4 | import os.path
|
| 5 | +import warnings |
5 | 6 | from abc import ABC, abstractmethod
|
6 | 7 | from collections import OrderedDict
|
7 | 8 | from collections.abc import Iterable
|
@@ -204,30 +205,48 @@ def __init__(
|
204 | 205 | )
|
205 | 206 | )
|
206 | 207 |
|
207 |
| - # Populate pixel_size with metadata if possible. |
208 |
| - if pixel_size is None: |
209 |
| - if self.has_metadata(["_rlnImagePixelSize"]): |
210 |
| - pixel_size = self.get_metadata(["_rlnImagePixelSize"])[0] |
211 |
| - elif self.has_metadata(["_rlnDetectorPixelSize", "_rlnMagnification"]): |
212 |
| - detector_pixel_size = self.get_metadata(["_rlnDetectorPixelSize"])[0] |
213 |
| - magnification = self.get_metadata(["_rlnMagnification"])[0] |
214 |
| - pixel_size = 10000 * detector_pixel_size / magnification |
215 |
| - else: |
216 |
| - raise ValueError( |
217 |
| - "No pixel size found in metadata. Pixel size must be provided." |
218 |
| - ) |
219 |
| - else: |
220 |
| - self.set_metadata("_rlnImagePixelSize", pixel_size) |
221 |
| - |
222 |
| - self.pixel_size = float(pixel_size) |
223 |
| - |
| 208 | + self._populate_pixel_size(pixel_size) |
224 | 209 | self._populate_symmetry_group(symmetry_group)
|
225 | 210 |
|
226 | 211 | self.unique_filters = []
|
227 | 212 | self.generation_pipeline = Pipeline(xforms=None, memory=memory)
|
228 | 213 |
|
229 | 214 | logger.info(f"Creating {self.__class__.__name__} with {len(self)} images.")
|
230 | 215 |
|
| 216 | + def _populate_pixel_size(self, pixel_size): |
| 217 | + # Populate pixel_size from metadata if possible. |
| 218 | + _pixel_size = None |
| 219 | + if self.has_metadata(["_rlnImagePixelSize"]): |
| 220 | + _pixel_size = self.get_metadata(["_rlnImagePixelSize"])[0] |
| 221 | + elif self.has_metadata(["_rlnDetectorPixelSize", "_rlnMagnification"]): |
| 222 | + detector_pixel_size = self.get_metadata(["_rlnDetectorPixelSize"])[0] |
| 223 | + magnification = self.get_metadata(["_rlnMagnification"])[0] |
| 224 | + _pixel_size = 10000 * detector_pixel_size / magnification |
| 225 | + |
| 226 | + # Resolve any pixel_size conflicts. |
| 227 | + if _pixel_size is None and pixel_size is None: |
| 228 | + raise ValueError( |
| 229 | + "No pixel size found in metadata. Please provide `pixel_size` argument." |
| 230 | + ) |
| 231 | + |
| 232 | + if pixel_size is not None: |
| 233 | + # If both provided prefer user, warn on mismatch. |
| 234 | + if _pixel_size is not None and not np.allclose(_pixel_size, pixel_size): |
| 235 | + warnings.warn( |
| 236 | + f"User provided pixel_size: {pixel_size} angstrom, does not match" |
| 237 | + f" pixel_size found in metadata: {_pixel_size} angstrom. Setting" |
| 238 | + f" pixel_size to {pixel_size} angstrom.", |
| 239 | + UserWarning, |
| 240 | + stacklevel=2, |
| 241 | + ) |
| 242 | + chosen = float(pixel_size) |
| 243 | + else: |
| 244 | + chosen = float(_pixel_size) |
| 245 | + |
| 246 | + # Set pixel_size attribute and metadata. |
| 247 | + self.set_metadata("_rlnImagePixelSize", chosen) |
| 248 | + self.pixel_size = chosen |
| 249 | + |
231 | 250 | @property
|
232 | 251 | def symmetry_group(self):
|
233 | 252 | """
|
|
0 commit comments