{"metadata":{"kernelspec":{"name":"ir","display_name":"R","language":"R"},"language_info":{"name":"R","codemirror_mode":"r","pygments_lexer":"r","mimetype":"text/x-r-source","file_extension":".r","version":"4.0.5"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#data loading and cleaning\nlibrary(tidyverse)\n\nlibrary(e1071)\nlibrary(tidyverse)\nlibrary(dslabs)\nlibrary(dplyr)\nlibrary(caret)\nlibrary(lubridate)\nlibrary(tidytext)\nlibrary(\"RColorBrewer\")\nlibrary(randomForest)\nlibrary(tictoc)\nlibrary(ggpubr)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-04-20T06:58:09.027803Z","iopub.execute_input":"2023-04-20T06:58:09.029302Z","iopub.status.idle":"2023-04-20T06:58:09.064448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Load datasets\nbcp_train <- read.csv(\"/kaggle/input/rsna-breast-cancer-detection/train.csv\")\nbcp_test <- read.csv(\"/kaggle/input/rsna-breast-cancer-detection/test.csv\")\n\ntrain_images <- list.files(\"train/\", full.names = TRUE)\ntest_images <- list.files(\"test/\", full.names = TRUE)","metadata":{"execution":{"iopub.status.busy":"2023-04-20T06:58:10.702184Z","iopub.execute_input":"2023-04-20T06:58:10.703696Z","iopub.status.idle":"2023-04-20T06:58:10.945739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Data size & structure\ndim(bcp_train)\ndim(bcp_test)","metadata":{"execution":{"iopub.status.busy":"2023-04-20T06:58:14.6858Z","iopub.execute_input":"2023-04-20T06:58:14.687422Z","iopub.status.idle":"2023-04-20T06:58:14.709701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"str(bcp_train)\nstr(bcp_test)","metadata":{"execution":{"iopub.status.busy":"2023-04-20T06:58:16.51761Z","iopub.execute_input":"2023-04-20T06:58:16.519423Z","iopub.status.idle":"2023-04-20T06:58:16.553102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Check duplicates\nsum(duplicated(bcp_train))\nsum(duplicated(bcp_test))","metadata":{"execution":{"iopub.status.busy":"2023-04-20T06:58:19.453044Z","iopub.execute_input":"2023-04-20T06:58:19.455328Z","iopub.status.idle":"2023-04-20T06:58:19.852061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bcp_train %>% head()","metadata":{"execution":{"iopub.status.busy":"2023-04-20T06:58:21.083359Z","iopub.execute_input":"2023-04-20T06:58:21.08782Z","iopub.status.idle":"2023-04-20T06:58:21.150363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bcp_test %>% head()","metadata":{"execution":{"iopub.status.busy":"2023-04-20T06:58:22.862645Z","iopub.execute_input":"2023-04-20T06:58:22.864384Z","iopub.status.idle":"2023-04-20T06:58:22.890545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist(bcp_train$age)","metadata":{"execution":{"iopub.status.busy":"2023-04-20T06:58:24.201558Z","iopub.execute_input":"2023-04-20T06:58:24.203243Z","iopub.status.idle":"2023-04-20T06:58:24.281936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Most of the cases are normal or not-malignant cancer. Thus, physicians sometimes overlook cancer.\nlibrary(ggplot2)\n\n# Create a data frame with the class labels\ndata <- data.frame(class = c(rep(\"Total\", nrow(bcp_train)), \n                             rep(\"Malignant Cancer\", sum(bcp_train$cancer == 1)), \n                             rep(\"Invasive Cancer\", sum(bcp_train$cancer == 1 & bcp_train$invasive == 1))))\n\n# Plot the class distribution\nggplot(data, aes(x = class)) + \n  geom_bar(fill = \"steelblue\") + \n  labs(x = \"Class\", y = \"Count\") + \n  ggtitle(\"Class Distribution\")","metadata":{"execution":{"iopub.status.busy":"2023-04-20T06:58:26.009045Z","iopub.execute_input":"2023-04-20T06:58:26.010744Z","iopub.status.idle":"2023-04-20T06:58:26.659814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Around 3000 patient took biopsy and malignant cancer was found from some of them.\n\n# Create a data frame with the class labels\ndata <- data.frame(class = c(rep(\"Biopsy\", sum(bcp_train$biopsy == 1)), \n                             rep(\"Malignant Cancer\", sum(bcp_train$cancer == 1)), \n                             rep(\"Invasive Cancer\", sum(bcp_train$cancer == 1 & bcp_train$invasive == 1))))\n\n# Plot the class distribution\nggplot(data, aes(x = class)) + \n  geom_bar(fill = \"steelblue\") + \n  labs(x = \"Class\", y = \"Count\") + \n  ggtitle(\"Biopsy and Cancer Class Distribution\")","metadata":{"execution":{"iopub.status.busy":"2023-04-20T06:58:28.171618Z","iopub.execute_input":"2023-04-20T06:58:28.173074Z","iopub.status.idle":"2023-04-20T06:58:28.420998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# the number of not-malignant cancer cases from biopsy\nnrow(subset(bcp_train, biopsy == 1 & cancer == 0))","metadata":{"execution":{"iopub.status.busy":"2023-04-20T06:58:30.214447Z","iopub.execute_input":"2023-04-20T06:58:30.216068Z","iopub.status.idle":"2023-04-20T06:58:30.237946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#the number of malignant cancer cases from biopsy\nnrow(subset(bcp_train, biopsy == 1 & cancer == 1))","metadata":{"execution":{"iopub.status.busy":"2023-04-20T06:58:32.217721Z","iopub.execute_input":"2023-04-20T06:58:32.219317Z","iopub.status.idle":"2023-04-20T06:58:32.241232Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#60% of biopsy resulted in not-malignanct cancer\n\n# Create a data frame with the class labels\ndata <- data.frame(class = c(rep(\"Biopsy but Not Malignant\", \n                                 length(bcp_train$biopsy[bcp_train$biopsy == 1 & bcp_train$cancer == 0])), \n                             rep(\"Malignant Cancer\", \n                                 length(bcp_train$cancer[bcp_train$cancer == 1])), \n                             rep(\"Invasive Cancer\", \n                                 length(bcp_train$cancer[bcp_train$cancer == 1 & bcp_train$invasive == 1]))))\n\n# Plot the class distribution\nggplot(data, aes(x = class)) + \n  geom_bar(fill = \"steelblue\") + \n  labs(x = \"Class\", y = \"Count\") + \n  ggtitle(\"Class Distribution for Biopsy and Cancer\") +\n  scale_y_continuous(labels = scales::percent_format(accuracy = 1))","metadata":{"execution":{"iopub.status.busy":"2023-04-20T06:58:33.815181Z","iopub.execute_input":"2023-04-20T06:58:33.81675Z","iopub.status.idle":"2023-04-20T06:58:34.06601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# The not-malignant cancer cases were limited into biopsy cases.\nBCP_train <- subset(bcp_train, biopsy == 1)\nBCP_train <- BCP_train[order(row.names(BCP_train)), ]\nrow.names(BCP_train) <- 1:nrow(BCP_train)\nhead(BCP_train)","metadata":{"execution":{"iopub.status.busy":"2023-04-20T06:58:35.714788Z","iopub.execute_input":"2023-04-20T06:58:35.716397Z","iopub.status.idle":"2023-04-20T06:58:35.765836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#The number of positive (malignant) and negative (not-malignat) cases should be the same to create a balanced dataset.\nlibrary(dplyr)\n\nBCP_train <- BCP_train %>% \n  group_by(cancer) %>% \n  sample_n(1158, replace = TRUE) %>% \n  ungroup() %>% \n  arrange(row.names(.)) %>% \n  mutate(row.names = 1:n())\n\ncat('New Data Size:', nrow(BCP_train), '\\n')","metadata":{"execution":{"iopub.status.busy":"2023-04-20T06:58:37.461684Z","iopub.execute_input":"2023-04-20T06:58:37.463538Z","iopub.status.idle":"2023-04-20T06:58:37.50735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Generally, invasive cancer is confirmed by biopsy, not by mammography.\n# Maybe it is also extremely difficult for AI to detect invasive cancer from mammography.\ndata <- data.frame(class = c(rep(\"Biopsy but Not Malignant\", sum(BCP_train$biopsy == 1 & BCP_train$cancer == 0)),\n                             rep(\"Malignant Cancer\", sum(BCP_train$cancer == 1)),\n                             rep(\"Invasive Cancer\", sum(BCP_train$cancer == 1 & BCP_train$invasive == 1))))\n\nggplot(data, aes(x = class)) +\n  geom_bar() +\n  labs(x = \"Class\", y = \"Count\") +\n  ggtitle(\"Distribution of Classes in BCP_train\")","metadata":{"execution":{"iopub.status.busy":"2023-04-20T06:58:39.723902Z","iopub.execute_input":"2023-04-20T06:58:39.725804Z","iopub.status.idle":"2023-04-20T06:58:39.976073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##Data Visualization##\n#Distinct Values\n\nbcp_train %>% summarise(n_site_id = n_distinct(site_id), \n                        n_patient_id = n_distinct(patient_id),\n                        n_image_id = n_distinct(image_id), \n                        n_laterality = n_distinct(laterality),\n                        n_view = n_distinct(view), \n                        n_age = n_distinct(age),\n                        n_cancer = n_distinct(cancer), \n                        n_biopsy = n_distinct(biopsy),\n                        n_invasive = n_distinct(invasive), \n                        n_BIRADS = n_distinct(BIRADS),\n                        n_implant = n_distinct(implant), \n                        n_density = n_distinct(density), \n                        n_machine_id = n_distinct(machine_id),\n                        n_difficult_negative_case = n_distinct(difficult_negative_case))","metadata":{"execution":{"iopub.status.busy":"2023-04-20T06:58:41.924692Z","iopub.execute_input":"2023-04-20T06:58:41.926451Z","iopub.status.idle":"2023-04-20T06:58:41.974756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bcp_test  %>% summarise(n_site_id = n_distinct(site_id), \n                        n_patient_id = n_distinct(patient_id),\n                        n_image_id = n_distinct(image_id), \n                        n_laterality = n_distinct(laterality),\n                        n_view = n_distinct(view), \n                        n_age = n_distinct(age),\n                        n_implant = n_distinct(implant),\n                        n_machine_id = n_distinct(machine_id),\n                        n_prediction_id = n_distinct(prediction_id))","metadata":{"execution":{"iopub.status.busy":"2023-04-20T06:58:43.503896Z","iopub.execute_input":"2023-04-20T06:58:43.505746Z","iopub.status.idle":"2023-04-20T06:58:43.536631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Converting categorical data to numeric\nbcp_train$laterality <- as.numeric(factor(bcp_train$laterality))\nbcp_train$view <- as.numeric(factor(bcp_train$view))\nbcp_train$difficult_negative_case <- as.numeric(factor(bcp_train$difficult_negative_case))\n\nbcp_train %>% head()","metadata":{"execution":{"iopub.status.busy":"2023-04-20T05:59:12.77161Z","iopub.execute_input":"2023-04-20T05:59:12.773214Z","iopub.status.idle":"2023-04-20T05:59:12.817716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Create path to each image\npath <- \"/kaggle/input/rsna-breast-cancer-detection/train_images\"\nfor (i in 1:nrow(bcp_train)) {\n  bcp_train[i, \"path\"] <- paste(path, \"/\", bcp_train[i, \"patient_id\"], \"/\", bcp_train[i, \"image_id\"], \".dcm\", sep = \"\")\n}\nhead(bcp_train)","metadata":{"execution":{"iopub.status.busy":"2023-04-20T06:59:12.10551Z","iopub.execute_input":"2023-04-20T06:59:12.107373Z","iopub.status.idle":"2023-04-20T06:59:27.371791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#a sample path\nbcp_train[1, \"path\"]","metadata":{"execution":{"iopub.status.busy":"2023-04-20T07:00:18.306964Z","iopub.execute_input":"2023-04-20T07:00:18.308939Z","iopub.status.idle":"2023-04-20T07:00:18.329444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path <- \"/kaggle/input/rsna-breast-cancer-detection/train_images/\"\nfor (i in 1:nrow(BCP_train)) {\n  BCP_train[i, \"path\"] <- paste(path, BCP_train[i, \"patient_id\"], \"/\", BCP_train[i, \"image_id\"], \".png\", sep = \"\")\n}\nhead(BCP_train)\n\n#a sample path\na <- BCP_train[1, \"path\"]\nprint(a)\n\nlibrary(IRdisplay)\nlibrary(png)\n\n# Specify the path to the image file\nimage_path <- a\n\n# Read the image file using the readPNG() function\nimage <- readPNG(image_path)\n\n# Display the image using the display_png() function\ndisplay_png(image)\n\n#/kaggle/input/rsna-breast-cancer-detection/train_images/10006/1459541791.dcm","metadata":{"execution":{"iopub.status.busy":"2023-04-19T17:34:04.383673Z","iopub.execute_input":"2023-04-19T17:34:04.386186Z","iopub.status.idle":"2023-04-19T17:34:05.610909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path <- \"/kaggle/input/rsna-breast-cancer-detection/train_images/\"\nfor (i in 1:nrow(BCP_train)) {\n  BCP_train[i, \"path\"] <- paste(path, BCP_train[i, \"patient_id\"], \"/\", BCP_train[i, \"image_id\"], \".png\", sep = \"\")\n}\nhead(BCP_train)\n\n#a sample path\nimage_path <- BCP_train[1, \"path\"]\nprint(image_path)\n\nlibrary(oro.dicom)\nlibrary(magick)\nlibrary(IRdisplay)\n\n# Specify the path to the DICOM file\n\n# Read the DICOM file using the readDICOMFile() function\nimage <- readDICOMFile(image_path)\n\n# Convert the image to a matrix and set the dimensions\nimage_matrix <- image$pixels\ndim(image_matrix) <- c(image$dim[2], image$dim[1])\n\n# Convert the matrix to a grayscale image using the magick package\nimage_gray <- image_data(image_matrix)\nimage_gray <- image_convert(image_gray, colorspace = \"gray\")\n\n# Display the image using the display_png() function\ndisplay_png(as.array(image_gray))","metadata":{"execution":{"iopub.status.busy":"2023-04-19T17:36:26.638155Z","iopub.execute_input":"2023-04-19T17:36:26.640318Z","iopub.status.idle":"2023-04-19T17:36:27.816369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(oro.dicom)\nlibrary(magick)\nlibrary(IRdisplay)\n\n# Specify the path to the DICOM file\nimage_path <- \"/kaggle/input/rsna-breast-cancer-detection/train_images/10006/1459541791.dcm\"\n\n# Read the DICOM file using the readDICOMFile() function\nimage <- readDICOMFile(image_path)\n\n# Convert the image to a matrix and set the dimensions\nimage_matrix <- image$pixels\ndim(image_matrix) <- c(image$dim[2], image$dim[1])\n\n# Convert the matrix to a grayscale image using the magick package\nimage_gray <- image_data(image_matrix)\nimage_gray <- image_convert(image_gray, colorspace = \"gray\")\n\n# Display the image using the display_png() function\ndisplay_png(as.array(image_gray))\n","metadata":{"execution":{"iopub.status.busy":"2023-04-19T17:37:40.982979Z","iopub.execute_input":"2023-04-19T17:37:40.984764Z","iopub.status.idle":"2023-04-19T17:37:41.247953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(oro.dicom)\nslice=readDICOM(\"/kaggle/input/rsna-breast-cancer-detection/train_images/10006/1459541791.dcm\")","metadata":{"execution":{"iopub.status.busy":"2023-04-19T17:57:50.954159Z","iopub.execute_input":"2023-04-19T17:57:50.956186Z","iopub.status.idle":"2023-04-19T17:57:51.571076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(oro.nifti)\nlibrary(neurobase)\nlibrary(ggplot2)\nlibrary(fslr)\nlibrary(oro.dicom)\nlibrary(data.table)\nlibrary(imager)\nlibrary(plyr)\nlibrary(magrittr)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T17:59:25.152339Z","iopub.execute_input":"2023-04-19T17:59:25.154141Z","iopub.status.idle":"2023-04-19T17:59:26.376263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_dcmfile <- readDICOMFile(\"/kaggle/input/rsna-breast-cancer-detection/train_images/10006/1459541791.dcm\")\nsummary(random_dcmfile)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T18:00:46.791564Z","iopub.execute_input":"2023-04-19T18:00:46.793518Z","iopub.status.idle":"2023-04-19T18:00:46.948042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"system(\"ls /kaggle/input/rsna-breast-cancer-detection/test_images\", intern = TRUE)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T18:08:35.884631Z","iopub.execute_input":"2023-04-19T18:08:35.886508Z","iopub.status.idle":"2023-04-19T18:08:35.915396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"system(\"ls /kaggle/input/rsna-breast-cancer-detection/train_images/10008\", intern = TRUE)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T18:08:58.604977Z","iopub.execute_input":"2023-04-19T18:08:58.60699Z","iopub.status.idle":"2023-04-19T18:08:58.631531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"system(\"ls /kaggle/input/rsna-breast-cancer-detection/train_images/10006/1459541791.dcm\", intern = TRUE)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T18:08:28.473116Z","iopub.execute_input":"2023-04-19T18:08:28.475135Z","iopub.status.idle":"2023-04-19T18:08:28.501331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_image <- readDICOMFile(\"/kaggle/input/rsna-breast-cancer-detection/test_images/10008/1591370361.dcm\")\ndim(sample_image$hdr)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T18:09:15.693925Z","iopub.execute_input":"2023-04-19T18:09:15.696169Z","iopub.status.idle":"2023-04-19T18:09:15.82602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load required libraries\nlibrary(ggplot2)\nlibrary(reshape2)\n\n# Compute correlation matrix\ncorr_train <- cor(bcp_train)\n\n# Create heatmap plot\nggplot(melt(corr_train), aes(x=Var1, y=Var2, fill=value)) +\n  geom_tile() +\n  scale_fill_gradient2(low=\"blue\", high=\"red\", mid=\"white\",\n                       midpoint=0, limit=c(-1,1), space=\"Lab\", \n                       name=\"Pearson\\nCorrelation\") +\n  theme_minimal() +\n  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +\n  labs(title=\"Correlation Matrix\")","metadata":{"execution":{"iopub.status.busy":"2023-04-19T19:12:20.467269Z","iopub.execute_input":"2023-04-19T19:12:20.468933Z","iopub.status.idle":"2023-04-19T19:12:20.760423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class(bcp_train)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T19:13:26.263955Z","iopub.execute_input":"2023-04-19T19:13:26.266553Z","iopub.status.idle":"2023-04-19T19:13:26.290987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bcp_train <- as.matrix(bcp_train)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T19:15:03.145403Z","iopub.execute_input":"2023-04-19T19:15:03.147047Z","iopub.status.idle":"2023-04-19T19:15:03.160877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class(bcp_train)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T19:15:05.116072Z","iopub.execute_input":"2023-04-19T19:15:05.117626Z","iopub.status.idle":"2023-04-19T19:15:05.138098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"corr_train <- cor(bcp_train)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T19:15:23.272719Z","iopub.execute_input":"2023-04-19T19:15:23.275502Z","iopub.status.idle":"2023-04-19T19:15:23.29708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load required libraries\nlibrary(ggplot2)\nlibrary(reshape2)\n\n# Compute correlation matrix\ncorr_train <- cor(bcp_train)\n\n# Create heatmap plot\nggplot(melt(corr_train), aes(x=Var1, y=Var2, fill=value)) +\n  geom_tile() +\n  scale_fill_gradient2(low=\"blue\", high=\"red\", mid=\"white\",\n                       midpoint=0, limit=c(-1,1), space=\"Lab\", \n                       name=\"Pearson\\nCorrelation\") +\n  theme_minimal() +\n  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +\n  labs(title=\"Correlation Matrix\")","metadata":{"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(EBImage)\nlibrary(ggplot2)\nlibrary(tidyverse)\nlibrary(psych)\n\n# Specify the path to the image file\nimage_path <- BCP_train[6,\"path\"]\n\n# Read the image file using the readImage() function\nimage <- readImage(image_path)\n\n# Display the image\ndisplay(image)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T17:28:46.358991Z","iopub.execute_input":"2023-04-19T17:28:46.360746Z","iopub.status.idle":"2023-04-19T17:28:46.394432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_list <- c()\ntrain_path <- \"/kaggle/input/rsna-breast-cancer-detection/train_images\"\nfolder_list <- list.files(train_path)\nfor (folder in folder_list) {\n  files <- list.files(file.path(train_path, folder))\n  file_list <- c(file_list, gsub(\"\\\\.dcm\", \"\", files))\n}\ncat(paste(length(folder_list), length(file_list), sep=\" \"))","metadata":{"execution":{"iopub.status.busy":"2023-04-19T07:41:59.252595Z","iopub.execute_input":"2023-04-19T07:41:59.25822Z","iopub.status.idle":"2023-04-19T07:43:44.425473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"diff <- setdiff(folder_list, as.character(unique(bcp_train$patient_id)))\ncat(\"Differences in patient/folder list: \", length(diff), \"\\n\")\ndiff <- setdiff(file_list, as.character(unique(bcp_train$image_id)))\ncat(\"Differences in image list: \", length(diff))","metadata":{"execution":{"iopub.status.busy":"2023-04-19T07:43:44.427925Z","iopub.execute_input":"2023-04-19T07:43:44.429398Z","iopub.status.idle":"2023-04-19T07:43:44.490408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"laterality_counts <- table(bcp_train$laterality)\nlaterality_counts","metadata":{"execution":{"iopub.status.busy":"2023-04-19T07:47:07.948913Z","iopub.execute_input":"2023-04-19T07:47:07.950853Z","iopub.status.idle":"2023-04-19T07:47:08.029466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"library(oro.dicom)\n\nextract_dicom_data <- function(data_path, patient_id) {\n  images_path <- file.path(data_path, patient_id)\n  for (image in list.files(images_path)) {\n    image_id <- gsub(\"\\\\.dcm\", \"\", image)\n    image_path <- file.path(images_path, image)\n    data_row_img_data <- readDICOMFile(image_path)\n    cat(\"=================================================\\n\")\n    cat(sprintf(\"Patient: %s Image_id: %s\\n\", patient_id, image_id))\n    cat(\"=================================================\\n\")\n    print(data_row_img_data)\n    cat(\"=================================================\\n\\n\")\n  }\n}\n","metadata":{"execution":{"iopub.status.busy":"2023-04-19T07:47:10.575644Z","iopub.execute_input":"2023-04-19T07:47:10.577169Z","iopub.status.idle":"2023-04-19T07:47:10.609294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient_id <- \"10006\"\ntrain_path <- \"/kaggle/input/rsna-breast-cancer-detection/train_images\"\n\nextract_dicom_data(train_path, patient_id)","metadata":{"execution":{"iopub.status.busy":"2023-04-19T07:47:30.54459Z","iopub.execute_input":"2023-04-19T07:47:30.546427Z","iopub.status.idle":"2023-04-19T07:47:31.098131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#DICOM Data processing\nlibrary(raster)\nlibrary(dcmr)\n\nprocess_dicom_data <- function(data_path, patient_id, dicom_features){\n  images_path <- file.path(data_path, as.character(patient_id))\n  for(image in list.files(images_path)){\n    tryCatch({\n      image_id <- gsub(\".dcm\", \"\", image)\n      image_path <- file.path(images_path, image)\n      data_row_img_data <- dicom.read_file(image_path)\n      rows <- data_row_img_data[[\"Rows\"]]\n      columns <- data_row_img_data[[\"Columns\"]]\n      content_date <- data_row_img_data[[\"ContentDate\"]]\n      photometric_interpretation <- data_row_img_data[[\"PhotometricInterpretation\"]]\n      dicom_features[[length(dicom_features)+1]] <- list(image_id, rows, columns, content_date, photometric_interpretation)\n    }, error = function(e) {\n      message(e)\n    })\n  }\n  return(dicom_features)\n}","metadata":{"execution":{"iopub.status.busy":"2023-04-19T07:47:33.221343Z","iopub.execute_input":"2023-04-19T07:47:33.222888Z","iopub.status.idle":"2023-04-19T07:47:38.739637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Sys.glob(\"/kaggle/input/rsna-breast-cancer-detection/train_images/*/*.dcm\")","metadata":{"execution":{"iopub.status.busy":"2023-04-18T11:29:56.514513Z","iopub.execute_input":"2023-04-18T11:29:56.516134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Sys.glob(\"/kaggle/input/rsna-breast-cancer-detection/test_images/*/*.dcm\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = Sys.glob(\"/kaggle/input/rsna-breast-cancer-detection/train_images/*/*.dcm\")\ntest_images = Sys.glob(\"/kaggle/input/rsna-breast-cancer-detection/test_images/*/*.dcm\")","metadata":{"execution":{"iopub.status.busy":"2023-04-18T08:25:24.555227Z","iopub.execute_input":"2023-04-18T08:25:24.557045Z","iopub.status.idle":"2023-04-18T08:25:31.940471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"length(train_images)  #54706","metadata":{"execution":{"iopub.status.busy":"2023-04-18T08:25:47.380769Z","iopub.execute_input":"2023-04-18T08:25:47.383151Z","iopub.status.idle":"2023-04-18T08:25:47.406111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"length(test_images)  #4","metadata":{"execution":{"iopub.status.busy":"2023-04-18T08:25:48.280598Z","iopub.execute_input":"2023-04-18T08:25:48.282592Z","iopub.status.idle":"2023-04-18T08:25:48.301315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Merge the image data with the diagnosis labels\n#data <- inner_join(bcp_train, image_data, by = c(\"id\" = \"image\"))\n\n# Data preprocessing\n#preprocess <- preProcess(trainData[,4:ncol(trainData)], method = c(\"center\", \"scale\"))\n#trainData[,4:ncol(trainData)] <- predict(preprocess, trainData[,4:ncol(trainData)])\n#testData[,4:ncol(testData)] <- predict(preprocess, testData[,4:ncol(testData)])","metadata":{"execution":{"iopub.status.busy":"2023-04-18T08:31:53.873127Z","iopub.execute_input":"2023-04-18T08:31:53.874888Z","iopub.status.idle":"2023-04-18T08:31:53.887734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# # # Modelling","metadata":{}},{"cell_type":"code","source":"library(randomForest)\nlibrary(kknn)\nlibrary(naivebayes)\nlibrary(nnet)\nlibrary(caret)\nlibrary(keras)\n\n# Split the data\nset.seed(123)\ntrain_index <- createDataPartition(data$diagnosis, p = 0.8, list = FALSE)\ntrain_data <- bcp_train[train_index, -1]\ntest_data <- bcp_train[-train_index, -1]\ntrain_labels <- bcp_train[train_index, 1]\ntest_labels <- bcp_train[-train_index, 1]\n\n# Random forest model\nrf_model <- randomForest(train_data, train_labels, ntree = 100)\nrf_pred <- predict(rf_model, test_data)\nrf_acc <- confusionMatrix(rf_pred, test_labels)$overall[1]\ncat(\"Random forest accuracy:\", rf_acc, \"\\n\")\n\n# SVM model\nsvm_model <- svm(train_data, train_labels)\nsvm_pred <- predict(svm_model, test_data)\nsvm_acc <- confusionMatrix(svm_pred, test_labels)$overall[1]\ncat(\"SVM accuracy:\", svm_acc, \"\\n\")\n\n# KNN model\nknn_model <- kknn(diagnosis ~ ., train_data, train_labels, k = 5)\nknn_pred <- predict(knn_model, test_data)\nknn_acc <- confusionMatrix(knn_pred, test_labels)$overall[1]\ncat(\"KNN accuracy:\", knn_acc, \"\\n\")\n\n# Logistic regression model\nglm_model <- glm(diagnosis ~ ., data = train_data, family = \"binomial\")\nglm_pred <- predict(glm_model, test_data, type = \"response\")\nglm_pred[glm_pred >= 0.5] <- \"M\"\nglm_pred[glm_pred < 0.5] <- \"B\"\nglm_acc <- confusionMatrix(glm_pred, test_labels)$overall[1]\ncat(\"Logistic regression accuracy:\", glm_acc, \"\\n\")\n\n# Naive Bayes model\nnb_model <- naiveBayes(diagnosis ~ ., data = train_data)\nnb_pred <- predict(nb_model, test_data)\nnb_acc <- confusionMatrix(nb_pred, test_labels)$overall[1]\ncat(\"Naive Bayes accuracy:\", nb_acc, \"\\n\")\n\n# CNN model\nx_train <- as.matrix(train_data)\nx_test <- as.matrix(test_data)\ny_train <- as.numeric(train_labels == \"M\")\ny_test <- as.numeric(test_labels == \"M\")\nx_train <- array_reshape(x_train, c(nrow(x_train), ncol(x_train), 1))\nx_test <- array_reshape(x_test, c(nrow(x_test), ncol(x_test), 1))\nmodel <- keras_model_sequential() %>%\n  layer_conv_2d(filters = 32, kernel_size = c(3, 3), activation = \"relu\", input_shape = c(ncol(x_train), 1)) %>%\n  layer_max_pooling_2d(pool_size = c(2, 2)) %>%\n  layer_dropout(rate = 0.25) %>%\n  layer_flatten() %>%\n  layer_dense(units = 128, activation = \"relu\") %>%\n  layer_dropout(rate = 0.5) %>%\n  layer_dense(units = 1, activation = \"sigmoid\")\nmodel %>% compile(\n  loss = \"binary_crossentropy\",\n  optimizer = \"adam\",\n  metrics = \"accuracy\"\n)\nmodel %>% fit(\n  x_train, y_train,\n  epochs = 10,\n  batch_size = 32,\n  validation_data = list(x_test,","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Trial","metadata":{}},{"cell_type":"code","source":"# Check the structure of the dataset\nstr(data)\n\n# Check the summary of the dataset\nsummary(data)","metadata":{"execution":{"iopub.status.busy":"2023-04-18T06:48:13.419942Z","iopub.execute_input":"2023-04-18T06:48:13.538407Z","iopub.status.idle":"2023-04-18T06:48:13.839772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}