Skip to content
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

Adding a class for images #133

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
3 changes: 2 additions & 1 deletion defdap/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 Mechanics of Microstructures Group
# Copyright 2024 Mechanics of Microstructures Group
# at The University of Manchester
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -921,6 +921,7 @@ def grain_data(self, map_data):

return grain_data


def grain_map_data(self, map_data=None, grain_data=None, bg=np.nan):
"""Extract a single grain map from the given map data.

Expand Down
2 changes: 1 addition & 1 deletion defdap/crystal.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 Mechanics of Microstructures Group
# Copyright 2024 Mechanics of Microstructures Group
# at The University of Manchester
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
41 changes: 27 additions & 14 deletions defdap/ebsd.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 Mechanics of Microstructures Group
# Copyright 2024 Mechanics of Microstructures Group
# at The University of Manchester
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -242,21 +242,34 @@ def scale(self):
return self.step_size

@report_progress("rotating EBSD data")
def rotate_data(self):
"""Rotate map by 180 degrees and transform quats accordingly.
def rotate_data(self, angle=180):
"""Rotate map counter-clockwise by the specified angle (90, 180, 270 degrees)
and transform quats accordingly.

"""

self.data.euler_angle = self.data.euler_angle[:, ::-1, ::-1]
self.data.band_contrast = self.data.band_contrast[::-1, ::-1]
self.data.band_slope = self.data.band_slope[::-1, ::-1]
self.data.phase = self.data.phase[::-1, ::-1]
self.calc_quat_array()

# Rotation from old coord system to new
transform_quat = Quat.from_axis_angle(np.array([0, 0, 1]), np.pi).conjugate
Parameters
----------
angle : int
The angle to rotate the map. Must be one of [90, 180, 270].

# Perform vectorised multiplication
"""
if angle not in [90, 180, 270]:
raise ValueError("Angle must be one of [90, 180, 270]")

# Rotate the data arrays by the specified angle
k = angle // 90 # Number of 90 degree rotations

# Change the shape of the EBSD data to match
if k % 2 == 1:
self.shape = self.shape[::-1]

self.data.euler_angle = np.rot90(self.data.euler_angle, k=k, axes=(1, 2))
self.data.band_contrast = np.rot90(self.data.band_contrast, k=k)
self.data.mean_angular_deviation = np.rot90(self.data.mean_angular_deviation, k=k)
self.data.band_slope = np.rot90(self.data.band_slope, k=k)
self.data.phase = np.rot90(self.data.phase, k=k)

# Rotation quaterions from old coord system to new
transform_quat = Quat.from_axis_angle(np.array([0, 0, 1]), np.deg2rad(-angle)).conjugate
quats = Quat.multiply_many_quats(self.data.orientation.flatten(), transform_quat)
self.data.orientation = np.array(quats).reshape(self.shape)

Expand Down
22 changes: 20 additions & 2 deletions defdap/file_readers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 Mechanics of Microstructures Group
# Copyright 2024 Mechanics of Microstructures Group
# at The University of Manchester
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -19,6 +19,7 @@
from abc import ABC, abstractmethod
import pathlib
import re
from skimage.io import imread

from typing import TextIO, Dict, List, Callable, Any, Type, Optional

Expand Down Expand Up @@ -205,7 +206,6 @@ def parse_phase() -> Phase:
'cmap': 'gray',
'clabel': 'Band contrast',
}

)
self.loaded_data.add(
'band_slope', data['BS'].reshape(shape),
Expand Down Expand Up @@ -791,13 +791,31 @@ def load(self, file_name: pathlib.Path) -> None:
self.check_data()


def load_image(file_name: pathlib.Path) -> Datastore:
image = imread(file_name, as_gray=True)
loaded_metadata = {
'shape': image.shape,
}
laoded_data = Datastore()
laoded_data.add(
'image', image, unit='', type='map', order=0,
plot_params={
'plot_colour_bar': False,
'cmap': 'gray',
}
)
return loaded_metadata, laoded_data


def read_until_string(
file: TextIO,
term_string: str,
comment_char: str = '*',
line_process: Optional[Callable[[str], Any]] = None,
exact: bool = False
) -> List[Any]:


"""Read lines in a file until a line starting with the `termString`
is encountered. The file position is returned before the line starting
with the `termString` when found. Comment and empty lines are ignored.
Expand Down
2 changes: 1 addition & 1 deletion defdap/file_writers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 Mechanics of Microstructures Group
# Copyright 2024 Mechanics of Microstructures Group
# at The University of Manchester
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down
Loading
Loading