Skip to content

Commit 8fea584

Browse files
committed
first start
1 parent 4b2e367 commit 8fea584

File tree

5 files changed

+203
-0
lines changed

5 files changed

+203
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ docs/_build
4141
# VSCode
4242
.vscode
4343

44+
# Large files
45+
*.hdf
46+
*normalized.tiff
47+
*sample.tiff
48+
*open_beam.tiff
49+
4450
# Misc
4551
..*
4652
*.log

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ requires-python = ">=3.9,<3.12"
3131
dependencies = [
3232
"easyscience>=0.6.0",
3333
"scipp>=23.12.0",
34+
"easycrystallography",
3435
]
3536

3637
[project.optional-dependencies]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import numbers
2+
from typing import List
3+
from typing import Tuple
4+
from typing import Union
5+
6+
from easycrystallography.Structures.Phase import Phase
7+
from easycrystallography.Structures.Phase import Phases
8+
9+
10+
class CrystalAlloy(Phases):
11+
12+
def __init__(self, name: str, phases: list[Phase], fractions: list[numbers.Number]):
13+
super().__init__(name=name)
14+
15+
if not isinstance(phases, list[Phase]):
16+
raise TypeError(f"phases should be a list of Phase objects, got {type(phases)}")
17+
if not isinstance(fractions, list[numbers.Number]):
18+
raise TypeError(f"fractions should be a list of numbers, got {type(fractions)}")
19+
if len(phases) != len(fractions):
20+
raise ValueError(f"phases and fractions should have the same length, got {len(phases)} and {len(fractions)}")
21+
if sum(fractions) > 1:
22+
raise ValueError(f"fractions should sum to 1 or less, got {sum(fractions)}")
23+
if any(fraction < 0 for fraction in fractions):
24+
raise ValueError(f"fractions should be non-negative, got {fractions}")
25+
26+
self._phases = phases
27+
self._fractions = fractions
28+
29+
def __getitem__(self, idx) -> List[Tuple[Phase, numbers.Number]]:
30+
if isinstance(idx, int):
31+
return self._phases[idx], self._fractions[idx]
32+
elif isinstance(idx, slice):
33+
return [(self._phases[i], self._fractions[i]) for i in range(len(self._phases))][idx]
34+
else:
35+
raise TypeError(f"Invalid index type: {type(idx)}. Expected int or slice.")
36+
37+
def __setitem__(self, idx, value) -> None:
38+
39+
def __len__(self) -> int:
40+
return len(self._phases)
41+
42+
def __delitem__(self, idx) -> None:
43+
if isinstance(idx, int):
44+
del self._phases[idx]
45+
del self._fractions[idx]
46+
elif isinstance(idx, slice):
47+
del self._phases[idx]
48+
del self._fractions[idx]
49+
else:
50+
raise TypeError(f"Invalid index type: {type(idx)}. Expected int or slice.")
51+
52+
@property
53+
def phases(self) -> List[Tuple[Phase, numbers.Number]]:
54+
"""Get the phases and their corresponding fractions."""
55+
return [(phase, fraction) for phase, fraction in zip(self._phases, self._fractions)]
56+
57+
@phases.setter
58+
def phases(self):
59+
raise AttributeError("phases is read-only, use set_phases() to overwrite it")
60+

src/easyimaging/project/sample_model/crystal_structure/phase.py

Whitespace-only changes.
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 7,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import easycrystallography as ec\n",
10+
"from easycrystallography.Structures.Phase import Phase"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 8,
16+
"metadata": {},
17+
"outputs": [],
18+
"source": [
19+
"phase = Phase(name='Iron')"
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": 9,
25+
"metadata": {},
26+
"outputs": [],
27+
"source": [
28+
"phase.space_group.name_hm_alt = 'P4/mmm'"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 10,
34+
"metadata": {},
35+
"outputs": [],
36+
"source": [
37+
"phase.cell.length_a = 2.866"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": 22,
43+
"metadata": {},
44+
"outputs": [],
45+
"source": [
46+
"phase.atoms.append(label='Fe', type_symbol='Fe', fract_x=0.0, fract_y=0.0, fract_z=0.0)"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": 23,
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
55+
"phase.add_atom(label='Fe', type_symbol='Fe', fract_x=0.5, fract_y=0.5, fract_z=0.5)"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": 17,
61+
"metadata": {},
62+
"outputs": [
63+
{
64+
"data": {
65+
"text/plain": [
66+
"Atom Fe (Fe) @ (0.5, 0.5, 0.5)"
67+
]
68+
},
69+
"execution_count": 17,
70+
"metadata": {},
71+
"output_type": "execute_result"
72+
}
73+
],
74+
"source": [
75+
"phase.atoms[1]"
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": 27,
81+
"metadata": {},
82+
"outputs": [],
83+
"source": [
84+
"phase.remove_atom('Fe')"
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": 28,
90+
"metadata": {},
91+
"outputs": [
92+
{
93+
"data": {
94+
"text/plain": [
95+
"Collection of 0 sites."
96+
]
97+
},
98+
"execution_count": 28,
99+
"metadata": {},
100+
"output_type": "execute_result"
101+
}
102+
],
103+
"source": [
104+
"phase.atoms"
105+
]
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": null,
110+
"metadata": {},
111+
"outputs": [],
112+
"source": []
113+
}
114+
],
115+
"metadata": {
116+
"kernelspec": {
117+
"display_name": "easyimaging",
118+
"language": "python",
119+
"name": "python3"
120+
},
121+
"language_info": {
122+
"codemirror_mode": {
123+
"name": "ipython",
124+
"version": 3
125+
},
126+
"file_extension": ".py",
127+
"mimetype": "text/x-python",
128+
"name": "python",
129+
"nbconvert_exporter": "python",
130+
"pygments_lexer": "ipython3",
131+
"version": "3.12.9"
132+
}
133+
},
134+
"nbformat": 4,
135+
"nbformat_minor": 2
136+
}

0 commit comments

Comments
 (0)