[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 |
---|
[c5e84fb] | 19 | from sans.guiframe.data_state import DataState |
---|
[75fbd17] | 20 | from sans.guiframe.utils import parse_name |
---|
[0348245] | 21 | import DataLoader.data_info as DataInfo |
---|
| 22 | from sans.guiframe.dataFitting import Data1D |
---|
| 23 | from sans.guiframe.dataFitting import Data2D |
---|
[f444b20] | 24 | |
---|
[3c44c66] | 25 | class DataManager(object): |
---|
| 26 | """ |
---|
[f444b20] | 27 | Manage a list of data |
---|
[3c44c66] | 28 | """ |
---|
[f444b20] | 29 | def __init__(self): |
---|
[3c44c66] | 30 | """ |
---|
[aebc4cc] | 31 | Store opened path and data object created at the loading time |
---|
[f444b20] | 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 |
---|
[4e9583c] | 36 | """ |
---|
[c5e84fb] | 37 | self._selected_data = {} |
---|
[f444b20] | 38 | self.stored_data = {} |
---|
[aebc4cc] | 39 | self.message = "" |
---|
[75fbd17] | 40 | self.data_name_dict = {} |
---|
[f444b20] | 41 | |
---|
[0348245] | 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 | |
---|
[75fbd17] | 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 | |
---|
[f444b20] | 107 | def add_data(self, data_list): |
---|
| 108 | """ |
---|
| 109 | receive a list of |
---|
| 110 | """ |
---|
[584c4c4] | 111 | self._selected_data = {} |
---|
[f444b20] | 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) |
---|
[c5e84fb] | 117 | data_state = DataState(data) |
---|
[584c4c4] | 118 | self._selected_data[data.id] = data_state |
---|
| 119 | self.stored_data[data.id] = data_state |
---|
[213892bc] | 120 | |
---|
[f444b20] | 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 |
---|
[3c44c66] | 133 | |
---|
[aebc4cc] | 134 | def get_message(self): |
---|
[3c44c66] | 135 | """ |
---|
[aebc4cc] | 136 | return message |
---|
[3c44c66] | 137 | """ |
---|
[aebc4cc] | 138 | return self.message |
---|
[3c44c66] | 139 | |
---|
[f444b20] | 140 | def get_by_id(self, id_list=None): |
---|
| 141 | """ |
---|
| 142 | get a list of data given a list of id |
---|
| 143 | """ |
---|
[584c4c4] | 144 | self._selected_data = {} |
---|
[f444b20] | 145 | for id in id_list: |
---|
| 146 | if id in self.stored_data: |
---|
[584c4c4] | 147 | self._selected_data[id] = self.stored_data[id] |
---|
[f444b20] | 148 | return self._selected_data |
---|
| 149 | |
---|
[213892bc] | 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 | """ |
---|
[6db811e] | 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 | |
---|
[f444b20] | 166 | def delete_by_id(self, id_list=None): |
---|
[3c44c66] | 167 | """ |
---|
[aebc4cc] | 168 | save data and path |
---|
[3c44c66] | 169 | """ |
---|
[f444b20] | 170 | for id in id_list: |
---|
| 171 | if id in self.stored_data: |
---|
| 172 | del self.stored_data[id] |
---|
[584c4c4] | 173 | if id in self._selected_data: |
---|
| 174 | del self._selected_data[id] |
---|
[aebc4cc] | 175 | |
---|
[f444b20] | 176 | def get_by_name(self, name_list=None): |
---|
[3c44c66] | 177 | """ |
---|
[f444b20] | 178 | return a list of data given a list of data names |
---|
[3c44c66] | 179 | """ |
---|
[584c4c4] | 180 | self._selected_data = {} |
---|
[f444b20] | 181 | for selected_name in name_list: |
---|
[584c4c4] | 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 |
---|
[f444b20] | 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: |
---|
[584c4c4] | 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] |
---|
[f444b20] | 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 | """ |
---|
[584c4c4] | 207 | return self.stored_data |
---|
[f444b20] | 208 | |
---|
| 209 | |
---|
[3c44c66] | 210 | |
---|