def align_volume_multiscale(self,vidx=0,rad=2,debug=True): self.logger.info('align_volume_multiscale: Starting') self.logger.info('align_volume_multiscale: Getting volume from data store.') avol = np.abs(self.h5.get(self.data_block)[vidx,:,:,:]) avol = np.swapaxes(avol,0,1) avol = (avol-np.mean(avol,axis=0))/np.std(avol,axis=0) # NOTE TO SELF: DON'T F*****G FORGET TO REMOVE THIS, YOU F*****G IDIOT # IF YOU DO, IT WILL RUIN YOUR LIFE FOR A MONTH WHILE YOU DEBUG IT # NOTE TO SELF: DON'T F*****G FORGET TO REMOVE THIS, YOU F*****G IDIOT # IF YOU DO, IT WILL RUIN YOUR LIFE FOR A MONTH WHILE YOU DEBUG IT # NOTE TO SELF: DON'T F*****G FORGET TO REMOVE THIS, YOU F*****G IDIOT # IF YOU DO, IT WILL RUIN YOUR LIFE FOR A MONTH WHILE YOU DEBUG IT # NOTE TO SELF: DON'T F*****G FORGET TO REMOVE THIS, YOU F*****G IDIOT # IF YOU DO, IT WILL RUIN YOUR LIFE FOR A MONTH WHILE YOU DEBUG IT # NOTE TO SELF: DON'T F*****G FORGET TO REMOVE THIS, YOU F*****G IDIOT # IF YOU DO, IT WILL RUIN YOUR LIFE FOR A MONTH WHILE YOU DEBUG IT # NOTE TO SELF: DON'T F*****G FORGET TO REMOVE THIS, YOU F*****G IDIOT # IF YOU DO, IT WILL RUIN YOUR LIFE FOR A MONTH WHILE YOU DEBUG IT avol = avol[:,:-2,:] ndepth,nslow,nfast = avol.shape profile = np.zeros(self.profile.shape) profile[...] = self.profile[...] profile = (profile-np.mean(profile))/np.std(profile) if len(profile)>ndepth: profile = profile[:ndepth] if ndepth>len(profile): avol = avol[:len(profile),:,:] ndepth,nslow,nfast = avol.shape gross_profile = np.mean(np.mean(avol,axis=2),axis=1) gross_offset,gross_goodness = translation1(gross_profile,profile,debug=False) offset_submatrix = np.ones((nslow,nfast))*gross_offset goodness_submatrix = np.ones((nslow,nfast))*gross_goodness # def smooth(v,k): # # an interface to lateral_smooth_3d that handles the # # transposing to minimize confusion # return np.transpose(lateral_smooth_3d(np.transpose(avol_bc,(2,0,1)),k),(1,2,0)) def show_brightest_layer(v): # assumes depth is first dimension p = np.mean(np.mean(v,axis=2),axis=1) mdepth = np.argmax(p) layer = v[mdepth,:,:] plt.figure() plt.imshow(layer) plt.colorbar() # fft the model, for cross-correlation by broadcasting f0 = np.fft.fft(profile,axis=0) self.logger.info('align_volume_multiscale: model length %d, FFT length %d.'%(len(profile),len(f0))) started = False sz,sy,sx = avol.shape initial_step = float(max(sy,sx)) final_exp = -np.log(1.49999/initial_step) steps = np.round(200*np.exp(-np.linspace(0.0,final_exp,10))) for k in steps: self.logger.info('align_volume_multiscale: smoothing layers in the volume') smoothed_avol = lateral_smooth_3d(avol,k) # depth is still first dimension f1 = np.fft.fft(smoothed_avol,axis=0) f1c = f1.conjugate() # transpose depth to the final dimension: f1_transposed = np.transpose(f1,(1,2,0)) f1c_transposed = np.transpose(f1c,(1,2,0)) num_transposed = f0*f1c_transposed denom_transposed = np.abs(f0)*np.abs(f1_transposed) frac_transposed = num_transposed/denom_transposed # now transpose back: frac = np.transpose(frac_transposed,(2,0,1)) ir = np.abs(np.fft.ifft(frac,axis=0)) print np.mean(ir),np.max(ir),np.min(ir) goodness = np.max(ir,axis=0) tx = np.argmax(ir,axis=0) if not started: #goodness_final = goodness.copy() #tx_final = tx.copy() goodness_final = np.zeros(goodness.shape) tx_final = np.zeros(tx.shape) goodness_final[...] = goodness[...] tx_final[...] = tx[...] started = True better_goodness_mask = np.zeros(goodness.shape) small_shifts_mask = np.zeros(tx.shape) else: better_goodness = (goodness-goodness_final)>-.2 shift_limit = 10 small_shifts = np.abs(tx-tx_final)<shift_limit better_goodness_mask = np.zeros(goodness.shape) better_goodness_mask[np.where(better_goodness)] = 1.0 small_shifts_mask = np.zeros(tx.shape) small_shifts_mask[np.where(small_shifts)] = 1.0 refinements = better_goodness_mask*small_shifts_mask refined_idx = np.where(refinements) print refined_idx goodness_final[refined_idx] = goodness[refined_idx] tx_final[refined_idx] = tx[refined_idx] if debug: plt.clf() plt.subplot(2,4,1) plt.cla() plt.imshow(goodness-goodness_final,cmap='gray') plt.colorbar() plt.title('$\Delta$ current goodness k=%d'%k) plt.subplot(2,4,2) plt.cla() plt.imshow(tx-tx_final,cmap='gray') plt.colorbar() plt.title('$\Delta$ current tx k=%d'%k) plt.subplot(2,4,3) plt.cla() plt.imshow(better_goodness_mask,cmap='gray') plt.title('better_goodness_mask') plt.colorbar() plt.subplot(2,4,4) plt.cla() plt.imshow(refinements,cmap='gray') plt.title('refinements') plt.colorbar() plt.subplot(2,4,5) plt.cla() plt.imshow(goodness_final,cmap='gray') plt.colorbar() plt.title('overall goodness k=%d'%k) plt.subplot(2,4,6) plt.cla() plt.imshow(tx_final,cmap='gray') plt.colorbar() plt.title('overall tx k=%d'%k) plt.subplot(2,4,7) plt.cla() plt.imshow(small_shifts_mask,cmap='gray') plt.colorbar() plt.title('small_shifts_mask') plt.subplot(2,4,8) plt.cla() plt.imshow(np.mean(smoothed_avol,axis=2),cmap='gray') plt.colorbar() plt.show()
def align_volume(self,vidx=0,rad=5): self.logger.info('align_volume: Starting') self.logger.info('align_volume: Getting volume from data store.') avol = np.abs(self.h5.get(self.data_block)[vidx,:,:,:]) avol = np.swapaxes(avol,0,1) self.logger.info('align_volume: Smoothing volume with smoothing kernel of size %d pixels.'%rad) if rad: print time.time() avol = lateral_smooth_3d(avol,rad) print time.time() sys.exit() ndepth,nslow,nfast = avol.shape offset_submatrix = np.zeros((nslow,nfast)) goodness_submatrix = np.zeros((nslow,nfast)) profile = self.profile if len(profile)>ndepth: profile = profile[:ndepth] if ndepth>len(profile): avol = avol[:len(profile),:,:] ndepth,nslow,nfast = avol.shape x = [] y = [] z = [] w = [] for islow in range(nslow): pct_done = int(round(100*float(islow)/float(nslow))) if islow%10==0: self.logger.info('align_volume: Aligning A-scans, volume %d is %d percent done.'%(vidx,pct_done)) for ifast in range(nfast): test = avol[:,islow,ifast] offset,goodness = translation1(profile,test,debug=False) x.append(ifast) y.append(islow) z.append(offset) w.append(goodness) offset_submatrix[islow,ifast] = offset goodness_submatrix[islow,ifast] = goodness fitting = True if fitting: # revisit this later; may be of use ptile = 75 goodness_threshold=np.percentile(w,ptile) self.logger.info('align_volume: Goodness %dth percentile %0.3f used as threshold.'%(ptile,goodness_threshold)) valid = np.where(w>goodness_threshold)[0] x0 = x y0 = y self.logger.info('align_volume: Using %d of %d points (%d percent) for fit.'%(len(valid),len(w),float(len(valid))/float(len(w))*100)) x = np.array(x) y = np.array(y) z = np.array(z) w = np.array(w) x = x[valid] y = y[valid] z = z[valid] w = w[valid] mode='median_filter' if mode=='spline': self.logger.info('Spline fitting surface to A-line axial positions.') tck = bisplrep(x,y,z,w=w,xb=0,xe=nfast-1,yb=0,ye=nslow-1) self.logger.info('Evaluating spline function at A-line coordinates.') fit_surface = bisplev(np.arange(nslow),np.arange(nfast),tck) if mode=='polyfit2d': self.logger.info('Polynomial fitting surface to A-line axial positions.') p = polyfit2d(x,y,z,order=2) self.logger.info('Evaluating polynomial function at A-line coordinates.') xx,yy = np.meshgrid(np.arange(nfast),np.arange(nslow)) fit_surface = polyval2d(xx,yy,p) if mode=='median_filter': # This is a dumb way to fit. Use the goodness matrix, dummy, perhaps with 2D splines! self.logger.info('Median filtering to create a smoothed offset surface.') slow_height = np.median(offset_submatrix,axis=1) plt.figure() plt.plot(slow_height) plt.figure() debias = (offset_submatrix.T-slow_height).T plt.figure() plt.imshow(debias) plt.colorbar() fit_surface_1 = median_filter(offset_submatrix,(3,3)) plt.figure() plt.imshow(fit_surface_1) plt.title('straight fit') plt.colorbar() fit_surface_2 = (median_filter(debias,(3,3)).T+slow_height).T plt.figure() plt.imshow(fit_surface_2) plt.title('debiased fit') plt.colorbar() plt.show() sys.exit() if mode=='interp2d': self.logger.info('Using interp2d to create a smoothed offset surface.') interpolation_function = interp2d(x,y,z) fit_surface = interpolation_function(x0,y0) print fit_surface print fit_surface.shape # print fit_surface # print fit_surface.shape if True: clim = np.min(offset_submatrix),np.max(offset_submatrix) plt.figure() plt.imshow(offset_submatrix,interpolation='none',clim=clim) plt.colorbar() plt.figure() plt.imshow(fit_surface,interpolation='none',clim=clim) plt.colorbar() plt.figure() plt.imshow(offset_submatrix-fit_surface,interpolation='none') plt.colorbar() plt.show() #goodness_used = np.zeros(goodness_submatrix.shape) #goodness_used[np.where(goodness_submatrix>goodness_threshold)] = 1.0 else: fit_surface = offset_submatrix return offset_submatrix,goodness_submatrix,fit_surface
def align_volume_to_profile_multiscale(avol,profile,debug=False): # put depth first avol = np.swapaxes(avol,0,1) # for testing use a recognizable image: #avol = np.load('/home/rjonnal/code/octopod/kid_with_grenade_volume.npy') ndepth,nslow,nfast = avol.shape profile = (profile-np.mean(profile))/np.std(profile) if len(profile)>ndepth: print 'growing volume to to %d pixels'%len(profile) new_avol = np.ones((len(profile),nslow,nfast))*np.mean(avol) new_avol[:ndepth,:,:] = avol avol = new_avol elif ndepth>len(profile): print 'growing profile ot %d pixels'%ndepth new_profile = np.ones(ndepth)*np.mean(profile) new_profile[:len(profile)] = profile profile = new_profile else: print 'profile and volume match in depth' plen = len(profile) gross_profile = np.mean(np.mean(avol,axis=2),axis=1) gross_offset,gross_goodness = translation1(gross_profile,profile,debug=False) offset_submatrix = np.ones((nslow,nfast))*gross_offset goodness_submatrix = np.ones((nslow,nfast))*gross_goodness def show_brightest_layer(v,mdepth=None,tstring=''): if mdepth is None: # assumes depth is first dimension p = np.mean(np.mean(v,axis=2),axis=1) mdepth = np.argmax(p) print 'showing layer %d'%mdepth layer = v[mdepth,:,:] plt.figure() plt.imshow(layer) plt.title(tstring) plt.colorbar() return mdepth def show_four(v,tstring='',other=None): plt.figure() p = np.mean(np.mean(v,axis=2),axis=1) idx = np.argsort(p)[::-1] if other is not None: extrapanel = 1 else: extrapanel = 0 for k in range(4): depth = idx[k] plt.subplot(1,4+extrapanel,k+1) plt.imshow(v[depth,:,:],cmap='gray') plt.colorbar() plt.title(depth) if extrapanel: plt.subplot(1,5,5) plt.imshow(other) plt.colorbar() # fft the model, for cross-correlation by broadcasting f0 = np.fft.fft(profile,axis=0) started = False sz,sy,sx = avol.shape initial_step = float(max(sy,sx))/4.0 #final_exp = -np.log(1.49999/initial_step) #steps = np.round(100*np.exp(-np.linspace(0.0,final_exp,8))) #steps = np.array([50,40,30,25,20,15,10,8,6,5,4,3,2,1]) n_steps = 3 step_steepness = 5.0 steps = np.linspace(initial_step**(1.0/step_steepness),0.8,n_steps)**step_steepness/2.0 ellipticities = np.linspace(1.0,0.5**.25,n_steps)**4 #print steps # show_volume = True # if show_volume: # for s in range(avol.shape[1]): # plt.cla() # plt.imshow(avol[:,s,:],clim=clim,cmap='gray') # plt.pause(.001) for rad,ellip in zip(steps,ellipticities): smoothed_avol,kernel = lateral_smooth_3d(avol,rad,ellipticity=0.8) # depth is still first dimension #show_four(avol,other=kernel) #show_four(smoothed_avol,other=kernel) #plt.show() smoothed_avol = (smoothed_avol-np.mean(smoothed_avol,axis=0))/np.std(smoothed_avol,axis=0) #show_brightest_layer(smoothed_avol) f1 = np.fft.fft(smoothed_avol,axis=0) f1c = f1.conjugate() # transpose depth to the final dimension: f1_transposed = np.transpose(f1,(1,2,0)) f1c_transposed = np.transpose(f1c,(1,2,0)) num_transposed = f0*f1c_transposed denom_transposed = np.abs(f0)*np.abs(f1_transposed) denom_transposed[np.where(denom_transposed==0)]=1.0 frac_transposed = num_transposed/denom_transposed # now transpose back: frac = np.transpose(frac_transposed,(2,0,1)) ir = np.abs(np.fft.ifft(frac,axis=0)) goodness = np.max(ir,axis=0) tx = np.argmax(ir,axis=0) tx[np.where(tx>plen//2)] = tx[np.where(tx>plen//2)] - plen #if tx > shape // 2: # tx -= shape if not started: #goodness_final = goodness.copy() #tx_final = tx.copy() goodness_final = np.zeros(goodness.shape) tx_final = np.zeros(tx.shape) goodness_final[...] = goodness[...] tx_final[...] = tx[...] started = True better_goodness_mask = np.zeros(goodness.shape) small_shifts_mask = np.zeros(tx.shape) else: better_goodness = (goodness-goodness_final)>0.0 shift_limit = 10 small_shifts = np.abs(tx-tx_final)<shift_limit better_goodness_mask = np.zeros(goodness.shape) better_goodness_mask[np.where(better_goodness)] = 1.0 small_shifts_mask = np.zeros(tx.shape) small_shifts_mask[np.where(small_shifts)] = 1.0 refinements = better_goodness_mask*small_shifts_mask refined_idx = np.where(refinements) goodness_final[refined_idx] = goodness[refined_idx] tx_final[refined_idx] = tx[refined_idx] if debug: plt.subplot(1,2,1) plt.cla() plt.plot(np.mean(np.mean(smoothed_avol,axis=2),axis=1)) plt.plot(profile) plt.title(rad) plt.subplot(1,2,2) plt.cla() plt.imshow(tx_final) plt.title('%d,%d'%(tx_final.min(),tx_final.max())) plt.pause(1) if debug: plt.subplot(1,2,1) plt.cla() plt.plot(np.mean(np.mean(smoothed_avol,axis=2),axis=1)) plt.plot(profile) plt.subplot(1,2,2) plt.cla() plt.imshow(tx_final) plt.title('final %d,%d'%(tx_final.min(),tx_final.max())) plt.pause(1) return tx_final,goodness_final