[3c44c66] | 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 | |
---|
[f444b20] | 14 | All modules "creating Data" posts their data to data_manager . |
---|
| 15 | Data_manager make these new data available for all other perspectives. |
---|
[3c44c66] | 16 | """ |
---|
[f444b20] | 17 | import logging |
---|
[0348245] | 18 | import os |
---|
[c70eb7c] | 19 | import copy |
---|
| 20 | |
---|
[c5e84fb] | 21 | from sans.guiframe.data_state import DataState |
---|
[75fbd17] | 22 | from sans.guiframe.utils import parse_name |
---|
[0348245] | 23 | import DataLoader.data_info as DataInfo |
---|
| 24 | from sans.guiframe.dataFitting import Data1D |
---|
| 25 | from sans.guiframe.dataFitting import Data2D |
---|
[5080cda] | 26 | import time |
---|
[a07e72f] | 27 | |
---|
[3c44c66] | 28 | class DataManager(object): |
---|
| 29 | """ |
---|
[f444b20] | 30 | Manage a list of data |
---|
[3c44c66] | 31 | """ |
---|
[f444b20] | 32 | def __init__(self): |
---|
[3c44c66] | 33 | """ |
---|
[aebc4cc] | 34 | Store opened path and data object created at the loading time |
---|
[f444b20] | 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 |
---|
[4e9583c] | 39 | """ |
---|
[f444b20] | 40 | self.stored_data = {} |
---|
[aebc4cc] | 41 | self.message = "" |
---|
[75fbd17] | 42 | self.data_name_dict = {} |
---|
[5080cda] | 43 | self.count = 0 |
---|
| 44 | self.list_of_id = [] |
---|
[f444b20] | 45 | |
---|
[c70eb7c] | 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 | |
---|
[0348245] | 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 | |
---|
[5080cda] | 61 | if issubclass(Data2D, data.__class__): |
---|
[0348245] | 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 |
---|
[5080cda] | 93 | new_plot.id = time.time() |
---|
[0348245] | 94 | ##group_id specify on which panel to plot this data |
---|
[5080cda] | 95 | new_plot.group_id = time.time() |
---|
[0348245] | 96 | new_plot.is_data = True |
---|
| 97 | new_plot.path = path |
---|
[52d3fd1] | 98 | new_plot.list_group_id = [] |
---|
[0348245] | 99 | ##post data to plot |
---|
| 100 | # plot data |
---|
| 101 | return new_plot |
---|
| 102 | |
---|
[75fbd17] | 103 | def rename(self, name): |
---|
| 104 | """ |
---|
| 105 | rename data |
---|
| 106 | """ |
---|
| 107 | ## name of the data allow to differentiate data when plotted |
---|
| 108 | name = parse_name(name=name, expression="_") |
---|
| 109 | |
---|
| 110 | max_char = name.find("[") |
---|
| 111 | if max_char < 0: |
---|
| 112 | max_char = len(name) |
---|
| 113 | name = name[0:max_char] |
---|
| 114 | |
---|
| 115 | if name not in self.data_name_dict: |
---|
| 116 | self.data_name_dict[name] = 0 |
---|
| 117 | else: |
---|
| 118 | self.data_name_dict[name] += 1 |
---|
| 119 | name = name + " [" + str(self.data_name_dict[name]) + "]" |
---|
| 120 | return name |
---|
| 121 | |
---|
[ec611aad] | 122 | |
---|
[f444b20] | 123 | def add_data(self, data_list): |
---|
| 124 | """ |
---|
| 125 | receive a list of |
---|
| 126 | """ |
---|
[e88ebfd] | 127 | for id, data in data_list.iteritems(): |
---|
| 128 | if id in self.stored_data: |
---|
[f444b20] | 129 | msg = "Data manager already stores %s" % str(data.name) |
---|
| 130 | msg += "" |
---|
| 131 | logging.info(msg) |
---|
[e88ebfd] | 132 | data_state = self.stored_data[id] |
---|
[a3c96f7a] | 133 | data_state.data = data |
---|
[5c4b674] | 134 | else: |
---|
| 135 | data_state = DataState(data) |
---|
[a3c96f7a] | 136 | data_state.id = id |
---|
[e88ebfd] | 137 | self.stored_data[id] = data_state |
---|
| 138 | |
---|
[c70eb7c] | 139 | def update_data(self, prev_data, new_data): |
---|
[f444b20] | 140 | """ |
---|
| 141 | """ |
---|
[c70eb7c] | 142 | if prev_data.id not in self.stored_data.keys(): |
---|
[ae83ad3] | 143 | return None, {} |
---|
[c70eb7c] | 144 | data_state = self.stored_data[prev_data.id] |
---|
| 145 | self.stored_data[new_data.id] = data_state.clone() |
---|
| 146 | self.stored_data[new_data.id].data = new_data |
---|
| 147 | if prev_data.id in self.stored_data.keys(): |
---|
| 148 | del self.stored_data[prev_data.id] |
---|
| 149 | return prev_data.id, {new_data.id: self.stored_data[new_data.id]} |
---|
| 150 | |
---|
[ec611aad] | 151 | def update_theory(self, theory, data_id=None, state=None): |
---|
[e88ebfd] | 152 | """ |
---|
| 153 | """ |
---|
[ec611aad] | 154 | uid = data_id |
---|
| 155 | if data_id is None and theory is not None: |
---|
| 156 | uid = theory.id |
---|
| 157 | if uid in self.stored_data.keys(): |
---|
| 158 | data_state = self.stored_data[uid] |
---|
| 159 | else: |
---|
| 160 | data_state = DataState() |
---|
| 161 | data_state.uid = uid |
---|
| 162 | data_state.set_theory(theory_data=theory, theory_state=state) |
---|
| 163 | self.stored_data[uid] = data_state |
---|
| 164 | return {uid: self.stored_data[uid]} |
---|
| 165 | |
---|
[e88ebfd] | 166 | |
---|
[aebc4cc] | 167 | def get_message(self): |
---|
[3c44c66] | 168 | """ |
---|
[aebc4cc] | 169 | return message |
---|
[3c44c66] | 170 | """ |
---|
[aebc4cc] | 171 | return self.message |
---|
[3c44c66] | 172 | |
---|
[df22224] | 173 | def get_by_id(self, id_list=None): |
---|
[f444b20] | 174 | """ |
---|
| 175 | """ |
---|
[df22224] | 176 | _selected_data = {} |
---|
| 177 | _selected_theory_list = {} |
---|
| 178 | if id_list is None: |
---|
[e88ebfd] | 179 | return |
---|
[df22224] | 180 | for d_id in self.stored_data: |
---|
| 181 | for search_id in id_list: |
---|
[e88ebfd] | 182 | data_state = self.stored_data[d_id] |
---|
| 183 | data = data_state.data |
---|
| 184 | theory_list = data_state.get_theory() |
---|
[df22224] | 185 | if search_id == d_id: |
---|
| 186 | _selected_data[search_id] = data |
---|
| 187 | if search_id in theory_list.keys(): |
---|
| 188 | _selected_theory_list[search_id] = theory_list[search_id] |
---|
| 189 | |
---|
[e88ebfd] | 190 | return _selected_data, _selected_theory_list |
---|
| 191 | |
---|
[e6a93df] | 192 | |
---|
| 193 | def freeze(self, theory_id): |
---|
| 194 | """ |
---|
| 195 | """ |
---|
| 196 | return self.freeze_theory(self.stored_data.keys(), theory_id) |
---|
| 197 | |
---|
[c70eb7c] | 198 | def freeze_theory(self, data_id, theory_id): |
---|
| 199 | """ |
---|
| 200 | """ |
---|
[ee2b492] | 201 | selected_theory = {} |
---|
[c70eb7c] | 202 | for d_id in data_id: |
---|
| 203 | if d_id in self.stored_data: |
---|
| 204 | data_state = self.stored_data[d_id] |
---|
| 205 | theory_list = data_state.get_theory() |
---|
| 206 | for t_id in theory_id: |
---|
| 207 | if t_id in theory_list.keys(): |
---|
[ee2b492] | 208 | theory_data, theory_state = theory_list[t_id] |
---|
| 209 | new_theory = copy.deepcopy(theory_data) |
---|
[5080cda] | 210 | new_theory.id = time.time() |
---|
[e6a93df] | 211 | new_theory.is_data = True |
---|
[ee2b492] | 212 | selected_theory[new_theory.id] = DataState(new_theory) |
---|
| 213 | self.stored_data[new_theory.id] = selected_theory[new_theory.id] |
---|
[bffa3d0] | 214 | |
---|
[ee2b492] | 215 | return selected_theory |
---|
[c70eb7c] | 216 | |
---|
| 217 | |
---|
| 218 | def delete_data(self, data_id, theory_id=None, delete_all=False): |
---|
| 219 | """ |
---|
| 220 | """ |
---|
| 221 | if data_id in self.stored_data.keys(): |
---|
[213892bc] | 222 | data_state = self.stored_data[data_id] |
---|
[c70eb7c] | 223 | if data_state.data.name in self.data_name_dict: |
---|
| 224 | del self.data_name_dict[data_state.data.name] |
---|
| 225 | del self.stored_data[data_id] |
---|
| 226 | |
---|
[665c083] | 227 | self.delete_theory(data_id, theory_id) |
---|
[c70eb7c] | 228 | if delete_all: |
---|
| 229 | self.stored_data = {} |
---|
| 230 | self.data_name_dict = {} |
---|
[213892bc] | 231 | |
---|
[c70eb7c] | 232 | def delete_theory(self, data_id, theory_id): |
---|
[213892bc] | 233 | """ |
---|
| 234 | """ |
---|
[665c083] | 235 | for d_id in data_id: |
---|
| 236 | if d_id in self.stored_data: |
---|
| 237 | data_state = self.stored_data[d_id] |
---|
| 238 | theory_list = data_state.get_theory() |
---|
| 239 | if theory_id in theory_list.keys(): |
---|
| 240 | del theory_list[theory_id] |
---|
[e88ebfd] | 241 | |
---|
[6db811e] | 242 | |
---|
[f444b20] | 243 | def delete_by_id(self, id_list=None): |
---|
[3c44c66] | 244 | """ |
---|
[aebc4cc] | 245 | save data and path |
---|
[3c44c66] | 246 | """ |
---|
[f444b20] | 247 | for id in id_list: |
---|
| 248 | if id in self.stored_data: |
---|
| 249 | del self.stored_data[id] |
---|
[e88ebfd] | 250 | |
---|
[aebc4cc] | 251 | |
---|
[f444b20] | 252 | def get_by_name(self, name_list=None): |
---|
[3c44c66] | 253 | """ |
---|
[f444b20] | 254 | return a list of data given a list of data names |
---|
[3c44c66] | 255 | """ |
---|
[e88ebfd] | 256 | _selected_data = {} |
---|
[f444b20] | 257 | for selected_name in name_list: |
---|
[584c4c4] | 258 | for id, data_state in self.stored_data.iteritems(): |
---|
| 259 | if data_state.data.name == selected_name: |
---|
[e88ebfd] | 260 | _selected_data[id] = data_state.data |
---|
| 261 | return _selected_data |
---|
[f444b20] | 262 | |
---|
| 263 | def delete_by_name(self, name_list=None): |
---|
| 264 | """ |
---|
| 265 | save data and path |
---|
| 266 | """ |
---|
| 267 | for selected_name in name_list: |
---|
[584c4c4] | 268 | for id, data_state in self.stored_data.iteritems(): |
---|
| 269 | if data_state.data.name == selected_name: |
---|
| 270 | del self._selected_data[id] |
---|
| 271 | del self.stored_data[data.id] |
---|
[f444b20] | 272 | |
---|
[e88ebfd] | 273 | def get_data_state(self, data_id): |
---|
[f444b20] | 274 | """ |
---|
| 275 | Send list of selected data |
---|
| 276 | """ |
---|
[e88ebfd] | 277 | _selected_data_state = {} |
---|
| 278 | for id in data_id: |
---|
| 279 | if id in self.stored_data.keys(): |
---|
| 280 | _selected_data_state[id] = self.stored_data[id] |
---|
| 281 | return _selected_data_state |
---|
[f444b20] | 282 | |
---|
| 283 | def get_all_data(self): |
---|
| 284 | """ |
---|
| 285 | return list of all available data |
---|
| 286 | """ |
---|
[584c4c4] | 287 | return self.stored_data |
---|
[f444b20] | 288 | |
---|
| 289 | |
---|
[3c44c66] | 290 | |
---|