def read2D(self, fname, tname): fp = open(fname, 'r') x = [] y = [] z = [] for l in fp: args = l.split('\t') if args[0][0].isalpha() == 0: x.append(float(args[0])) y.append(float(args[1])) z.append(float(args[2])) fp.close() x = np.array(x) y = np.array(y) z = np.array(z) data_x = np.unique(x) data_y = np.unique(y) data_2d = np.zeros((len(data_x),len(data_y))) if False: # accurate but very slow way of sorting for nx in range(len(data_x)): for ny in range(len(data_y)): data_2d[nx,ny] = z[(x==data_x[nx])*(y==data_y[ny])] else: for ny in range(len(data_y)): data_2d[:,ny] = z[ny*len(data_x):(ny+1)*len(data_x)] data = DataArray(name=tname, shape=list(data_2d.shape)) data.coords.append(data_x) data.coords.append(data_y) data.data = data_2d return [data]
def apply_filter(self, array): if len(array.shape)!=1: return False # High-pass filter p0 = min([np.floor(self.size/100.0*array.shape[0]), np.floor(array.shape[0]/2)]) # compute window wnd = DataArray(shape=[2*p0]) wnd.data = np.ones(2*p0) self.window.apply_filter(wnd) # take left half wnd.data[p0:] = max(wnd.data) # compute how much to add shp = max([0,array.shape[0]-2*p0]) if self.relative: # find max amplitude p1 = array.data.argmax() else: # take position from config p1 = np.floor((1.0-self.position/100.0)*array.shape[0]) if p1<p0: fct = np.concatenate((wnd.data,max(wnd.data)*np.ones(shp))) elif p1+p0>array.shape[0]: fct = np.concatenate((max(wnd.data)*np.zeros(shp),wnd.data)) else: fct = np.concatenate((np.zeros(array.shape[0]-p1-p0),wnd.data,np.ones(p1-p0)*max(wnd.data))) array.data = array.data*fct return True
def read1Dv(self, fname, tname): # open file and read column 0, 1 and 2 (variance), separator is tab data_t = [] data_x = [] data_v = [] fp = open(fname, 'r') for l in fp: args = l.split('\t') if args[0][0].isalpha() == 0: data_t.append(float(args[0])) data_x.append(float(args[1])) data_v.append(float(args[2])) fp.close() if len(data_v)<len(data_x): data_v = np.zeros(len(data_x)) data1 = DataArray(name=tname+" 0", shape=[len(data_t)]) data2 = DataArray(name=tname+" 1", shape=[len(data_t)]) data1.coords = [np.array(data_t)] data1.data = np.array(data_x) data1.shape = [len(data_x)] data2.coords = [np.array(data_t)] data2.data = np.array(data_v) data2.shape = [len(data_v)] return [data1, data2]
def read1D(self, fname, tname): # open file and read column 0 and 1, separator is tab data_t = [] data_x = [] fp = open(fname, 'r') for l in fp: args = l.split('\t') if args[0][0].isalpha() == 0: data_t.append(float(args[0])) data_x.append(float(args[1])) fp.close() data = DataArray(name=tname, shape=[len(data_t)]) data.coords = [np.array(data_t)] data.data = np.array(data_x) data.shape = [len(data_x)] return [data]
def GetDataArray(self): data = np.array(map(lambda x:np.array(x.data),self.columns)) arr = None if data.shape[0]==2: # 1D arr = DataArray(shape=[data.shape[1]],name=self.name) arr.name = self.name arr.coords[0] = data[0,:] arr.data = data[1,:] elif data.shape[0]==3: # 2D cx = np.unique(data[0,:]) cy = np.unique(data[1,:]) arr = DataArray(shape=[len(cx),len(cy)],name=self.name) arr.coords[0] = cx arr.coords[1] = cy arr.data = data[2,:].reshape((len(cx),len(cy))) return arr
def read_sheet(self, s): x = None if s.ncols == 2: # 1D x = DataArray(shape=[s.nrows]) for n in range(s.nrows): x.data[n] = s.cell(n, 1).value x.coords[0][n] = s.cell(n, 0).value elif s.ncols == 3: # 3D if not (isinstance(s.cell(0, 0).value, (int, long, float, complex))): # accept only numeric data return None c1 = np.zeros(s.nrows) c2 = np.zeros(s.nrows) d = np.zeros(s.nrows) for n in range(s.nrows): c1[n] = s.cell(n, 0).value c2[n] = s.cell(n, 1).value d[n] = s.cell(n, 2).value c1 = np.unique(c1) c2 = np.unique(c2) d = d.reshape((len(c1), len(c2))) x = DataArray(shape=[len(c1), len(c2)]) x.data = d return x
def ComputeReference(self, inst): """ Compute reference from given data if reference filter exists. Parameters: inst - array (DataArray) or pubsub event data inst.data must be DataArray """ if not(isinstance(inst,DataArray)): array = inst.data else: array = inst narray = DataArray(shape=array.shape[:]) narray.coords = array.coords[:] narray.data = array.data[:] # build reference data # first apply previous filter banks bank = self while bank.parent!=None: bank = bank.parent while bank!=self: narray = bank.ApplyFilters(narray) bank = bank.children[-1] # then apply current filter bank up to reference filter for ft in self.filters: if not(ft.is_reference): if ft.is_active: ft.apply_filter(narray) else: ft.ref = narray ft.source = array if inst!=array: pub.sendMessage("filter.change", data=self) # send filter change notification with filter bank as object break
def UpdateFilterDisplay(self, inst=None): """ Update display of apodization window. Parameters: inst - pubsub event data """ # update apodization filter preview arr = DataArray(shape=[201]) arr.coords = [np.linspace(-1.0,1.0,201)] arr.data = np.ones(201) is_before_ft = True if isinstance(self.bank,FilterBank): for ft in self.bank.filters: if ft.is_pre_filter and is_before_ft and ft.is_active: ft.apply_filter(arr) if ft.is_transform: is_before_ft = False data_x = np.concatenate(([-1.5,-1.0], arr.coords[0], [1.0,1.5])) data_y = np.concatenate(([0.0,0.0], arr.data, [0.0,0.0])) self.line_filter.set_data(data_x, data_y) self.plot_filter.draw()