diff --git a/configFiles/atrophyTables/withFreesurferLabels/atrophyTableTemporalEntorhinalCorticesAndHipps.txt b/configFiles/atrophyTables/withFreesurferLabels/atrophyTableTemporalEntorhinalCorticesAndHipps.txt new file mode 100644 index 0000000..4cf4e5c --- /dev/null +++ b/configFiles/atrophyTables/withFreesurferLabels/atrophyTableTemporalEntorhinalCorticesAndHipps.txt @@ -0,0 +1,20 @@ +labels newValues +2035 0.5 +2030 0.5 +2034 0.5 +2015 0.5 +2009 0.5 +2007 0.5 +2006 0.5 +53 0.6 +54 0.6 +1035 0.5 +1030 0.5 +1034 0.5 +1015 0.5 +1009 0.5 +1007 0.5 +1006 0.5 +17 0.6 +18 0.6 + diff --git a/configFiles/freeSurferLabelsAndTables/freeSurferCsfRegionsExceptAllVentriclesLabelTableForModel b/configFiles/freeSurferLabelsAndTables/freeSurferCsfRegionsExceptAllVentriclesLabelTableForModel new file mode 100644 index 0000000..c4783ec --- /dev/null +++ b/configFiles/freeSurferLabelsAndTables/freeSurferCsfRegionsExceptAllVentriclesLabelTableForModel @@ -0,0 +1,3 @@ +labels newValues +24 1 + diff --git a/configFiles/freeSurferLabelsAndTables/freeSurferLabelsAseg.txt b/configFiles/freeSurferLabelsAndTables/freeSurferLabelsAseg.txt new file mode 100644 index 0000000..f584877 --- /dev/null +++ b/configFiles/freeSurferLabelsAndTables/freeSurferLabelsAseg.txt @@ -0,0 +1,49 @@ +2 +3 +4 +5 +7 +8 +10 +11 +12 +13 +14 +15 +16 +17 +18 +24 +26 +28 +30 +31 +41 +42 +43 +44 +46 +47 +49 +50 +51 +52 +53 +54 +58 +60 +62 +63 +72 +77 +78 +79 +80 +81 +82 +85 +251 +252 +253 +254 +255 diff --git a/configFiles/freeSurferLabelsAndTables/freeSurferLabelsWmparc.txt b/configFiles/freeSurferLabelsAndTables/freeSurferLabelsWmparc.txt new file mode 100644 index 0000000..c325fb6 --- /dev/null +++ b/configFiles/freeSurferLabelsAndTables/freeSurferLabelsWmparc.txt @@ -0,0 +1,195 @@ +2 +3 +4 +5 +7 +8 +10 +11 +12 +13 +14 +15 +16 +17 +18 +24 +26 +28 +30 +31 +41 +42 +43 +44 +46 +47 +49 +50 +51 +52 +53 +54 +58 +60 +62 +63 +72 +77 +78 +79 +80 +81 +82 +85 +251 +252 +253 +254 +255 +1000 +1001 +1002 +1003 +1004 +1005 +1006 +1007 +1008 +1009 +1010 +1011 +1012 +1013 +1014 +1015 +1016 +1017 +1018 +1019 +1020 +1021 +1022 +1023 +1024 +1025 +1026 +1027 +1028 +1029 +1030 +1031 +1032 +1033 +1034 +1035 +2000 +2001 +2002 +2003 +2004 +2005 +2006 +2007 +2008 +2009 +2010 +2011 +2012 +2013 +2014 +2015 +2016 +2017 +2018 +2019 +2020 +2021 +2022 +2023 +2024 +2025 +2026 +2027 +2028 +2029 +2030 +2031 +2032 +2033 +2034 +2035 +3000 +3001 +3002 +3003 +3004 +3005 +3006 +3007 +3008 +3009 +3010 +3011 +3012 +3013 +3014 +3015 +3016 +3017 +3018 +3019 +3020 +3021 +3022 +3023 +3024 +3025 +3026 +3027 +3028 +3029 +3030 +3031 +3032 +3033 +3034 +3035 +4000 +4001 +4002 +4003 +4004 +4005 +4006 +4007 +4008 +4009 +4010 +4011 +4012 +4013 +4014 +4015 +4016 +4017 +4018 +4019 +4020 +4021 +4022 +4023 +4024 +4025 +4026 +4027 +4028 +4029 +4030 +4031 +4032 +4033 +4034 +4035 +5001 +5002 diff --git a/configFiles/post_process/oasis_study.txt b/configFiles/post_process/oasis_study.txt new file mode 100644 index 0000000..47c1e59 --- /dev/null +++ b/configFiles/post_process/oasis_study.txt @@ -0,0 +1,33 @@ +# data_base : Oasis +{ +'patients' : ['Subject_0048'], #Either from command line or in a list here. +'sim_prefixes' : ['div12pt_atrophyMapR2_0_'], #Either from command line or in a list here. +'sim_cropped_img_suffices' : ['WarpedImageBsplineT1.nii.gz'], #As a list +'sim_full_img_suffices' : ['WarpedImageBsplineFullSizeT1.nii.gz'], #As a list +#'sim_resampled_img_suffices' : ['WarpedImageBsplineFullSizeT1Resampled.nii.gz'], #As a list +'sim_resampled_img_suffices' : ['WarpedImageBsplineFullSizeT1ResampledMinus.nii.gz'], #As a list +'sim_vel_suffices' : ['T1vel.nii.gz'], #As a list +'resampling_disp_suffices' : ['ResamplingDispFieldT1.nii.gz'], #As a list + +'sim_base_dir' : '/epi/asclepios2/bkhanal/works/AdLemModel/results/patients', +'sim_crop_mask' : 'cropMaskD2_FS.nii.gz', + +'data_base_dir' : '/epi/asclepios2/mhadjham/OASIS_full', +'in_img_dir' : 'Rigid_reg_intra', +'reg_dir' : 'Non_Rigid_reg_intra_robex_Mask', + +'in_img_prefix' : 'Final_S0MNI_spline_', +'in_resample_img_prefix' : 'Final_S4MNI_spline_recall_S0_', +'in_img_to_resample_img_prefix' : 'Mask_Registered_output_S0MNI_to_S4MNI_', #This image is in reg_dir!! +'in_img_pid_skip' : 8, #num_of chars from start of patient id not used in real_img file names. +'in_img_suffix' : '.nii.gz', + +'svf_prefix' : 'Mask_output_stationary_velocity_field_4_', #1, 2, 3 or 4 to choose tpt. +'svf_suffix' : '_2.0_0.15_3.0.mha', +'div_prefix' : 'Minus_Div_SVF_longi_native_', +#'disp_field_prefix' : 'output_displacement_field_', +'disp_field_prefix' : 'Minus_Mask_output_displacement_field_4_', + +} + + diff --git a/configFiles/post_process/oasis_studyThreeTpts.txt b/configFiles/post_process/oasis_studyThreeTpts.txt new file mode 100644 index 0000000..0e6b1df --- /dev/null +++ b/configFiles/post_process/oasis_studyThreeTpts.txt @@ -0,0 +1,33 @@ +# data_base : Oasis +{ +'patients' : ['Subject_0048'], #Either from command line or in a list here. +'sim_prefixes' : ['div12pt_atrophyMapR2_0_'], #Either from command line or in a list here. +'sim_cropped_img_suffices' : ['WarpedImageBsplineT1.nii.gz', 'WarpedImageBsplineT2.nii.gz', 'WarpedImageBsplineT3.nii.gz'], #As a list +'sim_full_img_suffices' : ['WarpedImageBsplineFullSizeT1.nii.gz', 'WarpedImageBsplineFullSizeT2.nii.gz', 'WarpedImageBsplineFullSizeT3.nii.gz'], #As a list +#'sim_resampled_img_suffices' : ['WarpedImageBsplineFullSizeT1Resampled.nii.gz'], #As a list +'sim_resampled_img_suffices' : ['WarpedImageBsplineFullSizeT1ResampledMinus.nii.gz', 'WarpedImageBsplineFullSizeT2ResampledMinus.nii.gz', 'WarpedImageBsplineFullSizeT3ResampledMinus.nii.gz'], #As a list +'sim_vel_suffices' : ['T1vel.nii.gz', 'T2vel.nii.gz', 'T3vel.nii.gz'], #As a list +'resampling_disp_suffices' : ['ResamplingDispFieldT1.nii.gz', 'ResamplingDispFieldT2.nii.gz', 'ResamplingDispFieldT3.nii.gz'], #As a list + +'sim_base_dir' : '/epi/asclepios2/bkhanal/works/AdLemModel/results/patients', +'sim_crop_mask' : 'cropMaskD2_FS.nii.gz', + +'data_base_dir' : '/epi/asclepios2/mhadjham/OASIS_full', +'in_img_dir' : 'Rigid_reg_intra', +'reg_dir' : 'Non_Rigid_reg_intra_robex_Mask', + +'in_img_prefix' : 'Final_S0MNI_spline_', +'in_resample_img_prefix' : 'Final_S4MNI_spline_recall_S0_', +'in_img_to_resample_img_prefix' : 'Mask_Registered_output_S0MNI_to_S4MNI_', #This image is in reg_dir!! +'in_img_pid_skip' : 8, #num_of chars from start of patient id not used in real_img file names. +'in_img_suffix' : '.nii.gz', + +'svf_prefix' : 'Mask_output_stationary_velocity_field_4_', #1, 2, 3 or 4 to choose tpt. +'svf_suffix' : '_2.0_0.15_3.0.mha', +'div_prefix' : 'Minus_Div_SVF_longi_native_', +#'disp_field_prefix' : 'output_displacement_field_', +'disp_field_prefix' : 'Minus_Mask_output_displacement_field_4_', + +} + + diff --git a/configFiles/post_process/oasis_studyTwoTpts.txt b/configFiles/post_process/oasis_studyTwoTpts.txt new file mode 100644 index 0000000..8702b97 --- /dev/null +++ b/configFiles/post_process/oasis_studyTwoTpts.txt @@ -0,0 +1,33 @@ +# data_base : Oasis +{ +'patients' : ['Subject_0048'], #Either from command line or in a list here. +'sim_prefixes' : ['div12pt_atrophyMapR2_0_'], #Either from command line or in a list here. +'sim_cropped_img_suffices' : ['WarpedImageBsplineT1.nii.gz', 'WarpedImageBsplineT2.nii.gz'], #As a list +'sim_full_img_suffices' : ['WarpedImageBsplineFullSizeT1.nii.gz', 'WarpedImageBsplineFullSizeT2.nii.gz'], #As a list +#'sim_resampled_img_suffices' : ['WarpedImageBsplineFullSizeT1Resampled.nii.gz'], #As a list +'sim_resampled_img_suffices' : ['WarpedImageBsplineFullSizeT1ResampledMinus.nii.gz', 'WarpedImageBsplineFullSizeT2ResampledMinus.nii.gz'], #As a list +'sim_vel_suffices' : ['T1vel.nii.gz', 'T2vel.nii.gz'], #As a list +'resampling_disp_suffices' : ['ResamplingDispFieldT1.nii.gz', 'ResamplingDispFieldT2.nii.gz'], #As a list + +'sim_base_dir' : '/epi/asclepios2/bkhanal/works/AdLemModel/results/patients', +'sim_crop_mask' : 'cropMaskD2_FS.nii.gz', + +'data_base_dir' : '/epi/asclepios2/mhadjham/OASIS_full', +'in_img_dir' : 'Rigid_reg_intra', +'reg_dir' : 'Non_Rigid_reg_intra_robex_Mask', + +'in_img_prefix' : 'Final_S0MNI_spline_', +'in_resample_img_prefix' : 'Final_S4MNI_spline_recall_S0_', +'in_img_to_resample_img_prefix' : 'Mask_Registered_output_S0MNI_to_S4MNI_', #This image is in reg_dir!! +'in_img_pid_skip' : 8, #num_of chars from start of patient id not used in real_img file names. +'in_img_suffix' : '.nii.gz', + +'svf_prefix' : 'Mask_output_stationary_velocity_field_4_', #1, 2, 3 or 4 to choose tpt. +'svf_suffix' : '_2.0_0.15_3.0.mha', +'div_prefix' : 'Minus_Div_SVF_longi_native_', +#'disp_field_prefix' : 'output_displacement_field_', +'disp_field_prefix' : 'Minus_Mask_output_displacement_field_4_', + +} + + diff --git a/notebooks/create_image_from_labels.ipynb b/notebooks/create_image_from_labels.ipynb index 7fc01c6..6fb6595 100644 --- a/notebooks/create_image_from_labels.ipynb +++ b/notebooks/create_image_from_labels.ipynb @@ -17,7 +17,9 @@ "source": [ "import sys\n", "sys.path.insert(0, '../scripts')\n", - "\n", + "import os as os\n", + "import os.path as op\n", + "import bish_utils as bu\n", "def create_table_file(fname, header, label_value_pairs):\n", " '''\n", " Write a new file which starts with header and then writes\n", @@ -57,8 +59,7 @@ }, "outputs": [], "source": [ - "import os as os\n", - "import os.path as op\n", + "\n", "\n", "in_img_dir = '/proj/asclepios/cluster/asclepios2/bkhanal/works/AdLemModel/results/patients/miriad_189_AD_M_10_MR_1'\n", "#in_img_dir = '../results/patients/miriad_189_AD_M_10_MR_1'\n", @@ -119,7 +120,7 @@ "\n", "run_script = '/user/bkhanal/home/works/AdLemModel/scripts/create_atrophy_map.py'\n", "seg = op.join(out_dir, 'labelsWithCsfForAtrophyD2R0_FS.nii.gz')\n", - "idx1, idx2 = [2], [9, 10, 11, 12, 13]\n", + "idx1, idx2 = [4], [3]\n", "#idx1, idx2 = [3], [5, 6]\n", "#idx1, idx2 = [4], [0, 1, 2, 3]\n", "for i in idx1:\n", @@ -132,6 +133,13 @@ " bu.print_and_execute(cmd)\n" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Create atrophy maps using segmentation images" + ] + }, { "cell_type": "code", "execution_count": null, @@ -140,9 +148,54 @@ }, "outputs": [], "source": [ - "# R2_{9,10,11,12,13}\n", - "# R3_{5,6}\n", - "# R4_{0,1,2,3}" + "out_dir = '/proj/asclepios/cluster/asclepios2/bkhanal/works/AdLemModel/results/patients/Subject_0048'\n", + "seg = op.join(out_dir, 'labelsWithCsfForAtrophyD2R0_FS.nii.gz')\n", + "\n", + "# a = 'atrophyMap_hippAmyg90p_crtx30p_wm6p.nii.gz'\n", + "# hipp_amyg, crtx, wm = -0.9, -0.3, -0.06 # -ve so that no need to invert the disp. field\n", + "\n", + "a = 'atrophyMap_hippAmyg90p_crtx90p_wm40p.nii.gz'\n", + "hipp_amyg, crtx, wm = -0.9, -0.9, -0.4 # -ve so that no need to invert the disp. field\n", + "\n", + "\n", + "# FS labels: (L, R) : Hipp->(17, 53); Amyg->(18, 54); Crtx->(3, 42); WM->(2, 41)\n", + "a_val = [(17, hipp_amyg), (53, hipp_amyg), (18, hipp_amyg), (54, hipp_amyg),\n", + " (3, crtx), (42, crtx), (2, wm), (41, wm)]\n", + "\n", + "\n", + "create_image(op.join(out_dir, a), a_val, seg)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Modify existing atrophy maps" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "import image_math as img_mth\n", + "\n", + "a_map_opposite = '../results/patients/Subject_0048/Div_Mask_output_stationary_velocity_field_4_0048_2.0_0.15_3.0.nii.gz'\n", + "a_map = '../results/patients/Subject_0048/Minus_Div_Mask_output_stationary_velocity_field_4_0048_2.0_0.15_3.0.nii.gz'\n", + "#img_mth.mult_scalar_imgs([a_map_opposite], [a_map], -1)\n", + "a_map_zeroed_allcsf = '../results/patients/Subject_0048/atrophyMap_lcc_zeroedAllCsf.nii.gz'\n", + "a_map_zeroed_sulcalcsf = '../results/patients/Subject_0048/atrophyMap_lcc_zeroedOnlySulcalCsf.nii.gz'\n", + "\n", + "seg_all_csf = '../results/patients/Subject_0048/segForModelD2R0_FS_allCSF.nii.gz'\n", + "seg_sulcal_csf = '../results/patients/Subject_0048/segForModelD2R0_FS_onlySulcalCSF.nii.gz'\n", + "label_vals = [(1, 0), (0, 0)]\n", + "\n", + "#Use Script create_atrophy_map!! I need to crop the Jacobians, which is done\n", + "# over there. There are many more options over there and is much easier to\n", + "# use than do them again here. " ] } ], diff --git a/scripts/bish_utils.py b/scripts/bish_utils.py index 689c90c..13bf241 100644 --- a/scripts/bish_utils.py +++ b/scripts/bish_utils.py @@ -7,7 +7,7 @@ # import subprocess -import shutil +import shutil as su def print_and_execute(command, print_cmd=True): """prints the command by default and executes it in the shell""" @@ -27,6 +27,33 @@ def update_or_execute_cmd(cmd, cmd_n, execute): else: return cmd + cmd_n + + +def copy_file(src, dst, via_ssh=False): + ''' + Copy src file into dst directory or as dst file. + if via_ssh = True, use scp src dst + else use shutil.copy(src, dst) + ''' + if via_ssh: + cmd = 'scp %s %s' % (src, dst) + print_and_execute(cmd) + else: + su.copy(src, dst) + return + + + +def add_prefix_to_fname(prefix, fname): + ''' + Add prefix to the input filename and return the new filename. + You can give filename with full path, I will extract only the + filename, add prefix and then return with full path. + ''' + import os + return os.path.join(os.path.dirname(fname), prefix+os.path.basename(fname)) + + def get_lines_as_list(fname, delim): ''' Return a list that contains all the lines present in the file fname @@ -39,9 +66,9 @@ def copy_dir_tree(src, dest): """ Copy entire directory tree rooted at the src directory. Uses shutil interface.""" try: - shutil.copytree(src, dest) + su.copytree(src, dest) # Directories are the same - except shutil.Error as ex: + except su.Error as ex: print('Directory not copied. Error: %s' % ex) # Any error saying that the directory doesn't exist except OSError as ex: diff --git a/scripts/extractSliceVideoFrom4d.py b/scripts/extractSliceVideoFrom4d.py index b4351c3..a539c02 100755 --- a/scripts/extractSliceVideoFrom4d.py +++ b/scripts/extractSliceVideoFrom4d.py @@ -27,6 +27,9 @@ def get_input_options(): parser.add_argument( '-rotate', help='If given overrides the default rotation used for ' 'proper orientation of the slice.') + parser.add_argument( + '-crop', help='convert -crop ops: If given crops 2D slices before ' + 'combining. wxh+x+y') ops = parser.parse_args() return ops @@ -65,6 +68,9 @@ def main(): else: cmd = 'convert -rotate %s %s %s' % (ops.rotate, tmp2DImage, tmp2DImage) bu.print_and_execute(cmd, False) + if ops.crop: + cmd = 'convert %s -crop %s +repage %s' % (tmp2DImage, ops.crop, tmp2DImage) + bu.print_and_execute(cmd, False) # Write time-point info if ops.time_unit is not None: if ops.time_step is not None: diff --git a/scripts/generateCroppedSegImagesFromFsSeg.py b/scripts/generateCroppedSegImagesFromFsSeg.py index 33da810..a8d8be9 100755 --- a/scripts/generateCroppedSegImagesFromFsSeg.py +++ b/scripts/generateCroppedSegImagesFromFsSeg.py @@ -46,7 +46,7 @@ def get_input_options(): 'considered as CSF regions which depends on the labels present in the ' 'file you provided with -fs_csf_labels. ') parser.add_argument( - 'in_new_cluster', action='store_true', help='Env variables for new nef') + '--in_new_cluster', action='store_true', help='Env variables for new nef') ops = parser.parse_args() if not op.exists(ops.res_dir): os.makedirs(ops.res_dir) diff --git a/scripts/image_math.py b/scripts/image_math.py index f95f8b0..c58e926 100644 --- a/scripts/image_math.py +++ b/scripts/image_math.py @@ -16,6 +16,23 @@ import os.path as op import time as tm +def get_min_max(imgs, are_fnames=True): + ''' + Return the minimum and maximum intensities from among the given images. + imgs must be a list! + are_fnames: boolean; if True imgs is a list of img_filenames. + So first loads those files, otherwise considers them as numpy array images. + ''' + mins, maxs = np.zeros(len(imgs)), np.zeros(len(imgs)) + i = 0 + for img in imgs: + if are_fnames: + img = (ni.load(img)).get_data() + mins[i], maxs[i] = np.amin(img), np.amax(img) + i += 1 + return np.amin(mins), np.amax(maxs) + + def diff_image(in_img1, in_img2, out_img=None): ''' If third argument is not given or None (default), considers diff --git a/scripts/post_process.py b/scripts/post_process.py index b1aea5f..10d005c 100644 --- a/scripts/post_process.py +++ b/scripts/post_process.py @@ -1,4 +1,4 @@ -0#!/usr/bin/python +#!/usr/bin/python """ Post process after simulating the images. """ # Author: Bishesh Khanal @@ -93,7 +93,7 @@ def set_binaries_and_paths(): #img_math = op.join(ants_dir, 'ImageMath') #binarize = op.join(adlem_dir, 'build/src/BinarizeThreshold') paste_img = op.join( - adlem_dir, 'build/src/pasteImageToBiggerImage') + adlem_dir, 'buildNewNef/src/pasteImageToBiggerImage') compose_transform = op.join(ants_dir, 'ComposeMultiTransform') apply_transform = op.join(ants_dir, 'antsApplyTransforms') return