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