Skip to content

Commit

Permalink
Merge branch 'travis' into 'master'
Browse files Browse the repository at this point in the history
Implement regression testing and CI/CD

See merge request oet5/cbRw!1
  • Loading branch information
beansrowning committed Mar 20, 2019
2 parents 08e8e02 + 4a439a6 commit 7a17ab1
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 46 deletions.
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
^.*\.Rproj$
^\.Rproj\.user$
^\.travis\.yml$
^data-raw$
.gitlab-ci.yml
41 changes: 41 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
document:
stage: document
script:
- R --vanilla -e 'library(devtools); devtools::document(); devtools::document()'
artifacts:
paths:
- man/
- NAMESPACE

test:
stage: test
script:
- R --vanilla -e 'library(devtools); library(testthat); devtools::test(reporter
= StopReporter)'
dependencies:
- document

build:
stage: build
script:
- R --vanilla -e 'library(devtools); devtools::build(path = "./")'
artifacts:
paths:
- '*.tar.gz'
dependencies:
- document

check:
stage: check
script:
- R --vanilla -e 'library(devtools); tar_file <- file.path(getwd(), list.files(".",
pattern = ".tar.gz")); results <- devtools::check_built(tar_file); stopifnot(sum(length(results$errors), length(results$warnings)) <= 0)'
dependencies:
- build

stages:
- document
- test
- build
- check

5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# R for travis: see documentation at https://docs.travis-ci.com/user/languages/r

language: R
sudo: false
cache: packages
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ export(biased_trans_matrix)
export(cbrw)
import(dplyr)
import(rlang)
importFrom(stats,setNames)
importFrom(tidyr,spread)
importFrom(utils,combn)
4 changes: 2 additions & 2 deletions R/cbrw.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' @title Coupled Biased Random Walks
#' TODO
#' @description TODO
#' @param data a data.frame containing catgorical data
#' @value the input data frame with an additional \emph{score} variable representing
#' @return the input data frame with an additional \emph{score} variable representing
#' relative outlier-ness of the observation
#' @export
cbrw <- function(data) {
Expand Down
26 changes: 14 additions & 12 deletions R/count.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# TODO: Probably consolidate, or just build an R6 class
#' @title Counting helper functions
#' @name counting_helpers
# @title Counting helper functions
# @name counting_helpers

#' @rdname counting_helpers
#' @param data a tibble of categorical data
#' @return a tibble containing all unique combinations of feature values
#' with columns: \emph{u}, \emph{v}, \emph{freq}, emph{p}, and \emph{feature}
# @rdname counting_helpers
# @param data a tibble of categorical data
# @return a tibble containing all unique combinations of feature values
# with columns: \emph{u}, \emph{v}, \emph{freq}, emph{p}, and \emph{feature}
intra_freq <- function(data) {
var_quos <- lapply(names(data), as.name)

Expand All @@ -30,12 +30,14 @@ intra_freq <- function(data) {
return(out)
}

#' Helper function to calculate all bivariate frequencies
#' within the dataset
#' @param data a tibble of categorical data
#' @return a tibble containing all unique combinations of feature values
#' with columns: \emph{u}, \emph{v}, \emph{freq}, and \emph{group}
#' @rdname counting_helpers
# Helper function to calculate all bivariate frequencies
# within the dataset
# @param data a tibble of categorical data
# @return a tibble containing all unique combinations of feature values
# with columns: \emph{u}, \emph{v}, \emph{freq}, and \emph{group}
# @rdname counting_helpers
#' @importFrom utils combn
#' @importFrom stats setNames
inter_freq <- function(data) {

# Calculate all 2 variable combinations of variables
Expand Down
1 change: 1 addition & 0 deletions R/data.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#' @title CBRW paper exmaple dataset
#' @description See dataset in cited paper (TODO)
#' Example dataset as shown on the first page of the paper
"cbrw_example"
4 changes: 4 additions & 0 deletions R/matrix.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#' @title Generate Biased Transition Matrix
#' @description
#' Takes in a data.frame of categorical data and returns a weighted transition matrix
#' which characterizes the edge weights of a directed graph representation of the inter-feature value couplings
#'
#' @param data a data.frame containing mosly categorical data
#' @param all_data a boolean (default: FALSE) on whether to return additional node and edge data (see Note)
#' @return a biased transition matrix of dim [k,k] where k is the number of unique feature values
Expand Down
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# cbRw, Coupled Biased Random Walks
# cbRw, Coupled Biased Random Walks [![Build Status](https://travis-ci.org/beansrowning/cbRw.svg?branch=master)](https://travis-ci.org/beansrowning/cbRw)
Anomaly detection for complex categorical data

## Overview
Expand All @@ -7,7 +7,15 @@ Described by [Pang, Cao, and Chen (2016)](https://www.ijcai.org/Proceedings/16/P
Also based on work by Daniel Kaslovsky in the Python implementation, [Coupled-Biased-Random-Walks
](https://github.com/dkaslovsky/Coupled-Biased-Random-Walks)

## Work in progress
## Installation

*Note that this is still very much developmental*

```r
# Installation is straightforward with devtools
# install.packages("devtools")
devtools::install_github("beansrowning/cbRw")
```

## Public Domain
This repository constitutes a work of the United States Government and is not
Expand Down
4 changes: 4 additions & 0 deletions man/biased_trans_matrix.Rd

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

18 changes: 18 additions & 0 deletions man/cbrw.Rd

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

7 changes: 5 additions & 2 deletions man/cbrw_example.Rd

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

28 changes: 0 additions & 28 deletions man/counting_helpers.Rd

This file was deleted.

4 changes: 4 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
library(testthat)
library(cbRw)

test_check("cbRw")
27 changes: 27 additions & 0 deletions tests/testthat/test_cbrw.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
context("Algorithm accuracy")

test_that("Score values are equal to canonical", {

# Canonical values pulled from running cbrw test data through
# the python CBRW package, dkaslovsky/Coupled-Biased-Random-Walks
canonical <- c(
0.123556072282254,
0.0504085179712585,
0.0476111020550283,
0.0495085666704524,
0.0844538406306188,
0.0468666953948249,
0.0476111020550283,
0.0493436236504131,
0.0459772220233669,
0.0694800337106498,
0.0495038674807341,
0.0668533395540888
)

# Process example dataset and compare values
data(cbrw_example)
cbrw_example <- cbrw(cbrw_example)

expect_equal(cbrw_example$score, canonical)
})

0 comments on commit 7a17ab1

Please sign in to comment.