Commit 1f2b1b8e9a29717a4ed87c2242513b8227b14c00

Authored by Karol Niewiadomski
0 parents
Exists in master

First commit

.Rbuildignore 0 → 100644
  1 +++ a/.Rbuildignore
... ... @@ -0,0 +1,2 @@
  1 +^ExperimentsPackage\.Rproj$
  2 +^\.Rproj\.user$
... ...
.Rhistory 0 → 100644
  1 +++ a/.Rhistory
... ... @@ -0,0 +1,16 @@
  1 +devtools::check()
  2 +devtools::use_package()
  3 +roxygen2::roxygenise()
  4 +roxygen2::roxygenise()
  5 +usethis::use_package("dplyr")
  6 +usethis::use_package("feather")
  7 +usethis::use_package("tibble")
  8 +usethis::use_package("purrr")
  9 +usethis::use_package("stringr")
  10 +devtools::check()
  11 +devtools::check()
  12 +devtools::check()
  13 +devtools::check()
  14 +devtools::check()
  15 +devtools::build()
  16 +devtools::install()
... ...
.gitignore 0 → 100644
  1 +++ a/.gitignore
... ... @@ -0,0 +1 @@
  1 +.Rproj.user
... ...
DESCRIPTION 0 → 100644
  1 +++ a/DESCRIPTION
... ... @@ -0,0 +1,22 @@
  1 +Package: ExperimentsPackage
  2 +Title: Useful functions for data analysis of UZ experiments.
  3 +Version: 0.0.0.9000
  4 +Authors@R:
  5 + person(given = "Karol",
  6 + family = "Niewiadomski",
  7 + role = c("aut", "cre"),
  8 + email = "karol.niewiadomski@nottingham.ac.uk",
  9 + comment = c(ORCID = "YOUR-ORCID-ID"))
  10 +Description: This package collects useful functions for the data analysis of experiments prepared at University of Zielona Góra.
  11 +License: `use_mit_license()`, `use_gpl3_license()` or friends to
  12 + pick a license
  13 +Encoding: UTF-8
  14 +LazyData: true
  15 +Roxygen: list(markdown = TRUE)
  16 +RoxygenNote: 7.1.0
  17 +Imports:
  18 + dplyr,
  19 + feather,
  20 + tibble,
  21 + purrr,
  22 + stringr
... ...
ExperimentsPackage.Rproj 0 → 100644
  1 +++ a/ExperimentsPackage.Rproj
... ... @@ -0,0 +1,21 @@
  1 +Version: 1.0
  2 +
  3 +RestoreWorkspace: No
  4 +SaveWorkspace: No
  5 +AlwaysSaveHistory: Default
  6 +
  7 +EnableCodeIndexing: Yes
  8 +UseSpacesForTab: Yes
  9 +NumSpacesForTab: 2
  10 +Encoding: UTF-8
  11 +
  12 +RnwWeave: Sweave
  13 +LaTeX: pdfLaTeX
  14 +
  15 +AutoAppendNewline: Yes
  16 +StripTrailingWhitespace: Yes
  17 +
  18 +BuildType: Package
  19 +PackageUseDevtools: Yes
  20 +PackageInstallArgs: --no-multiarch --with-keep.source
  21 +PackageRoxygenize: rd,collate,namespace
... ...
NAMESPACE 0 → 100644
  1 +++ a/NAMESPACE
... ... @@ -0,0 +1,14 @@
  1 +# Generated by roxygen2: do not edit by hand
  2 +
  3 +export(SWITCHING_FREQUENCY)
  4 +export(SWITCHING_PERIOD)
  5 +export(read_interference)
  6 +export(read_no_interference)
  7 +importFrom(dplyr,between)
  8 +importFrom(dplyr,bind_rows)
  9 +importFrom(dplyr,filter)
  10 +importFrom(dplyr,mutate)
  11 +importFrom(feather,read_feather)
  12 +importFrom(purrr,map_chr)
  13 +importFrom(stringr,str_interp)
  14 +importFrom(tibble,tibble)
... ...
R/reading_funs.R 0 → 100644
  1 +++ a/R/reading_funs.R
