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,365 @@
1
+ # -*- coding: utf-8 -*-
2
+ import logging
3
+ from enum import Enum
4
+
5
+ import numpy as np
6
+
7
+ from PyQt5.QtWidgets import (
8
+ QMainWindow,
9
+ QWidget,
10
+ QFrame,
11
+ QHBoxLayout,
12
+ QVBoxLayout,
13
+ QPushButton,
14
+ )
15
+ from PyQt5.QtGui import QColor, QPen
16
+ from PyQt5.QtCore import Qt, pyqtSignal
17
+
18
+ from pyqtgraph import GraphicsLayoutWidget, LinearRegionItem
19
+ from .sliders import LabeledSlider
20
+ from .toolbars import BlackToolBar
21
+ from ..basic_widgets import RoundedPushButton
22
+ from ..tools import Icon, show_error
23
+ from ...app.profiles import BasicProfile
24
+
25
+ from ..tools import get_pen, center_widget
26
+
27
+ logger = logging.getLogger(__name__)
28
+
29
+
30
+ class Custom1DPlot(GraphicsLayoutWidget):
31
+ @property
32
+ def view_box(self):
33
+ return self.plot_item.vb
34
+
35
+ def __init__(self, *args, parent=None):
36
+ super().__init__(parent)
37
+ self.plot_item = self.addPlot()
38
+ self.plot_item.setMenuEnabled(False)
39
+ self.plot = self.plot_item.plot(*args)
40
+ self.set_pen()
41
+
42
+ def set_data(self, *args):
43
+ self.plot.setData(*args)
44
+
45
+ def clear_plot(self):
46
+ self.plot.clear()
47
+
48
+ def set_x(self, x):
49
+ x = np.array(x)
50
+ y = self.plot.yData
51
+ if y is None or y.shape != x.shape:
52
+ return
53
+ self.plot.setData(x, y)
54
+
55
+ def set_pen(self, width: int = 3, color: str or QColor = 'white'):
56
+ if isinstance(color, str):
57
+ color = QColor(color)
58
+ pen = QPen(color)
59
+ pen.setStyle(Qt.SolidLine)
60
+ pen.setWidth(width)
61
+ pen.setCapStyle(Qt.RoundCap)
62
+ pen.setJoinStyle(Qt.RoundJoin)
63
+ pen.setCosmetic(True)
64
+ self.plot.setPen(pen)
65
+
66
+
67
+ class Smooth1DPlot(QMainWindow):
68
+ _MaximumSliderWidth = 200
69
+ _MaximumSliderHeight = 30
70
+
71
+ sigSigmaChanged = pyqtSignal(float)
72
+
73
+ @property
74
+ def view_box(self):
75
+ return self.image_view.view_box
76
+
77
+ def __init__(self, profile: BasicProfile, parent=None):
78
+ super().__init__(parent)
79
+ self.profile = profile
80
+ self.image_view = Custom1DPlot()
81
+ self.setCentralWidget(self.image_view)
82
+ self._init_toolbars()
83
+
84
+ @property
85
+ def x(self):
86
+ return self.profile.x
87
+
88
+ @x.setter
89
+ def x(self, value):
90
+ self.profile.x = value
91
+ self.plot()
92
+
93
+ @property
94
+ def y(self) -> np.ndarray or None:
95
+ return self.profile.y
96
+
97
+ def _init_toolbars(self):
98
+ param_toolbar = BlackToolBar('Parameters', self)
99
+ self.addToolBar(param_toolbar)
100
+
101
+ self._init_sigma_slider(param_toolbar)
102
+
103
+ def _init_sigma_slider(self, toolbar):
104
+ sigma_slider = LabeledSlider('𝞼', (0, 10), self.profile.sigma,
105
+ decimals=2)
106
+
107
+ sigma_slider.valueChanged.connect(self.update_sigma)
108
+
109
+ self.sigma_slider = sigma_slider
110
+ frame = QFrame()
111
+ layout = QHBoxLayout()
112
+ frame.setLayout(layout)
113
+ frame.setGeometry(0, 0, self._MaximumSliderWidth, int(toolbar.height() * 0.9))
114
+ layout.addWidget(sigma_slider, alignment=Qt.AlignLeft)
115
+ toolbar.addWidget(frame)
116
+
117
+ def set_sigma(self, value: float):
118
+ self.update_sigma(value)
119
+ self.sigma_slider.set_value(value, change_bounds=True)
120
+
121
+ def update_sigma(self, value: float):
122
+ self.profile.set_sigma(value)
123
+ self.plot()
124
+ self.sigSigmaChanged.emit(self.profile.sigma)
125
+
126
+ def auto_range(self):
127
+ self.image_view.plot_item.autoRange()
128
+
129
+ def plot(self):
130
+ if self.x is not None and self.y is not None:
131
+ self.image_view.set_data(self.x, self.y)
132
+ self.auto_range()
133
+
134
+ def clear_plot(self):
135
+ self.image_view.clear_plot()
136
+
137
+
138
+ class PlotBC(Smooth1DPlot):
139
+ sigBackgroundChanged = pyqtSignal()
140
+
141
+ def __init__(self, profile: BasicProfile, parent=None):
142
+ super().__init__(profile, parent)
143
+ self._status = BaseLineStatus.no_baseline
144
+ self.baseline_plot = self.image_view.plot_item.plot()
145
+ self._init_roi()
146
+ self._baseline_setup_widget = BaseLineSetup(self, self._status, **self.profile.get_parameters())
147
+ self.profile.sigDataUpdated.connect(self.update_plot)
148
+
149
+ @property
150
+ def y(self):
151
+ if self._status == BaseLineStatus.baseline_subtracted and self.profile.baseline is not None:
152
+ return self.profile.y - self.profile.baseline
153
+ else:
154
+ return self.profile.y
155
+
156
+ def update_data(self, *args, **kwargs):
157
+ self.profile.update_data(*args, **kwargs)
158
+
159
+ def is_shown(self, shown: bool):
160
+ self.profile.is_shown = shown
161
+
162
+ def _init_toolbars(self):
163
+ super()._init_toolbars()
164
+
165
+ baseline_toolbar = BlackToolBar('Baseline Correction')
166
+ self.addToolBar(baseline_toolbar)
167
+
168
+ baseline_button = RoundedPushButton(parent=baseline_toolbar, icon=Icon('baseline'),
169
+ radius=30)
170
+ baseline_button.clicked.connect(self.open_baseline_setup)
171
+ baseline_toolbar.addWidget(baseline_button)
172
+
173
+ def _init_roi(self):
174
+ self._roi = LinearRegionItem()
175
+ self._roi.hide()
176
+ self._roi.setBrush(QColor(255, 255, 255, 50))
177
+ self.image_view.plot_item.addItem(self._roi)
178
+
179
+ def open_baseline_setup(self):
180
+ if self.y is None:
181
+ return
182
+ setup = self._baseline_setup_widget
183
+
184
+ if self.profile.x_range is None:
185
+ self._set_default_bounds()
186
+ self._roi.setRegion(self.profile.x_range)
187
+
188
+ if self.profile.baseline is None:
189
+ self._set_status(BaseLineStatus.no_baseline)
190
+ # elif self._status == BaseLineStatus.no_baseline:
191
+ # self._set_status(BaseLineStatus.baseline_subtracted)
192
+ # else:
193
+ # self._set_status(self._status)
194
+
195
+ # self.plot()
196
+
197
+ setup.set_parameters(self.profile.get_parameters())
198
+
199
+ setup.calculate_signal.connect(self._on_calculate_baseline)
200
+ setup.subtract_signal.connect(self._on_subtracting_baseline)
201
+ setup.restore_signal.connect(self._on_restoring_data)
202
+ setup.close_signal.connect(self._on_closing_setup)
203
+ setup.show()
204
+ self._roi.show()
205
+
206
+ # def show_baseline(self):
207
+ # if (self.profile.baseline is None or self._status == BaseLineStatus.baseline_calculated or
208
+ # self._status == BaseLineStatus.baseline_restored):
209
+ # return
210
+ # self._on_restoring_data()
211
+
212
+ def update_plot(self):
213
+ self.sigma_slider.set_value(self.profile.sigma, True)
214
+
215
+ if self.profile.baseline is None:
216
+ self.clear_baseline()
217
+ else:
218
+ self._set_status(BaseLineStatus.baseline_subtracted)
219
+ self.plot()
220
+
221
+ def plot_baseline(self):
222
+ if self.profile.baseline is not None:
223
+ self.baseline_plot.setData(self.profile.x, self.profile.baseline,
224
+ pen=get_pen(width=4, color='red', style=Qt.DashDotLine))
225
+
226
+ def _set_default_bounds(self):
227
+ if self.x is None:
228
+ self.profile.x_range = (0, 1)
229
+ else:
230
+ self.profile.x_range = (np.nanmin(self.x), np.nanmax(self.x))
231
+
232
+ def _update_bounds(self):
233
+ self.profile.x_range = self._roi.getRegion()
234
+
235
+ def _set_status(self, status: 'BaseLineStatus'):
236
+ self._status = status
237
+ self._baseline_setup_widget.set_status(status)
238
+
239
+ def _on_calculate_baseline(self, params: dict):
240
+ self.profile.set_parameters(**params)
241
+ self._update_bounds()
242
+ try:
243
+ self.profile.update_baseline()
244
+ except Exception as err:
245
+ logger.exception(err)
246
+ show_error('Failed calculating baseline. Change roi region or parameters and try again.',
247
+ error_title='Baseline calculation error')
248
+ return
249
+ self._set_status(BaseLineStatus.baseline_calculated)
250
+ self.plot_baseline()
251
+
252
+ def _on_subtracting_baseline(self):
253
+ self.baseline_plot.clear()
254
+ self._set_status(BaseLineStatus.baseline_subtracted)
255
+ self.plot()
256
+
257
+ def _on_restoring_data(self):
258
+ self._set_status(BaseLineStatus.baseline_restored)
259
+ self.plot_baseline()
260
+ self.plot()
261
+
262
+ def _on_closing_setup(self):
263
+ self._baseline_setup_widget.hide()
264
+ self._roi.hide()
265
+ self.clear_baseline()
266
+ if self._status != BaseLineStatus.baseline_subtracted:
267
+ self._set_status(BaseLineStatus.no_baseline)
268
+ self.profile.clear_baseline(clear_range=False)
269
+ self.sigBackgroundChanged.emit()
270
+
271
+ def clear_baseline(self):
272
+ self.baseline_plot.clear()
273
+
274
+
275
+ class BaseLineStatus(Enum):
276
+ no_baseline = 1
277
+ baseline_calculated = 2
278
+ baseline_restored = 3
279
+ baseline_subtracted = 4
280
+
281
+
282
+ class BaseLineSetup(QWidget):
283
+ calculate_signal = pyqtSignal(dict)
284
+ subtract_signal = pyqtSignal()
285
+ restore_signal = pyqtSignal()
286
+ close_signal = pyqtSignal()
287
+
288
+ def __init__(self, parent, status: BaseLineStatus, smoothness: float, asymmetry: float):
289
+ super().__init__(parent=parent)
290
+ self.setAttribute(Qt.WA_DeleteOnClose, False)
291
+ self.setWindowFlag(Qt.WindowStaysOnTopHint)
292
+ self.setWindowFlag(Qt.Window)
293
+ self.setWindowTitle('Baseline setup')
294
+ self.setWindowIcon(Icon('baseline'))
295
+ self.setGeometry(0, 0, 400, 300)
296
+ self._status = None
297
+ self._init_ui(smoothness, asymmetry)
298
+ self.set_status(status)
299
+ center_widget(self)
300
+
301
+ def _init_ui(self, smoothness: float, asymmetry: float):
302
+ layout = QVBoxLayout(self)
303
+
304
+ self.smoothness_slider = LabeledSlider('Smoothness', (1e1, 1e10), smoothness,
305
+ self, Qt.Horizontal, decimals=3, scientific=True, log_scale=True)
306
+
307
+ self.asymmetry_slider = LabeledSlider('Asymmetry', (0.0001, 1), asymmetry,
308
+ self, Qt.Horizontal, decimals=3, scientific=True, log_scale=True)
309
+
310
+ # self.save_params_box = QRadioButton('Save as default')
311
+ # self.save_params_box.setChecked(True)
312
+ self.calculate_button = QPushButton('Calculate baseline')
313
+ self.calculate_button.clicked.connect(self.emit_calculate)
314
+ self.subtract_button = QPushButton('Subtract baseline')
315
+ self.subtract_button.clicked.connect(self.subtract_signal.emit)
316
+ self.restore_button = QPushButton('Restore line')
317
+ self.restore_button.clicked.connect(self.restore_signal.emit)
318
+
319
+ layout.addWidget(self.smoothness_slider)
320
+ layout.addWidget(self.asymmetry_slider)
321
+ # layout.addWidget(self.save_params_box)
322
+ layout.addWidget(self.calculate_button)
323
+ layout.addWidget(self.subtract_button)
324
+ layout.addWidget(self.restore_button)
325
+
326
+ def set_status(self, status: BaseLineStatus):
327
+ if status == BaseLineStatus.no_baseline:
328
+ self.calculate_button.setEnabled(True)
329
+ self.subtract_button.setEnabled(False)
330
+ self.restore_button.setEnabled(False)
331
+ elif status == BaseLineStatus.baseline_calculated:
332
+ self.subtract_button.setEnabled(True)
333
+ self.restore_button.setEnabled(False)
334
+ self.calculate_button.setEnabled(True)
335
+ elif status == BaseLineStatus.baseline_subtracted:
336
+ self.subtract_button.setEnabled(False)
337
+ self.restore_button.setEnabled(True)
338
+ self.calculate_button.setEnabled(False)
339
+ elif status == BaseLineStatus.baseline_restored:
340
+ self.subtract_button.setEnabled(True)
341
+ self.restore_button.setEnabled(False)
342
+ self.calculate_button.setEnabled(True)
343
+ else:
344
+ logger.error(f'Unknown status {status}')
345
+ return
346
+ self._status = status
347
+
348
+ def get_params_dict(self):
349
+ return dict(smoothness=self.smoothness_slider.value,
350
+ asymmetry=self.asymmetry_slider.value)
351
+
352
+ def set_parameters(self, params: dict):
353
+ if 'smoothness' in params:
354
+ self.smoothness_slider.set_value(params['smoothness'], True)
355
+ if 'asymmetry' in params:
356
+ self.asymmetry_slider.set_value(params['asymmetry'], True)
357
+
358
+ def emit_calculate(self):
359
+ self.calculate_signal.emit(self.get_params_dict())
360
+
361
+ def closeEvent(self, a0) -> None:
362
+ # if self.save_params_box.isChecked():
363
+ # save_config(self.NAME, self.get_params_dict())
364
+ self.close_signal.emit()
365
+ super().closeEvent(a0)
@@ -0,0 +1,224 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ import numpy as np
4
+ from PyQt5.QtGui import QTransform
5
+ from PyQt5.QtCore import pyqtSignal, pyqtSlot
6
+ from PyQt5.QtWidgets import QWidgetAction
7
+
8
+ from pyqtgraph import (GraphicsLayoutWidget, setConfigOptions,
9
+ ImageItem, HistogramLUTItem)
10
+ from pyqtgraph.graphicsItems.ViewBox.ViewBoxMenu import ViewBoxMenu
11
+
12
+ from mlgidGUI.gui.basic_widgets.sliders import LabeledSlider
13
+ from mlgidGUI.app.image_processing import standard_contrast_correction
14
+
15
+
16
+ class CustomViewBoxMenu(ViewBoxMenu):
17
+ sigSigmaChanged = pyqtSignal(float)
18
+ sigRangeAsDefault = pyqtSignal()
19
+ sigUseClahe = pyqtSignal(bool)
20
+
21
+ def __init__(self, view, sigma: float = 3):
22
+ super().__init__(view)
23
+ self._use_clahe = True
24
+ self.slider = LabeledSlider('Clip sigma factor', bounds=(0.001, 4),
25
+ value=sigma, parent=self)
26
+ self.slider.valueChanged.connect(self.sigSigmaChanged.emit)
27
+ self.addAction('Set range as default', lambda *x: self.sigRangeAsDefault.emit())
28
+ self.clahe_action = self.addAction('Disable CLAHE', self._use_clahe_changed)
29
+ self.sigma_factor_menu = self.addMenu('Set sigma factor')
30
+ action = QWidgetAction(self)
31
+
32
+ action.setDefaultWidget(self.slider)
33
+ self.sigma_factor_menu.addAction(action)
34
+
35
+ @pyqtSlot()
36
+ def _use_clahe_changed(self):
37
+ self._use_clahe = not self._use_clahe
38
+
39
+ if self._use_clahe:
40
+ self.clahe_action.setText("Disable CLAHE")
41
+ else:
42
+ self.clahe_action.setText("Enable CLAHE")
43
+ self.sigUseClahe.emit(self._use_clahe)
44
+
45
+
46
+ class CustomImageViewer(GraphicsLayoutWidget):
47
+ @property
48
+ def view_box(self):
49
+ return self.image_plot.vb
50
+
51
+ def __init__(self,
52
+ parent=None, *,
53
+ hist_range: tuple = None,
54
+ sigma_factor: float = 3,
55
+ **kwargs
56
+ ):
57
+ setConfigOptions(imageAxisOrder='row-major')
58
+ super(CustomImageViewer, self).__init__(parent)
59
+
60
+ self._raw_data = None
61
+ self._use_clahe: bool = True
62
+
63
+ self._scale = (1., 1.)
64
+ self._center = (0, 0)
65
+
66
+ self._hist_range = hist_range
67
+ self._sigma_factor = sigma_factor
68
+
69
+ self._init_ui(**kwargs)
70
+
71
+ def _init_ui(self, **kwargs):
72
+ self.setWindowTitle('Image Viewer')
73
+ self.image_plot = self.addPlot(**kwargs)
74
+ self.image_plot.vb.setAspectLocked()
75
+ self.image_plot.vb.invertY()
76
+ self.image_item = ImageItem()
77
+ self.image_plot.addItem(self.image_item)
78
+ self.image_plot.setMenuEnabled(False)
79
+ self.hist = HistogramLUTItem()
80
+ self.hist.setImageItem(self.image_item)
81
+ self.addItem(self.hist)
82
+ self.hist.vb.scene().contextMenu = None
83
+ self.hist.vb.menu = CustomViewBoxMenu(self.hist.vb)
84
+ self.hist.vb.menu.sigSigmaChanged.connect(self.set_sigma_factor)
85
+ self.hist.vb.menu.sigRangeAsDefault.connect(self.set_limit_as_default)
86
+ self.hist.vb.menu.sigUseClahe.connect(self.enable_clahe)
87
+
88
+ def set_data(self, data, *, reset_axes: bool = False):
89
+ self._raw_data = data
90
+
91
+ if data is None:
92
+ return
93
+
94
+ if self._use_clahe:
95
+ data = standard_contrast_correction(data)
96
+
97
+ self.image_item.setImage(data)
98
+ self.set_levels()
99
+ if reset_axes:
100
+ self.image_item.resetTransform()
101
+ self.set_default_range()
102
+
103
+ def hist_params(self) -> dict:
104
+ return dict(sigma_factor=self._sigma_factor, hist_range=self._hist_range)
105
+
106
+ def clear_image(self):
107
+ self.set_data(np.zeros((1, 1)))
108
+
109
+ def set_default_range(self):
110
+ if self.image_item.image is None:
111
+ return
112
+ # self.set_auto_range()
113
+ axes = self.get_axes()
114
+ self.image_plot.setRange(xRange=axes[1], yRange=axes[0])
115
+
116
+ @pyqtSlot(bool)
117
+ def enable_clahe(self, enable: bool):
118
+ self._use_clahe = enable
119
+ self.set_data(self._raw_data)
120
+
121
+ def set_auto_range(self):
122
+ self.image_plot.autoRange()
123
+
124
+ def set_levels(self):
125
+ img = self.image_item.image
126
+ if img is None:
127
+ return
128
+
129
+ if self._sigma_factor and self._sigma_factor > 0:
130
+ m, s = np.nanmean(img.flatten()), np.nanstd(img.flatten()) * self._sigma_factor
131
+ self.hist.setLevels(max(m - s, np.nanmin(img)), min(m + s, np.nanmax(img)))
132
+ elif self._hist_range:
133
+ self.hist.setLevels(*self._hist_range)
134
+ else:
135
+ self.hist.setLevels(np.nanmin(self.image_item.image),
136
+ np.nanmax(self.image_item.image))
137
+
138
+ @pyqtSlot(float)
139
+ def set_sigma_factor(self, sigma_factor: float):
140
+ self._sigma_factor = sigma_factor
141
+ self.set_levels()
142
+
143
+ @pyqtSlot()
144
+ def set_limit_as_default(self):
145
+ self._hist_range = self.hist.getLevels()
146
+ self._sigma_factor = None
147
+ self.set_levels()
148
+
149
+ def get_levels(self):
150
+ return self.hist.getLevels()
151
+
152
+ def set_center(self, center: tuple, pixel_units: bool = True):
153
+ if not pixel_units:
154
+ scale = self.get_scale()
155
+ center = (center[0] / scale[0], center[1] / scale[1])
156
+ if self._center != (0, 0) or self._scale != (1., 1.):
157
+ self.image_item.resetTransform()
158
+ self.image_item.scale(*self._scale)
159
+ self.image_item.translate(- center[0], - center[1])
160
+ self._center = center
161
+ self.set_default_range()
162
+
163
+ def set_scale(self, scale: float or tuple):
164
+ if isinstance(scale, float) or isinstance(scale, int):
165
+ scale = (scale, scale)
166
+ if self._center != (0, 0) or self._scale != (1., 1.):
167
+ self.image_item.resetTransform()
168
+ print(scale)
169
+ self.image_item.scale(*scale)
170
+ if self._center != (0, 0):
171
+ self.image_item.translate(- self._center[0], - self._center[1])
172
+ self._scale = scale
173
+ self.set_default_range()
174
+
175
+ def get_scale(self) -> tuple:
176
+ # scale property is occupied by Qt superclass.
177
+ return self._scale
178
+
179
+ def get_center(self) -> tuple:
180
+ return self._center
181
+
182
+ def set_x_axis(self, x_min, x_max):
183
+ self._set_axis(x_min, x_max, 0)
184
+ self.set_default_range()
185
+
186
+ def set_y_axis(self, y_min, y_max):
187
+ self._set_axis(y_min, y_max, 1)
188
+ self.set_default_range()
189
+
190
+ def _set_axis(self, min_: float, max_: float, axis_ind: int):
191
+ shape = self.image_item.image.shape
192
+ scale = np.array(self._scale)
193
+ scale[axis_ind] = (max_ - min_) / shape[axis_ind]
194
+ center = np.array(self._center)
195
+ center[axis_ind] = - min_ / scale[axis_ind]
196
+ if self._center != (0, 0) or self._scale != (1., 1.):
197
+ self.image_item.resetTransform()
198
+ transform = QTransform()
199
+ transform.scale(scale[0], scale[1])
200
+ transform.translate(-center[0], -center[1])
201
+ self.image_item.setTransform(transform)
202
+ self._scale = tuple(scale)
203
+ self._center = tuple(center)
204
+
205
+ def get_axes(self):
206
+ shape = np.array(self.image_item.image.shape)
207
+ scale = np.array(self._scale)
208
+ min_ = - np.array((self._center[1], self._center[0])) * scale
209
+ max_ = min_ + shape * scale
210
+ return (min_[0], max_[0]), (min_[1], max_[1])
211
+
212
+
213
+ if __name__ == '__main__':
214
+ import sys
215
+ from PyQt5.QtWidgets import QApplication
216
+
217
+ app = QApplication(sys.argv)
218
+ window = CustomImageViewer()
219
+ image = np.random.randint(0, 100, (100, 150))
220
+ window.set_data(image)
221
+ window.set_center((100, 0))
222
+ window.set_scale(0.01)
223
+ window.show()
224
+ sys.exit(app.exec_())