Using BEN with Nipype
As skull stripping is usually the first preprocessing step in the most pipeline, BEN can be used as a tool independently or called in shell scripts to work with other neuroimaging tool synergistically.
1.0 Set up and download MRI data.
[ ]:
!pip install nipype simpleitk
[2]:
!git clone https://github.com/yu02019/BEN.git
Cloning into 'BEN'...
remote: Enumerating objects: 139, done.
remote: Counting objects: 100% (66/66), done.
remote: Compressing objects: 100% (42/42), done.
remote: Total 139 (delta 22), reused 65 (delta 22), pack-reused 73
Receiving objects: 100% (139/139), 6.38 MiB | 12.46 MiB/s, done.
Resolving deltas: 100% (41/41), done.
[3]:
cd BEN
/content/BEN
[4]:
!gdown --id 1fmyBtxNJAYFtlwVBY5IiFsmUixc2MsU9
!sh download.sh
/usr/local/lib/python3.7/dist-packages/gdown/cli.py:131: FutureWarning: Option `--id` was deprecated in version 4.3.1 and will be removed in 5.0. You don't need to pass it anymore to use a file ID.
category=FutureWarning,
Downloading...
From: https://drive.google.com/uc?id=1fmyBtxNJAYFtlwVBY5IiFsmUixc2MsU9
To: /content/BEN/download.sh
100% 273/273 [00:00<00:00, 449kB/s]
/usr/local/lib/python3.7/dist-packages/gdown/cli.py:131: FutureWarning: Option `--id` was deprecated in version 4.3.1 and will be removed in 5.0. You don't need to pass it anymore to use a file ID.
category=FutureWarning,
Downloading...
From: https://drive.google.com/uc?id=1z-VMfPvFILNcaEc9jSYzcnub7t4ZcrfJ
To: /content/BEN/cross_domain/cross_domain.zip
100% 26.8M/26.8M [00:00<00:00, 144MB/s]
Archive: cross_domain.zip
creating: 7T/
creating: 7T/src/
inflating: 7T/src/20171226_185416T1RAREs30001a001.nii.gz
inflating: 7T/src/20180321_133106T2TurboRAREs20001a001.nii.gz
inflating: 7T/src/20180510_143551T1RAREs30001a001.nii.gz
creating: epi/
creating: epi/src/
inflating: epi/src/ASD_Xinhua_SJL_20191215_con06__E3_P1.nii.gz
inflating: epi/src/ASD_Xinhua_SJL_20191215_tcell_0__E3_P1.1.nii.gz
creating: rat/
creating: rat/label/
inflating: rat/label/42d_cas_27_T2.nii.gz
creating: rat/src/
inflating: rat/src/42d_cas_27_T2.nii.gz
inflating: rat/src/42d_cas_40_T2.nii.gz
inflating: rat/src/42d_cas_44_T2.nii.gz
/usr/local/lib/python3.7/dist-packages/gdown/cli.py:131: FutureWarning: Option `--id` was deprecated in version 4.3.1 and will be removed in 5.0. You don't need to pass it anymore to use a file ID.
category=FutureWarning,
Downloading...
From: https://drive.google.com/uc?id=1K5qIe6o8uG7cw_Uu0H3aB0FE40YXb9Hp
To: /content/BEN/weight/weight.zip
100% 69.3M/69.3M [00:00<00:00, 89.6MB/s]
Archive: weight.zip
creating: unet_fp32_all_BN_NoCenterScale_polyic_epoch15_bottle256_04012051/
inflating: unet_fp32_all_BN_NoCenterScale_polyic_epoch15_bottle256_04012051/.data-00000-of-00002
inflating: unet_fp32_all_BN_NoCenterScale_polyic_epoch15_bottle256_04012051/.data-00001-of-00002
inflating: unet_fp32_all_BN_NoCenterScale_polyic_epoch15_bottle256_04012051/.index
inflating: unet_fp32_all_BN_NoCenterScale_polyic_epoch15_bottle256_04012051/checkpoint
creating: unet_fp32_all_BN_NoCenterScale_polyic_epoch15_bottle256_04012056/
inflating: unet_fp32_all_BN_NoCenterScale_polyic_epoch15_bottle256_04012056/.data-00000-of-00002
inflating: unet_fp32_all_BN_NoCenterScale_polyic_epoch15_bottle256_04012056/.data-00001-of-00002
inflating: unet_fp32_all_BN_NoCenterScale_polyic_epoch15_bottle256_04012056/.index
inflating: unet_fp32_all_BN_NoCenterScale_polyic_epoch15_bottle256_04012056/checkpoint
1.1 Downloading, Extracting and Copying ANTs binaries
Install ANTs in Colab
ANTs installation guide: https://www.suyogjadhav.com/misc/2019/03/28/Using-ANTs-package-on-Google-Colaboratory/
[5]:
!gdown 1N1Qx-R5tLCX5EhXPoPdyg6YvEkDtf-cD
Downloading...
From: https://drive.google.com/uc?id=1N1Qx-R5tLCX5EhXPoPdyg6YvEkDtf-cD
To: /content/BEN/ANTs-28-03-2019.7z
100% 316M/316M [00:02<00:00, 154MB/s]
[6]:
!7z x ANTs-28-03-2019.7z
7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=en_US.UTF-8,Utf16=on,HugeFiles=on,64 bits,2 CPUs Intel(R) Xeon(R) CPU @ 2.20GHz (406F0),ASM,AES-NI)
Scanning the drive for archives:
0M Scan 1 file, 315597527 bytes (301 MiB)
Extracting archive: ANTs-28-03-2019.7z
--
Path = ANTs-28-03-2019.7z
Type = 7z
Physical Size = 315597527
Headers Size = 2380
Method = LZMA2:24 BCJ
Solid = +
Blocks = 1
0% 0% 1 - bin/ANTS 1% 1 - bin/ANTS 1% 2 - bin/ANTSIntegrateVectorField 2% 2 - bin/ANTSIntegrateVectorField 2% 3 - bin/ANTSIntegrateVelocityField 3% 3 - bin/ANTSIntegrateVelocityField 3% 4 - bin/ANTSJacobian 4% 5 - bin/ANTSUseDeformationFieldToGetAffineTransform 5% 5 - bin/ANTSUseDeformationFieldToGetAffineTransform 5% 6 - bin/ANTSUseLandmarkImagesToGetAffineTransform 6% 6 - bin/ANTSUseLandmarkImagesToGetAffineTransform 6% 7 - bin/ANTSUseLandmarkImagesToGetBSplineDisplacementField 7% 7 - bin/ANTSUseLandmarkImagesToGetBSplineDisplacementField 7% 8 - bin/Atropos 8% 8 - bin/Atropos 8% 9 - bin/AverageAffineTransform 9% 9 - bin/AverageAffineTransform 10% 10 - bin/AverageAffineTransformNoRigid 11% 10 - bin/AverageAffineTransformNoRigid 11% 11 - bin/AverageImages 12% 12 - bin/AverageTensorImages 12% 13 - bin/ClusterImageStatistics 13% 14 - bin/ComposeMultiTransform 14% 14 - bin/ComposeMultiTransform 14% 15 - bin/CompositeTransformUtil 15% 15 - bin/CompositeTransformUtil 16% 15 - bin/CompositeTransformUtil 16% 16 - bin/ConvertImage 17% 16 - bin/ConvertImage 17% 17 - bin/ConvertImagePixelType 18% 18 - bin/ConvertInputImagePixelTypeToFloat 19% 19 - bin/ConvertScalarImageToRGB 20% 20 - bin/ConvertToJpg 21% 21 - bin/ConvertTransformFile 21% 22 - bin/CopyImageHeaderInformation 22% 22 - bin/CopyImageHeaderInformation 22% 23 - bin/CreateDTICohort 23% 24 - bin/CreateDisplacementField 24% 24 - bin/CreateDisplacementField 25% 25 - bin/CreateImage 26% 27 - bin/CreateTiledMosaic 26% 28 - bin/CreateWarpedGridImage 28% 29 - bin/DeNrrd 28% 30 - bin/DenoiseImage 29% 31 - bin/ExtractRegionFromImage 30% 31 - bin/ExtractRegionFromImage 30% 33 - bin/ExtractSliceFromImage 31% 34 - bin/FitBSplineToPoints 32% 35 - bin/GetConnectedComponentsFeatureImages 33% 35 - bin/GetConnectedComponentsFeatureImages 33% 38 - bin/ImageCompare 34% 39 - bin/ImageIntensityStatistics 35% 40 - bin/ImageMath 36% 40 - bin/ImageMath 36% 41 - bin/ImageSetStatistics 37% 41 - bin/ImageSetStatistics 37% 42 - bin/KellyKapowski 38% 42 - bin/KellyKapowski 39% 43 - bin/KellySlater 39% 44 - bin/LabelClustersUniquely 40% 44 - bin/LabelClustersUniquely 40% 45 - bin/LabelGeometryMeasures 41% 45 - bin/LabelGeometryMeasures 41% 46 - bin/LabelOverlapMeasures 42% 46 - bin/LabelOverlapMeasures 43% 47 - bin/LaplacianThickness 44% 48 - bin/LesionFilling 44% 49 - bin/MeasureImageSimilarity 45% 49 - bin/MeasureImageSimilarity 46% 50 - bin/MeasureMinMaxMean 46% 51 - bin/MemoryTest 47% 51 - bin/MemoryTest 47% 52 - bin/MultiplyImages 48% 53 - bin/N3BiasFieldCorrection 49% 54 - bin/N4BiasFieldCorrection 50% 54 - bin/N4BiasFieldCorrection 50% 55 - bin/NonLocalSuperResolution 51% 55 - bin/NonLocalSuperResolution 51% 56 - bin/PasteImageIntoImage 52% 57 - bin/PermuteFlipImageOrientationAxes 53% 58 - bin/PrintHeader 53% 59 - bin/RebaseTensorImage 54% 59 - bin/RebaseTensorImage 54% 60 - bin/ReorientTensorImage 55% 60 - bin/ReorientTensorImage 56% 61 - bin/ResampleImage 57% 61 - bin/ResampleImage 57% 62 - bin/ResampleImageBySpacing 58% 63 - bin/ResetDirection 58% 64 - bin/SetDirectionByMatrix 59% 65 - bin/SetOrigin 60% 66 - bin/SetSpacing 61% 67 - bin/SimulateDisplacementField 62% 68 - bin/SmoothDisplacementField 63% 69 - bin/SmoothImage 64% 71 - bin/SuperResolution 65% 72 - bin/SurfaceBasedSmoothing 66% 73 - bin/SurfaceCurvature 67% 74 - bin/TextureCooccurrenceFeatures 68% 76 - bin/ThresholdImage 69% 77 - bin/TileImages 70% 78 - bin/TimeSCCAN 71% 79 - bin/WarpImageMultiTransform 72% 79 - bin/WarpImageMultiTransform 72% 80 - bin/WarpTensorImageMultiTransform 73% 80 - bin/WarpTensorImageMultiTransform 73% 81 - bin/WarpTimeSeriesImageMultiTransform 74% 81 - bin/WarpTimeSeriesImageMultiTransform 74% 82 - bin/antsAI 75% 82 - bin/antsAI 76% 82 - bin/antsAI 76% 83 - bin/antsAffineInitializer 77% 83 - bin/antsAffineInitializer 77% 84 - bin/antsAlignOrigin 78% 84 - bin/antsAlignOrigin 78% 85 - bin/antsApplyTransforms 79% 85 - bin/antsApplyTransforms 80% 85 - bin/antsApplyTransforms 80% 86 - bin/antsApplyTransformsToPoints 81% 86 - bin/antsApplyTransformsToPoints 81% 87 - bin/antsJointFusion 82% 87 - bin/antsJointFusion 83% 88 - bin/antsJointTensorFusion 83% 89 - bin/antsLandmarkBasedTransformInitializer 84% 89 - bin/antsLandmarkBasedTransformInitializer 84% 90 - bin/antsMotionCorr 85% 90 - bin/antsMotionCorr 85% 91 - bin/antsMotionCorrDiffusionDirection 86% 91 - bin/antsMotionCorrDiffusionDirection 87% 91 - bin/antsMotionCorrDiffusionDirection 87% 92 - bin/antsMotionCorrStats 88% 92 - bin/antsMotionCorrStats 88% 93 - bin/antsRegistration 89% 93 - bin/antsRegistration 90% 93 - bin/antsRegistration 91% 93 - bin/antsRegistration 91% 94 - bin/antsSliceRegularizedRegistration 92% 94 - bin/antsSliceRegularizedRegistration 92% 95 - bin/antsTransformInfo 93% 95 - bin/antsTransformInfo 93% 96 - bin/antsUtilitiesTesting 94% 96 - bin/antsUtilitiesTesting 94% 97 - bin/compareTwoTransforms 95% 97 - bin/compareTwoTransforms 95% 98 - bin/iMath 96% 98 - bin/iMath 96% 99 - bin/itkTestDriver 97% 99 - bin/itkTestDriver 98% 100 - bin/sccan 98% 101 - bin/simpleSynRegistration 99% 101 - bin/simpleSynRegistration Everything is Ok
Folders: 1
Files: 101
Size: 1961813896
Compressed: 315597527
Now copy the entire contents of the newly created bin folder to /usr/local/bin/
[7]:
!cp bin/* /usr/local/bin
Test the installation by running:
[8]:
!which antsRegistration
!N4BiasFieldCorrection --version
/usr/local/bin/antsRegistration
ANTs Version: 3.0.0.0.dev35-g68094
Compiled: Mar 28 2019 10:37:32
[14]:
cd ..
/content/BEN
1.2 Transfer BEN to this new domain/application (if already have trained weight, skip this step)
Please refer this notebook.
''' cross species '''
from glob import glob
from utils.load_data import get_itk_array
from utils.visualization import load_slice_cross_species, plot_segmentation_cross_species
# from utils.visualization import plot_segmentation
raw, zeroshot, ft, DA, gt = load_slice_cross_species(raw_path=r'cross_domain/rat/src',
zeroshot_path=r'cross_domain/rat/pred-Rat-42d-2022-%0-ft',
ft_path=r'cross_domain/rat/pred-Rat-42d-2022-%1-ft',
DA_path=r'cross_domain/rat/pred-Rat-42d-2022-%1-DA',
gt_path=r'cross_domain/rat/gt', scans_num=2)
plot_segmentation_cross_species(raw, zeroshot, ft, DA, gt, task='species')
2.0 N4 Bias Field Correction
2.1 Using BEN with original ANTs (without Nipype)
[10]:
pwd
[10]:
'/content/BEN/cross_domain/rat'
[11]:
!N4BiasFieldCorrection -d 3 --input-image ./cross_domain/rat/src/42d_cas_40_T2.nii.gz --output correctedImage.nii.gz
[12]:
ls
correctedImage.nii.gz label/ src/
Feel free to run BEN here or before N4
BEN_infer -i correctedImage.nii.gz -o test.nii.gz -weight path/to/weight
2.1.1 Plot
[30]:
from utils.load_data import get_itk_array
import matplotlib.pyplot as plt
raw = get_itk_array('/content/BEN/cross_domain/rat/src/42d_cas_40_T2.nii.gz')
print('Raw MRI')
plt.imshow(raw[25] ,cmap='gray');plt.show()
correctedImage = get_itk_array('/content/BEN/cross_domain/rat/correctedImage.nii.gz')
print('CorrectedImage')
plt.imshow(correctedImage[25] ,cmap='gray');plt.show()
Raw MRI
CorrectedImage
2.2 Using BEN with ANTs in Nipype
[31]:
from nipype.interfaces.ants import N4BiasFieldCorrection
correct = N4BiasFieldCorrection()
correct.inputs.input_image = '/content/BEN/cross_domain/rat/src/42d_cas_40_T2.nii.gz'
correct.inputs.output_image = '/content/BEN/cross_domain/rat/correctedImage_nipype.nii.gz'
correct.inputs.dimension = 3
done = correct.run()
done.outputs.output_image
''' Feel free to run BEN here or before N4 '''
# from BEN_infer import BEN_inference
# BEN_inference(correctedImage.nii.gz, test.nii.gz, path/to/weight)
[31]:
'/content/BEN/cross_domain/rat/correctedImage_nipype.nii.gz'
2.2.1 Plot
[33]:
raw = get_itk_array('/content/BEN/cross_domain/rat/src/42d_cas_40_T2.nii.gz')
print('Raw MRI')
plt.imshow(raw[25] ,cmap='gray');plt.show()
correctedImage = get_itk_array('/content/BEN/cross_domain/rat/correctedImage_nipype.nii.gz')
print('CorrectedImage_nipype')
plt.imshow(correctedImage[25] ,cmap='gray');plt.show()
Raw MRI
CorrectedImage_nipype
[ ]: