source: sasview/guiframe/data_loader.py @ e9b12eaf

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since e9b12eaf was cc1ead1, checked in by Gervaise Alina <gervyh@…>, 15 years ago

remove thread when loading

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