ChessAnalysisPipeline 0.0.17.dev3__py3-none-any.whl

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.
Files changed (70) hide show
  1. CHAP/TaskManager.py +216 -0
  2. CHAP/__init__.py +27 -0
  3. CHAP/common/__init__.py +57 -0
  4. CHAP/common/models/__init__.py +8 -0
  5. CHAP/common/models/common.py +124 -0
  6. CHAP/common/models/integration.py +659 -0
  7. CHAP/common/models/map.py +1291 -0
  8. CHAP/common/processor.py +2869 -0
  9. CHAP/common/reader.py +658 -0
  10. CHAP/common/utils.py +110 -0
  11. CHAP/common/writer.py +730 -0
  12. CHAP/edd/__init__.py +23 -0
  13. CHAP/edd/models.py +876 -0
  14. CHAP/edd/processor.py +3069 -0
  15. CHAP/edd/reader.py +1023 -0
  16. CHAP/edd/select_material_params_gui.py +348 -0
  17. CHAP/edd/utils.py +1572 -0
  18. CHAP/edd/writer.py +26 -0
  19. CHAP/foxden/__init__.py +19 -0
  20. CHAP/foxden/models.py +71 -0
  21. CHAP/foxden/processor.py +124 -0
  22. CHAP/foxden/reader.py +224 -0
  23. CHAP/foxden/utils.py +80 -0
  24. CHAP/foxden/writer.py +168 -0
  25. CHAP/giwaxs/__init__.py +11 -0
  26. CHAP/giwaxs/models.py +491 -0
  27. CHAP/giwaxs/processor.py +776 -0
  28. CHAP/giwaxs/reader.py +8 -0
  29. CHAP/giwaxs/writer.py +8 -0
  30. CHAP/inference/__init__.py +7 -0
  31. CHAP/inference/processor.py +69 -0
  32. CHAP/inference/reader.py +8 -0
  33. CHAP/inference/writer.py +8 -0
  34. CHAP/models.py +227 -0
  35. CHAP/pipeline.py +479 -0
  36. CHAP/processor.py +125 -0
  37. CHAP/reader.py +124 -0
  38. CHAP/runner.py +277 -0
  39. CHAP/saxswaxs/__init__.py +7 -0
  40. CHAP/saxswaxs/processor.py +8 -0
  41. CHAP/saxswaxs/reader.py +8 -0
  42. CHAP/saxswaxs/writer.py +8 -0
  43. CHAP/server.py +125 -0
  44. CHAP/sin2psi/__init__.py +7 -0
  45. CHAP/sin2psi/processor.py +8 -0
  46. CHAP/sin2psi/reader.py +8 -0
  47. CHAP/sin2psi/writer.py +8 -0
  48. CHAP/tomo/__init__.py +15 -0
  49. CHAP/tomo/models.py +210 -0
  50. CHAP/tomo/processor.py +3862 -0
  51. CHAP/tomo/reader.py +9 -0
  52. CHAP/tomo/writer.py +59 -0
  53. CHAP/utils/__init__.py +6 -0
  54. CHAP/utils/converters.py +188 -0
  55. CHAP/utils/fit.py +2947 -0
  56. CHAP/utils/general.py +2655 -0
  57. CHAP/utils/material.py +274 -0
  58. CHAP/utils/models.py +595 -0
  59. CHAP/utils/parfile.py +224 -0
  60. CHAP/writer.py +122 -0
  61. MLaaS/__init__.py +0 -0
  62. MLaaS/ktrain.py +205 -0
  63. MLaaS/mnist_img.py +83 -0
  64. MLaaS/tfaas_client.py +371 -0
  65. chessanalysispipeline-0.0.17.dev3.dist-info/LICENSE +60 -0
  66. chessanalysispipeline-0.0.17.dev3.dist-info/METADATA +29 -0
  67. chessanalysispipeline-0.0.17.dev3.dist-info/RECORD +70 -0
  68. chessanalysispipeline-0.0.17.dev3.dist-info/WHEEL +5 -0
  69. chessanalysispipeline-0.0.17.dev3.dist-info/entry_points.txt +2 -0
  70. chessanalysispipeline-0.0.17.dev3.dist-info/top_level.txt +2 -0
