diff --git a/data/sub-01/anat/sub-01_run-1_T2w_masked.nii.gz b/data/sub-01/anat/sub-01_run-1_T2w_masked.nii.gz new file mode 100644 index 000000000..36ddf6b70 Binary files /dev/null and b/data/sub-01/anat/sub-01_run-1_T2w_masked.nii.gz differ diff --git a/data/sub-01/anat/sub-01_run-2_T2w_masked.nii.gz b/data/sub-01/anat/sub-01_run-2_T2w_masked.nii.gz new file mode 100644 index 000000000..6e05be889 Binary files /dev/null and b/data/sub-01/anat/sub-01_run-2_T2w_masked.nii.gz differ diff --git a/data/sub-01/anat/sub-01_run-3_T2w_masked.nii.gz b/data/sub-01/anat/sub-01_run-3_T2w_masked.nii.gz new file mode 100644 index 000000000..3f45e933a Binary files /dev/null and b/data/sub-01/anat/sub-01_run-3_T2w_masked.nii.gz differ diff --git a/data/sub-01/anat/sub-01_run-4_T2w_masked.nii.gz b/data/sub-01/anat/sub-01_run-4_T2w_masked.nii.gz new file mode 100644 index 000000000..ae8834043 Binary files /dev/null and b/data/sub-01/anat/sub-01_run-4_T2w_masked.nii.gz differ diff --git a/data/sub-01/anat/sub-01_run-5_T2w_masked.nii.gz b/data/sub-01/anat/sub-01_run-5_T2w_masked.nii.gz new file mode 100644 index 000000000..7f3697839 Binary files /dev/null and b/data/sub-01/anat/sub-01_run-5_T2w_masked.nii.gz differ diff --git a/data/sub-01/anat/sub-01_run-6_T2w_masked.nii.gz b/data/sub-01/anat/sub-01_run-6_T2w_masked.nii.gz new file mode 100644 index 000000000..97b359807 Binary files /dev/null and b/data/sub-01/anat/sub-01_run-6_T2w_masked.nii.gz differ diff --git a/notebooks/brainExtractionU-Net.ipynb b/notebooks/brainExtractionU-Net.ipynb index 944da4df2..316215908 100644 --- a/notebooks/brainExtractionU-Net.ipynb +++ b/notebooks/brainExtractionU-Net.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 21, + "execution_count": 29, "metadata": {}, "outputs": [], "source": [ @@ -45,7 +45,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 30, "metadata": {}, "outputs": [], "source": [ @@ -60,7 +60,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 45, "metadata": {}, "outputs": [], "source": [ @@ -84,6 +84,10 @@ " \n", " def _run_interface(self, runtime): \n", " \n", + " #_, name, ext = split_filename(os.path.abspath(self.inputs.in_file))\n", + " #out_file = os.path.join(os.getcwd().replace(self.inputs.base_dir,'/fetaldata'), ''.join((name, self.inputs.out_postfix, ext)))\n", + " print(\"9lawi:\")\n", + " #print(out_file)\n", " cmd = 'python brainExtractionTwoSteps.py -i \"{}\" -c \"{}\" -t \"{}\" -C \"{}\" -T \"{}\" -o \"{}\" '.format(self.inputs.in_file,self.inputs.in_ckpt_loc,\n", " self.inputs.threshold_loc,self.inputs.in_ckpt_seg,\n", " self.inputs.threshold_seg,self.inputs.out_postfix)\n", @@ -96,9 +100,11 @@ " return runtime\n", "\n", " def _list_outputs(self):\n", - "\n", + " #outputs = self._outputs().get()\n", + " #_, name, ext = split_filename(os.path.abspath(self.inputs.in_file))\n", + " #outputs['out_file'] = os.path.join(os.getcwd().replace(self.inputs.base_dir,'/fetaldata'), ''.join((name, self.inputs.out_postfix, ext)))\n", " return {'out_file': self.inputs.in_file[:-4]+self.inputs.out_postfix}\n", - " \n", + " #return outputs\n", " \n", "class MultipleBrainExtractionInputSpec(BaseInterfaceInputSpec):\n", " base_dir = Directory(desc='Root directory',mandatory=True,exists=True)\n", @@ -131,31 +137,34 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "200819-18:26:32,370 nipype.workflow INFO:\n", + "200826-16:58:06,787 nipype.workflow INFO:\n", "\t Workflow BrainExtractTwoStepsWorfklow settings: ['check', 'execution', 'logging', 'monitoring']\n", - "200819-18:26:32,374 nipype.workflow INFO:\n", + "200826-16:58:06,791 nipype.workflow INFO:\n", "\t Running serially.\n", - "200819-18:26:32,375 nipype.workflow INFO:\n", + "200826-16:58:06,792 nipype.workflow INFO:\n", "\t [Node] Setting-up \"BrainExtractTwoStepsWorfklow.data_grabber\" in \"/media/hkebiri/Storage/dti_t2_ANDRAS/export_normal/original-t2/NIFTI_HK/TestBrainExtraction/BrainExtractTwoStepsWorfklow/data_grabber\".\n", - "200819-18:26:32,379 nipype.workflow INFO:\n", + "200826-16:58:06,796 nipype.workflow INFO:\n", "\t [Node] Running \"data_grabber\" (\"nipype.interfaces.io.DataGrabber\")\n", - "200819-18:26:32,383 nipype.workflow INFO:\n", + "200826-16:58:06,799 nipype.workflow INFO:\n", "\t [Node] Finished \"BrainExtractTwoStepsWorfklow.data_grabber\".\n", - "200819-18:26:32,384 nipype.workflow INFO:\n", + "200826-16:58:06,800 nipype.workflow INFO:\n", "\t [Node] Setting-up \"BrainExtractTwoStepsWorfklow.Multiple_Brain_extraction\" in \"/media/hkebiri/Storage/dti_t2_ANDRAS/export_normal/original-t2/NIFTI_HK/TestBrainExtraction/BrainExtractTwoStepsWorfklow/Multiple_Brain_extraction\".\n", - "200819-18:26:32,391 nipype.workflow INFO:\n", + "200826-16:58:06,803 nipype.workflow INFO:\n", "\t [Node] Running \"Multiple_Brain_extraction\" (\"__main__.MultipleBrainExtraction\")\n", + "9lawi:\n", "... cmd: python brainExtractionTwoSteps.py -i \"/media/hkebiri/Storage/dti_t2_ANDRAS/export_normal/original-t2/NIFTI_HK/TestBrainExtraction/6_twoSteps/Image6_worldmatrixModified_lr.nii\" -c \"/home/hkebiri/Desktop/mialsuperresolutiontoolkit/pymialsrtk/data/Network_checkpoints_localization/Unet.ckpt-88000\" -t \"0.49\" -C \"/home/hkebiri/Desktop/mialsuperresolutiontoolkit/pymialsrtk/data/Network_checkpoints_segmentation/Unet.ckpt-20000\" -T \"0.5\" -o \"_masked.nii.gz\" \n", + "9lawi:\n", "... cmd: python brainExtractionTwoSteps.py -i \"/media/hkebiri/Storage/dti_t2_ANDRAS/export_normal/original-t2/NIFTI_HK/TestBrainExtraction/6_twoSteps/Image0_worldmatrixModified_lr.nii\" -c \"/home/hkebiri/Desktop/mialsuperresolutiontoolkit/pymialsrtk/data/Network_checkpoints_localization/Unet.ckpt-88000\" -t \"0.49\" -C \"/home/hkebiri/Desktop/mialsuperresolutiontoolkit/pymialsrtk/data/Network_checkpoints_segmentation/Unet.ckpt-20000\" -T \"0.5\" -o \"_masked.nii.gz\" \n", + "9lawi:\n", "... cmd: python brainExtractionTwoSteps.py -i \"/media/hkebiri/Storage/dti_t2_ANDRAS/export_normal/original-t2/NIFTI_HK/TestBrainExtraction/6_twoSteps/Image1_worldmatrixModified_lr.nii\" -c \"/home/hkebiri/Desktop/mialsuperresolutiontoolkit/pymialsrtk/data/Network_checkpoints_localization/Unet.ckpt-88000\" -t \"0.49\" -C \"/home/hkebiri/Desktop/mialsuperresolutiontoolkit/pymialsrtk/data/Network_checkpoints_segmentation/Unet.ckpt-20000\" -T \"0.5\" -o \"_masked.nii.gz\" \n", - "200819-18:26:52,511 nipype.workflow INFO:\n", + "200826-16:58:20,347 nipype.workflow INFO:\n", "\t [Node] Finished \"BrainExtractTwoStepsWorfklow.Multiple_Brain_extraction\".\n" ] } diff --git a/notebooks/crash-20200826-150727-hkebiri-Multiple_Brain_extraction-13880d5a-1c67-49ad-ab43-ad2eb34a9da6.pklz b/notebooks/crash-20200826-150727-hkebiri-Multiple_Brain_extraction-13880d5a-1c67-49ad-ab43-ad2eb34a9da6.pklz new file mode 100644 index 000000000..894b2b260 Binary files /dev/null and b/notebooks/crash-20200826-150727-hkebiri-Multiple_Brain_extraction-13880d5a-1c67-49ad-ab43-ad2eb34a9da6.pklz differ diff --git a/notebooks/crash-20200826-150806-hkebiri-Multiple_Brain_extraction-fb02b5dd-c5ad-4c00-9b6d-f8050757102c.pklz b/notebooks/crash-20200826-150806-hkebiri-Multiple_Brain_extraction-fb02b5dd-c5ad-4c00-9b6d-f8050757102c.pklz new file mode 100644 index 000000000..f7ea31d41 Binary files /dev/null and b/notebooks/crash-20200826-150806-hkebiri-Multiple_Brain_extraction-fb02b5dd-c5ad-4c00-9b6d-f8050757102c.pklz differ diff --git a/notebooks/histogramNormalization.ipynb b/notebooks/histogramNormalization.ipynb index 2afe5d74b..41f796673 100644 --- a/notebooks/histogramNormalization.ipynb +++ b/notebooks/histogramNormalization.ipynb @@ -295,7 +295,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.2" + "version": "3.7.3" } }, "nbformat": 4, diff --git a/pymialsrtk/interfaces/preprocess.py b/pymialsrtk/interfaces/preprocess.py index cd5360b6a..b9314ca35 100644 --- a/pymialsrtk/interfaces/preprocess.py +++ b/pymialsrtk/interfaces/preprocess.py @@ -661,16 +661,19 @@ class BrainExtraction(BaseInterface): input_spec = BrainExtractionInputSpec output_spec = BrainExtractionOutputSpec - def _run_interface(self, runtime): + def _run_interface(self, runtime): + _, name, ext = split_filename(os.path.abspath(self.inputs.in_file)) + out_file = os.path.join(os.getcwd().replace(self.inputs.bids_dir,'/fetaldata'), ''.join((name, self.inputs.out_postfix, ext))) + try: self._extractBrain(self.inputs.in_file, self.inputs.in_ckpt_loc, self.inputs.threshold_loc, - self.inputs.in_ckpt_seg, self.inputs.threshold_seg, self.inputs.out_postfix) + self.inputs.in_ckpt_seg, self.inputs.threshold_seg, self.inputs.bids_dir, self.inputs.out_postfix) except Exception as e: print('Failed') print(e) return runtime - def _extractBrain(self, dataPath, modelCkptLoc, thresholdLoc,modelCkptSeg,thresholdSeg, out_postfix): + def _extractBrain(self, dataPath, modelCkptLoc, thresholdLoc,modelCkptSeg,thresholdSeg, bidsDir, out_postfix): #Step1: Main part brain localization normalize = "local_max" @@ -680,6 +683,7 @@ def _extractBrain(self, dataPath, modelCkptLoc, thresholdLoc,modelCkptSeg,thresh border_y = 15 n_channels = 1 + img_nib = nibabel.load(os.path.join(dataPath)) image_data = img_nib.get_data() images = np.zeros((image_data.shape[2], width, height, n_channels)) @@ -865,7 +869,6 @@ def _extractBrain(self, dataPath, modelCkptLoc, thresholdLoc,modelCkptSeg,thresh subImages = np.zeros((images.shape[0], width, height)) - print(images.shape) for ii in range(images.shape[0]): subImages[ii, :, :] = cv2.resize(images[ii, x_beg:x_end, y_beg:y_end,:], dsize=(width, height)) print(images.shape) @@ -890,17 +893,20 @@ def _extractBrain(self, dataPath, modelCkptLoc, thresholdLoc,modelCkptSeg,thresh pred3dFinal[idx, x_beg:x_end, y_beg:y_end,0] = pred_bin.astype('float64') #pred3d.append(pred_bin[0, :, :, 0].astype('float64')) - pppp = False + pppp = True if pppp: pred3dFinal = self._post_processing(np.asarray(pred3dFinal)) pred3d = [cv2.resize(elem, dsize=(image_data.shape[1], image_data.shape[0]), interpolation=cv2.INTER_NEAREST) for elem in pred3dFinal] pred3d = np.asarray(pred3d) upsampled = np.swapaxes(np.swapaxes(pred3d,1,2),0,2) #if Orient module applied, no need for this line(?) up_mask = nibabel.Nifti1Image(upsampled,img_nib.affine) - nibabel.save(up_mask, dataPath.split('.')[0]+out_postfix) + + _, name, ext = split_filename(os.path.abspath(dataPath)) + save_file = os.path.join(os.getcwd().replace(bidsDir,'/fetaldata'), ''.join((name, out_postfix, ext))) + nibabel.save(up_mask, save_file) #Funnction returning largest connected component of an object - def _extractLargestCC(image): + def _extractLargestCC(self, image): nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(image, connectivity=4) sizes = stats[:, -1] max_label = 1 diff --git a/setup.py b/setup.py index 5bb34c8b0..a2d6bfb69 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,8 @@ "pymialsrtk.pipelines.anatomical"] package_data = {"pymialsrtk": - ['data/Network_checkpoints/*'] + ['data/Network_checkpoints/Network_checkpoints_localization/*', + 'data/Network_checkpoints/Network_checkpoints_segmentation/*'] } # package_data = {'cmtklib': diff --git a/src/BrainLocalizationExtraction/brainExtractionTwoSteps.py b/src/BrainLocalizationExtraction/brainExtractionTwoSteps.py index 5e4200846..36ace9f0d 100644 --- a/src/BrainLocalizationExtraction/brainExtractionTwoSteps.py +++ b/src/BrainLocalizationExtraction/brainExtractionTwoSteps.py @@ -254,6 +254,7 @@ def extractBrain(dataPath, modelCkptLoc, thresholdLoc,modelCkptSeg,thresholdSeg, pred3d = np.asarray(pred3d) upsampled = np.swapaxes(np.swapaxes(pred3d,1,2),0,2) #if Orient module applied, no need for this line(?) up_mask = nibabel.Nifti1Image(upsampled,img_nib.affine) + print(dataPath.split('.')[0]+out_postfix) nibabel.save(up_mask, dataPath.split('.')[0]+out_postfix) #Funnction returning largest connected component of an object