source: sasview/guiframe/data_loader.py @ bc3dd65d

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

Added dx (dQ) to read from data file if exists

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