<jupyter_start><jupyter_text><jupyter_code>from google.colab import drive
drive.mount('/content/drive')
#!ls /content/drive/My\ Drive/Capstone/05_Data/
#!cd /content/drive/My\ Drive/Capstone/05_Data/02_Sample_MRI/ && unzip sample.zip
import nibabel as nib
import numpy as np
import glob

import os
from tqdm.auto import tqdm


#!pip install nilearn
#import nilearn
#!python -m pip install nilearn
# Restart kernel runtime if the package doesn't load
from nilearn import plotting

import matplotlib.pyplot as plt
%matplotlib inline
path = '/content/drive/My Drive/Capstone/05_Data/02_Sample_MRI/sample/'
all_files = glob.glob(path + '*.gz')
all_files = sorted(all_files)
#all_files
#plotting.plot_glass_brain(all_files[10])

# Plotting brain images
plotting.plot_epi(all_files[8])
plotting.show()

plotting.plot_anat(all_files[8])
plotting.show()


plotting.plot_anat(all_files[4])
plotting.show()
plotting.plot_anat(all_files[5])
plotting.show()


plotting.plot_img(all_files[4])
plotting.show()
plotting.plot_img(all_files[5])
plotting.show()
# Intereactive plotting - Too slow. Maybe better implementations available
#plotting.view_img_on_surf(all_files[4], threshold='90%', surf_mesh='fsaverage')

import nilearn
print(all_files[4])
plotting.plot_anat(all_files[4])
plotting.show()

print(all_files[11])
plotting.plot_anat(all_files[11])
plotting.show()

print(all_files[18])
plotting.plot_anat(all_files[18])
plotting.show()

print("Comparison of diffrent Sampling Techniques \n")

print(all_files[5])

org = nib.load(all_files[5])
re_nearest = nilearn.image.resample_img(org, target_affine=np.eye(3)*4.1, interpolation='nearest')
re_linear = nilearn.image.resample_img(org, target_affine=np.eye(3)*4.1, interpolation='linear')
re_continuous = nilearn.image.resample_img(org, target_affine=np.eye(3)*4.1, interpolation='continuous')

print("Orignal Shape:\t {} \nResampled Shape: {}".format(org.shape,re_nearest.shape))

plotting.plot_anat(org)
plotting.show()

print("linear")
plotting.plot_anat(re_linear)
plotting.show()

print("nearest")
plotting.plot_anat(re_nearest)
plotting.show()

print("continuous")
plotting.plot_anat(re_continuous)
plotting.show()

# Subsetting only T1 and T2 files
all_t1_files = [i for i in all_files if i.split('/')[-1].split('.')[-3].split('_')[-1] == 'T1']
all_t2_files = [i for i in all_files if i.split('/')[-1].split('.')[-3].split('_')[-1] == 'T2']

display(all_t1_files[:2])
display(all_t2_files[:2])
def resample(file_list, path_to_save):
  """
    Function to resample images to 64x64x64 and save it in the path
    
  """
  
  # Checking if the directory doesn't exist then make one
  if not os.path.exists(path_to_save):
    os.makedirs(path_to_save)
  
  for file in tqdm(file_list):

    org = nib.load(file)
    re = nilearn.image.resample_img(org, target_affine=np.eye(3)*4.1, interpolation='continuous')

    # If the scaling dimensions are slightly off - to compensate 
    if re.shape == (63,63,63):
      re = nilearn.image.resample_img(org, target_affine=np.eye(3)*4., interpolation='continuous')
    elif re.shape == (65,65,65):
      re = nilearn.image.resample_img(org, target_affine=np.eye(3)*4.15, interpolation='continuous')

    # Asserting once again the shape is right
    #display(file, org.shape, re.shape)
    assert org.shape==(256,256,256)
    assert re.shape==(64,64,64)

    file_new_path = path_to_save + file.split('/')[-1]

    nib.save(img=re,filename=file_new_path)
    
    
# For T1
path_to_save = "/content/drive/My Drive/Capstone/05_Data/02_Sample_MRI/subsample/t1/"
resample(file_list=all_t1_files, path_to_save=path_to_save)

# For T2
path_to_save = "/content/drive/My Drive/Capstone/05_Data/02_Sample_MRI/subsample/t2/"
resample(file_list=all_t2_files, path_to_save=path_to_save)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1439493804376483, 'fraction_numerical': 0.03031900870023728, 'mean_word_length': 5.715044247787611, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '335721', 'license': 'MIT', 'n_tokens_mistral': 1523, 'n_tokens_neox': 1440, 'n_words': 260, 'path': 'Down_Sampling.ipynb', 'repo_name': 'BNLCSISMR/ABCD-MRI', 'size': '3793'}