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