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._selected_data = {} |
---|
42 | self.stored_data = {} |
---|
43 | self.message = "" |
---|
44 | self.data_name_dict = {} |
---|
45 | |
---|
46 | def __str__(self): |
---|
47 | _str = "" |
---|
48 | _str += "No of states is %s \n" % str(len(self.stored_data)) |
---|
49 | n_count = 0 |
---|
50 | for value in self.stored_data.values(): |
---|
51 | n_count += 1 |
---|
52 | _str += "State No %s \n" % str(n_count) |
---|
53 | _str += str(value) + "\n" |
---|
54 | return _str |
---|
55 | |
---|
56 | def create_gui_data(self, data, path=None): |
---|
57 | """ |
---|
58 | Receive data from loader and create a data to use for guiframe |
---|
59 | """ |
---|
60 | |
---|
61 | if issubclass(DataInfo.Data2D, data.__class__): |
---|
62 | new_plot = Data2D(image=None, err_image=None) |
---|
63 | else: |
---|
64 | new_plot = Data1D(x=[], y=[], dx=None, dy=None) |
---|
65 | |
---|
66 | new_plot.copy_from_datainfo(data) |
---|
67 | data.clone_without_data(clone=new_plot) |
---|
68 | #creating a name for data |
---|
69 | name = "" |
---|
70 | title = "" |
---|
71 | file_name = "" |
---|
72 | if path is not None: |
---|
73 | file_name = os.path.basename(path) |
---|
74 | if data.run: |
---|
75 | name = data.run[0] |
---|
76 | if name == "": |
---|
77 | name = file_name |
---|
78 | name = self.rename(name) |
---|
79 | #find title |
---|
80 | if data.title.strip(): |
---|
81 | title = data.title |
---|
82 | if title.strip() == "": |
---|
83 | title = file_name |
---|
84 | |
---|
85 | if new_plot.filename.strip() == "": |
---|
86 | new_plot.filename = file_name |
---|
87 | |
---|
88 | new_plot.name = name |
---|
89 | new_plot.title = title |
---|
90 | ## allow to highlight data when plotted |
---|
91 | new_plot.interactive = True |
---|
92 | ## when 2 data have the same id override the 1 st plotted |
---|
93 | new_plot.id = wx.NewId() |
---|
94 | ##group_id specify on which panel to plot this data |
---|
95 | new_plot.group_id = [wx.NewId()] |
---|
96 | new_plot.is_data = True |
---|
97 | new_plot.path = path |
---|
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 | self._selected_data = {} |
---|
126 | for data in data_list: |
---|
127 | if data.id in self.stored_data: |
---|
128 | msg = "Data manager already stores %s" % str(data.name) |
---|
129 | msg += "" |
---|
130 | logging.info(msg) |
---|
131 | self.stored_data[data.id].data = data |
---|
132 | data_state = self.stored_data[data.id] |
---|
133 | else: |
---|
134 | data_state = DataState(data) |
---|
135 | data_state.id = wx.NewId() |
---|
136 | self.stored_data[data.id] = data_state |
---|
137 | self._selected_data[data.id] = data_state |
---|
138 | |
---|
139 | |
---|
140 | def update_data(self, prev_data, new_data): |
---|
141 | """ |
---|
142 | """ |
---|
143 | if prev_data.id not in self.stored_data.keys(): |
---|
144 | return None, {} |
---|
145 | data_state = self.stored_data[prev_data.id] |
---|
146 | self.stored_data[new_data.id] = data_state.clone() |
---|
147 | self.stored_data[new_data.id].data = new_data |
---|
148 | if prev_data.id in self.stored_data.keys(): |
---|
149 | del self.stored_data[prev_data.id] |
---|
150 | return prev_data.id, {new_data.id: self.stored_data[new_data.id]} |
---|
151 | |
---|
152 | def get_message(self): |
---|
153 | """ |
---|
154 | return message |
---|
155 | """ |
---|
156 | return self.message |
---|
157 | |
---|
158 | def get_by_id(self, id_list=None): |
---|
159 | """ |
---|
160 | get a list of data given a list of id |
---|
161 | """ |
---|
162 | self._selected_data = {} |
---|
163 | for id in id_list: |
---|
164 | if id in self.stored_data: |
---|
165 | self._selected_data[id] = self.stored_data[id] |
---|
166 | return self._selected_data |
---|
167 | |
---|
168 | def append_theory(self, data_id, theory, state=None): |
---|
169 | """ |
---|
170 | """ |
---|
171 | data_state = self.stored_data[data_id] |
---|
172 | data_state.set_theory(theory_data=theory, |
---|
173 | theory_state=state) |
---|
174 | return {data_id: self.stored_data[data_id]} |
---|
175 | |
---|
176 | def freeze_theory(self, data_id, theory_id): |
---|
177 | """ |
---|
178 | """ |
---|
179 | new_data_state = [] |
---|
180 | for d_id in data_id: |
---|
181 | if d_id in self.stored_data: |
---|
182 | data_state = self.stored_data[d_id] |
---|
183 | theory_list = data_state.get_theory() |
---|
184 | for t_id in theory_id: |
---|
185 | if t_id in theory_list.keys(): |
---|
186 | theory = theory_list[t_id] |
---|
187 | new_theory = copy.deepcopy(theory) |
---|
188 | new_theory.id = wx.NewId() |
---|
189 | data_state.append_theory(new_theory) |
---|
190 | theory_list.append(new_theory) |
---|
191 | new_data_state.append(data_state) |
---|
192 | msg = "Theory with ID %s " % str(theory_id) |
---|
193 | msg += "couldn't not be frozen" |
---|
194 | raise ValueError, msg |
---|
195 | return new_data_state |
---|
196 | |
---|
197 | |
---|
198 | def delete_data(self, data_id, theory_id=None, delete_all=False): |
---|
199 | """ |
---|
200 | """ |
---|
201 | if data_id in self.stored_data.keys(): |
---|
202 | data_state = self.stored_data[data_id] |
---|
203 | if data_state.data.name in self.data_name_dict: |
---|
204 | del self.data_name_dict[data_state.data.name] |
---|
205 | del self.stored_data[data_id] |
---|
206 | if data_id in self._selected_data.keys(): |
---|
207 | data_state = self._selected_data[data_id] |
---|
208 | if data_state.data.name in self.data_name_dict: |
---|
209 | del self.data_name_dict[data_state.data.name] |
---|
210 | del self._selected_data[data_id] |
---|
211 | |
---|
212 | self.delete_theory(self, data_id, theory_id) |
---|
213 | if delete_all: |
---|
214 | self._selected_data = {} |
---|
215 | self.stored_data = {} |
---|
216 | self.data_name_dict = {} |
---|
217 | |
---|
218 | def delete_theory(self, data_id, theory_id): |
---|
219 | """ |
---|
220 | """ |
---|
221 | if data_id in self.stored_data: |
---|
222 | data_state = self.stored_data[data_id] |
---|
223 | theory_list = data_state.get_theory() |
---|
224 | if theory_id in theory_list.key(): |
---|
225 | del theory_list[theory_id] |
---|
226 | if data_id in self._selected_data: |
---|
227 | data_state = self._selected_data[data_id] |
---|
228 | theory_list = data_state.get_theory() |
---|
229 | if theory_id in theory_list.key(): |
---|
230 | del theory_list[theory_id] |
---|
231 | |
---|
232 | def delete_by_id(self, id_list=None): |
---|
233 | """ |
---|
234 | save data and path |
---|
235 | """ |
---|
236 | for id in id_list: |
---|
237 | if id in self.stored_data: |
---|
238 | del self.stored_data[id] |
---|
239 | if id in self._selected_data: |
---|
240 | del self._selected_data[id] |
---|
241 | |
---|
242 | def get_by_name(self, name_list=None): |
---|
243 | """ |
---|
244 | return a list of data given a list of data names |
---|
245 | """ |
---|
246 | self._selected_data = {} |
---|
247 | for selected_name in name_list: |
---|
248 | for id, data_state in self.stored_data.iteritems(): |
---|
249 | if data_state.data.name == selected_name: |
---|
250 | self._selected_data[id] = data_state |
---|
251 | return self._selected_data |
---|
252 | |
---|
253 | def delete_by_name(self, name_list=None): |
---|
254 | """ |
---|
255 | save data and path |
---|
256 | """ |
---|
257 | for selected_name in name_list: |
---|
258 | for id, data_state in self.stored_data.iteritems(): |
---|
259 | if data_state.data.name == selected_name: |
---|
260 | del self._selected_data[id] |
---|
261 | del self.stored_data[data.id] |
---|
262 | |
---|
263 | def get_selected_data(self): |
---|
264 | """ |
---|
265 | Send list of selected data |
---|
266 | """ |
---|
267 | return self._selected_data |
---|
268 | |
---|
269 | def get_all_data(self): |
---|
270 | """ |
---|
271 | return list of all available data |
---|
272 | """ |
---|
273 | return self.stored_data |
---|
274 | |
---|
275 | |
---|
276 | |
---|