hdf5_generator.py 10.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
""" Database Generator

Description:

    This file contains the required fuctions for generating the databases required for training the network.
    This file is designed to be a standalone package, intendead to be run separately from the main network.

Usage:

    To use content from this folder, import the functions and instantiate them as you wish to use them:

        from utils.hdf5_generator import function_name

"""

import os
import h5py
import numpy as np
Andrei Roibu's avatar
Andrei Roibu committed
19
20
21
22
import data_utils as data_utils
import preprocessor as preprocessor
from settings import Settings
from common_utils import create_folder
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81


def convert_hdf5(data_parameters, file_information):
    # First, we split the data:
    if data_parameters['data_split_flag'] == True:
        print('Data is shuffling... This could take a few minutes!')

        if data_parameters['use_data_file'] == True:
            train_subjects, validation_subjects = preprocessor.data_preparation(data_parameters['data_folder_name'],
                                                                            data_parameters['test_percentage'],
                                                                            data_parameters['subject_number'],
                                                                            data_directory=data_parameters['data_directory'],
                                                                            train_inputs=data_parameters['train_data_file'],
                                                                            train_targets=data_parameters['train_output_targets'],
                                                                            rsfMRI_mean_mask_path=data_parameters['rsfmri_mean_mask_path'],
                                                                            dMRI_mean_mask_path=data_parameters['dmri_mean_mask_path'],
                                                                            data_file=data_parameters['data_file'],
                                                                            )
        else:
            train_subjects, validation_subjects = preprocessor.data_preparation(data_parameters['data_folder_name'],
                                                                            data_parameters['test_percentage'],
                                                                            data_parameters['subject_number'],
                                                                            data_directory=data_parameters['data_directory'],
                                                                            train_inputs=data_parameters['train_data_file'],
                                                                            train_targets=data_parameters['train_output_targets'],
                                                                            rsfMRI_mean_mask_path=data_parameters['rsfmri_mean_mask_path'],
                                                                            dMRI_mean_mask_path=data_parameters['dmri_mean_mask_path'],
                                                                            )
        preprocessor.update_shuffling_flag('utils/hdf5_settings.ini')
        print('Data shuffling... Complete!')

    elif data_parameters['train_test_file_read_flag'] == True:

        # Read the subjects from the files!

        train_subjects =  data_utils.load_subjects_from_path(data_directory=data_parameters['data_directory'],
                                                            data_list=data_parameters['train_list'])
        validation_subjects = data_utils.load_subjects_from_path(data_directory=data_parameters['data_directory'],
                                                                data_list=data_parameters['validation_list'])
    
    else:
        raise ValueError('Either a split flag, or a read-from-file flag must be provided as True')

    # Then, we have to read the various test and train data, process them and write them to H5
    # First, let's do this for the training data

    print('-> Processing training data:')

    train_dMRI, train_rsfMRI = preprocessor.load_datasets(subjects = train_subjects, 
                                                        data_directory = data_parameters['data_directory'], 
                                                        input_file = data_parameters['train_data_file'], 
                                                        output_target = data_parameters['train_output_targets'], 
                                                        mean_regression_flag = data_parameters['mean_regression_flag'], 
                                                        mean_regression_all_flag = data_parameters['mean_regression_all_flag'], 
                                                        regression_weights_path = data_parameters['regression_weights_path'],
                                                        dMRI_mean_mask_path = data_parameters['dmri_mean_mask_path'], 
                                                        rsfMRI_mean_mask_path = data_parameters['rsfmri_mean_mask_path'], 
                                                        mean_subtraction_flag = data_parameters['mean_subtraction_flag'], 
                                                        scale_volumes_flag = data_parameters['scale_volumes_flag'], 
Andrei Roibu's avatar
Andrei Roibu committed
82
83
                                                        normalize_flag = data_parameters['normalize_flag'],
                                                        minus_one_scaling_flag = data_parameters['minus_one_scaling_flag'],
84
85
86
                                                        negative_flag = data_parameters['negative_flag'],
                                                        outlier_flag = data_parameters['outlier_flag'], 
                                                        shrinkage_flag = data_parameters['shrinkage_flag'], 
87
88
                                                        hard_shrinkage_flag = data_parameters['hard_shrinkage_flag'],
                                                        crop_flag = data_parameters['crop_flag']
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
                                                        )

    write_hdf5(train_dMRI, train_rsfMRI, file_information, mode='train')                                                                                                    

    # Then, we'll do it for the validation data

    print('-> Processing validation data:')

    validation_dMRI, validation_rsfMRI = preprocessor.load_datasets(subjects = validation_subjects, 
                                                                    data_directory = data_parameters['data_directory'], 
                                                                    input_file = data_parameters['train_data_file'], 
                                                                    output_target = data_parameters['train_output_targets'], 
                                                                    mean_regression_flag = data_parameters['mean_regression_flag'], 
                                                                    mean_regression_all_flag = data_parameters['mean_regression_all_flag'], 
                                                                    regression_weights_path = data_parameters['regression_weights_path'],
                                                                    dMRI_mean_mask_path = data_parameters['dmri_mean_mask_path'], 
                                                                    rsfMRI_mean_mask_path = data_parameters['rsfmri_mean_mask_path'], 
                                                                    mean_subtraction_flag = data_parameters['mean_subtraction_flag'], 
                                                                    scale_volumes_flag = data_parameters['scale_volumes_flag'], 
                                                                    normalize_flag = data_parameters['normalize_flag'], 
Andrei Roibu's avatar
Andrei Roibu committed
109
                                                                    minus_one_scaling_flag = data_parameters['minus_one_scaling_flag'],
110
111
112
                                                                    negative_flag = data_parameters['negative_flag'],
                                                                    outlier_flag = data_parameters['outlier_flag'], 
                                                                    shrinkage_flag = data_parameters['shrinkage_flag'], 
113
114
                                                                    hard_shrinkage_flag = data_parameters['hard_shrinkage_flag'],
                                                                    crop_flag = data_parameters['crop_flag']
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
                                                                    )

    write_hdf5(validation_dMRI, validation_rsfMRI, file_information, mode='validation')


def write_hdf5(input_volumes, target_volumes, file_information, mode):
    """ HDF5 Writer

    Function which writes the hdf5 files.

    Args:
        input_volumes (list): List of all the input volumes.
        target_volumes (list) List of all the target volumes.
        file_information (dict): Dictionary containing the outputs paths for the various databases
        mode (str): String indicating the type of data observed
    """

Andrei Roibu's avatar
Andrei Roibu committed
132
133
134
135
136
137
    if os.path.exists(file_information[mode]['input']):
        os.remove(file_information[mode]['input'])

    if os.path.exists(file_information[mode]['target']):
        os.remove(file_information[mode]['target'])

138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
    with h5py.File(file_information[mode]['input'], 'w') as data_handle:
        data_handle.create_dataset('input', data=input_volumes)
    
    with h5py.File(file_information[mode]['target'], 'w') as data_handle:
        data_handle.create_dataset('target', data=target_volumes)


if __name__ == "__main__":

    print('Started Data Generation!')

    settings = Settings('utils/hdf5_settings.ini')
    data_parameters = settings['DATA']
    create_folder(data_parameters['data_folder_name'])

    file_information = {
Andrei Roibu's avatar
Andrei Roibu committed
154
155
                        'train': {"input" : os.path.join(data_parameters['data_folder_name'], data_parameters['input_data_train']),
                                  "target" : os.path.join(data_parameters['data_folder_name'], data_parameters['target_data_train']),
156
                                  },
Andrei Roibu's avatar
Andrei Roibu committed
157
158
                        'validation': {"input" : os.path.join(data_parameters['data_folder_name'], data_parameters['input_data_validation']),
                                       "target" : os.path.join(data_parameters['data_folder_name'], data_parameters['target_data_validation']),
159
160
161
162
163
164
165
                                      }
                        }

    convert_hdf5(data_parameters, file_information)

    print('Completed Data Generation!')