Skip to content

Commit

Permalink
Prevent mis-using signal shapes by adding private attr shape -> (row,…
Browse files Browse the repository at this point in the history
… col)

Signed-off-by: Håkon Wiik Ånes <hwaanes@gmail.com>
  • Loading branch information
hakonanes committed Feb 3, 2023
1 parent 56dccb6 commit 01f4e68
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 91 deletions.
10 changes: 5 additions & 5 deletions kikuchipy/data/bruker_h5ebsd/create_bruker_h5ebsd_file.py
Expand Up @@ -31,14 +31,14 @@


s = kp.data.nickel_ebsd_small()
ny, nx = s.axes_manager.navigation_shape[::-1]
ny, nx = s._navigation_shape_rc
n = ny * nx
sy, sx = s.axes_manager.signal_shape[::-1]
sy, sx = s._signal_shape_rc

dir_data = os.path.abspath(os.path.dirname(__file__))


## File with no region of interest (ROI)
# --- File with no region of interest (ROI)
f1 = File(os.path.join(dir_data, "patterns.h5"), mode="w")

# Top group
Expand Down Expand Up @@ -141,7 +141,7 @@
f1.close()


## File with rectangular ROI (SEM group under EBSD group as well)
# --- File with rectangular ROI (SEM group under EBSD group as well)
f2 = File(os.path.join(dir_data, "patterns_roi.h5"), mode="w")

# Top group
Expand Down Expand Up @@ -253,7 +253,7 @@
f2.close()


## File with non-rectangular ROI (SEM group under EBSD group as well)
# --- File with non-rectangular ROI (SEM group under EBSD group as well)
f3 = File(os.path.join(dir_data, "patterns_roi_nonrectangular.h5"), mode="w")

# Top group
Expand Down
Expand Up @@ -54,10 +54,10 @@
orig_data = kp.io.plugins.h5ebsd.hdf5group2dict(group=f["/"], recursive=True)

# Overwrite master patterns in original data
lp_shape = (1, 1) + mp_lp.axes_manager.signal_shape[::-1]
lp_shape = (1, 1) + mp_lp._signal_shape_rc
orig_data["EMData"]["EBSDmaster"]["mLPNH"] = mp_lp.inav[0].data.reshape(lp_shape)
orig_data["EMData"]["EBSDmaster"]["mLPSH"] = mp_lp.inav[1].data.reshape(lp_shape)
sp_shape = (1,) + mp_sp.axes_manager.signal_shape[::-1]
sp_shape = (1,) + mp_sp._signal_shape_rc
orig_data["EMData"]["EBSDmaster"]["masterSPNH"] = mp_sp.inav[0].data.reshape(sp_shape)
orig_data["EMData"]["EBSDmaster"]["masterSPSH"] = mp_sp.inav[1].data.reshape(sp_shape)

Expand Down
4 changes: 2 additions & 2 deletions kikuchipy/data/oxford_h5ebsd/create_oxford_h5ebsd_file.py
Expand Up @@ -37,9 +37,9 @@
euler = rot.to_euler()

s = kp.data.nickel_ebsd_small()
ny, nx = s.axes_manager.navigation_shape[::-1]
ny, nx = s._navigation_shape_rc
n = ny * nx
sy, sx = s.axes_manager.signal_shape[::-1]
sy, sx = s._signal_shape_rc
dx = s.axes_manager["x"].scale

dir_data = os.path.abspath(os.path.dirname(__file__))
Expand Down
2 changes: 1 addition & 1 deletion kikuchipy/draw/tests/test_navigators.py
Expand Up @@ -24,7 +24,7 @@

def test_get_rgb_navigator():
s = kp.data.nickel_ebsd_small(lazy=True).inav[:2, :3]
nav_shape = s.axes_manager.navigation_shape[::-1]
nav_shape = s._navigation_shape_rc
image = np.random.random(np.prod(nav_shape) * 3).reshape(nav_shape + (3,))

