# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import nibabel as nib
import numpy as np

data = nib.load("fa_S11.nii.gz")

print(data) #Generate image and metadata

image_data = data.get_fdata()


image_data.shape


import matplotlib.pyplot as plt
plt.imshow(image_data[:,:,64], cmap="gray")  #Sample visualization

plt.imshow(image_data[:,:,56], cmap="gray")


plt.imshow(image_data[20,:,:], cmap="gray") #Axial visualization


arr = []

arr += [image_data]

len(arr)

for i in range(9,10) :
    data = nib.load(f"fa_S{i}.nii.gz")
    img_data = data.get_fdata()
    arr += [img_data]

len(arr)

arr_out = np.vstack(np.expand_dims(arr,0))  #Create a batch of np tensors

arr_out.shape

arr_out_b = np.expand_dims(arr_out , -1) #Get the extra dimension for color

arr_out_b.shape

import torch

x = torch.from_numpy(arr_out_b).float().view(-1,1,96,114,94) #Need to resize to BxCxHxW format

x.size()

torch.save(x , 'trial_data.pt') #Save torch tensor



StarcoderdataJupyterScriptsDedupFiltered