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

add to_dict method to crystal_map to allow for easy extraction of tho… #457

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
37 changes: 37 additions & 0 deletions orix/crystal_map/crystal_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,43 @@ def __repr__(self) -> str:

return representation

def to_dict(self) -> dict[dict[str,str]]:
"""Return dictionary of the data."""
result = {}
if self.size == 0:
return result

phases = self.phases_in_data
phase_ids = self.phase_id

# Ensure attributes set to None are treated OK
names = ["None" if not name else name for name in phases.names]
sg_names = ["None" if not i else i.short_name for i in phases.space_groups]
pg_names = ["None" if not i else i.name for i in phases.point_groups]
ppg_names = [
"None" if not i else i.proper_subgroup.name for i in phases.point_groups
]

# Determine dictionary of phases
unique_phases = np.unique(phase_ids)
for i, phase_id in enumerate(unique_phases.astype(int)):
p_size = np.where(phase_ids == phase_id)[0].size
p_percentage = 100 * p_size / self.size
result[phase_id] = {
"number pixel" : p_size,
"percentage" : p_percentage,
"name" : names[i],
"space group" : sg_names[i],
"point group" : pg_names[i],
"proper point group": ppg_names[i],
"color" : phases.colors[i]
}

# Properties and spatial coordinates and scan unit
result["properties"] = list(self.prop.keys())
result["scan unit"] = self.scan_unit
return result

def deepcopy(self) -> "CrystalMap":
"""Return a deep copy using :func:`copy.deepcopy` function."""
return copy.deepcopy(self)
Expand Down