a b/src/LiviaNet/Modules/IO/loadData.py
1
""" 
2
Copyright (c) 2016, Jose Dolz .All rights reserved.
3
4
Redistribution and use in source and binary forms, with or without modification,
5
are permitted provided that the following conditions are met:
6
7
    1. Redistributions of source code must retain the above copyright notice,
8
       this list of conditions and the following disclaimer.
9
    2. Redistributions in binary form must reproduce the above copyright notice,
10
       this list of conditions and the following disclaimer in the documentation
11
       and/or other materials provided with the distribution.
12
13
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15
    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17
    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20
    OTHER DEALINGS IN THE SOFTWARE.
21
22
Jose Dolz. Dec, 2016.
23
email: jose.dolz.upv@gmail.com
24
LIVIA Department, ETS, Montreal.
25
"""
26
27
import numpy as np
28
import pdb
29
# If you are not using nifti files you can comment this line
30
import nibabel as nib
31
import scipy.io as sio
32
33
from ImgOperations.imgOp import applyPadding
34
35
# ----- Loader for nifti files ------ #
36
def load_nii (imageFileName, printFileNames) :
37
    if printFileNames == True:
38
        print (" ... Loading file: {}".format(imageFileName))
39
40
    img_proxy = nib.load(imageFileName)
41
    imageData = img_proxy.get_data()
42
    
43
    return (imageData,img_proxy)
44
    
45
def release_nii_proxy(img_proxy) :
46
    img_proxy.uncache()
47
48
49
# ----- Loader for matlab format ------- #
50
# Very important: All the volumes should have been saved as 'vol'.
51
# Otherwise, change its name here
52
def load_matlab (imageFileName, printFileNames) :
53
    if printFileNames == True:
54
        print (" ... Loading file: {}".format(imageFileName))
55
    
56
    mat_contents = sio.loadmat(imageFileName)
57
    imageData = mat_contents['vol']
58
    
59
    return (imageData)
60
    
61
""" It loads the images (CT/MRI + Ground Truth + ROI) for the patient image Idx"""
62
def load_imagesSinglePatient(imageIdx, 
63
                             imageNames, 
64
                             groundTruthNames, 
65
                             roiNames,
66
                             applyPaddingBool,
67
                             receptiveField, 
68
                             sampleSizes,
69
                             imageType
70
                             ):
71
    
72
    if imageIdx >= len(imageNames) :
73
        print (" ERROR!!!!! : The image index specified is greater than images array size....)")
74
        exit(1)
75
    
76
    # --- Load image data (CT/MRI/...) ---
77
    printFileNames = False # Get this from config.ini
78
79
    imageFileName = imageNames[imageIdx]
80
81
    if imageType == 0:
82
        [imageData,img_proxy] = load_nii(imageFileName, printFileNames)
83
    else:
84
        imageData = load_matlab(imageFileName, printFileNames)
85
        
86
    if applyPaddingBool == True : 
87
        [imageData, paddingValues] = applyPadding(imageData, sampleSizes, receptiveField)
88
    else:
89
        paddingValues = ((0,0),(0,0),(0,0))
90
91
92
    if len(imageData.shape) > 3 :
93
         imageData = imageData[:,:,:,0]
94
    
95
    if imageType == 0:
96
        release_nii_proxy(img_proxy)
97
    
98
    # --- Load ground truth (i.e. labels) ---
99
    if len(groundTruthNames) > 0 : 
100
        GTFileName = groundTruthNames[imageIdx]
101
        
102
        if imageType == 0:
103
            [gtLabelsData, gt_proxy] = load_nii (GTFileName, printFileNames)
104
        else:
105
            gtLabelsData = load_matlab(GTFileName, printFileNames)
106
        
107
        # Convert ground truth to int type
108
        if np.issubdtype( gtLabelsData.dtype, np.int ) : 
109
            gtLabelsData = gtLabelsData 
110
        else: 
111
            np.rint(gtLabelsData).astype("int32")
112
        
113
        imageGtLabels = gtLabelsData
114
        
115
        if imageType == 0:
116
            # Release data
117
            release_nii_proxy(gt_proxy)
118
        
119
        if applyPaddingBool == True : 
120
            [imageGtLabels, paddingValues] = applyPadding(imageGtLabels,  sampleSizes, receptiveField)
121
        
122
    else : 
123
        imageGtLabels = np.empty(0)
124
        
125
    # --- Load roi ---
126
    if len(roiNames)> 0 :
127
        roiFileName = roiNames[imageIdx]
128
        
129
        if imageType == 0:
130
            [roiMaskData, roi_proxy] = load_nii (roiFileName, printFileNames)
131
        else:
132
            roiMaskData = load_matlab(roiFileName, printFileNames)
133
            
134
        roiMask = roiMaskData
135
        
136
        if imageType == 0:
137
            # Release data
138
            release_nii_proxy(roi_proxy)
139
        
140
        if applyPaddingBool == True : 
141
            [roiMask, paddingValues] = applyPadding(roiMask, sampleSizes, receptiveField)
142
    else :
143
        roiMask = np.ones(imageGtLabels.shape)
144
145
    return [imageData, imageGtLabels, roiMask, paddingValues]
146
147
148
# -------------------------------------------------------- #
149
def getRandIndexes(total, maxNumberIdx) :
150
    # Generate a shuffle array of a vector containing "total" elements
151
    idxs = range(total)
152
    np.random.shuffle(idxs)
153
    rand_idxs = idxs[0:maxNumberIdx]
154
    return rand_idxs
155