-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset_description.R
193 lines (148 loc) · 6.05 KB
/
dataset_description.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
################################################################################
#' Title:
#' Author: Jorge del Pozo Lerida
#' Date: 2023-12-10
#' Description:
################################################################################
## Setup -------------------------------------------------------------------
# Load necessary packages
source("src/library_imports.R")
# Load custom functions
source("src/functions_utils.R")
CMB_prio <- read_csv("/home/cerebriu/data/RESEARCH/Segmentation_CMB/data/CMB_detected_SilviaAnnotation.csv")
CMB_meddare <- read_csv("/home/cerebriu/data/RESEARCH/Segmentation_CMB/data/CMB_MedDARE_data.csv") %>%
distinct(StudyInstanceUID, .keep_all = T)
sequence_metadata <- read_csv("/home/cerebriu/data/DM/MyCerebriu/Pathology_Overview/all_sequences_final.csv")
study_metadata <- read_csv("/home/cerebriu/data/DM/MyCerebriu/Pathology_Overview/all_studies_final.csv")
# Match all data ----------------------------------------------------------
data_all <- sequence_metadata %>%
filter(StudyInstanceUID %in% CMB_meddare$StudyInstanceUID) %>%
# identify only SWI or T2S
filter(grepl("T2S|SWI", CRBSeriesDescription, ignore.case = TRUE)) %>%
group_by(StudyInstanceUID) %>%
mutate(n=n()) %>%
filter(n==1) %>%
# Add study-level
left_join(study_metadata %>%
filter(StudyInstanceUID %in% CMB_meddare$StudyInstanceUID) %>%
select(-Dataset, -Step),
by="StudyInstanceUID") %>%
# Add desired columns
mutate(
Resolution= paste0("(", Rows, ", " ,Columns, ", ", Slices, ")"),
country= sapply(str_split(Dataset, "-"), `[`, 1),
country = case_when(
country == 'BR' ~ "Brazil",
country == "IN" ~ "India",
country == "US" ~ "U.S.A"
),
MagneticFieldStrength = round(as.numeric(MagneticFieldStrength), 2),
MagneticFieldStrength = case_when(
MagneticFieldStrength== "15000" ~ 1.5,
TRUE ~ MagneticFieldStrength
),
Demographics = "Not available",
Location = country,
RepetitionTime = round(as.numeric(RepetitionTime)),
EchoTime = round(as.numeric(EchoTime)),
`TR/TE (ms)`= paste0(RepetitionTime, "/", EchoTime),
`TR (ms)`= RepetitionTime,
`TE (ms)`= EchoTime,
`Scanner Type` = paste0(Manufacturer, " ", MagneticFieldStrength, "T" ),
`Scanner Model` = ManufacturerModelName ,
`Flip Angle` = FlipAngle,
voxel_vals_1 = round(as.numeric(sapply(str_extract_all(PixelSpacing, "\\d+\\.?\\d*"), `[[`, 1)), 2),
voxel_vals_2 = round(as.numeric(sapply(str_extract_all(PixelSpacing, "\\d+\\.?\\d*"), `[[`, 2)),2),
`Voxel Size (mm3)` = paste0(voxel_vals_1, "x",voxel_vals_2, "x", SliceThickness),
`Seq. Type`= CRBSeriesDescription,
Dataset_big = sapply(str_split(Dataset, "-"), `[`, 2),
Hospital= case_when(
Dataset_big == "FIDI" ~ "Source 1",
Dataset_big == "BodyScanData"~ "Source 2",
Dataset_big == "Victoria"~ "Source 3",
Dataset_big == "Aarthi" ~ "Source 4",
Dataset_big == "SUNY"~ "Source 5"
)
) %>%
# Select relevant
select(StudyInstanceUID, Demographics, Hospital, Location, `Scanner Type`, `Scanner Model`, `Seq. Type`, `TR/TE (ms)`,`TR (ms)`, `TE (ms)`, `Flip Angle`, Resolution, `Voxel Size (mm3)`)
# Build table -------------------------------------------------------------
# Function to calculate percentages and format output
calc_percent <- function(x) {
freq <- table(x)
percent <- round(100 * freq / sum(freq), 2)
# Check if only one category exists
if (length(freq) == 1) {
return(names(freq))
} else {
return(paste(paste0(round(percent), "%"), names(freq), sep=": ", collapse=", "))
}
}
# Aggregating data
summary_table <- data_all %>%
group_by(Hospital) %>%
summarise(
# Demographics = calc_percent(Demographics),
Location = calc_percent(Location),
`Scanner Type` = calc_percent(`Scanner Type`),
`Scanner Model` = calc_percent(`Scanner Model`),
`Seq. Type` = calc_percent(`Seq. Type`),
`TR/TE (ms)` = calc_percent(`TR/TE (ms)`),
# `TR (ms)` = calc_percent(`TR (ms)`),
# `TE (ms)` = calc_percent(`TE (ms)`),
`Flip Angle` = calc_percent(`Flip Angle`),
Resolution = calc_percent(Resolution),
`Voxel Size (mm3)` = calc_percent(`Voxel Size (mm3)`),
`# patients` = n()
) %>%
ungroup()
summary_table
write_csv(summary_table, "/home/cerebriu/data/RESEARCH/Segmentation_CMB/data/summary_newdataset_scannerparams.csv")
# Pathologies -------------------------------------------------------------
# Convert CRB_ columns to boolean
df <- CMB_meddare %>%
mutate(
CRB_Infarct = as.logical(CRB_Infarct),
CRB_Hemorrhage = as.logical(CRB_Hemorrhage),
CRB_Tumor = as.logical(CRB_Tumor)
)
df2 <- df %>%
select(-contains("location")) %>%
pivot_longer(cols = c(starts_with("other"), "infarct", "hemorrhage", "tumor"), names_to = "Pathology_Type") %>%
group_by(StudyInstanceUID, Pathology_Type, value) %>%
summarise(count = n()) %>%
pivot_wider(names_from = value, values_from = count, values_fill = 0) %>%
ungroup() %>%
select(-Pathology_Type)
result <- df %>%
summarize(
CRB_Infarct_count = sum(CRB_Infarct),
CRB_Hemorrhage_count = sum(CRB_Hemorrhage),
CRB_Tumor_count = sum(CRB_Tumor)
) %>%
ungroup()
#subtypes
# Get the list of column names in df2 excluding StudyInstanceUID
column_names <- setdiff(colnames(df2), c("StudyInstanceUID", "Not present"))
# Initialize an empty list to store the results
column_counts <- list()
# Loop over the column names and calculate counts
for (col_name in column_names) {
counts <- df2 %>%
summarise(count = sum(!!sym(col_name))) %>%
rename(!!paste0(col_name, "_count") := count)
# Append the counts to the list
column_counts[[col_name]] <- counts
}
# Combine the counts into a single dataframe
result_counts <- bind_cols(column_counts)
# Print the counts for each column
print(result_counts)
unique_values_1 <- unique(df$additional_findings_1)
unique_values_2 <- unique(df$additional_findings_2)
unique_values_3 <- unique(df$additional_findings_3)
unique_vals <- unique(
c(unique_values_1,
unique_values_2,
unique_values_3)
)