Skip to content
This repository was archived by the owner on Nov 13, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ Suggests: testthat
License: GPL-3
LazyData: true
Packaged: 2014-12-08 07:54:07 UTC; owenvallis
RoxygenNote: 6.0.1
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by roxygen2 (4.1.0): do not edit by hand
# Generated by roxygen2: do not edit by hand

export(AnomalyDetectionTs)
export(AnomalyDetectionVec)
21 changes: 11 additions & 10 deletions R/date_utils.R
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
format_timestamp <- function(indf, index = 1) {
if (class(indf[[index]])[1] == "POSIXlt") {
if (class(indf[[index]])[1] == "POSIXct") {
return(indf)
}
if (stringr::str_detect(indf[[index]][1], "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2} \\+\\d{4}$")) {
indf[[index]] <- strptime(indf[[index]], format="%Y-%m-%d %H:%M:%S", tz="UTC")
indf[[index]] <- strptime(indf[[index]], format = "%Y-%m-%d %H:%M:%S", tz = "UTC")
}
else if (stringr::str_detect(indf[[index]][1], "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$")) {
indf[[index]] <- strptime(indf[[index]], format="%Y-%m-%d %H:%M:%S", tz="UTC")
indf[[index]] <- strptime(indf[[index]], format = "%Y-%m-%d %H:%M:%S", tz = "UTC")
}
else if (stringr::str_detect(indf[[index]][1], "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}$")) {
indf[[index]] <- strptime(indf[[index]], format="%Y-%m-%d %H:%M", tz="UTC")
indf[[index]] <- strptime(indf[[index]], format = "%Y-%m-%d %H:%M", tz = "UTC")
}
else if (stringr::str_detect(indf[[index]][1], "^\\d{2}/\\d{2}/\\d{2}$")) {
indf[[index]] <- strptime(indf[[index]], format="%m/%d/%y", tz="UTC")
indf[[index]] <- strptime(indf[[index]], format = "%m/%d/%y", tz = "UTC")
}
else if (stringr::str_detect(indf[[index]][1], "^\\d{2}/\\d{2}/\\d{4}$")) {
indf[[index]] <- strptime(indf[[index]], format="%m/%d/%Y", tz="UTC")
indf[[index]] <- strptime(indf[[index]], format = "%m/%d/%Y", tz = "UTC")
}
else if (stringr::str_detect(indf[[index]][1], "^\\d{4}\\d{2}\\d{2}$")) {
indf[[index]] <- strptime(indf[[index]], format="%Y%m%d", tz="UTC")
indf[[index]] <- strptime(indf[[index]], format = "%Y%m%d", tz = "UTC")
}
else if (stringr::str_detect(indf[[index]][1], "^\\d{4}/\\d{2}/\\d{2}/\\d{2}$")) {
indf[[index]] <- strptime(indf[[index]], format="%Y/%m/%d/%H", tz="UTC")
indf[[index]] <- strptime(indf[[index]], format = "%Y/%m/%d/%H", tz = "UTC")
}
else if (stringr::str_detect(indf[[index]][1], "^\\d{10}$")) {
# Handle Unix seconds in milliseconds
indf[[index]] <- as.POSIXlt(indf[[index]], origin="1970-01-01", tz="UTC")
indf[[index]] <- as.POSIXlt(indf[[index]], origin = "1970-01-01", tz = "UTC")
}

indf[[index]] <- as.POSIXct(indf[[index]], tz = "UTC")
return(indf)
}

get_gran = function(tsdf, index=1) {
n = length(tsdf[[index]])
# We calculate the granularity from the time difference between the last 2 entries (sorted)
gran = round(difftime(max(tsdf[[index]]), sort(tsdf[[index]], partial = n-1)[n-1],
gran = round(difftime(max(tsdf[[index]]), sort(tsdf[[index]], partial = n - 1)[n - 1],
units = "secs"))

if (gran >= 86400) {
Expand Down
218 changes: 119 additions & 99 deletions R/ts_anom_detection.R

Large diffs are not rendered by default.

Binary file modified data/raw_data.rda
Binary file not shown.
9 changes: 5 additions & 4 deletions man/AnomalyDetectionTs.Rd

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

31 changes: 15 additions & 16 deletions man/AnomalyDetectionVec.Rd

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

3 changes: 1 addition & 2 deletions man/raw_data.Rd

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

9 changes: 4 additions & 5 deletions tests/testthat/test-edge.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ context("Testing edge cases")

test_that("checking for errors if time series has constant value for all values", {
data <- rep(1,1000)
expect_true({AnomalyDetectionVec(data, period=14, plot=T, direction='both'); TRUE})

expect_true({AnomalyDetectionVec(data, period = 14, plot = T, direction = 'both'); TRUE})
})

test_that("checking that midnight dates get H%M%S format applied", {
data_file <- system.file("extdata", "data.csv", package="AnomalyDetection")
data_file <- system.file("extdata", "data.csv", package = "AnomalyDetection")
data <- read.csv(data_file)
data$date <- as.POSIXct(strptime(data$date, "%Y-%m-%d %H:%M", tz = "UTC"))
anomalyDetectionResult <- AnomalyDetectionTs(data, max_anoms=0.2, threshold = "None",
direction='both', plot=FALSE,
anomalyDetectionResult <- AnomalyDetectionTs(data, max_anoms = 0.2, threshold = "None",
direction = 'both', plot = FALSE,
only_last = "day", e_value = TRUE)

expect_equal(length(anomalyDetectionResult$anoms$anoms), length(anomalyDetectionResult$anoms$expected_value))
Expand Down