R/Rread_block.R

Defines functions get_plateaux_range get_fit_n get_block_n read_df

Documented in get_block_n get_fit_n get_plateaux_range read_df

library(dplyr)


#' read a data frame from a file with maximum 20 column
#'
#' This function allows you read a file in gnuplot style,
#'  it enumerates the file with  blocks.
#'  Each block is a chunk of data separated by an empty line
#' @param file connection to a file or string with the name .
#' @details  Important it allows only for 20 columns 
#' @export
#' @examples read_df(file)

read_df<- function(file){
  columns_file<-c(1:20)  
  lc<- length(columns_file)+1
  
  all_file <- read.table(file,header=FALSE,fill = TRUE ,
                         blank.lines.skip=FALSE,skip=0, comment.char = "", 
                         col.names = columns_file)
  # get the blank lines
  #add to the list an extra fictitious blank line
  ix <- c(which(all_file[,1]==""),nrow(all_file) )
  # get the blank line after one blank line
  ixp <- c(which(all_file[ix+1,1]=="")  )
  #ixp <- c(0,which(all_file[ix+1,1]=="") )
  ixm<- c(0,ix[-ixp])
  # enumerate  with blocks 
  iblock<-rep(1:length(diff(ixm)),diff(ixm))
  #m <- cbind(all_file,rep(1:length(diff(ix)),diff(ix)))
  #remove blank lines
  #m <- m[!(m[,1]==""),]

  mt<-cbind("index"=iblock, all_file)
  mt <- mt[!(mt[,2]==""),]
  if (mt[1,1]!=1)
    mt[,1]<-mt[,1]-mt[1,1]+1
  return(mt)
}


#' get a block of data
#'
#' This function allows you read a file in gnuplot style,
#'  it enumerates the file with  blocks.
#'  Each block is a chunk of data separated by an empty line
#' @param df data frame constructed with read_df
#' @param n index of the block of data 
#' @details  Important it allows only for 20 columns 
#' @export
#' @examples get_block_n(df,n)

get_block_n<- function(df,n){
  bo<-which(df[,1]==(n*2-1))
  data <- df[bo,-1]
  if (!is_empty(grep("^#", data[,1]) ) ) {
    data<-data[-grep("^#", data[,1])  ,  ]
  }
  data<-mutate_all(data, function(x) as.numeric(as.character(x)))
  return(data)
}


#' get fit result 
#'
#' This function allows you read a file in gnuplot style,
#'  Each block is a chunk of data separated by an empty line
#' @param df data frame constructed with read_df
#' @param n index of the block of data 
#' @details  Important it allows only for 20 columns 
#' @export
#' @examples get_fit_n(df,n)

get_fit_n<- function(df,n){
  be<-which(df[,1]==(n*2))
  data <- df[be,-1]
  if (!is_empty(grep("^#", data[,1]) ) ){
    data<-data[-grep("^#", data[,1])  ,  ]
  }
  data<-mutate_all(data, function(x) as.numeric(as.character(x)))
  return(data)
}


#' get plateaux range result 
#'
#' This function allows you read a file in gnuplot style,
#'  Each block is a chunk of data separated by an empty line
#' @param df data frame constructed with read_df
#' @param n index of the block of data 
#' @details  Important it allows only for 20 columns 
#' @export
#' @examples get_plateaux_range(df,n)

get_plateaux_range<-function(df,n){
  l<-grep("fit",df[,3])
  a1<-gsub("\\[","c\\(", df[l,5][n])
  a2<-gsub("\\]","\\)", a1)
  fit_range <- eval(parse(text=a2))
  
  return(fit_range)
}

#  dir <- "/home/marco/analysis/phi4/tuning_masses/out" 
# # #dir <- "Data" 
# # 
#  file=sprintf("%s/G2t_T%d_L%d_msq0%.6f_msq1%.6f_l0%.6f_l1%.6f_mu%.6f_g%.6f_rep%d_meff_correlators",
#               dir, 128, 20,  -4.925,  -4.85,  2.5,  2.5,  5,  0,  0)
#  mt<-read_df(file)
#  file=sprintf("%s/G2t_T%d_L%d_msq0%.6f_msq1%.6f_l0%.6f_l1%.6f_mu%.6f_g%.6f_rep%d_output",
#               dir, 48, 20,  -4.925,  -4.85,  2.5,  2.5,  5,  0,  0)
#  
#  mt<-read_df(file)
Marcogarofalo/Rose_vecchie documentation built on Feb. 13, 2021, 12:49 a.m.