library(readr) library(ggplot2) library(tidyr) library(purrr) library(reshape2) library(Tplyr) library(stringr) library(rlang) library(tidystats) library(dplyr) library(lazyeval) library(corrplot) GGMMYY<-"..." #to be changed according to MPs group/ file name myPath <- "..." #your path for saving folders #change with your output_data path batch<-"batch1" ```{r verify reference} tempR = as.vector(list.files(paste0(myPath,GGMMYY), pattern ="areaREF.csv")) #data from ImageJ, "..." your data folder #tempR <- tempR[str_detect(tempR, batch)] namesR<- paste0(myPath,GGMMYY,"/",tempR) myfilesR = lapply(namesR, read.csv) DF_RL<-lapply(myfilesR, "[", c(2,3)) DF_R<-bind_rows(DF_RL, .id = "picture_number") SMZR<- melt(DF_R,"picture_number")%>%group_by(variable) SMZR<-summarise(SMZR,mean = mean(value),max=max(value),min=min(value), D = (max(value)-min(value))) SMZR<-cbind(SMZR[,1],(round(SMZR[,2:5],digits=3))) write.csv(SMZR,paste0(myPath,GGMMYY,"/",GGMMYY,batch,"Reference.csv")) #depends on your saving folder rm(SMZR,DF_RL,DF_R,namesR,tempR,myfilesR) ``` ```{r load files of results from ImageJ and prepare file list} temp = list.files(paste0(myPath,GGMMYY), pattern = "part.csv") names<- paste0(paste0(myPath,GGMMYY,"/",temp)) #prepare files giving an individual ID to each particle myfiles = lapply(names, read.csv) for (i in 1:length(myfiles)){ myfiles[[i]]$ID_list_row=paste0("p_",i,"_",seq.int(1:nrow(myfiles[[i]]))) } # Remove column by name in each element of the list ToRemove<-c("Angle", "FeretX", "FeretY","FeretAngle","MinFeret", "BX","BY","Width","Height") myfiles = map(myfiles, ~ (.x %>% select(-all_of(ToRemove)))) rm(ToRemove,i) for (i in 1:length(myfiles)){ myfiles[[i]] <-myfiles[[i]][,c(11,4,5,2,3,6,7,8,9,10)] } rm(i,names,temp) ``` ```{r filter list} #from records with area = 0 myfiles_f<-list() df_f<-data.frame() for (i in 1:length(myfiles)){ df_f<-myfiles[[i]] df_f<-df_f[df_f$Area <= 0.5,] #to remove background noise, keeping possible fragmented particles. Use 0.5 for S25,P25. For all others use 0.05 if(nrow(df_f)>0){ myfiles_f[[i]] <- myfiles[[i]][ ! myfiles[[i]]$ID_list_row %in% df_f$ID_list_row, ] }else{ myfiles_f[[i]]<-myfiles[[i]] } } rm(df_f) rm(i) ``` ```{r from wide to long data} DF_long_f<-fun_long(myfiles_f,names_pl ="PARAM",values_pl="measurement",from_pl = "Major",to_pl = "Solidity") ``` ```{r models} #NB change the Tanoiri function if studying primary or secondary MPs DFL_mod<- list() for (i in 1:length(myfiles_f)){ myfiles_f[[i]]$ID_list_row=paste0("p_",i,"_",seq.int(1:nrow(myfiles_f[[i]]))) } RT<-list() for (i in 1:length(myfiles_f)){ RT[[i]]<-median(myfiles_f[[i]]$Minor/myfiles_f[[i]]$Major) DFL_mod[[i]]<-round(as.data.frame(cbind(fun_medina(myfiles_f[[i]]$Area),fun_simon(myfiles_f[[i]]$Major, myfiles_f[[i]]$Minor,RT[[i]]), fun_cozar(myfiles_f[[i]]$Major),fun_isobe(myfiles_f[[i]]$Major), fun_Tanoiri_2(myfiles_f[[i]]$Major,myfiles_f[[i]]$Minor), fun_Barchiesi(myfiles_f[[i]]$Major,myfiles_f[[i]]$Minor,RT[[i]],myfiles_f[[i]]$Perim.))),digits = 3) a=myfiles_f[[i]]$ID_list_row DFL_mod[[i]]$ID_list_row<-a DFL_mod[[i]]$ID<-paste0("p_",i) DFL_mod[[i]]<-DFL_mod[[i]][,c(8,7,1,2,3,4,5,6)] colnames(DFL_mod[[i]])<-c("ID","ID_list_row","Medina_V","Simon_V","Cozar_V","Isobe_V","Tanoiri_V","Mod_new") } ``` ```{r, verify total} SUM_mod<-list() LabData <- read_csv(paste0("...",GGMMYY,"batch1LAB_DATA.txt")) # "..." depends on your input data folder (where the volume data is stored) SUM_mod<-list() for (i in 1:length(DFL_mod)){ SUM_mod[[i]]<-round(colSums(DFL_mod[[i]][,c(3:8)])/1000, digits = 3)#units of measurements from cmc to mmc SUM_mod[[i]]<-as.data.frame(SUM_mod[[i]]) } SUM_modT <- do.call("cbind",SUM_mod) SUM_modT$Rmean <- round(rowMeans(SUM_modT), digits= 3) D_mod_data_rel <-round((as.vector(SUM_modT$Rmean)/as.numeric(LabData[4,2])),digits=2) SUM_modT<-cbind(SUM_modT,D_mod_data_rel) colnames(SUM_modT)<-c("ID_1","ID_2","ID_3","Rmean") write.csv(SUM_modT,paste0(myPath,GGMMYY,"/",GGMMYY,"_Comparison_model_data_mod.csv")) #"..." your output folder rm(i,D_mod_data_rel) #rm(LabData) ``` ```{r, RSS} opt_m<-list() LDM<-as.numeric(LabData[4,2]) for(i in 1:3){ opt_m[[i]]<-OPTI_m(0.02,myfiles_f[[i]]$Major,myfiles_f[[i]]$Minor,LDM) } opt_m<-as.data.frame(do.call(cbind,opt_m)) row.names(opt_m)<-c("OPT_PAR_m","RSS_m") colnames(opt_m)<-c("B1","B2","B3") opt_M<-list() for(i in 1:3){ opt_M[[i]]<-OPTI_M(0.002,myfiles_f[[i]]$Major,myfiles_f[[i]]$Minor,LDM) } opt_M<-as.data.frame(do.call(cbind,opt_M)) row.names(opt_M)<-c("OPT_PAR_M","RSS_M") colnames(opt_M)<-c("B1","B2","B3") opt_C<-list() for(i in 1:3){ opt_C[[i]]<-OPTI_COZAR(0.1,myfiles_f[[i]]$Major,LDM) } opt_C<-as.data.frame(do.call(cbind,opt_C)) row.names(opt_C)<-c("OPT_PAR_C","RSS_C") colnames(opt_C)<-c("B1","B2","B3") opt_I<-list() for(i in 1:3){ opt_I[[i]]<-OPTI_ISOBE(0.4,myfiles_f[[i]]$Major,LDM) } opt_I<-as.data.frame(do.call(cbind,opt_I)) row.names(opt_I)<-c("OPT_PAR_I","RSS_I") colnames(opt_I)<-c("B1","B2","B3") #opt_B<-list() #for(i in 1:3){ #opt_B[[i]]<-OPTI_B(0.4,myfiles_f[[i]]$Major,myfiles_f[[i]]$Minor,myfiles_f[[i]]$Perim.,LDM) #} #opt_B<-as.data.frame(do.call(cbind,opt_B)) #row.names(opt_B)<-c("OPT_PAR_B","RSS_B") #colnames(opt_B)<-c("B1","B2","B3") OPT_tot<-rbind(opt_B,opt_m,opt_M,opt_C,opt_I) write.csv(OPT_tot,paste0(".../RSS_models_assessment/OPT_results/",GGMMYY,"OPT_ToT.csv"))#change according to your folders/path ```