Spaces:
Sleeping
Sleeping
File size: 12,780 Bytes
377b913 | 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 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | #!/usr/bin/env python
# -*- coding: utf-8 -*-
# File: dataReader.py
# Author: Amir Alansary <amiralansary@gmail.com>
import SimpleITK as sitk
import numpy as np
import warnings
warnings.simplefilter("ignore", category=ResourceWarning)
__all__ = [
'filesListBrainMRLandmark',
'filesListCardioLandmark',
'filesListFetalUSLandmark',
'NiftiImage']
def getLandmarksFromTXTFile(file, split=','):
"""
Extract each landmark point line by line from a text file, and return
vector containing all landmarks.
"""
with open(file) as fp:
landmarks = []
for i, line in enumerate(fp):
landmarks.append([float(k) for k in line.split(split)])
landmarks = np.asarray(landmarks).reshape((-1, 3))
return landmarks
def getLandmarksFromVTKFile(file):
"""
Extract each landmark point line by line from a VTK file, and return vector
containing all landmarks.
For cardiac data landmark indexes:
0-2 RV insert points
1 -> RV lateral wall turning point
3 -> LV lateral wall mid-point
4 -> apex
5-> center of the mitral valve
"""
with open(file) as fp:
landmarks = []
for i, line in enumerate(fp):
if i == 5:
landmarks.append([float(k) for k in line.split()])
elif i == 6:
landmarks.append([float(k) for k in line.split()])
elif i > 6:
landmarks = np.asarray(landmarks).reshape((-1, 3))
# correct landmark according to image direction
landmarks[:, [0, 1]] = -landmarks[:, [0, 1]]
return landmarks
###############################################################################
class filesListBrainMRLandmark(object):
""" A class for managing train files for mri brain data
Attributes:
files_list: Two or one text files that contain a list of all images and
(landmarks)
returnLandmarks: Return landmarks if task is train or eval
(default: True)
"""
def __init__(self, files_list=None, returnLandmarks=True, agents=1):
# check if files_list exists
assert files_list, 'There is no file given'
# read image filenames
self.image_files = [line.split('\n')[0]
for line in open(files_list[0].name)]
# read landmark filenames if task is train or eval
self.returnLandmarks = returnLandmarks
self.agents = agents
if self.returnLandmarks:
self.landmark_files = [
line.split('\n')[0] for line in open(
files_list[1].name)]
assert len(
self.image_files) == len(
self.landmark_files), """number of image files is not equal to
number of landmark files"""
@property
def num_files(self):
return len(self.image_files)
def sample_circular(self, landmark_ids, shuffle=False):
""" return a random sampled ImageRecord from the list of files
"""
if shuffle:
# TODO: could use PyTorch shuffles
# indexes = rng.choice(x, len(x), replace=False)
pass
else:
indexes = np.arange(self.num_files)
while True:
for idx in indexes:
sitk_image, image = NiftiImage().decode(self.image_files[idx])
if self.returnLandmarks:
# transform landmarks to image space if they are in
# physical space
landmark_file = self.landmark_files[idx]
all_landmarks = getLandmarksFromTXTFile(landmark_file)
# landmark = all_landmarks[14]
# landmark index is 13 for ac-point and 14 pc-point
# transform landmark from physical to image space if
# required
# landmarks = sitk_image.
# TransformPhysicalPointToContinuousIndex(landmark)
landmarks = [np.round(all_landmarks[landmark_ids[i] % 15])
for i in range(self.agents)]
else:
landmarks = None
# extract filename from path, remove .nii.gz extension
image_filenames = [self.image_files[idx][:-7]] * self.agents
images = [image] * self.agents
yield (images, landmarks, image_filenames,
sitk_image.GetSpacing())
###############################################################################
class filesListCardioLandmark(object):
""" A class for managing train files for mri cardiac data
Attributes:
files_list: Two or one text files that contain a list of all images and
(landmarks)
returnLandmarks: Return landmarks if task is train or eval
(default: True)
"""
def __init__(self, files_list=None, returnLandmarks=True, agents=1):
# check if files_list exists
assert files_list, 'There is no file given'
# read image filenames
self.image_files = [line.split('\n')[0]
for line in open(files_list[0].name)]
# read landmark filenames if task is train or eval
self.returnLandmarks = returnLandmarks
self.agents = agents
if self.returnLandmarks:
self.landmark_files = [
line.split('\n')[0] for line in open(
files_list[1].name)]
assert len(
self.image_files) == len(
self.landmark_files), """number of image files is not equal to
number of landmark files"""
@property
def num_files(self):
return len(self.image_files)
def sample_circular(self, landmark_ids, shuffle=False):
""" return a random sampled ImageRecord from the list of files
"""
if shuffle:
# indexes = rng.choice(x, len(x), replace=False)
pass
else:
indexes = np.arange(self.num_files)
while True:
for idx in indexes:
sitk_image, image = NiftiImage().decode(self.image_files[idx])
if self.returnLandmarks:
landmark_file = self.landmark_files[idx]
all_landmarks = getLandmarksFromVTKFile(landmark_file)
# transform landmarks to image coordinates
all_landmarks = [
sitk_image.TransformPhysicalPointToContinuousIndex(
point) for point in all_landmarks]
# Indexes: 0-2 RV insert points
# 1 -> RV lateral wall turning point
# 3 -> LV lateral wall mid-point,
# 4 -> apex, 5-> center of the mitral valve
landmarks = [np.round(all_landmarks[landmark_ids[i] % 6])
for i in range(self.agents)] # Apex + MV
# landmarks = [np.round(all_landmarks[(i + 3) % 6])
# for i in range(self.agents)] # LV + Apex
# landmarks = [np.round(all_landmarks[((i + 1) + 3) % 6])
# for i in range(self.agents)] # LV + MV
else:
landmarks = None
# extract filename from path, remove .nii.gz extension
image_filenames = [self.image_files[idx][:-7]] * self.agents
images = [image] * self.agents
yield (images, landmarks, image_filenames,
sitk_image.GetSpacing())
###############################################################################
class filesListFetalUSLandmark(object):
""" A class for managing train files for fetal ultrasound data
Attributes:
files_list: Two or one text files that contain a list of all images and
(landmarks)
returnLandmarks: Return landmarks if task is train or eval
(default: True)
"""
def __init__(self, files_list=None, returnLandmarks=True, agents=1):
# check if files_list exists
assert files_list, 'There is no file given'
# read image filenames
self.image_files = [line.split('\n')[0]
for line in open(files_list[0].name)]
# read landmark filenames if task is train or eval
self.returnLandmarks = returnLandmarks
self.agents = agents
if self.returnLandmarks:
self.landmark_files = [
line.split('\n')[0] for line in open(
files_list[1].name)]
assert len(
self.image_files) == len(
self.landmark_files), """number of image files is not equal to
number of landmark files"""
@property
def num_files(self):
return len(self.image_files)
def sample_circular(self, landmark_ids, shuffle=False):
""" return a random sampled ImageRecord from the list of files
"""
if shuffle:
# indexes = rng.choice(x, len(x), replace=False)
pass
else:
indexes = np.arange(self.num_files)
while True:
for idx in indexes:
sitk_image, image = NiftiImage().decode(self.image_files[idx])
if self.returnLandmarks:
landmark_file = self.landmark_files[idx]
all_landmarks = getLandmarksFromTXTFile(
landmark_file, split=' ')
# landmark point 12 csp
# 11 leftCerebellar
# 10 rightCerebellar
landmarks = [np.round(all_landmarks[landmark_ids[i] % 13])
for i in range(self.agents)] # Apex + MV
else:
landmarks = None
# extract filename from path, remove .nii.gz extension
image_filenames = [self.image_files[idx][:-7]] * self.agents
images = [image] * self.agents
yield (images, landmarks, image_filenames,
sitk_image.GetSpacing())
###############################################################################
class ImageRecord(object):
'''image object to contain height,width, depth and name '''
pass
class NiftiImage(object):
"""Helper class that provides TensorFlow image coding utilities."""
def __init__(self):
pass
def _is_nifti(self, filename):
"""Determine if a file contains a nifti format image.
Args
filename: string, path of the image file
Returns
boolean indicating if the image is a nifti
"""
extensions = ['.nii', '.nii.gz', '.img', '.hdr']
return any(i in filename for i in extensions)
def decode(self, filename, label=False):
""" decode a single nifti image
Args
filename: string for input images
label: True if nifti image is label
Returns
image: an image container with attributes; name, data, dims
"""
image = ImageRecord()
image.name = filename
assert self._is_nifti(
image.name), "unknown image format for %r" % image.name
if label:
sitk_image = sitk.ReadImage(image.name, sitk.sitkInt8)
else:
sitk_image = sitk.ReadImage(image.name, sitk.sitkFloat32)
np_image = sitk.GetArrayFromImage(sitk_image)
# threshold image between p10 and p98 then re-scale [0-255]
p0 = np_image.min().astype('float')
p10 = np.percentile(np_image, 10)
p99 = np.percentile(np_image, 99)
p100 = np_image.max().astype('float')
sitk_image = sitk.Threshold(sitk_image,
lower=p10,
upper=p100,
outsideValue=p10)
sitk_image = sitk.Threshold(sitk_image,
lower=p0,
upper=p99,
outsideValue=p99)
sitk_image = sitk.RescaleIntensity(sitk_image,
outputMinimum=0,
outputMaximum=255)
# Convert from [depth, width, height] to [width, height, depth]
image.data = sitk.GetArrayFromImage(
sitk_image).transpose(2, 1, 0) # .astype('uint8')
image.dims = np.shape(image.data)
return sitk_image, image
|