-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathR_angio.R
373 lines (295 loc) · 14.5 KB
/
R_angio.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
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
<<<<<<< HEAD
# angio statistics and figure generation script
library(dplyr)
library(readr)
library(ggplot2)
vesselLoad <- function(tag, path = "Vessel_Measurements/", group) {
# Construct the pattern to search for files with the specific tag
pattern <- paste0(tag, "_", group, "[0-9]+\\.csv$")
# List all files in the directory
all_files <- list.files(path, pattern = pattern, full.names = TRUE)
print(all_files)
# Function to read a file and add NA rows if needed
readAndPad <- function(filePath) {
df <- read_csv(filePath)
neededRows <- 5 - nrow(df)
if (neededRows > 0) {
# Create a tibble of NAs with the same number of columns as df
padRows <- as_tibble(matrix(NA, ncol = ncol(df), nrow = neededRows))
colnames(padRows) <- colnames(df)
# Bind the padRows on top of df
df <- bind_rows(padRows, df)
}
return(df)
}
# Load each file and store in a list
data_list <- lapply(all_files, readAndPad)
return(data_list)
}
adjust_and_calculate_CNR <- function(data) {
adjusted_data <- list()
noise_data <- read.csv("cnr_adjustments.csv")
for (i in seq_along(data)) {
if (i <= nrow(noise_data)) {
df <- data[[i]]
# Example CNR calculation (modify according to your logic)
df$CNR_HU <- (df$Signal_HU - noise_data$adj_noise[i])/noise_data$adj_std[i]
df$CNR_Kedge <- (df$Signal_Kedge - noise_data$adj_kedge[i])/noise_data$adj_kstd[i]
adjusted_data[[i]] <- df
}
}
return(adjusted_data)
}
vesselStats <- function(data) {
# Prepare data frames to store the final averages and standard deviations
averages_df <- data.frame(matrix(ncol = ncol(data[[1]]), nrow = nrow(data[[1]])))
colnames(averages_df) <- colnames(data[[1]])
std_dev_df <- data.frame(matrix(ncol = ncol(data[[1]]), nrow = nrow(data[[1]])))
colnames(std_dev_df) <- colnames(data[[1]])
# Iterate over each row
for(row in 1:nrow(data[[1]])) {
sums <- vector("numeric", ncol(data[[1]]))
squared_sums <- vector("numeric", ncol(data[[1]]))
counts <- rep(0, ncol(data[[1]]))
# Loop through each data frame in the list
for(df in data) {
# Check if the row exists in the current data frame
if(nrow(df) >= row) {
# Convert the row to numeric
row_values <- as.numeric(df[row, ])
# Identify non-NA indices
non_na_indices <- !is.na(row_values)
# Perform calculations only on non-NA values
sums[non_na_indices] <- sums[non_na_indices] + row_values[non_na_indices]
squared_sums[non_na_indices] <- squared_sums[non_na_indices] + (row_values[non_na_indices])^2
counts[non_na_indices] <- counts[non_na_indices] + 1
}
}
# Calculate the averages and standard deviations
averages <- sums / counts
variances <- (squared_sums - (sums^2 / counts)) / (counts - 1)
std_devs <- sqrt(variances)
# Handle cases where count is zero to avoid division by zero
averages[is.na(averages)] <- NA # Set NA where division by zero occurred
std_devs[is.na(std_devs)] <- NA
# Store results in the corresponding data frames
averages_df[row, ] <- averages
std_dev_df[row, ] <- std_devs
}
return(list(averages = averages_df, std_devs = std_dev_df))
}
#SAA_data <- vesselLoad("SAA", group = "d")
dotarem_IRA_data <- vesselLoad("IRA", group = "d")
#IVC_data <- vesselLoad("IVC", group = "d")
#IRVC_data <- vesselLoad("IRVC", group = "d")
# Adjust noise and calculate CNR for each data set
#SAA_adjusted <- adjust_and_calculate_CNR(SAA_data)
IRA_adjusted <- adjust_and_calculate_CNR(dotarem_IRA_data)
#IVC_adjusted <- adjust_and_calculate_CNR(IVC_data)
#IRVC_adjusted <- adjust_and_calculate_CNR(IRVC_data)
#aguix ONLY
#correct <-c(1,3,4,5,6,7)
#aguix_IRA_data <- aguix_IRA_data[correct]
#aguix_IRA_stats <- vesselStats(IRA_adjusted)
dotarem_IRA_stats <- vesselStats(IRA_adjusted)
generateAngioBarsWithError <- function(stats_list, column_name, title, x_label, y_label, y_breaks) {
# Assuming Time Points are consistent across datasets
time_points <- factor(c(0.11, 0.5, 1, 3, 10)) # Convert to factor for discrete axis
#vessel_names <- c("SAA", "IRA", "IVC", "IRVC")
#vessel_names <- c("SAA", "IVC")
vessel_names <- c("AGuIX", "Dotarem")
colors <- c("SAA" = "red", "IRA" = "pink", "IVC" = "blue", "IRVC" = "lightblue","AGuIX" = "lightblue", "Dotarem" = "gold")
#colors <- c("SAA" = "red", "IVC" = "blue")
# Prepare data for plotting
plot_data <- expand.grid(TimePoint = time_points, Vessel = vessel_names)
plot_data$Signal <- unlist(lapply(stats_list, function(x) x$averages[[column_name]]))
plot_data$SD <- unlist(lapply(stats_list, function(x) x$std_devs[[column_name]]))
gg <- ggplot(plot_data, aes(x = TimePoint, y = Signal, fill = Vessel, group = Vessel)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.7), width = 0.6) +
geom_errorbar(aes(ymin = pmax(Signal - SD, 0), ymax = Signal + SD), width = 0.2, position = position_dodge(width = 0.7)) +
#geom_errorbar(aes(ymin = Signal - SD, ymax = Signal + SD), width = 0.2, position = position_dodge(width = 0.7)) +
scale_fill_manual(values = colors) +
labs(title = title, x = x_label, y = y_label) +
#limits = c(1,10)
scale_y_continuous(limits = c(0,600), expand = expansion(mult = c(0.003, 0.006)), breaks = y_breaks) +
theme_minimal() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.title = element_text(hjust = 0.5, size = 30),
legend.title = element_blank(),
axis.line = element_line(color="black", size = 1),
axis.line.x = element_line(color = "black", size = 1),
axis.line.y = element_line(color = "black", size = 1),
axis.ticks = element_line(color = "black"), axis.title = element_text(size = 30),
panel.border = element_blank(),
axis.title.y = element_text(size = 30, margin = margin(r = 20)),
axis.title.x = element_text(size = 30, margin = margin(t = 20)),
panel.background = element_rect(fill = "white"), axis.text.x = element_text(size = 30),
axis.text = element_text(size = 30, margin = margin(r = 20), face = "bold" )
)+
scale_x_discrete(name = x_label) +
geom_vline(xintercept = c(2.5, 4.5), linetype = "dotted", color = "black") +
geom_hline(yintercept = c(0.2), linetype = "dotted", color = "black", size = 1.5)
# Print the plot
print(gg)
ggsave("CNR_angio_Kedge_plot.png", plot = gg, width = 5000, height = 2500, units = "px")
}
# Example usage
stats_list <- list(aguix_IRA_stats, dotarem_IRA_stats)
#stats_list <- list(SAA_stats, IRA_stats, IVC_stats, IRVC_stats)
#stats_list <- list(SAA_stats, IVC_stats)
#stats_list <- list(aguix_SAA_stats, aguix_IRA_stats, aguix_IVC_stats, aguix_IRVC_stats)
#generateAngioBarsWithError(stats_list, "Signal_HU", "IRA Enhancement", "Time (min)", "Hounsfield Units (HU)", y_breaks = c(100, 200, 300, 400 ,500, 600))
generateAngioBarsWithError(stats_list, "Signal_HU", "Infrarenal aorta angiography", "Time (min)", "CT Number (HU units)", y_breaks = c(100, 200, 300, 400, 500, 600))
generateAngioBarsWithError(stats_list, "CNR_Kedge", "CNR in Color K-edge Images", "Time (min)", "log10(A.U)", y_breaks = c(0:100))
print(max(SAA_stats$averages$CNR_HU
))
=======
# angio statistics and figure generation script
library(dplyr)
library(readr)
library(ggplot2)
vesselLoad <- function(tag, path = "Vessel_Measurements/", group) {
# Construct the pattern to search for files with the specific tag
pattern <- paste0(tag, "_", group, "[0-9]+\\.csv$")
# List all files in the directory
all_files <- list.files(path, pattern = pattern, full.names = TRUE)
print(all_files)
# Function to read a file and add NA rows if needed
readAndPad <- function(filePath) {
df <- read_csv(filePath)
neededRows <- 5 - nrow(df)
if (neededRows > 0) {
# Create a tibble of NAs with the same number of columns as df
padRows <- as_tibble(matrix(NA, ncol = ncol(df), nrow = neededRows))
colnames(padRows) <- colnames(df)
# Bind the padRows on top of df
df <- bind_rows(padRows, df)
}
return(df)
}
# Load each file and store in a list
data_list <- lapply(all_files, readAndPad)
return(data_list)
}
adjust_and_calculate_CNR <- function(data) {
adjusted_data <- list()
noise_data <- read.csv("cnr_adjustments.csv")
for (i in seq_along(data)) {
if (i <= nrow(noise_data)) {
df <- data[[i]]
# Example CNR calculation (modify according to your logic)
df$CNR_HU <- (df$Signal_HU - noise_data$adj_noise[i])/noise_data$adj_std[i]
df$CNR_Kedge <- (df$Signal_Kedge - noise_data$adj_kedge[i])/noise_data$adj_kstd[i]
adjusted_data[[i]] <- df
}
}
return(adjusted_data)
}
vesselStats <- function(data) {
# Prepare data frames to store the final averages and standard deviations
averages_df <- data.frame(matrix(ncol = ncol(data[[1]]), nrow = nrow(data[[1]])))
colnames(averages_df) <- colnames(data[[1]])
std_dev_df <- data.frame(matrix(ncol = ncol(data[[1]]), nrow = nrow(data[[1]])))
colnames(std_dev_df) <- colnames(data[[1]])
# Iterate over each row
for(row in 1:nrow(data[[1]])) {
sums <- vector("numeric", ncol(data[[1]]))
squared_sums <- vector("numeric", ncol(data[[1]]))
counts <- rep(0, ncol(data[[1]]))
# Loop through each data frame in the list
for(df in data) {
# Check if the row exists in the current data frame
if(nrow(df) >= row) {
# Convert the row to numeric
row_values <- as.numeric(df[row, ])
# Identify non-NA indices
non_na_indices <- !is.na(row_values)
# Perform calculations only on non-NA values
sums[non_na_indices] <- sums[non_na_indices] + row_values[non_na_indices]
squared_sums[non_na_indices] <- squared_sums[non_na_indices] + (row_values[non_na_indices])^2
counts[non_na_indices] <- counts[non_na_indices] + 1
}
}
# Calculate the averages and standard deviations
averages <- sums / counts
variances <- (squared_sums - (sums^2 / counts)) / (counts - 1)
std_devs <- sqrt(variances)
# Handle cases where count is zero to avoid division by zero
averages[is.na(averages)] <- NA # Set NA where division by zero occurred
std_devs[is.na(std_devs)] <- NA
# Store results in the corresponding data frames
averages_df[row, ] <- averages
std_dev_df[row, ] <- std_devs
}
return(list(averages = averages_df, std_devs = std_dev_df))
}
#SAA_data <- vesselLoad("SAA", group = "d")
dotarem_IRA_data <- vesselLoad("IRA", group = "d")
#IVC_data <- vesselLoad("IVC", group = "d")
#IRVC_data <- vesselLoad("IRVC", group = "d")
# Adjust noise and calculate CNR for each data set
#SAA_adjusted <- adjust_and_calculate_CNR(SAA_data)
IRA_adjusted <- adjust_and_calculate_CNR(dotarem_IRA_data)
#IVC_adjusted <- adjust_and_calculate_CNR(IVC_data)
#IRVC_adjusted <- adjust_and_calculate_CNR(IRVC_data)
#aguix ONLY
#correct <-c(1,3,4,5,6,7)
#aguix_IRA_data <- aguix_IRA_data[correct]
#aguix_IRA_stats <- vesselStats(IRA_adjusted)
dotarem_IRA_stats <- vesselStats(IRA_adjusted)
generateAngioBarsWithError <- function(stats_list, column_name, title, x_label, y_label, y_breaks) {
# Assuming Time Points are consistent across datasets
time_points <- factor(c(0.11, 0.5, 1, 3, 10)) # Convert to factor for discrete axis
#vessel_names <- c("SAA", "IRA", "IVC", "IRVC")
#vessel_names <- c("SAA", "IVC")
vessel_names <- c("AGuIX", "Dotarem")
colors <- c("SAA" = "red", "IRA" = "pink", "IVC" = "blue", "IRVC" = "lightblue","AGuIX" = "lightblue", "Dotarem" = "gold")
#colors <- c("SAA" = "red", "IVC" = "blue")
# Prepare data for plotting
plot_data <- expand.grid(TimePoint = time_points, Vessel = vessel_names)
plot_data$Signal <- unlist(lapply(stats_list, function(x) x$averages[[column_name]]))
plot_data$SD <- unlist(lapply(stats_list, function(x) x$std_devs[[column_name]]))
gg <- ggplot(plot_data, aes(x = TimePoint, y = Signal, fill = Vessel, group = Vessel)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.7), width = 0.6) +
geom_errorbar(aes(ymin = pmax(Signal - SD, 0), ymax = Signal + SD), width = 0.2, position = position_dodge(width = 0.7)) +
#geom_errorbar(aes(ymin = Signal - SD, ymax = Signal + SD), width = 0.2, position = position_dodge(width = 0.7)) +
scale_fill_manual(values = colors) +
labs(title = title, x = x_label, y = y_label) +
#limits = c(1,10)
scale_y_continuous(limits = c(0,600), expand = expansion(mult = c(0.003, 0.006)), breaks = y_breaks) +
theme_minimal() +
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.title = element_text(hjust = 0.5, size = 30),
legend.title = element_blank(),
axis.line = element_line(color="black", size = 1),
axis.line.x = element_line(color = "black", size = 1),
axis.line.y = element_line(color = "black", size = 1),
axis.ticks = element_line(color = "black"), axis.title = element_text(size = 30),
panel.border = element_blank(),
axis.title.y = element_text(size = 30, margin = margin(r = 20)),
axis.title.x = element_text(size = 30, margin = margin(t = 20)),
panel.background = element_rect(fill = "white"), axis.text.x = element_text(size = 30),
axis.text = element_text(size = 30, margin = margin(r = 20), face = "bold" )
)+
scale_x_discrete(name = x_label) +
geom_vline(xintercept = c(2.5, 4.5), linetype = "dotted", color = "black") +
geom_hline(yintercept = c(0.2), linetype = "dotted", color = "black", size = 1.5)
# Print the plot
print(gg)
ggsave("CNR_angio_Kedge_plot.png", plot = gg, width = 5000, height = 2500, units = "px")
}
# Example usage
stats_list <- list(aguix_IRA_stats, dotarem_IRA_stats)
#stats_list <- list(SAA_stats, IRA_stats, IVC_stats, IRVC_stats)
#stats_list <- list(SAA_stats, IVC_stats)
#stats_list <- list(aguix_SAA_stats, aguix_IRA_stats, aguix_IVC_stats, aguix_IRVC_stats)
#generateAngioBarsWithError(stats_list, "Signal_HU", "IRA Enhancement", "Time (min)", "Hounsfield Units (HU)", y_breaks = c(100, 200, 300, 400 ,500, 600))
generateAngioBarsWithError(stats_list, "Signal_HU", "Infrarenal aorta angiography", "Time (min)", "CT Number (HU units)", y_breaks = c(100, 200, 300, 400, 500, 600))
generateAngioBarsWithError(stats_list, "CNR_Kedge", "CNR in Color K-edge Images", "Time (min)", "log10(A.U)", y_breaks = c(0:100))
print(max(SAA_stats$averages$CNR_HU
))
>>>>>>> 77a3982bf0902d3f28a8584daad61788e60cc341