cohere-beamlines 4.2.1__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 (41) hide show
  1. cohere_beamlines/Petra3_P10/RSM_post_processing.py +300 -0
  2. cohere_beamlines/Petra3_P10/__init__.py +0 -0
  3. cohere_beamlines/Petra3_P10/beam_tabs.py +570 -0
  4. cohere_beamlines/Petra3_P10/beam_verifier.py +410 -0
  5. cohere_beamlines/Petra3_P10/detectors.py +230 -0
  6. cohere_beamlines/Petra3_P10/diffractometers.py +256 -0
  7. cohere_beamlines/Petra3_P10/instrument.py +149 -0
  8. cohere_beamlines/Petra3_P10/p10_scan_reader.py +908 -0
  9. cohere_beamlines/Petra3_P10/preprocessor.py +134 -0
  10. cohere_beamlines/aps_1ide/__init__.py +0 -0
  11. cohere_beamlines/aps_1ide/beam_tabs.py +617 -0
  12. cohere_beamlines/aps_1ide/beam_verifier.py +484 -0
  13. cohere_beamlines/aps_1ide/detectors.py +312 -0
  14. cohere_beamlines/aps_1ide/diffractometers.py +235 -0
  15. cohere_beamlines/aps_1ide/instrument.py +128 -0
  16. cohere_beamlines/aps_1ide/preprocessor.py +134 -0
  17. cohere_beamlines/aps_34idc/__init__.py +0 -0
  18. cohere_beamlines/aps_34idc/beam_tabs.py +624 -0
  19. cohere_beamlines/aps_34idc/beam_verifier.py +338 -0
  20. cohere_beamlines/aps_34idc/detectors.py +433 -0
  21. cohere_beamlines/aps_34idc/diffractometers.py +257 -0
  22. cohere_beamlines/aps_34idc/instrument.py +190 -0
  23. cohere_beamlines/aps_34idc/preprocessor.py +149 -0
  24. cohere_beamlines/esrf_id01/__init__.py +0 -0
  25. cohere_beamlines/esrf_id01/beam_tabs.py +272 -0
  26. cohere_beamlines/esrf_id01/beam_verifier.py +3 -0
  27. cohere_beamlines/esrf_id01/detectors.py +139 -0
  28. cohere_beamlines/esrf_id01/diffractometers.py +204 -0
  29. cohere_beamlines/esrf_id01/instrument.py +139 -0
  30. cohere_beamlines/esrf_id01/preprocessor.py +107 -0
  31. cohere_beamlines/simple/__init__.py +0 -0
  32. cohere_beamlines/simple/beam_verifier.py +3 -0
  33. cohere_beamlines/simple/detectors.py +216 -0
  34. cohere_beamlines/simple/diffractometers.py +178 -0
  35. cohere_beamlines/simple/instrument.py +133 -0
  36. cohere_beamlines/simple/preprocessor.py +25 -0
  37. cohere_beamlines-4.2.1.dist-info/METADATA +39 -0
  38. cohere_beamlines-4.2.1.dist-info/RECORD +41 -0
  39. cohere_beamlines-4.2.1.dist-info/WHEEL +5 -0
  40. cohere_beamlines-4.2.1.dist-info/licenses/LICENSE +18 -0
  41. cohere_beamlines-4.2.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,300 @@
