source: sasview/guiframe/data_loader.py @ 675e0ab

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 675e0ab was 675e0ab, checked in by Mathieu Doucet <doucetm@…>, 15 years ago

guiframe: added TODO suggesting to remove very annoying feature.

  • Property mode set to 100644
File size: 8.2 KB
RevLine 
[6d920cd]1import os, sys,numpy
[de9483d]2import wx
[81812d9]3from dataFitting import Data1D, Theory1D
4from danse.common.plottools.plottables import Data2D
[c7bc3e7]5from DataLoader.loader import Loader
[58eac5d]6
[de9483d]7def choose_data_file(parent, location=None):
8    path = None
9    if location==None:
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
53                if len(toks)==4:
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   
[fc2b91a]65        if has_dy==False:
66            file_dy = None
[bc3dd65d]67        if has_dx==False:
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
[6d920cd]73def plot_data(parent, path):
74    """
75        Use the DataLoader loader to created data to plot.
[1abcb04]76       
77        TODO: this method needs a major cleanup.
78        It's a complete mess. It needs:
79          - to be cleaned of code duplication
80          - to be cleaned of data duplication: we should not
81            need to reference all the Data1D data members.
82            It should be sufficient to use the Data1D class
83            defined in dataFitting (which btw needs a better name).
84       
[6d920cd]85        @param path: the path of the data to load
86    """
[ccb560a]87    from sans.guicomm.events import NewPlotEvent, StatusEvent
[4102709]88    from DataLoader.loader import  Loader
[f2776f6]89   
[4102709]90    #Instantiate a loader
[81812d9]91    L = Loader()
[4102709]92   
93    #Recieves data
[ccb560a]94    try:
95        output=L.load(path)
[81812d9]96       
[ccb560a]97    except:
98        wx.PostEvent(parent, StatusEvent(status="Problem loading file: %s" % sys.exc_value))
99        return
[7c427a6]100    if output==None:
101        msg="Could not open this page"
102        wx.PostEvent(parent, StatusEvent(status=msg))
103        return
[acb1ad1]104    filename = os.path.basename(path)
[cd84dca]105   
[81812d9]106    if not  output.__class__.__name__=="list":
[12aa9b5]107        ## smearing info
[81812d9]108        try:
[12aa9b5]109            dxl = output.dxl
110            dxw = output.dxw
[81812d9]111        except:
[12aa9b5]112            dxl = None
113            dxw = None
[25ccf33]114       
[ecd74a1]115           
[12aa9b5]116        ## Creating a Data2D with output
[81812d9]117        if hasattr(output,'data'):
[b86c920]118           
119            new_plot = Data2D(image=output.data,err_image=output.err_data,
[81812d9]120                              xmin=output.xmin,xmax=output.xmax,
121                              ymin=output.ymin,ymax=output.ymax)
122            new_plot.x_bins=output.x_bins
123            new_plot.y_bins=output.y_bins
[ecd74a1]124           
[12aa9b5]125        ##Creating Data1D with output
[81812d9]126        else:
[12aa9b5]127            ##dy values checked
[7c427a6]128            dy= output.dy
129            if dy ==None:
130                dy= numpy.zeros(len(output.y))
131           
132            msg="Loading 1D data: "
133            wx.PostEvent(parent, StatusEvent(status= "%s %s"%(msg, output.filename)))
134            new_plot = Data1D(x=output.x, y=output.y, dx=output.dx,
135                              dy= dy, dxl=dxl, dxw=dxw)
[1abcb04]136            # Copy all the data to the new object so that the
137            # new_plot object properly behaves as a data_info.Data1D.
138            # This also means that we should not have to copy all
139            # the data below...
140            output.clone_without_data(clone=new_plot)
[12aa9b5]141               
142        ## source will request in dataLoader .manipulation module
[81812d9]143        new_plot.source=output.source
[25ccf33]144        ## data 's name
145        if output.filename==None:
146            output.filename=str(filename)
[12aa9b5]147        ## name of the data allow to differentiate data when plotted
[35eeea8]148        name= output.filename
[25ccf33]149        if not name in parent.indice_load_data.keys():
150            parent.indice_load_data[name]=0
151        else:
152            ## create a copy of the loaded data
153            parent.indice_load_data[name]+=1
154            name = name +"(copy %i)"%parent.indice_load_data[name]
155           
[35eeea8]156        new_plot.name = name
[12aa9b5]157        ## allow to highlight data when plotted
[81812d9]158        new_plot.interactive = True
[12aa9b5]159        ## when 2 data have the same id override the 1 st plotted
[35eeea8]160        new_plot.id = name
[12aa9b5]161        ## info is a reference to output of dataloader that can be used
162        ## to save  data 1D as cansas xml file
[81812d9]163        new_plot.info= output
[12aa9b5]164        ## detector used by dataLoader.manipulation module
[81812d9]165        new_plot.detector =output.detector
[12aa9b5]166        ## If the data file does not tell us what the axes are, just assume...
[81812d9]167        new_plot.xaxis(output._xaxis,output._xunit)
168        new_plot.yaxis(output._yaxis,output._yunit)
[12aa9b5]169        ##group_id specify on which panel to plot this data
[35eeea8]170        new_plot.group_id = name
[5ee2306]171        new_plot.is_data =True
[12aa9b5]172        ##post data to plot
[35eeea8]173        wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=str(name)))
[8bd764d]174       
[12aa9b5]175    ## the output of the loader is a list , some xml files contain more than one data
[1abcb04]176    #TODO: refactor this so that there is no duplication of code.
177    # There is no reason why the code above shouldn't be put in
178    # a private method to be used within the loop below.
[81812d9]179    else:
[768656e]180        i=1
[81812d9]181        for item in output:
182            try:
[bc3dd65d]183                dx=item.dx
[81812d9]184                dxl=item.dxl
185                dxw=item.dxw
186            except:
[bc3dd65d]187                dx=None
[81812d9]188                dxl=None
189                dxw=None
[7c427a6]190            dy = item.dy 
191            if dy ==None:
192                dy= numpy.zeros(len(item.y))
193               
194            new_plot = Data1D(x=item.x,y=item.y,dx=dx,dy=item.dy,dxl=dxl,dxw=dxw)
[1abcb04]195            # Copy all the data to the new object
196            item.clone_without_data(clone=new_plot)
[81812d9]197            new_plot.source=item.source
[35eeea8]198           
199            name= str(item.run[0])
[25ccf33]200            if not name in parent.indice_load_data.keys():
201                parent.indice_load_data[name]=0
202            else:
203                ## create a copy of the loaded data
[675e0ab]204               
205                #TODO: this is a very annoying feature. We should make this
206                # an option. Excel doesn't do this. Why should we?
207                # What is the requirement for this feature, and are the
208                # counter arguments stronger? Is this feature developed
209                # to please at least 80% of the users or a special few?
[25ccf33]210                parent.indice_load_data[name]+=1
211                name = name +"(copy %i)"%parent.indice_load_data[name]
212               
[35eeea8]213            new_plot.name = name
[81812d9]214            new_plot.interactive = True
215            new_plot.detector =item.detector
216            # If the data file does not tell us what the axes are, just assume...
217            new_plot.xaxis(item._xaxis,item._xunit)
218            new_plot.yaxis(item._yaxis,item._yunit)
[35eeea8]219            new_plot.group_id = name
220            new_plot.id = name
[6d920cd]221            new_plot.info= item
[5ee2306]222            new_plot.is_data =True
[18eba35]223            if hasattr(item,"title"):
224                title= item.title
225            else:
[35eeea8]226                title= name
[18eba35]227            wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=str(title)))
[768656e]228            i+=1
[81812d9]229           
230           
Note: See TracBrowser for help on using the repository browser.