def csplines(self): ''' Cubic spline interpolation of both the real and reciprocal space projector functions. ''' from scipy.interpolate import CubicSpline as cs # for reciprocal space projector functions, natural boundary condition # (Y'' = 0) is applied at both ends. self.spl_qproj = [ cs(self.proj_qgrid, qproj, bc_type='natural') for qproj in self.qprojs ] # for real space projector functions, natural boundary condition # (Y'' = 0) is applied at the point N. self.spl_rproj = [] for l, rproj in zip(self.proj_l, self.rprojs): # Copy from VASP pseudo.F, I don't know why y1p depend on "l". if l == 1: yp1 = (rproj[1] - rproj[0]) / (self.proj_rmax / self.NPSRNL) else: y1p = 0.0 self.spl_rproj.append( cs(self.proj_rgrid, rproj, bc_type=((1, y1p), (2, 0))))
def __interpolate(self, data, index): if self.interpolate == 'linear': x, y, z = data[:, 3 * index + 2], data[:, 3 * index + 3], data[:, 3 * index + 4] time = np.where(~np.isnan(x))[0] if time.size == 0: return data[:, 3 * index + 2:3 * index + 5] interp1d_x = interp1d(time, x[time], fill_value='extrapolate') interp1d_y = interp1d(time, y[time], fill_value='extrapolate') interp1d_z = interp1d(time, z[time], fill_value='extrapolate') time = [i for i in range(x.shape[0])] return np.vstack( (interp1d_x(time), interp1d_y(time), interp1d_z(time))).T # print(np.sum(np.isnan(interp1d_x(time))), np.sum(np.isnan(interp1d_y(time))), np.sum(np.isnan(interp1d_z(time)))) # if np.where(np.isnan(self.joints[joint]))[0].size > 0: # print(joint, self.trcpath) elif self.interpolate == 'spline': x, y, z = data[:, 3 * index + 2], data[:, 3 * index + 3], data[:, 3 * index + 4] time = np.where(~np.isnan(x))[0] if time.size == 0: return data[:, 3 * index + 2:3 * index + 5] spline_x = cs(time, x[time]) spline_y = cs(time, y[time]) spline_z = cs(time, z[time]) time = [i for i in range(x.shape[0])] return np.vstack( (spline_x(time), spline_y(time), spline_z(time))).T else: raise ValueError( "{0} is not defined as interpolation method".format( self.interpolate))
RD1=np.loadtxt("Eff_per_pixel_new.Dat") W_Eff=RD1.transpose()[0] E_Eff=RD1.transpose()[1] W=W_Eff[0:133265] E=E_Eff[0:133265] #**************************************************************** RD=np.loadtxt("Seeing.Dat") See=RD.transpose()[0] per_flux_enclosed=RD.transpose()[1] for i in range(len(See)): if (See[i]==float(e4.get())): per_flux_en=per_flux_enclosed[i] #**************************************************************** F_new=cs(lam,fl) F1=(F_new(W))*1e-3 star_flux=F1[66130] #star flux emitted at 5500A #print(W[53053],star_flux) scaled_flux=((F1)/star_flux)*3.62e-12 scaled_flux_mag=scaled_flux*(10**(-float(e3.get())/2.5)) """ plt.plot(W,F, "b",label="Emitted by source") plt.legend(loc=1) plt.xlabel("Lambda in A") plt.ylabel("flux in J/m2/sec") plt.show() plt.plot(W,scaled_flux, "b",label="scaled flux") plt.legend(loc=1)
areas.append([p.area for p in df.loc[:, 'geometry']]) volumes.append( vv([p.area for p in df.loc[:, 'geometry']], df.loc[:, 'mean_heigh'])) dates_str = [(p.split("_clustering")[0] if "GR" not in p else p.split("-GR_clustering")[0]).split("-")[-1] for p in paths] dates = [ datetime.datetime(int(d[:4]), int(d[4:6]), int(d[6:8]), int(d[8:10]), int(d[10:12])) for d in dates_str ] time_diffs = [(dates[i] - dates[0]).total_seconds() / (60 * 60 * 24) for i in range(len(dates))] fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2) ax1.set_title("Height") if smooth: s1 = cs(time_diffs, [np.nanmedian(heights[i]) for i in range(len(dfs))]) ax1.plot(np.arange(np.max(time_diffs)), s1(np.arange(np.max(time_diffs)))) s1_25 = cs(time_diffs, [np.nanpercentile(heights[i], 25) for i in range(len(dfs))]) s1_75 = cs(time_diffs, [np.nanpercentile(heights[i], 75) for i in range(len(dfs))]) ax1.fill_between(np.arange(np.max(time_diffs)), s1_25(np.arange(np.max(time_diffs))), s1_75(np.arange(np.max(time_diffs))), color="cyan") else: ax1.plot(time_diffs, [np.nanmedian(heights[i]) for i in range(len(dfs))]) ax1.fill_between( time_diffs, [np.nanpercentile(heights[i], 25) for i in range(len(dfs))], [np.nanpercentile(heights[i], 75) for i in range(len(dfs))],
def interpolate_dir(self, method='spline', save=False, **new_feature): new = {} # eliminate initial and last nan for interpolation #print(self.frame_num) #self.__adjust_time() #print(self.frame_num) # interpolation indivisually if method == 'spline': for key, feature in new_feature.items(): if self.frame_num == np.array(feature).shape[0]: new[key] = [] for var in feature: time = np.where(~np.isnan(var))[0] if time.shape[0] == 0: new[key].append(np.zeros(var.shape)) continue spline = cs(time, var[time]) time = [i for i in range(len(var))] tmp = spline(time) new[key].append( np.where(tmp < 0, 0, np.where(tmp > 1, 1, tmp))) else: raise TypeError( '{0}\'s shape which is {1} must be ({2},...)'.format( key, np.array(feature).shape, self.frame_num)) elif method == 'linear': for key, feature in new_feature.items(): if self.frame_num == np.array(feature).shape[0]: new[key] = [] for var in feature: time = np.where(~np.isnan(var))[0] if time.shape[0] == 0: new[key].append(np.zeros(var.shape)) continue interp1d_ = interp1d(time, var[time], fill_value="extrapolate") time = [i for i in range(len(var))] tmp = interp1d_(time) if key == 'length': new[key].append( np.where(tmp <= 0, 0.001, np.where(tmp > 1, 1, tmp))) else: new[key].append( np.where(tmp < -1, -1, np.where(tmp > 1, 1, tmp))) else: raise TypeError( '{0}\'s shape which is {1} must be ({2},...)'.format( key, np.array(feature).shape, self.frame_num)) else: print("warning: {0} is not defined as interpolation method".format( method)) for key in new.keys(): new[key] = np.array(new[key]) try: l = np.sqrt(new['dirx'] * new['dirx'] + new['diry'] * new['diry']) new['dirx'] /= l new['diry'] /= l except KeyError: pass #for var in args: # exec 'self.{0} = new[{0}]'.format(var) in {} if save: x = [[] for i in range(18)] y = [[] for i in range(18)] # [between joints][time] newx = new['dirx'].T newy = new['diry'].T length = new['length'].T # here is restoring angle, length into x,y #Lines = [[1, 0],[1, 2],[2, 3],[3, 4],[1, 5],[5, 6],[6, 7],[1, 8],[8, 9],[9, 10],[1, 11],[11, 12], #[12, 13], [0, 14],[14, 16],[0, 15],[15, 17]] x[1] = np.zeros(self.frame_num) y[1] = np.zeros(self.frame_num) for joint_index, bet_index in zip([0, 2, 5, 8, 11], [0, 1, 4, 7, 10]): x[joint_index] = newx[bet_index] * length[bet_index] y[joint_index] = newy[bet_index] * length[bet_index] jointlists = [[1, 2, 3, 4], [1, 5, 6, 7], [1, 8, 9, 10], [1, 11, 12, 13], [0, 14, 16], [0, 15, 17]] betlists = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14], [15, 16]] for jointlist, betlist in zip(jointlists, betlists): for index, bet_index in zip(range(1, len(jointlist)), betlist): x[jointlist[index]] = newx[bet_index] * length[ bet_index] + x[jointlist[index - 1]] y[jointlist[index]] = newy[bet_index] * length[ bet_index] + y[jointlist[index - 1]] self.save(path='bag/interpolate-{0}/{1}'.format( method, self.videopath.split('/')[-1]), plot=(np.array(x), np.array(y))) return new """
def interpolate(self, method='spline', update=True, save=False): newx = [] newy = [] newc = [] # eliminate initial and last nan for interpolation # print(self.frame_num) # self.__adjust_time() # print(self.frame_num) # interpolation indivisually if method == 'spline': for x, y, c in zip(self.x, self.y, self.c): time = np.where(~np.isnan(x))[0] if time.shape[0] == 0: newx.append(np.zeros(x.shape)) newy.append(np.zeros(y.shape)) newc.append(np.zeros(c.shape)) continue spline_x = cs(time, x[time]) spline_y = cs(time, y[time]) spline_c = cs(time, c[time]) time = [i for i in range(len(x))] tmp = spline_x(time) newx.append(np.where(tmp < 0, 0, np.where(tmp > 1, 1, tmp))) tmp = spline_y(time) newy.append(np.where(tmp < 0, 0, np.where(tmp > 1, 1, tmp))) tmp = spline_c(time) newc.append(np.where(tmp < 0, 0, np.where(tmp > 1, 1, tmp))) elif method == 'linear': for x, y, c in zip(self.x, self.y, self.c): time = np.where(~np.isnan(x))[0] if time.shape[0] == 0: newx.append(np.zeros(x.shape)) newy.append(np.zeros(y.shape)) newc.append(np.zeros(c.shape)) continue interp1d_x = interp1d(time, x[time], fill_value='extrapolate') interp1d_y = interp1d(time, y[time], fill_value='extrapolate') interp1d_c = interp1d(time, c[time], fill_value='extrapolate') time = [i for i in range(len(x))] tmp = interp1d_x(time) newx.append(np.where(tmp < 0, 0, np.where(tmp > 1, 1, tmp))) tmp = interp1d_y(time) newy.append(np.where(tmp < 0, 0, np.where(tmp > 1, 1, tmp))) tmp = interp1d_c(time) newc.append(np.where(tmp < 0, 0, np.where(tmp > 1, 1, tmp))) else: print("warning: {0} is not defined as interpolation method".format( method)) newx = np.array(newx) newy = np.array(newy) newc = np.array(newc) if save: self.save(path='bag/interpolate-{0}/{1}'.format( method, self.videopath.split('/')[-1]), plot=(newx, newy)) if update: self.x = newx self.y = newy self.c = newc else: return newx, newy, newc
def shift(spikes, peaks, res=100, way='peak'): ''' Shift data series in order to align maximum/minimum with serie indices. Parameters ---------- spikes : np.array(shape(n, dim)) Time serie. peaks : list Some pathological spikes shifts could result in non realistic spike shapes. Those cases will be deleted from the list of available peaks res : int Resolution points to compute shift. way : str() if 'valley', the shift will align with global minima if 'peak', the shift will align with global maxima Returns ------- out : list() (shape(n-deleted spikes, dim)) List of spikes shifted to be aligned with max/min. peaks : list() List containing the indices of peaks in raw data (pathological cases errased) ''' # lenght of each spike vector size = len(spikes[0]) # steps vector t = np.arange(0, size, 1./res) # output out = list() # find minima for i, spk in enumerate(spikes): # build cubic splines function f = cs(range(size), spk, bc_type='natural') # compute max/min displacement if way=='valley': disp = np.argmin(f(t)) /res - np.argmin(spk) else: disp = np.argmax(f(t)) /res - np.argmax(spk) # delete spike if minimum moved further than one position if abs(disp)>1: out.append([]) else: out.append(f(np.arange(size)+disp)) # print message with deleted number of spikes deleted = len([True for spk in out if len(spk)==0]) print(' Interpolated spike deleted: \t%d' % deleted) # delete pathological cases peaks = [pt for spk, pt in zip(out, peaks) if len(spk)!=0] out = [spk for spk in out if len(spk)!=0] # convert back to integer type out = [np.round(spk).astype('int16') for spk in out] return out, peaks
def buildNormalSpline(self): x = self.x_points y = self.y_points spline = cs(x, y, bc_type='natural') return spline