source: sasview/guiframe/data_loader.py @ 509af3d

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 509af3d was 509af3d, checked in by Gervaise Alina <gervyh@…>, 15 years ago

add condition check to append more data

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