source: sasview/guiframe/data_loader.py @ 2f6c260

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 2f6c260 was 04349fe, checked in by Gervaise Alina <gervyh@…>, 15 years ago

working on the status bar

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