os.path.join

os.path.join()函数:连接两个或更多的路径名组件

1.如果各组件名首字母不包含’/’,则函数会自动加上

2.如果有一个组件是一个绝对路径,则在它之前的所有组件均会被舍弃

3.如果最后一个组件为空,则生成的路径以一个’/’分隔符结尾

Demo1

import os

Path1 = 'home'
Path2 = 'develop'
Path3 = 'code'

Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1,Path2,Path3)
print ('Path10 = ',Path10)
print ('Path20 = ',Path20)

输出

Path10 = homedevelopcode
Path20 = home\develop\code

Demo2

import os

Path1 = '/home'
Path2 = 'develop'
Path3 = 'code'

Path10 = Path1 + Path2 + Path3
Path20 = `os.path.join`(Path1,Path2,Path3)
print ('Path10 = ',Path10)
print ('Path20 = ',Path20) 

输出

Path10 = /homedevelopcode
Path20 = /home\develop\code

Demo3

import os

Path1 = 'home'
Path2 = '/develop'
Path3 = 'code'

Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1,Path2,Path3)
print ('Path10 = ',Path10)
print ('Path20 = ',Path20) 

输出

Path10 = home/developcode
Path20 = /develop\code

Demo4

import os

Path1 = 'home'
Path2 = 'develop'
Path3 = '/code'

Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1,Path2,Path3)
print ('Path10 = ',Path10)
print ('Path20 = ',Path20 )

输出

