-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-simulation-normalize-data.py
65 lines (45 loc) · 1.6 KB
/
3-simulation-normalize-data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import numpy as np
import torch
import os
import sbi
import sbi.utils
from sbi.utils.user_input_checks_utils import MultipleIndependent
from sbi.utils import BoxUniform
from sbi.inference import prepare_for_sbi, simulate_for_sbi, SNPE, SNLE, SNRE
from sbi.analysis import pairplot
import nibabel as nb
def get_data(file, mmap=True):
"""
Load NIfTI image data from a file.
Parameters:
file (str): The path to the NIfTI file.
mmap (bool, optional): Whether to use memory-mapped file access. Default is True.
Returns:
numpy.ndarray: The voxel data from the NIfTI file.
"""
import nibabel as nb
img = nb.load(file, mmap=mmap)
img_voxels = img.get_fdata()
return img_voxels
def export_nifti(data, orig_data, output_path, name):
"""
Args:
data:
orig_data:
output_path:
name:
"""
import nibabel as nb
import os
# Copy the header of the original image
aff_mat = orig_data.affine
nb.save(nb.Nifti2Image(data, affine=aff_mat), os.path.join(output_path, name))
#Load files
dPath = '/Simulations/noise-free-data'
data = get_data(f'{dPath}/test-noisy_data_snr10_b0b1b2k.nii.gz')
#b0s are the first 5 elements
ax_signal = len(data.shape) - 1
mean_nob0vols = np.mean(data[..., :5], axis=ax_signal)
data_norm = data / np.expand_dims(mean_nob0vols, axis=ax_signal)
orig_data = nb.load(f'{dPath}/test-noisy_data_snr10_b0b1b2k.nii.gz', mmap=True) # This is to capture the correct header of the nifti image when exporting
export_nifti(data_norm, orig_data, dPath, 'test-noisy_data_snr10_b0b1b2k_normalized.nii.gz')