From 94966c3c3fdcc8ed3b0d83b65240c07e3cb2e041 Mon Sep 17 00:00:00 2001 From: Benjamin James Date: Thu, 8 Feb 2024 14:52:54 -0500 Subject: [PATCH] Removed graph until fixed, keep main stable --- R/RcppExports.R | 20 ------- src/RcppExports.cpp | 69 ------------------------- src/bigclam.hpp | 59 --------------------- src/graph.hpp | 123 -------------------------------------------- src/r_bigclam.cpp | 23 --------- src/r_graph.cpp | 31 ----------- 6 files changed, 325 deletions(-) delete mode 100644 src/bigclam.hpp delete mode 100644 src/graph.hpp delete mode 100644 src/r_bigclam.cpp delete mode 100644 src/r_graph.cpp diff --git a/R/RcppExports.R b/R/RcppExports.R index 09882cf..b68a3d6 100644 --- a/R/RcppExports.R +++ b/R/RcppExports.R @@ -1,26 +1,6 @@ # Generated by using Rcpp::compileAttributes() -> do not edit by hand # Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 -r_bigclam_init <- function(X, n_comm) { - .Call('_scdemon_r_bigclam_init', PACKAGE = 'scdemon', X, n_comm) -} - -r_bigclam <- function(C, n_comm, max_iter = 1000L) { - .Call('_scdemon_r_bigclam', PACKAGE = 'scdemon', C, n_comm, max_iter) -} - -r_graph_conductance <- function(X, min_deg) { - .Call('_scdemon_r_graph_conductance', PACKAGE = 'scdemon', X, min_deg) -} - -r_graph_conductance_row <- function(X, i, degree, deg_sum, min_deg) { - .Call('_scdemon_r_graph_conductance_row', PACKAGE = 'scdemon', X, i, degree, deg_sum, min_deg) -} - -r_graph_degree <- function(X, diag = FALSE) { - .Call('_scdemon_r_graph_degree', PACKAGE = 'scdemon', X, diag) -} - r_ols_beta <- function(X, Y) { .Call('_scdemon_r_ols_beta', PACKAGE = 'scdemon', X, Y) } diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 7459c89..1fa66e7 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -11,70 +11,6 @@ Rcpp::Rostream& Rcpp::Rcout = Rcpp::Rcpp_cout_get(); Rcpp::Rostream& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get(); #endif -// r_bigclam_init -Eigen::MatrixXd r_bigclam_init(const Eigen::Map >& X, int n_comm); -RcppExport SEXP _scdemon_r_bigclam_init(SEXP XSEXP, SEXP n_commSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< const Eigen::Map >& >::type X(XSEXP); - Rcpp::traits::input_parameter< int >::type n_comm(n_commSEXP); - rcpp_result_gen = Rcpp::wrap(r_bigclam_init(X, n_comm)); - return rcpp_result_gen; -END_RCPP -} -// r_bigclam -Eigen::ArrayXXd r_bigclam(const Eigen::Map >& C, int n_comm, int max_iter); -RcppExport SEXP _scdemon_r_bigclam(SEXP CSEXP, SEXP n_commSEXP, SEXP max_iterSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< const Eigen::Map >& >::type C(CSEXP); - Rcpp::traits::input_parameter< int >::type n_comm(n_commSEXP); - Rcpp::traits::input_parameter< int >::type max_iter(max_iterSEXP); - rcpp_result_gen = Rcpp::wrap(r_bigclam(C, n_comm, max_iter)); - return rcpp_result_gen; -END_RCPP -} -// r_graph_conductance -Eigen::ArrayXd r_graph_conductance(const Eigen::Map >& X, int min_deg); -RcppExport SEXP _scdemon_r_graph_conductance(SEXP XSEXP, SEXP min_degSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< const Eigen::Map >& >::type X(XSEXP); - Rcpp::traits::input_parameter< int >::type min_deg(min_degSEXP); - rcpp_result_gen = Rcpp::wrap(r_graph_conductance(X, min_deg)); - return rcpp_result_gen; -END_RCPP -} -// r_graph_conductance_row -double r_graph_conductance_row(const Eigen::Map >& X, Eigen::Index i, const Eigen::ArrayXd& degree, double deg_sum, int min_deg); -RcppExport SEXP _scdemon_r_graph_conductance_row(SEXP XSEXP, SEXP iSEXP, SEXP degreeSEXP, SEXP deg_sumSEXP, SEXP min_degSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< const Eigen::Map >& >::type X(XSEXP); - Rcpp::traits::input_parameter< Eigen::Index >::type i(iSEXP); - Rcpp::traits::input_parameter< const Eigen::ArrayXd& >::type degree(degreeSEXP); - Rcpp::traits::input_parameter< double >::type deg_sum(deg_sumSEXP); - Rcpp::traits::input_parameter< int >::type min_deg(min_degSEXP); - rcpp_result_gen = Rcpp::wrap(r_graph_conductance_row(X, i, degree, deg_sum, min_deg)); - return rcpp_result_gen; -END_RCPP -} -// r_graph_degree -Eigen::ArrayXd r_graph_degree(const Eigen::Map >& X, bool diag); -RcppExport SEXP _scdemon_r_graph_degree(SEXP XSEXP, SEXP diagSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< const Eigen::Map >& >::type X(XSEXP); - Rcpp::traits::input_parameter< bool >::type diag(diagSEXP); - rcpp_result_gen = Rcpp::wrap(r_graph_degree(X, diag)); - return rcpp_result_gen; -END_RCPP -} // r_ols_beta Eigen::MatrixXd r_ols_beta(const Eigen::Map& X, const Eigen::Map& Y); RcppExport SEXP _scdemon_r_ols_beta(SEXP XSEXP, SEXP YSEXP) { @@ -128,11 +64,6 @@ END_RCPP } static const R_CallMethodDef CallEntries[] = { - {"_scdemon_r_bigclam_init", (DL_FUNC) &_scdemon_r_bigclam_init, 2}, - {"_scdemon_r_bigclam", (DL_FUNC) &_scdemon_r_bigclam, 3}, - {"_scdemon_r_graph_conductance", (DL_FUNC) &_scdemon_r_graph_conductance, 2}, - {"_scdemon_r_graph_conductance_row", (DL_FUNC) &_scdemon_r_graph_conductance_row, 5}, - {"_scdemon_r_graph_degree", (DL_FUNC) &_scdemon_r_graph_degree, 2}, {"_scdemon_r_ols_beta", (DL_FUNC) &_scdemon_r_ols_beta, 2}, {"_scdemon_r_ols_resid", (DL_FUNC) &_scdemon_r_ols_resid, 3}, {"_scdemon_r_robust_se_X", (DL_FUNC) &_scdemon_r_robust_se_X, 2}, diff --git a/src/bigclam.hpp b/src/bigclam.hpp deleted file mode 100644 index 209d6c1..0000000 --- a/src/bigclam.hpp +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef BIGCLAM_HPP -#define BIGCLAM_HPP -#include -#include -#include -#include -#include - -#include "graph.hpp" -template -Eigen::MatrixXd bigclam_init(const Eigen::SparseMatrixBase &C, int n_comm) -{ - Eigen::ArrayXXd F = Eigen::ArrayXXd::Random(std::min(C.rows(), C.cols()), - n_comm); - return ((F + 1.) * 0.5).matrix(); -} - -template -Eigen::ArrayXd bigclam_gradient(const Eigen::MatrixBase &node, - const Eigen::MatrixBase &neighbors, - const Eigen::ArrayBase &Fsum0) -{ - Eigen::ArrayXd raw = (node * neighbors.transpose()).array().min(15).max(-15).eval(); - Eigen::ArrayXd scores = (-raw).exp() / (1 - (-raw).exp()); - Eigen::ArrayXd neighbor_grad = (scores.matrix() * neighbors).array().eval(); - Eigen::ArrayXd no_neigh_grad = (Fsum0 - node.array() - neighbors.array().colwise().sum()).eval(); - return neighbor_grad - no_neigh_grad; -} - -// bg edge: \epsilon = 2|E|/(|V||V-1|) -// todo find out why F increases with # of iterations -#include -template -Eigen::ArrayXXd bigclam(const Eigen::SparseCompressedBase &C, - int n_comm, int max_iter=1000, - double conv_threshold=1e-3, double eta=0.005, - double min_value=0.00001, double max_value=20) -{ - Eigen::MatrixXd F = bigclam_init(C, n_comm); - Eigen::ArrayXd Fsum0 = F.colwise().sum(); - std::cout << "Fsum0 size: " << Fsum0.size() << " data: " << Fsum0 << std::endl; - for (int iter = 0; iter < max_iter; iter++) { - std::cout << "Iter: " << iter << std::endl; - for (Eigen::Index i = 0; i < C.cols(); i++) { - std::vector neb_idx = graph_neighbors(C, i); - //std::cout << i << " Neighbor: " << neb_idx.size() << std::endl; - if (neb_idx.size() > 0) { - Eigen::MatrixXd F_neighbors = matrix_slice_rows(F, neb_idx); - Eigen::ArrayXd F_node = F.row(i); - Eigen::ArrayXd gradient = bigclam_gradient(F_node.matrix(), F_neighbors, Fsum0); - F.row(i) = F.row(i).array() + eta * gradient; - F.row(i) = F.row(i).array().max(min_value).min(max_value); - Fsum0 = Fsum0 - F_node + F.row(i).array(); - } - } - } - return F.array(); -} -#endif diff --git a/src/graph.hpp b/src/graph.hpp deleted file mode 100644 index 116ec08..0000000 --- a/src/graph.hpp +++ /dev/null @@ -1,123 +0,0 @@ -#ifndef GRAPH_HPP -#define GRAPH_HPP - -#include -#include -#include -#include -#include -#include -#include - -#include "implprogress.hpp" -template -std::vector > graph_neighbors(const Eigen::SparseCompressedBase &mat, - Eigen::Index outerIndex, - bool weighted=true) -{ - std::vector > out; - for (typename T::InnerIterator it(mat, outerIndex); it; ++it) { - out.emplace_back(T::IsRowMajor ? it.col() : it.row(), - weighted ? 1. / it.value() : 1.); - } - return out; -} - -/* Since Eigen 3.4 is not supported in Rcpp, write own iterator */ -template -Eigen::MatrixXd matrix_slice_rows(const Eigen::DenseBase &mat, - const std::vector > &indices) -{ - Eigen::MatrixXd out(indices.size(), mat.cols()); - for (std::pair i : indices) { - Eigen::ArrayXd row = mat.row(i.first).array() * i.second; - out << row.matrix(); - } - return out; -} - -template -double graph_conductance_row(const Eigen::SparseCompressedBase &mat, Eigen::Index i, const Eigen::ArrayXd& degree, double deg_sum, int min_deg=5) -{ - std::unordered_set subgraph = {i}; - for (typename T::InnerIterator it(mat, i); it; ++it) { - Eigen::Index j = T::IsRowMajor ? it.col() : it.row(); - if (i != j) { - subgraph.insert(j); - } - } - if (subgraph.size() < min_deg) { - return 1.; - } - double subgraph_sum = 0; - double subgraph_degree_sum = 0; - for (Eigen::Index j : subgraph) { - // For each neighbor, add the degrees - subgraph_degree_sum += degree(j); - for (typename T::InnerIterator it(mat, j); it; ++it) { - Eigen::Index k = T::IsRowMajor ? it.col() : it.row(); - if (j == k) { - continue; - } else if (subgraph.find(k) != subgraph.end()) { - subgraph_sum += it.value(); - } - } - } - /* - * Numerator is the edge crossings: - * Denom is deg sum and its complement - */ - double numer = subgraph_degree_sum - subgraph_sum; - double denom = std::min(subgraph_degree_sum, deg_sum - subgraph_degree_sum); - return numer / denom; -} - -template -Eigen::ArrayXd graph_degree(const Eigen::SparseCompressedBase &mat, bool diag=false) -{ - Eigen::Index size; - if (T::IsRowMajor) { - size = mat.rows(); - } else { - size = mat.cols(); - } - Eigen::ArrayXd degree = Eigen::ArrayXd::Zero(size); -#if defined(_OPENMP) -#pragma omp parallel for -#endif - for (Eigen::Index i = 0; i < size; i++) { - double cur = 0; - for (typename T::InnerIterator it(mat, i); it; ++it) { - Eigen::Index j = T::IsRowMajor ? it.col() : it.row(); - if (diag || (i != j)) { - cur += it.value(); - } - degree(i) = cur; - } - } - return degree; -} -template -Eigen::ArrayXd graph_conductance(const Eigen::SparseCompressedBase &mat, int min_deg=5) -{ - Eigen::ArrayXd degree = graph_degree(mat, false); - const double deg_sum = degree.sum(); - ImplProgress p(mat.cols()); - Eigen::ArrayXd conductance = Eigen::ArrayXd::Ones(degree.size()); -#if defined(_OPENMP) -#pragma omp parallel for -#endif - for (Eigen::Index i = 0; i < mat.cols(); i++) { - /* - * For each index, find the subgraph defined - * by the immediate neighbors of row i - */ - if (!p.check_abort()) { - p.increment(); - double cond = graph_conductance_row(mat, i, degree, deg_sum, min_deg); - conductance(i) = cond; - } - } - return conductance; -} -#endif diff --git a/src/r_bigclam.cpp b/src/r_bigclam.cpp deleted file mode 100644 index 0b4b004..0000000 --- a/src/r_bigclam.cpp +++ /dev/null @@ -1,23 +0,0 @@ - -#include -// [[Rcpp::plugins(openmp)]] - -#include -// [[Rcpp::depends(RcppEigen)]] - -#include "bigclam.hpp" - -// [[Rcpp::export]] -Eigen::MatrixXd r_bigclam_init(const Eigen::Map > &X, - int n_comm) -{ - return bigclam_init(X, n_comm); -} - - -// [[Rcpp::export]] -Eigen::ArrayXXd r_bigclam(const Eigen::Map >&C, - int n_comm, int max_iter=1000) -{ - return bigclam(C, n_comm, max_iter); -} diff --git a/src/r_graph.cpp b/src/r_graph.cpp deleted file mode 100644 index a9ea497..0000000 --- a/src/r_graph.cpp +++ /dev/null @@ -1,31 +0,0 @@ - -#include -// [[Rcpp::plugins(openmp)]] - -#include -// [[Rcpp::depends(RcppEigen)]] - -#include "graph.hpp" - -// [[Rcpp::export]] -Eigen::ArrayXd r_graph_conductance(const Eigen::Map > &X, - int min_deg) -{ - return graph_conductance(X, min_deg); -} - -// [[Rcpp::export]] -double r_graph_conductance_row(const Eigen::Map > &X, - Eigen::Index i, - const Eigen::ArrayXd& degree, - double deg_sum, - int min_deg) -{ - return graph_conductance_row(X, i, degree, deg_sum, min_deg); -} -// [[Rcpp::export]] -Eigen::ArrayXd r_graph_degree(const Eigen::Map > &X, - bool diag=false) -{ - return graph_degree(X, diag); -}