-
Notifications
You must be signed in to change notification settings - Fork 13
/
medical.R
318 lines (271 loc) · 5.5 KB
/
medical.R
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
#' @title Dicom class
#' @return None
#' @export
Dicom = function() {
invisible(fastai2$medical$imaging$PILDicom)
}
#' @title get_dicom_files
#'
#' @description Get dicom files in `path` recursively, only in `folders`, if specified.
#'
#'
#' @param path path to files
#' @param recurse recursive or not
#' @param folders folder names
#' @return lsit of files
#'
#'
#' @examples
#'
#' \dontrun{
#'
#' items = get_dicom_files("siim_small/train/")
#'
#'
#' }
#'
#' @export
get_dicom_files <- function(path, recurse = TRUE, folders = NULL) {
args = list(
path = path,
recurse = recurse,
folders = folders
)
if(is.null(args$folders))
args$folders <- NULL
do.call(medical()$get_dicom_files, args)
}
#' @title Read dicom
#'
#' @description Open a `DICOM` file
#'
#'
#' @param fn file name
#' @param force logical, force
#' @return dicom object
#'
#'
#' @examples
#'
#' \dontrun{
#'
#' img = dcmread('hemorrhage.dcm')
#'
#'
#' }
#'
#' @export
dcmread <- function(fn, force = FALSE) {
medical()$Path$dcmread(
fn = fn,
force = force
)
}
#' @title Dicom
#' @description prints dicom file
#' @param x dicom file
#' @param ... additional parameters to pass
#' @return None
#' @export
print.pydicom.dataset.FileDataset <- function(x, ...) {
cat(x$`__str__`())
}
#' @title Get image matrix
#'
#' @param img dicom file
#' @param type img transformation
#' @param scan apply uniform or gaussian blur effects
#' @param size size of image
#' @param convert to R matrix or keep tensor
#' @return tensor
#'
#'
#' @examples
#'
#' \dontrun{
#'
#' img = dcmread('hemorrhage.dcm')
#' img %>% get_dcm_matrix(type = 'raw')
#'
#' }
#'
#' @export
get_dcm_matrix <- function(img, type = 'raw', scan = '', size = 50, convert = TRUE) {
obj = medical()$dicom_windows
fun = function() {
if(scan=='uniform_blur2d') {
img = medical()$uniform_blur2d(
img$scaled_px$windowed(res[[1]], res[[2]]), as.integer(size))
} else if(scan=='gauss_blur2d') {
img = medical()$gauss_blur2d(
img$scaled_px$windowed(res[[1]], res[[2]]), as.integer(size))
} else {
img = img$scaled_px$windowed(res[[1]], res[[2]])
}
if(convert) {
img$cpu()$numpy()
} else {
img
}
}
if (type=='raw') {
img$pixel_array
} else if (type=='abdomen_soft') {
res = obj$abdomen_soft
fun()
} else if (type=='brain') {
res = obj$brain
fun()
} else if (type=='brain_bone') {
res = obj$brain_bone
fun()
} else if (type=='brain_soft') {
res = obj$brain_soft
fun()
} else if (type=='liver') {
res = obj$liver
fun()
} else if (type=='lungs') {
res = obj$lungs
fun()
} else if (type=='mediastinum') {
res = obj$mediastinum
fun()
} else if (type=='spine_bone') {
res = obj$spine_bone
fun()
} else if (type=='spine_soft') {
res = obj$spine_soft
fun()
} else if (type=='stroke') {
res = obj$stroke
fun()
} else if (type=='subdural') {
res = obj$subdural
fun()
} else if (type=='normalized') {
if(scan=='uniform_blur2d') {
img = medical()$uniform_blur2d(
img$scaled_px$hist_scaled(), as.integer(size))
} else if(scan=='gauss_blur2d') {
img = medical()$gauss_blur2d(
img$scaled_px$hist_scaled(), as.integer(size))
} else {
img = img$hist_scaled()
}
if (convert) {
img$cpu()$numpy()
} else {
img
}
}
}
#' @title Mask from blur
#' @param img image
#' @param window windowing effect
#' @param sigma sigma
#' @param thresh thresholf point
#' @param remove_max remove maximum or not
#'
#' @export
mask_from_blur <- function(img, window, sigma = 0.3, thresh = 0.05, remove_max = TRUE) {
img$mask_from_blur(
window = list(as.integer(window[[1]]),as.integer(window[[2]])),
sigma = sigma,
thresh = thresh,
remove_max = remove_max
)
}
#' @title Zoom
#' @param img image files
#' @param ratio ratio
#' @return image
#' @export
zoom <- function(img, ratio) {
img$zoom(
ratio = ratio
)
}
#' @title Mask2bbox
#' @param mask mask
#' @param convert to R matrix
#' @return tensor
#' @export
mask2bbox <- function(mask, convert = TRUE) {
res = medical()$mask2bbox(
mask = mask
)
if(convert) {
lo = res[[0]]$cpu()$numpy()
hi = res[[1]]$cpu()$numpy()
list(lo,hi)
} else {
res
}
}
#' @title Abdomen soft
#' @return list
#' @export
win_abdoment_soft <- function() {
medical()$dicom_windows$abdomen_soft
}
#' @title Brain
#' @return list
#' @export
win_brain <- function() {
medical()$dicom_windows$brain
}
#' @title Brain bone
#' @return list
#' @export
win_brain_bone <- function() {
medical()$dicom_windows$brain_bone
}
#' @title Brain soft
#' @return list
#' @export
win_brain_soft <- function() {
medical()$dicom_windows$brain_soft
}
#' @title Liver
#' @return list
#' @export
win_liver <- function() {
medical()$dicom_windows$liver
}
#' @title Lungs
#' @return list
#' @export
win_lungs <- function() {
medical()$dicom_windows$lungs
}
#' @title Mediastinum
#' @return list
#' @export
win_mediastinum <- function() {
medical()$dicom_windows$mediastinum
}
#' @title Spine bone
#' @return list
#' @export
win_spine_bone <- function() {
medical()$dicom_windows$spine_bone
}
#' @title Spine soft
#' @return list
#' @export
win_spine_soft <- function() {
medical()$dicom_windows$spine_soft
}
#' @title Stroke
#' @return list
#' @export
win_stroke <- function() {
medical()$dicom_windows$stroke
}
#' @title Subdural
#' @return list
#' @export
win_subdural <- function() {
medical()$dicom_windows$subdural
}