[6d920cd] | 1 | import os, sys,numpy |
---|
[de9483d] | 2 | import wx |
---|
[81812d9] | 3 | from dataFitting import Data1D, Theory1D |
---|
| 4 | from danse.common.plottools.plottables import Data2D |
---|
| 5 | |
---|
[c7bc3e7] | 6 | from DataLoader.loader import Loader |
---|
[58eac5d] | 7 | |
---|
[de9483d] | 8 | def 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 | |
---|
| 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 | |
---|
[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] | 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 | """ |
---|
[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": |
---|
| 85 | try: |
---|
| 86 | dxl=output.dxl |
---|
| 87 | dxw=output.dxw |
---|
| 88 | except: |
---|
| 89 | dxl=None |
---|
| 90 | dxw=None |
---|
| 91 | if hasattr(output,'data'): |
---|
[a19cd4d] | 92 | temp = output.err_data |
---|
| 93 | temp[temp==0]=1 |
---|
| 94 | |
---|
[8bd764d] | 95 | wx.PostEvent(parent, StatusEvent(status="Loading 2D error bars of value 1 \ |
---|
| 96 | were added to : %s"% output.filename)) |
---|
[a19cd4d] | 97 | new_plot = Data2D(image=output.data,err_image=temp, |
---|
[81812d9] | 98 | xmin=output.xmin,xmax=output.xmax, |
---|
| 99 | ymin=output.ymin,ymax=output.ymax) |
---|
| 100 | new_plot.x_bins=output.x_bins |
---|
| 101 | new_plot.y_bins=output.y_bins |
---|
| 102 | else: |
---|
[6d920cd] | 103 | |
---|
[8bd764d] | 104 | if output.dy ==None : |
---|
[81812d9] | 105 | new_plot = Theory1D(output.x,output.y, dxl, dxw) |
---|
[8bd764d] | 106 | elif len(output.dy[output.dy==0])==len(output.dy): |
---|
| 107 | output.dy[output.dy==0]=1 |
---|
| 108 | wx.PostEvent(parent, StatusEvent(status="Loading 1D error bars of value 1 \ |
---|
| 109 | were added to : %s"%output.filename)) |
---|
| 110 | new_plot = Theory1D(output.x,output.y,output.dy, dxl, dxw) |
---|
[81812d9] | 111 | else: |
---|
[8bd764d] | 112 | new_plot = Data1D(x=output.x,y=output.y,dx=output.dx,dy=output.dy, dxl=dxl, dxw=dxw) |
---|
[6d920cd] | 113 | if output.filename==None: |
---|
| 114 | output.filename=str(filename) |
---|
[81812d9] | 115 | new_plot.source=output.source |
---|
[cd84dca] | 116 | new_plot.name = output.filename |
---|
[81812d9] | 117 | new_plot.interactive = True |
---|
[169460a] | 118 | new_plot.id = True |
---|
[81812d9] | 119 | new_plot.info= output |
---|
| 120 | if hasattr(output, "dxl"): |
---|
| 121 | new_plot.dxl = output.dxl |
---|
| 122 | if hasattr(output, "dxw"): |
---|
| 123 | new_plot.dxw = output.dxw |
---|
| 124 | new_plot.detector =output.detector |
---|
| 125 | |
---|
| 126 | # If the data file does not tell us what the axes are, just assume... |
---|
| 127 | new_plot.xaxis(output._xaxis,output._xunit) |
---|
| 128 | new_plot.yaxis(output._yaxis,output._yunit) |
---|
[ab8f936] | 129 | new_plot.group_id = output.filename |
---|
| 130 | new_plot.id = output.filename |
---|
[18eba35] | 131 | try: |
---|
| 132 | title=output.filename |
---|
| 133 | except: |
---|
| 134 | title= filename |
---|
[8bd764d] | 135 | |
---|
[18eba35] | 136 | wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=str(title))) |
---|
[81812d9] | 137 | else: |
---|
[768656e] | 138 | i=1 |
---|
[81812d9] | 139 | for item in output: |
---|
[cd84dca] | 140 | |
---|
[81812d9] | 141 | try: |
---|
| 142 | dxl=item.dxl |
---|
| 143 | dxw=item.dxw |
---|
| 144 | except: |
---|
| 145 | dxl=None |
---|
| 146 | dxw=None |
---|
[7a7bf55] | 147 | if item.dy ==None: |
---|
[81812d9] | 148 | new_plot = Theory1D(item.x,item.y,dxl,dxw) |
---|
| 149 | else: |
---|
[8bd764d] | 150 | new_plot = Data1D(x=item.x,y=item.y,dx=item.dx,dy=item.dy,dxl=dxl,dxw=dxw) |
---|
[81812d9] | 151 | |
---|
| 152 | new_plot.source=item.source |
---|
[cd84dca] | 153 | new_plot.name = str(item.run[0]) |
---|
[81812d9] | 154 | new_plot.interactive = True |
---|
| 155 | new_plot.detector =item.detector |
---|
| 156 | # If the data file does not tell us what the axes are, just assume... |
---|
| 157 | new_plot.xaxis(item._xaxis,item._xunit) |
---|
| 158 | new_plot.yaxis(item._yaxis,item._yunit) |
---|
[ab8f936] | 159 | new_plot.group_id = str(item.run[0]) |
---|
| 160 | new_plot.id = str(item.run[0]) |
---|
[6d920cd] | 161 | new_plot.info= item |
---|
| 162 | |
---|
[18eba35] | 163 | if hasattr(item,"title"): |
---|
| 164 | title= item.title |
---|
| 165 | else: |
---|
| 166 | title= str(item.run[0]) |
---|
| 167 | wx.PostEvent(parent, NewPlotEvent(plot=new_plot, title=str(title))) |
---|
[768656e] | 168 | i+=1 |
---|
[81812d9] | 169 | |
---|
| 170 | |
---|