Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
malcolmbarrett committed Nov 10, 2024
1 parent 3a00443 commit d14c3f1
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions R/super_partition.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,31 @@ super_partition <- function(full_data,
}

# ensure 0 < threshold < 1
if (0 > threshold | 1 < threshold) stop("Threshold must be between 0 and 1.")
if (0 > threshold | 1 < threshold) {
stop("Threshold must be between 0 and 1.")
}

# ensure no column names contain x
if (any(grepl(x, colnames(full_data)))) stop(paste0("The prefix for new variable names, ", x, ", is contained within existing data column names. Please choose a different prefix to avoid errors."))
if (any(grepl(x, colnames(full_data)))) {
stop(paste0("The prefix for new variable names, ", x, ", is contained within existing data column names. Please choose a different prefix to avoid errors."))
}

# ensure data frame structure
full_data <- as.data.frame(full_data)

# if < cluster_size features, call regular partition
if (ncol(full_data) < cluster_size) {
message(paste0("Using `partition()` since there are < ", cluster_size, "features."))
return(partition(full_data, threshold, partitioner, tolerance, niter, x, .sep))
prt <- partition(
full_data,
threshold = threshold,
partitioner = partitioner,
tolerance = tolerance,
niter = niter,
x = x,
.sep = .sep
)
return(prt)
}

# iteration counters
Expand Down Expand Up @@ -182,7 +195,7 @@ super_partition <- function(full_data,
width = 100
)
}

# if no dimension reduction, use partition instead
if (length(unique(master_cluster$cluster)) == ncol(full_data)) {
if (verbose) message("No dimension reduction occured using Super Partition. Using Partition instead.")
Expand All @@ -192,7 +205,7 @@ super_partition <- function(full_data,
## first cluster - always use largest cluster
clust_sizes <- as.data.frame(table(master_cluster$cluster))
first_clust <- which(unique(master_cluster$cluster) == clust_sizes[which.max(clust_sizes$Freq), 1])

# get initial partition to build off
part_master <- partition(
full_data[, which(master_cluster$cluster == unique(master_cluster$cluster)[first_clust])],
Expand All @@ -217,10 +230,10 @@ super_partition <- function(full_data,
if (progress_bar) pb$tick()

# for each cluster...
for (i in 1:n_iter) {
for (i in seq_len(n_iter)) {
# skip if first cluster
if(i == first_clust) next()
if (i == first_clust) next()

# what to do if cluster is of size one
if (sum(master_cluster$cluster == unique(master_cluster$cluster)[i]) == 1) {
# cbind data to master partition reduced data
Expand Down

0 comments on commit d14c3f1

Please sign in to comment.