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 sas.sasgui.guiframe.data_state import DataState |
---|
22 | from sas.sasgui.guiframe.utils import parse_name |
---|
23 | import sas.sascalc.dataloader.data_info as DataInfo |
---|
24 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
25 | from sas.sasgui.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 | elif data.meta_data['loader'] == 'SESANS': |
---|
65 | new_plot = Data1D(x=[], y=[], dx=None, dy=None, lam=None, dlam=None, isSesans=True) |
---|
66 | else: |
---|
67 | new_plot = Data1D(x=[], y=[], dx=None, dy=None, lam=None, dlam=None) #SESANS check??? |
---|
68 | |
---|
69 | new_plot.copy_from_datainfo(data) |
---|
70 | data.clone_without_data(clone=new_plot) |
---|
71 | #creating a name for data |
---|
72 | title = "" |
---|
73 | file_name = os.path.basename(path) if path is not None else data.filename |
---|
74 | if file_name: |
---|
75 | name = file_name |
---|
76 | elif data.run: |
---|
77 | name = data.run[0] |
---|
78 | else: |
---|
79 | name = "data" |
---|
80 | name = self.rename(name) |
---|
81 | #find title |
---|
82 | if data.title.strip(): |
---|
83 | title = data.title |
---|
84 | if title.strip() == "": |
---|
85 | title = file_name |
---|
86 | |
---|
87 | if new_plot.filename.strip() == "": |
---|
88 | new_plot.filename = file_name |
---|
89 | |
---|
90 | new_plot.name = name |
---|
91 | new_plot.title = title |
---|
92 | ## allow to highlight data when plotted |
---|
93 | new_plot.interactive = True |
---|
94 | ## when 2 data have the same id override the 1 st plotted |
---|
95 | self.time_stamp += 1 |
---|
96 | new_plot.id = str(name) + str(self.time_stamp) |
---|
97 | ##group_id specify on which panel to plot this data |
---|
98 | new_plot.group_id = str(name) + str(self.time_stamp) |
---|
99 | new_plot.is_data = True |
---|
100 | new_plot.path = path |
---|
101 | new_plot.list_group_id = [] |
---|
102 | ##post data to plot |
---|
103 | # plot data |
---|
104 | return new_plot |
---|
105 | |
---|
106 | def rename(self, name): |
---|
107 | """ |
---|
108 | rename data |
---|
109 | """ |
---|
110 | ## name of the data allow to differentiate data when plotted |
---|
111 | name = parse_name(name=name, expression="_") |
---|
112 | |
---|
113 | max_char = name.find("[") |
---|
114 | if max_char < 0: |
---|
115 | max_char = len(name) |
---|
116 | name = name[0:max_char] |
---|
117 | |
---|
118 | if name not in self.data_name_dict: |
---|
119 | self.data_name_dict[name] = 0 |
---|
120 | else: |
---|
121 | self.data_name_dict[name] += 1 |
---|
122 | name = name + " [" + str(self.data_name_dict[name]) + "]" |
---|
123 | return name |
---|
124 | |
---|
125 | |
---|
126 | def add_data(self, data_list): |
---|
127 | """ |
---|
128 | receive a list of |
---|
129 | """ |
---|
130 | for id, data in data_list.iteritems(): |
---|
131 | if id in self.stored_data: |
---|
132 | msg = "Data manager already stores %s" % str(data.name) |
---|
133 | msg += "" |
---|
134 | logging.info(msg) |
---|
135 | data_state = self.stored_data[id] |
---|
136 | data_state.data = data |
---|
137 | else: |
---|
138 | data_state = DataState(data) |
---|
139 | data_state.id = id |
---|
140 | data_state.path = data.path |
---|
141 | self.stored_data[id] = data_state |
---|
142 | |
---|
143 | def update_data(self, prev_data, new_data): |
---|
144 | """ |
---|
145 | """ |
---|
146 | if prev_data.id not in self.stored_data.keys(): |
---|
147 | return None, {} |
---|
148 | data_state = self.stored_data[prev_data.id] |
---|
149 | self.stored_data[new_data.id] = data_state.clone() |
---|
150 | self.stored_data[new_data.id].data = new_data |
---|
151 | if prev_data.id in self.stored_data.keys(): |
---|
152 | del self.stored_data[prev_data.id] |
---|
153 | return prev_data.id, {new_data.id: self.stored_data[new_data.id]} |
---|
154 | |
---|
155 | def update_theory(self, theory, data_id=None, state=None): |
---|
156 | """ |
---|
157 | """ |
---|
158 | uid = data_id |
---|
159 | if data_id is None and theory is not None: |
---|
160 | uid = theory.id |
---|
161 | if uid in self.stored_data.keys(): |
---|
162 | data_state = self.stored_data[uid] |
---|
163 | else: |
---|
164 | data_state = DataState() |
---|
165 | data_state.uid = uid |
---|
166 | data_state.set_theory(theory_data=theory, theory_state=state) |
---|
167 | self.stored_data[uid] = data_state |
---|
168 | return {uid: self.stored_data[uid]} |
---|
169 | |
---|
170 | |
---|
171 | def get_message(self): |
---|
172 | """ |
---|
173 | return message |
---|
174 | """ |
---|
175 | return self.message |
---|
176 | |
---|
177 | def get_by_id(self, id_list=None): |
---|
178 | """ |
---|
179 | """ |
---|
180 | _selected_data = {} |
---|
181 | _selected_theory_list = {} |
---|
182 | if id_list is None: |
---|
183 | return |
---|
184 | for d_id in self.stored_data: |
---|
185 | for search_id in id_list: |
---|
186 | data_state = self.stored_data[d_id] |
---|
187 | data = data_state.data |
---|
188 | theory_list = data_state.get_theory() |
---|
189 | if search_id == d_id: |
---|
190 | _selected_data[search_id] = data |
---|
191 | if search_id in theory_list.keys(): |
---|
192 | _selected_theory_list[search_id] = theory_list[search_id] |
---|
193 | |
---|
194 | return _selected_data, _selected_theory_list |
---|
195 | |
---|
196 | |
---|
197 | def freeze(self, theory_id): |
---|
198 | """ |
---|
199 | """ |
---|
200 | return self.freeze_theory(self.stored_data.keys(), theory_id) |
---|
201 | |
---|
202 | def freeze_theory(self, data_id, theory_id): |
---|
203 | """ |
---|
204 | """ |
---|
205 | selected_theory = {} |
---|
206 | for d_id in data_id: |
---|
207 | if d_id in self.stored_data: |
---|
208 | data_state = self.stored_data[d_id] |
---|
209 | theory_list = data_state.get_theory() |
---|
210 | for t_id in theory_id: |
---|
211 | if t_id in theory_list.keys(): |
---|
212 | theory_data, theory_state = theory_list[t_id] |
---|
213 | new_theory = copy.deepcopy(theory_data) |
---|
214 | new_theory.id = time.time() |
---|
215 | new_theory.is_data = True |
---|
216 | new_theory.name += '_@' + \ |
---|
217 | str(new_theory.id)[7:-1].replace('.', '') |
---|
218 | new_theory.title = new_theory.name |
---|
219 | new_theory.label = new_theory.name |
---|
220 | selected_theory[new_theory.id] = DataState(new_theory) |
---|
221 | self.stored_data[new_theory.id] = \ |
---|
222 | selected_theory[new_theory.id] |
---|
223 | |
---|
224 | return selected_theory |
---|
225 | |
---|
226 | |
---|
227 | def delete_data(self, data_id, theory_id=None, delete_all=False): |
---|
228 | """ |
---|
229 | """ |
---|
230 | for d_id in data_id: |
---|
231 | if d_id in self.stored_data.keys(): |
---|
232 | data_state = self.stored_data[d_id] |
---|
233 | if data_state.data.name in self.data_name_dict: |
---|
234 | del self.data_name_dict[data_state.data.name] |
---|
235 | del self.stored_data[d_id] |
---|
236 | |
---|
237 | self.delete_theory(data_id, theory_id) |
---|
238 | if delete_all: |
---|
239 | self.stored_data = {} |
---|
240 | self.data_name_dict = {} |
---|
241 | |
---|
242 | def delete_theory(self, data_id, theory_id): |
---|
243 | """ |
---|
244 | """ |
---|
245 | for d_id in data_id: |
---|
246 | if d_id in self.stored_data: |
---|
247 | data_state = self.stored_data[d_id] |
---|
248 | theory_list = data_state.get_theory() |
---|
249 | if theory_id in theory_list.keys(): |
---|
250 | del theory_list[theory_id] |
---|
251 | #del pure theory |
---|
252 | self.delete_by_id(theory_id) |
---|
253 | |
---|
254 | def delete_by_id(self, id_list=None): |
---|
255 | """ |
---|
256 | save data and path |
---|
257 | """ |
---|
258 | for id in id_list: |
---|
259 | if id in self.stored_data: |
---|
260 | del self.stored_data[id] |
---|
261 | |
---|
262 | |
---|
263 | def get_by_name(self, name_list=None): |
---|
264 | """ |
---|
265 | return a list of data given a list of data names |
---|
266 | """ |
---|
267 | _selected_data = {} |
---|
268 | for selected_name in name_list: |
---|
269 | for id, data_state in self.stored_data.iteritems(): |
---|
270 | if data_state.data.name == selected_name: |
---|
271 | _selected_data[id] = data_state.data |
---|
272 | return _selected_data |
---|
273 | |
---|
274 | def delete_by_name(self, name_list=None): |
---|
275 | """ |
---|
276 | save data and path |
---|
277 | """ |
---|
278 | for selected_name in name_list: |
---|
279 | for id, data_state in self.stored_data.iteritems(): |
---|
280 | if data_state.data.name == selected_name: |
---|
281 | del self.stored_data[id] |
---|
282 | |
---|
283 | def get_data_state(self, data_id): |
---|
284 | """ |
---|
285 | Send list of selected data |
---|
286 | """ |
---|
287 | _selected_data_state = {} |
---|
288 | for id in data_id: |
---|
289 | if id in self.stored_data.keys(): |
---|
290 | _selected_data_state[id] = self.stored_data[id] |
---|
291 | return _selected_data_state |
---|
292 | |
---|
293 | def get_all_data(self): |
---|
294 | """ |
---|
295 | return list of all available data |
---|
296 | """ |
---|
297 | return self.stored_data |
---|
298 | |
---|
299 | |
---|
300 | |
---|