group_mean.py 3.23 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
"""Group Mean Calculator

Description:

    This file contains the relevant scripts for calculating the group mean for the subjects present in the UK Biobank.
    This is a standalone scrip, intended to be used only once during the project. Hence, it is not integrated into the larger utils packages.

Usage:

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

        from utils.group_mean import function_name

"""

import os
import numpy as np
from fsl.data.image import Image
from fsl.utils.image.resample import resampleToPixdims
Andrei Roibu's avatar
Andrei Roibu committed
20
from utils.preprocessor import directory_reader
21
22
23
24
25
26
27
28
29
30
31
32


def mean_calculator(data_directory, mean_type='dMRI'):
    """ Population mean calculator

    This function calculates the mean across all subjects for a given data type.

    Args:
        data_directory (str): A string containing the address of the required directory.
        mean_type (str): String flag indicating which data type the algorithm should compute the mean for.

    """
Andrei Roibu's avatar
Andrei Roibu committed
33
34
    subDirectoryList, number_of_subjects = directory_reader(
        os.path.join(os.path.expanduser("~"), data_directory))
35

Andrei Roibu's avatar
Andrei Roibu committed
36
37
    subject_number = len(os.listdir(os.path.join(
        os.path.expanduser("~"), data_directory)))
38
39
40
41
42
43
44
45
46
47
48
49
50

    volume_sum = None
    counter = None
    header = None

    for directory in subDirectoryList:
        if mean_type == 'dMRI':
            data_path = "dMRI/autoptx_preproc/tractsNormSummed.nii.gz"
        elif mean_type == 'fMRI':
            data_path = "fMRI/rfMRI_25.dr/dr_stage2.nii.gz"
        else:
            raise ValueError('mean_type can be either dMRI or fMRI!')

Andrei Roibu's avatar
Andrei Roibu committed
51
52
        subject_path = os.path.join(os.path.expanduser(
            "~"), data_directory, directory, data_path)
53
54
55
        if mean_type == 'dMRI':
            volume = Image(subject_path).data
        else:
Andrei Roibu's avatar
Andrei Roibu committed
56
            volume = Image(subject_path).data[:, :, :, 0]
57
58
59
60
61
62
63
64
65
66
67
68
69

        if volume_sum is None:
            volume_sum = np.zeros(volume.shape)

        if counter is None:
            counter = 0

        if header is None:
            header = Image(subject_path).header

        volume_sum = np.add(volume_sum, volume)
        counter += 1

Andrei Roibu's avatar
Andrei Roibu committed
70
71
        print("Added volume {}/{}, --{}%".format(counter,
                                                 subject_number, counter/subject_number*100))
72
73
74
75
76
77
78

    volume_mean = np.divide(volume_sum, counter)

    volume_mean_image = Image(volume_mean, header=header)

    if mean_type == 'dMRI':
        volume_mean_image.save('utils/mean_tractsNormSummed.nii.gz')
Andrei Roibu's avatar
Andrei Roibu committed
79
80
81
82
83
84
85
86
        volume_mean_donwsampled, xform = resampleToPixdims(
            Image('utils/mean_tractsNormSummed.nii.gz'), (2, 2, 2))
        header_downsampled = Image(volume_mean_donwsampled, header=Image(
            'utils/mean_tractsNormSummed.nii.gz').header, xform=xform).header
        volume_mean_downsampled_image = Image(
            volume_mean_donwsampled, header=header_downsampled)
        volume_mean_downsampled_image.save(
            'utils/mean_tractsNormSummed_downsampled.nii.gz')
87
88
89
90
91
92
93
94
95
    else:
        volume_mean_image.save('utils/mean_dr_stage2.nii.gz')


if __name__ == '__main__':
    data_directory = "/well/win-biobank/projects/imaging/data/data3/subjectsAll/"
    mean_type = 'dMRI'
    # mean_type = 'fMRI'

Andrei Roibu's avatar
Andrei Roibu committed
96
    mean_calculator(data_directory, mean_type)