source: sasview/guiframe/data_loader.py @ 910792f

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 910792f was 910792f, checked in by Jae Cho <jhjcho@…>, 15 years ago

Removed Image2D class

  • Property mode set to 100644
File size: 6.7 KB
Line 
1import os, sys,numpy
2import wx
3from dataFitting import Data1D, Theory1D
4from danse.common.plottools.plottables import Data2D
5from DataLoader.loader import Loader
6
7def choose_data_file(parent, location=None):
8    path = None
9    if location==None:
10        location = os.getcwd()
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)
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   
31        file_x = numpy.zeros(0)
32        file_y = numpy.zeros(0)
33        file_dy = numpy.zeros(0)
34        file_dx = numpy.zeros(0)
35       
36        input_f = open(path,'r')
37        buff = input_f.read()
38        lines = buff.split('\n')
39       
40        has_dy = False
41        has_dx = False
42       
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:
49                    has_dy = True
50                    errdy = float(toks[2])
51                else:
52                    errdy = 0.0
53                if len(toks)==4:
54                    has_dx = True
55                    errdx = float(toks[3])
56                else:
57                    errdx = 0.0
58                file_x  = numpy.append(file_x, x)
59                file_y  = numpy.append(file_y, y)
60                file_dy = numpy.append(file_dy, dyerr)
61                file_dx = numpy.append(file_dx, dxerr)
62            except:
63                print "READ ERROR", line
64   
65        if has_dy==False:
66            file_dy = None
67        if has_dx==False:
68            file_dx = None
69           
70        return file_x, file_y, file_dy, file_dx
71    return None, None, None
72
73def plot_data(parent, path):
74    """
75        Use the DataLoader loader to created data to plot.
76        @param path: the path of the data to load
77    """
78    from sans.guicomm.events import NewPlotEvent, StatusEvent
79    from DataLoader.loader import  Loader
80   
81    #Instantiate a loader
82    L = Loader()
83   
84    #Recieves data
85    try:
86        output=L.load(path)
87       
88    except:
89        wx.PostEvent(parent, StatusEvent(status="Problem loading file: %s" % sys.exc_value))
90        return
91    filename = os.path.basename(path)
92   
93    if not  output.__class__.__name__=="list":
94        ## smearing info
95        try:
96            dxl = output.dxl
97            dxw = output.dxw
98        except:
99            dxl = None
100            dxw = None
101        ## data 's name
102        if output.filename==None:
103            output.filename=str(filename)
104           
105        ## Creating a Data2D with output
106        if hasattr(output,'data'):
107            temp = output.err_data
108            temp[temp==0]=1
109            msg= "Loading 2D error bars of value 1 were added to"
110            wx.PostEvent(parent, StatusEvent(status=" %s %s"% (msg,output.filename)))
111            new_plot = Data2D(image=output.data,err_image=temp,
112                              xmin=output.xmin,xmax=output.xmax,
113                              ymin=output.ymin,ymax=output.ymax)
114            new_plot.x_bins=output.x_bins
115            new_plot.y_bins=output.y_bins
116           
117        ##Creating Data1D with output
118        else:
119            ##dy values checked
120            if output.dy ==None :
121                new_plot = Theory1D(output.x,output.y, dxl, dxw)
122               
123            elif len(output.dy[output.dy==0])==len(output.dy):
124                output.dy[output.dy==0]=1 
125                msg="Loading 1D error bars of value 1 were added to "
126                wx.PostEvent(parent, StatusEvent(status= "%s %s"%(msg, output.filename)))
127                new_plot = Theory1D(output.x,output.y,output.dy, dxl, dxw)
128            else:
129                msg="Loading 1D data: "
130                wx.PostEvent(parent, StatusEvent(status= "%s %s"%(msg, output.filename)))
131                new_plot = Data1D(x=output.x, y=output.y, dx=output.dx,
132                                  dy=output.dy, dxl=dxl, dxw=dxw)
133               
134        ## source will request in dataLoader .manipulation module
135        new_plot.source=output.source
136        ## name of the data allow to differentiate data when plotted
137        name= output.filename
138       
139           
140        #print "data_name_list",data_name_list
141        new_plot.name = name
142        ## allow to highlight data when plotted
143        new_plot.interactive = True
144        ## when 2 data have the same id override the 1 st plotted
145        new_plot.id = name
146        ## info is a reference to output of dataloader that can be used
147        ## to save  data 1D as cansas xml file
148        new_plot.info= output
149        ## detector used by dataLoader.manipulation module
150        new_plot.detector =output.detector
151        ## If the data file does not tell us what the axes are, just assume...
152        new_plot.xaxis(output._xaxis,output._xunit)
153        new_plot.yaxis(output._yaxis,output._yunit)
154        ##group_id specify on which panel to plot this data
155        new_plot.group_id = name
156        ##post data to plot
157        wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=str(name)))
158       
159    ## the output of the loader is a list , some xml files contain more than one data
160    else:
161        i=1
162        for item in output:
163            try:
164                dx=item.dx
165                dxl=item.dxl
166                dxw=item.dxw
167            except:
168                dx=None
169                dxl=None
170                dxw=None
171               
172            if item.dy ==None:
173                new_plot = Theory1D(item.x,item.y,dxl,dxw)
174            else:
175                new_plot = Data1D(x=item.x,y=item.y,dx=dx,dy=item.dy,dxl=dxl,dxw=dxw)
176           
177            new_plot.source=item.source
178           
179            name= str(item.run[0])
180           
181            new_plot.name = name
182            new_plot.interactive = True
183            new_plot.detector =item.detector
184            # If the data file does not tell us what the axes are, just assume...
185            new_plot.xaxis(item._xaxis,item._xunit)
186            new_plot.yaxis(item._yaxis,item._yunit)
187            new_plot.group_id = name
188            new_plot.id = name
189            new_plot.info= item
190           
191            if hasattr(item,"title"):
192                title= item.title
193            else:
194                title= name
195            wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=str(title)))
196            i+=1
197           
198           
Note: See TracBrowser for help on using the repository browser.