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