Skip to content

Commit

Permalink
Initial commit, added BB discretizer
Browse files Browse the repository at this point in the history
  • Loading branch information
jenzopr committed Feb 22, 2017
1 parent 5cd09ed commit 9aa5611
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
^.*\.Rproj$
^\.Rproj\.user$
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.Rproj.user
.Rhistory
.RData
11 changes: 11 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Package: rPID
Title: rPID - Partial information decompostion in R
Version: 0.0.0.9000
Authors@R: person("Jens", "Preussner", email = "jens.preussner@mpi-bn.mpg.de", role = c("aut", "cre"))
Description: Implements methods for calulating entropy, mutual information and higher order measures from information theory.
Depends: R (>= 3.3.2)
License: MIT
Encoding: UTF-8
LazyData: true
Imports: rPython
RoxygenNote: 6.0.1
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Generated by roxygen2: do not edit by hand

export(bayesian_blocks)
export(discretize.bb)
57 changes: 57 additions & 0 deletions R/density_estimation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#' Discretize a matrix using the bayesian blocks algorithm
#'
#' @param X Numerical matrix to be discretized column-wise.
#' @param ... Additional parameters passed to bayesian_blocks
#'
#' @return A discretized matrix
#'
#' @export
discretize.bb <- function(X, ...) {
X <- as.matrix(X)

ret <- apply(X, 2, function(x, ...) {
bins <- bayesian_blocks(x, ...)
bins[which.min(bins)] <- min(x)
as.numeric(cut(x, breaks = bins, include.lowest = T))
}, ...)

ret
}

#' Wrapper for astroML.density_estimation.bayesian_blocks
#'
#' @param t A numeric
#' @param x A numeric
#' @param sigma Data error
#' @param fitness Fitness function
#' @param kwargs A named list of ot <ther parameters
#'
#' @return array containing the (N+1) bin edges
#'
#' @export
bayesian_blocks <- function(t, x=NULL, sigma=NULL, fitness="events", kwargs = list()) {
if (!is.numeric(t)) {
t <- as.numeric(t)
}

rPython::python.assign("t", t)
rPython::python.exec("x = None")
rPython::python.exec("sigma = None")

if (!is.null(x)) {
x <- as.numeric(x)
rPython::python.assign("x", x)
}
if (!is.null(sigma)) {
sigma <- as.numeric(sigma)
rPython::python.assign("sigma", sigma)
}

fi <- match.arg(fitness, c("events", "regular_events", "measures"))
rPython::python.assign("fitness", fi)

rPython::python.exec("from astroML.density_estimation import bayesian_blocks")
rPython::python.exec("bins = bayesian_blocks(t=t, x=x, sigma=sigma, fitness=fitness)")
bins <- rPython::python.method.call("bins","tolist")
bins
}
26 changes: 26 additions & 0 deletions man/bayesian_blocks.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions man/discretize.bb.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions rPID.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Version: 1.0

RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace

0 comments on commit 9aa5611

Please sign in to comment.