picometer 0.5.1__tar.gz → 0.7.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {picometer-0.5.1 → picometer-0.7.0}/PKG-INFO +34 -14
- {picometer-0.5.1 → picometer-0.7.0}/README.md +18 -8
- {picometer-0.5.1 → picometer-0.7.0}/picometer/__main__.py +14 -3
- picometer-0.7.0/picometer/atom.py +285 -0
- {picometer-0.5.1 → picometer-0.7.0}/picometer/instructions.py +93 -43
- picometer-0.7.0/picometer/loader.py +288 -0
- picometer-0.7.0/picometer/logging_.py +101 -0
- {picometer-0.5.1 → picometer-0.7.0}/picometer/process.py +11 -5
- {picometer-0.5.1 → picometer-0.7.0}/picometer/settings.py +4 -1
- {picometer-0.5.1 → picometer-0.7.0}/picometer/settings.yaml +4 -1
- picometer-0.7.0/picometer/shapes.py +269 -0
- picometer-0.7.0/picometer/utility.py +108 -0
- {picometer-0.5.1 → picometer-0.7.0}/pyproject.toml +51 -17
- picometer-0.5.1/picometer/atom.py +0 -277
- picometer-0.5.1/picometer/logging.py +0 -48
- picometer-0.5.1/picometer/shapes.py +0 -152
- picometer-0.5.1/picometer/utility.py +0 -13
- {picometer-0.5.1 → picometer-0.7.0}/LICENSE +0 -0
- {picometer-0.5.1 → picometer-0.7.0}/picometer/__init__.py +0 -0
- {picometer-0.5.1 → picometer-0.7.0}/picometer/models.py +0 -0
|
@@ -1,23 +1,33 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: picometer
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.7.0
|
|
4
4
|
Summary: Precisely define and calculate measurements across multiple crystal structures
|
|
5
|
-
License: MIT
|
|
5
|
+
License-Expression: MIT
|
|
6
6
|
License-File: LICENSE
|
|
7
|
+
Keywords: crystallography,crystal structure,CIF,geometry,structural chemistry,uncertainty
|
|
7
8
|
Author: Daniel Tchoń
|
|
8
9
|
Requires-Python: >=3.10
|
|
9
|
-
Classifier:
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Environment :: Console
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
10
14
|
Classifier: Programming Language :: Python :: 3
|
|
11
15
|
Classifier: Programming Language :: Python :: 3.10
|
|
12
16
|
Classifier: Programming Language :: Python :: 3.11
|
|
13
17
|
Classifier: Programming Language :: Python :: 3.12
|
|
14
18
|
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
-
Classifier:
|
|
16
|
-
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Chemistry
|
|
20
|
+
Classifier: Topic :: Scientific/Engineering :: Physics
|
|
21
|
+
Requires-Dist: hikari-toolkit (>=0.3)
|
|
17
22
|
Requires-Dist: numpy (>=2)
|
|
18
|
-
Requires-Dist: pandas (>=2)
|
|
23
|
+
Requires-Dist: pandas (>=2,<4)
|
|
19
24
|
Requires-Dist: pyyaml (>=5)
|
|
20
25
|
Requires-Dist: uncertainties (>=3)
|
|
26
|
+
Project-URL: Changelog, https://github.com/Baharis/picometer/blob/master/CHANGELOG.md
|
|
27
|
+
Project-URL: Documentation, https://picometer.readthedocs.io
|
|
28
|
+
Project-URL: Homepage, https://github.com/Baharis/picometer
|
|
29
|
+
Project-URL: Issues, https://github.com/Baharis/picometer/issues
|
|
30
|
+
Project-URL: Repository, https://github.com/Baharis/picometer
|
|
21
31
|
Description-Content-Type: text/markdown
|
|
22
32
|
|
|
23
33
|
# picometer
|
|
@@ -91,22 +101,31 @@ created during installation following instructions therein.
|
|
|
91
101
|
Running the program with no arguments produces the help string.
|
|
92
102
|
|
|
93
103
|
```shell
|
|
94
|
-
|
|
104
|
+
picometer
|
|
95
105
|
```
|
|
96
106
|
```text
|
|
97
|
-
usage: picometer [-h] filename
|
|
107
|
+
usage: picometer [-h] [--version] [-v] filename
|
|
98
108
|
|
|
99
109
|
Precisely define and measure across multiple crystal structures
|
|
100
110
|
|
|
101
111
|
positional arguments:
|
|
102
|
-
filename
|
|
112
|
+
filename Path to yaml file with routine settings and instructions
|
|
103
113
|
|
|
104
114
|
options:
|
|
105
|
-
-h, --help
|
|
115
|
+
-h, --help show this help message and exit
|
|
116
|
+
--version show program's version number and exit
|
|
117
|
+
-v, --verbose Print more to the terminal: -v progress, -vv picometer debug
|
|
118
|
+
and other INFO, -vvv debug of all libraries
|
|
106
119
|
|
|
107
120
|
Author: Daniel Tchoń, baharis @ GitHub
|
|
108
121
|
```
|
|
109
122
|
|
|
123
|
+
Warnings and errors are always printed to the terminal.
|
|
124
|
+
The `-v` flag can be stacked to print more:
|
|
125
|
+
`picometer -v routine.yaml` reports progress of each instruction,
|
|
126
|
+
while `-vv` and `-vvv` add debugging details.
|
|
127
|
+
A log is also saved to `picometer.log`.
|
|
128
|
+
|
|
110
129
|
Picometer inputs its settings and instructions from an input
|
|
111
130
|
[`.yaml`](https://en.wikipedia.org/wiki/YAML) file.
|
|
112
131
|
The file can contain a dictionary of `settings`,
|
|
@@ -124,8 +143,9 @@ on the example provided.
|
|
|
124
143
|
The following instructions are currently supported by picometer:
|
|
125
144
|
- **Input/output instructions**
|
|
126
145
|
- `load` model from a cif file, given `filename` or mapping syntax:
|
|
127
|
-
`{
|
|
146
|
+
`{cif: filename.cif, block: cif_block}`.
|
|
128
147
|
- `write` table with all evaluations to a csv file.
|
|
148
|
+
- `read` table with previous evaluation (to append further).
|
|
129
149
|
- **Selection instructions**
|
|
130
150
|
- `select` atoms, groups, or shapes to be used; use raw element names
|
|
131
151
|
or provide symmetry relation / recenter using mapping syntax, for example:
|
|
@@ -153,16 +173,16 @@ The following instructions are currently supported by picometer:
|
|
|
153
173
|
## Contributing
|
|
154
174
|
|
|
155
175
|
Interested in contributing? Check out the
|
|
156
|
-
[contributing guidelines](CONTRIBUTING.md).
|
|
176
|
+
[contributing guidelines](https://github.com/Baharis/picometer/blob/master/CONTRIBUTING.md).
|
|
157
177
|
Please note that this project is released with
|
|
158
|
-
a [Code of Conduct](CODE_OF_CONDUCT).
|
|
178
|
+
a [Code of Conduct](https://github.com/Baharis/picometer/blob/master/CODE_OF_CONDUCT.md).
|
|
159
179
|
By contributing to this project, you agree to abide by its terms.
|
|
160
180
|
|
|
161
181
|
|
|
162
182
|
## License
|
|
163
183
|
|
|
164
184
|
`picometer` was created by Daniel Tchoń.
|
|
165
|
-
It is licensed under the terms of the [MIT license](LICENSE).
|
|
185
|
+
It is licensed under the terms of the [MIT license](https://github.com/Baharis/picometer/blob/master/LICENSE).
|
|
166
186
|
|
|
167
187
|
|
|
168
188
|
## Credits
|
|
@@ -69,22 +69,31 @@ created during installation following instructions therein.
|
|
|
69
69
|
Running the program with no arguments produces the help string.
|
|
70
70
|
|
|
71
71
|
```shell
|
|
72
|
-
|
|
72
|
+
picometer
|
|
73
73
|
```
|
|
74
74
|
```text
|
|
75
|
-
usage: picometer [-h] filename
|
|
75
|
+
usage: picometer [-h] [--version] [-v] filename
|
|
76
76
|
|
|
77
77
|
Precisely define and measure across multiple crystal structures
|
|
78
78
|
|
|
79
79
|
positional arguments:
|
|
80
|
-
filename
|
|
80
|
+
filename Path to yaml file with routine settings and instructions
|
|
81
81
|
|
|
82
82
|
options:
|
|
83
|
-
-h, --help
|
|
83
|
+
-h, --help show this help message and exit
|
|
84
|
+
--version show program's version number and exit
|
|
85
|
+
-v, --verbose Print more to the terminal: -v progress, -vv picometer debug
|
|
86
|
+
and other INFO, -vvv debug of all libraries
|
|
84
87
|
|
|
85
88
|
Author: Daniel Tchoń, baharis @ GitHub
|
|
86
89
|
```
|
|
87
90
|
|
|
91
|
+
Warnings and errors are always printed to the terminal.
|
|
92
|
+
The `-v` flag can be stacked to print more:
|
|
93
|
+
`picometer -v routine.yaml` reports progress of each instruction,
|
|
94
|
+
while `-vv` and `-vvv` add debugging details.
|
|
95
|
+
A log is also saved to `picometer.log`.
|
|
96
|
+
|
|
88
97
|
Picometer inputs its settings and instructions from an input
|
|
89
98
|
[`.yaml`](https://en.wikipedia.org/wiki/YAML) file.
|
|
90
99
|
The file can contain a dictionary of `settings`,
|
|
@@ -102,8 +111,9 @@ on the example provided.
|
|
|
102
111
|
The following instructions are currently supported by picometer:
|
|
103
112
|
- **Input/output instructions**
|
|
104
113
|
- `load` model from a cif file, given `filename` or mapping syntax:
|
|
105
|
-
`{
|
|
114
|
+
`{cif: filename.cif, block: cif_block}`.
|
|
106
115
|
- `write` table with all evaluations to a csv file.
|
|
116
|
+
- `read` table with previous evaluation (to append further).
|
|
107
117
|
- **Selection instructions**
|
|
108
118
|
- `select` atoms, groups, or shapes to be used; use raw element names
|
|
109
119
|
or provide symmetry relation / recenter using mapping syntax, for example:
|
|
@@ -131,16 +141,16 @@ The following instructions are currently supported by picometer:
|
|
|
131
141
|
## Contributing
|
|
132
142
|
|
|
133
143
|
Interested in contributing? Check out the
|
|
134
|
-
[contributing guidelines](CONTRIBUTING.md).
|
|
144
|
+
[contributing guidelines](https://github.com/Baharis/picometer/blob/master/CONTRIBUTING.md).
|
|
135
145
|
Please note that this project is released with
|
|
136
|
-
a [Code of Conduct](CODE_OF_CONDUCT).
|
|
146
|
+
a [Code of Conduct](https://github.com/Baharis/picometer/blob/master/CODE_OF_CONDUCT.md).
|
|
137
147
|
By contributing to this project, you agree to abide by its terms.
|
|
138
148
|
|
|
139
149
|
|
|
140
150
|
## License
|
|
141
151
|
|
|
142
152
|
`picometer` was created by Daniel Tchoń.
|
|
143
|
-
It is licensed under the terms of the [MIT license](LICENSE).
|
|
153
|
+
It is licensed under the terms of the [MIT license](https://github.com/Baharis/picometer/blob/master/LICENSE).
|
|
144
154
|
|
|
145
155
|
|
|
146
156
|
## Credits
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from argparse import ArgumentParser, Namespace
|
|
2
|
+
from picometer import __version__
|
|
2
3
|
from picometer.instructions import Routine
|
|
3
|
-
from picometer.
|
|
4
|
+
from picometer.logging_ import add_file_handler, add_stream_handler, logger
|
|
4
5
|
from picometer.process import process
|
|
5
6
|
import sys
|
|
6
7
|
|
|
@@ -10,8 +11,13 @@ def parse_args() -> Namespace:
|
|
|
10
11
|
desc = 'Precisely define and measure across multiple crystal structures'
|
|
11
12
|
author = 'Author: Daniel Tchoń, baharis @ GitHub'
|
|
12
13
|
ap = ArgumentParser(prog='picometer', description=desc, epilog=author)
|
|
14
|
+
ap.add_argument('--version', action='version',
|
|
15
|
+
version=f'%(prog)s {__version__}')
|
|
13
16
|
ap.add_argument('filename', help='Path to yaml file with routine '
|
|
14
17
|
'settings and instructions')
|
|
18
|
+
ap.add_argument('-v', '--verbose', action='count', default=0,
|
|
19
|
+
help='Print more to the terminal: -v progress, -vv picometer '
|
|
20
|
+
'debug and other INFO, -vvv debug of all libraries')
|
|
15
21
|
if len(sys.argv) == 1:
|
|
16
22
|
ap.print_help(sys.stderr)
|
|
17
23
|
sys.exit(1)
|
|
@@ -22,8 +28,13 @@ def main() -> int:
|
|
|
22
28
|
args = parse_args()
|
|
23
29
|
if filename := args.filename:
|
|
24
30
|
add_file_handler('picometer.log')
|
|
25
|
-
|
|
26
|
-
|
|
31
|
+
add_stream_handler(verbosity=args.verbose)
|
|
32
|
+
try:
|
|
33
|
+
routine = Routine.from_yaml(filename)
|
|
34
|
+
process(routine)
|
|
35
|
+
except Exception:
|
|
36
|
+
logger.exception(f'Picometer failed to process {filename}')
|
|
37
|
+
return 1
|
|
27
38
|
return 0
|
|
28
39
|
|
|
29
40
|
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
from copy import deepcopy
|
|
2
|
+
from typing import Annotated, Dict, Literal, NamedTuple, Optional, Sequence
|
|
3
|
+
import logging
|
|
4
|
+
|
|
5
|
+
from hikari.dataframes import BaseFrame, CifFrame
|
|
6
|
+
from hikari.symmetry import Operation
|
|
7
|
+
from numpy.linalg import norm
|
|
8
|
+
import numpy as np
|
|
9
|
+
import numpy.typing as npt
|
|
10
|
+
import pandas as pd
|
|
11
|
+
import uncertainties.unumpy as unp
|
|
12
|
+
|
|
13
|
+
from picometer.loader import StructuralDataLoader
|
|
14
|
+
from picometer.shapes import degrees_between, Line, Plane, Shape, Vector3
|
|
15
|
+
from picometer.utility import nominal, norm, set_values, uabs, uatan2, ucross
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
UFloatArray = npt.NDArray[np.object_]
|
|
19
|
+
UFloatArray3x3 = Annotated[npt.NDArray[np.object_], Literal[3, 3]]
|
|
20
|
+
UFloatArrayNx3 = Annotated[npt.NDArray[np.object_], Literal['N', 3]]
|
|
21
|
+
UFloatArrayNx3x3 = Annotated[npt.NDArray[np.object_], Literal['N', 3, 3]]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
logger = logging.getLogger(__name__)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class Locator(NamedTuple):
|
|
29
|
+
label: str
|
|
30
|
+
symm: str = 'x,y,z'
|
|
31
|
+
at: 'Sequence[Locator]' = None
|
|
32
|
+
|
|
33
|
+
@classmethod
|
|
34
|
+
def from_dict(cls, d: dict) -> 'Locator':
|
|
35
|
+
symm = d.get('symm')
|
|
36
|
+
at = d.get('at')
|
|
37
|
+
return Locator(label=d['label'],
|
|
38
|
+
symm=symm if symm else 'x,y,z',
|
|
39
|
+
at=at if at else None)
|
|
40
|
+
|
|
41
|
+
def __bool__(self):
|
|
42
|
+
return self.label is not None
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
group_registry: Dict[str, list[Locator]] = {}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class AtomSet(Shape):
|
|
49
|
+
"""Container class w/ atoms stored in pd.Dataframe & convenience methods"""
|
|
50
|
+
|
|
51
|
+
kind = Shape.Kind.spatial
|
|
52
|
+
|
|
53
|
+
def __init__(self,
|
|
54
|
+
base: BaseFrame = None,
|
|
55
|
+
table: pd.DataFrame = None,
|
|
56
|
+
) -> None:
|
|
57
|
+
self.base = base
|
|
58
|
+
self.table = table
|
|
59
|
+
logger.debug(f'Created AtomSet with {base!r} and {len(self)} elements')
|
|
60
|
+
|
|
61
|
+
def __len__(self) -> int:
|
|
62
|
+
return len(self.table) if self.table is not None else 0
|
|
63
|
+
|
|
64
|
+
def __add__(self, other) -> 'AtomSet':
|
|
65
|
+
if not (self.base or self.table):
|
|
66
|
+
return other
|
|
67
|
+
elif not (other.base or other.table):
|
|
68
|
+
return self
|
|
69
|
+
return self.copy(table=pd.concat([self.table, other.table], axis=0))
|
|
70
|
+
|
|
71
|
+
def __getitem__(self, key: str) -> 'AtomSet':
|
|
72
|
+
"""A slice of self with subset of atoms selected with label or regex."""
|
|
73
|
+
mask = self.table.index == key
|
|
74
|
+
if not any(mask): # noqa: mask will in fact be Iterable
|
|
75
|
+
mask = self.table.index.str.match(key)
|
|
76
|
+
logger.debug(f'Selected {sum(mask)} atoms with {key=}')
|
|
77
|
+
return self.copy(table=deepcopy(self.table[mask]))
|
|
78
|
+
|
|
79
|
+
@classmethod
|
|
80
|
+
def from_paths(
|
|
81
|
+
cls,
|
|
82
|
+
cif_path: str,
|
|
83
|
+
cov_path: Optional[str],
|
|
84
|
+
block_name: str = None,
|
|
85
|
+
uncertainties_force: bool = False,
|
|
86
|
+
uncertainties_allow: bool = True,
|
|
87
|
+
) -> 'AtomSet':
|
|
88
|
+
"""Initialize from cif file using hikari's `BaseFrame` and `CifFrame`.
|
|
89
|
+
Atoms get correlated su if `cov_path` is given and `uncertainties_allow`;
|
|
90
|
+
else independent su from cif if `uncertainties_force`; else no su.
|
|
91
|
+
Forcing uncertainties that are not allowed is a contradiction."""
|
|
92
|
+
if uncertainties_force and not uncertainties_allow:
|
|
93
|
+
raise ValueError('Settings `uncertainties_force: True` and '
|
|
94
|
+
'`uncertainties_allow: False` are contradictory')
|
|
95
|
+
loader = StructuralDataLoader()
|
|
96
|
+
loader.load_cif(cif_path, block_name)
|
|
97
|
+
base = loader.get_base()
|
|
98
|
+
if cov_path is not None and uncertainties_allow:
|
|
99
|
+
loader.load_cov(cov_path)
|
|
100
|
+
return cls(base=base, table=loader.get_atoms_correlated())
|
|
101
|
+
if uncertainties_force:
|
|
102
|
+
return cls(base=base, table=loader.get_atoms_uncorrelated())
|
|
103
|
+
return cls(base=base, table=loader.get_atoms_certain())
|
|
104
|
+
|
|
105
|
+
@property
|
|
106
|
+
def fract_xyz(self) -> UFloatArrayNx3:
|
|
107
|
+
"""A 3xN numpy array with the fractional coordinates as UFloats."""
|
|
108
|
+
return np.vstack([self.table[k].to_numpy() for k in 'xyz'])
|
|
109
|
+
|
|
110
|
+
@property
|
|
111
|
+
def cart_xyz(self) -> UFloatArrayNx3:
|
|
112
|
+
return self.orthogonalise(self.fract_xyz)
|
|
113
|
+
|
|
114
|
+
@property
|
|
115
|
+
def fract_uij(self) -> UFloatArrayNx3x3:
|
|
116
|
+
"""Return a 3D array i.e. stack of 3x3 fract. displacement tensors."""
|
|
117
|
+
t = self.table
|
|
118
|
+
default = pd.Series([np.nan] * len(t), index=t.index)
|
|
119
|
+
uij = np.zeros((len(t), 3, 3), dtype=np.object_)
|
|
120
|
+
uij[:, 0, 0] = t.get('U11', default)
|
|
121
|
+
uij[:, 1, 1] = t.get('U22', default)
|
|
122
|
+
uij[:, 2, 2] = t.get('U33', default)
|
|
123
|
+
uij[:, 0, 1] = uij[:, 1, 0] = t.get('U12', default)
|
|
124
|
+
uij[:, 0, 2] = uij[:, 2, 0] = t.get('U13', default)
|
|
125
|
+
uij[:, 1, 2] = uij[:, 2, 1] = t.get('U23', default)
|
|
126
|
+
return uij
|
|
127
|
+
|
|
128
|
+
def copy(self, **kwargs) -> 'AtomSet':
|
|
129
|
+
"""A deep copy of self with some attr replaced via kwargs if needed"""
|
|
130
|
+
attr_names = ('base', 'table')
|
|
131
|
+
new = {n: kwargs.get(n, deepcopy(getattr(self, n))) for n in attr_names}
|
|
132
|
+
return self.__class__(**new)
|
|
133
|
+
|
|
134
|
+
def fractionalise(self, cart_xyz: UFloatArrayNx3) -> UFloatArrayNx3:
|
|
135
|
+
"""Multiply 3xN vector by crystallographic matrix to get fract coord"""
|
|
136
|
+
return np.linalg.inv(self.base.A_d.T) @ cart_xyz # noqa: float @ ufloat
|
|
137
|
+
|
|
138
|
+
def orthogonalise(self, fract_xyz: UFloatArrayNx3) -> UFloatArrayNx3:
|
|
139
|
+
"""Multiply 3xN vector by crystallographic matrix to get Cart. coord"""
|
|
140
|
+
return self.base.A_d.T @ fract_xyz
|
|
141
|
+
|
|
142
|
+
def locate(self, locators: Sequence[Locator]) -> 'AtomSet':
|
|
143
|
+
"""Convenience method to select multiple fragments from locators
|
|
144
|
+
while interpreting and extending groups if necessary"""
|
|
145
|
+
logger.debug(f'Locate {locators} in {self}')
|
|
146
|
+
new = AtomSet(base=self.base)
|
|
147
|
+
assert len(locators) == 0 or isinstance(locators[0], Locator)
|
|
148
|
+
for label, symm_op_code, at in locators:
|
|
149
|
+
if label in group_registry:
|
|
150
|
+
new2 = self.locate(locators=group_registry[label])
|
|
151
|
+
else:
|
|
152
|
+
new2 = self[label]
|
|
153
|
+
new2 = new2.transform(symm_op_code)
|
|
154
|
+
if at:
|
|
155
|
+
new2.origin = self.locate(at).origin
|
|
156
|
+
new += new2
|
|
157
|
+
return new
|
|
158
|
+
|
|
159
|
+
def transform(self, symm_op_code: str) -> 'AtomSet':
|
|
160
|
+
transformation = Operation.from_code(symm_op_code)
|
|
161
|
+
fract_xyz = transformation.transform(self.fract_xyz.T)
|
|
162
|
+
transformed = deepcopy(self.table)
|
|
163
|
+
transformed['x'] = fract_xyz[:, 0]
|
|
164
|
+
transformed['y'] = fract_xyz[:, 1]
|
|
165
|
+
transformed['z'] = fract_xyz[:, 2]
|
|
166
|
+
if {'U11', 'U22', 'U33', 'U12', 'U13', 'U23'}.issubset(transformed.columns):
|
|
167
|
+
uij = self.fract_uij # shape: (n_atoms, 3, 3)
|
|
168
|
+
isnan = np.vectorize(lambda x: isinstance(x, float) and np.isnan(x))
|
|
169
|
+
mask = ~isnan(uij).all(axis=(1, 2))
|
|
170
|
+
if np.any(mask):
|
|
171
|
+
abc_r = [self.base.a_r, self.base.b_r, self.base.c_r]
|
|
172
|
+
k = np.diag([1 / a for a in abc_r]) @ transformation.tf @ np.diag(abc_r)
|
|
173
|
+
uij_rot = k @ uij[mask] @ k.T
|
|
174
|
+
# `set_values` instead of `.loc`: float64 Uij columns may get UFloats
|
|
175
|
+
set_values(transformed, mask, 'U11', uij_rot[:, 0, 0])
|
|
176
|
+
set_values(transformed, mask, 'U22', uij_rot[:, 1, 1])
|
|
177
|
+
set_values(transformed, mask, 'U33', uij_rot[:, 2, 2])
|
|
178
|
+
set_values(transformed, mask, 'U12', uij_rot[:, 0, 1])
|
|
179
|
+
set_values(transformed, mask, 'U13', uij_rot[:, 0, 2])
|
|
180
|
+
set_values(transformed, mask, 'U23', uij_rot[:, 1, 2])
|
|
181
|
+
return self.copy(table=transformed)
|
|
182
|
+
|
|
183
|
+
@property
|
|
184
|
+
def u_cartesian(self) -> UFloatArrayNx3x3:
|
|
185
|
+
"""Nx3x3 stack of displacement tensors in the Cartesian frame.
|
|
186
|
+
Atoms without full anisotropic Uij yield NaN-filled tensors."""
|
|
187
|
+
u_fract = self.fract_uij # Nx3x3 stack of abc-normalized U_cif tensors
|
|
188
|
+
a_mat = self.base.A_d.T # fractional to orthogonal cartesian metric
|
|
189
|
+
n_mat = np.diag([self.base.a_r, self.base.b_r, self.base.c_r])
|
|
190
|
+
u_star = (n_mat @ u_fract) @ n_mat # eq. 4b @ S0021889802008580
|
|
191
|
+
return (a_mat @ u_star) @ a_mat.T # eq. 3a @ S0021889802008580
|
|
192
|
+
|
|
193
|
+
@property
|
|
194
|
+
def u_equivalent(self) -> UFloatArray:
|
|
195
|
+
"""Ueq = 1/3 trace(U_cart) for every atom (NaN if Uij are missing).
|
|
196
|
+
Note that mean(U11, U22, U33) of the CIF-convention tensor is equal
|
|
197
|
+
to Ueq only for orthogonal unit cells, so the trace must be taken
|
|
198
|
+
from the Cartesian tensor (Fischer & Tillmanns, 1988, eq. 1)."""
|
|
199
|
+
u_cart = self.u_cartesian
|
|
200
|
+
return (u_cart[:, 0, 0] + u_cart[:, 1, 1] + u_cart[:, 2, 2]) / 3
|
|
201
|
+
|
|
202
|
+
@property
|
|
203
|
+
def u_cartesian_eigenvalues(self):
|
|
204
|
+
u_columns = ['U11', 'U12', 'U13', 'U22', 'U23', 'U33']
|
|
205
|
+
eigenvalues = np.full((len(self), 3), np.nan)
|
|
206
|
+
if not set(u_columns).issubset(self.table.keys()):
|
|
207
|
+
return eigenvalues
|
|
208
|
+
u_cart = self.u_cartesian
|
|
209
|
+
mask = ~self.table[u_columns].isna().any(axis=1)
|
|
210
|
+
eigenvalues[mask, :] = np.linalg.eigh(unp.nominal_values(u_cart[mask, :, :]))[0]
|
|
211
|
+
return eigenvalues
|
|
212
|
+
|
|
213
|
+
@property
|
|
214
|
+
def centroid(self) -> np.ndarray:
|
|
215
|
+
"""A 3-vector with average atom position."""
|
|
216
|
+
return self.cart_xyz.T.mean(axis=0)
|
|
217
|
+
|
|
218
|
+
@property
|
|
219
|
+
def direction(self) -> Vector3:
|
|
220
|
+
return None
|
|
221
|
+
|
|
222
|
+
@property
|
|
223
|
+
def line(self) -> Line:
|
|
224
|
+
"""Line that best fits the Cartesian coordinates of atoms, with
|
|
225
|
+
uncertainty of its direction propagated from atoms' coordinates"""
|
|
226
|
+
return Line.from_points(self.cart_xyz.T)
|
|
227
|
+
|
|
228
|
+
@property
|
|
229
|
+
def plane(self) -> Plane:
|
|
230
|
+
"""Plane that best fits the Cartesian coordinates of atoms, with
|
|
231
|
+
uncertainty of its normal propagated from atoms' coordinates"""
|
|
232
|
+
return Plane.from_points(self.cart_xyz.T)
|
|
233
|
+
|
|
234
|
+
@property
|
|
235
|
+
def origin(self) -> Vector3:
|
|
236
|
+
return self.centroid
|
|
237
|
+
|
|
238
|
+
@origin.setter
|
|
239
|
+
def origin(self, new_origin) -> None:
|
|
240
|
+
"""Change origin to the new one provided in cartesian coordinates"""
|
|
241
|
+
new_origin_fract = self.fractionalise(new_origin)
|
|
242
|
+
delta = new_origin_fract - self.fractionalise(self.centroid)
|
|
243
|
+
self.table['x'] += delta[0]
|
|
244
|
+
self.table['y'] += delta[1]
|
|
245
|
+
self.table['z'] += delta[2]
|
|
246
|
+
assert np.allclose(unp.nominal_values(new_origin),
|
|
247
|
+
unp.nominal_values(self.centroid))
|
|
248
|
+
|
|
249
|
+
def _angle(self, *others: 'Shape') -> float:
|
|
250
|
+
assert all(o.kind is o.Kind.spatial for o in [self, *others])
|
|
251
|
+
combined = sum(others, self)
|
|
252
|
+
xyz = combined.cart_xyz.T
|
|
253
|
+
assert len(combined) == 3, 'Input AtomSet must contain exactly 3 atoms'
|
|
254
|
+
return degrees_between(xyz[0] - xyz[1], xyz[2] - xyz[1])
|
|
255
|
+
|
|
256
|
+
def _distance(self, other: 'Shape') -> float:
|
|
257
|
+
if other.kind is self.Kind.spatial:
|
|
258
|
+
# https://stackoverflow.com/a/43359192/8279065 bloody brilliant
|
|
259
|
+
other: 'AtomSet'
|
|
260
|
+
xy1, xy2 = self.cart_xyz.T, other.cart_xyz.T
|
|
261
|
+
p = np.add.outer(np.sum(xy1**2, axis=1), np.sum(xy2**2, axis=1))
|
|
262
|
+
n = np.dot(xy1, xy2.T)
|
|
263
|
+
return min(np.ravel(unp.sqrt(p - 2 * n)), key=nominal)
|
|
264
|
+
elif other.kind is self.Kind.planar:
|
|
265
|
+
deltas = self.cart_xyz.T - other.origin
|
|
266
|
+
return min((uabs(d) for d in np.dot(deltas, other.direction)), key=nominal)
|
|
267
|
+
else: # if other.kind is self.Kind.axial:
|
|
268
|
+
deltas = self.cart_xyz.T - other.origin
|
|
269
|
+
return min((norm(ucross(delta, other.direction))
|
|
270
|
+
for delta in deltas), key=nominal)
|
|
271
|
+
|
|
272
|
+
def dihedral(self, *others: 'AtomSet') -> float:
|
|
273
|
+
"""Torsion angle in degrees in (-180, 180], IUPAC sign convention.
|
|
274
|
+
Computed as ``atan2(|b2| b1.(b2 x b3), (b1 x b2).(b2 x b3))``, which
|
|
275
|
+
keeps the sign and value well-defined near 0 and 180 degrees,
|
|
276
|
+
where the former arccos-based formula was numerically unstable."""
|
|
277
|
+
assert all(o.kind is o.Kind.spatial for o in [self, *others])
|
|
278
|
+
combined = sum(others, self)
|
|
279
|
+
xyz = combined.cart_xyz.T
|
|
280
|
+
assert len(combined) == 4, 'Input AtomSet must contain exactly 4 atoms'
|
|
281
|
+
b1, b2, b3 = xyz[1] - xyz[0], xyz[2] - xyz[1], xyz[3] - xyz[2]
|
|
282
|
+
n1, n2 = ucross(b1, b2), ucross(b2, b3)
|
|
283
|
+
y = norm(b2) * sum(b1 * n2)
|
|
284
|
+
x = sum(n1 * n2)
|
|
285
|
+
return uatan2(y, x) * (180 / np.pi)
|