Nothing
#' Generate row indices for partitioning a matrix-like object
#'
#' @param nrow Integer. Total number of rows in the object to be
#' partitioned.
#' @param n_partitions Integer. Number of (roughly) equal partitions.
#' Mutually exclusive with `partition_size`.
#' @param partition_size Integer. Target number of rows per partition.
#' Mutually exclusive with `n_partitions`.
#'
#' @return A named list of integer vectors, each containing the row indices
#' for one partition.
#'
#' @details
#' This function returns indices only and does not hold any data in memory.
#' Intended for use in loops or iterators where partitions are materialised
#' one at a time. The remainder from unequal division is retained as a smaller
#' final partition rather than distributed or discarded.
#'
#' When \code{n_partitions} is used, partition sizes differ by at most one row,
#' with larger partitions appearing first. When \code{partition_size} is used,
#' all partitions except possibly the last will have exactly
#' \code{partition_size} rows.
#'
#' @export
#'
#' @examples
#' idx <- partition_indices(nrow = 20, n_partitions = 3)
#'
partition_indices <- function(nrow, n_partitions = NULL, partition_size = NULL) {
# --- Input validation -------------------------------------------------------
if (!is.numeric(nrow) || length(nrow) != 1L ||
nrow < 1L || nrow != floor(nrow)) {
stop("`nrow` must be a single positive integer.")
}
nrow <- as.integer(nrow)
if (is.null(n_partitions) == is.null(partition_size)) {
stop("Provide exactly one of `n_partitions` or `partition_size`.")
}
# --- Compute groups ---------------------------------------------------------
if (!is.null(n_partitions)) {
if (!is.numeric(n_partitions) || length(n_partitions) != 1L ||
n_partitions < 1L || n_partitions != floor(n_partitions)) {
stop("`n_partitions` must be a single positive integer.")
}
n_partitions <- min(as.integer(n_partitions), nrow)
groups <- cut(seq_len(nrow), breaks = n_partitions, labels = FALSE)
} else {
if (!is.numeric(partition_size) || length(partition_size) != 1L ||
partition_size < 1L || partition_size != floor(partition_size)) {
stop("`partition_size` must be a single positive integer.")
}
partition_size <- as.integer(partition_size)
groups <- ceiling(seq_len(nrow) / partition_size)
}
# --- Build index list -------------------------------------------------------
n_parts <- max(groups)
pad_width <- nchar(as.character(n_parts))
nms <- paste0("partition_", formatC(seq_len(n_parts),
width = pad_width,
flag = "0"))
indices <- lapply(seq_len(n_parts), function(i) which(groups == i))
stats::setNames(indices, nms)
}
# #' Partitioning of the population graph into smaller partitions of family graphs
# #' from some set of proband ids
# #'
# #'
# #'
# #' @param pop_graph An igraph object from the `prepare_graph()` function.
# #' @param proband_vec A vector of proband ids to be split into partitions.
# #' @param ndegree Number of steps away from proband to include, a `get_family_graphs()` argument.
# #' @param root_file_partition Root file path for output files.
# #' @param n_partitions Integer. Number of (roughly) equal partitions.
# #' Mutually exclusive with `partition_size`.
# #' @param partition_size Integer. Target number of rows per partition.
# #' Mutually exclusive with `n_partitions`.
# #'
# #' @returns This function returns only NULLs. The real output are files on the disk.
# #' files are intended to be temporary only and must be deleted by the user manually later.
# #' The files can be used as input for other steps in the data processing steps
# #' before being used in `estimate_liability()`.
# #' The files may be more easily used in a split-apply-combine strategy with parallelisation
# #'
# #'
# #' @export
# #'
# #' @examples
# partition_graphs <- function(pop_graph, proband_vec, ndegree,
# root_file_partition,
# n_partitions = NULL,
# partition_size = NULL) {
#
# # determining partitioning of the probands
# indx_list <- partition_indices(nrow = length(proband_vec),
# n_partitions = n_partitions,
# partition_size = partition_size)
#
# # calculating family graphs from the population graph
# # a new family graph object is generated with each partition of proband_vec
# # we loop over elements of indx_list so we can access name and content separately
# NULL_catcher <- lapply(seq_along(indx_list), function(i) {
# # calculate family graphs:
# fam_graphs <- get_family_graphs(pop_graph = pop_graph,
# ndegree = ndegree,
# proband_vec = proband_vec[indx_list[[i]]])
#
# # constructing the output file name of the partitioned family graphs:
# partition_name <- paste0(root_file_partition, "_", names(indx_list)[i], ".rds")
# # saving partition
# saveRDS(fam_graphs, partition_name)
# # returning NULL:
# return(NULL)
# })
# # returning NULL:
# return(NULL)
# }
#
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.