{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"library(rlang) # attach rlang first since it is listed in namespace, not attached, and is dependency for torch\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"install.packages('torch', dependencies = TRUE) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"library(torch) # will load additional on first use, similar to keras/tensorflow dance","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"?torch_mean","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"library(imager)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"install.packages('imager', dependencies = TRUE)"},{"metadata":{"trusted":true},"cell_type":"code","source":"library(oro.dicom);\nlibrary(oro.nifti);\nlibrary(neurobase)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"install.packages(c('oro.dicom', 'oro.nifti', 'neurobase'), dependencies = TRUE)"},{"metadata":{"trusted":true},"cell_type":"markdown","source":"Going to use some base r to approach heart segmentation per Larrey-Ruiz[2014], with apologies to same."},{"metadata":{"trusted":true},"cell_type":"code","source":"p001_dicom <- oro.dicom::readDICOM('../input/pulmonary-embolism-in-ct-images/FUMPE/CT_scans/PAT001/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"p001_ni <- dicom2nifti(p001_dicom, rescale = TRUE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"str(p001_ni)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"hist(p001_ni@.Data)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"p001_mu_vec <- apply(p001_ni@.Data,3,mean)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"str(p001_mu_vec)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"p001_mu_mtx_lst <- list()\nfor (j in 1:length(p001_mu_vec)) {\n    p001_mu_mtx_lst[[j]] <- matrix(p001_mu_vec[j],nrow = dim(p001_ni@.Data)[1], ncol = dim(p001_ni@.Data)[2])\n    }\np001_mu_arr <- array(unlist(p001_mu_mtx_lst),c(dim(p001_ni@.Data)[1], dim(p001_ni@.Data)[2],dim(p001_ni@.Data)[3]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"hist(p001_ni@.Data[,,1])\nabline(v = p001_mu_vec[1], col = 'red')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"p001_mu2_gt_mu_vec <- vector(mode='numeric', length=dim(p001_ni@.Data)[3])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for(j in 1:dim(p001_ni@.Data)[3]) {\n    p001_mu2_gt_mu_vec[j] <- sum(which(p001_ni@.Data[,,j] > p001_mu_vec[j], arr.ind = TRUE))/length(which(p001_ni@.Data[,,j] > p001_mu_vec[j], arr.ind = TRUE)[,1])+p001_mu_vec[j]\n}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"p001_mu2_gt_mu_mtx_lst <- list()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for (j in 1:dim(p001_ni@.Data)[3]) {\n    p001_mu2_gt_mu_mtx_lst[[j]] <- matrix(p001_mu2_gt_mu_vec[j], nrow = dim(p001_ni@.Data)[1], ncol = dim(p001_ni@.Data)[2])\n    }","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"p001_mu2_gt_mu_arr <- array(unlist(p001_mu2_gt_mu_mtx_lst),c(dim(p001_ni@.Data)[1], dim(p001_ni@.Data)[2], dim(p001_ni@.Data)[3]))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now we want the sd around this mu2, which I find conceptually easier as vectors. Basically, the part below mu is now annoying, so take slices to vectors, index < mu, drop values < mu, sd remaining values and make -/+ arrays of the sd."},{"metadata":{"trusted":true},"cell_type":"code","source":"get_sd <- function(nifti) {\n    vec_lst <- list()\n    idx_lst <- list()\n    gt_mu_lst <- list()\n    sd_lst <- list()\n    for (j in 1:dim(nifti@.Data)[3]) {\n        vec_lst[[j]] <- c(nifti@.Data[,,j])\n        }\n    for (j in 1:length(vec_lst)) {\n        idx_lst[[j]] <- which(vec_lst[[j]] < p001_mu_vec[j])\n        }\n    for (j in 1:length(idx_lst)) {\n        gt_mu_lst[[j]] <- vec_lst[[j]][-idx_lst[[j]]]\n        }\n    for(j in 1:length(gt_mu_lst)) {\n        sd_lst[[j]] <- sd(gt_mu_lst[[j]])\n        }\n    return(unlist(sd_lst))\n    }","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"p001_sd_mu2 <- get_sd(p001_ni)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"p001_sd_mtx_lst <- list()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for (j in 1:dim(p001_ni@.Data)[3]){\n    p001_sd_mtx_lst[[j]] <- matrix(p001_sd_mu2[j], nrow = dim(p001_ni@.Data)[1], ncol = (p001_ni@.Data)[2])\n    }","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"p001_sd_arr <- array(unlist(p001_sd_mtx_lst), c(dim(p001_ni@.Data)[1], dim(p001_ni@.Data)[2], dim(p001_ni@.Data)[3]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"p001_mu2_plus_sd <- p001_mu2_gt_mu_arr + p001_sd_arr\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"p001_global <- p001_mu2_gt_mu_arr - p001_sd_arr","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"str(p001_global)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"hist(p001_ni@.Data[,,1])\nabline(v = p001_mu_arr[1,1,1], col = 'red')\nabline(v = p001_mu2_gt_mu_arr[1,1,1], col = 'blue')\nabline(v= p001_mu2_plus_sd[1,1,1], col = 'green')\nabline(v = p001_global[1,1,1], col='yellow')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"So between yellow and green, sweet spot for heart tissues"},{"metadata":{"trusted":true},"cell_type":"code","source":"neurobase::ortho2(p001_ni, p001_ni > p001_mu2_gt_mu_arr & p001_ni < p001_mu2_plus_sd)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true},"cell_type":"code","source":"ls()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"str(p001_global)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"p001_gt_global_logi_arr <- p001_global < p001_ni","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"p001_global_all_tru_mtx <- apply(p001_gt_global_logi_arr@.Data, 1:2, sum)==dim(p001_ni@.Data)[3]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"which(p001_global_all_tru_mtx == TRUE, arr.ind = TRUE)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"p001_global_all_tru_mtx_lst <- list()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for (j in 1:dim(p001_ni@.Data)[3]) {\np001_global_all_tru_mtx_lst[[j]] <- p001_global_all_tru_mtx[j]\n}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"p001_global_all_tru_ni <- niftiarr(p001_ni, array(unlist(p001_global_all_tru_mtx), c(dim(p001_ni@.Data)[1], dim(p001_ni@.Data)[2], dim(p001_ni@.Data)[3]))\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"p001_global_mask <- mask_img(p001_ni, p001_global_all_tru_ni)\northo2(p001_ni, p001_global_mask)\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"looking like a spine; and the torch approach, perhaps this afternoon."}],"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":"3.6.3"}},"nbformat":4,"nbformat_minor":4}