Skip to content

Commit 9e753be

Browse files
crystal: Missing basis, lattice now indicated by None; Checked in __post__init__ for nan floats
1 parent d8a2427 commit 9e753be

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

tests/t_labels.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,4 @@ def make_example_label() -> PowderExperiment:
4949
return PowderExperiment(phases=[crystal_structure], xray_info=xray_info, crystallite_size_nm=10, temp_K=300)
5050

5151
if __name__ == "__main__":
52-
TestPowderExperiment.execute_all()
53-
# TestTensorization.execute_all()
52+
TestTensorization.execute_all()

xrdpattern/crystal/components/crystal.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import copy
34
import json
45
import logging
56
from dataclasses import dataclass, asdict
@@ -19,8 +20,8 @@
1920

2021
@dataclass
2122
class CrystalStructure(JsonDataclass):
22-
lattice : Lattice
23-
basis : CrystalBasis
23+
lattice : Optional[Lattice]
24+
basis : Optional[CrystalBasis]
2425
spacegroup : Optional[int] = None
2526
chemical_composition : Optional[str] = None
2627
wyckoff_symbols : Optional[list[str]] = None
@@ -30,9 +31,22 @@ def __post_init__(self):
3031
if not self.phase_fraction is None:
3132
if not 0 <= self.phase_fraction <= 1:
3233
raise ValueError(f'Phase fraction must be between 0 and 1. Got {self.phase_fraction}')
33-
if not len(self.basis) == 0:
34+
if not self.lattice is None:
35+
for p in self.lattice.parameters:
36+
if p != p:
37+
raise ValueError(f'Lattice parameters {self.lattice.parameters} contain nan values')
38+
if self.phase_fraction != self.phase_fraction:
39+
raise ValueError(f'Phase fraction {self.phase_fraction} is nan')
40+
41+
if not self.basis is None:
3442
self.calculate_properties()
3543

44+
45+
46+
@classmethod
47+
def make_empty(cls):
48+
return CrystalStructure(lattice=None, basis=None)
49+
3650
@classmethod
3751
def from_cif(cls, cif_content : str) -> CrystalStructure:
3852
pymatgen_structure = Structure.from_str(cif_content, fmt='cif')
@@ -85,7 +99,7 @@ def get_view(self) -> str:
8599
# properties
86100

87101
def calculate_properties(self):
88-
if len(self.basis) == 0:
102+
if self.basis is None:
89103
raise ValueError('Base is empty! Cannot calculate properties of empty crystal. Aborting ...')
90104

91105
pymatgen_structure = self.to_pymatgen()
@@ -97,10 +111,14 @@ def calculate_properties(self):
97111
self.chemical_composition = pymatgen_structure.composition.formula
98112

99113
def get_standardized(self) -> CrystalStructure:
100-
struct = self.to_pymatgen() if len(self.basis) > 0 else Structure(self.lattice, ["H"], [[0, 0, 0]])
114+
if self.lattice is None:
115+
return copy.deepcopy(self)
116+
117+
struct = self.to_pymatgen() if not self.basis is None else Structure(self.lattice, ["H"], [[0, 0, 0]])
101118
analzyer = SpacegroupAnalyzer(structure=struct)
102119
std_struct = analzyer.get_conventional_standard_structure()
103-
if len(self.basis) == 0:
120+
121+
if self.basis is None:
104122
return CrystalStructure(lattice=std_struct.lattice, basis=CrystalBasis.empty())
105123
else:
106124
return CrystalStructure.from_pymatgen(pymatgen_structure=std_struct)

0 commit comments

Comments
 (0)