mlgidGUI 0.4.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 (202) hide show
  1. mlgidGUI/__init__.py +52 -0
  2. mlgidGUI/__main__.py +7 -0
  3. mlgidGUI/__version.py +1 -0
  4. mlgidGUI/app/__init__.py +8 -0
  5. mlgidGUI/app/actions_stack.py +1 -0
  6. mlgidGUI/app/app.py +67 -0
  7. mlgidGUI/app/cif_rois/__init__.py +0 -0
  8. mlgidGUI/app/cif_rois/cif_peaks_calculation.py +57 -0
  9. mlgidGUI/app/cif_rois/cif_simulation/__init__.py +0 -0
  10. mlgidGUI/app/cif_rois/cif_simulation/data/__init__.py +0 -0
  11. mlgidGUI/app/cif_rois/cif_simulation/data/atoms.py +15 -0
  12. mlgidGUI/app/cif_rois/cif_simulation/data/export_database.py +28 -0
  13. mlgidGUI/app/cif_rois/cif_simulation/experiment.py +57 -0
  14. mlgidGUI/app/cif_rois/cif_simulation/giwaxs_sim.py +286 -0
  15. mlgidGUI/app/cif_rois/cif_simulation/int_sim.py +105 -0
  16. mlgidGUI/app/cif_rois/cif_simulation/possible_directions.py +20 -0
  17. mlgidGUI/app/cif_rois/cif_simulation/q_sim.py +159 -0
  18. mlgidGUI/app/data_manager/__init__.py +4 -0
  19. mlgidGUI/app/data_manager/data_manager.py +164 -0
  20. mlgidGUI/app/data_manager/import_from_h5.py +68 -0
  21. mlgidGUI/app/data_manager/load_data.py +78 -0
  22. mlgidGUI/app/data_manager/pascal/__init__.py +2 -0
  23. mlgidGUI/app/data_manager/pascal/annotation_fabric.py +59 -0
  24. mlgidGUI/app/data_manager/pascal/draw_objects.py +119 -0
  25. mlgidGUI/app/data_manager/pascal/exceptions.py +6 -0
  26. mlgidGUI/app/data_manager/pascal/format_convertor.py +127 -0
  27. mlgidGUI/app/data_manager/pascal/pascal_annotation.py +85 -0
  28. mlgidGUI/app/data_manager/pascal/protocol_implementations.py +28 -0
  29. mlgidGUI/app/data_manager/pascal/protocols.py +47 -0
  30. mlgidGUI/app/data_manager/pascal/utils.py +64 -0
  31. mlgidGUI/app/data_manager/save_dataset.py +57 -0
  32. mlgidGUI/app/data_manager/save_h5.py +178 -0
  33. mlgidGUI/app/data_manager/saving_parameters.py +88 -0
  34. mlgidGUI/app/debug_tracker.py +129 -0
  35. mlgidGUI/app/edf_reader.py +135 -0
  36. mlgidGUI/app/file_manager/__init__.py +201 -0
  37. mlgidGUI/app/file_manager/config_manager.py +76 -0
  38. mlgidGUI/app/file_manager/keys.py +542 -0
  39. mlgidGUI/app/file_manager/npy_file_manager.py +17 -0
  40. mlgidGUI/app/file_manager/object_file_manager.py +77 -0
  41. mlgidGUI/app/file_manager/project_structure.py +169 -0
  42. mlgidGUI/app/file_manager/read_fits.py +76 -0
  43. mlgidGUI/app/file_manager/read_geometry.py +54 -0
  44. mlgidGUI/app/file_manager/read_images.py +31 -0
  45. mlgidGUI/app/file_manager/read_meta_roi.py +40 -0
  46. mlgidGUI/app/file_manager/read_polar_images.py +22 -0
  47. mlgidGUI/app/file_manager/read_radial_profile.py +6 -0
  48. mlgidGUI/app/file_manager/read_roi_data.py +39 -0
  49. mlgidGUI/app/fitting/__init__.py +5 -0
  50. mlgidGUI/app/fitting/background.py +92 -0
  51. mlgidGUI/app/fitting/fit.py +128 -0
  52. mlgidGUI/app/fitting/fit_holder.py +273 -0
  53. mlgidGUI/app/fitting/fit_object.py +247 -0
  54. mlgidGUI/app/fitting/functions.py +107 -0
  55. mlgidGUI/app/fitting/range_strategy.py +14 -0
  56. mlgidGUI/app/fitting/utils.py +17 -0
  57. mlgidGUI/app/geometry.py +236 -0
  58. mlgidGUI/app/geometry_holder.py +131 -0
  59. mlgidGUI/app/image_holder.py +200 -0
  60. mlgidGUI/app/image_processing.py +23 -0
  61. mlgidGUI/app/polar_image.py +96 -0
  62. mlgidGUI/app/profiles/__init__.py +5 -0
  63. mlgidGUI/app/profiles/angular_profile.py +20 -0
  64. mlgidGUI/app/profiles/basic_profile.py +169 -0
  65. mlgidGUI/app/profiles/radial_profile.py +38 -0
  66. mlgidGUI/app/read_image.py +21 -0
  67. mlgidGUI/app/rois/__init__.py +4 -0
  68. mlgidGUI/app/rois/new_roi_dict.py +670 -0
  69. mlgidGUI/app/rois/roi.py +194 -0
  70. mlgidGUI/app/rois/roi_colors.py +76 -0
  71. mlgidGUI/app/rois/roi_data.py +177 -0
  72. mlgidGUI/app/rois/roi_dict.py +466 -0
  73. mlgidGUI/app/rois/roi_meta_data.py +149 -0
  74. mlgidGUI/app/structures/__init__.py +6 -0
  75. mlgidGUI/app/structures/crystal_ring.py +19 -0
  76. mlgidGUI/app/structures/crystals_holders.py +144 -0
  77. mlgidGUI/app/structures/custom_crystal.py +45 -0
  78. mlgidGUI/app/structures/get_ring_list.py +89 -0
  79. mlgidGUI/app/transformations.py +82 -0
  80. mlgidGUI/app/update/__init__.py +3 -0
  81. mlgidGUI/app/update/check_version.py +51 -0
  82. mlgidGUI/app/update/launch_program.py +15 -0
  83. mlgidGUI/app/update/script_update.py +64 -0
  84. mlgidGUI/app/utils.py +124 -0
  85. mlgidGUI/gui/__init__.py +3 -0
  86. mlgidGUI/gui/background_tasks.py +41 -0
  87. mlgidGUI/gui/background_update.py +87 -0
  88. mlgidGUI/gui/basic_widgets/__init__.py +12 -0
  89. mlgidGUI/gui/basic_widgets/buttons.py +144 -0
  90. mlgidGUI/gui/basic_widgets/labels.py +51 -0
  91. mlgidGUI/gui/basic_widgets/mouse_events.py +119 -0
  92. mlgidGUI/gui/basic_widgets/path_line.py +162 -0
  93. mlgidGUI/gui/basic_widgets/plots_1d.py +365 -0
  94. mlgidGUI/gui/basic_widgets/plots_2d.py +224 -0
  95. mlgidGUI/gui/basic_widgets/popups.py +164 -0
  96. mlgidGUI/gui/basic_widgets/progress_bar.py +127 -0
  97. mlgidGUI/gui/basic_widgets/setup_widgets.py +138 -0
  98. mlgidGUI/gui/basic_widgets/sliders.py +616 -0
  99. mlgidGUI/gui/basic_widgets/spinner.py +65 -0
  100. mlgidGUI/gui/basic_widgets/toolbars.py +19 -0
  101. mlgidGUI/gui/debug_widgets.py +388 -0
  102. mlgidGUI/gui/dock_area.py +115 -0
  103. mlgidGUI/gui/exception_message.py +42 -0
  104. mlgidGUI/gui/fitting/__init__.py +2 -0
  105. mlgidGUI/gui/fitting/fit_widget.py +966 -0
  106. mlgidGUI/gui/fitting/inplace_fit.py +536 -0
  107. mlgidGUI/gui/fitting/selected_fit_widget.py +286 -0
  108. mlgidGUI/gui/gui_main.py +349 -0
  109. mlgidGUI/gui/image_viewer.py +323 -0
  110. mlgidGUI/gui/init_window.py +170 -0
  111. mlgidGUI/gui/load_window/__init__.py +2 -0
  112. mlgidGUI/gui/load_window/load_folder.py +61 -0
  113. mlgidGUI/gui/load_window/load_h5.py +73 -0
  114. mlgidGUI/gui/notifications.py +68 -0
  115. mlgidGUI/gui/polar_image_viewer.py +153 -0
  116. mlgidGUI/gui/profiles/__init__.py +1 -0
  117. mlgidGUI/gui/profiles/angular_profile_widget.py +62 -0
  118. mlgidGUI/gui/profiles/radial_profile_widget.py +164 -0
  119. mlgidGUI/gui/project_manager/__init__.py +17 -0
  120. mlgidGUI/gui/project_manager/cif_files_widget.py +196 -0
  121. mlgidGUI/gui/project_manager/cif_results.py +146 -0
  122. mlgidGUI/gui/project_manager/metadata_widget.py +15 -0
  123. mlgidGUI/gui/project_manager/project_viewer.py +151 -0
  124. mlgidGUI/gui/project_manager/roi_widget.py +171 -0
  125. mlgidGUI/gui/roi_widgets/__init__.py +6 -0
  126. mlgidGUI/gui/roi_widgets/abstract_roi_holder.py +189 -0
  127. mlgidGUI/gui/roi_widgets/abstract_roi_widget.py +111 -0
  128. mlgidGUI/gui/roi_widgets/cif_file_widget.py +16 -0
  129. mlgidGUI/gui/roi_widgets/cif_peak_widget.py +43 -0
  130. mlgidGUI/gui/roi_widgets/confidence_level_widget.py +105 -0
  131. mlgidGUI/gui/roi_widgets/roi_1d_widget.py +136 -0
  132. mlgidGUI/gui/roi_widgets/roi_2d_rect_widget.py +98 -0
  133. mlgidGUI/gui/roi_widgets/roi_2d_ring_widget.py +203 -0
  134. mlgidGUI/gui/roi_widgets/rois_context_menu.py +150 -0
  135. mlgidGUI/gui/roi_widgets/select_roi_color_widget.py +58 -0
  136. mlgidGUI/gui/save_window/__init__.py +152 -0
  137. mlgidGUI/gui/save_window/format_box.py +33 -0
  138. mlgidGUI/gui/save_window/num_selected_widget.py +45 -0
  139. mlgidGUI/gui/save_window/options_widgets.py +105 -0
  140. mlgidGUI/gui/save_window/save_mode_box.py +30 -0
  141. mlgidGUI/gui/save_window/select_images.py +198 -0
  142. mlgidGUI/gui/tools.py +179 -0
  143. mlgidGUI/static/__init__.py +0 -0
  144. mlgidGUI/static/icons/__init__.py +0 -0
  145. mlgidGUI/static/icons/add.png +0 -0
  146. mlgidGUI/static/icons/add_yellow.png +0 -0
  147. mlgidGUI/static/icons/angular_profile.png +0 -0
  148. mlgidGUI/static/icons/arrow.png +0 -0
  149. mlgidGUI/static/icons/baseline.png +0 -0
  150. mlgidGUI/static/icons/center.png +0 -0
  151. mlgidGUI/static/icons/confirm.png +0 -0
  152. mlgidGUI/static/icons/control_widget.png +0 -0
  153. mlgidGUI/static/icons/data.png +0 -0
  154. mlgidGUI/static/icons/delete.png +0 -0
  155. mlgidGUI/static/icons/delete_yellow.png +0 -0
  156. mlgidGUI/static/icons/error.png +0 -0
  157. mlgidGUI/static/icons/find.png +0 -0
  158. mlgidGUI/static/icons/fit.png +0 -0
  159. mlgidGUI/static/icons/fix_all.png +0 -0
  160. mlgidGUI/static/icons/flip_horizontal.png +0 -0
  161. mlgidGUI/static/icons/flip_vertical.png +0 -0
  162. mlgidGUI/static/icons/folder.png +0 -0
  163. mlgidGUI/static/icons/fromfullscreen.png +0 -0
  164. mlgidGUI/static/icons/h5_folder.png +0 -0
  165. mlgidGUI/static/icons/h5_group_folder.png +0 -0
  166. mlgidGUI/static/icons/info.png +0 -0
  167. mlgidGUI/static/icons/interpolate.png +0 -0
  168. mlgidGUI/static/icons/item.png +0 -0
  169. mlgidGUI/static/icons/properties.png +0 -0
  170. mlgidGUI/static/icons/radial_profile.png +0 -0
  171. mlgidGUI/static/icons/roi_item.png +0 -0
  172. mlgidGUI/static/icons/rotate.png +0 -0
  173. mlgidGUI/static/icons/rotate_image.png +0 -0
  174. mlgidGUI/static/icons/rotate_right.png +0 -0
  175. mlgidGUI/static/icons/setup.png +0 -0
  176. mlgidGUI/static/icons/setup_white.png +0 -0
  177. mlgidGUI/static/icons/tofullscreen.png +0 -0
  178. mlgidGUI/static/icons/unfix_all.png +0 -0
  179. mlgidGUI/static/icons/update.png +0 -0
  180. mlgidGUI/static/icons/update_suggested.png +0 -0
  181. mlgidGUI/static/icons/window_icon.png +0 -0
  182. mlgidGUI/static/icons/window_icon_256.png +0 -0
  183. mlgidGUI/utils/__init__.py +1 -0
  184. mlgidGUI/utils/project2h5.py +11 -0
  185. mlgidgui-0.4.1.dist-info/METADATA +119 -0
  186. mlgidgui-0.4.1.dist-info/RECORD +202 -0
  187. mlgidgui-0.4.1.dist-info/WHEEL +5 -0
  188. mlgidgui-0.4.1.dist-info/entry_points.txt +5 -0
  189. mlgidgui-0.4.1.dist-info/licenses/LICENSE +674 -0
  190. mlgidgui-0.4.1.dist-info/top_level.txt +4 -0
  191. tests/__init__.py +0 -0
  192. tests/conftest.py +1 -0
  193. tests/fixures/__init__.py +2 -0
  194. tests/fixures/projects.py +102 -0
  195. tests/func/__init__.py +1 -0
  196. tests/func/test_loading_state.py +21 -0
  197. tests/func/test_parse_project_data.py +23 -0
  198. tests/func/test_save.py +62 -0
  199. tests/unit/__init__.py +0 -0
  200. tests/unit/rois/__init__.py +0 -0
  201. tests/unit/rois/test_basic_operations.py +18 -0
  202. tests/unit/test_add_data.py +33 -0
