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 | |
---|
36 | input_f = open(path,'r') |
---|
37 | buff = input_f.read() |
---|
38 | lines = buff.split('\n') |
---|
39 | |
---|
40 | has_dy = False |
---|
41 | |
---|
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: |
---|
48 | has_dy = True |
---|
49 | err = float(toks[2]) |
---|
50 | else: |
---|
51 | err = 0.0 |
---|
52 | file_x = numpy.append(file_x, x) |
---|
53 | file_y = numpy.append(file_y, y) |
---|
54 | file_dy = numpy.append(file_dy, err) |
---|
55 | except: |
---|
56 | print "READ ERROR", line |
---|
57 | |
---|
58 | if has_dy==False: |
---|
59 | file_dy = None |
---|
60 | |
---|
61 | return file_x, file_y, file_dy |
---|
62 | return None, None, None |
---|
63 | |
---|
64 | def 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 | """ |
---|
69 | from sans.guicomm.events import NewPlotEvent, StatusEvent |
---|
70 | from DataLoader.loader import Loader |
---|
71 | |
---|
72 | #Instantiate a loader |
---|
73 | L = Loader() |
---|
74 | |
---|
75 | #Recieves data |
---|
76 | try: |
---|
77 | output=L.load(path) |
---|
78 | |
---|
79 | except: |
---|
80 | wx.PostEvent(parent, StatusEvent(status="Problem loading file: %s" % sys.exc_value)) |
---|
81 | return |
---|
82 | filename = os.path.basename(path) |
---|
83 | |
---|
84 | if not output.__class__.__name__=="list": |
---|
85 | ## smearing info |
---|
86 | try: |
---|
87 | dxl = output.dxl |
---|
88 | dxw = output.dxw |
---|
89 | except: |
---|
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 |
---|
96 | if hasattr(output,'data'): |
---|
97 | temp = output.err_data |
---|
98 | temp[temp==0]=1 |
---|
99 | msg= "Loading 2D error bars of value 1 were added to" |
---|
100 | wx.PostEvent(parent, StatusEvent(status=" %s %s"% (msg,output.filename))) |
---|
101 | new_plot = Data2D(image=output.data,err_image=temp, |
---|
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 |
---|
106 | ##Creating Data1D with output |
---|
107 | else: |
---|
108 | ##dy values checked |
---|
109 | if output.dy ==None : |
---|
110 | new_plot = Theory1D(output.x,output.y, dxl, dxw) |
---|
111 | |
---|
112 | elif len(output.dy[output.dy==0])==len(output.dy): |
---|
113 | output.dy[output.dy==0]=1 |
---|
114 | msg="Loading 1D error bars of value 1 were added to " |
---|
115 | wx.PostEvent(parent, StatusEvent(status= "%s %s"%(msg, output.filename))) |
---|
116 | new_plot = Theory1D(output.x,output.y,output.dy, dxl, dxw) |
---|
117 | else: |
---|
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 |
---|
124 | new_plot.source=output.source |
---|
125 | ## name of the data allow to differentiate data when plotted |
---|
126 | new_plot.name = output.filename |
---|
127 | ## allow to highlight data when plotted |
---|
128 | new_plot.interactive = True |
---|
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 |
---|
133 | new_plot.info= output |
---|
134 | ## detector used by dataLoader.manipulation module |
---|
135 | new_plot.detector =output.detector |
---|
136 | ## If the data file does not tell us what the axes are, just assume... |
---|
137 | new_plot.xaxis(output._xaxis,output._xunit) |
---|
138 | new_plot.yaxis(output._yaxis,output._yunit) |
---|
139 | ##group_id specify on which panel to plot this data |
---|
140 | new_plot.group_id = output.filename |
---|
141 | ##post data to plot |
---|
142 | wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=str(output.filename))) |
---|
143 | |
---|
144 | ## the output of the loader is a list , some xml files contain more than one data |
---|
145 | else: |
---|
146 | i=1 |
---|
147 | for item in output: |
---|
148 | try: |
---|
149 | dxl=item.dxl |
---|
150 | dxw=item.dxw |
---|
151 | except: |
---|
152 | dxl=None |
---|
153 | dxw=None |
---|
154 | |
---|
155 | if item.dy ==None: |
---|
156 | new_plot = Theory1D(item.x,item.y,dxl,dxw) |
---|
157 | else: |
---|
158 | new_plot = Data1D(x=item.x,y=item.y,dx=item.dx,dy=item.dy,dxl=dxl,dxw=dxw) |
---|
159 | |
---|
160 | new_plot.source=item.source |
---|
161 | new_plot.name = str(item.run[0]) |
---|
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) |
---|
167 | new_plot.group_id = str(item.run[0]) |
---|
168 | new_plot.id = str(item.run[0]) |
---|
169 | new_plot.info= item |
---|
170 | |
---|
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))) |
---|
176 | i+=1 |
---|
177 | |
---|
178 | |
---|