CHAP/utils/material.py ADDED
@@ -0,0 +1,274 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ File : general.py
5
+ Author : Rolf Verberg <rolfverberg AT gmail dot com>
6
+ Description: Module defining the Material class
7
+ """
8
+
9
+ # System modules
10
+ from logging import getLogger
11
+ from os import path
12
+
13
+ # Third party modules
14
+ import numpy as np
15
+ try:
16
+ from xrayutilities import materials
17
+ from xrayutilities import simpack
18
+ HAVE_XU = True
19
+ except ImportError:
20
+ HAVE_XU = False
21
+ try:
22
+ from hexrd import material
23
+ HAVE_HEXRD = True
24
+ except ImportError:
25
+ HAVE_HEXRD = False
26
+ if HAVE_HEXRD:
27
+ try:
28
+ from hexrd.valunits import valWUnit
29
+ except ImportError:
30
+ HAVE_HEXRD = False
31
+ #from xrayutilities import materials
32
+ #from xrayutilities import simpack
33
+ #HAVE_XU = True
34
+ #HAVE_HEXRD = False
35
+
36
+ POEDER_INTENSITY_CUTOFF = 1.e-8
37
+
38
+ logger = getLogger(__name__)
39
+
40
+
41
+ class Material:
42
+ """Base class for materials in an sin2psi or EDD analysis. Right
43
+ now it assumes a single material, extend its ability to do
44
+ differently when test data is available.
45
+ """
46
+ def __init__(
47
+ self, material_name=None, material_file=None, sgnum=None,
48
+ lattice_parameters_angstroms=None, atoms=None, pos=None,
49
+ enrgy=None):
50
+ """Initialize Material."""
51
+ self._enrgy = enrgy
52
+ self._materials = []
53
+ self._ds_min = []
54
+ self._ds_unique = None
55
+ self._hkls_unique = None
56
+ if material_name is not None:
57
+ self.add_material(
58
+ material_name, material_file, sgnum,
59
+ lattice_parameters_angstroms, atoms, pos)
60
+
61
+ def lattice_parameters(self, index=0):
62
+ """Convert from internal nm units to angstrom."""
63
+ matl = self._materials[index]
64
+ if isinstance(matl, materials.material.Crystal):
65
+ return [matl.a, matl.b, matl.c]
66
+ if isinstance(matl, material.Material):
67
+ return [
68
+ lpars.getVal('angstrom')
69
+ for lpars in self._materials[index].latticeParameters[0:3]]
70
+ raise ValueError('Illegal material class type')
71
+
72
+ def ds_unique(self, tth_tol=None, tth_max=None, round_sig=8):
73
+ """Return the unique lattice spacings."""
74
+ if self._ds_unique is None:
75
+ self.get_ds_unique(tth_tol, tth_max, round_sig)
76
+ return self._ds_unique
77
+
78
+ def hkls_unique(self, tth_tol=None, tth_max=None, round_sig=8):
79
+ """Return the unique HKLs."""
80
+ if self._hkls_unique is None:
81
+ self.get_ds_unique(tth_tol, tth_max, round_sig)
82
+ return self._hkls_unique
83
+
84
+ def add_material(
85
+ self, material_name, material_file=None, sgnum=None,
86
+ lattice_parameters_angstroms=None, atoms=None, pos=None,
87
+ dmin_angstroms=0.6):
88
+ """Add a material."""
89
+ # At this point only for a single material
90
+ # Unique energies works for more, but fitting with different
91
+ # materials is not implemented
92
+ if len(self._materials) == 1:
93
+ raise ValueError('Multiple materials not implemented yet')
94
+ self._ds_min.append(dmin_angstroms)
95
+ self._materials.append(
96
+ Material.make_material(
97
+ material_name, material_file, sgnum,
98
+ lattice_parameters_angstroms, atoms, pos, dmin_angstroms))
99
+
100
+ def get_ds_unique(self, tth_tol=None, tth_max=None, round_sig=8):
101
+ """Get the list of unique lattice spacings from material HKLs.
102
+
103
+ :param tth_tol: Two-theta tolerance (in degrees).
104
+ :type tth_tol: float, optional
105
+ :param tth_max: Maximum two-theta value (in degrees).
106
+ :type tth_max: float, optional
107
+ :param round_sig: Significant digits, passed to round()
108
+ function, defaults to `8`.
109
+ :type round_sig: int, optional
110
+ :returns: The list of hkl's corresponding to the unique lattice
111
+ spacings and the list of the unique lattice spacings.
112
+ :rtype: list, list
113
+ """
114
+ hkls = np.empty((0,3))
115
+ ds = np.empty((0))
116
+ ds_index = np.empty((0))
117
+ for i, m in enumerate(self._materials):
118
+ material_class_valid = False
119
+ if HAVE_XU:
120
+ if isinstance(m, materials.material.Crystal):
121
+ if self._enrgy is None:
122
+ powder = simpack.PowderDiffraction(m)
123
+ else:
124
+ powder = simpack.PowderDiffraction(m, en=self._enrgy)
125
+ hklsi = [hkl for hkl in powder.data
126
+ if powder.data[hkl]['active']]
127
+ ds_i = [m.planeDistance(hkl) for hkl in powder.data
128
+ if powder.data[hkl]['active']]
129
+ mask = [d > self._ds_min[i] for d in ds_i]
130
+ hkls = np.vstack((hkls, np.array(hklsi)[mask,:]))
131
+ ds_i = np.array(ds_i)[mask]
132
+ material_class_valid = True
133
+ if HAVE_HEXRD:
134
+ if isinstance(m, material.Material):
135
+ plane_data = m.planeData
136
+ if tth_tol is not None:
137
+ plane_data.tThWidth = np.radians(tth_tol)
138
+ if tth_max is not None:
139
+ plane_data.exclusions = None
140
+ plane_data.tThMax = np.radians(tth_max)
141
+ hkls = np.vstack((hkls, plane_data.hkls.T))
142
+ ds_i = plane_data.getPlaneSpacings()
143
+ material_class_valid = True
144
+ if not material_class_valid:
145
+ raise ValueError('Illegal material class type')
146
+ ds = np.hstack((ds, ds_i))
147
+ ds_index = np.hstack((ds_index, i*np.ones(len(ds_i))))
148
+
149
+ # Sort lattice spacings in reverse order (use -)
150
+ ds_unique, ds_index_unique, _ = np.unique(
151
+ -ds.round(round_sig), return_index=True, return_counts=True)
152
+ ds_unique = np.abs(ds_unique)
153
+
154
+ # Limit the list to unique lattice spacings
155
+ self._hkls_unique = hkls[ds_index_unique,:].astype(int)
156
+ self._ds_unique = ds[ds_index_unique]
157
+ hkl_list = np.vstack(
158
+ (np.arange(self._ds_unique.shape[0]), ds_index[ds_index_unique],
159
+ self._hkls_unique.T, self._ds_unique)).T
160
+ logger.info("Unique d's:")
161
+ for hkl in hkl_list:
162
+ logger.info(
163
+ f'{hkl[0]:4.0f} {hkl[1]:.0f} {hkl[2]:.0f} {hkl[3]:.0f} '
164
+ f'{hkl[4]:.0f} {hkl[5]:.6f}')
165
+
166
+ return self._hkls_unique, self._ds_unique
167
+
168
+ @staticmethod
169
+ def make_material(
170
+ material_name, material_file=None, sgnum=None,
171
+ lattice_parameters_angstroms=None, atoms=None, pos=None,
172
+ dmin_angstroms=0.6):
173
+ """Use HeXRD to get material properties when a materials file
174
+ is provided. Use xrayutilities otherwise.
175
+ """
176
+ # pylint: disable=possibly-used-before-assignment
177
+ lattice_parameters = None
178
+ if not isinstance(material_name, str):
179
+ raise ValueError(
180
+ f'Illegal material_name: {material_name} '
181
+ f'{type(material_name)}')
182
+ if lattice_parameters_angstroms is not None:
183
+ if material_file is not None:
184
+ logger.warning(
185
+ 'Overwrite lattice_parameters of material_file with input '
186
+ f'values ({lattice_parameters_angstroms})')
187
+ if isinstance(lattice_parameters_angstroms, (int, float)):
188
+ lattice_parameters = [lattice_parameters_angstroms]
189
+ elif isinstance(
190
+ lattice_parameters_angstroms, (tuple, list, np.ndarray)):
191
+ lattice_parameters = list(lattice_parameters_angstroms)
192
+ else:
193
+ raise ValueError(
194
+ 'Illegal lattice_parameters_angstroms: '
195
+ f'{lattice_parameters_angstroms} '
196
+ f'{type(lattice_parameters_angstroms)}')
197
+ if material_file is None:
198
+ if not isinstance(sgnum, int):
199
+ raise ValueError(f'Illegal sgnum: {sgnum} {type(sgnum)}')
200
+ if (sgnum is None or lattice_parameters_angstroms is None
201
+ or pos is None):
202
+ raise ValueError(
203
+ 'Valid inputs for sgnum, lattice_parameters_angstroms and '
204
+ 'pos are required if materials file is not specified'
205
+ f' {sgnum} {lattice_parameters_angstroms} {pos}')
206
+ if isinstance(pos, str):
207
+ pos = [pos]
208
+ use_xu = True
209
+ if (np.array(pos).ndim == 1 and isinstance(pos[0], (int, float))
210
+ and np.array(pos).size == 3):
211
+ if HAVE_HEXRD:
212
+ pos = np.array([pos])
213
+ use_xu = False
214
+ elif (np.array(pos).ndim == 2 and np.array(pos).shape[0] > 0
215
+ and np.array(pos).shape[1] == 3):
216
+ if HAVE_HEXRD:
217
+ pos = np.array(pos)
218
+ use_xu = False
219
+ elif not (np.array(pos).ndim == 1 and isinstance(pos[0], str)
220
+ and np.array(pos).size > 0 and HAVE_XU):
221
+ raise ValueError(
222
+ f'Illegal pos (HAVE_XU = {HAVE_XU}): {pos} {type(pos)}')
223
+ if use_xu:
224
+ if atoms is None:
225
+ atoms = [material_name]
226
+ matl = materials.Crystal(
227
+ material_name,
228
+ materials.SGLattice(sgnum, *lattice_parameters,
229
+ atoms=atoms, pos=list(np.array(pos))))
230
+ else:
231
+ matl = material.Material(material_name)
232
+ matl.sgnum = sgnum
233
+ matl.atominfo = np.vstack((pos.T, np.ones(pos.shape[0]))).T
234
+ matl.latticeParameters = lattice_parameters
235
+ matl.dmin = valWUnit(
236
+ 'lp', 'length', dmin_angstroms, 'angstrom')
237
+ exclusions = matl.planeData.get_exclusions()
238
+ powder_intensity = matl.planeData.powder_intensity
239
+ exclusions = [
240
+ exclusion or i >= len(powder_intensity)
241
+ or powder_intensity[i] < POEDER_INTENSITY_CUTOFF
242
+ for i, exclusion in enumerate(exclusions)]
243
+ matl.planeData.set_exclusions(exclusions)
244
+ logger.debug(
245
+ f'powder_intensity = {matl.planeData.powder_intensity}')
246
+ logger.debug(f'exclusions = {matl.planeData.exclusions}')
247
+ else:
248
+ if not HAVE_HEXRD:
249
+ raise ValueError(
250
+ 'Illegal inputs: must provide detailed material info when '
251
+ 'hexrd package is unavailable')
252
+ if sgnum is not None:
253
+ logger.warning(
254
+ 'Ignore sgnum input when material_file is specified')
255
+ if not (path.splitext(material_file)[1] in
256
+ ('.h5', '.hdf5', '.xtal', '.cif')):
257
+ raise ValueError(f'Illegal material file {material_file}')
258
+ matl = material.Material(
259
+ material_name, material_file,
260
+ dmin=valWUnit('lp', 'length', dmin_angstroms, 'angstrom'))
261
+ if lattice_parameters_angstroms is not None:
262
+ matl.latticeParameters = lattice_parameters
263
+ exclusions = matl.planeData.get_exclusions()
264
+ powder_intensity = matl.planeData.powder_intensity
265
+ exclusions = [
266
+ exclusion or i >= len(powder_intensity)
267
+ or powder_intensity[i] < POEDER_INTENSITY_CUTOFF
268
+ for i, exclusion in enumerate(exclusions)]
269
+ matl.planeData.set_exclusions(exclusions)
270
+ logger.debug(
271
+ f'powder_intensity = {matl.planeData.powder_intensity}')
272
+ logger.debug(f'exclusions = {matl.planeData.exclusions}')
273
+
274
+ return matl