-
Notifications
You must be signed in to change notification settings - Fork 12
/
acr_snr.py
375 lines (303 loc) · 12.7 KB
/
acr_snr.py
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"""
ACR SNR
Calculates the SNR for slice 7 (the uniformity slice) of the ACR phantom.
This script utilises the smoothed subtraction method described in McCann 2013:
A quick and robust method for measurement of signal-to-noise ratio in MRI, Phys. Med. Biol. 58 (2013) 3775:3790
and a standard subtraction SNR.
Created by Neil Heraghty (Adapted by Yassine Azma)
09/01/2023
"""
import os
import sys
import traceback
import pydicom
import numpy as np
from scipy import ndimage
import hazenlib.utils
from hazenlib.HazenTask import HazenTask
from hazenlib.ACRObject import ACRObject
class ACRSNR(HazenTask):
"""Signal-to-noise ratio measurement class for DICOM images of the ACR phantom
Inherits from HazenTask class
"""
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.ACR_obj = ACRObject(self.dcm_list)
# measured slice width is expected to be a floating point number
try:
self.measured_slice_width = float(kwargs["measured_slice_width"])
except:
self.measured_slice_width = None
# subtract is expected to be a path to a folder
try:
if os.path.isdir(kwargs["subtract"]):
self.subtract = kwargs["subtract"]
except:
self.subtract = None
def run(self) -> dict:
"""Main function for performing SNR measurement
using slice 7 from the ACR phantom image set
Notes:
using the smoothing method by default or the subtraction method if a second set of images are provided (in a separate folder)
Returns:
dict: results are returned in a standardised dictionary structure specifying the task name, input DICOM Series Description + SeriesNumber + InstanceNumber, task measurement key-value pairs, optionally path to the generated images for visualisation
"""
# Identify relevant slice
snr_dcm = self.ACR_obj.slice7_dcm
# Initialise results dictionary
results = self.init_result_dict()
# SINGLE METHOD (SMOOTHING)
if self.subtract is None:
try:
results["file"] = self.img_desc(snr_dcm)
snr, normalised_snr = self.snr_by_smoothing(
snr_dcm, self.measured_slice_width
)
results["measurement"]["snr by smoothing"] = {
"measured": round(snr, 2),
"normalised": round(normalised_snr, 2),
}
except Exception as e:
print(
f"Could not calculate the SNR for {self.img_desc(snr_dcm)} because of : {e}"
)
traceback.print_exc(file=sys.stdout)
# SUBTRACTION METHOD
else:
# Get the absolute path to all FILES found in the directory provided
filepaths = [
os.path.join(self.subtract, f)
for f in os.listdir(self.subtract)
if os.path.isfile(os.path.join(self.subtract, f))
]
data2 = [pydicom.dcmread(dicom) for dicom in filepaths]
snr_dcm2 = ACRObject(data2).slice7_dcm
results["file"] = [self.img_desc(snr_dcm), self.img_desc(snr_dcm2)]
try:
snr, normalised_snr = self.snr_by_subtraction(
snr_dcm, snr_dcm2, self.measured_slice_width
)
results["measurement"]["snr by subtraction"] = {
"measured": round(snr, 2),
"normalised": round(normalised_snr, 2),
}
except Exception as e:
print(
f"Could not calculate the SNR for {self.img_desc(snr_dcm)} and "
f"{self.img_desc(snr_dcm2)} because of : {e}"
)
traceback.print_exc(file=sys.stdout)
# only return reports if requested
if self.report:
results["report_image"] = self.report_files
return results
def get_normalised_snr_factor(self, dcm, measured_slice_width=None) -> float:
"""Calculate the normalisation factor to be applied
Args:
dcm (pydicom.Dataset): DICOM image object
measured_slice_width (float, optional): Provide the true slice width for the set of images. Defaults to None.
Returns:
float: normalisation factor
"""
dx, dy = hazenlib.utils.get_pixel_size(dcm)
bandwidth = hazenlib.utils.get_bandwidth(dcm)
TR = hazenlib.utils.get_TR(dcm)
rows = hazenlib.utils.get_rows(dcm)
columns = hazenlib.utils.get_columns(dcm)
if measured_slice_width:
slice_thickness = measured_slice_width
else:
slice_thickness = hazenlib.utils.get_slice_thickness(dcm)
averages = hazenlib.utils.get_average(dcm)
bandwidth_factor = np.sqrt((bandwidth * columns / 2) / 1000) / np.sqrt(30)
voxel_factor = 1 / (0.001 * dx * dy * slice_thickness)
normalised_snr_factor = (
bandwidth_factor
* voxel_factor
* (1 / (np.sqrt(averages * rows * (TR / 1000))))
)
return normalised_snr_factor
def filtered_image(self, dcm: pydicom.Dataset) -> np.array:
"""Apply filtering to a pixel array (image)
Notes:
Performs a 2D convolution (for filtering images)
uses uniform_filter SciPy function
Args:
dcm (pydicom.Dataset): DICOM image object
Returns:
np.array: pixel array of the filtered image
"""
a = dcm.pixel_array.astype("int")
# filter size = 9, following MATLAB code and McCann 2013 paper for head coil, although note McCann 2013
# recommends 25x25 for body coil.
filtered_array = ndimage.uniform_filter(a, 25, mode="constant")
return filtered_array
def get_noise_image(self, dcm: pydicom.Dataset) -> np.array:
"""Get noise image by subtracting the filtered image from the original pixel array
Notes:
Separates the image noise by smoothing the image and subtracting the smoothed image from the original.
Args:
dcm (pydicom.Dataset): DICOM image object
Returns:
np.array: pixel array representing the image noise
"""
a = dcm.pixel_array.astype("int")
# Convolve image with boxcar/uniform kernel
imsmoothed = self.filtered_image(dcm)
# Subtract smoothed array from original
imnoise = a - imsmoothed
return imnoise
def get_roi_samples(
self, ax, dcm: pydicom.Dataset or np.ndarray, centre_col: int, centre_row: int
) -> list:
"""Identify regions of interest
Args:
ax (matplotlib.pyplot.subplots): matplotlib axis for visualisation
dcm (pydicom.Dataset or np.ndarray): DICOM image object, or its pixel array
centre_col (int): x coordinate of the centre
centre_row (int): y coordinate of the centre
Returns:
list of np.array: subsets of the original pixel array
"""
if type(dcm) == np.ndarray:
data = dcm
else:
data = dcm.pixel_array
sample = [None] * 5
# for array indexing: [row, column] format
sample[0] = data[
(centre_row - 10) : (centre_row + 10), (centre_col - 10) : (centre_col + 10)
]
sample[1] = data[
(centre_row - 50) : (centre_row - 30), (centre_col - 50) : (centre_col - 30)
]
sample[2] = data[
(centre_row + 30) : (centre_row + 50), (centre_col - 50) : (centre_col - 30)
]
sample[3] = data[
(centre_row - 50) : (centre_row - 30), (centre_col + 30) : (centre_col + 50)
]
sample[4] = data[
(centre_row + 30) : (centre_row + 50), (centre_col + 30) : (centre_col + 50)
]
if ax:
from matplotlib.patches import Rectangle
from matplotlib.collections import PatchCollection
# for patches: [column/x, row/y] format
rects = [
Rectangle((centre_col - 10, centre_row - 10), 20, 20),
Rectangle((centre_col - 50, centre_row - 50), 20, 20),
Rectangle((centre_col + 30, centre_row - 50), 20, 20),
Rectangle((centre_col - 50, centre_row + 30), 20, 20),
Rectangle((centre_col + 30, centre_row + 30), 20, 20),
]
pc = PatchCollection(
rects, edgecolors="red", facecolors="None", label="ROIs"
)
ax.add_collection(pc)
return sample
def snr_by_smoothing(
self, dcm: pydicom.Dataset, measured_slice_width=None
) -> float:
"""Calculate signal to noise ratio based on smoothing method
Args:
dcm (pydicom.Dataset): DICOM image object
measured_slice_width (float, optional): Provide the true slice width for the set of images. Defaults to None.
Returns:
float: normalised_snr
"""
centre = self.ACR_obj.centre
col, row = centre
noise_img = self.get_noise_image(dcm)
signal = [
np.mean(roi)
for roi in self.get_roi_samples(
ax=None, dcm=dcm, centre_col=int(col), centre_row=int(row)
)
]
noise = [
np.std(roi, ddof=1)
for roi in self.get_roi_samples(
ax=None, dcm=noise_img, centre_col=int(col), centre_row=int(row)
)
]
# note no root_2 factor in noise for smoothed subtraction (one image) method, replicating Matlab approach and
# McCann 2013
snr = np.mean(np.divide(signal, noise))
normalised_snr = snr * self.get_normalised_snr_factor(dcm, measured_slice_width)
if self.report:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 1)
fig.set_size_inches(8, 16)
fig.tight_layout(pad=4)
axes[0].imshow(dcm.pixel_array)
axes[0].scatter(centre[0], centre[1], c="red")
axes[0].set_title("Centroid Location")
axes[1].set_title("Smoothed Noise Image")
axes[1].imshow(noise_img, cmap="gray")
self.get_roi_samples(axes[1], dcm, int(col), int(row))
img_path = os.path.realpath(
os.path.join(self.report_path, f"{self.img_desc(dcm)}_smoothing.png")
)
fig.savefig(img_path)
self.report_files.append(img_path)
return snr, normalised_snr
def snr_by_subtraction(
self, dcm1: pydicom.Dataset, dcm2: pydicom.Dataset, measured_slice_width=None
) -> float:
"""Calculate signal to noise ratio based on subtraction method
Args:
dcm1 (pydicom.Dataset): DICOM image object to calculate signal
dcm2 (pydicom.Dataset): DICOM image object to calculate noise
measured_slice_width (float, optional): Provide the true slice width for the set of images. Defaults to None.
Returns:
float: normalised_snr
"""
centre = self.ACR_obj.centre
col, row = centre
difference = np.subtract(
dcm1.pixel_array.astype("int"), dcm2.pixel_array.astype("int")
)
signal = [
np.mean(roi)
for roi in self.get_roi_samples(
ax=None, dcm=dcm1, centre_col=int(col), centre_row=int(row)
)
]
noise = np.divide(
[
np.std(roi, ddof=1)
for roi in self.get_roi_samples(
ax=None, dcm=difference, centre_col=int(col), centre_row=int(row)
)
],
np.sqrt(2),
)
snr = np.mean(np.divide(signal, noise))
normalised_snr = snr * self.get_normalised_snr_factor(
dcm1, measured_slice_width
)
if self.report:
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 1)
fig.set_size_inches(8, 16)
fig.tight_layout(pad=4)
axes[0].imshow(dcm1.pixel_array)
axes[0].scatter(centre[0], centre[1], c="red")
axes[0].axis("off")
axes[0].set_title("Centroid Location")
axes[1].set_title("Difference Image")
axes[1].imshow(
difference,
cmap="gray",
)
self.get_roi_samples(axes[1], dcm1, int(col), int(row))
axes[1].axis("off")
img_path = os.path.realpath(
os.path.join(
self.report_path, f"{self.img_desc(dcm1)}_snr_subtraction.png"
)
)
fig.savefig(img_path)
self.report_files.append(img_path)
return snr, normalised_snr