|
9 | 9 | import h5py |
10 | 10 | import matplotlib |
11 | 11 | import numpy as np |
| 12 | +from scipy.signal import medfilt2d |
12 | 13 | import yaml |
13 | 14 |
|
14 | 15 | import hexrd.imageseries.save |
|
18 | 19 | from hexrd.instrument.physics_package import HEDPhysicsPackage |
19 | 20 | from hexrd.material import load_materials_hdf5, save_materials_hdf5, Material |
20 | 21 | from hexrd.rotations import RotMatEuler |
| 22 | +from hexrd.utils.decorators import memoize |
21 | 23 | from hexrd.utils.yaml import NumpyToNativeDumper |
22 | 24 | from hexrd.valunits import valWUnit |
23 | 25 |
|
@@ -331,6 +333,7 @@ def __init__(self): |
331 | 333 | self._physics_package = None |
332 | 334 | self._detector_coatings = {} |
333 | 335 | self._instrument_rigid_body_params = {} |
| 336 | + self._median_filer_correction = {} |
334 | 337 |
|
335 | 338 | # Make sure that the matplotlib font size matches the application |
336 | 339 | self.font_size = self.font_size |
@@ -434,6 +437,8 @@ def _attributes_to_persist(self): |
434 | 437 | ('custom_polar_tth_distortion_object_serialized', None), |
435 | 438 | ('detector_coatings_dictified', {}), |
436 | 439 | ('overlays_dictified', []), |
| 440 | + ('apply_median_filter_correction', False), |
| 441 | + ('median_filter_kernel_size', 7), |
437 | 442 | ] |
438 | 443 |
|
439 | 444 | # Provide a mapping from attribute names to the keys used in our state |
@@ -563,6 +568,8 @@ def load_from_state(self, state): |
563 | 568 | self.show_all_colormaps = self.show_all_colormaps == 'true' |
564 | 569 | if not isinstance(self.apply_absorption_correction, bool): |
565 | 570 | self.apply_absorption_correction = self.apply_absorption_correction == 'true' |
| 571 | + if not isinstance(self.apply_median_filter_correction, bool): |
| 572 | + self.apply_median_filter_correction = self.apply_median_filter_correction == 'true' |
566 | 573 |
|
567 | 574 | # This is None sometimes. Make sure it is an empty list instead. |
568 | 575 | if self.recent_state_files is None: |
@@ -973,6 +980,13 @@ def intensity_corrected_images_dict(self): |
973 | 980 | for name, img in images_dict.items(): |
974 | 981 | images_dict[name] = img - minimum |
975 | 982 |
|
| 983 | + if HexrdConfig().apply_median_filter_correction: |
| 984 | + for name, img in images_dict.items(): |
| 985 | + images_dict[name] = medfilt2d_memoized( |
| 986 | + img, |
| 987 | + kernel_size=HexrdConfig().median_filter_kernel_size |
| 988 | + ) |
| 989 | + |
976 | 990 | return images_dict |
977 | 991 |
|
978 | 992 | @property |
@@ -2479,6 +2493,7 @@ def any_intensity_corrections(self): |
2479 | 2493 | 'apply_lorentz_correction', |
2480 | 2494 | 'intensity_subtract_minimum', |
2481 | 2495 | 'apply_absorption_correction', |
| 2496 | + 'apply_median_filter_correction', |
2482 | 2497 | ] |
2483 | 2498 |
|
2484 | 2499 | return any(getattr(self, x) for x in corrections) |
@@ -2992,3 +3007,30 @@ def update_detector_phosphor(self, det_name, **kwargs): |
2992 | 3007 | self._set_detector_coatings('phosphor') |
2993 | 3008 | phosphor = self._detector_coatings[det_name]['phosphor'] |
2994 | 3009 | phosphor.deserialize(**kwargs) |
| 3010 | + |
| 3011 | + @property |
| 3012 | + def apply_median_filter_correction(self): |
| 3013 | + return self._median_filer_correction.get('apply', False) |
| 3014 | + |
| 3015 | + @apply_median_filter_correction.setter |
| 3016 | + def apply_median_filter_correction(self, v): |
| 3017 | + if v != self.apply_median_filter_correction: |
| 3018 | + self._median_filer_correction['apply'] = v |
| 3019 | + self.deep_rerender_needed.emit() |
| 3020 | + |
| 3021 | + @property |
| 3022 | + def median_filter_kernel_size(self): |
| 3023 | + return self._median_filer_correction.get('kernel', 7) |
| 3024 | + |
| 3025 | + @median_filter_kernel_size.setter |
| 3026 | + def median_filter_kernel_size(self, v): |
| 3027 | + if v != self.median_filter_kernel_size: |
| 3028 | + self._median_filer_correction['kernel'] = int(v) |
| 3029 | + self.deep_rerender_needed.emit() |
| 3030 | + |
| 3031 | + |
| 3032 | +# This is set to (num_fiddle_plates * num_time_steps) + num_image_plates |
| 3033 | +# This feature is primarily for FIDDLE |
| 3034 | +@memoize(maxsize=21) |
| 3035 | +def medfilt2d_memoized(img: np.ndarray, kernel_size: int): |
| 3036 | + return medfilt2d(img, kernel_size) |
0 commit comments