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