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 | logger = logging.getLogger(__name__) |
---|
29 | |
---|
30 | class DataManager(object): |
---|
31 | """ |
---|
32 | Manage a list of data |
---|
33 | """ |
---|
34 | def __init__(self): |
---|
35 | """ |
---|
36 | Store opened path and data object created at the loading time |
---|
37 | :param auto_plot: if True the datamanager sends data to plotting |
---|
38 | plugin. |
---|
39 | :param auto_set_data: if True the datamanager sends to the current |
---|
40 | perspective |
---|
41 | """ |
---|
42 | self.stored_data = {} |
---|
43 | self.message = "" |
---|
44 | self.data_name_dict = {} |
---|
45 | self.count = 0 |
---|
46 | self.list_of_id = [] |
---|
47 | self.time_stamp = time.time() |
---|
48 | |
---|
49 | def __str__(self): |
---|
50 | _str = "" |
---|
51 | _str += "No of states is %s \n" % str(len(self.stored_data)) |
---|
52 | n_count = 0 |
---|
53 | for value in self.stored_data.values(): |
---|
54 | n_count += 1 |
---|
55 | _str += "State No %s \n" % str(n_count) |
---|
56 | _str += str(value) + "\n" |
---|
57 | return _str |
---|
58 | |
---|
59 | def create_gui_data(self, data, path=None): |
---|
60 | """ |
---|
61 | Receive data from loader and create a data to use for guiframe |
---|
62 | """ |
---|
63 | |
---|
64 | if issubclass(Data2D, data.__class__): |
---|
65 | new_plot = Data2D(image=None, err_image=None) # For now, isSesans for 2D data is always false |
---|
66 | else: |
---|
67 | new_plot = Data1D(x=[], y=[], dx=None, dy=None, lam=None, dlam=None, isSesans=data.isSesans) |
---|
68 | |
---|
69 | |
---|
70 | #elif data.meta_data['loader'] == 'SESANS': |
---|
71 | # new_plot = Data1D(x=[], y=[], dx=None, dy=None, lam=None, dlam=None, isSesans=True) |
---|
72 | #else: |
---|
73 | # new_plot = Data1D(x=[], y=[], dx=None, dy=None, lam=None, dlam=None) #SESANS check??? |
---|
74 | |
---|
75 | new_plot.copy_from_datainfo(data) |
---|
76 | data.clone_without_data(clone=new_plot) |
---|
77 | #creating a name for data |
---|
78 | title = "" |
---|
79 | file_name = os.path.basename(path) if path is not None else data.filename |
---|
80 | if file_name: |
---|
81 | name = file_name |
---|
82 | elif data.run: |
---|
83 | name = data.run[0] |
---|
84 | else: |
---|
85 | name = "data" |
---|
86 | name = self.rename(name) |
---|
87 | #find title |
---|
88 | if data.title.strip(): |
---|
89 | title = data.title |
---|
90 | if title.strip() == "": |
---|
91 | title = file_name |
---|
92 | |
---|
93 | if new_plot.filename.strip() == "": |
---|
94 | new_plot.filename = file_name |
---|
95 | |
---|
96 | new_plot.name = name |
---|
97 | new_plot.title = title |
---|
98 | ## allow to highlight data when plotted |
---|
99 | new_plot.interactive = True |
---|
100 | ## when 2 data have the same id override the 1 st plotted |
---|
101 | self.time_stamp += 1 |
---|
102 | new_plot.id = str(name) + str(self.time_stamp) |
---|
103 | ##group_id specify on which panel to plot this data |
---|
104 | new_plot.group_id = str(name) + str(self.time_stamp) |
---|
105 | new_plot.is_data = True |
---|
106 | new_plot.path = path |
---|
107 | new_plot.list_group_id = [] |
---|
108 | ##post data to plot |
---|
109 | # plot data |
---|
110 | return new_plot |
---|
111 | |
---|
112 | def rename(self, name): |
---|
113 | """ |
---|
114 | rename data |
---|
115 | """ |
---|
116 | ## name of the data allow to differentiate data when plotted |
---|
117 | name = parse_name(name=name, expression="_") |
---|
118 | max_char = name.find("[") |
---|
119 | if max_char < 0: |
---|
120 | max_char = len(name) |
---|
121 | name = name[0:max_char] |
---|
122 | if name not in self.data_name_dict: |
---|
123 | self.data_name_dict[name] = 0 |
---|
124 | else: |
---|
125 | self.data_name_dict[name] += 1 |
---|
126 | name = name + " [" + str(self.data_name_dict[name]) + "]" |
---|
127 | return name |
---|
128 | |
---|
129 | |
---|
130 | def add_data(self, data_list): |
---|
131 | """ |
---|
132 | receive a list of |
---|
133 | """ |
---|
134 | for id, data in data_list.iteritems(): |
---|
135 | if id in self.stored_data: |
---|
136 | msg = "Data manager already stores %s" % str(data.name) |
---|
137 | msg += "" |
---|
138 | logger.info(msg) |
---|
139 | data_state = self.stored_data[id] |
---|
140 | data_state.data = data |
---|
141 | else: |
---|
142 | data_state = DataState(data) |
---|
143 | data_state.id = id |
---|
144 | data_state.path = data.path |
---|
145 | self.stored_data[id] = data_state |
---|
146 | |
---|
147 | def update_data(self, prev_data, new_data): |
---|
148 | """ |
---|
149 | """ |
---|
150 | if prev_data.id not in self.stored_data.keys(): |
---|
151 | return None, {} |
---|
152 | data_state = self.stored_data[prev_data.id] |
---|
153 | self.stored_data[new_data.id] = data_state.clone() |
---|
154 | self.stored_data[new_data.id].data = new_data |
---|
155 | if prev_data.id in self.stored_data.keys(): |
---|
156 | del self.stored_data[prev_data.id] |
---|
157 | return prev_data.id, {new_data.id: self.stored_data[new_data.id]} |
---|
158 | |
---|
159 | def update_theory(self, theory, data_id=None, state=None): |
---|
160 | """ |
---|
161 | """ |
---|
162 | uid = data_id |
---|
163 | if data_id is None and theory is not None: |
---|
164 | uid = theory.id |
---|
165 | if uid in self.stored_data.keys(): |
---|
166 | data_state = self.stored_data[uid] |
---|
167 | else: |
---|
168 | data_state = DataState() |
---|
169 | data_state.uid = uid |
---|
170 | data_state.set_theory(theory_data=theory, theory_state=state) |
---|
171 | self.stored_data[uid] = data_state |
---|
172 | return {uid: self.stored_data[uid]} |
---|
173 | |
---|
174 | def get_message(self): |
---|
175 | """ |
---|
176 | return message |
---|
177 | """ |
---|
178 | return self.message |
---|
179 | |
---|
180 | def get_by_id(self, id_list=None): |
---|
181 | """ |
---|
182 | """ |
---|
183 | _selected_data = {} |
---|
184 | _selected_theory_list = {} |
---|
185 | if id_list is None: |
---|
186 | return |
---|
187 | for d_id in self.stored_data: |
---|
188 | for search_id in id_list: |
---|
189 | data_state = self.stored_data[d_id] |
---|
190 | data = data_state.data |
---|
191 | theory_list = data_state.get_theory() |
---|
192 | if search_id == d_id: |
---|
193 | _selected_data[search_id] = data |
---|
194 | if search_id in theory_list.keys(): |
---|
195 | _selected_theory_list[search_id] = theory_list[search_id] |
---|
196 | |
---|
197 | return _selected_data, _selected_theory_list |
---|
198 | |
---|
199 | |
---|
200 | def freeze(self, theory_id): |
---|
201 | """ |
---|
202 | """ |
---|
203 | return self.freeze_theory(self.stored_data.keys(), theory_id) |
---|
204 | |
---|
205 | def freeze_theory(self, data_id, theory_id): |
---|
206 | """ |
---|
207 | """ |
---|
208 | selected_theory = {} |
---|
209 | for d_id in data_id: |
---|
210 | if d_id in self.stored_data: |
---|
211 | data_state = self.stored_data[d_id] |
---|
212 | theory_list = data_state.get_theory() |
---|
213 | for t_id in theory_id: |
---|
214 | if t_id in theory_list.keys(): |
---|
215 | theory_data, theory_state = theory_list[t_id] |
---|
216 | new_theory = copy.deepcopy(theory_data) |
---|
217 | new_theory.id = time.time() |
---|
218 | new_theory.is_data = True |
---|
219 | new_theory.name += '_@' + \ |
---|
220 | str(new_theory.id)[7:-1].replace('.', '') |
---|
221 | new_theory.title = new_theory.name |
---|
222 | new_theory.label = new_theory.name |
---|
223 | selected_theory[new_theory.id] = DataState(new_theory) |
---|
224 | self.stored_data[new_theory.id] = \ |
---|
225 | selected_theory[new_theory.id] |
---|
226 | |
---|
227 | return selected_theory |
---|
228 | |
---|
229 | def delete_data(self, data_id, theory_id=None, delete_all=False): |
---|
230 | """ |
---|
231 | """ |
---|
232 | for d_id in data_id: |
---|
233 | if d_id in self.stored_data.keys(): |
---|
234 | data_state = self.stored_data[d_id] |
---|
235 | if data_state.data.name in self.data_name_dict: |
---|
236 | del self.data_name_dict[data_state.data.name] |
---|
237 | del self.stored_data[d_id] |
---|
238 | self.delete_theory(data_id, theory_id) |
---|
239 | if delete_all: |
---|
240 | self.stored_data = {} |
---|
241 | self.data_name_dict = {} |
---|
242 | |
---|
243 | def delete_theory(self, data_id, theory_id): |
---|
244 | """ |
---|
245 | """ |
---|
246 | for d_id in data_id: |
---|
247 | if d_id in self.stored_data: |
---|
248 | data_state = self.stored_data[d_id] |
---|
249 | theory_list = data_state.get_theory() |
---|
250 | if theory_id in theory_list.keys(): |
---|
251 | del theory_list[theory_id] |
---|
252 | #del pure theory |
---|
253 | self.delete_by_id(theory_id) |
---|
254 | |
---|
255 | def delete_by_id(self, id_list=None): |
---|
256 | """ |
---|
257 | save data and path |
---|
258 | """ |
---|
259 | for id in id_list: |
---|
260 | if id in self.stored_data: |
---|
261 | del self.stored_data[id] |
---|
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 |
---|