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