[b3644f3] | 1 | |
---|
[6d920cd] | 2 | import os, sys,numpy |
---|
[de9483d] | 3 | import wx |
---|
[a618972] | 4 | import re |
---|
[b3644f3] | 5 | |
---|
[ff3f900b] | 6 | from dataFitting import Data1D |
---|
| 7 | from dataFitting import Data2D |
---|
[c7bc3e7] | 8 | from DataLoader.loader import Loader |
---|
[b3644f3] | 9 | from load_thread import DataReader |
---|
| 10 | |
---|
[4e9583c] | 11 | from sans.guicomm.events import NewPlotEvent, StatusEvent |
---|
[b3644f3] | 12 | |
---|
[d792e813] | 13 | def enable_add_data(existing_panel, new_plot): |
---|
| 14 | """ |
---|
[d955bf19] | 15 | Enable append data on a plot panel |
---|
[d792e813] | 16 | """ |
---|
| 17 | is_theory = len(existing_panel.plots)<= 1 and \ |
---|
| 18 | existing_panel.plots.values()[0].__class__.__name__=="Theory1D" |
---|
| 19 | |
---|
| 20 | is_data2d = hasattr(new_plot, 'data') |
---|
| 21 | is_data1d = existing_panel.__class__.__name__ == "ModelPanel1D"\ |
---|
| 22 | and existing_panel.group_id is not None |
---|
| 23 | |
---|
| 24 | return is_data1d and not is_data2d and not is_theory |
---|
[58eac5d] | 25 | |
---|
[a618972] | 26 | def parse_name(name, expression): |
---|
| 27 | """ |
---|
[d955bf19] | 28 | remove "_" in front of a name |
---|
[a618972] | 29 | """ |
---|
| 30 | if re.match(expression, name) is not None: |
---|
| 31 | word = re.split(expression, name, 1) |
---|
| 32 | for item in word: |
---|
| 33 | if item.lstrip().rstrip() != '': |
---|
| 34 | return item |
---|
| 35 | else: |
---|
| 36 | return name |
---|
| 37 | |
---|
[de9483d] | 38 | def choose_data_file(parent, location=None): |
---|
[d955bf19] | 39 | """ |
---|
| 40 | """ |
---|
[de9483d] | 41 | path = None |
---|
[196608d] | 42 | if location == None: |
---|
[de9483d] | 43 | location = os.getcwd() |
---|
[c7bc3e7] | 44 | |
---|
| 45 | l = Loader() |
---|
| 46 | cards = l.get_wildcards() |
---|
| 47 | wlist = '|'.join(cards) |
---|
| 48 | |
---|
[4e9583c] | 49 | dlg = wx.FileDialog(parent, "Choose a file", location, "", wlist, wx.OPEN) |
---|
[de9483d] | 50 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 51 | path = dlg.GetPath() |
---|
| 52 | mypath = os.path.basename(path) |
---|
| 53 | dlg.Destroy() |
---|
| 54 | |
---|
[4e9583c] | 55 | return path |
---|
[de9483d] | 56 | |
---|
[d792e813] | 57 | def open_dialog_append_data(panel_name, data_name): |
---|
[b91c736] | 58 | """ |
---|
[d955bf19] | 59 | Pop up an error message. |
---|
| 60 | |
---|
| 61 | :param panel_name: the name of the current panel |
---|
| 62 | :param data_name: the name of the current data |
---|
| 63 | |
---|
[b91c736] | 64 | """ |
---|
| 65 | message = " Do you want to append %s data\n in "%(str(data_name)) |
---|
| 66 | message += " %s panel?\n\n"%(str(panel_name)) |
---|
| 67 | dial = wx.MessageDialog(None, message, 'Question', |
---|
| 68 | wx.YES_NO|wx.NO_DEFAULT|wx.ICON_QUESTION) |
---|
| 69 | if dial.ShowModal() == wx.ID_YES: |
---|
| 70 | return True |
---|
| 71 | else: |
---|
| 72 | return False |
---|
[4e9583c] | 73 | |
---|
[de9483d] | 74 | |
---|
[4e9583c] | 75 | def load_ascii_1D(path): |
---|
| 76 | """ |
---|
| 77 | Load a 1D ascii file, with errors |
---|
| 78 | """ |
---|
| 79 | if path and os.path.isfile(path): |
---|
| 80 | |
---|
| 81 | file_x = numpy.zeros(0) |
---|
| 82 | file_y = numpy.zeros(0) |
---|
| 83 | file_dy = numpy.zeros(0) |
---|
| 84 | file_dx = numpy.zeros(0) |
---|
| 85 | |
---|
| 86 | input_f = open(path,'r') |
---|
| 87 | buff = input_f.read() |
---|
| 88 | lines = buff.split('\n') |
---|
| 89 | |
---|
| 90 | has_dy = False |
---|
| 91 | has_dx = False |
---|
| 92 | |
---|
| 93 | for line in lines: |
---|
| 94 | try: |
---|
| 95 | toks = line.split() |
---|
| 96 | x = float(toks[0]) |
---|
| 97 | y = float(toks[1]) |
---|
| 98 | if len(toks)==3: |
---|
| 99 | has_dy = True |
---|
| 100 | errdy = float(toks[2]) |
---|
| 101 | else: |
---|
| 102 | errdy = 0.0 |
---|
| 103 | if len(toks) == 4: |
---|
| 104 | has_dx = True |
---|
| 105 | errdx = float(toks[3]) |
---|
| 106 | else: |
---|
| 107 | errdx = 0.0 |
---|
| 108 | file_x = numpy.append(file_x, x) |
---|
| 109 | file_y = numpy.append(file_y, y) |
---|
| 110 | file_dy = numpy.append(file_dy, dyerr) |
---|
| 111 | file_dx = numpy.append(file_dx, dxerr) |
---|
| 112 | except: |
---|
| 113 | print "READ ERROR", line |
---|
| 114 | |
---|
| 115 | if has_dy == False: |
---|
| 116 | file_dy = None |
---|
| 117 | if has_dx == False: |
---|
| 118 | file_dx = None |
---|
| 119 | |
---|
| 120 | return file_x, file_y, file_dy, file_dx |
---|
| 121 | return None, None, None, None |
---|
[fc2b91a] | 122 | |
---|
[cb0d17c] | 123 | def load_error(error=None): |
---|
| 124 | """ |
---|
[d955bf19] | 125 | Pop up an error message. |
---|
| 126 | |
---|
| 127 | :param error: details error message to be displayed |
---|
[cb0d17c] | 128 | """ |
---|
[0881f51] | 129 | message = "The data file you selected could not be loaded.\n" |
---|
[cb0d17c] | 130 | message += "Make sure the content of your file is properly formatted.\n\n" |
---|
| 131 | |
---|
| 132 | if error is not None: |
---|
| 133 | message += "When contacting the DANSE team, mention the following:\n%s" % str(error) |
---|
| 134 | |
---|
| 135 | dial = wx.MessageDialog(None, message, 'Error Loading File', wx.OK | wx.ICON_EXCLAMATION) |
---|
| 136 | dial.ShowModal() |
---|
| 137 | |
---|
[b3644f3] | 138 | def on_load_error(parent): |
---|
| 139 | """ |
---|
| 140 | """ |
---|
| 141 | wx.PostEvent(parent, StatusEvent(status="Load cancel..", info="warning", |
---|
| 142 | type="stop")) |
---|
[cc1ead1] | 143 | |
---|
[6d920cd] | 144 | def plot_data(parent, path): |
---|
| 145 | """ |
---|
[d955bf19] | 146 | Use the DataLoader loader to created data to plot. |
---|
| 147 | |
---|
| 148 | :param path: the path of the data to load |
---|
| 149 | |
---|
[6d920cd] | 150 | """ |
---|
[cc1ead1] | 151 | from sans.guicomm.events import NewPlotEvent, StatusEvent |
---|
| 152 | from DataLoader.loader import Loader |
---|
[f2776f6] | 153 | |
---|
[cc1ead1] | 154 | # Instantiate a loader |
---|
| 155 | L = Loader() |
---|
| 156 | |
---|
| 157 | # Load data |
---|
| 158 | try: |
---|
| 159 | output = L.load(path) |
---|
| 160 | except: |
---|
[4e9583c] | 161 | load_error(sys.exc_value) |
---|
[cc1ead1] | 162 | return |
---|
| 163 | |
---|
[cb0d17c] | 164 | # Notify user if the loader completed the load but no data came out |
---|
| 165 | if output == None: |
---|
[cc1ead1] | 166 | load_error("The data file appears to be empty.") |
---|
[cb0d17c] | 167 | return |
---|
[cc1ead1] | 168 | |
---|
| 169 | |
---|
[acb1ad1] | 170 | filename = os.path.basename(path) |
---|
[cd84dca] | 171 | |
---|
[196608d] | 172 | if not output.__class__.__name__ == "list": |
---|
[12aa9b5] | 173 | ## Creating a Data2D with output |
---|
[81812d9] | 174 | if hasattr(output,'data'): |
---|
[04349fe] | 175 | msg = "Loading 2D data: %s"%output.filename |
---|
| 176 | wx.PostEvent(parent, StatusEvent(status=msg, info="info", type="stop")) |
---|
[196608d] | 177 | new_plot = Data2D(image=None, err_image=None) |
---|
[ff3f900b] | 178 | |
---|
[81812d9] | 179 | else: |
---|
[b3644f3] | 180 | msg = "Loading 1D data: %s"%output.filename |
---|
| 181 | wx.PostEvent(parent, StatusEvent(status=msg, info="info", type="stop")) |
---|
[196608d] | 182 | new_plot = Data1D(x=[], y=[], dx=None, dy=None) |
---|
[ff3f900b] | 183 | |
---|
| 184 | new_plot.copy_from_datainfo(output) |
---|
| 185 | output.clone_without_data(clone=new_plot) |
---|
| 186 | |
---|
[25ccf33] | 187 | ## data 's name |
---|
[196608d] | 188 | if output.filename is None or output.filename == "": |
---|
[ff3f900b] | 189 | output.filename = str(filename) |
---|
[12aa9b5] | 190 | ## name of the data allow to differentiate data when plotted |
---|
[a618972] | 191 | name = parse_name(name=output.filename, expression="_") |
---|
[aebc4cc] | 192 | #if not name in parent.indice_load_data.keys(): |
---|
| 193 | # parent.indice_load_data[name] = 0 |
---|
| 194 | #else: |
---|
[25ccf33] | 195 | ## create a copy of the loaded data |
---|
[aebc4cc] | 196 | # parent.indice_load_data[name] += 1 |
---|
| 197 | # name = name +"[%i]"%parent.indice_load_data[name] |
---|
[ff3f900b] | 198 | |
---|
[35eeea8] | 199 | new_plot.name = name |
---|
[12aa9b5] | 200 | ## allow to highlight data when plotted |
---|
[81812d9] | 201 | new_plot.interactive = True |
---|
[12aa9b5] | 202 | ## when 2 data have the same id override the 1 st plotted |
---|
[35eeea8] | 203 | new_plot.id = name |
---|
[12aa9b5] | 204 | ##group_id specify on which panel to plot this data |
---|
[35eeea8] | 205 | new_plot.group_id = name |
---|
[196608d] | 206 | new_plot.is_data = True |
---|
[12aa9b5] | 207 | ##post data to plot |
---|
[b91c736] | 208 | title = output.filename |
---|
[ff3f900b] | 209 | if hasattr(new_plot,"title"): |
---|
[b91c736] | 210 | title = str(new_plot.title.lstrip().rstrip()) |
---|
[196608d] | 211 | if title == "": |
---|
| 212 | title = str(name) |
---|
[ff3f900b] | 213 | else: |
---|
| 214 | title = str(name) |
---|
[b91c736] | 215 | if hasattr(parent, "panel_on_focus") and not(parent.panel_on_focus is None): |
---|
| 216 | existing_panel = parent.panel_on_focus |
---|
| 217 | panel_name = existing_panel.window_caption |
---|
| 218 | data_name = new_plot.name |
---|
[d792e813] | 219 | if enable_add_data(existing_panel, new_plot): |
---|
| 220 | if open_dialog_append_data(panel_name, data_name): |
---|
[b91c736] | 221 | #add this plot the an existing panel |
---|
| 222 | new_plot.group_id = existing_panel.group_id |
---|
[aebc4cc] | 223 | parent.set_loaded_data(data_list=[(new_plot, path)]) |
---|
[12aa9b5] | 224 | ## the output of the loader is a list , some xml files contain more than one data |
---|
[81812d9] | 225 | else: |
---|
[768656e] | 226 | i=1 |
---|
[81812d9] | 227 | for item in output: |
---|
[b3644f3] | 228 | msg = "Loading 1D data: %s"%str(item.run[0]) |
---|
| 229 | wx.PostEvent(parent, StatusEvent(status=msg, info="info", type="stop")) |
---|
[81812d9] | 230 | try: |
---|
[196608d] | 231 | dx = item.dx |
---|
| 232 | dxl = item.dxl |
---|
| 233 | dxw = item.dxw |
---|
[81812d9] | 234 | except: |
---|
[196608d] | 235 | dx = None |
---|
| 236 | dxl = None |
---|
| 237 | dxw = None |
---|
[ff3f900b] | 238 | |
---|
| 239 | new_plot = Data1D(x=item.x,y=item.y,dx=dx,dy=item.dy) |
---|
| 240 | new_plot.copy_from_datainfo(item) |
---|
[1abcb04] | 241 | item.clone_without_data(clone=new_plot) |
---|
[ff3f900b] | 242 | new_plot.dxl = dxl |
---|
| 243 | new_plot.dxw = dxw |
---|
| 244 | |
---|
[a618972] | 245 | name = parse_name(name=str(item.run[0]), expression="_") |
---|
[83ee3851] | 246 | #if not name in parent.indice_load_data.keys(): |
---|
| 247 | # parent.indice_load_data[name] = 0 |
---|
| 248 | #else: |
---|
[25ccf33] | 249 | ## create a copy of the loaded data |
---|
[675e0ab] | 250 | |
---|
| 251 | #TODO: this is a very annoying feature. We should make this |
---|
| 252 | # an option. Excel doesn't do this. Why should we? |
---|
| 253 | # What is the requirement for this feature, and are the |
---|
| 254 | # counter arguments stronger? Is this feature developed |
---|
| 255 | # to please at least 80% of the users or a special few? |
---|
[83ee3851] | 256 | #parent.indice_load_data[name] += 1 |
---|
| 257 | #name = name + "(copy %i)"%parent.indice_load_data[name] |
---|
[25ccf33] | 258 | |
---|
[35eeea8] | 259 | new_plot.name = name |
---|
[81812d9] | 260 | new_plot.interactive = True |
---|
[35eeea8] | 261 | new_plot.group_id = name |
---|
| 262 | new_plot.id = name |
---|
[b91c736] | 263 | new_plot.is_data = True |
---|
| 264 | |
---|
[18eba35] | 265 | if hasattr(item,"title"): |
---|
[b91c736] | 266 | title = item.title.lstrip().rstrip() |
---|
[196608d] | 267 | if title == "": |
---|
| 268 | title = str(name) |
---|
[18eba35] | 269 | else: |
---|
[196608d] | 270 | title = name |
---|
[b91c736] | 271 | if hasattr(parent, "panel_on_focus") and not(parent.panel_on_focus is None): |
---|
| 272 | existing_panel = parent.panel_on_focus |
---|
| 273 | panel_name = existing_panel.window_caption |
---|
| 274 | data_name = new_plot.name |
---|
[d792e813] | 275 | if enable_add_data(existing_panel, new_plot): |
---|
| 276 | if open_dialog_append_data(panel_name, data_name): |
---|
[b91c736] | 277 | #add this plot the an existing panel |
---|
| 278 | new_plot.group_id = existing_panel.group_id |
---|
[aebc4cc] | 279 | parent.set_loaded_data(data_list=[(new_plot, path)]) |
---|
[768656e] | 280 | i+=1 |
---|
[4e9583c] | 281 | |
---|