1 | import os, sys,numpy |
---|
2 | import wx |
---|
3 | from dataFitting import Data1D, Theory1D |
---|
4 | from danse.common.plottools.plottables import Data2D |
---|
5 | |
---|
6 | from DataLoader.loader import Loader |
---|
7 | |
---|
8 | def 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 | |
---|
26 | def 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 | |
---|
74 | def 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 | name= output.filename |
---|
137 | |
---|
138 | |
---|
139 | #print "data_name_list",data_name_list |
---|
140 | new_plot.name = name |
---|
141 | ## allow to highlight data when plotted |
---|
142 | new_plot.interactive = True |
---|
143 | ## when 2 data have the same id override the 1 st plotted |
---|
144 | new_plot.id = name |
---|
145 | ## info is a reference to output of dataloader that can be used |
---|
146 | ## to save data 1D as cansas xml file |
---|
147 | new_plot.info= output |
---|
148 | ## detector used by dataLoader.manipulation module |
---|
149 | new_plot.detector =output.detector |
---|
150 | ## If the data file does not tell us what the axes are, just assume... |
---|
151 | new_plot.xaxis(output._xaxis,output._xunit) |
---|
152 | new_plot.yaxis(output._yaxis,output._yunit) |
---|
153 | ##group_id specify on which panel to plot this data |
---|
154 | new_plot.group_id = name |
---|
155 | ##post data to plot |
---|
156 | wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=str(name))) |
---|
157 | |
---|
158 | ## the output of the loader is a list , some xml files contain more than one data |
---|
159 | else: |
---|
160 | i=1 |
---|
161 | for item in output: |
---|
162 | try: |
---|
163 | dx=item.dx |
---|
164 | dxl=item.dxl |
---|
165 | dxw=item.dxw |
---|
166 | except: |
---|
167 | dx=None |
---|
168 | dxl=None |
---|
169 | dxw=None |
---|
170 | |
---|
171 | if item.dy ==None: |
---|
172 | new_plot = Theory1D(item.x,item.y,dxl,dxw) |
---|
173 | else: |
---|
174 | new_plot = Data1D(x=item.x,y=item.y,dx=dx,dy=item.dy,dxl=dxl,dxw=dxw) |
---|
175 | |
---|
176 | new_plot.source=item.source |
---|
177 | |
---|
178 | name= str(item.run[0]) |
---|
179 | |
---|
180 | new_plot.name = name |
---|
181 | new_plot.interactive = True |
---|
182 | new_plot.detector =item.detector |
---|
183 | # If the data file does not tell us what the axes are, just assume... |
---|
184 | new_plot.xaxis(item._xaxis,item._xunit) |
---|
185 | new_plot.yaxis(item._yaxis,item._yunit) |
---|
186 | new_plot.group_id = name |
---|
187 | new_plot.id = name |
---|
188 | new_plot.info= item |
---|
189 | |
---|
190 | if hasattr(item,"title"): |
---|
191 | title= item.title |
---|
192 | else: |
---|
193 | title= name |
---|
194 | wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=str(title))) |
---|
195 | i+=1 |
---|
196 | |
---|
197 | |
---|