-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_io.py
42 lines (31 loc) · 985 Bytes
/
data_io.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
from __future__ import absolute_import
from __future__ import print_function
import csv
import nibabel as nib
import numpy as np
def write_history(history_dict, output_path):
with open(output_path,'w') as f:
writer = csv.writer(f)
writer.writerow(history_dict.keys())
writer.writerows(zip(*history_dict.values()))
def read_nifti_image(file_path):
img_obj = nib.load(file_path)
image = img_obj.get_fdata()
return image
def read_nifti_header(file_path):
img_obj = nib.load(file_path)
header = img_obj.header
return header
def write_nifti_image(file_path, image, header):
if header:
sform = header.get_sform()
image_obj = nib.Nifti1Image(image, sform)
else:
image_obj = nib.Nifti1Image(image, None)
nib.save(image_obj, file_path)
#def read_csv_label(csv_file):
# with open(csv_file, 'r') as f:
# reader = csv.reader(f)
# label_list = list(reader)
# label_list = [int(x[0]) for x in label_list]
# return np.array(label_list)