Skip to content
Merged
Show file tree
Hide file tree
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
32 changes: 22 additions & 10 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@
#' must have two columns, the first of which represents the preferred item
#' and the second of which represents the disfavored item.
#' @param n_items An integer specifying the number of items to sort.
#' @param output_directory A string specifying the directory where the output files will be saved.
#' @param save_frac Number between 0 and 1 specifying which fraction of sorts to save.
#'
#' @details
#' The function generates all possible topological sorts for the provided preference matrix
#' and saves approximately `save_frac` of the sorts as binary file in the specified output directory.
#' The output files are named sequentially as `sort0.bin`, `sort1.bin`, and so on.
#' and saves approximately `save_frac` of the sorts in a matrix which is returned.
#'
#' @return This function returns the number of topological sorts.
#'
Expand All @@ -27,19 +25,33 @@
#' prefs <- pairwise_preferences[
#' pairwise_preferences$user == 1, c("top_item", "bottom_item"), drop = FALSE]
#'
#' # Count the number of sorts without saving them.
#' precompute_topological_sorts(
#' # Generate all topological sorts, but don't save them:
#' sorts <- precompute_topological_sorts(
#' prefs = as.matrix(prefs),
#' n_items = 5,
#' output_directory = tempdir(),
#' save_frac = 0
#' )
#' # Number of sorts
#' sorts$sort_count
#' # Empty matrix
#' sorts$sort_matrix
#'
precompute_topological_sorts <- function(prefs, n_items, output_directory, save_frac) {
.Call(`_BayesMallowsSMC2_precompute_topological_sorts`, prefs, n_items, output_directory, save_frac)
#' # Generate all topological sorts and save them:
#' sorts <- precompute_topological_sorts(
#' prefs = as.matrix(prefs),
#' n_items = 5,
#' save_frac = 1
#' )
#' # Number of sorts
#' sorts$sort_count
#' # Matrix with all of them
#' sorts$sort_matrix
#'
precompute_topological_sorts <- function(prefs, n_items, save_frac) {
.Call(`_BayesMallowsSMC2_precompute_topological_sorts`, prefs, n_items, save_frac)
}

run_smc <- function(input_timeseries, input_prior, input_options) {
.Call(`_BayesMallowsSMC2_run_smc`, input_timeseries, input_prior, input_options)
run_smc <- function(input_timeseries, input_prior, input_options, input_sort_matrices, input_sort_counts) {
.Call(`_BayesMallowsSMC2_run_smc`, input_timeseries, input_prior, input_options, input_sort_matrices, input_sort_counts)
}

44 changes: 17 additions & 27 deletions R/compute_sequentially.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,9 @@
#'
#' @param hyperparameters A list returned from [set_hyperparameters()].
#' @param smc_options A list returned from [set_smc_options()]
#' @param topological_sorts_directory Path to a directory where precomputed
#' topological sorts can be found. Must contain subdirectories with names
#' `user1`, `user2`, etc., exactly matching the `user` column of `data`. See
#' [precompute_topological_sorts()]. Defaults to `NULL`, but has to be
#' provided when data contain pairwise preferences.
#'
#' @param num_topological_sorts Integer vector containing the number of
#' topological sorts for each user.
#' @param file_count Integer vector containing the number of files with
#' topological sorts for each user.
#' @param topological_sorts A list returned from
#' [precompute_topological_sorts()]. Only used with preference data, and
#' defaults to `NULL`.
#'
#' @return An object
#' @export
Expand All @@ -43,9 +36,7 @@ compute_sequentially <- function(
data,
hyperparameters = set_hyperparameters(),
smc_options = set_smc_options(),
topological_sorts_directory = NULL,
num_topological_sorts = NULL,
file_count = NULL
topological_sorts = NULL
){
rank_columns <- grepl("item[0-9]+", colnames(data))
preference_columns <- grepl("top\\_item|bottom\\_item", colnames(data))
Expand All @@ -60,34 +51,33 @@ compute_sequentially <- function(
} else {
attr(input_timeseries, "type") <- "complete rankings"
}
sort_matrices <- sort_counts <- list()
} else if(sum(preference_columns) == 2) {
if(is.null(topological_sorts_directory)) {
stop("topological_sorts_directory must be provided with preference data.")
}
if(is.null(num_topological_sorts)) {
stop("num_topological_sorts must be provided with preference data.")
}
if(is.null(file_count)) {
stop("file_count must be provided with preference data.")
if(is.null(topological_sorts)) {
stop("topological_sorts must be provided with preference data.")
}
input_timeseries <- split(data, f = ~ timepoint) |>
lapply(split, f = ~ user) |>
lapply(function(x) lapply(x, function(y) as.matrix(y[preference_columns])))
attr(input_timeseries, "type") <- "pairwise preferences"
attr(input_timeseries, "topological_sorts_directory") <- topological_sorts_directory
names(num_topological_sorts) <- names(file_count) <- as.character(lapply(input_timeseries, names))
attr(input_timeseries, "num_topological_sorts") <- num_topological_sorts
attr(input_timeseries, "file_count") <- file_count

sort_matrices <- lapply(topological_sorts, function(x) {
lapply(x, function(y) y$sort_matrix)
})

sort_counts <- lapply(topological_sorts, function(x) {
lapply(x, function(y) y$sort_count)
})
} else {
stop("Something wrong with data")
}

attr(input_timeseries, "updated_users") <- FALSE
if(max(table(data$user)) > 1 &&
attr(input_timeseries, "type") != "pairwise preferences") {
stop("Updated users not supported.")
}

ret <- run_smc(input_timeseries, hyperparameters, smc_options)
ret <- run_smc(input_timeseries, hyperparameters, smc_options,
sort_matrices, sort_counts)
}

18 changes: 4 additions & 14 deletions man/compute_sequentially.Rd

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

27 changes: 19 additions & 8 deletions man/precompute_topological_sorts.Rd

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

19 changes: 10 additions & 9 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,37 @@ Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
#endif

// precompute_topological_sorts
long long int precompute_topological_sorts(arma::umat prefs, int n_items, std::string output_directory, double save_frac);
RcppExport SEXP _BayesMallowsSMC2_precompute_topological_sorts(SEXP prefsSEXP, SEXP n_itemsSEXP, SEXP output_directorySEXP, SEXP save_fracSEXP) {
Rcpp::List precompute_topological_sorts(arma::umat prefs, int n_items, double save_frac);
RcppExport SEXP _BayesMallowsSMC2_precompute_topological_sorts(SEXP prefsSEXP, SEXP n_itemsSEXP, SEXP save_fracSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< arma::umat >::type prefs(prefsSEXP);
Rcpp::traits::input_parameter< int >::type n_items(n_itemsSEXP);
Rcpp::traits::input_parameter< std::string >::type output_directory(output_directorySEXP);
Rcpp::traits::input_parameter< double >::type save_frac(save_fracSEXP);
rcpp_result_gen = Rcpp::wrap(precompute_topological_sorts(prefs, n_items, output_directory, save_frac));
rcpp_result_gen = Rcpp::wrap(precompute_topological_sorts(prefs, n_items, save_frac));
return rcpp_result_gen;
END_RCPP
}
// run_smc
Rcpp::List run_smc(Rcpp::List input_timeseries, Rcpp::List input_prior, Rcpp::List input_options);
RcppExport SEXP _BayesMallowsSMC2_run_smc(SEXP input_timeseriesSEXP, SEXP input_priorSEXP, SEXP input_optionsSEXP) {
Rcpp::List run_smc(Rcpp::List input_timeseries, Rcpp::List input_prior, Rcpp::List input_options, Rcpp::List input_sort_matrices, Rcpp::List input_sort_counts);
RcppExport SEXP _BayesMallowsSMC2_run_smc(SEXP input_timeseriesSEXP, SEXP input_priorSEXP, SEXP input_optionsSEXP, SEXP input_sort_matricesSEXP, SEXP input_sort_countsSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::List >::type input_timeseries(input_timeseriesSEXP);
Rcpp::traits::input_parameter< Rcpp::List >::type input_prior(input_priorSEXP);
Rcpp::traits::input_parameter< Rcpp::List >::type input_options(input_optionsSEXP);
rcpp_result_gen = Rcpp::wrap(run_smc(input_timeseries, input_prior, input_options));
Rcpp::traits::input_parameter< Rcpp::List >::type input_sort_matrices(input_sort_matricesSEXP);
Rcpp::traits::input_parameter< Rcpp::List >::type input_sort_counts(input_sort_countsSEXP);
rcpp_result_gen = Rcpp::wrap(run_smc(input_timeseries, input_prior, input_options, input_sort_matrices, input_sort_counts));
return rcpp_result_gen;
END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
{"_BayesMallowsSMC2_precompute_topological_sorts", (DL_FUNC) &_BayesMallowsSMC2_precompute_topological_sorts, 4},
{"_BayesMallowsSMC2_run_smc", (DL_FUNC) &_BayesMallowsSMC2_run_smc, 3},
{"_BayesMallowsSMC2_precompute_topological_sorts", (DL_FUNC) &_BayesMallowsSMC2_precompute_topological_sorts, 3},
{"_BayesMallowsSMC2_run_smc", (DL_FUNC) &_BayesMallowsSMC2_run_smc, 5},
{NULL, NULL, 0}
};

Expand Down
Loading