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 | from sans.guiframe.data_state import DataState |
---|
20 | from sans.guiframe.utils import parse_name |
---|
21 | import DataLoader.data_info as DataInfo |
---|
22 | from sans.guiframe.dataFitting import Data1D |
---|
23 | from sans.guiframe.dataFitting import Data2D |
---|
24 | |
---|
25 | class DataManager(object): |
---|
26 | """ |
---|
27 | Manage a list of data |
---|
28 | """ |
---|
29 | def __init__(self): |
---|
30 | """ |
---|
31 | Store opened path and data object created at the loading time |
---|
32 | :param auto_plot: if True the datamanager sends data to plotting |
---|
33 | plugin. |
---|
34 | :param auto_set_data: if True the datamanager sends to the current |
---|
35 | perspective |
---|
36 | """ |
---|
37 | self._selected_data = {} |
---|
38 | self.stored_data = {} |
---|
39 | self.message = "" |
---|
40 | self.data_name_dict = {} |
---|
41 | |
---|
42 | def create_gui_data(self, data, path=None): |
---|
43 | """ |
---|
44 | Receive data from loader and create a data to use for guiframe |
---|
45 | """ |
---|
46 | |
---|
47 | if issubclass(DataInfo.Data2D, data.__class__): |
---|
48 | new_plot = Data2D(image=None, err_image=None) |
---|
49 | else: |
---|
50 | new_plot = Data1D(x=[], y=[], dx=None, dy=None) |
---|
51 | |
---|
52 | new_plot.copy_from_datainfo(data) |
---|
53 | data.clone_without_data(clone=new_plot) |
---|
54 | #creating a name for data |
---|
55 | name = "" |
---|
56 | title = "" |
---|
57 | file_name = "" |
---|
58 | if path is not None: |
---|
59 | file_name = os.path.basename(path) |
---|
60 | if data.run: |
---|
61 | name = data.run[0] |
---|
62 | if name == "": |
---|
63 | name = file_name |
---|
64 | name = self.rename(name) |
---|
65 | #find title |
---|
66 | if data.title.strip(): |
---|
67 | title = data.title |
---|
68 | if title.strip() == "": |
---|
69 | title = file_name |
---|
70 | |
---|
71 | if new_plot.filename.strip() == "": |
---|
72 | new_plot.filename = file_name |
---|
73 | |
---|
74 | new_plot.name = name |
---|
75 | new_plot.title = title |
---|
76 | ## allow to highlight data when plotted |
---|
77 | new_plot.interactive = True |
---|
78 | ## when 2 data have the same id override the 1 st plotted |
---|
79 | new_plot.id = name |
---|
80 | ##group_id specify on which panel to plot this data |
---|
81 | new_plot.group_id = name |
---|
82 | new_plot.is_data = True |
---|
83 | new_plot.path = path |
---|
84 | ##post data to plot |
---|
85 | # plot data |
---|
86 | return new_plot |
---|
87 | |
---|
88 | def rename(self, name): |
---|
89 | """ |
---|
90 | rename data |
---|
91 | """ |
---|
92 | ## name of the data allow to differentiate data when plotted |
---|
93 | name = parse_name(name=name, expression="_") |
---|
94 | |
---|
95 | max_char = name.find("[") |
---|
96 | if max_char < 0: |
---|
97 | max_char = len(name) |
---|
98 | name = name[0:max_char] |
---|
99 | |
---|
100 | if name not in self.data_name_dict: |
---|
101 | self.data_name_dict[name] = 0 |
---|
102 | else: |
---|
103 | self.data_name_dict[name] += 1 |
---|
104 | name = name + " [" + str(self.data_name_dict[name]) + "]" |
---|
105 | return name |
---|
106 | |
---|
107 | def add_data(self, data_list): |
---|
108 | """ |
---|
109 | receive a list of |
---|
110 | """ |
---|
111 | self._selected_data = {} |
---|
112 | for data in data_list: |
---|
113 | if data.id in self.stored_data: |
---|
114 | msg = "Data manager already stores %s" % str(data.name) |
---|
115 | msg += "" |
---|
116 | logging.info(msg) |
---|
117 | data_state = DataState(data) |
---|
118 | self._selected_data[data.id] = data_state |
---|
119 | self.stored_data[data.id] = data_state |
---|
120 | |
---|
121 | def set_auto_plot(self, flag=False): |
---|
122 | """ |
---|
123 | When flag is true the data is plotted automatically |
---|
124 | """ |
---|
125 | self._auto_set_data = flag |
---|
126 | |
---|
127 | def set_auto_set_data(self, flag=False): |
---|
128 | """ |
---|
129 | When flag is true the data is send to the current perspective |
---|
130 | automatically |
---|
131 | """ |
---|
132 | self._auto_set_data = flag |
---|
133 | |
---|
134 | def get_message(self): |
---|
135 | """ |
---|
136 | return message |
---|
137 | """ |
---|
138 | return self.message |
---|
139 | |
---|
140 | def get_by_id(self, id_list=None): |
---|
141 | """ |
---|
142 | get a list of data given a list of id |
---|
143 | """ |
---|
144 | self._selected_data = {} |
---|
145 | for id in id_list: |
---|
146 | if id in self.stored_data: |
---|
147 | self._selected_data[id] = self.stored_data[id] |
---|
148 | return self._selected_data |
---|
149 | |
---|
150 | def append_theory(self, data_id, theory): |
---|
151 | """ |
---|
152 | """ |
---|
153 | print "append theory", self.stored_data, data_id |
---|
154 | if data_id in self.stored_data: |
---|
155 | data_state = self.stored_data[data_id] |
---|
156 | data_state.set_theory(theory) |
---|
157 | |
---|
158 | def delete_data(self, data_id, theory_id, delete_all): |
---|
159 | """ |
---|
160 | """ |
---|
161 | if data_id in self.stored_data: |
---|
162 | del self.stored_data[data_id] |
---|
163 | if data_id in self._selected_data: |
---|
164 | del self._selected_data |
---|
165 | |
---|
166 | def delete_by_id(self, id_list=None): |
---|
167 | """ |
---|
168 | save data and path |
---|
169 | """ |
---|
170 | for id in id_list: |
---|
171 | if id in self.stored_data: |
---|
172 | del self.stored_data[id] |
---|
173 | if id in self._selected_data: |
---|
174 | del self._selected_data[id] |
---|
175 | |
---|
176 | def get_by_name(self, name_list=None): |
---|
177 | """ |
---|
178 | return a list of data given a list of data names |
---|
179 | """ |
---|
180 | self._selected_data = {} |
---|
181 | for selected_name in name_list: |
---|
182 | for id, data_state in self.stored_data.iteritems(): |
---|
183 | if data_state.data.name == selected_name: |
---|
184 | self._selected_data[id] = data_state |
---|
185 | return self._selected_data |
---|
186 | |
---|
187 | def delete_by_name(self, name_list=None): |
---|
188 | """ |
---|
189 | save data and path |
---|
190 | """ |
---|
191 | for selected_name in name_list: |
---|
192 | for id, data_state in self.stored_data.iteritems(): |
---|
193 | if data_state.data.name == selected_name: |
---|
194 | del self._selected_data[id] |
---|
195 | del self.stored_data[data.id] |
---|
196 | |
---|
197 | def get_selected_data(self): |
---|
198 | """ |
---|
199 | Send list of selected data |
---|
200 | """ |
---|
201 | return self._selected_data |
---|
202 | |
---|
203 | def get_all_data(self): |
---|
204 | """ |
---|
205 | return list of all available data |
---|
206 | """ |
---|
207 | return self.stored_data |
---|
208 | |
---|
209 | |
---|
210 | |
---|