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.
- mlgidGUI/__init__.py +52 -0
- mlgidGUI/__main__.py +7 -0
- mlgidGUI/__version.py +1 -0
- mlgidGUI/app/__init__.py +8 -0
- mlgidGUI/app/actions_stack.py +1 -0
- mlgidGUI/app/app.py +67 -0
- mlgidGUI/app/cif_rois/__init__.py +0 -0
- mlgidGUI/app/cif_rois/cif_peaks_calculation.py +57 -0
- mlgidGUI/app/cif_rois/cif_simulation/__init__.py +0 -0
- mlgidGUI/app/cif_rois/cif_simulation/data/__init__.py +0 -0
- mlgidGUI/app/cif_rois/cif_simulation/data/atoms.py +15 -0
- mlgidGUI/app/cif_rois/cif_simulation/data/export_database.py +28 -0
- mlgidGUI/app/cif_rois/cif_simulation/experiment.py +57 -0
- mlgidGUI/app/cif_rois/cif_simulation/giwaxs_sim.py +286 -0
- mlgidGUI/app/cif_rois/cif_simulation/int_sim.py +105 -0
- mlgidGUI/app/cif_rois/cif_simulation/possible_directions.py +20 -0
- mlgidGUI/app/cif_rois/cif_simulation/q_sim.py +159 -0
- mlgidGUI/app/data_manager/__init__.py +4 -0
- mlgidGUI/app/data_manager/data_manager.py +164 -0
- mlgidGUI/app/data_manager/import_from_h5.py +68 -0
- mlgidGUI/app/data_manager/load_data.py +78 -0
- mlgidGUI/app/data_manager/pascal/__init__.py +2 -0
- mlgidGUI/app/data_manager/pascal/annotation_fabric.py +59 -0
- mlgidGUI/app/data_manager/pascal/draw_objects.py +119 -0
- mlgidGUI/app/data_manager/pascal/exceptions.py +6 -0
- mlgidGUI/app/data_manager/pascal/format_convertor.py +127 -0
- mlgidGUI/app/data_manager/pascal/pascal_annotation.py +85 -0
- mlgidGUI/app/data_manager/pascal/protocol_implementations.py +28 -0
- mlgidGUI/app/data_manager/pascal/protocols.py +47 -0
- mlgidGUI/app/data_manager/pascal/utils.py +64 -0
- mlgidGUI/app/data_manager/save_dataset.py +57 -0
- mlgidGUI/app/data_manager/save_h5.py +178 -0
- mlgidGUI/app/data_manager/saving_parameters.py +88 -0
- mlgidGUI/app/debug_tracker.py +129 -0
- mlgidGUI/app/edf_reader.py +135 -0
- mlgidGUI/app/file_manager/__init__.py +201 -0
- mlgidGUI/app/file_manager/config_manager.py +76 -0
- mlgidGUI/app/file_manager/keys.py +542 -0
- mlgidGUI/app/file_manager/npy_file_manager.py +17 -0
- mlgidGUI/app/file_manager/object_file_manager.py +77 -0
- mlgidGUI/app/file_manager/project_structure.py +169 -0
- mlgidGUI/app/file_manager/read_fits.py +76 -0
- mlgidGUI/app/file_manager/read_geometry.py +54 -0
- mlgidGUI/app/file_manager/read_images.py +31 -0
- mlgidGUI/app/file_manager/read_meta_roi.py +40 -0
- mlgidGUI/app/file_manager/read_polar_images.py +22 -0
- mlgidGUI/app/file_manager/read_radial_profile.py +6 -0
- mlgidGUI/app/file_manager/read_roi_data.py +39 -0
- mlgidGUI/app/fitting/__init__.py +5 -0
- mlgidGUI/app/fitting/background.py +92 -0
- mlgidGUI/app/fitting/fit.py +128 -0
- mlgidGUI/app/fitting/fit_holder.py +273 -0
- mlgidGUI/app/fitting/fit_object.py +247 -0
- mlgidGUI/app/fitting/functions.py +107 -0
- mlgidGUI/app/fitting/range_strategy.py +14 -0
- mlgidGUI/app/fitting/utils.py +17 -0
- mlgidGUI/app/geometry.py +236 -0
- mlgidGUI/app/geometry_holder.py +131 -0
- mlgidGUI/app/image_holder.py +200 -0
- mlgidGUI/app/image_processing.py +23 -0
- mlgidGUI/app/polar_image.py +96 -0
- mlgidGUI/app/profiles/__init__.py +5 -0
- mlgidGUI/app/profiles/angular_profile.py +20 -0
- mlgidGUI/app/profiles/basic_profile.py +169 -0
- mlgidGUI/app/profiles/radial_profile.py +38 -0
- mlgidGUI/app/read_image.py +21 -0
- mlgidGUI/app/rois/__init__.py +4 -0
- mlgidGUI/app/rois/new_roi_dict.py +670 -0
- mlgidGUI/app/rois/roi.py +194 -0
- mlgidGUI/app/rois/roi_colors.py +76 -0
- mlgidGUI/app/rois/roi_data.py +177 -0
- mlgidGUI/app/rois/roi_dict.py +466 -0
- mlgidGUI/app/rois/roi_meta_data.py +149 -0
- mlgidGUI/app/structures/__init__.py +6 -0
- mlgidGUI/app/structures/crystal_ring.py +19 -0
- mlgidGUI/app/structures/crystals_holders.py +144 -0
- mlgidGUI/app/structures/custom_crystal.py +45 -0
- mlgidGUI/app/structures/get_ring_list.py +89 -0
- mlgidGUI/app/transformations.py +82 -0
- mlgidGUI/app/update/__init__.py +3 -0
- mlgidGUI/app/update/check_version.py +51 -0
- mlgidGUI/app/update/launch_program.py +15 -0
- mlgidGUI/app/update/script_update.py +64 -0
- mlgidGUI/app/utils.py +124 -0
- mlgidGUI/gui/__init__.py +3 -0
- mlgidGUI/gui/background_tasks.py +41 -0
- mlgidGUI/gui/background_update.py +87 -0
- mlgidGUI/gui/basic_widgets/__init__.py +12 -0
- mlgidGUI/gui/basic_widgets/buttons.py +144 -0
- mlgidGUI/gui/basic_widgets/labels.py +51 -0
- mlgidGUI/gui/basic_widgets/mouse_events.py +119 -0
- mlgidGUI/gui/basic_widgets/path_line.py +162 -0
- mlgidGUI/gui/basic_widgets/plots_1d.py +365 -0
- mlgidGUI/gui/basic_widgets/plots_2d.py +224 -0
- mlgidGUI/gui/basic_widgets/popups.py +164 -0
- mlgidGUI/gui/basic_widgets/progress_bar.py +127 -0
- mlgidGUI/gui/basic_widgets/setup_widgets.py +138 -0
- mlgidGUI/gui/basic_widgets/sliders.py +616 -0
- mlgidGUI/gui/basic_widgets/spinner.py +65 -0
- mlgidGUI/gui/basic_widgets/toolbars.py +19 -0
- mlgidGUI/gui/debug_widgets.py +388 -0
- mlgidGUI/gui/dock_area.py +115 -0
- mlgidGUI/gui/exception_message.py +42 -0
- mlgidGUI/gui/fitting/__init__.py +2 -0
- mlgidGUI/gui/fitting/fit_widget.py +966 -0
- mlgidGUI/gui/fitting/inplace_fit.py +536 -0
- mlgidGUI/gui/fitting/selected_fit_widget.py +286 -0
- mlgidGUI/gui/gui_main.py +349 -0
- mlgidGUI/gui/image_viewer.py +323 -0
- mlgidGUI/gui/init_window.py +170 -0
- mlgidGUI/gui/load_window/__init__.py +2 -0
- mlgidGUI/gui/load_window/load_folder.py +61 -0
- mlgidGUI/gui/load_window/load_h5.py +73 -0
- mlgidGUI/gui/notifications.py +68 -0
- mlgidGUI/gui/polar_image_viewer.py +153 -0
- mlgidGUI/gui/profiles/__init__.py +1 -0
- mlgidGUI/gui/profiles/angular_profile_widget.py +62 -0
- mlgidGUI/gui/profiles/radial_profile_widget.py +164 -0
- mlgidGUI/gui/project_manager/__init__.py +17 -0
- mlgidGUI/gui/project_manager/cif_files_widget.py +196 -0
- mlgidGUI/gui/project_manager/cif_results.py +146 -0
- mlgidGUI/gui/project_manager/metadata_widget.py +15 -0
- mlgidGUI/gui/project_manager/project_viewer.py +151 -0
- mlgidGUI/gui/project_manager/roi_widget.py +171 -0
- mlgidGUI/gui/roi_widgets/__init__.py +6 -0
- mlgidGUI/gui/roi_widgets/abstract_roi_holder.py +189 -0
- mlgidGUI/gui/roi_widgets/abstract_roi_widget.py +111 -0
- mlgidGUI/gui/roi_widgets/cif_file_widget.py +16 -0
- mlgidGUI/gui/roi_widgets/cif_peak_widget.py +43 -0
- mlgidGUI/gui/roi_widgets/confidence_level_widget.py +105 -0
- mlgidGUI/gui/roi_widgets/roi_1d_widget.py +136 -0
- mlgidGUI/gui/roi_widgets/roi_2d_rect_widget.py +98 -0
- mlgidGUI/gui/roi_widgets/roi_2d_ring_widget.py +203 -0
- mlgidGUI/gui/roi_widgets/rois_context_menu.py +150 -0
- mlgidGUI/gui/roi_widgets/select_roi_color_widget.py +58 -0
- mlgidGUI/gui/save_window/__init__.py +152 -0
- mlgidGUI/gui/save_window/format_box.py +33 -0
- mlgidGUI/gui/save_window/num_selected_widget.py +45 -0
- mlgidGUI/gui/save_window/options_widgets.py +105 -0
- mlgidGUI/gui/save_window/save_mode_box.py +30 -0
- mlgidGUI/gui/save_window/select_images.py +198 -0
- mlgidGUI/gui/tools.py +179 -0
- mlgidGUI/static/__init__.py +0 -0
- mlgidGUI/static/icons/__init__.py +0 -0
- mlgidGUI/static/icons/add.png +0 -0
- mlgidGUI/static/icons/add_yellow.png +0 -0
- mlgidGUI/static/icons/angular_profile.png +0 -0
- mlgidGUI/static/icons/arrow.png +0 -0
- mlgidGUI/static/icons/baseline.png +0 -0
- mlgidGUI/static/icons/center.png +0 -0
- mlgidGUI/static/icons/confirm.png +0 -0
- mlgidGUI/static/icons/control_widget.png +0 -0
- mlgidGUI/static/icons/data.png +0 -0
- mlgidGUI/static/icons/delete.png +0 -0
- mlgidGUI/static/icons/delete_yellow.png +0 -0
- mlgidGUI/static/icons/error.png +0 -0
- mlgidGUI/static/icons/find.png +0 -0
- mlgidGUI/static/icons/fit.png +0 -0
- mlgidGUI/static/icons/fix_all.png +0 -0
- mlgidGUI/static/icons/flip_horizontal.png +0 -0
- mlgidGUI/static/icons/flip_vertical.png +0 -0
- mlgidGUI/static/icons/folder.png +0 -0
- mlgidGUI/static/icons/fromfullscreen.png +0 -0
- mlgidGUI/static/icons/h5_folder.png +0 -0
- mlgidGUI/static/icons/h5_group_folder.png +0 -0
- mlgidGUI/static/icons/info.png +0 -0
- mlgidGUI/static/icons/interpolate.png +0 -0
- mlgidGUI/static/icons/item.png +0 -0
- mlgidGUI/static/icons/properties.png +0 -0
- mlgidGUI/static/icons/radial_profile.png +0 -0
- mlgidGUI/static/icons/roi_item.png +0 -0
- mlgidGUI/static/icons/rotate.png +0 -0
- mlgidGUI/static/icons/rotate_image.png +0 -0
- mlgidGUI/static/icons/rotate_right.png +0 -0
- mlgidGUI/static/icons/setup.png +0 -0
- mlgidGUI/static/icons/setup_white.png +0 -0
- mlgidGUI/static/icons/tofullscreen.png +0 -0
- mlgidGUI/static/icons/unfix_all.png +0 -0
- mlgidGUI/static/icons/update.png +0 -0
- mlgidGUI/static/icons/update_suggested.png +0 -0
- mlgidGUI/static/icons/window_icon.png +0 -0
- mlgidGUI/static/icons/window_icon_256.png +0 -0
- mlgidGUI/utils/__init__.py +1 -0
- mlgidGUI/utils/project2h5.py +11 -0
- mlgidgui-0.4.1.dist-info/METADATA +119 -0
- mlgidgui-0.4.1.dist-info/RECORD +202 -0
- mlgidgui-0.4.1.dist-info/WHEEL +5 -0
- mlgidgui-0.4.1.dist-info/entry_points.txt +5 -0
- mlgidgui-0.4.1.dist-info/licenses/LICENSE +674 -0
- mlgidgui-0.4.1.dist-info/top_level.txt +4 -0
- tests/__init__.py +0 -0
- tests/conftest.py +1 -0
- tests/fixures/__init__.py +2 -0
- tests/fixures/projects.py +102 -0
- tests/func/__init__.py +1 -0
- tests/func/test_loading_state.py +21 -0
- tests/func/test_parse_project_data.py +23 -0
- tests/func/test_save.py +62 -0
- tests/unit/__init__.py +0 -0
- tests/unit/rois/__init__.py +0 -0
- tests/unit/rois/test_basic_operations.py +18 -0
- tests/unit/test_add_data.py +33 -0
mlgidGUI/app/rois/roi.py
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
from collections import OrderedDict
|
|
2
|
+
from dataclasses import dataclass, field
|
|
3
|
+
from enum import Enum
|
|
4
|
+
from typing import Tuple, List
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
class PostponedImport(object):
|
|
8
|
+
def __init__(self):
|
|
9
|
+
self._module = None
|
|
10
|
+
|
|
11
|
+
@classmethod
|
|
12
|
+
def init_module(cls):
|
|
13
|
+
pass
|
|
14
|
+
|
|
15
|
+
@classmethod
|
|
16
|
+
def __getattr__(cls, item):
|
|
17
|
+
if not hasattr(cls, '_module'):
|
|
18
|
+
cls.init_module()
|
|
19
|
+
return cls._module.__getattr__(item)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class Fit(PostponedImport):
|
|
23
|
+
@classmethod
|
|
24
|
+
def init_module(cls):
|
|
25
|
+
import mlgidGUI.app.fitting as fit
|
|
26
|
+
cls._module = fit
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class RoiTypes(Enum):
|
|
30
|
+
ring = 1
|
|
31
|
+
segment = 2
|
|
32
|
+
background = 3
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
# (type, selected, fixed): (r, g, b)
|
|
36
|
+
ROI_COLOR_KEY = Tuple[RoiTypes, bool, bool]
|
|
37
|
+
|
|
38
|
+
_CONFIDENCE_LEVELS = OrderedDict([
|
|
39
|
+
('High', 1.),
|
|
40
|
+
('Medium', 0.5),
|
|
41
|
+
('Low', 0.1),
|
|
42
|
+
('Not set', -1.),
|
|
43
|
+
])
|
|
44
|
+
|
|
45
|
+
_DEFAULT_CONFIDENCE_LEVEL = 'Not set'
|
|
46
|
+
|
|
47
|
+
ROI_DICT_KEYS = (
|
|
48
|
+
'radius',
|
|
49
|
+
'width',
|
|
50
|
+
'angle',
|
|
51
|
+
'angle_std',
|
|
52
|
+
'key',
|
|
53
|
+
'type',
|
|
54
|
+
'confidence_level',
|
|
55
|
+
'cif_file'
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
DTYPES = [
|
|
59
|
+
('radius', 'f4'),
|
|
60
|
+
('width', 'f4'),
|
|
61
|
+
('angle', 'f4'),
|
|
62
|
+
('angle_std', 'f4'),
|
|
63
|
+
('key', 'i4'),
|
|
64
|
+
('type', 'i4'),
|
|
65
|
+
('confidence_level', 'f4'),
|
|
66
|
+
('cif_file', '|S30')
|
|
67
|
+
]
|
|
68
|
+
|
|
69
|
+
_ROI_NAMES = list(map(lambda x: x[0], DTYPES))
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@dataclass
|
|
73
|
+
class Roi:
|
|
74
|
+
radius: float
|
|
75
|
+
width: float
|
|
76
|
+
angle: float = 180
|
|
77
|
+
angle_std: float = 360
|
|
78
|
+
key: int = None
|
|
79
|
+
name: str = ''
|
|
80
|
+
group: str = ''
|
|
81
|
+
type: RoiTypes = RoiTypes.ring
|
|
82
|
+
movable: bool = True
|
|
83
|
+
fitted_parameters: dict = None
|
|
84
|
+
active: bool = False
|
|
85
|
+
deleted: bool = False
|
|
86
|
+
confidence_level: float = -1.
|
|
87
|
+
cif_file: str = 'not set'
|
|
88
|
+
|
|
89
|
+
CONFIDENCE_LEVELS = _CONFIDENCE_LEVELS
|
|
90
|
+
DEFAULT_CONFIDENCE_LEVEL = _DEFAULT_CONFIDENCE_LEVEL
|
|
91
|
+
|
|
92
|
+
@staticmethod
|
|
93
|
+
def confidence_name2level(name: str):
|
|
94
|
+
if name not in _CONFIDENCE_LEVELS.keys():
|
|
95
|
+
return _CONFIDENCE_LEVELS[_DEFAULT_CONFIDENCE_LEVEL]
|
|
96
|
+
for level_name, level in _CONFIDENCE_LEVELS.items():
|
|
97
|
+
if name == level_name:
|
|
98
|
+
return level
|
|
99
|
+
|
|
100
|
+
@staticmethod
|
|
101
|
+
def confidence_level2name(level: float):
|
|
102
|
+
values = np.array(list(_CONFIDENCE_LEVELS.values()))
|
|
103
|
+
idx = np.argmin(np.abs(level - values))
|
|
104
|
+
return list(_CONFIDENCE_LEVELS.items())[idx][0]
|
|
105
|
+
|
|
106
|
+
@property
|
|
107
|
+
def confidence_level_name(self):
|
|
108
|
+
return self.confidence_level2name(self.confidence_level)
|
|
109
|
+
|
|
110
|
+
def set_confidence_level(self, level: float):
|
|
111
|
+
self.confidence_level = level
|
|
112
|
+
|
|
113
|
+
def update(self, other: 'Roi'):
|
|
114
|
+
self.__dict__ = other.__dict__
|
|
115
|
+
|
|
116
|
+
def to_array(self) -> np.ndarray:
|
|
117
|
+
return np.array([self.radius, self.width, self.angle, self.angle_std,
|
|
118
|
+
self.key, self.type.value])
|
|
119
|
+
|
|
120
|
+
@classmethod
|
|
121
|
+
def from_array(cls, arr: np.ndarray, **meta_data):
|
|
122
|
+
roi = cls(**dict(zip(_ROI_NAMES, arr)), **meta_data)
|
|
123
|
+
roi.type = RoiTypes(roi.type)
|
|
124
|
+
return roi
|
|
125
|
+
|
|
126
|
+
def should_adjust_angles(self, angle: float, angle_std: float) -> bool:
|
|
127
|
+
return (
|
|
128
|
+
self.type == RoiTypes.ring or
|
|
129
|
+
self.type == RoiTypes.background
|
|
130
|
+
) and (
|
|
131
|
+
self.angle != angle or self.angle_std != angle_std
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
def has_fixed_angles(self) -> bool:
|
|
135
|
+
return self.type == RoiTypes.segment
|
|
136
|
+
|
|
137
|
+
@property
|
|
138
|
+
def intensity(self) -> float or None:
|
|
139
|
+
if self.is_fitted:
|
|
140
|
+
return self.fitted_parameters.get('peak height', None)
|
|
141
|
+
|
|
142
|
+
@property
|
|
143
|
+
def is_fitted(self) -> bool:
|
|
144
|
+
return bool(self.fitted_parameters)
|
|
145
|
+
|
|
146
|
+
@property
|
|
147
|
+
def color_key(self):
|
|
148
|
+
return self.type, self.active, not self.movable
|
|
149
|
+
|
|
150
|
+
def to_dict(self) -> dict:
|
|
151
|
+
roi_dict = dict(self.fitted_parameters or {})
|
|
152
|
+
roi_dict.update({
|
|
153
|
+
key: getattr(self, key) for key in ROI_DICT_KEYS
|
|
154
|
+
})
|
|
155
|
+
roi_dict['type'] = roi_dict['type'].value
|
|
156
|
+
return roi_dict
|
|
157
|
+
|
|
158
|
+
@classmethod
|
|
159
|
+
def from_dict(cls, roi_dict: dict, name, **kwargs):
|
|
160
|
+
cls_params = {key: roi_dict[key] for key in ROI_DICT_KEYS if key in roi_dict}
|
|
161
|
+
cls_params['key'] = int(name)
|
|
162
|
+
cls_params['name'] = str(name)
|
|
163
|
+
try:
|
|
164
|
+
cls_params['cif_file'] = roi_dict['cif_file'].decode('UTF-8')
|
|
165
|
+
except:
|
|
166
|
+
pass
|
|
167
|
+
cls_params['type'] = RoiTypes(cls_params.get('type', 1))
|
|
168
|
+
|
|
169
|
+
if 'fitting_function' in roi_dict:
|
|
170
|
+
fit_func_name = roi_dict['fitting_function']
|
|
171
|
+
fit_func = Fit.FITTING_FUNCTIONS[Fit.FittingType(fit_func_name)]
|
|
172
|
+
fit_param_keys = fit_func.PARAM_NAMES
|
|
173
|
+
fitted_parameters = {'fitting_function': fit_func_name}
|
|
174
|
+
fitted_parameters.update({p: roi_dict[p] for p in fit_param_keys if p in roi_dict})
|
|
175
|
+
cls_params['fitted_parameters'] = fitted_parameters
|
|
176
|
+
|
|
177
|
+
return cls(**cls_params, **kwargs)
|
|
178
|
+
|
|
179
|
+
def roi_to_tuple(roi: Roi):
|
|
180
|
+
return (roi.radius, roi.width, roi.angle, roi.angle_std,
|
|
181
|
+
roi.key, roi.name, roi.group, roi.type.value)
|
|
182
|
+
|
|
183
|
+
@dataclass
|
|
184
|
+
class CIFPeakRoi(Roi):
|
|
185
|
+
cif_file_nr: int = 0
|
|
186
|
+
cif_file_name: str = ''
|
|
187
|
+
is_powder: bool = False
|
|
188
|
+
q_xy: float = 0
|
|
189
|
+
q_z: float = 0
|
|
190
|
+
miller_id: List[int] = field(default_factory=list)
|
|
191
|
+
intensity: int = 0
|
|
192
|
+
intensity_raw: int = 0
|
|
193
|
+
intensity_perc: float = 0
|
|
194
|
+
roi_widget = None
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
from typing import Dict, Tuple
|
|
2
|
+
|
|
3
|
+
from PyQt5.QtCore import QObject, pyqtSignal
|
|
4
|
+
|
|
5
|
+
from ..file_manager import FileManager
|
|
6
|
+
from .roi import RoiTypes
|
|
7
|
+
|
|
8
|
+
SELECTED = True
|
|
9
|
+
NOT_SELECTED = False
|
|
10
|
+
FIXED = True
|
|
11
|
+
NOT_FIXED = False
|
|
12
|
+
|
|
13
|
+
# (type, selected, fixed): (r, g, b)
|
|
14
|
+
ROI_COLOR_KEY = Tuple[RoiTypes, bool, bool]
|
|
15
|
+
RGB = Tuple[int, int, int]
|
|
16
|
+
ROI_COLORS = Dict[ROI_COLOR_KEY, RGB]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
_DEFAULT_ROI_COLORS: ROI_COLORS = {
|
|
20
|
+
(RoiTypes.ring, NOT_SELECTED, NOT_FIXED): (0, 0, 255),
|
|
21
|
+
(RoiTypes.ring, SELECTED, NOT_FIXED): (0, 128, 255),
|
|
22
|
+
(RoiTypes.ring, NOT_SELECTED, FIXED): (0, 0, 255),
|
|
23
|
+
(RoiTypes.ring, SELECTED, FIXED): (255, 0, 255),
|
|
24
|
+
|
|
25
|
+
(RoiTypes.segment, NOT_SELECTED, NOT_FIXED): (0, 0, 255),
|
|
26
|
+
(RoiTypes.segment, SELECTED, NOT_FIXED): (0, 128, 255),
|
|
27
|
+
(RoiTypes.segment, NOT_SELECTED, FIXED): (0, 0, 255),
|
|
28
|
+
(RoiTypes.segment, SELECTED, FIXED): (255, 0, 255),
|
|
29
|
+
|
|
30
|
+
(RoiTypes.background, NOT_SELECTED, NOT_FIXED): (0, 0, 255),
|
|
31
|
+
(RoiTypes.background, SELECTED, NOT_FIXED): (0, 128, 255),
|
|
32
|
+
(RoiTypes.background, NOT_SELECTED, FIXED): (0, 0, 255),
|
|
33
|
+
(RoiTypes.background, SELECTED, FIXED): (255, 0, 255),
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class RoiColorsDict(dict):
|
|
38
|
+
def __init__(self, colors: ROI_COLORS = None):
|
|
39
|
+
super().__init__()
|
|
40
|
+
self.update(_DEFAULT_ROI_COLORS)
|
|
41
|
+
|
|
42
|
+
if colors:
|
|
43
|
+
self.update(colors)
|
|
44
|
+
|
|
45
|
+
def reset(self):
|
|
46
|
+
self.update(_DEFAULT_ROI_COLORS)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class RoiColors(QObject):
|
|
50
|
+
_SAVE_KEY = 'roi_colors'
|
|
51
|
+
|
|
52
|
+
sigColorChanged = pyqtSignal(tuple)
|
|
53
|
+
sigColorDictSet = pyqtSignal()
|
|
54
|
+
|
|
55
|
+
def __init__(self, fm: FileManager, color_dict: RoiColorsDict = None, parent=None):
|
|
56
|
+
super().__init__(parent)
|
|
57
|
+
self._fm = fm
|
|
58
|
+
self._color_dict: RoiColorsDict = color_dict or RoiColorsDict(fm.config[self._SAVE_KEY])
|
|
59
|
+
|
|
60
|
+
def save_colors(self):
|
|
61
|
+
self._fm.config[self._SAVE_KEY] = self._color_dict
|
|
62
|
+
|
|
63
|
+
def set_color(self, key: ROI_COLOR_KEY, color: RGB):
|
|
64
|
+
if color != self._color_dict[key]:
|
|
65
|
+
self._color_dict[key] = color
|
|
66
|
+
self.sigColorChanged.emit(key)
|
|
67
|
+
|
|
68
|
+
def __getitem__(self, item):
|
|
69
|
+
return self._color_dict[item]
|
|
70
|
+
|
|
71
|
+
def __setitem__(self, key: ROI_COLOR_KEY, color: RGB):
|
|
72
|
+
self.set_color(key, color)
|
|
73
|
+
|
|
74
|
+
def reset(self):
|
|
75
|
+
self._color_dict.reset()
|
|
76
|
+
self.sigColorDictSet.emit()
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
from typing import List, Tuple, Dict
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
from .roi import Roi, RoiTypes
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class RoiData(dict):
|
|
9
|
+
def __init__(self, rois: List[Roi] = None):
|
|
10
|
+
super().__init__()
|
|
11
|
+
self._selected_keys = set()
|
|
12
|
+
if rois:
|
|
13
|
+
self.add_rois(rois)
|
|
14
|
+
|
|
15
|
+
@property
|
|
16
|
+
def selected_keys(self):
|
|
17
|
+
yield from self._selected_keys
|
|
18
|
+
|
|
19
|
+
@property
|
|
20
|
+
def selected_num(self):
|
|
21
|
+
return len(self._selected_keys)
|
|
22
|
+
|
|
23
|
+
def select(self, key: int) -> Tuple:
|
|
24
|
+
if len(self._selected_keys) == 1 and key in self._selected_keys:
|
|
25
|
+
return ()
|
|
26
|
+
try:
|
|
27
|
+
for k in self._selected_keys:
|
|
28
|
+
self[k].active = False
|
|
29
|
+
self[key].active = True
|
|
30
|
+
self._selected_keys.add(key)
|
|
31
|
+
change_select = tuple(self._selected_keys)
|
|
32
|
+
self._selected_keys = {key}
|
|
33
|
+
return change_select
|
|
34
|
+
|
|
35
|
+
except KeyError:
|
|
36
|
+
raise ValueError(f'The selected roi {key} is missing.')
|
|
37
|
+
|
|
38
|
+
def shift_select(self, key: int):
|
|
39
|
+
try:
|
|
40
|
+
self[key].active = not self[key].active
|
|
41
|
+
if self[key].active:
|
|
42
|
+
self._selected_keys.add(key)
|
|
43
|
+
else:
|
|
44
|
+
try:
|
|
45
|
+
self._selected_keys.remove(key)
|
|
46
|
+
except KeyError:
|
|
47
|
+
pass
|
|
48
|
+
except KeyError:
|
|
49
|
+
raise ValueError(f'The selected roi {key} is missing.')
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def selected_rois(self) -> List[Roi]:
|
|
53
|
+
return [self[i] for i in self._selected_keys]
|
|
54
|
+
|
|
55
|
+
def select_all(self) -> List[int]:
|
|
56
|
+
to_select = list()
|
|
57
|
+
for roi in self.values():
|
|
58
|
+
if not roi.active:
|
|
59
|
+
roi.active = True
|
|
60
|
+
to_select.append(roi.key)
|
|
61
|
+
self._selected_keys = set(self.keys())
|
|
62
|
+
return to_select
|
|
63
|
+
|
|
64
|
+
def unselect_all(self) -> List[int]:
|
|
65
|
+
to_unselect = list()
|
|
66
|
+
for roi in self.values():
|
|
67
|
+
if roi.active:
|
|
68
|
+
roi.active = False
|
|
69
|
+
to_unselect.append(roi.key)
|
|
70
|
+
self._selected_keys = set()
|
|
71
|
+
return to_unselect
|
|
72
|
+
|
|
73
|
+
def add_roi(self, roi: Roi) -> None:
|
|
74
|
+
self[roi.key] = roi
|
|
75
|
+
|
|
76
|
+
if roi.active:
|
|
77
|
+
self._selected_keys.add(roi.key)
|
|
78
|
+
|
|
79
|
+
def add_rois(self, roi_list: List[Roi]) -> List[int]:
|
|
80
|
+
keys = list()
|
|
81
|
+
to_select = list()
|
|
82
|
+
for roi in roi_list:
|
|
83
|
+
self[roi.key] = roi
|
|
84
|
+
if roi.active:
|
|
85
|
+
self._selected_keys.add(roi.key)
|
|
86
|
+
to_select.append(roi.key)
|
|
87
|
+
keys.append(roi.key)
|
|
88
|
+
return to_select
|
|
89
|
+
|
|
90
|
+
def delete_roi(self, k: int):
|
|
91
|
+
del self[k]
|
|
92
|
+
|
|
93
|
+
if k in self._selected_keys:
|
|
94
|
+
self._selected_keys.remove(k)
|
|
95
|
+
|
|
96
|
+
def clear(self):
|
|
97
|
+
super().clear()
|
|
98
|
+
self._selected_keys = set()
|
|
99
|
+
|
|
100
|
+
def change_ring_bounds(self, bounds: Tuple[float, float]) -> List[int]:
|
|
101
|
+
angle, angle_std = bounds
|
|
102
|
+
keys = list()
|
|
103
|
+
for k, roi in self.items():
|
|
104
|
+
if roi.should_adjust_angles(angle, angle_std):
|
|
105
|
+
keys.append(k)
|
|
106
|
+
roi.angle, roi.angle_std = bounds
|
|
107
|
+
return keys
|
|
108
|
+
|
|
109
|
+
def on_scale_changed(self, scale_change: float):
|
|
110
|
+
for roi in self.values():
|
|
111
|
+
roi.radius *= scale_change
|
|
112
|
+
roi.width *= scale_change
|
|
113
|
+
|
|
114
|
+
def apply_fit(self, rois: List[Roi]):
|
|
115
|
+
keys_to_move = []
|
|
116
|
+
rois_to_create = []
|
|
117
|
+
|
|
118
|
+
for roi in rois:
|
|
119
|
+
roi.active = True
|
|
120
|
+
roi.movable = False
|
|
121
|
+
|
|
122
|
+
if roi.key in self.keys():
|
|
123
|
+
keys_to_move.append(roi.key)
|
|
124
|
+
self[roi.key].update(roi)
|
|
125
|
+
self.select(roi.key)
|
|
126
|
+
else:
|
|
127
|
+
rois_to_create.append(roi)
|
|
128
|
+
|
|
129
|
+
self.add_rois(rois_to_create)
|
|
130
|
+
keys_to_create = [roi.key for roi in rois_to_create]
|
|
131
|
+
|
|
132
|
+
return tuple(keys_to_create), tuple(keys_to_move)
|
|
133
|
+
|
|
134
|
+
def to_array(self) -> np.ndarray:
|
|
135
|
+
return np.array([roi.to_array() for roi in self.values()])
|
|
136
|
+
|
|
137
|
+
@classmethod
|
|
138
|
+
def from_array(cls, arr: np.ndarray):
|
|
139
|
+
return cls([Roi.from_array(a) for a in arr])
|
|
140
|
+
|
|
141
|
+
def to_dict(self) -> Dict[str, np.ndarray]:
|
|
142
|
+
roi_dicts = [roi.to_dict() for roi in self.values()]
|
|
143
|
+
keys = set().union(*[d.keys() for d in roi_dicts])
|
|
144
|
+
# if not all peaks are fitted, save without fitting parameters
|
|
145
|
+
#if not np.all(np.array(['fitted_params' in d.keys() for d in roi_dicts])):
|
|
146
|
+
keys -= { 'background', 'background (level)', 'background (slope)', 'fit_errors', 'fitted_params', 'fitting_function', 'init_params', 'lower_bounds', 'peak height', 'r_range', 'upper_bounds', 'x_range'}
|
|
147
|
+
return_dict = {}
|
|
148
|
+
for k in keys:
|
|
149
|
+
if type(roi_dicts[0].get(k, np.nan)).__name__ == 'str':
|
|
150
|
+
return_dict[k] = np.array([d.get(k, np.nan) for d in roi_dicts],dtype='S30')
|
|
151
|
+
else:
|
|
152
|
+
return_dict[k] = np.array([d.get(k, np.nan) for d in roi_dicts])
|
|
153
|
+
return return_dict
|
|
154
|
+
|
|
155
|
+
@classmethod
|
|
156
|
+
def from_dict(cls, arr_dict: Dict[str, np.ndarray]):
|
|
157
|
+
keys = list(arr_dict.keys())
|
|
158
|
+
if not keys:
|
|
159
|
+
return cls()
|
|
160
|
+
num_rois = len(arr_dict[keys[0]])
|
|
161
|
+
rois = [
|
|
162
|
+
Roi.from_dict({k: arr_dict[k][i] for k in arr_dict.keys()}, name = i)
|
|
163
|
+
for i in range(num_rois)
|
|
164
|
+
]
|
|
165
|
+
return cls(rois)
|
|
166
|
+
|
|
167
|
+
@property
|
|
168
|
+
def intensities(self):
|
|
169
|
+
return np.array([roi.intensity for roi in self.values()], dtype=float)
|
|
170
|
+
|
|
171
|
+
@property
|
|
172
|
+
def confidence_levels(self):
|
|
173
|
+
return np.array([roi.confidence_level for roi in self.values()], dtype=float)
|
|
174
|
+
|
|
175
|
+
@property
|
|
176
|
+
def cif_files(self):
|
|
177
|
+
return np.array([roi.cif_file for roi in self.values()], dtype=str)
|