source: sasview/guiframe/data_loader.py @ 00d3528

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 00d3528 was 196608d, checked in by Gervaise Alina <gervyh@…>, 15 years ago

code style change very minor shouldn't affect anything

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