s_rgb8 = kp.draw.get_rgb_navigator(image, dtype=np.uint8)
Expand Down
4 changes: 1 addition & 3 deletions kikuchipy/generators/virtual_bse_generator.py
Expand Up @@ -154,9 +154,7 @@ def get_rgb_image(
if isinstance(rois, tuple) or hasattr(rois, "__iter__") is False:
rois = (rois,)

image = np.zeros(
self.signal.axes_manager.navigation_shape[::-1], dtype=np.float64
)
image = np.zeros(self.signal._navigation_shape_rc, dtype=np.float64)
for roi in rois:
if isinstance(roi, tuple):
roi = self.roi_from_grid(roi)
Expand Down
2 changes: 1 addition & 1 deletion kikuchipy/indexing/tests/test_dictionary_indexing.py
Expand Up @@ -164,7 +164,7 @@ def test_dictionary_indexing_navigation_mask_raises(self, dummy_signal):

def test_dictionary_indexing_navigation_mask(self, dummy_signal):
s = dummy_signal
nav_shape = s.axes_manager.navigation_shape[::-1]
nav_shape = s._navigation_shape_rc
nav_size = int(np.prod(nav_shape))
s_dict = kp.signals.EBSD(dummy_signal.data.reshape(nav_size, 3, 3))
s_dict.axes_manager[0].name = "x"
Expand Down
74 changes: 37 additions & 37 deletions kikuchipy/indexing/tests/test_ebsd_refinement.py
Expand Up @@ -90,7 +90,7 @@ def test_refine_check_raises(
):
s = ebsd_with_axes_and_random_data
xmap = get_single_phase_xmap(
nav_shape=s.axes_manager.navigation_shape[::-1],
nav_shape=s._navigation_shape_rc,
step_sizes=tuple(a.scale for a in s.axes_manager.navigation_axes)[::-1],
)
with pytest.raises(ValueError, match=error_msg):
Expand All @@ -101,10 +101,10 @@ def test_refine_check_raises(
def test_refine_raises(self, dummy_signal, get_single_phase_xmap):
s = dummy_signal
xmap = get_single_phase_xmap(
nav_shape=s.axes_manager.navigation_shape[::-1],
nav_shape=s._navigation_shape_rc,
step_sizes=tuple(a.scale for a in s.axes_manager.navigation_axes)[::-1],
)
detector = kp.detectors.EBSDDetector(shape=s.axes_manager.signal_shape[::-1])
detector = kp.detectors.EBSDDetector(shape=s._signal_shape_rc)
refine_kwargs = dict(master_pattern=self.mp, energy=20, detector=detector)

with pytest.raises(ValueError, match="Method 'a' not in the list of supported"):
Expand All @@ -123,11 +123,11 @@ def test_refine_raises(self, dummy_signal, get_single_phase_xmap):
def test_refine_signal_mask(self, dummy_signal, get_single_phase_xmap):
s = dummy_signal
xmap = get_single_phase_xmap(
nav_shape=s.axes_manager.navigation_shape[::-1],
nav_shape=s._navigation_shape_rc,
rotations_per_point=1,
step_sizes=tuple(a.scale for a in s.axes_manager.navigation_axes)[::-1],
)
det = kp.detectors.EBSDDetector(shape=s.axes_manager.signal_shape[::-1])
det = kp.detectors.EBSDDetector(shape=s._signal_shape_rc)
ref_kw = dict(
xmap=xmap,
master_pattern=self.mp,
Expand All @@ -137,7 +137,7 @@ def test_refine_signal_mask(self, dummy_signal, get_single_phase_xmap):
method_kwargs=dict(method="Nelder-Mead", options=dict(maxfev=10)),
)
xmap_ref_no_mask = s.refine_orientation(**ref_kw)
signal_mask = np.zeros(s.axes_manager.signal_shape[::-1], dtype=bool)
signal_mask = np.zeros(s._signal_shape_rc, dtype=bool)
signal_mask[0, 0] = 1 # Mask away upper left pixel

xmap_ref_mask = s.refine_orientation(signal_mask=signal_mask, **ref_kw)
Expand Down Expand Up @@ -193,7 +193,7 @@ def test_refine_orientation_chunking(
"""
s = ebsd_with_axes_and_random_data
xmap = get_single_phase_xmap(
nav_shape=s.axes_manager.navigation_shape[::-1],
nav_shape=s._navigation_shape_rc,
rotations_per_point=1,
step_sizes=tuple(a.scale for a in s.axes_manager.navigation_axes)[::-1],
)
Expand All @@ -214,13 +214,13 @@ def test_refine_raises_nlopt_import_error(
self, dummy_signal, get_single_phase_xmap
): # pragma: no cover
s = dummy_signal
nav_shape = s.axes_manager.navigation_shape[::-1]
nav_shape = s._navigation_shape_rc
xmap = get_single_phase_xmap(
nav_shape=nav_shape,
rotations_per_point=1,
step_sizes=tuple(a.scale for a in s.axes_manager.navigation_axes)[::-1],
)
det = kp.detectors.EBSDDetector(shape=s.axes_manager.signal_shape[::-1])
det = kp.detectors.EBSDDetector(shape=s._signal_shape_rc)

with pytest.raises(ImportError, match="Package `nlopt`, required for method "):
_ = s.refine_orientation_projection_center(
Expand All @@ -236,13 +236,13 @@ def test_refine_raises_initial_step_nlopt(
self, dummy_signal, get_single_phase_xmap
):
s = dummy_signal
nav_shape = s.axes_manager.navigation_shape[::-1]
nav_shape = s._navigation_shape_rc
xmap = get_single_phase_xmap(
nav_shape=nav_shape,
rotations_per_point=1,
step_sizes=tuple(a.scale for a in s.axes_manager.navigation_axes)[::-1],
)
det = kp.detectors.EBSDDetector(shape=s.axes_manager.signal_shape[::-1])
det = kp.detectors.EBSDDetector(shape=s._signal_shape_rc)

with pytest.raises(
ValueError, match="The initial step must be a single number"
Expand Down Expand Up @@ -396,7 +396,7 @@ def test_refine_orientation_local(
):
s = ebsd_with_axes_and_random_data
xmap = get_single_phase_xmap(
nav_shape=s.axes_manager.navigation_shape[::-1],
nav_shape=s._navigation_shape_rc,
rotations_per_point=1,
step_sizes=tuple(a.scale for a in s.axes_manager.navigation_axes)[::-1],
)
Expand Down Expand Up @@ -454,7 +454,7 @@ def test_refine_orientation_local_nlopt(
):
s = ebsd_with_axes_and_random_data
xmap = get_single_phase_xmap(
nav_shape=s.axes_manager.navigation_shape[::-1],
nav_shape=s._navigation_shape_rc,
rotations_per_point=1,
step_sizes=tuple(a.scale for a in s.axes_manager.navigation_axes)[::-1],
)
Expand All @@ -480,10 +480,10 @@ def test_refine_orientation_not_compute(
):
s = dummy_signal
xmap = get_single_phase_xmap(
nav_shape=s.axes_manager.navigation_shape[::-1],
nav_shape=s._navigation_shape_rc,
step_sizes=tuple(a.scale for a in s.axes_manager.navigation_axes)[::-1],
)
det = kp.detectors.EBSDDetector(shape=s.axes_manager.signal_shape[::-1])
det = kp.detectors.EBSDDetector(shape=s._signal_shape_rc)

dask_arr = s.refine_orientation(
xmap=xmap,
Expand Down Expand Up @@ -527,9 +527,9 @@ def test_refine_orientation_global(
get_single_phase_xmap,
):
s = ebsd_with_axes_and_random_data
det = kp.detectors.EBSDDetector(shape=s.axes_manager.signal_shape[::-1])
det = kp.detectors.EBSDDetector(shape=s._signal_shape_rc)
xmap = get_single_phase_xmap(
nav_shape=s.axes_manager.navigation_shape[::-1],
nav_shape=s._navigation_shape_rc,
rotations_per_point=1,
step_sizes=tuple(a.scale for a in s.axes_manager.navigation_axes)[::-1],
)
Expand All @@ -552,7 +552,7 @@ def test_refine_orientation_nickel_ebsd_small(self):
s = self.nickel_ebsd_small

energy = 20
signal_mask = kp.filters.Window("circular", s.axes_manager.signal_shape[::-1])
signal_mask = kp.filters.Window("circular", s._signal_shape_rc)
signal_mask = ~signal_mask.astype(bool)
xmap_ref = s.refine_orientation(
xmap=s.xmap,
Expand All @@ -573,7 +573,7 @@ def test_refine_orientation_nickel_ebsd_small_nlopt(self):
s = self.nickel_ebsd_small

energy = 20
signal_mask = kp.filters.Window("circular", s.axes_manager.signal_shape[::-1])
signal_mask = kp.filters.Window("circular", s._signal_shape_rc)
signal_mask = ~signal_mask.astype(bool)

xmap_ref = s.refine_orientation(
Expand All @@ -595,7 +595,7 @@ def test_refine_orientation_pseudo_symmetry_nlopt(self):
s = self.nickel_ebsd_small

energy = 20
signal_mask = kp.filters.Window("circular", s.axes_manager.signal_shape[::-1])
signal_mask = kp.filters.Window("circular", s._signal_shape_rc)
signal_mask = ~signal_mask.astype(bool)

rot_ps = Rotation.from_axes_angles([[0, 0, 1], [0, 0, -1]], np.deg2rad(30))
Expand Down Expand Up @@ -625,7 +625,7 @@ def test_refine_orientation_pseudo_symmetry_scipy(self):
s = self.nickel_ebsd_small

energy = 20
signal_mask = kp.filters.Window("circular", s.axes_manager.signal_shape[::-1])
signal_mask = kp.filters.Window("circular", s._signal_shape_rc)
signal_mask = ~signal_mask.astype(bool)

rot_ps = Rotation.from_axes_angles([[0, 0, 1], [0, 0, -1]], np.deg2rad(30))
Expand Down Expand Up @@ -698,7 +698,7 @@ def test_refine_projection_center_local(
get_single_phase_xmap,
):
s = ebsd_with_axes_and_random_data
nav_shape = s.axes_manager.navigation_shape[::-1]
nav_shape = s._navigation_shape_rc
xmap = get_single_phase_xmap(
nav_shape=nav_shape,
rotations_per_point=1,
Expand Down Expand Up @@ -765,7 +765,7 @@ def test_refine_projection_center_local_nlopt(
get_single_phase_xmap,
):
s = ebsd_with_axes_and_random_data
nav_shape = s.axes_manager.navigation_shape[::-1]
nav_shape = s._navigation_shape_rc
xmap = get_single_phase_xmap(
nav_shape=nav_shape,
rotations_per_point=1,
Expand Down Expand Up @@ -823,9 +823,9 @@ def test_refine_projection_center_global(
get_single_phase_xmap,
):
s = ebsd_with_axes_and_random_data
detector = kp.detectors.EBSDDetector(shape=s.axes_manager.signal_shape[::-1])
detector = kp.detectors.EBSDDetector(shape=s._signal_shape_rc)
xmap = get_single_phase_xmap(
nav_shape=s.axes_manager.navigation_shape[::-1],
nav_shape=s._navigation_shape_rc,
rotations_per_point=1,
step_sizes=tuple(a.scale for a in s.axes_manager.navigation_axes)[::-1],
)
Expand All @@ -851,10 +851,10 @@ def test_refine_projection_center_not_compute(
):
s = dummy_signal
xmap = get_single_phase_xmap(
nav_shape=s.axes_manager.navigation_shape[::-1],
nav_shape=s._navigation_shape_rc,
step_sizes=tuple(a.scale for a in s.axes_manager.navigation_axes)[::-1],
)
det = kp.detectors.EBSDDetector(shape=s.axes_manager.signal_shape[::-1])
det = kp.detectors.EBSDDetector(shape=s._signal_shape_rc)

dask_arr = s.refine_projection_center(
xmap=xmap,
Expand Down Expand Up @@ -886,13 +886,13 @@ def test_refine_orientation_projection_center_local(
get_single_phase_xmap,
):
s = dummy_signal
nav_shape = s.axes_manager.navigation_shape[::-1]
nav_shape = s._navigation_shape_rc
xmap = get_single_phase_xmap(
nav_shape=nav_shape,
rotations_per_point=1,
step_sizes=tuple(a.scale for a in s.axes_manager.navigation_axes)[::-1],
)
det = kp.detectors.EBSDDetector(shape=s.axes_manager.signal_shape[::-1])
det = kp.detectors.EBSDDetector(shape=s._signal_shape_rc)
method_kwargs.update(dict(options=dict(maxfev=10)))
signal_mask = np.zeros(det.shape, dtype=bool)

Expand Down Expand Up @@ -929,13 +929,13 @@ def test_refine_orientation_projection_center_local_nlopt(
get_single_phase_xmap,
): # pragma: no cover
s = dummy_signal
nav_shape = s.axes_manager.navigation_shape[::-1]
nav_shape = s._navigation_shape_rc
xmap = get_single_phase_xmap(
nav_shape=nav_shape,
rotations_per_point=1,
step_sizes=tuple(a.scale for a in s.axes_manager.navigation_axes)[::-1],
)
det = kp.detectors.EBSDDetector(shape=s.axes_manager.signal_shape[::-1])
det = kp.detectors.EBSDDetector(shape=s._signal_shape_rc)
signal_mask = np.zeros(det.shape, dtype=bool)

xmap_ref, det_ref = s.refine_orientation_projection_center(
Expand Down Expand Up @@ -984,9 +984,9 @@ def test_refine_orientation_projection_center_global(
get_single_phase_xmap,
):
s = dummy_signal
det = kp.detectors.EBSDDetector(shape=s.axes_manager.signal_shape[::-1])
det = kp.detectors.EBSDDetector(shape=s._signal_shape_rc)
xmap = get_single_phase_xmap(
nav_shape=s.axes_manager.navigation_shape[::-1],
nav_shape=s._navigation_shape_rc,
rotations_per_point=1,
step_sizes=tuple(a.scale for a in s.axes_manager.navigation_axes)[::-1],
)
Expand All @@ -1010,11 +1010,11 @@ def test_refine_orientation_projection_center_not_compute(
):
s = dummy_signal
xmap = get_single_phase_xmap(
nav_shape=s.axes_manager.navigation_shape[::-1],
nav_shape=s._navigation_shape_rc,
rotations_per_point=1,
step_sizes=tuple(a.scale for a in s.axes_manager.navigation_axes)[::-1],
)
det = kp.detectors.EBSDDetector(shape=s.axes_manager.signal_shape[::-1])
det = kp.detectors.EBSDDetector(shape=s._signal_shape_rc)

dask_array = s.refine_orientation_projection_center(
xmap=xmap,
Expand All @@ -1034,7 +1034,7 @@ def test_refine_orientation_pc_pseudo_symmetry_nlopt(self):
s = self.nickel_ebsd_small

energy = 20
signal_mask = kp.filters.Window("circular", s.axes_manager.signal_shape[::-1])
signal_mask = kp.filters.Window("circular", s._signal_shape_rc)
signal_mask = ~signal_mask.astype(bool)

rot_ps = Rotation.from_axes_angles([[0, 0, 1], [0, 0, -1]], np.deg2rad(30))
Expand Down Expand Up @@ -1064,7 +1064,7 @@ def test_refine_orientation_pc_pseudo_symmetry_scipy(self):
s = self.nickel_ebsd_small

energy = 20
signal_mask = kp.filters.Window("circular", s.axes_manager.signal_shape[::-1])
signal_mask = kp.filters.Window("circular", s._signal_shape_rc)
signal_mask = ~signal_mask.astype(bool)

rot_ps = Rotation.from_axes_angles([[0, 0, 1], [0, 0, -1]], np.deg2rad(30))
Expand Down
2 changes: 1 addition & 1 deletion kikuchipy/io/plugins/tests/test_edax_binary.py
Expand Up @@ -89,4 +89,4 @@ def test_nav_shape(self):
def test_nav_shape_up2_not_hex(self, edax_binary_file):
"""Test navigation shape when not hex."""
s = kp.load(edax_binary_file.name)
assert s.axes_manager.navigation_shape[::-1] == (2, 3)
assert s._navigation_shape_rc == (2, 3)
2 changes: 1 addition & 1 deletion kikuchipy/io/plugins/tests/test_oxford_binary.py
Expand Up @@ -99,7 +99,7 @@ def test_not_all_patterns_present(self, oxford_binary_file):
def test_versions(self, oxford_binary_file, ver, desired_nav_shape):
"""Ensure that versions 0, 1 and > 1 can be read."""
s = kp.load(oxford_binary_file.name)
assert s.axes_manager.navigation_shape[::-1] == desired_nav_shape
assert s._navigation_shape_rc == desired_nav_shape
if ver > 0:
assert s.original_metadata.has_item("beam_x")
assert s.original_metadata.has_item("beam_y")
Expand Down

0 comments on commit 01f4e68

Please sign in to comment.