a b/libs/datasets/gen_acdcjson.py
1
import os
2
import nibabel as nib
3
import numpy as np
4
import json
5
6
'''
7
DCM,HCM,MINF,NOR,RV
8
'''
9
10
def load_nii(nii_path):
11
    data = nib.load(nii_path)
12
    img = data.get_data()
13
    affine = data.affine
14
    header = data.header
15
    return img ,affine,header
16
17
18
def read_Infocfg(cfg_path):
19
    patient_info = {}
20
21
22
23
24
    with open (cfg_path) as f_in:
25
        for line in f_in:
26
            l = line.rstrip().split(":")
27
            #l is the list of the patient_info
28
            print(len(l))
29
            patient_info[l[0]] = l[1]
30
        # print(patient_info)
31
        '''
32
        ['ED', ' 1']
33
        ['ES', ' 12']
34
        ['Group', ' DCM']
35
        ['Height', ' 184.0']
36
        ['NbFrame', ' 30']
37
        ['Weight', ' 95.0']
38
        {'ED': ' 1', 'ES': ' 12', 'Group': ' DCM', 'Height': ' 184.0', 'NbFrame': ' 30', 'Weight': ' 95.0'}
39
        '''
40
    return patient_info
41
42
def read_json(fpath):
43
    with open(fpath,'r') as f:
44
        obj = json.load(f)
45
    return obj
46
47
def write_json(obj, fpath):
48
    with open(fpath,'w') as f:
49
        json.dump(obj,f,indent=4)
50
51
def write_json_append(obj, fpath):
52
    with open(fpath,'a') as f:
53
        json.dump(obj,f,indent=4)
54
55
def gen_alldatalist(path):
56
    filelist = []
57
    for dir in os.listdir(path):
58
59
        for file in os.listdir(os.path.join(root_path,dir)):
60
            filelist.append(os.path.join(root_path,dir,file))
61
    #the length of the filelist is 1902
62
    # print(len(filelist))
63
    filelist.sort()
64
    out_dir = os.path.dirname(os.path.abspath(__file__))
65
    #/home/ffbian/chencheng/XieheCardiac/2DUNet/UNet/libs/datasets
66
    write_json(filelist, os.path.join(out_dir, "./acdcjson/ACDCDataList.json"))
67
68
def gen_every_kind_datalist(kind_path):
69
    filelist = []
70
71
    for file in os.listdir(kind_path):
72
        filelist.append(os.path.join(kind_path,file))
73
74
    filelist.sort()
75
    out_dir = os.path.dirname(os.path.abspath(__file__))
76
    #/home/ffbian/chencheng/XieheCardiac/2DUNet/UNet/libs/datasets
77
    write_json(filelist, os.path.join(out_dir, "./acdcjson/{}DataList.json".format(kind_path[-2:])))
78
79
def generate_train_test_list(json_file_path):
80
    # json_file = "/home/fcheng/Cardia/DataList.json"
81
    # json_file = "/home/ffbian/chencheng/XieheCardiac/2DUNet/UNet/libs/datasets/ACDCDataList.json"
82
    fileslist = read_json(json_file_path)
83
84
    nums = len(fileslist)
85
    train_ind = set(np.random.choice(nums, size=int(np.ceil(0.8 * nums)), replace=False))
86
    test_ind = set(np.arange(nums)) - train_ind
87
88
    test_ind = list(test_ind)
89
    test_ind.sort()
90
91
    train_list = [fileslist[fl] for fl in train_ind]
92
    test_list = [fileslist[fl] for fl in test_ind]
93
94
    out_dir = os.path.dirname(os.path.abspath(__file__))
95
    print(out_dir)
96
    write_json(train_list, os.path.join(out_dir, "./acdcjson/RVtrain.json"))
97
    write_json(test_list, os.path.join(out_dir, "./acdcjson/RVtest.json"))
98
    write_json_append(train_list, os.path.join(out_dir, "./acdcjson/train.json"))
99
    write_json_append(test_list, os.path.join(out_dir, "./acdcjson/test.json"))
100
    # write_json(test_list, "/home/ffbian/chencheng/XieheCardiac/2DUNet/UNet/libs/datasets/differentkind/kuodatest.json")
101
102
103
if __name__ == "__main__":
104
    # read_Infocfg(os.path.join(root_path,"./DCM/patient001/Info.cfg"))
105
    '''
106
    a,b,c = load_nii(os.path.join(root_path,"./patient001/patient001_frame01.nii.gz"))
107
    print(np.unique(a))
108
109
    print(c)
110
    '''
111
    root_path = "/home/ffbian/chencheng/MICCAIACDC2017/processed_acdc_dataset/hdf5_files/Bykind"
112
    kind_path = "/home/ffbian/chencheng/MICCAIACDC2017/processed_acdc_dataset/hdf5_files/Bykind/RV"
113
114
    # gen_alldatalist(root_path)
115
    json_file_path = "/home/ffbian/chencheng/MICCAIACDC2017/mycode/libs/dataset/acdcjson/RVDataList.json"
116
117
    # generate_train_test_list(json_file_path)
118
    # gen_every_kind_datalist(kind_path)
119
    result = read_json(json_file_path)
120
    print(result)