... ... @@ -0,0 +1,99 @@
  1 +
  2 +#' @export
  3 +SWITCHING_FREQUENCY <- 10e3
  4 +
  5 +#' @export
  6 +SWITCHING_PERIOD <- 1/10e3
  7 +
  8 +get_times <- function(start_from_period, how_many_periods) {
  9 + fc <- 10e3
  10 + pc <- 1/fc
  11 + start_time <- start_from_period * pc
  12 + end_time <- start_time + how_many_periods * pc
  13 + return(list(start_time = start_time, end_time = end_time))
  14 +}
  15 +
  16 +#' Read data with interference
  17 +#'
  18 +#' Reads data from the experiment.
  19 +#'
  20 +#' @param which number (should be from 0 to 16) which measurement to take
  21 +#' @param start_from_period
  22 +#' @param how_many_periods
  23 +#' @param add_no_interference
  24 +#'
  25 +#' @return
  26 +#' @export
  27 +#'
  28 +#' @importFrom dplyr filter mutate bind_rows between
  29 +#' @importFrom feather read_feather
  30 +#' @importFrom tibble tibble
  31 +#' @importFrom purrr map_chr
  32 +#' @importFrom stringr str_interp
  33 +#'
  34 +#'
  35 +#' @examples
  36 +#'
  37 +#' T1 <- read_interference("/Users/karolniewiadomski/Documents/SCENT/Simulations/ExperimentsUZ/ErrorModelsDataClean/",
  38 +#' which = c(2, 13), start_from_period=3, how_many_periods=1, add_no_interference=TRUE)
  39 +#' head(T1)
  40 +#'
  41 +read_interference <- function(
  42 + data_path, which = 0, start_from_period = 2, how_many_periods = 2, add_no_interference = FALSE) {
  43 + times <- get_times(start_from_period, how_many_periods)
  44 +
  45 + if(min(which) < 0 || max(which) > 16) stop("Which between 0 and 16.")
  46 + filenames <- map_chr(which, function(x)
  47 + file.path(data_path, str_interp("data_$[.5d]{x}.feather") ))
  48 +
  49 + TD <- tibble()
  50 + i <- 1
  51 + for(.filename in filenames) {
  52 + T1 <- read_feather(.filename)
  53 + T1 <- filter(T1, between(time, times$start_time, times$end_time))
  54 + T1 <- mutate(T1, measurement_no = which[i])
  55 + TD <- bind_rows(TD, T1)
  56 + i <- i + 1
  57 + }
  58 +
  59 + # No interference is declared as measurement_no -1.
  60 + if(add_no_interference) {
  61 + T2 <- read_no_interference(data_path, start_from_period, how_many_periods)
  62 + TD <- bind_rows(TD, T2)
  63 + }
  64 +
  65 + return(TD)
  66 +
  67 +}
  68 +
  69 +
  70 +
  71 +#' Read data with no interference
  72 +#'
  73 +#' Reads data from measurements, without interference.
  74 +#'
  75 +#' @param start_from_period from which
  76 +#' @param how_many_periods
  77 +#'
  78 +#' @return
  79 +#' @export
  80 +#'
  81 +#' @importFrom feather read_feather
  82 +#' @importFrom dplyr filter mutate between
  83 +#'
  84 +#' @examples
  85 +#'
  86 +#' T1 <- read_no_interference("/Users/karolniewiadomski/Documents/SCENT/Simulations/ExperimentsUZ/ErrorModelsDataClean/", 2, 2)
  87 +#' head(T1)
  88 +#'
  89 +read_no_interference <- function(
  90 + data_path, start_from_period = 2, how_many_periods = 2) {
  91 +
  92 + times <- get_times(start_from_period, how_many_periods)
  93 + filename <- file.path(data_path, "data_no.feather")
  94 + TD <- read_feather(filename)
  95 + TD <- filter(TD, between(time, times$start_time, times$end_time))
  96 + TD <- mutate(TD, measurement_no = -1) # To be consistent measurement_no is -1.
  97 + return(TD)
  98 +}
  99 +
... ...
man/read_interference.Rd 0 → 100644
  1 +++ a/man/read_interference.Rd
... ... @@ -0,0 +1,32 @@
  1 +% Generated by roxygen2: do not edit by hand
  2 +% Please edit documentation in R/reading_funs.R
  3 +\name{read_interference}
  4 +\alias{read_interference}
  5 +\title{Read data with interference}
  6 +\usage{
  7 +read_interference(
  8 + data_path,
  9 + which = 0,
  10 + start_from_period = 2,
  11 + how_many_periods = 2,
  12 + add_no_interference = FALSE
  13 +)
  14 +}
  15 +\arguments{
  16 +\item{which}{number (should be from 0 to 16) which measurement to take}
  17 +
  18 +\item{add_no_interference}{}
  19 +}
  20 +\value{
  21 +
  22 +}
  23 +\description{
  24 +Reads data from the experiment.
  25 +}
  26 +\examples{
  27 +
  28 +T1 <- read_interference("/Users/karolniewiadomski/Documents/SCENT/Simulations/ExperimentsUZ/ErrorModelsDataClean/",
  29 + which = c(2, 13), start_from_period=3, how_many_periods=1, add_no_interference=TRUE)
  30 +head(T1)
  31 +
  32 +}
... ...
man/read_no_interference.Rd 0 → 100644
  1 +++ a/man/read_no_interference.Rd
... ... @@ -0,0 +1,25 @@
  1 +% Generated by roxygen2: do not edit by hand
  2 +% Please edit documentation in R/reading_funs.R
  3 +\name{read_no_interference}
  4 +\alias{read_no_interference}
  5 +\title{Read data with no interference}
  6 +\usage{
  7 +read_no_interference(data_path, start_from_period = 2, how_many_periods = 2)
  8 +}
  9 +\arguments{
  10 +\item{start_from_period}{from which}
  11 +
  12 +\item{how_many_periods}{}
  13 +}
  14 +\value{
  15 +
  16 +}
  17 +\description{
  18 +Reads data from measurements, without interference.
  19 +}
  20 +\examples{
  21 +
  22 +T1 <- read_no_interference("/Users/karolniewiadomski/Documents/SCENT/Simulations/ExperimentsUZ/ErrorModelsDataClean/", 2, 2)
  23 +head(T1)
  24 +
  25 +}
... ...