def mask_similarity_transformed_atlas(auto, ann, atlas, dst, verbose=True): ''' Function to similarity transform atlas, mask based on ostu's method and save out Inputs ----------- auto = path to autofluor image from clearmap cluster: './clearmap_cluster_output/autofluo_resampled.tif' atlas = path to atlas ann = path to annotation file dst = location to save files: './clearmap_cluster_output' Return ---------- masked_atlas_pth masked_ann_pth ''' #setup directory sim_dst = os.path.join(dst, 'similarity_transform') makedir(dst) #do similarity transform on atlas to roughly align them - we might consider affine sim_atlas, tp = similarity_transform(fx=auto, mv=atlas, dst=sim_dst, nm='atlas_similarity_to_auto.tif', level='intermediate', cleanup=False) #transform ann transformix_command_line_call(src=ann, dst=sim_dst, transformfile=tp) sim_ann = os.path.join() #binarize auto if verbose: sys.stdout.write('Thresholding similarity tranformed atlas...') sys.stdout.flush() mask = otsu_threshold(auto, verbose=verbose) #mask atlas if verbose: print('Masking...') masked_atlas = tifffile.imread(sim_atlas) masked_ann = tifffile.imread(sim_ann) masked_atlas[mask == 0] = 0 masked_ann[mask == 0] = 0 #save out masked_atlas_pth = os.path.join(dst, 'atlas_similarity_to_auto_masked.tif') masked_ann_pth = os.path.join(dst, 'ann_similarity_to_auto_masked.tif') if verbose: print('Saving as {}'.format(masked_atlas_pth)) tifffile.imsave(masked_atlas_pth, masked_atlas) tifffile.imsave(masked_ann_pth, masked_ann) return masked_atlas_pth, masked_ann_pth
transformfiles = modify_transform_files(transformfiles=[a2r0, a2r1], dst=aldst) [change_interpolation_order(xx, 0) for xx in transformfiles] #change the parameter in the transform files that outputs 16bit images instead for fl in transformfiles: # Read in the file with open(fl, "r") as file: filedata = file.read() # Replace the target string filedata = filedata.replace('(ResultImagePixelType "short")', '(ResultImagePixelType "float")') # Write the file out again with open(fl, "w") as file: file.write(filedata) #run transformix transformix_command_line_call(ann, aldst, transformfiles[-1]) # #now zoom out - this is heavy! transformed_ann = os.path.join(aldst, "result.tif") tann = tifffile.imread(transformed_ann).astype("float32") pl0 = sitk.GetArrayFromImage( sitk.ReadImage((os.path.join(cellvol, os.listdir(cellvol)[0])))) dv0, ap0, ml0 = len(os.listdir(cellvol)), pl0.shape[0], pl0.shape[1] ml1, ap1, dv1 = tann.shape #scale in dv only first and rotate to hor orientation bigdvann = np.swapaxes(zoom(tann, [1, 1, dv0 / float(dv1)], order=0), 0, 2) save_dst = os.path.join(aldst, "single_tifs") makedir(save_dst)
tp0 = [ xx for xx in listall(os.path.dirname(cellvol.ch_to_reg_to_atlas), 'TransformParameters.0.txt') if 'sig_to_reg' in xx and 'regtoatlas' not in xx ][0] transformfiles = [ tp0, os.path.join(fld, 'elastix/TransformParameters.0.txt'), os.path.join(fld, 'elastix/TransformParameters.1.txt') ] transformfiles = modify_transform_files(transformfiles, dst=dst1) #cell_registered channel if generate_registered_overlay: transformix_command_line_call(cellvol.resampled_for_elastix_vol, transformed_dst, transformfiles[-1]) cell_reg = tifffile.imread( os.path.join(transformed_dst, 'result.tif')) cell_cnn = np.zeros_like(cell_reg) errors = [] for zyx, v in zyx_cnt.items(): z, y, x = [ int(xx) for xx in zyx.replace( '(', '', ).replace(')', '').split(',') ] try: cell_cnn[z, y, x] = v * 100 except Exception as e: print(e)
verbose=verbose) #load and convert to single voxel loc zyx = load_np(converted_points) zyx = np.asarray([ str((int(xx[0]), int(xx[1]), int(xx[2]))) for xx in load_np(converted_points) ]) from collections import Counter zyx_cnt = Counter(zyx) #manually call transformix.. c_rfe = '/home/wanglab/wang/pisano/tracing_output/antero_4x/20170115_tp_bl6_lob6a_1000r_02/20170115_tp_bl6_lob6a_1000r_647_010na_z7d5um_125msec_10povlp_resized_ch00_resampledforelastix.tif' transformed_dst = '/home/wanglab/wang/pisano/tracing_output/antero_4x/20170115_tp_bl6_lob6a_1000r_02/3dunet_output/transformed_points' transformfile = '/home/wanglab/wang/pisano/tracing_output/antero_4x/20170115_tp_bl6_lob6a_1000r_02/elastix/TransformParameters.1.txt' transformix_command_line_call(c_rfe, transformed_dst, transformfile) #cell_registered channel cell_reg = tifffile.imread( '/home/wanglab/wang/pisano/tracing_output/antero_4x/20170115_tp_bl6_lob6a_1000r_02/3dunet_output/transformed_points/result.tif' ) cell_cnn = np.zeros_like(cell_reg) for zyx, v in zyx_cnt.iteritems(): z, y, x = [ int(xx) for xx in zyx.replace( '(', '', ).replace(')', '').split(',') ] cell_cnn[z, y, x] = v * 100 merged = np.stack([cell_cnn, cell_reg, np.zeros_like(cell_reg)], -1)
def overlay_qc(args): #unpacking this way for multiprocessing fld, folder_suffix, output_folder, verbose, doubletransform, make_volumes = args try: #get 3dunet cell dataframe csv file input_csv = listdirfull(os.path.join(fld, folder_suffix), ".csv") assert len(input_csv) == 1, "multiple csv files" dataframe = pd.read_csv(input_csv[0]) #location to save out dst = os.path.join(output_folder, os.path.basename(fld)); makedir(dst) #EXAMPLE USING LIGHTSHEET - assumes marking centers in the "raw" full sized cell channel. This will transform those #centers into "atlas" space (in this case the moving image) #in this case the "inverse transform has the atlas as the moving image in the first step, #and the autofluorescence channel as the moving image in the second step #NOTE - it seems that the registration of cell to auto is failing on occasion....thus get new files... ################################ cell_inverse_folder = listdirfull(os.path.join(fld, "elastix_inverse_transform"), "cellch")[0] a2r = listall(cell_inverse_folder, "atlas2reg_TransformParameters"); a2r.sort() r2s = listall(cell_inverse_folder, "reg2sig_TransformParameters"); r2s.sort() #possibly remove #IMPORTANT. the idea is to apply cfos->auto->atlas transformfiles = r2s + a2r if doubletransform else a2r #might get rid of r2s lightsheet_parameter_dictionary = os.path.join(fld, "param_dict.p") converted_points = generate_transformed_cellcount(dataframe, dst, transformfiles, lightsheet_parameter_dictionary, verbose=verbose) #load and convert to single voxel loc zyx = np.asarray([str((int(xx[0]), int(xx[1]), int(xx[2]))) for xx in np.nan_to_num(np.load(converted_points))]) from collections import Counter zyx_cnt = Counter(zyx) #check... if make_volumes: #manually call transformix kwargs = load_dictionary(lightsheet_parameter_dictionary) vol = [xx for xx in kwargs["volumes"] if xx.ch_type == "cellch"][0].resampled_for_elastix_vol transformed_vol = os.path.join(dst, "transformed_volume"); makedir(transformed_vol) if not doubletransform: transformfiles = [os.path.join(fld, "elastix/TransformParameters.0.txt"), os.path.join(fld, "elastix/TransformParameters.1.txt")] transformfiles = modify_transform_files(transformfiles, transformed_vol) #copy over elastix files transformix_command_line_call(vol, transformed_vol, transformfiles[-1]) else: v=[xx for xx in kwargs["volumes"] if xx.ch_type == "cellch"][0] #sig to reg tps = [listall(os.path.dirname(v.ch_to_reg_to_atlas), "/TransformParameters.0")[0], listall(os.path.dirname(v.ch_to_reg_to_atlas), "/TransformParameters.1")[0]] #reg to atlas transformfiles = tps+[os.path.join(fld, "elastix/TransformParameters.0.txt"), os.path.join(fld, "elastix/TransformParameters.1.txt")] transformfiles = modify_transform_files(transformfiles, transformed_vol) #copy over elastix files transformix_command_line_call(vol, transformed_vol, transformfiles[-1]) #cell_registered channel cell_reg = tifffile.imread(os.path.join(transformed_vol, "result.tif")) tifffile.imsave(os.path.join(transformed_vol, "result.tif"), cell_reg, compress=1) cell_cnn = np.zeros_like(cell_reg) tarr = []; badlist=[] for zyx,v in zyx_cnt.items(): z,y,x = [int(xx) for xx in zyx.replace("(","",).replace(")","").split(",")] tarr.append([z,y,x]) try: cell_cnn[z,y,x] = v*100 except: badlist.append([z,y,x]) #apply x y dilation r = 2 selem = ball(r)[int(r/2)] cell_cnn = cell_cnn.astype("uint8") cell_cnn = np.asarray([cv2.dilate(cell_cnn[i], selem, iterations = 1) for i in range(cell_cnn.shape[0])]) tarr=np.asarray(tarr) if len(badlist)>0: print("{} errors in mapping with cell_cnn shape {}, each max dim {}, \npossibly due to a registration overshoot \ or not using double transform\n\n{}".format(len(badlist), cell_cnn.shape, np.max(tarr,0), badlist)) merged = np.stack([cell_cnn, cell_reg, np.zeros_like(cell_reg)], -1) tifffile.imsave(os.path.join(transformed_vol, "merged.tif"), merged)#, compress=1) #out = np.concatenate([cell_cnn, cell_reg, ], 0) #####check at the resampled for elastix phase before transform...this mapping looks good... if make_volumes: #make zyx numpy arry zyx = dataframe[["z","y","x"]].values kwargs = load_dictionary(lightsheet_parameter_dictionary) vol = [xx for xx in kwargs["volumes"] if xx.ch_type =="cellch"][0] fullsizedimensions = get_fullsizedims_from_kwargs(kwargs) #don"t get from kwargs["volumes"][0].fullsizedimensions it"s bad! use this instead zyx = fix_contour_orientation(zyx, verbose=verbose, **kwargs) #now in orientation of resample zyx = points_resample(zyx, original_dims = fix_dimension_orientation(fullsizedimensions, **kwargs), resample_dims = tifffile.imread(vol.resampled_for_elastix_vol).shape, verbose = verbose)[:, :3] #cell channel cell_ch = tifffile.imread(vol.resampled_for_elastix_vol) cell_cnn = np.zeros_like(cell_ch) tarr = []; badlist=[] for _zyx in zyx: z,y,x = [int(xx) for xx in _zyx] tarr.append([z,y,x]) try: cell_cnn[z,y,x] = 100 except: badlist.append([z,y,x]) tarr=np.asarray(tarr) merged = np.stack([cell_cnn, cell_ch, np.zeros_like(cell_ch)], -1) tifffile.imsave(os.path.join(transformed_vol, "resampled_merged.tif"), merged)#, compress=1) except Exception as e: print(e) with open(error_file, "a") as err_fl: err_fl.write("\n\n{} {}\n\n".format(fld, e))
transformfiles = ['/home/wanglab/wang/pisano/Python/atlas/tp_to_allen/aba_to_tp/TransformParameters.0.txt', '/home/wanglab/wang/pisano/Python/atlas/tp_to_allen/aba_to_tp/TransformParameters.1.txt'] transformed_dst = os.path.join(ndst, 'elastix'); makedir(transformed_dst) transformfiles = modify_transform_files(transformfiles, transformed_dst) [change_interpolation_order(xx, order = 0) for xx in transformfiles] #reoriented into dv scan space rvol = np.copy(vol) rvol = np.swapaxes(np.swapaxes(rvol, 0,1),0,2) #sitk.Show(sitk.GetImageFromArray(rvol)) #split up pvol = rvol[:,:,:,1] pvol[pvol!=0.0] = 1.0 pdst = os.path.join(transformed_dst, 'pvol'); makedir(pdst) pfl = os.path.join(pdst, 'pvol_aba_space.tif') tifffile.imsave(pfl, pvol) transformix_command_line_call(src = pfl, dst=pdst, transformfile=transformfiles[-1]) nvol = rvol[:,:,:,0] nvol[nvol!=0.0] = 1.0 ndst = os.path.join(transformed_dst, 'nvol'); makedir(ndst) nfl = os.path.join(ndst, 'nvol_aba_space.tif') tifffile.imsave(nfl, nvol) transformix_command_line_call(src = nfl, dst=ndst, transformfile=transformfiles[-1]) #%%#%%PMA space sag #keep in sag orientation import os import matplotlib as mpl import scipy import scipy.stats os.chdir('/home/wanglab/wang/pisano/Python/lightsheet') from tools.imageprocessing.orientation import fix_orientation
#saved out annotation volume print("\nsaving zoomed volume...") tif.imsave(os.path.join(src, "WHS_SD_rat_atlas_v3_annotation_for_pra_reg.tif"), watl_for_pra.astype("uint16")) reg = os.path.join(src, "waxholm_to_pra") a2r = [os.path.join(reg, xx) for xx in os.listdir(reg) if "Transform" in xx] a2r.sort() dst = os.path.join(src, "transformed_annotation_volume") makedir(dst) #transformix transformfiles = modify_transform_files(transformfiles=a2r, dst=dst) [change_interpolation_order(xx, 0) for xx in transformfiles] #change the parameter in the transform files that outputs 16bit images instead for fl in transformfiles: # Read in the file with open(fl, "r") as file: filedata = file.read() # Replace the target string filedata = filedata.replace('(ResultImagePixelType "float")', '(ResultImagePixelType "short")') # Write the file out again with open(fl, "w") as file: file.write(filedata) #run transformix transformix_command_line_call( os.path.join(src, "WHS_SD_rat_atlas_v3_annotation_for_pra_reg.tif"), dst, transformfiles[-1])
#changes the parameter in the transform files that outputs 16bit images instead for fl in transformfiles:# Read in the file with open(fl, "r") as file: filedata = file.read() # Replace the target string filedata = filedata.replace('(ResultImagePixelType "short")', '(ResultImagePixelType "float")') # Write the file out again with open(fl, "w") as file: file.write(filedata) print("Copied over transform files and modified them") # run transformix - makes the transformed atlas as result.tif in aldst print("running transformix") transformed_ann = os.path.join(aldst, "result.tif") if not os.path.exists(transformed_ann): transformix_command_line_call(annotation_volume_path, aldst, transformfiles[-1]) print("ran transformix successfully!") else: print("transformix was already run before") sys.stdout.flush() if step == 'step2': #now zoom out - this is heavy! print("Now zooming out in z first. This can take a while") transformed_ann = os.path.join(aldst, "result.tif") tann = tifffile.imread(transformed_ann) raw_planes = sorted(glob.glob(raw_dir + '/*tif')) pl0 = tifffile.imread(raw_planes[0]) # Z=0 plane dv0 = len(raw_planes) ap0,ml0 = pl0.shape[0], pl0.shape[1] # dimensions of original raw data, z, y, z ml1,ap1,dv1 = tann.shape # dimensions of downsized raw data