source: sasview/guiframe/data_loader.py @ 1fbb343

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 1fbb343 was 12aa9b5, checked in by Gervaise Alina <gervyh@…>, 16 years ago

class and method commented

  • Property mode set to 100644
File size: 6.2 KB
RevLine 
[6d920cd]1import os, sys,numpy
[de9483d]2import wx
[81812d9]3from dataFitting import Data1D, Theory1D
4from danse.common.plottools.plottables import Data2D
5
[c7bc3e7]6from DataLoader.loader import Loader
[58eac5d]7
[de9483d]8def choose_data_file(parent, location=None):
9    path = None
10    if location==None:
11        location = os.getcwd()
[c7bc3e7]12   
13    l = Loader()
14    cards = l.get_wildcards()
15    wlist = '|'.join(cards)
16   
17    dlg = wx.FileDialog(parent, "Choose a file", location, "", wlist, wx.OPEN)
[de9483d]18    if dlg.ShowModal() == wx.ID_OK:
19        path = dlg.GetPath()
20        mypath = os.path.basename(path)
21    dlg.Destroy()
22   
23    return path
24
25
26def load_ascii_1D(path):
27    """
28        Load a 1D ascii file, with errors
29    """
30    if path and os.path.isfile(path):
31   
[fc2b91a]32        file_x = numpy.zeros(0)
33        file_y = numpy.zeros(0)
34        file_dy = 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
41       
[de9483d]42        for line in lines:
43            try:
44                toks = line.split()
45                x = float(toks[0])
46                y = float(toks[1])
47                if len(toks)==3:
[fc2b91a]48                    has_dy = True
[de9483d]49                    err = float(toks[2])
50                else:
51                    err = 0.0
[fc2b91a]52                file_x  = numpy.append(file_x, x)
53                file_y  = numpy.append(file_y, y)
54                file_dy = numpy.append(file_dy, err)
[de9483d]55            except:
56                print "READ ERROR", line
57   
[fc2b91a]58        if has_dy==False:
59            file_dy = None
60           
[de9483d]61        return file_x, file_y, file_dy
[fc2b91a]62    return None, None, None
63
[6d920cd]64def plot_data(parent, path):
65    """
66        Use the DataLoader loader to created data to plot.
67        @param path: the path of the data to load
68    """
[ccb560a]69    from sans.guicomm.events import NewPlotEvent, StatusEvent
[4102709]70    from DataLoader.loader import  Loader
[6d920cd]71   
[4102709]72    #Instantiate a loader
[81812d9]73    L = Loader()
[4102709]74   
75    #Recieves data
[ccb560a]76    try:
77        output=L.load(path)
[81812d9]78       
[ccb560a]79    except:
80        wx.PostEvent(parent, StatusEvent(status="Problem loading file: %s" % sys.exc_value))
81        return
[acb1ad1]82    filename = os.path.basename(path)
[cd84dca]83   
[81812d9]84    if not  output.__class__.__name__=="list":
[12aa9b5]85        ## smearing info
[81812d9]86        try:
[12aa9b5]87            dxl = output.dxl
88            dxw = output.dxw
[81812d9]89        except:
[12aa9b5]90            dxl = None
91            dxw = None
92        ## data 's name
93        if output.filename==None:
94            output.filename=str(filename)
95        ## Creating a Data2D with output
[81812d9]96        if hasattr(output,'data'):
[a19cd4d]97            temp = output.err_data
98            temp[temp==0]=1
[12aa9b5]99            msg= "Loading 2D error bars of value 1 were added to"
100            wx.PostEvent(parent, StatusEvent(status=" %s %s"% (msg,output.filename)))
[a19cd4d]101            new_plot = Data2D(image=output.data,err_image=temp,
[81812d9]102                              xmin=output.xmin,xmax=output.xmax,
103                              ymin=output.ymin,ymax=output.ymax)
104            new_plot.x_bins=output.x_bins
105            new_plot.y_bins=output.y_bins
[12aa9b5]106        ##Creating Data1D with output
[81812d9]107        else:
[12aa9b5]108            ##dy values checked
[8bd764d]109            if output.dy ==None :
[81812d9]110                new_plot = Theory1D(output.x,output.y, dxl, dxw)
[12aa9b5]111               
[8bd764d]112            elif len(output.dy[output.dy==0])==len(output.dy):
113                output.dy[output.dy==0]=1 
[12aa9b5]114                msg="Loading 1D error bars of value 1 were added to "
115                wx.PostEvent(parent, StatusEvent(status= "%s %s"%(msg, output.filename)))
[8bd764d]116                new_plot = Theory1D(output.x,output.y,output.dy, dxl, dxw)
[81812d9]117            else:
[12aa9b5]118                msg="Loading 1D data: "
119                wx.PostEvent(parent, StatusEvent(status= "%s %s"%(msg, output.filename)))
120                new_plot = Data1D(x=output.x, y=output.y, dx=output.dx,
121                                  dy=output.dy, dxl=dxl, dxw=dxw)
122               
123        ## source will request in dataLoader .manipulation module
[81812d9]124        new_plot.source=output.source
[12aa9b5]125        ## name of the data allow to differentiate data when plotted
[cd84dca]126        new_plot.name = output.filename
[12aa9b5]127        ## allow to highlight data when plotted
[81812d9]128        new_plot.interactive = True
[12aa9b5]129        ## when 2 data have the same id override the 1 st plotted
130        new_plot.id = output.filename
131        ## info is a reference to output of dataloader that can be used
132        ## to save  data 1D as cansas xml file
[81812d9]133        new_plot.info= output
[12aa9b5]134        ## detector used by dataLoader.manipulation module
[81812d9]135        new_plot.detector =output.detector
[12aa9b5]136        ## If the data file does not tell us what the axes are, just assume...
[81812d9]137        new_plot.xaxis(output._xaxis,output._xunit)
138        new_plot.yaxis(output._yaxis,output._yunit)
[12aa9b5]139        ##group_id specify on which panel to plot this data
[ab8f936]140        new_plot.group_id = output.filename
[12aa9b5]141        ##post data to plot
142        wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=str(output.filename)))
[8bd764d]143       
[12aa9b5]144    ## the output of the loader is a list , some xml files contain more than one data
[81812d9]145    else:
[768656e]146        i=1
[81812d9]147        for item in output:
148            try:
149                dxl=item.dxl
150                dxw=item.dxw
151            except:
152                dxl=None
153                dxw=None
[12aa9b5]154               
[7a7bf55]155            if item.dy ==None:
[81812d9]156                new_plot = Theory1D(item.x,item.y,dxl,dxw)
157            else:
[8bd764d]158                new_plot = Data1D(x=item.x,y=item.y,dx=item.dx,dy=item.dy,dxl=dxl,dxw=dxw)
[81812d9]159           
160            new_plot.source=item.source
[cd84dca]161            new_plot.name = str(item.run[0])
[81812d9]162            new_plot.interactive = True
163            new_plot.detector =item.detector
164            # If the data file does not tell us what the axes are, just assume...
165            new_plot.xaxis(item._xaxis,item._xunit)
166            new_plot.yaxis(item._yaxis,item._yunit)
[ab8f936]167            new_plot.group_id = str(item.run[0])
168            new_plot.id = str(item.run[0])
[6d920cd]169            new_plot.info= item
170           
[18eba35]171            if hasattr(item,"title"):
172                title= item.title
173            else:
174                title= str(item.run[0])
175            wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=str(title)))
[768656e]176            i+=1
[81812d9]177           
178           
Note: See TracBrowser for help on using the repository browser.