Repository navigation
Vignette masking and cosmic ray masking tasks #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
80e1d4c
2077205
14b7725
0e6a2f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,9 +15,7 @@ | |
| from eregion.utils import ( | ||
| ensure_dataarray, | ||
| slice_data, | ||
| ensure_numpy, | ||
| configure_logger, | ||
| decrease_slicer_stop_index, | ||
| set_slice_in_data, | ||
| XRDATA | ||
| ) | ||
|
|
@@ -101,7 +99,8 @@ class Output(Mappable): | |
| ) | ||
| masks: Optional[xr.Dataset] = Field( | ||
| default=None, | ||
| description="Optional xr.Dataset containing masks for this output.", | ||
| description="Optional xr.Dataset containing masks for this output. " | ||
| "Write masks via set_mask() so the parent DetImage's full mask is rebuilt.", | ||
| exclude=True, | ||
| ) | ||
|
|
||
|
|
@@ -137,6 +136,22 @@ def get_output_mask(self, mask_key: str | Literal['combined', 'all'] = 'combined | |
| else: | ||
| return None | ||
|
|
||
| def set_mask(self, mask_key: str, mask: xr.DataArray, combine: bool = True): | ||
| """ | ||
| Set a mask for this output and clear the parent's full mask so it is rebuilt on next access. | ||
| :param mask_key: str, name of the mask data_var in self.masks. | ||
| :param mask: xr.DataArray, boolean mask with the same coords as this output. | ||
| :param combine: bool, if True OR with an existing mask under mask_key, else replace it. | ||
| """ | ||
| if self.masks is None: | ||
| self.masks = mask.to_dataset(name=mask_key) | ||
| elif combine and mask_key in self.masks: | ||
| self.masks[mask_key] = self.masks[mask_key] | mask | ||
| else: | ||
| self.masks[mask_key] = mask | ||
| if self.parent is not None: | ||
| self.parent.masks = None | ||
|
|
||
| def get_masked_output_data(self, data_var: str = 'data', mask_key: str = 'combined', fill_value=np.nan): | ||
| mask = self.get_output_mask(mask_key) | ||
| datarr = self.get_output_data(data_var) | ||
|
|
@@ -256,7 +271,8 @@ def _select_header_and_data( | |
|
|
||
| return header, data | ||
|
|
||
| def show(self, ax=None, save=None, with_mask=True, mask_key='sigma_clip_mask', data_var='data', **imshow_kwargs): | ||
| def show(self, ax=None, save=None, with_mask=True, mask_key='sigma_clip_mask', data_var='data', | ||
| **imshow_kwargs): | ||
| """ | ||
| Plot this output's image. | ||
| :param ax: Matplotlib Axes object to plot on. If None, a new figure and axes are created. | ||
|
|
@@ -398,8 +414,8 @@ def get_overscan( | |
| """ | ||
| return self.get_scan(axis=axis, kind='overscan', corner=corner, data_var=data_var) | ||
|
|
||
| def show(self, ax=None, shade_regions=False, save=None, with_mask=True, mask_key='sigma_clip_mask', data_var='data', | ||
| **imshow_kwargs): | ||
| def show(self, ax=None, save=None, with_mask=True, mask_key='sigma_clip_mask', data_var='data', | ||
| shade_regions=False, **imshow_kwargs): | ||
| ax = super().show(ax=ax, save=None, with_mask=with_mask, mask_key=mask_key, data_var=data_var, **imshow_kwargs) | ||
|
|
||
| if shade_regions: | ||
|
|
@@ -697,8 +713,6 @@ def _build_full_mask(self) -> bool: | |
| Build a full mask from the individual output masks. | ||
| :return: True if a full mask was built, False otherwise. | ||
| """ | ||
| if self.masks is not None: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good fix, I think we may hit a performance issue though since now every call to rebuilds the full mask from scratch For performance improvement we could clear the cache when output masks change
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I'll put in something to avoid unnecessary mask rebuilding |
||
| return True | ||
| self.masks = xr.Dataset(coords=self.data.coords) | ||
| for out_id, output in self.outputs.items(): | ||
| if output.masks is not None: | ||
|
|
@@ -722,7 +736,8 @@ def get_mask(self, mask_key: str | Literal['all', 'combined'] = 'combined') -> X | |
| :param mask_key: str or 'all' or 'combined'. Returns dataset if 'all'. Returns DataArray if key is specified. Returns OR combined mask from all keys if 'combined'. | ||
| :return: xr.DataArray or xr.Dataset | ||
| """ | ||
| if self._build_full_mask(): | ||
| built = self._build_full_mask() if self.masks is None else True | ||
| if built: | ||
| match mask_key: | ||
| case 'all': | ||
| return self.masks | ||
|
|
@@ -739,7 +754,8 @@ def get_mask(self, mask_key: str | Literal['all', 'combined'] = 'combined') -> X | |
|
|
||
| @property | ||
| def combined_mask(self): | ||
| if self._build_full_mask(): | ||
| built = self._build_full_mask() if self.masks is None else True | ||
| if built: | ||
| combmask = np.any(self.masks.to_array().to_numpy(), axis=0) | ||
| return xr.DataArray(combmask, dims=self.masks.dims, coords=self.masks.coords) | ||
| else: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.