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

Fix phase list when initializing CrystalMap with a sparse phase list #433

Merged
merged 6 commits into from
Mar 14, 2023
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
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ All user facing changes to this project are documented in this file. The format
on `Keep a Changelog <https://keepachangelog.com/en/1.0.0/>`__, and this project tries
its best to adhere to `Semantic Versioning <https://semver.org/spec/v2.0.0.html>`__.

2023-03-14 - version 0.11.1
===========================

Fixed
-----
- Initialization of a crystal map with a phase list with fewer phases than in the phase
ID array given returns a map with a new phase list with correct phase IDs.

2023-02-09 - version 0.11.0
===========================

Expand Down
2 changes: 1 addition & 1 deletion orix/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__name__ = "orix"
__version__ = "0.11.0"
__version__ = "0.11.1"
__author__ = "orix developers"
__author_email__ = "pyxem.team@gmail.com"
__description__ = "orix is an open-source Python library for handling crystal orientation mapping data."
Expand Down
25 changes: 15 additions & 10 deletions orix/crystal_map/crystal_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import numpy as np

from orix.crystal_map.crystal_map_properties import CrystalMapProperties
from orix.crystal_map.phase_list import Phase, PhaseList
from orix.crystal_map.phase_list import ALL_COLORS, Phase, PhaseList
from orix.quaternion import Orientation, Rotation


Expand Down Expand Up @@ -251,7 +251,7 @@ def __init__(
if phase_list is None:
self._phases = PhaseList(ids=unique_phase_ids)
else:
phase_list = copy.deepcopy(phase_list)
phase_list = phase_list.deepcopy()
phase_ids = phase_list.ids
n_different = len(phase_ids) - len(unique_phase_ids)
if n_different > 0:
Expand All @@ -265,15 +265,20 @@ def __init__(
break
elif n_different < 0:
# Create new phase list adding the missing phases with
# default initial values
phase_list = PhaseList(
names=phase_list.names,
space_groups=phase_list.space_groups,
point_groups=phase_list.point_groups,
colors=phase_list.colors,
structures=phase_list.structures,
ids=unique_phase_ids,
# default initial values (but unique colors)
phase_dict = {}
all_colors = list(ALL_COLORS.keys())
all_unique_colors = np.delete(
all_colors, np.isin(all_colors, phase_list.colors)
)
ci = 0
for i in unique_phase_ids:
if i in phase_ids:
phase_dict[i] = phase_list[i]
else:
phase_dict[i] = Phase(color=all_unique_colors[ci])
ci += 1
phase_list = PhaseList(phase_dict)
# Ensure phase list IDs correspond to IDs in phase_id array
new_ids = list(unique_phase_ids.astype(int))
phase_list._dict = dict(zip(new_ids, phase_list._dict.values()))
Expand Down
25 changes: 25 additions & 0 deletions orix/tests/test_crystal_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,31 @@ def test_init_with_phase_list(self, crystal_map_input, expected_presence):
unique_phase_ids = list(np.unique(crystal_map_input["phase_id"]).astype(int))
assert xmap.phases.ids == unique_phase_ids

def test_init_with_sparse_phase_list(self):
"""Initialize a map with a sparse phase list only containing one
of the phase IDs passed in the phase list.
"""
pl = PhaseList(ids=[1], names=["b"], space_groups=[225])
phase_id = np.array([0, 0, 1, 1, 1, 3])
xmap = CrystalMap(
rotations=Rotation.identity(phase_id.size),
is_in_data=phase_id == 1,
phase_list=pl,
phase_id=phase_id,
)
for i, name, color, sg_name in zip(
[0, 1, 3],
["", "b", ""],
["tab:orange", "tab:blue", "tab:green"],
[None, "Fm-3m", None],
):
assert xmap.phases[i].name == name
assert xmap.phases[i].color == color
if sg_name is None:
assert xmap.phases[i].space_group is None
else:
assert xmap.phases[i].space_group.short_name == sg_name

def test_init_with_single_point_group(self, crystal_map_input):
point_group = O
phase_list = PhaseList(point_groups=point_group)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"sphinx-design",
"sphinx-gallery < 0.11",
"sphinx-last-updated-by-git",
"pydata-sphinx-theme",
"pydata-sphinx-theme >= 0.13.1",
"sphinxcontrib-bibtex >= 1.0",
"scikit-image",
"scikit-learn",
Expand Down