Skip to content

Commit

Permalink
Corrected bug where masks were not saved in the right location and he…
Browse files Browse the repository at this point in the history
…nce the following nodes were wrongly working
  • Loading branch information
Hamza Kebiri committed Aug 26, 2020
1 parent b4945a1 commit d394ad3
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 23 deletions.
Binary file added data/sub-01/anat/sub-01_run-1_T2w_masked.nii.gz
Binary file not shown.
Binary file added data/sub-01/anat/sub-01_run-2_T2w_masked.nii.gz
Binary file not shown.
Binary file added data/sub-01/anat/sub-01_run-3_T2w_masked.nii.gz
Binary file not shown.
Binary file added data/sub-01/anat/sub-01_run-4_T2w_masked.nii.gz
Binary file not shown.
Binary file added data/sub-01/anat/sub-01_run-5_T2w_masked.nii.gz
Binary file not shown.
Binary file added data/sub-01/anat/sub-01_run-6_T2w_masked.nii.gz
Binary file not shown.
37 changes: 23 additions & 14 deletions notebooks/brainExtractionU-Net.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -45,7 +45,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -60,7 +60,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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"
]
}
Expand Down
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion notebooks/histogramNormalization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
"version": "3.7.3"
}
},
"nbformat": 4,
Expand Down
20 changes: 13 additions & 7 deletions pymialsrtk/interfaces/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
1 change: 1 addition & 0 deletions src/BrainLocalizationExtraction/brainExtractionTwoSteps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d394ad3

Please sign in to comment.