From 10dabacf1da9702640e355ad0c1fdd79ba227c92 Mon Sep 17 00:00:00 2001 From: Julien Cohen-Adad Date: Tue, 16 Jan 2024 22:35:51 -0500 Subject: [PATCH] Use manual seg and labeling (#62) Fixes #54, Fixes #60 --- data_processing.ipynb | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/data_processing.ipynb b/data_processing.ipynb index a8a1939..c466d3c 100644 --- a/data_processing.ipynb +++ b/data_processing.ipynb @@ -93,6 +93,7 @@ "from scipy.interpolate import interp1d\n", "from scipy.ndimage import uniform_filter1d\n", "from scipy.stats import f_oneway\n", + "import shutil\n", "from statsmodels.stats.multicomp import pairwise_tukeyhsd" ] }, @@ -170,6 +171,7 @@ "\n", "path_data = os.getcwd()\n", "print(f\"path_data: {path_data}\")\n", + "path_labels = os.path.join(path_data, \"derivatives\", \"labels\")\n", "path_qc = os.path.join(path_data, \"qc\")\n", "shim_modes = [\"CP\", \"patient\", \"volume\", \"phase\", \"CoV\", \"target\", \"SAReff\"]\n", "shim_modes_MPRAGE = [\"CP\", \"CoV\"] # TODO: change variable name PEP8\n", @@ -198,18 +200,30 @@ "metadata": {}, "outputs": [], "source": [ - "# Run segmentation on CoV_T1w scan\n", + "# Run segmentation on CoV_T1w scan, unless it is already in the derivatives/labels\n", "\n", "for subject in subjects:\n", " os.chdir(os.path.join(path_data, subject, \"anat\"))\n", - " !sct_deepseg_sc -i {subject}_acq-CoV_T1w.nii.gz -c t1 -qc {path_qc}" + " fname_manual_seg = os.path.join(path_labels, subject, \"anat\", f\"{subject}_acq-CoV_T1w_seg.nii.gz\")\n", + " if os.path.exists(fname_manual_seg):\n", + " # Manual segmentation already exists. Copy it to local folder\n", + " print(f\"{subject}: Manual segmentation found\\n\")\n", + " shutil.copyfile(fname_manual_seg, f\"{subject}_acq-CoV_T1w_seg.nii.gz\")\n", + " # Generate QC report to make sure the manual segmentation is correct\n", + " !sct_qc -i {subject}_acq-CoV_T1w.nii.gz -s {subject}_acq-CoV_T1w_seg.nii.gz -p sct_deepseg_sc -qc {path_qc} -qc-subject {subject}\n", + " else:\n", + " # Manual segmentation does not exist. Run automatic segmentation.\n", + " print(f\"{subject}: Manual segmentation not found\")\n", + " !sct_deepseg_sc -i {subject}_acq-CoV_T1w.nii.gz -c t1 -qc {path_qc}" ] }, { "cell_type": "code", "execution_count": null, "id": "663bb98c", - "metadata": {}, + "metadata": { + "scrolled": true + }, "outputs": [], "source": [ "# Crop the images for faster processing\n", @@ -229,19 +243,14 @@ "source": [ "# Vertebral labeling on the CoV_T1w scan\n", "\n", + "# Given the low resolution of the GRE scan, the automatic detection of C2-C3 disc is unreliable. Therefore, we \n", + "# need to use the manual disc labels that are part of the dataset.\n", "for subject in subjects:\n", " os.chdir(os.path.join(path_data, subject, \"anat\"))\n", - " !sct_label_vertebrae -i {subject}_acq-CoV_T1w_crop.nii.gz -s {subject}_acq-CoV_T1w_crop_seg.nii.gz -c t1 -qc {path_qc}\n", - " \n", - "# TODO: replace by code below\n", - "# https://github.com/shimming-toolbox/rf-shimming-7t/issues/59\n", - "# Given the low resolution of the GRE scan, the automatic detection of C2-C3 disc is unreliable. Therefore we need to use the manual disc labels that are part of the dataset.\n", - "# for subject in subjects:\n", - "# os.chdir(os.path.join(path_data, subject, \"anat\"))\n", - "# fname_label_discs = os.path.join(path_data, \"derivatives\", \"labels\", subject, \"anat\", f\"{subject}_acq-CoV_T2starw_label-discs_dseg.nii.gz\")\n", - "# !sct_label_utils -i {subject}_acq-CoV_T2starw_crop_seg.nii.gz -disc {fname_label_discs} -o {subject}_acq-CoV_T2starw_crop_seg_labeled.nii.gz\n", - "# # Generate QC report to assess labeled segmentation\n", - "# !sct_qc -i {subject}_acq-CoV_T2starw_crop.nii.gz -s {subject}_acq-CoV_T2starw_crop_seg_labeled.nii.gz -p sct_label_vertebrae -qc {path_qc} -qc-subject {subject}" + " fname_label_discs = os.path.join(path_labels, subject, \"anat\", f\"{subject}_acq-CoV_T1w_label-discs_dseg.nii.gz\")\n", + " !sct_label_utils -i {subject}_acq-CoV_T1w_crop_seg.nii.gz -disc {fname_label_discs} -o {subject}_acq-CoV_T1w_crop_seg_labeled.nii.gz\n", + " # Generate QC report to assess labeled segmentation\n", + " !sct_qc -i {subject}_acq-CoV_T1w_crop.nii.gz -s {subject}_acq-CoV_T1w_crop_seg_labeled.nii.gz -p sct_label_vertebrae -qc {path_qc} -qc-subject {subject}" ] }, {