1 | ################################################################################ |
---|
2 | #This software was developed by the University of Tennessee as part of the |
---|
3 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
4 | #project funded by the US National Science Foundation. |
---|
5 | # |
---|
6 | #See the license text in license.txt |
---|
7 | # |
---|
8 | #copyright 2010, University of Tennessee |
---|
9 | ################################################################################ |
---|
10 | """ |
---|
11 | This module manages all data loaded into the application. Data_manager makes |
---|
12 | available all data loaded for the current perspective. |
---|
13 | |
---|
14 | All modules "creating Data" posts their data to data_manager . |
---|
15 | Data_manager make these new data available for all other perspectives. |
---|
16 | """ |
---|
17 | import logging |
---|
18 | import os |
---|
19 | import copy |
---|
20 | |
---|
21 | from sans.guiframe.data_state import DataState |
---|
22 | from sans.guiframe.utils import parse_name |
---|
23 | import sans.dataloader.data_info as DataInfo |
---|
24 | from sans.guiframe.dataFitting import Data1D |
---|
25 | from sans.guiframe.dataFitting import Data2D |
---|
26 | import time |
---|
27 | |
---|
28 | class DataManager(object): |
---|
29 | """ |
---|
30 | Manage a list of data |
---|
31 | """ |
---|
32 | def __init__(self): |
---|
33 | """ |
---|
34 | Store opened path and data object created at the loading time |
---|
35 | :param auto_plot: if True the datamanager sends data to plotting |
---|
36 | plugin. |
---|
37 | :param auto_set_data: if True the datamanager sends to the current |
---|
38 | perspective |
---|
39 | """ |
---|
40 | self.stored_data = {} |
---|
41 | self.message = "" |
---|
42 | self.data_name_dict = {} |
---|
43 | self.count = 0 |
---|
44 | self.list_of_id = [] |
---|
45 | self.time_stamp = time.time() |
---|
46 | |
---|
47 | def __str__(self): |
---|
48 | _str = "" |
---|
49 | _str += "No of states is %s \n" % str(len(self.stored_data)) |
---|
50 | n_count = 0 |
---|
51 | for value in self.stored_data.values(): |
---|
52 | n_count += 1 |
---|
53 | _str += "State No %s \n" % str(n_count) |
---|
54 | _str += str(value) + "\n" |
---|
55 | return _str |
---|
56 | |
---|
57 | def create_gui_data(self, data, path=None): |
---|
58 | """ |
---|
59 | Receive data from loader and create a data to use for guiframe |
---|
60 | """ |
---|
61 | |
---|
62 | if issubclass(Data2D, data.__class__): |
---|
63 | new_plot = Data2D(image=None, err_image=None) |
---|
64 | else: |
---|
65 | new_plot = Data1D(x=[], y=[], dx=None, dy=None) |
---|
66 | |
---|
67 | new_plot.copy_from_datainfo(data) |
---|
68 | data.clone_without_data(clone=new_plot) |
---|
69 | #creating a name for data |
---|
70 | name = "" |
---|
71 | title = "" |
---|
72 | file_name = "" |
---|
73 | if path is not None: |
---|
74 | file_name = os.path.basename(path) |
---|
75 | if file_name == "": |
---|
76 | file_name = data.filename |
---|
77 | if data.run: |
---|
78 | run_num = data.run[0] |
---|
79 | name = file_name |
---|
80 | if name == "": |
---|
81 | name = run_num |
---|
82 | name = self.rename(name) |
---|
83 | #find title |
---|
84 | if data.title.strip(): |
---|
85 | title = data.title |
---|
86 | if title.strip() == "": |
---|
87 | title = file_name |
---|
88 | |
---|
89 | if new_plot.filename.strip() == "": |
---|
90 | new_plot.filename = file_name |
---|
91 | |
---|
92 | new_plot.name = name |
---|
93 | new_plot.title = title |
---|
94 | ## allow to highlight data when plotted |
---|
95 | new_plot.interactive = True |
---|
96 | ## when 2 data have the same id override the 1 st plotted |
---|
97 | self.time_stamp += 1 |
---|
98 | new_plot.id = str(name) + str(self.time_stamp) |
---|
99 | ##group_id specify on which panel to plot this data |
---|
100 | new_plot.group_id = str(name) + str(self.time_stamp) |
---|
101 | new_plot.is_data = True |
---|
102 | new_plot.path = path |
---|
103 | new_plot.list_group_id = [] |
---|
104 | ##post data to plot |
---|
105 | # plot data |
---|
106 | return new_plot |
---|
107 | |
---|
108 | def rename(self, name): |
---|
109 | """ |
---|
110 | rename data |
---|
111 | """ |
---|
112 | ## name of the data allow to differentiate data when plotted |
---|
113 | name = parse_name(name=name, expression="_") |
---|
114 | |
---|
115 | max_char = name.find("[") |
---|
116 | if max_char < 0: |
---|
117 | max_char = len(name) |
---|
118 | name = name[0:max_char] |
---|
119 | |
---|
120 | if name not in self.data_name_dict: |
---|
121 | self.data_name_dict[name] = 0 |
---|
122 | else: |
---|
123 | self.data_name_dict[name] += 1 |
---|
124 | name = name + " [" + str(self.data_name_dict[name]) + "]" |
---|
125 | return name |
---|
126 | |
---|
127 | |
---|
128 | def add_data(self, data_list): |
---|
129 | """ |
---|
130 | receive a list of |
---|
131 | """ |
---|
132 | for id, data in data_list.iteritems(): |
---|
133 | if id in self.stored_data: |
---|
134 | msg = "Data manager already stores %s" % str(data.name) |
---|
135 | msg += "" |
---|
136 | logging.info(msg) |
---|
137 | data_state = self.stored_data[id] |
---|
138 | data_state.data = data |
---|
139 | else: |
---|
140 | data_state = DataState(data) |
---|
141 | data_state.id = id |
---|
142 | self.stored_data[id] = data_state |
---|
143 | |
---|
144 | def update_data(self, prev_data, new_data): |
---|
145 | """ |
---|
146 | """ |
---|
147 | if prev_data.id not in self.stored_data.keys(): |
---|
148 | return None, {} |
---|
149 | data_state = self.stored_data[prev_data.id] |
---|
150 | self.stored_data[new_data.id] = data_state.clone() |
---|
151 | self.stored_data[new_data.id].data = new_data |
---|
152 | if prev_data.id in self.stored_data.keys(): |
---|
153 | del self.stored_data[prev_data.id] |
---|
154 | return prev_data.id, {new_data.id: self.stored_data[new_data.id]} |
---|
155 | |
---|
156 | def update_theory(self, theory, data_id=None, state=None): |
---|
157 | """ |
---|
158 | """ |
---|
159 | uid = data_id |
---|
160 | if data_id is None and theory is not None: |
---|
161 | uid = theory.id |
---|
162 | if uid in self.stored_data.keys(): |
---|
163 | data_state = self.stored_data[uid] |
---|
164 | else: |
---|
165 | data_state = DataState() |
---|
166 | data_state.uid = uid |
---|
167 | data_state.set_theory(theory_data=theory, theory_state=state) |
---|
168 | self.stored_data[uid] = data_state |
---|
169 | return {uid: self.stored_data[uid]} |
---|
170 | |
---|
171 | |
---|
172 | def get_message(self): |
---|
173 | """ |
---|
174 | return message |
---|
175 | """ |
---|
176 | return self.message |
---|
177 | |
---|
178 | def get_by_id(self, id_list=None): |
---|
179 | """ |
---|
180 | """ |
---|
181 | _selected_data = {} |
---|
182 | _selected_theory_list = {} |
---|
183 | if id_list is None: |
---|
184 | return |
---|
185 | for d_id in self.stored_data: |
---|
186 | for search_id in id_list: |
---|
187 | data_state = self.stored_data[d_id] |
---|
188 | data = data_state.data |
---|
189 | theory_list = data_state.get_theory() |
---|
190 | if search_id == d_id: |
---|
191 | _selected_data[search_id] = data |
---|
192 | if search_id in theory_list.keys(): |
---|
193 | _selected_theory_list[search_id] = theory_list[search_id] |
---|
194 | |
---|
195 | return _selected_data, _selected_theory_list |
---|
196 | |
---|
197 | |
---|
198 | def freeze(self, theory_id): |
---|
199 | """ |
---|
200 | """ |
---|
201 | return self.freeze_theory(self.stored_data.keys(), theory_id) |
---|
202 | |
---|
203 | def freeze_theory(self, data_id, theory_id): |
---|
204 | """ |
---|
205 | """ |
---|
206 | selected_theory = {} |
---|
207 | for d_id in data_id: |
---|
208 | if d_id in self.stored_data: |
---|
209 | data_state = self.stored_data[d_id] |
---|
210 | theory_list = data_state.get_theory() |
---|
211 | for t_id in theory_id: |
---|
212 | if t_id in theory_list.keys(): |
---|
213 | theory_data, theory_state = theory_list[t_id] |
---|
214 | new_theory = copy.deepcopy(theory_data) |
---|
215 | new_theory.id = time.time() |
---|
216 | new_theory.is_data = True |
---|
217 | selected_theory[new_theory.id] = DataState(new_theory) |
---|
218 | self.stored_data[new_theory.id] = selected_theory[new_theory.id] |
---|
219 | |
---|
220 | return selected_theory |
---|
221 | |
---|
222 | |
---|
223 | def delete_data(self, data_id, theory_id=None, delete_all=False): |
---|
224 | """ |
---|
225 | """ |
---|
226 | for d_id in data_id: |
---|
227 | if d_id in self.stored_data.keys(): |
---|
228 | data_state = self.stored_data[d_id] |
---|
229 | if data_state.data.name in self.data_name_dict: |
---|
230 | del self.data_name_dict[data_state.data.name] |
---|
231 | del self.stored_data[d_id] |
---|
232 | |
---|
233 | self.delete_theory(data_id, theory_id) |
---|
234 | if delete_all: |
---|
235 | self.stored_data = {} |
---|
236 | self.data_name_dict = {} |
---|
237 | |
---|
238 | def delete_theory(self, data_id, theory_id): |
---|
239 | """ |
---|
240 | """ |
---|
241 | for d_id in data_id: |
---|
242 | if d_id in self.stored_data: |
---|
243 | data_state = self.stored_data[d_id] |
---|
244 | theory_list = data_state.get_theory() |
---|
245 | if theory_id in theory_list.keys(): |
---|
246 | del theory_list[theory_id] |
---|
247 | |
---|
248 | |
---|
249 | def delete_by_id(self, id_list=None): |
---|
250 | """ |
---|
251 | save data and path |
---|
252 | """ |
---|
253 | for id in id_list: |
---|
254 | if id in self.stored_data: |
---|
255 | del self.stored_data[id] |
---|
256 | |
---|
257 | |
---|
258 | def get_by_name(self, name_list=None): |
---|
259 | """ |
---|
260 | return a list of data given a list of data names |
---|
261 | """ |
---|
262 | _selected_data = {} |
---|
263 | for selected_name in name_list: |
---|
264 | for id, data_state in self.stored_data.iteritems(): |
---|
265 | if data_state.data.name == selected_name: |
---|
266 | _selected_data[id] = data_state.data |
---|
267 | return _selected_data |
---|
268 | |
---|
269 | def delete_by_name(self, name_list=None): |
---|
270 | """ |
---|
271 | save data and path |
---|
272 | """ |
---|
273 | for selected_name in name_list: |
---|
274 | for id, data_state in self.stored_data.iteritems(): |
---|
275 | if data_state.data.name == selected_name: |
---|
276 | del self._selected_data[id] |
---|
277 | del self.stored_data[data.id] |
---|
278 | |
---|
279 | def get_data_state(self, data_id): |
---|
280 | """ |
---|
281 | Send list of selected data |
---|
282 | """ |
---|
283 | _selected_data_state = {} |
---|
284 | for id in data_id: |
---|
285 | if id in self.stored_data.keys(): |
---|
286 | _selected_data_state[id] = self.stored_data[id] |
---|
287 | return _selected_data_state |
---|
288 | |
---|
289 | def get_all_data(self): |
---|
290 | """ |
---|
291 | return list of all available data |
---|
292 | """ |
---|
293 | return self.stored_data |
---|
294 | |
---|
295 | |
---|
296 | |
---|