1
+ #!/usr/local/bin/python2.7.3 -tttt
2
+ """
3
+ Functions to cut and plot the result RSM.
4
+ Created on Thu Apr 27 13:50:07 2023
5
+
6
+ @author: renzhe
7
+ @email: renzhe@ihep.ac.cn
8
+ """
9
+
10
+ import numpy as np
11
+ import matplotlib.pyplot as plt
12
+ from scipy.ndimage import measurements
13
+ import os
14
+
15
+
16
+ def check_cut_box_size(bs, peak_pos, data_shape):
17
+ """
18
+ Check the box size possible for the symmetrical cut around the given peak position in the data.
19
+
20
+ Parameters
21
+ ----------
22
+ bs : list
23
+ The Half width of the boxsize in [Z, Y, X] order.
24
+ peak_pos : list
25
+ The peak position in the dataset.
26
+ data_shape : Unions(list|tuple)
27
+ The shape of the data to be cutted.
28
+
29
+ Returns
30
+ -------
31
+ bs : list
32
+ The suggested box size according to the shape of the data.
33
+
34
+ """
35
+ bs[0] = int(np.amin([bs[0], peak_pos[0] * 0.95, 0.95 * (data_shape[0] - peak_pos[0])]))
36
+ bs[1] = int(np.amin([bs[1], peak_pos[1] * 0.95, 0.95 * (data_shape[1] - peak_pos[1])]))
37
+ bs[2] = int(np.amin([bs[2], peak_pos[2] * 0.95, 0.95 * (data_shape[2] - peak_pos[2])]))
38
+ return bs
39
+
40
+
41
+ def Cut_central(dataset, bs, cut_mode='maximum integration', peak_pos=None):
42
+ """
43
+ Cut the three dimensional dataset symmetrically with the box size given.
44
+
45
+ Parameters
46
+ ----------
47
+ dataset : ndarray
48
+ The three dimensional dataset to be cutted.
49
+ bs : list
50
+ The half width of the box size.
51
+ cut_mode : str, optional
52
+ The mode for choosing the center position for the cutting.
53
+ The cut mode can be 'maximum integration', 'maximum intensity', 'weight center' and 'given'.
54
+ 'maximum integration': The dataset is cutted around the maximum integrated intensity in each dimension.
55
+ 'maximum intensity': The dataset is cutted around the maximum intensity.
56
+ 'weight center': The dataset is cutted around weight center in the box.
57
+ 'given': The dataset is cutted around the given peak position.
58
+ The default is 'maximum integration'.
59
+ peak_pos : list, optional
60
+ The position for given peak position. The default is None.
61
+
62
+ Returns
63
+ -------
64
+ intcut : ndarray
65
+ The symmetrically cutted intensity.
66
+ peak_pos : list
67
+ The position of the peak position.
68
+ bs : list
69
+ The used box size for symmetrically cutting the dataset.
70
+
71
+ """
72
+ # Cutting the three dimensional data with the center of mass in the center of the intensity distribution
73
+ if cut_mode == 'maximum integration':
74
+ peak_pos = np.array([np.argmax(np.sum(dataset, axis=(1, 2))), np.argmax(np.sum(dataset, axis=(0, 2))), np.argmax(np.sum(dataset, axis=(0, 1)))], dtype=int)
75
+ print('finding the centeral position for the cutting')
76
+ bs = check_cut_box_size(bs, peak_pos, dataset.shape)
77
+ intcut = np.array(dataset[(peak_pos[0] - bs[0]):(peak_pos[0] + bs[0]), (peak_pos[1] - bs[1]):(peak_pos[1] + bs[1]), (peak_pos[2] - bs[2]):(peak_pos[2] + bs[2])])
78
+ elif cut_mode == 'maximum intensity':
79
+ peak_pos = np.unravel_index(np.argmax(dataset), dataset.shape)
80
+ bs = check_cut_box_size(bs, peak_pos, dataset.shape)
81
+ intcut = np.array(dataset[(peak_pos[0] - bs[0]):(peak_pos[0] + bs[0]), (peak_pos[1] - bs[1]):(peak_pos[1] + bs[1]), (peak_pos[2] - bs[2]):(peak_pos[2] + bs[2])])
82
+ elif cut_mode == 'weight center':
83
+ peak_pos = np.array(np.around(measurements.center_of_mass(dataset)), dtype=int)
84
+ bs = check_cut_box_size(bs, peak_pos, dataset.shape)
85
+ intcut = np.array(dataset[(peak_pos[0] - bs[0]):(peak_pos[0] + bs[0]), (peak_pos[1] - bs[1]):(peak_pos[1] + bs[1]), (peak_pos[2] - bs[2]):(peak_pos[2] + bs[2])])
86
+ print('cut according to the weight center')
87
+ i = 0
88
+ torlerence = 0.5
89
+ while not np.allclose(measurements.center_of_mass(intcut), np.array(bs, dtype=float) - 0.5, atol=torlerence):
90
+ peak_pos = np.array(peak_pos + np.around(measurements.center_of_mass(intcut) - np.array(bs, dtype=float) + 0.5), dtype=int)
91
+ bs = check_cut_box_size(bs, peak_pos, dataset.shape)
92
+ intcut = np.array(dataset[(peak_pos[0] - bs[0]):(peak_pos[0] + bs[0]), (peak_pos[1] - bs[1]):(peak_pos[1] + bs[1]), (peak_pos[2] - bs[2]):(peak_pos[2] + bs[2])])
93
+ i += 1
94
+ if i == 5:
95
+ print("Loosen the constrain for the weight center cutting")
96
+ torlerence = 1
97
+ elif i > 8:
98
+ print("could not find the weight center for the cutting")
99
+ break
100
+ elif cut_mode == 'given':
101
+ if peak_pos is None:
102
+ print('Could not find the given position for the cutting, please check it again!')
103
+ peak_pos = np.array((dataset.shape) / 2, dtype=int)
104
+ else:
105
+ peak_pos = np.array(peak_pos, dtype=int)
106
+ bs = check_cut_box_size(bs, peak_pos, dataset.shape)
107
+ intcut = np.array(dataset[(peak_pos[0] - bs[0]):(peak_pos[0] + bs[0]), (peak_pos[1] - bs[1]):(peak_pos[1] + bs[1]), (peak_pos[2] - bs[2]):(peak_pos[2] + bs[2])])
108
+ return intcut, peak_pos, bs
109
+
110
+
111
+ def plot_with_units(RSM_int, q_origin, unit, pathsavetmp, qmax=np.array([]), display_range=None):
112
+ """
113
+ Plot and save the diffraction pattern with correct units.
114
+
115
+ Parameters
116
+ ----------
117
+ RSM_int : ndarray
118
+ The diffraction pattern to be plotted.
119
+ q_origin : list
120
+ The minimum origin of the diffraction pattern.
121
+ unit : float
122
+ The unit of the diffraction pattern.
123
+ pathsavetmp : str
124
+ The template for saving the diffraction pattern.
125
+ The parameter should be the complete path with %s in the filename for the position indicating different cut directions.
126
+ qmax : list, optional
127
+ If given, the cutted diffraction pattern will be displayed around the given position.
128
+ Else the integrated diffraction intensity will be plotted.
129
+ The default is np.array([]).
130
+ display_range : list, optional
131
+ The half width of the display range in [qz, qy, qx] order. The default is None.
132
+
133
+ Returns
134
+ -------
135
+ None.
136
+
137
+ """
138
+ dz, dy, dx = RSM_int.shape
139
+ qz = np.arange(dz) * unit + q_origin[0]
140
+ qy = np.arange(dy) * unit + q_origin[1]
141
+ qx = np.arange(dx) * unit + q_origin[2]
142
+ # save the qx qy qz cut of the 3D intensity
143
+ print('Saving the qx qy qz cuts......')
144
+ plt.figure(figsize=(12, 12))
145
+ pathsaveimg = pathsavetmp % ('qz')
146
+ if len(qmax) == 0:
147
+ plt.contourf(qx, qy, np.log10(np.sum(RSM_int, axis=0) + 1.0), 150, cmap='jet')
148
+ else:
149
+ plt.contourf(qx, qy, np.log10(RSM_int[qmax[0], :, :] + 1.0), 150, cmap='jet')
150
+ plt.xlabel(r'Q$_x$ ($1/\AA$)', fontsize=20, fontstyle='italic', fontfamily='Arial', fontweight='bold')
151
+ plt.ylabel(r'Q$_y$ ($1/\AA$)', fontsize=20, fontstyle='italic', fontfamily='Arial', fontweight='bold')
152
+ plt.axis('scaled')
153
+ plt.tick_params(axis='both', labelsize=20)
154
+ if (display_range is not None) and (len(qmax) != 0):
155
+ plt.xlim(qmax[2] * unit + q_origin[2] - display_range[2], qmax[2] * unit + q_origin[2] + display_range[2])
156
+ plt.ylim(qmax[1] * unit + q_origin[1] - display_range[1], qmax[1] * unit + q_origin[1] + display_range[1])
157
+ plt.savefig(pathsaveimg)
158
+ plt.show()
159
+ # plt.close()
160
+
161
+ plt.figure(figsize=(12, 12))
162
+ pathsaveimg = pathsavetmp % ('qy')
163
+ if len(qmax) == 0:
164
+ plt.contourf(qx, qz, np.log10(np.sum(RSM_int, axis=1) + 1.0), 150, cmap='jet')
165
+ else:
166
+ plt.contourf(qx, qz, np.log10(RSM_int[:, qmax[1], :] + 1.0), 150, cmap='jet')
167
+ plt.xlabel(r'Q$_x$ ($1/\AA$)', fontsize=20, fontstyle='italic', fontfamily='Arial', fontweight='bold')
168
+ plt.ylabel(r'Q$_z$ ($1/\AA$)', fontsize=20, fontstyle='italic', fontfamily='Arial', fontweight='bold')
169
+ plt.axis('scaled')
170
+ plt.tick_params(axis='both', labelsize=20)
171
+ if (display_range is not None) and (len(qmax) != 0):
172
+ plt.xlim(qmax[2] * unit + q_origin[2] - display_range[2], qmax[2] * unit + q_origin[2] + display_range[2])
173
+ plt.ylim(qmax[0] * unit + q_origin[0] - display_range[0], qmax[0] * unit + q_origin[0] + display_range[0])
174
+ plt.savefig(pathsaveimg)
175
+ plt.show()
176
+ # plt.close()
177
+
178
+ plt.figure(figsize=(12, 12))
179
+ pathsaveimg = pathsavetmp % ('qx')
180
+ if len(qmax) == 0:
181
+ plt.contourf(qy, qz, np.log10(np.sum(RSM_int, axis=2) + 1.0), 150, cmap='jet')
182
+ else:
183
+ plt.contourf(qy, qz, np.log10(RSM_int[:, :, qmax[2]] + 1.0), 150, cmap='jet')
184
+ plt.xlabel(r'Q$_y$ ($1/\AA$)', fontsize=20, fontstyle='italic', fontfamily='Arial', fontweight='bold')
185
+ plt.ylabel(r'Q$_z$ ($1/\AA$)', fontsize=20, fontstyle='italic', fontfamily='Arial', fontweight='bold')
186
+ plt.axis('scaled')
187
+ plt.tick_params(axis='both', labelsize=20)
188
+ if (display_range is not None) and (len(qmax) != 0):
189
+ plt.xlim(qmax[1] * unit + q_origin[1] - display_range[1], qmax[1] * unit + q_origin[1] + display_range[1])
190
+ plt.ylim(qmax[0] * unit + q_origin[0] - display_range[0], qmax[0] * unit + q_origin[0] + display_range[0])
191
+ plt.savefig(pathsaveimg)
192
+ plt.show()
193
+ # plt.close()
194
+ return
195
+
196
+
197
+ def plot_without_units(RSM_int, mask, pathsavetmp):
198
+ """
199
+ Plot and save the diffraction pattern without units.
200
+
201
+ Parameters
202
+ ----------
203
+ RSM_int : ndarray
204
+ The diffraction pattern to be plotted.
205
+ mask : ndarray
206
+ The mask to be used. The masked pixels will be displayed by the red colors in the result plot.
207
+ pathsavetmp : str
208
+ The template for saving the diffraction pattern.
209
+ The parameter should be the complete path with %s in the filename for the position indicating different cut directions.
210
+
211
+ Returns
212
+ -------
213
+ None.
214
+
215
+ """
216
+ mask = np.ma.masked_where(mask == 0, mask)
217
+ dz, dy, dx = RSM_int.shape
218
+ # save the qx qy qz cut of the 3D intensity
219
+ print('Saving the qx qy qz cuts......')
220
+ plt.figure(figsize=(12, 12))
221
+ pathsaveimg = pathsavetmp % 'qz'
222
+ plt.imshow(np.log10(RSM_int[int(dz / 2), :, :] + 1.0), cmap='Blues')
223
+ if mask.ndim != 1:
224
+ plt.imshow(mask[int(dz / 2), :, :], cmap='Reds', alpha=0.5, vmin=0, vmax=1)
225
+ plt.xlabel(r'Q$_x$ (pixel)', fontsize=24)
226
+ plt.ylabel(r'Q$_y$ (pixel)', fontsize=24)
227
+ plt.axis('scaled')
228
+ plt.tick_params(axis='both', labelsize=24)
229
+ plt.savefig(pathsaveimg)
230
+ plt.show()
231
+ plt.close()
232
+
233
+ plt.figure(figsize=(12, 12))
234
+ pathsaveimg = pathsavetmp % 'qy'
235
+ plt.imshow(np.log10(RSM_int[:, int(dy / 2), :] + 1.0), cmap='Blues')
236
+ if mask.ndim != 1:
237
+ plt.imshow(mask[:, int(dy / 2), :], cmap='Reds', alpha=0.5, vmin=0, vmax=1)
238
+ plt.xlabel(r'Q$_x$ (pixel)', fontsize=24)
239
+ plt.ylabel(r'Q$_z$ (pixel)', fontsize=24)
240
+ plt.axis('scaled')
241
+ plt.tick_params(axis='both', labelsize=24)
242
+ plt.savefig(pathsaveimg)
243
+ plt.show()
244
+ plt.close()
245
+
246
+ plt.figure(figsize=(12, 12))
247
+ pathsaveimg = pathsavetmp % 'qx'
248
+ plt.imshow(np.log10(RSM_int[:, :, int(dx / 2)] + 1.0), cmap='Blues')
249
+ if mask.ndim != 1:
250
+ plt.imshow(mask[:, :, int(dx / 2)], cmap='Reds', alpha=0.5, vmin=0, vmax=1)
251
+ plt.xlabel(r'Q$_y$ (pixel)', fontsize=24)
252
+ plt.ylabel(r'Q$_z$ (pixel)', fontsize=24)
253
+ plt.axis('scaled')
254
+ plt.tick_params(axis='both', labelsize=24)
255
+ plt.savefig(pathsaveimg)
256
+ plt.show()
257
+ plt.close()
258
+ return
259
+
260
+
261
+ def RSM2vti(pathsave, RSM_dataset, filename, RSM_unit, origin=(0, 0, 0)):
262
+ """
263
+ Save the reciprocal space map to vti format for reading with paraview.
264
+
265
+ Parameters
266
+ ----------
267
+ pathsave : str
268
+ The folder path to save the RSM.
269
+ RSM_dataset : ndarray
270
+ The RSM to be saved.
271
+ filename : str
272
+ The filename for the saving.
273
+ RSM_unit : float
274
+ The unit of the RSM.
275
+ origin : list, optional
276
+ The origin of the RSM. The default is (0, 0, 0).
277
+
278
+ Returns
279
+ -------
280
+ None.
281
+
282
+ """
283
+ import vtk
284
+ from vtk.util.numpy_support import numpy_to_vtk
285
+
286
+ pathsave = os.path.join(pathsave, filename)
287
+ imdata = vtk.vtkImageData()
288
+ imdata.SetOrigin(origin[0], origin[1], origin[2])
289
+ imdata.SetSpacing(RSM_unit, RSM_unit, RSM_unit)
290
+ imdata.SetDimensions(RSM_dataset.shape)
291
+
292
+ RSM_vtk = numpy_to_vtk(np.ravel(np.transpose(np.log10(RSM_dataset + 1.0))), deep=True, array_type=vtk.VTK_DOUBLE)
293
+
294
+ imdata.GetPointData().SetScalars(RSM_vtk)
295
+ writer = vtk.vtkXMLImageDataWriter()
296
+ writer.SetFileName(pathsave)
297
+ writer.SetInputData(imdata)
298
+
299
+ writer.Write()
300
+ return
File without changes