Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion hexrd/projections/polar.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
_project_on_detector_cylinder,
_project_on_detector_plane,
)
from hexrd.utils.panel_buffer import panel_buffer_as_2d_array


class PolarView:
Expand Down Expand Up @@ -325,10 +326,25 @@ def _warp_image_from_coordinate_map(
coordinate_map: dict[str, dict[str, np.ndarray]],
pad_with_nans: bool = False,
do_interpolation=True) -> np.ma.MaskedArray:

panel_buffer_fill_value = np.nan
img_dict = dict.fromkeys(self.detectors)
nan_mask = None
for detector_id, panel in self.detectors.items():
img = image_dict[detector_id]
# Make a copy since we may modify
img = image_dict[detector_id].copy()

# Before warping, mask out any pixels that are invalid,
# so that they won't affect the results.
buffer = panel_buffer_as_2d_array(panel)
if (np.issubdtype(type(panel_buffer_fill_value), np.floating) and
not np.issubdtype(img.dtype, np.floating)):
# Convert to float. This is especially important
# for nan, since it is a float...
img = img.astype(float)

img[~buffer] = panel_buffer_fill_value

xypts = coordinate_map[detector_id]['xypts']
on_panel = coordinate_map[detector_id]['on_panel']

Expand Down
30 changes: 30 additions & 0 deletions hexrd/utils/panel_buffer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import numpy as np

from hexrd.instrument.detector import Detector


def panel_buffer_as_2d_array(panel: Detector) -> np.ndarray:
# Take whatever the panel buffer is and convert it to a 2D array
if panel.panel_buffer is None:
# Just make a panel buffer with all True values
return np.ones(panel.shape, dtype=bool)
elif panel.panel_buffer.shape == (2,):
# The two floats are specifying the borders in mm for x and y.
# Convert to pixel borders. Swap x and y so we have i, j in pixels.
borders = np.round([
panel.panel_buffer[1] / panel.pixel_size_row,
panel.panel_buffer[0] / panel.pixel_size_col,
]).astype(int)

# Convert to array
panel_buffer = np.zeros(panel.shape, dtype=bool)

# We can't do `-borders[i]` since that doesn't work for 0,
# so we must do `panel.shape[i] - borders[i]` instead.
panel_buffer[borders[0]:panel.shape[0] - borders[0],
borders[1]:panel.shape[1] - borders[1]] = True
return panel_buffer
elif panel.panel_buffer.ndim == 2:
return panel.panel_buffer

raise NotImplementedError(panel.panel_buffer.ndim)
Binary file modified tests/data/test_polar_view_expected.npy
Binary file not shown.
4 changes: 1 addition & 3 deletions tests/test_polar_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ def test_polar_view(
img = img.filled(np.nan)

# Verify that the image is identical to a reference image
ref = np.load(
test_data_dir / 'test_polar_view_expected.npy', allow_pickle=True
)
ref = np.load(test_data_dir / 'test_polar_view_expected.npy')
assert np.allclose(img, ref, equal_nan=True)

# Also generate it using the cache
Expand Down
Loading