-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_fig_css.py
141 lines (124 loc) · 5.69 KB
/
merge_fig_css.py
1
"""-----------------------------------------------------------------------------------------merge_fig_css.py-----------------------------------------------------------------------------------------Goal of the script:Concat all pycortex maps and prf figures of one subject in one pdf-----------------------------------------------------------------------------------------Input(s):sys.argv[1]: main project directorysys.argv[2]: project name (correspond to directory)sys.argv[3]: number of subjectssys.argv[4]: group-----------------------------------------------------------------------------------------Output(s):one pdf file with pycortex maps and one pdf file with prf figures-----------------------------------------------------------------------------------------To run:0. To Run on meso (not on node)1. cd to function>> cd ~/projects/[PROJECT]/analysis_code/postproc/prf/postfit2. run python command>> python merge_fig_css.py [main directory] [project name] [subject num] [group]-----------------------------------------------------------------------------------------Exemple:cd ~/projects/pRF_analysis/analysis_code/postproc/prf/postfitpython merge_fig_css.py /scratch/mszinte/data MotConf sub-01 327python merge_fig_css.py /scratch/mszinte/data MotConf sub-170k 327python merge_fig_css.py /scratch/mszinte/data MotConf group 327python merge_fig_css.py /scratch/mszinte/data RetinoMaps sub-01 327python merge_fig_css.py /scratch/mszinte/data RetinoMaps sub-170k 327python merge_fig_css.py /scratch/mszinte/data RetinoMaps group 327python merge_fig_css.py /scratch/mszinte/data amblyo_prf sub-01 327python merge_fig_css.py /scratch/mszinte/data amblyo_prf sub-170k 327python merge_fig_css.py /scratch/mszinte/data amblyo_prf group 327python merge_fig_css.py /scratch/mszinte/data amblyo_prf group_patient 327python merge_fig_css.py /scratch/mszinte/data amblyo_prf group_aniso 327python merge_fig_css.py /scratch/mszinte/data amblyo_prf group_mixed 327python merge_fig_css.py /scratch/mszinte/data amblyo_prf group_strab 327python merge_fig_css.py /scratch/mszinte/data amblyo_prf group_control 327python merge_fig_css.py /scratch/mszinte/data amblyo_prf group_excluded 327-----------------------------------------------------------------------------------------Written by Uriel Lascombes ([email protected])Edited by Martin Szinte ([email protected])-----------------------------------------------------------------------------------------"""# General importsimport osimport sysimport jsonimport globfrom pypdf import PdfWriter# Debugimport ipdbdeb = ipdb.set_trace# Inputsmain_dir = sys.argv[1]project_dir = sys.argv[2]subject = sys.argv[3]group = sys.argv[4]# load settingsbase_dir = os.path.abspath(os.path.join(os.getcwd(), "../../../../"))settings_path = os.path.join(base_dir, project_dir, "settings.json")with open(settings_path) as f: json_s = f.read() analysis_info = json.loads(json_s)if subject == 'sub-170k': formats = ['170k'] extensions = ['dtseries.nii']else: formats = analysis_info['formats'] extensions = analysis_info['extensions']prf_task_name = analysis_info['prf_task_name']# Merge Pycortex Mapsfor format_, extension in zip(formats, extensions): # Defind output directory and file prf_merge_dir = '{}/{}/derivatives/pp_data/{}/{}/figures/prf'.format( main_dir, project_dir, subject, format_) os.makedirs(prf_merge_dir, exist_ok = True) pycortex_merge_fn = '{}_prf-all_maps.pdf'.format(subject) # Find all pycortex maps pycortex_roi_dir = '{}/{}/derivatives/pp_data/{}/{}/rois/pycortex/flatmaps_rois/*'.format( main_dir, project_dir, subject, format_) pycortex_corr_dir = '{}/{}/derivatives/pp_data/{}/{}/corr/pycortex/flatmaps_inter-run-corr/*{}*'.format( main_dir, project_dir, subject, format_, prf_task_name) pycortex_gauss_dir = '{}/{}/derivatives/pp_data/{}/{}/prf/pycortex/flatmaps_avg_gauss_gridfit/*'.format( main_dir, project_dir, subject, format_) pycortex_css_dir = '{}/{}/derivatives/pp_data/{}/{}/prf/pycortex/flatmaps_css_loo-median/*'.format( main_dir, project_dir, subject, format_) pycortex_all = glob.glob(pycortex_roi_dir) \ + glob.glob(pycortex_corr_dir) \ + glob.glob(pycortex_gauss_dir) \ + glob.glob(pycortex_css_dir) # Merge pdf and export final pdf if 'group' not in subject: pycortex_merger = PdfWriter() for n_page, pycortex_map in enumerate(pycortex_all): # merge pycortex_merger.append(pycortex_map) # Export the pdf merge_fn = "{}/{}".format(prf_merge_dir, pycortex_merge_fn) print("Saving {}".format(merge_fn)) pycortex_merger.write(merge_fn) pycortex_merger.close() # Merge prf figures # Defind output directory and file prf_fig_merge_fn = '{}_prf-all_fig.pdf'.format(subject) # Find all prf figures prf_fig_dir = '{}/{}/derivatives/pp_data/{}/{}/prf/figures/*'.format( main_dir, project_dir, subject, format_) prf_fig_dir_fns = glob.glob(prf_fig_dir) # Merge pdf and export final pdf figures_merger = PdfWriter() for n_page, figure_fn in enumerate(prf_fig_dir_fns): # merge figures_merger.append(figure_fn) # Export the pdf merge_fn = '{}/{}'.format(prf_merge_dir, prf_fig_merge_fn) print("Saving {}".format(merge_fn)) figures_merger.write(merge_fn) figures_merger.close() # Define permission cmdprint('Changing files permissions in {}/{}'.format(main_dir, project_dir))os.system("chmod -Rf 771 {}/{}".format(main_dir, project_dir))os.system("chgrp -Rf {} {}/{}".format(group, main_dir, project_dir))