Skip to content
This repository was archived by the owner on Jun 9, 2023. It is now read-only.

Add check for missing index parameter to BallCover.R #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
63 changes: 33 additions & 30 deletions R/BallCover.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#' Ball Cover
#'
#' @docType class
#' @description This class provides a cover whose open sets are formed by the union of \deqn{\epsilon}-balls centered
#' @description This class provides a cover whose open sets are formed by the union of \deqn{\epsilon}-balls centered
#' about each point. Using this class requires the \code{RANN} package to be installed, and thus explicitly assumes
#' the filter space endowed with the euclidean metric.
#'
#' the filter space endowed with the euclidean metric.
#'
#' @field epsilon := radius of the ball to form around each point
#' @author Matt Piekenbrock
#' @export
Expand Down Expand Up @@ -41,33 +41,36 @@ BallCover$set("public", "construct_cover", function(filter, index=NULL){
stop("Package \"RANN\" is needed for to use this cover.", call. = FALSE)
}
self$validate()

## Get filter values
fv <- filter()
f_dim <- ncol(fv)
f_size <- nrow(fv)

## Construct the balls
ball_cover <- RANN::nn2(fv, query = fv, searchtype = "radius", radius = self$epsilon)

## Union them together
ds <- union_find(f_size)
apply(ball_cover$nn.idx, 1, function(idx){
connected_idx <- idx[idx != 0] - 1L
if (length(connected_idx) > 0){
ds$union_all(connected_idx)
}
})

## Construct the intersections between the open sets and the data
cc <- ds$connected_components()
self$index_set <- as.character(unique(cc))
ls <- lapply(self$index_set, function(idx){
which(cc == as.integer(idx))
})
self$level_sets <- structure(ls, names=self$index_set)

if(missing(index) || is.null(index)){
## Get filter values
fv <- filter()
f_dim <- ncol(fv)
f_size <- nrow(fv)

## Construct the balls
ball_cover <- RANN::nn2(fv, query = fv, searchtype = "radius", radius = self$epsilon)

## Union them together
ds <- union_find(f_size)
apply(ball_cover$nn.idx, 1, function(idx){
connected_idx <- idx[idx != 0] - 1L
if (length(connected_idx) > 0){
ds$union_all(connected_idx)
}
})

## Construct the intersections between the open sets and the data
cc <- ds$connected_components()
self$index_set <- as.character(unique(cc))
ls <- lapply(self$index_set, function(idx){
which(cc == as.integer(idx))
})

self$level_sets <- structure(ls, names=self$index_set)
}
if (!missing(index)){ return(self$level_sets[[index]]) }
## Always return self

## Always return self
invisible(self)
})