|
|
from __future__ import generators |
|
|
|
|
|
import logging |
|
|
import glob, os, functools |
|
|
import sys |
|
|
sys.path.append('../') |
|
|
|
|
|
|
|
|
import SimpleITK as sitk |
|
|
import numpy as np |
|
|
import scipy |
|
|
import nibabel as nib |
|
|
import skimage |
|
|
import matplotlib.pyplot as plt |
|
|
import scipy.misc |
|
|
from scipy import ndimage |
|
|
from skimage.transform import resize,rescale |
|
|
import cv2 |
|
|
import itk |
|
|
import subprocess |
|
|
from tqdm import tqdm |
|
|
import pandas as pd |
|
|
import warnings |
|
|
import statistics |
|
|
import torch |
|
|
import csv |
|
|
import os |
|
|
import yaml |
|
|
|
|
|
from HD_BET.run import run_hd_bet |
|
|
from dataset.preprocess_utils import enhance, enhance_noN4 |
|
|
from dataset.preprocess_datasets_T1_to_2d import create_quantile_from_brain |
|
|
|
|
|
warnings.filterwarnings('ignore') |
|
|
cuda_device = '1' |
|
|
os.environ['CUDA_VISIBLE_DEVICES'] = cuda_device |
|
|
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") |
|
|
|
|
|
|
|
|
|
|
|
def select_template_based_on_age(age): |
|
|
for golden_file_path, age_values in age_ranges.items(): |
|
|
if age_values['min_age'] <= int(age) and int(age) <= age_values['max_age']: |
|
|
print(golden_file_path) |
|
|
return golden_file_path |
|
|
|
|
|
def register_to_template(input_image_path, output_path, fixed_image_path,rename_id,create_subfolder=True): |
|
|
fixed_image = itk.imread(fixed_image_path, itk.F) |
|
|
|
|
|
|
|
|
parameter_object = itk.ParameterObject.New() |
|
|
parameter_object.AddParameterFile('/media/sdb/divyanshu/divyanshu/aidan_segmentation/pediatric-brain-age-main/dataset/golden_image/mni_templates/Parameters_Rigid.txt') |
|
|
|
|
|
if "nii" in input_image_path and "._" not in input_image_path: |
|
|
print(input_image_path) |
|
|
|
|
|
|
|
|
try: |
|
|
moving_image = itk.imread(input_image_path, itk.F) |
|
|
result_image, result_transform_parameters = itk.elastix_registration_method( |
|
|
fixed_image, moving_image, |
|
|
parameter_object=parameter_object, |
|
|
log_to_console=False) |
|
|
image_id = input_image_path.split("/")[-1] |
|
|
|
|
|
itk.imwrite(result_image, output_path+"/"+rename_id+".nii.gz") |
|
|
|
|
|
print("Registered ", rename_id) |
|
|
except: |
|
|
print("Cannot transform", rename_id) |
|
|
|
|
|
def outlier_voting(numbers): |
|
|
mean = statistics.mean(numbers) |
|
|
stdev = statistics.stdev(numbers) |
|
|
|
|
|
threshold = stdev |
|
|
|
|
|
good_nums_avg =[] |
|
|
for n in numbers: |
|
|
if n > mean + threshold or n < mean - threshold: |
|
|
continue |
|
|
else: |
|
|
good_nums_avg.append(n) |
|
|
|
|
|
|
|
|
|
|
|
return np.average(good_nums_avg) |
|
|
|
|
|
|
|
|
|
|
|
img_path = '/media/sdb/divyanshu/divyanshu/aidan_segmentation/dummy_t1_preprocess/OAS2_0001_MR1.nii.gz' |
|
|
data_path = "/media/sdb/divyanshu/divyanshu/longitudinal_fm/datasets/abide/data" |
|
|
gt_age = 86 |
|
|
gender = "M" |
|
|
path_to = "/media/sdb/divyanshu/divyanshu/longitudinal_fm/datasets/abide/preprocessed_data" |
|
|
|
|
|
|
|
|
age_ranges = {"/media/data/BrainIAC/src/BrainIAC/golden_image/mni_templates/nihpd_asym_04.5-08.5_t1w.nii" : {"min_age":3, "max_age":7}, |
|
|
"/media/data/BrainIAC/src/BrainIAC/golden_image/mni_templates/nihpd_asym_07.5-13.5_t1w.nii": {"min_age":8, "max_age":13}, |
|
|
"/media/data/BrainIAC/src/BrainIAC/golden_image/mni_templates/nihpd_asym_13.0-18.5_t1w.nii": {"min_age":14, "max_age":100}} |
|
|
|
|
|
|
|
|
for eachimage in tqdm(os.listdir(data_path), desc="Processing images", unit="image"): |
|
|
if 1: |
|
|
|
|
|
img_path = os.path.join(data_path, eachimage) |
|
|
nii= nib.load(img_path) |
|
|
image, affine = nii.get_fdata(), nii.affine |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
new_path_to = path_to |
|
|
if eachimage in os.listdir(new_path_to): |
|
|
print("yay") |
|
|
else: |
|
|
if not os.path.exists(path_to): |
|
|
os.mkdir(path_to) |
|
|
if not os.path.exists(new_path_to): |
|
|
os.mkdir(new_path_to) |
|
|
|
|
|
|
|
|
golden_file_path = select_template_based_on_age(gt_age) |
|
|
print("Registering to template:", golden_file_path) |
|
|
|
|
|
register_to_template(img_path, new_path_to, golden_file_path,eachimage.split(".")[0]+"_"+"registered.nii.gz", create_subfolder=False) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
image_sitk = sitk.ReadImage(os.path.join(new_path_to, eachimage.split(".")[0]+"_"+"registered.nii.gz")) |
|
|
image_array = sitk.GetArrayFromImage(image_sitk) |
|
|
image_array = enhance(image_array) |
|
|
image3 = sitk.GetImageFromArray(image_array) |
|
|
sitk.WriteImage(image3,os.path.join(new_path_to, eachimage.split(".")[0]+"_"+"registered_no_z.nii.gz")) |
|
|
|
|
|
|
|
|
new_path_to = path_to |
|
|
run_hd_bet(os.path.join(new_path_to, eachimage.split(".")[0]+"_"+"registered_no_z.nii.gz"),os.path.join(new_path_to, eachimage), |
|
|
mode="accurate", |
|
|
config_file='/media/sdb/divyanshu/divyanshu/aidan_segmentation/pediatric-brain-age-main/HD_BET/config.py', |
|
|
device=device, |
|
|
postprocess=False, |
|
|
do_tta=True, |
|
|
keep_mask=True, |
|
|
overwrite=True) |
|
|
|
|
|
|