def load_mask(self): ''' mask Jcam to isolate the brain from background ''' if self.absmaskpath: print 'loading mask from ' + str(self.absmaskpath) mask = tiff.imread(self.absmaskpath) # mask must be 8bit thresholded as 0 & 255 mask = mask/255 mask_savepath = self.absmaskpath else: # create mask automatically from top %50 pixels in histogram if self.img_source == 'npy': #cut off file extension mask_savepath = os.path.join(self.savepath, self.filename[:-4]+ '_' + self.special_string + self.maskname) else: mask_savepath = os.path.join(self.savepath, self.filename+ '_' + self.special_string + self.maskname) open_tb = tb.open_file(self.mask_moviepath, 'r') mask_mov = open_tb.root.data if len(np.shape(mask_mov)) == 2: frame = mask_mov[:,:] else: frame = mask_mov[0,:,:] if self.dsfactor != 1: frame = block_reduce(frame, block_size=(self.dsfactor,self.dsfactor), func=np.mean) del mask_mov mask = Jcorr.generate_mask(self, frame, 50) #mask_tosave = mask.astype('uint8') #mask_tosave = mask_tosave*255 # for historical reasons, the mask is either 0 and 255 #tiff.imsave(mask_savepath, mask_tosave) return mask, mask_savepath
def get_masks(self): ''' load masks ''' maskpath = os.path.join(self.basepath, self.maskname) mask = tiff.imread( maskpath) # mask must be 8bit thresholded as 0 & 255 mask = mask / 255 masky = mask.shape[0] maskx = mask.shape[1] maskpathleft = os.path.join(self.basepath, self.masknameleft) maskpathright = os.path.join(self.basepath, self.masknameright) maskleft = tiff.imread(maskpathleft) maskleft = maskleft / 255 maskright = tiff.imread(maskpathright) maskright = maskright / 255 return mask, masky, maskx, maskleft, maskright
def get_masks(self): ''' load masks ''' #maskpath = os.path.join(self.basepath,self.maskname) #print maskpath mask = tiff.imread( self.maskname) # mask must be 8bit thresholded as 0 & 255 mask = mask / 255 masky = mask.shape[0] maskx = mask.shape[1] return mask, masky, maskx
def load_mask(self): ''' mask Jcam to isolate the brain from background ''' if self.importmask: mask = tiff.imread( self.absmaskpath) # mask must be 8bit thresholded as 0 & 255 mask = mask / 255 mask_savepath = self.absmaskpath else: # create mask automatically from top %50 pixels in histogram if self.img_source == 'npy': #cut off file extension mask_savepath = os.path.join( self.savepath, self.filename[:-4] + '_' + self.special_string + self.maskname) else: mask_savepath = os.path.join( self.savepath, self.filename + '_' + self.special_string + self.maskname) mask_mov = Jcorr.load_mov(self, frameNum=1) if self.transposed: frame = mask_mov[:, 0].reshape( [np.sqrt(mask_mov.shape[0]), np.sqrt(mask_mov.shape[0])]) else: if len(np.shape(mask_mov)) == 2: frame = mask_mov[:, :] else: frame = mask_mov[0, :, :] if self.dsfactor != 1: frame = block_reduce(frame, block_size=(self.dsfactor, self.dsfactor), func=np.mean) del mask_mov mask = Jcorr.generate_mask(self, frame, 50) mask_tosave = mask.astype('uint8') mask_tosave = mask_tosave * 255 # for historical reasons, the mask is either 0 and 255 tiff.imsave(mask_savepath, mask_tosave) return mask, mask_savepath
def ImportMappingTif( self, path ): #used to import tiff stacks, most often with vasculature maps imageFile = tiff.imread(path) exposureTime = 10 return imageFile, exposureTime
def thresh_corr(all_corr, maskpathleft, maskpathright, corr_thresh): maskleft = tiff.imread(maskpathleft) maskleft = maskleft / 255 maskright = tiff.imread(maskpathright) maskright = maskright / 255 all_centroids_l, all_centroids_r, submask_l, submask_r = thresh_corr_centroids( all_corr, maskleft, maskright, corr_thresh) all_submask = submask_l + submask_r all_centroids = np.concatenate((all_centroids_l, all_centroids_r), axis=1) if len(corr_thresh) > 1: cost, rand_dist = euclidean_cost_centroids(all_centroids, corr_thresh) #min_thresh = np.nonzero(cost == np.min(cost[1:-1]))[0].astype('i2') # index of optiminum threshold #opt_thresh = corr_thresh[min_thresh] # the actual value of optimum threshold opt_thresh = [.75] min_thresh = np.nonzero(corr_thresh == opt_thresh)[0] opt_centroids = np.squeeze(all_centroids[min_thresh, :, :]) #pdb.set_trace() submask_l, submask_r, l_db_cores, r_db_cores, uber_centroids, all_label_map = cluster_using_DBSCAN( np.squeeze(all_centroids_l[min_thresh, :, :]), np.squeeze(all_centroids_r[min_thresh, :, :]), maskleft, maskright, min_thresh) fig5 = plt.figure(figsize=(12, 4)) fig5.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=.05, wspace=.05) uber_masks = np.zeros([len(uber_centroids), masky, maskx]) all_corr_mov = all_corr_to_image(all_corr) for ii, idx in enumerate(uber_centroids): pushmask_idx = masky * idx[0] + idx[1] lin_idx = [ nn for nn in range(pushmask.shape[0]) if pushmask[nn] == pushmask_idx ] uber_masks[ii, :, :] = all_corr_mov[lin_idx, :, :] uber_masks[uber_masks < opt_thresh] = np.nan ax5 = fig5.add_subplot(3, 5, ii, xticks=[], yticks=[]) ax5.scatter(uber_centroids[ii][1], uber_centroids[ii][0], color='blue', edgecolors='white') ax5.imshow(mask, vmin=0, vmax=2, cmap=plt.get_cmap('gray_r')) ax5.imshow(uber_masks[ii, :, :], cmap='gnuplot2') #pdb.set_trace() all_label_map = all_label_map.reshape( [all_label_map.shape[0], masky, maskx]) fig6 = plt.figure(figsize=(12, 4)) fig6.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=.05, wspace=.05) for ii in range(all_label_map.shape[0]): ax6 = fig6.add_subplot(3, 5, ii, xticks=[], yticks=[]) ax6.scatter(uber_centroids[ii][1], uber_centroids[ii][0], color='blue', edgecolors='white') ax6.imshow(mask, vmin=0, vmax=2, cmap=plt.get_cmap('gray_r')) ax6.imshow(all_label_map[ii, :, :], vmin=0, vmax=1, cmap=plt.get_cmap('gray_r')) fig7 = plt.figure(figsize=(4, 4)) fig7.subplots_adjust(left=0, right=1, bottom=0, top=1, hspace=.05, wspace=.05) ax7 = fig7.add_subplot(1, 1, 1, xticks=[], yticks=[]) ax7.imshow(np.nanmax(uber_masks, axis=0), cmap='gnuplot2') ax7.scatter(zip(*uber_centroids)[1], zip(*uber_centroids)[0], color='blue', edgecolors='white') return opt_centroids, all_submask, uber_centroids, uber_masks, all_label_map
# maskspace_predictions[:] = np.nan # maskspace_predictions[all_submask] = np.squeeze(predictions[min_bic_idx, :]) #maskspace_predictions[submask_l] = predictions_l # if submasks have overlap (they might depending on how accurently teh submasks are drawn), the left will overwrite the right # if SAVE_PLOT: # special = '_k=' + str(k) + '_' + gmm_type + '_corr_thresh=' + str(min_thresh) # figpath = os.path.join(gmm_path,filename_short+'_centroid_gmm_bics_'+special+timestamp_str[:10]) # plt.savefig(figpath, dpi=300, transparent=True) # plt.close() # # gmm_centroid_map(pushmask, maskspace_predictions, kays[min_bic_idx], min_thresh, best_k) # return best_k ############# load data and mask mask = tiff.imread(maskpath) # mask must be 8bit thresholded as 0 & 255 mask = mask / 255 masky = mask.shape[0] maskx = mask.shape[1] mask_idx, pullmask, pushmask = mask_to_index(mask) datalength = [pushmask.shape[0], masky, maskx] try: # if "all_corr" is in memory already all_corr except NameError: if hdf5_format: f = h5py.File(filepath, 'r') imported_data = f['all_corr'] all_corr = np.copy(imported_data) #f.close()