-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
195 lines (169 loc) · 6 KB
/
server.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
######################
####### SERVER #######
######################
server <- function(input, output, session) {
# File selected to view
selected_file <- eventReactive(input$fileInput, {
filename <- file.path(datafile_path, input$fileInput)
# If no file has been selected, use default images
if(length(filename) == 0 || !dir.exists(filename) || is.na(filename)) {
filename <- list.files(file.path(datafile_path, default_dicom))
}
return(filename)
})
observe({
val <- selected_nifti()
d <- nifti_dim()
# update sliders with the selected DICOM dimensions
withProgress(message = "Updating Position...", value = 1, {
updateSliderInput(session, "slider_x", value = as.integer(d[1]/2), max = d[1])
updateSliderInput(session, "slider_y", value = as.integer(d[2]/2), max = d[2])
updateSliderInput(session, "slider_z", value = as.integer(d[3]/2), max = d[3])
})
})
# Read image
dcm_images <- reactive({
dicoms <- selected_file()
withProgress(message = "Processing DICOM file...", {
# Very similar to what readDICOM does. This allows updates to the progress bar as each file is read.
filenames <- list.files(dicoms, full.names = TRUE)
nfiles <- length(filenames)
headers <- images <- vector("list", nfiles)
names(images) <- names(headers) <- filenames
for(i in 1:nfiles) {
incProgress(1/nfiles)
dcm <- readDICOMFile(filenames[i])
images[[i]] <- dcm$img
headers[[i]] <- dcm$hdr
}
return(list(hdr = headers, img = images))
})
})
# Build table below the image
out_table <- reactive({
withProgress(message = "Extracting information...", {
hdr <- dcm_images()$hdr
header_fields <- c("StudyID", "StudyDescription", "StudyDate", "ProtocolName",
"AcquisitionDate", "Modality", "PatientsName", "PatientID",
"PatientsBirthDate", "RequestedProcedureID")
header_values <- map(header_fields, ~extractHeader(hdr, ..1, numeric = FALSE))
df <- as.data.frame(header_values)
names(df) <- header_fields
df$Findings <- "No findings"
return(df)
})
})
# Renders out table
output$table <- renderTable({
table <- out_table()
names(table) <- sapply(names(table), function(x) gsub("(.)([A-Z][a-z])|([a-z])([A-Z][A-Z])","\\1\\3 \\2\\4", x))
table[1, ]
})
# Header information of DICOM file
selected_nifti <- reactive({
dicom2nifti(dcm_images())
})
# Dimention of nifti
nifti_dim <- reactive({
dim(selected_nifti())
})
# Plot in the right-side of the image
output$distPlot <- renderPlot({
#withProgress(message = "Updating Orthongonal Positions...", value = 1, {
img <- selected_nifti()
x <- min(c(input$slider_x, nifti_dim()[1]))
y <- min(c(input$slider_y, nifti_dim()[2]))
z <- min(c(input$slider_z, nifti_dim()[3]))
orthographic(
img,
col.crosshairs = "green",
xyz = c(x, y, z),
oma = rep(0, 4),
mar = rep(0.5, 4),
col = gray(0:64/64),
text = paste(
"StudyID: " , out_table()[1, "StudyID"], "\n",
"Study Type: ", out_table()[1, "StudyType"], "\n",
"Date: " , out_table()[1, "AcquisitionDate"], "\n",
"Patient: " , out_table()[1, "PatientsName"], "\n",
"PatientID: " , out_table()[1, "PatientID"], "\n"
),
text.color = "white",
text.cex = 1,
useRaster = TRUE
)
})
# Setting patient info for each file
patientInfo <- reactive({
pat <- switch(input$fileInput,
"SOUS - 702" = c("pat_001", "Gandalf", "Brain MRI"),
"sT2-TSE-T - 301" = c("pat_002", "Aragorn", "Neck MRI"),
"sT2W-FLAIR - 401" = c("pat_003", "Legolas", "Pituitary MRI"),
"T1-3D-FFE-C - 801" = c("pat_004", "Gimli", "Thoratic-spine MRI"),
"T1-SE-extrp - 601" = c("pat_005", "Frodo", "Some-other MRI"),
"T1-SE-extrp - 701" = c("pat_006", "Bilbo", "Some-other MRI"),
"T2W-FE-EPI - 501" = c("pat_007", "Gollum", "Some-other MRI"),
c("Unspecified", "Unspecified", "Unspecified")
)
df <- data.frame(pat, row.names = c("Patients ID: ", "Name: ", "Exam: "))
colnames(df) <- NULL
return(df)
})
# Button to write to DB
onoClick <- observeEvent(input$writeDB, {
i <- input$writeDB
if (i < 1){
return()
}
tableName <- "image_analysis_findings"
pat <- t(patientInfo())
df <- out_table()[1, ]
output <- cbind(pat, df)
withProgress(message = paste("Exporting to ", tableName, "..."), {
writeTable(output, tableName)
feedback$text <- paste("Dataframe written to database table", tableName)
})
})
# Print image - axial view
output$axial <- renderPlot({
#withProgress(message = "Updating Axial Image...", value = 1, {
image(selected_nifti(), z = input$slider_z, plane = "axial",
plot.type = "single", col = gray(0:64/64), useRaster = TRUE)
#})
})
# Print image - sagittal view
output$sagittal <- renderPlot({
image(selected_nifti(), z = input$slider_x, plane = "sagittal",
plot.type = "single", col = gray(0:64/64), useRaster = TRUE)
})
# Print image - coronal view
output$coronal <- renderPlot({
image(selected_nifti(), z = input$slider_y, plane = "coronal",
plot.type = "single", col = gray(0:64/64), useRaster = TRUE)
})
# Sliders
observe({
n <- input$axialn
if(is.null(input$axialn)) {
return()
}
change <- sign(input$axialdy)
isolate(updateSliderInput(session, "slider_z", value = input$slider_z + change))
})
observe({
n <- input$sagittaln
if(is.null(input$sagittaln)) {
return()
}
change <- sign(input$sagittaldy)
isolate(updateSliderInput(session, "slider_x", value = input$slider_x + change))
})
observe({
n <- input$coronaln
if(is.null(input$coronaln)) {
return()
}
change <- sign(input$coronaldy)
isolate(updateSliderInput(session, "slider_y", value = input$slider_y + change))
})
}