@@ -0,0 +1,236 @@
1
+ from typing import Tuple, NamedTuple
2
+ from copy import deepcopy
3
+
4
+ import numpy as np
5
+
6
+ from .transformations import TransformationsHolder
7
+
8
+
9
+ class AxesScale(object):
10
+ def __init__(self, scale: float = 1):
11
+ self._scale = scale
12
+ self._prev_scale = 1.
13
+
14
+ @property
15
+ def scale(self):
16
+ return self._scale
17
+
18
+ @scale.setter
19
+ def scale(self, value):
20
+ self.set_scale(value)
21
+
22
+ @property
23
+ def scale_change(self):
24
+ return self.scale / self._prev_scale
25
+
26
+ def set_scale(self, scale: float):
27
+ self._scale, self._prev_scale = scale, self._scale
28
+
29
+
30
+ class BeamCenter(NamedTuple):
31
+ z: float = 0
32
+ y: float = 0
33
+
34
+
35
+ class Geometry(object):
36
+ def __init__(self, *, beam_center: tuple = (0, 0),
37
+ scale: float = 1.,
38
+ shape: Tuple[int, int] = (10, 10),
39
+ polar_shape: Tuple[int, int] = (1024, 1024),
40
+ t_key: str = None, update: bool = True,
41
+ **kwargs):
42
+
43
+ self._beam_center = BeamCenter(*beam_center)
44
+ self._scale = AxesScale(scale)
45
+ self._transforms = TransformationsHolder()
46
+ if t_key:
47
+ self._transforms.update(t_key)
48
+ self._shape = tuple(shape)
49
+ self._polar_shape = tuple(polar_shape)
50
+
51
+ self._r_range = (0, 1)
52
+ self._phi_range = (0, 2 * np.pi)
53
+ self._ring_bounds = (0, 2 * np.pi)
54
+ self._r = self._phi = None
55
+ self._y = self._z = None
56
+ self._polar_zz = self._polar_yy = None
57
+ self._polar_aspect_ratio = None
58
+
59
+ if update:
60
+ self.update()
61
+
62
+ def to_dict(self) -> dict:
63
+ return dict(beam_center=tuple(self.beam_center),
64
+ shape=self.shape,
65
+ scale=self.scale,
66
+ t_key=self.t.key,
67
+ polar_shape=self.polar_shape)
68
+
69
+ @staticmethod
70
+ def keys():
71
+ return 'beam_center', 'shape', 'scale', 't_key', 'polar_shape'
72
+
73
+ @classmethod
74
+ def fromdict(cls, d: dict):
75
+ return cls(**d)
76
+
77
+ def __eq__(self, other):
78
+ if type(other) != Geometry:
79
+ return False
80
+
81
+ return self.to_dict() == other.to_dict()
82
+
83
+ @property
84
+ def t(self):
85
+ return self._transforms
86
+
87
+ def copy(self) -> 'Geometry':
88
+ return deepcopy(self)
89
+
90
+ @property
91
+ def is_available(self) -> bool:
92
+ return self.shape is not None
93
+
94
+ @property
95
+ def ring_bounds(self):
96
+ return self._ring_bounds
97
+
98
+ @property
99
+ def beam_center(self):
100
+ return self._beam_center
101
+
102
+ @property
103
+ def r_range(self) -> Tuple[float, float]:
104
+ return self._r_range
105
+
106
+ @property
107
+ def phi_range(self) -> Tuple[float, float]:
108
+ return self._phi_range
109
+
110
+ @property
111
+ def polar_grids(self):
112
+ return self._polar_yy, self._polar_zz
113
+
114
+ @property
115
+ def r_axis(self):
116
+ return self._r
117
+
118
+ @property
119
+ def phi_axis(self):
120
+ return self._phi
121
+
122
+ @property
123
+ def polar_aspect_ratio(self):
124
+ return self._polar_aspect_ratio
125
+
126
+ @property
127
+ def y_axis(self):
128
+ return self._y
129
+
130
+ @property
131
+ def z_axis(self):
132
+ return self._z
133
+
134
+ @property
135
+ def shape(self):
136
+ return self._shape
137
+
138
+ @property
139
+ def polar_shape(self):
140
+ return self._polar_shape
141
+
142
+ @property
143
+ def scale(self):
144
+ return self._scale.scale
145
+
146
+ @property
147
+ def scale_change(self):
148
+ return self._scale.scale_change
149
+
150
+ def set_scale(self, scale: float, update: bool = True):
151
+ if scale != self.scale:
152
+ self._scale.set_scale(scale)
153
+ if update:
154
+ self._update_axes_on_scale(False)
155
+
156
+ def set_beam_center(self, z: float, y: float, update: bool = True):
157
+ if (self._beam_center.z, self._beam_center.y) != (z, y):
158
+ self._beam_center = BeamCenter(z, y)
159
+ if update:
160
+ self.update()
161
+ return True
162
+ return False
163
+
164
+ def set_shape(self, shape: Tuple[int, int], update: bool = True):
165
+ if shape != self.shape:
166
+ self._shape = shape
167
+ if update:
168
+ self.update()
169
+ return True
170
+ return False
171
+
172
+ def set_polar_shape(self, shape: Tuple[int, int], update: bool = True):
173
+ if shape != self.polar_shape:
174
+ self._polar_shape = shape
175
+ if update:
176
+ self.update_polar()
177
+ return True
178
+ return False
179
+
180
+ def update(self):
181
+ if not self.is_available:
182
+ return
183
+ self._update_ranges()
184
+ self._update_polar_grid()
185
+ self._update_axes_on_scale()
186
+
187
+ def update_polar(self):
188
+ self._update_polar_grid()
189
+ self._r *= self.scale
190
+ self._polar_aspect_ratio /= self.scale
191
+
192
+ def _update_ranges(self):
193
+ self._y = (np.arange(self._shape[1]) - self._beam_center.y)
194
+ self._z = (np.arange(self._shape[0]) - self._beam_center.z)
195
+
196
+ yy, zz = np.meshgrid(self._y, self._z)
197
+ rr = np.sqrt(yy ** 2 + zz ** 2)
198
+ phi = np.arctan2(zz, yy)
199
+ self._r_range = (np.nanmin(rr), np.nanmax(rr))
200
+ self._phi_range = p_min, p_max = np.nanmin(phi), np.nanmax(phi)
201
+ angle, angle_std = (p_max + p_min) / 2 * 180 / np.pi, (p_max - p_min) * 180 / np.pi
202
+ self._ring_bounds = (angle, angle_std)
203
+
204
+ def _update_polar_grid(self):
205
+ self._phi = np.linspace(*self.phi_range, self.polar_shape[0])
206
+ self._r = np.linspace(*self.r_range, self.polar_shape[1])
207
+
208
+ r_matrix = self._r[np.newaxis, :].repeat(self.polar_shape[0], axis=0)
209
+ p_matrix = self._phi[:, np.newaxis].repeat(self.polar_shape[1], axis=1)
210
+
211
+ self._polar_yy = r_matrix * np.cos(p_matrix) + self.beam_center.y
212
+ self._polar_zz = r_matrix * np.sin(p_matrix) + self.beam_center.z
213
+
214
+ self._phi *= 180 / np.pi
215
+
216
+ aspect_ratio = (np.nanmax(self._phi) - np.nanmin(self._phi)) * \
217
+ self.polar_shape[0] / (self._r_range[1] - self._r_range[0]) / self.polar_shape[1]
218
+ self._polar_aspect_ratio = aspect_ratio
219
+
220
+ def _update_axes_on_scale(self, init: bool = True):
221
+ if init:
222
+ scale = self.scale
223
+ else:
224
+ scale = self.scale_change
225
+
226
+ self._r = self._r * scale
227
+ self._y = self._y * scale
228
+ self._z = self._z * scale
229
+ self._polar_aspect_ratio = self._polar_aspect_ratio / scale
230
+
231
+ def r2p(self, r):
232
+ return (r - self.r_range[0]) / (self.r_range[1] - self.r_range[0]) * self.polar_shape[1]
233
+
234
+ def a2p(self, a):
235
+ return (a / 180 * np.pi - self.phi_range[0]) / (self.phi_range[1] - self.phi_range[0]) * self.polar_shape[0]
236
+
@@ -0,0 +1,131 @@
1
+ from typing import Tuple
2
+ import logging
3
+
4
+ from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject
5
+
6
+ import numpy as np
7
+
8
+ from .transformations import Transformation
9
+ from .file_manager import FileManager, ImageKey
10
+ from .geometry import Geometry
11
+
12
+
13
+ class GeometryHolder(QObject):
14
+ sigBeamCenterChanged = pyqtSignal()
15
+ sigGeometryChangeFinished = pyqtSignal()
16
+ sigPolarGeometryChanged = pyqtSignal()
17
+ sigTransformed = pyqtSignal()
18
+ sigScaleChanged = pyqtSignal()
19
+ sigRingBoundsChanged = pyqtSignal(tuple)
20
+
21
+ log = logging.getLogger(__name__)
22
+
23
+ def __init__(self, fm: FileManager):
24
+ QObject.__init__(self)
25
+ self._fm = fm
26
+ self._default_geometry = Geometry()
27
+ self._current_geometry = None
28
+ self._ring_bounds = (0, np.pi * 2)
29
+ self._current_key: ImageKey or None = None
30
+
31
+ def transform_image(self, raw_image: np.ndarray):
32
+ return self.geometry.t(raw_image)
33
+
34
+ def add_transform(self, op: Transformation):
35
+ if not self._current_geometry:
36
+ self._current_geometry = self.geometry.copy()
37
+ self.geometry.t.add(op)
38
+ self.sigTransformed.emit()
39
+
40
+ def get_geometry(self, image_key: ImageKey):
41
+ return self._fm.geometries[image_key] or self._fm.geometries.default[image_key.parent] or Geometry()
42
+
43
+ @property
44
+ def geometry(self) -> Geometry:
45
+ return self._current_geometry or self._default_geometry
46
+
47
+ @property
48
+ def is_default(self) -> bool:
49
+ return self._current_geometry is None
50
+
51
+ def change_image(self, image_key: ImageKey, image: np.ndarray = None):
52
+ if image_key == self._current_key:
53
+ return
54
+
55
+ self.save_state()
56
+
57
+ self._current_key = image_key
58
+ if not self._current_key:
59
+ return
60
+
61
+ self._default_geometry = self._fm.geometries.default[image_key.parent] or Geometry()
62
+ self._current_geometry = self._fm.geometries[image_key]
63
+
64
+ if image is not None:
65
+ image = self.transform_image(image)
66
+ if image.shape != self.geometry.shape:
67
+ self.set_shape(image.shape)
68
+ return image
69
+
70
+ def save_as_default(self):
71
+ if self._current_geometry and self._current_key:
72
+ self._default_geometry = self._current_geometry
73
+ self._fm.geometries.default[self._current_key.parent] = self._current_geometry
74
+ del self._fm.geometries[self._current_key]
75
+ self._current_geometry = None
76
+ self.log.info('Geometry saved as default.')
77
+
78
+ self.log.info('Geometry not saved (default used)')
79
+
80
+ def save_state(self):
81
+ if not (self._current_key and self._current_geometry):
82
+ self.log.info('Geometry not saved (default used)')
83
+ return
84
+
85
+ if not self._fm.geometries.default[self._current_key.parent]:
86
+ self.save_as_default()
87
+ elif self._current_geometry != self._default_geometry:
88
+ self._fm.geometries[self._current_key] = self._current_geometry
89
+ self.log.info('Non-default geometry saved')
90
+ else:
91
+ del self._fm.geometries[self._current_key]
92
+ self.log.info('Non-default geometry deleted.')
93
+
94
+ @pyqtSlot(tuple, bool, name='changeBeamCenter')
95
+ def set_beam_center(self, beam_center: tuple, finished: bool = True):
96
+ if (beam_center[0] == self.geometry.beam_center.z and
97
+ beam_center[1] == self.geometry.beam_center.y):
98
+ return
99
+ if not self._current_geometry:
100
+ self._current_geometry = self.geometry.copy()
101
+ self._current_geometry.set_beam_center(*beam_center)
102
+ self.sigBeamCenterChanged.emit()
103
+ self.check_ring_bounds()
104
+ if finished:
105
+ self.sigGeometryChangeFinished.emit()
106
+
107
+ def set_shape(self, shape: Tuple[int, int]):
108
+ if not self._current_geometry:
109
+ self._current_geometry = self.geometry.copy()
110
+ self._current_geometry.set_shape(shape)
111
+
112
+ @pyqtSlot(tuple, name='changePolarShape')
113
+ def set_polar_shape(self, shape: Tuple[int, int]):
114
+ if not self._current_geometry:
115
+ self._current_geometry = self.geometry.copy()
116
+ self._current_geometry.set_polar_shape(shape)
117
+ self.sigGeometryChangeFinished.emit()
118
+
119
+ @pyqtSlot(float, name='changeScale')
120
+ def set_scale(self, scale: float):
121
+ if self.geometry.scale == scale:
122
+ return
123
+ if not self._current_geometry:
124
+ self._current_geometry = self.geometry.copy()
125
+ self._current_geometry.set_scale(scale)
126
+ self.sigScaleChanged.emit()
127
+
128
+ def check_ring_bounds(self):
129
+ if self.geometry and self.geometry.ring_bounds != self._ring_bounds:
130
+ self._ring_bounds = self.geometry.ring_bounds
131
+ self.sigRingBoundsChanged.emit(self._ring_bounds)
@@ -0,0 +1,200 @@
1
+ import logging
2
+ from typing import List
3
+ from datetime import datetime as dt
4
+
5
+ import numpy as np
6
+
7
+ from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject
8
+
9
+ from .rois.roi_dict import RoiDict, Roi
10
+ from .geometry import Geometry
11
+ from .geometry_holder import GeometryHolder
12
+ from .polar_image import (PolarImage, InterpolationParams,
13
+ INTERPOLATION_ALGORITHMS, INTERPOLATION_ALGORITHMS_INVERSED)
14
+ from .file_manager import FileManager, ImageKey
15
+ from .fitting import FitObject
16
+
17
+
18
+ class ImageHolder(QObject):
19
+ sigImageChanged = pyqtSignal()
20
+ sigPolarImageChanged = pyqtSignal()
21
+ sigFitOpen = pyqtSignal(object)
22
+ sigFitSaved = pyqtSignal(tuple)
23
+ sigEmptyImage = pyqtSignal()
24
+ sigCIFPeaksChanged = pyqtSignal(float)
25
+
26
+ log = logging.getLogger(__name__)
27
+
28
+ def __init__(self, fm: FileManager, g_holder: GeometryHolder, roi_dict: RoiDict):
29
+ QObject.__init__(self)
30
+ self._fm = fm
31
+ self._roi_dict = roi_dict
32
+ self._image = self._raw_image = None
33
+ self._current_key: ImageKey = None
34
+ self._polar_image = PolarImage()
35
+ self._g_holder = g_holder
36
+
37
+ self._roi_dict.sigFitRoisOpen.connect(self.open_fit_rois)
38
+ self._g_holder.sigPolarGeometryChanged.connect(self._update_polar_image)
39
+ self._g_holder.sigGeometryChangeFinished.connect(self._update_polar_image)
40
+ self._g_holder.sigTransformed.connect(self._update_image)
41
+
42
+ @property
43
+ def current_key(self) -> ImageKey or None:
44
+ return self._current_key
45
+
46
+ @property
47
+ def image(self) -> np.ndarray or None:
48
+ return self._image
49
+
50
+ @property
51
+ def raw_image(self) -> np.ndarray or None:
52
+ return self._raw_image
53
+
54
+ @property
55
+ def polar(self) -> PolarImage:
56
+ return self._polar_image
57
+
58
+ @property
59
+ def polar_image(self) -> np.ndarray or None:
60
+ return self._polar_image.polar_image
61
+
62
+ @property
63
+ def polar_params(self) -> InterpolationParams:
64
+ return self._polar_image.polar_params
65
+
66
+ @property
67
+ def g_holder(self) -> GeometryHolder:
68
+ return self._g_holder
69
+
70
+ @property
71
+ def geometry(self) -> Geometry:
72
+ return self.g_holder.geometry
73
+
74
+ @pyqtSlot(object, name='changeImage')
75
+ def change_image(self, image_key: ImageKey):
76
+ if self._current_key == image_key:
77
+ return
78
+ self._current_key = image_key
79
+
80
+ self._roi_dict.save_and_clear()
81
+
82
+ if not image_key:
83
+ self.g_holder.change_image(None)
84
+ self._roi_dict.change_image(None)
85
+ self.sigEmptyImage.emit()
86
+ return
87
+
88
+ image = self._fm.images[image_key]
89
+
90
+ if image is None:
91
+ self.sigEmptyImage.emit()
92
+ return
93
+
94
+ polar_image = self._fm.polar_images[image_key]
95
+ prev_geometry = self.geometry
96
+
97
+ self._raw_image = image
98
+ self._image = self.g_holder.change_image(image_key, image)
99
+ self._update_polar_image(polar_image, False)
100
+
101
+ if self.geometry.beam_center != prev_geometry.beam_center:
102
+ self.g_holder.sigBeamCenterChanged.emit()
103
+ if self.geometry.scale != prev_geometry.scale:
104
+ self.g_holder.sigScaleChanged.emit()
105
+
106
+ self.sigImageChanged.emit()
107
+ self.sigPolarImageChanged.emit()
108
+
109
+ # self.g_holder.check_ring_bounds()
110
+ self._roi_dict.change_image(image_key)
111
+
112
+ def get_data_by_key(self, image_key: ImageKey, save: bool = False):
113
+ polar_image = self._fm.polar_images[image_key]
114
+ image = self._fm.images[image_key]
115
+ geometry = self.g_holder.get_geometry(image_key)
116
+ if image is None or geometry is None:
117
+ return None, None, None
118
+
119
+ image = geometry.t(image)
120
+ if geometry.shape != image.shape:
121
+ geometry.set_shape(image.shape)
122
+ if polar_image is None:
123
+ yy, zz = geometry.polar_grids
124
+ polar_image = self.polar.calc_polar_image(image, yy, zz, self.polar_params.algorithm)
125
+ if save:
126
+ self._fm.polar_images[image_key] = polar_image
127
+ return image, polar_image, geometry
128
+
129
+ def _update_polar_image(self, polar_image=None, emit: bool = True):
130
+ if polar_image is not None:
131
+ self._polar_image.set_polar_image(polar_image)
132
+ else:
133
+ self._polar_image.update(self.geometry, self.image)
134
+ if emit:
135
+ self.sigPolarImageChanged.emit()
136
+
137
+ def _update_image(self, polar_image: np.ndarray = None, emit: bool = True) -> None:
138
+ if self.raw_image is None:
139
+ return
140
+ old_shape = self.image.shape if self.image is not None else None
141
+ self._image = self.g_holder.transform_image(self._raw_image)
142
+ if old_shape != self.image.shape:
143
+ self.g_holder.set_shape(self.image.shape)
144
+ self._update_polar_image(polar_image, emit=emit)
145
+ if emit:
146
+ self.sigImageChanged.emit()
147
+
148
+ def set_polar_image_params(self, params: dict):
149
+ shape = (params.get('phi_size', self.polar_params.shape[0]),
150
+ params.get('r_size', self.polar_params.shape[1]))
151
+ algorithm = INTERPOLATION_ALGORITHMS.get(params.get('mode', None), self.polar_params.algorithm)
152
+ self.polar.set_params(shape=shape, algorithm=algorithm)
153
+ self.g_holder.set_polar_shape(shape)
154
+
155
+ def polar_image_params_dict(self):
156
+ return dict(phi_size=self.geometry.polar_shape[0], r_size=self.geometry.polar_shape[1],
157
+ algorithm=INTERPOLATION_ALGORITHMS_INVERSED[self.polar_params.algorithm])
158
+
159
+ def get_radial_profile(self) -> np.ndarray or None:
160
+ return self.polar.get_radial_profile()
161
+
162
+ def get_angular_profile(self, key: int) -> np.ndarray or None:
163
+ if key is None:
164
+ try:
165
+ roi = self._roi_dict.selected_rois[0]
166
+ except IndexError:
167
+ return
168
+ else:
169
+ roi = self._roi_dict[key]
170
+
171
+ return self.polar.get_angular_profile(self.geometry, roi)
172
+
173
+ @pyqtSlot(list, name='openFitRois')
174
+ def open_fit_rois(self, rois: List[Roi]):
175
+ self.sigFitOpen.emit(self.create_fit_object(rois))
176
+
177
+ def create_fit_object(self, rois: List[Roi]) -> FitObject:
178
+ fit_object = FitObject(self._current_key, self.polar_image,
179
+ self.geometry.r_axis, self.geometry.phi_axis)
180
+ profile = self._fm.profiles[self._current_key]
181
+
182
+ if profile:
183
+ fit_object.set_profile(profile)
184
+
185
+ for roi in rois:
186
+ fit_object.new_fit(roi)
187
+ return fit_object
188
+
189
+ @pyqtSlot(object, name='applyFit')
190
+ def apply_fit(self, fit_object: FitObject):
191
+ name = dt.now().ctime()
192
+ fit_object.name = name
193
+ parent = fit_object.image_key.parent
194
+ fit_object.image_key.remove_parent()
195
+ self._fm.fits[fit_object.image_key, name] = fit_object
196
+ fit_object.image_key.set_parent(parent)
197
+
198
+ self._roi_dict.apply_fit([fit.roi for fit in fit_object.fits.values()], fit_object.image_key)
199
+
200
+ self.sigFitSaved.emit((fit_object.image_key, name))
@@ -0,0 +1,23 @@
1
+ import cv2 as cv
2
+ import numpy as np
3
+ from numpy import nanmax, nanmin, nan_to_num
4
+
5
+
6
+ def clahe(img, limit: float = 5000):
7
+ return cv.createCLAHE(clipLimit=limit, tileGridSize=(1, 1)).apply(img.astype('uint16')).astype(np.float32)
8
+
9
+
10
+ def norm_img(img):
11
+ return nan_to_num((img - nanmin(img)) / (nanmax(img) - nanmin(img)))
12
+
13
+
14
+ def standard_contrast_correction(
15
+ img,
16
+ limit: float = 2000,
17
+ coef: float = 5000,
18
+ log: bool = True,
19
+ ):
20
+ if log:
21
+ img = np.log10(norm_img(img) * coef + 1)
22
+
23
+ return norm_img(clahe(norm_img(img) * coef, limit))
@@ -0,0 +1,96 @@
1
+ from typing import Tuple, NamedTuple
2
+
3
+ import cv2
4
+ import numpy as np
5
+
6
+ from .geometry import Geometry
7
+ from ..app.rois.roi import Roi
8
+
9
+ INTERPOLATION_ALGORITHMS = {
10
+ 'Nearest': cv2.INTER_NEAREST,
11
+ 'Bilinear': cv2.INTER_LINEAR,
12
+ 'Cubic': cv2.INTER_CUBIC,
13
+ 'Lanczos': cv2.INTER_LANCZOS4
14
+ }
15
+
16
+ INTERPOLATION_ALGORITHMS_INVERSED = {v: k for k, v in INTERPOLATION_ALGORITHMS.items()}
17
+
18
+
19
+ class InterpolationParams(NamedTuple):
20
+ shape: Tuple[int, int] = (1024, 1024)
21
+ algorithm: int = cv2.INTER_LINEAR
22
+
23
+
24
+ class PolarImage(object):
25
+
26
+ def __init__(self, polar_img: np.ndarray = None,
27
+ parameters: InterpolationParams = None):
28
+ self._polar_img = polar_img
29
+ self._parameters = parameters or InterpolationParams()
30
+
31
+ @property
32
+ def polar_image(self) -> np.ndarray or None:
33
+ return self._polar_img
34
+
35
+ @property
36
+ def polar_params(self) -> InterpolationParams:
37
+ return self._parameters
38
+
39
+ def set_params(self, shape: Tuple[int, int] = None, algorithm=None):
40
+ shape = shape or self.polar_params.shape
41
+ algorithm = algorithm if algorithm is not None else self.polar_params.algorithm
42
+
43
+ self._parameters = InterpolationParams(shape, algorithm)
44
+
45
+ def set_algorithm(self, algorithm: int, image: np.ndarray, geometry: Geometry):
46
+ if self.polar_params.algorithm == algorithm:
47
+ return
48
+
49
+ self._parameters = InterpolationParams(self.polar_params.shape, algorithm)
50
+ self.update(geometry, image)
51
+
52
+ def update(self, geometry: Geometry, image: np.ndarray):
53
+ if image is None:
54
+ self._polar_img = None
55
+ return
56
+
57
+ yy, zz = geometry.polar_grids
58
+ self._polar_img = self.calc_polar_image(image, yy, zz, self.polar_params.algorithm)
59
+
60
+ def set_polar_image(self, img: np.ndarray):
61
+ self._polar_img = img
62
+ if img.shape != self._parameters.shape:
63
+ self._parameters = InterpolationParams(img.shape, self._parameters.algorithm)
64
+
65
+ @staticmethod
66
+ def calc_polar_image(img: np.ndarray, yy: np.ndarray, zz: np.ndarray,
67
+ algorithm=cv2.INTER_LINEAR) -> np.ndarray or None:
68
+ try:
69
+ return cv2.remap(img.astype(np.float32),
70
+ yy.astype(np.float32),
71
+ zz.astype(np.float32),
72
+ interpolation=algorithm)
73
+ except cv2.error:
74
+ return
75
+
76
+ def get_radial_profile(self) -> np.ndarray or None:
77
+ if self.polar_image is None:
78
+ return
79
+ return np.nanmean(self.polar_image,axis=0)
80
+
81
+ def get_angular_profile(self, geometry: Geometry, roi: Roi) -> np.ndarray or None:
82
+ if self.polar_image is None:
83
+ return
84
+ r1, r2 = roi.radius - roi.width / 2, roi.radius + roi.width / 2
85
+
86
+ r_min, r_max = geometry.r_range
87
+ scale = geometry.scale
88
+ r_size = self.polar_image.shape[1]
89
+ r_ratio = (r_max - r_min) / r_size * scale
90
+
91
+ r1, r2 = int((r1 - r_min) / r_ratio), int((r2 - r_min) / r_ratio)
92
+ r1, r2 = max(min((r1, r2)), 0), min(max((r1, r2)), r_size)
93
+
94
+ if r1 > r_size or r2 < 0:
95
+ return
96
+ return np.nanmean(self.polar_image[:, r1:r2],axis=1)
@@ -0,0 +1,5 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ from .basic_profile import BasicProfile, BaselineParams, SavedProfile
4
+ from .radial_profile import RadialProfile
5
+ from .angular_profile import AngularProfile