Skip to content

Commit

Permalink
Set iterpath, crop_image->crop_signal, import new markers
Browse files Browse the repository at this point in the history
Signed-off-by: Håkon Wiik Ånes <[email protected]>
  • Loading branch information
hakonanes committed Nov 26, 2023
1 parent 7ca0dfa commit efb5806
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 115 deletions.
6 changes: 3 additions & 3 deletions examples/selecting_data/crop_signal_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
This example shows various ways to crop the signal axes of an
:class:`~kikuchipy.signals.EBSD` signal using HyperSpy's ``isig`` slicer and the
:meth:`~kikuchipy.signals.EBSD.crop` and
:meth:`~hyperspy._signals.signal2d.Signal2D.crop_image` methods (see
:meth:`~hyperspy._signals.signal2d.Signal2D.crop_signal` methods (see
:ref:`hyperspy:signal.indexing` for details).
"""

Expand Down Expand Up @@ -49,10 +49,10 @@
print(s3.detector)

# %%
# Do the same inplace using ``crop_image()``
# Do the same inplace using :meth:`~hyperspy._signals.signal2d.Signal2D.crop_signal`

s4 = s.deepcopy()
s4.crop_image(top=10, bottom=50, left=5, right=55)
s4.crop_signal(top=10, bottom=50, left=5, right=55)

_ = hs.plot.plot_images(s4, **plot_kwds)
print(s4)
Expand Down
8 changes: 4 additions & 4 deletions kikuchipy/draw/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from typing import Union

from hyperspy.utils.markers import line_segment, point, text
import hyperspy.api as hs
import numpy as np


Expand Down Expand Up @@ -50,7 +50,7 @@ def get_line_segment_list(lines: Union[list, np.ndarray], **kwargs) -> list:
y1 = lines[..., i, 1]
x2 = lines[..., i, 2]
y2 = lines[..., i, 3]
marker_list.append(line_segment(x1=x1, y1=y1, x2=x2, y2=y2, **kwargs))
marker_list.append(hs.plot.markers.Lines([[x1, y1], [x2, y2]], **kwargs))
return marker_list


Expand All @@ -77,7 +77,7 @@ def get_point_list(points: Union[list, np.ndarray], **kwargs) -> list:
for i in range(points.shape[-2]): # Iterate over zone axes
if not np.allclose(points[..., i, :], np.nan, equal_nan=True):
marker_list.append(
point(x=points[..., i, 0], y=points[..., i, 1], **kwargs)
hs.plot.markers.Points([points[..., i, 0], points[..., i, 1]], **kwargs)
)
return marker_list

Expand Down Expand Up @@ -114,6 +114,6 @@ def get_text_list(
y = coordinates[..., i, 1]
x[~is_finite[..., i]] = np.nan
y[~is_finite[..., i]] = np.nan
text_marker = text(x=x, y=y, text=texts[i], **kwargs)
text_marker = hs.plot.markers.Texts([x, y], s=texts[i], **kwargs)
marker_list.append(text_marker)
return marker_list
8 changes: 4 additions & 4 deletions kikuchipy/draw/tests/test_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with kikuchipy. If not, see <http://www.gnu.org/licenses/>.

from hyperspy.utils.markers import line_segment, point, text
import hyperspy.api as hs
import numpy as np
import pytest

Expand All @@ -35,7 +35,7 @@ def test_get_line_segment_list0d(self, n_lines):
# Number of markers
assert isinstance(line_markers, list)
assert len(line_markers) == n_lines
assert isinstance(line_markers[0], line_segment)
assert isinstance(line_markers, hs.plot.markers.Lines)

# Coordinates, data shape and marker properties
for line, marker in zip(lines, line_markers):
Expand Down Expand Up @@ -119,7 +119,7 @@ def test_get_point_list0d(self, n_points):
# Number of markers
assert isinstance(point_markers, list)
assert len(point_markers) == n_points
assert isinstance(point_markers[0], point)
assert isinstance(point_markers, hs.plot.markers.Points)

# Coordinates, data shape and marker properties
for i, marker in zip(points, point_markers):
Expand Down Expand Up @@ -212,7 +212,7 @@ def test_get_text_list0d(self, n_labels):
# Number of markers
assert isinstance(text_markers, list)
assert len(text_markers) == n_labels
assert isinstance(text_markers[0], text)
assert isinstance(text_markers, hs.plot.markers.Texts)

# Coordinates, data shape and marker properties
for i, (t, marker) in enumerate(zip(text_coords, text_markers)):
Expand Down
5 changes: 1 addition & 4 deletions kikuchipy/draw/tests/test_navigators.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
# You should have received a copy of the GNU General Public License
# along with kikuchipy. If not, see <http://www.gnu.org/licenses/>.

from hyperspy.misc.rgb_tools import rgb8, rgb16
import matplotlib.pyplot as plt
import numpy as np
from rsciio.utils.rgb_tools import rgb8, rgb16

import kikuchipy as kp

Expand All @@ -38,5 +37,3 @@ def test_get_rgb_navigator():
rgb16_data = s_rgb16.data
assert rgb16_data.shape == nav_shape
assert np.issubdtype(rgb16_data.dtype, rgb16)

plt.close("all")
41 changes: 21 additions & 20 deletions kikuchipy/imaging/vbse.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@
from typing import List, Optional, Tuple, Union

from dask.array import Array
from hyperspy.drawing._markers.horizontal_line import HorizontalLine
from hyperspy.drawing._markers.vertical_line import VerticalLine
from hyperspy.drawing._markers.rectangle import Rectangle
from hyperspy.drawing._markers.text import Text
from hyperspy.roi import BaseInteractiveROI, RectangularROI
from hyperspy._signals.signal2d import Signal2D
import hyperspy.api as hs
from hyperspy.roi import BaseInteractiveROI
import numpy as np

from kikuchipy.signals import EBSD, LazyEBSD
Expand Down Expand Up @@ -165,7 +161,7 @@ def get_rgb_image(

channels.append(image)

if alpha is not None and isinstance(alpha, Signal2D):
if alpha is not None and isinstance(alpha, hs.signals.Signal2D):
alpha = alpha.data

dtype_out = np.dtype(dtype_out)
Expand Down Expand Up @@ -237,7 +233,7 @@ def get_images_from_grid(

return vbse_images

def roi_from_grid(self, index: Union[Tuple, List[Tuple]]) -> RectangularROI:
def roi_from_grid(self, index: Union[Tuple, List[Tuple]]) -> hs.roi.RectangularROI:
"""Return a rectangular region of interest (ROI) on the EBSD
detector from one or multiple grid tile indices as row(s) and
column(s).
Expand Down Expand Up @@ -266,7 +262,9 @@ def roi_from_grid(self, index: Union[Tuple, List[Tuple]]) -> RectangularROI:
min_row = rows[min(index[:, 0])] * dr
max_row = (rows[max(index[:, 0])] + rows[1]) * dr

return RectangularROI(left=min_col, top=min_row, right=max_col, bottom=max_row)
return hs.roi.RectangularROI(
left=min_col, top=min_row, right=max_col, bottom=max_row
)

def plot_grid(
self,
Expand Down Expand Up @@ -313,18 +311,21 @@ def plot_grid(
color = kwargs.pop("color", "r")
for row, col in np.ndindex(*self.grid_shape):
markers.append(
Text(
x=cols[col] * dc,
y=(rows[row] + (0.1 * rows[1])) * dr,
text=f"{row,col}",
hs.plot.markers.Texts(
[cols[col] * dc, (rows[row] + (0.1 * rows[1])) * dr],
texts=f"{row,col}",
color=color,
)
)

# Set lines
kwargs.setdefault("color", "w")
markers += [HorizontalLine((i - 0.5) * dr, **kwargs) for i in rows]
markers += [VerticalLine((j - 0.5) * dc, **kwargs) for j in cols]
markers += [
hs.plot.markers.HorizontalLines((i - 0.5) * dr, **kwargs) for i in rows
]
markers += [
hs.plot.markers.VerticalLines((j - 0.5) * dc, **kwargs) for j in cols
]

# Color RGB tiles
if rgb_channels is not None:
Expand All @@ -334,12 +335,12 @@ def plot_grid(
for row, col in channels:
kwargs.update({"color": color, "zorder": 3, "linewidth": 2})
roi = self.roi_from_grid((row, col))

markers += [
Rectangle(
x1=(roi.left - 0.5) * dc,
y1=(roi.top - 0.5) * dc,
x2=(roi.right - 0.5) * dr,
y2=(roi.bottom - 0.5) * dr,
hs.plot.markers.Rectangles(
[(roi.left - 0.5) * dc, (roi.top - 0.5) * dr],
(roi.right - roi.left) * dc,
(roi.bottom - roi.top) * dr,
**kwargs,
)
]
Expand Down
10 changes: 5 additions & 5 deletions kikuchipy/io/plugins/nordif/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def file_reader(
"load by zero padding incomplete frames."
)
# Data is stored image by image
pw = [(0, ny * nx * sy * sx - data.size)]
pw = [0, ny * nx * sy * sx - data.size]
data = np.pad(data, pw)
data = data.reshape((ny, nx, sy, sx))
scan["data"] = data
Expand Down Expand Up @@ -439,8 +439,8 @@ def file_writer(filename: str, signal: Union["EBSD", "LazyEBSD"]):
"""
with open(filename, "wb") as f:
if signal._lazy:
for pattern in signal._iterate_signal():
np.array(pattern.flatten()).tofile(f)
for pattern in signal._iterate_signal("flyback"):
np.asanyarray(pattern.ravel()).tofile(f)
else:
for pattern in signal._iterate_signal():
pattern.flatten().tofile(f)
for pattern in signal._iterate_signal("flyback"):
pattern.ravel().tofile(f)
Loading

0 comments on commit efb5806

Please sign in to comment.