Path10 = homedevelop/code
Path20 = /code
else: print('Normal') # process in parallel if parallel: print('Reshape reconstructed volumes in parallel') max_processes = 10 list_of_arguments = [] reconstructed_dir = os.path.join(os.path.abspath(args.output_dir), 'reconstructed_niftis') reshaped_dir = os.path.join(os.path.abspath(args.output_dir), 'recon_niftis_reshaped') if os.path.exists(reshaped_dir) == False: os.makedirs(reshaped_dir) for plane in planes: for sub in full_subject_list: list_of_arguments.append((sub, plane, reconstructed_dir, reshaped_dir)) # spawn up to 10 processes at a time with multiprocessing.Pool(processes=max_processes) as pool: pool.starmap(reshape_reconstructed_volumes.reshape_t1_t2_flair, list_of_arguments) # run in series else: reshape_reconstructed_volumes.iterate_for_each_plane( subject_list=full_subject_list, data_dir=os.path.abspath(args.output_dir), out_dir=os.path.join(os.path.abspath(args.output_dir), 'recon_niftis_reshaped') ) print('Finished reshaping reconstructed volumes') print('Coregister reshaped reconstructed volumes') # coregister reshaped reconstructed volumes if ensemble: print('Ensemble') coregister_reshaped_reconstructed_volumes_ensemble.coregister_all_folds( subs_file=os.path.abspath(args.subs_file), data_dir=os.path.abspath(args.output_dir), out_dir=os.path.abspath(args.output_dir), n_splits=12 ) else: print('Normal') coregister_reshaped_reconstructed_volumes.coregister_all_subjects( subjects_list=full_subject_list, data_dir=os.path.abspath(args.output_dir), output_dir=os.path.join(os.path.abspath(args.output_dir), 'recon_niftis_reshaped_coregistered') ) print('Finished coregistering reshaped reconstructed volumes') print('Filtering volumes using wavelet transform') if ensemble: if parallel: print('Ensemble') wavelet_transform_filter_ensemble.filter_in_parallel( subs_file=os.path.abspath(args.subs_file), path=os.path.abspath(args.output_dir), output_dir=os.path.abspath(args.output_dir), n_splits=12, decNum=8, wname='db25', damp_sigma=8 ) else: wavelet_transform_filter_ensemble.filter_in_series( subs_file=os.path.abspath(args.subs_file), path=os.path.abspath(args.output_dir), output_dir=os.path.abspath(args.output_dir), n_splits=12, decNum=8, wname='db25', damp_sigma=8 ) else: print('Normal') if parallel: wavelet_transform_filter.filter_in_parallel( subjects=full_subject_list, path=os.path.abspath(args.output_dir), output_dir=os.path.join(os.path.abspath(args.output_dir), 'recon_niftis_reshaped_coregistered_filtered'), decNum=8, wname='db25', damp_sigma=8 ) else: wavelet_transform_filter.filter_in_series( subjects=full_subject_list, path=os.path.abspath(args.output_dir), output_dir=os.path.join(os.path.abspath(args.output_dir), 'recon_niftis_reshaped_coregistered_filtered'), decNum=8, wname='db25', damp_sigma=8 ) print('Finished filtering volumes using wavelet transform') print('Coregistering filtered outputs') if ensemble: coregister_filtered_outputs_ensemble.coregister_all_subjects( subs_file=os.path.abspath(args.subs_file), ensemble_output_dir=os.path.abspath(args.output_dir), n_splits=12 ) print('Finished coregistering filtered outputs') # average the outputs across each fold to increase signal-to-noise ratio if ensemble: print('Averaging filtered volumes') if parallel: print('Averaging in parallel') averaging_filtered_ensemble.iterate_in_parallel( subs_file=os.path.abspath(args.subs_file), input_dir=os.path.abspath(args.output_dir), output_dir=os.path.join(os.path.abspath(args.output_dir), 'recon_niftis_filtered_averaged_across_folds'), n_splits=12 ) else: print('Averaging in series') averaging_filtered_ensemble.iterate_for_each_sub( subs_file=os.path.abspath(args.subs_file), input_dir=os.path.abspath(args.output_dir), output_dir=os.path.join(os.path.abspath(args.output_dir), 'recon_niftis_filtered_averaged_across_folds'), n_splits=12 ) print('Finished averaging filtered volumes') print('Unpadding volumes to original shape') if ensemble: if parallel: unpad.unpad_parallel( subjects=full_subject_list, padded_dir=os.path.join(os.path.abspath(args.output_dir), 'recon_niftis_filtered_averaged_across_folds'), orig_dir=os.path.abspath(args.data), output_dir=os.path.join(os.path.abspath(args.output_dir), 'LowGAN_outputs') ) else: unpad.unpad_series( subjects=full_subject_list, padded_dir=os.path.join(os.path.abspath(args.output_dir), 'recon_niftis_filtered_averaged_across_folds'), orig_dir=os.path.abspath(args.data), output_dir=os.path.join(os.path.abspath(args.output_dir), 'LowGAN_outputs') ) else: if parallel: unpad.unpad_parallel( subjects=full_subject_list, padded_dir=os.path.join(os.path.abspath(args.output_dir), 'recon_niftis_reshaped_coregistered_filtered'), orig_dir=os.path.abspath(args.data), output_dir=os.path.join(os.path.abspath(args.output_dir), 'LowGAN_outputs') ) else: unpad.unpad_series( subjects=full_subject_list, padded_dir=os.path.join(os.path.abspath(args.output_dir), 'recon_niftis_reshaped_coregistered_filtered'), orig_dir=os.path.abspath(args.data), output_dir=os.path.join(os.path.abspath(args.output_dir), 'LowGAN_outputs') ) print('Finished unpadding volumes to original shape') if intermediates: print('Moving intermediate outputs to intermediates directory') if os.path.exists(os.path.join(os.path.abspath(args.output_dir), 'intermediates')) == False: os.makedirs(os.path.join(os.path.abspath(args.output_dir), 'intermediates')) # we will move only the specific directories that were generated by LowGAN to the intermediates directory # to make sure that we don't mess with any other files that may be in the output directory planes = ['axial', 'coronal', 'sagittal'] for plane in planes: os.system(f'mv {os.path.join(os.path.abspath(args.output_dir), f"dataset_{plane}_pix2pix")} {os.path.join(os.path.abspath(args.output_dir), "intermediates")}') if ensemble: folds = [f'fold_{fold}' for fold in range(0, 12)] for fold in folds: os.system(f'mv {os.path.join(os.path.abspath(args.output_dir), fold)} {os.path.join(os.path.abspath(args.output_dir), "intermediates")}') os.system(f'mv {os.path.join(os.path.abspath(args.output_dir), "recon_niftis_filtered_averaged_across_folds")} {os.path.join(os.path.abspath(args.output_dir), "intermediates")}') else: os.system(f'mv {os.path.join(os.path.abspath(args.output_dir), "results_LowGAN")} {os.path.join(os.path.abspath(args.output_dir), "intermediates")}') os.system(f'mv {os.path.join(os.path.abspath(args.output_dir), "reconstructed_niftis")} {os.path.join(os.path.abspath(args.output_dir), "intermediates")}') os.system(f'mv {os.path.join(os.path.abspath(args.output_dir), "recon_niftis_reshaped")} {os.path.join(os.path.abspath(args.output_dir), "intermediates")}') os.system(f'mv {os.path.join(os.path.abspath(args.output_dir), "recon_niftis_reshaped_coregistered")} {os.path.join(os.path.abspath(args.output_dir), "intermediates")}') os.system(f'mv {os.path.join(os.path.abspath(args.output_dir), "recon_niftis_reshaped_coregistered_filtered")} {os.path.join(os.path.abspath(args.output_dir), "intermediates")}') print('Finished moving intermediate outputs to intermediates directory')改为中文注释
07-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值