Skip to content

Commit 91e2c6a

Browse files
committed
Merge branch 'transfer-consensus' into expose-lapjv
2 parents d7d49d9 + 87e6cd8 commit 91e2c6a

14 files changed

+70
-126
lines changed

src/day_1985.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ using namespace Rcpp;
33

44
#include "tree_distances.h" /* includes <TreeTools/SplitList.h> */
55
#include "information.h"
6+
#include <cassert>
7+
#include <TreeTools/assert.h>
68

79
#include <TreeTools.h> /* for root_on_node() */
810
#include <TreeTools/root_tree.h> /* for root_on_node() */
@@ -96,7 +98,7 @@ double calc_consensus_info(const List &trees, const LogicalVector &phylo,
9698

9799
std::vector<ClusterTable> tables;
98100
if (std::size_t(n_trees) > tables.max_size()) {
99-
Rcpp::stop("Not enough memory available to compute consensus of so many trees"); // LCOV_EXCL_LINE
101+
ASSERT(false && "Not enough memory for consensus of so many trees"); // LCOV_EXCL_LINE
100102
}
101103

102104
tables.reserve(n_trees);
@@ -321,11 +323,9 @@ IntegerVector robinson_foulds_all_pairs(const List& tables) {
321323
// [[Rcpp::export]]
322324
double consensus_info(const List trees, const LogicalVector phylo,
323325
const NumericVector p) {
324-
if (p[0] > 1 + 1e-15) { // epsilon catches floating point error
325-
Rcpp::stop("p must be <= 1.0 in consensus_info()");
326-
} else if (p[0] < 0.5) {
327-
Rcpp::stop("p must be >= 0.5 in consensus_info()");
328-
}
326+
// Validated by R caller (ConsensusInfo checks p range)
327+
ASSERT(p[0] <= 1 + 1e-15 && "p must be <= 1.0 in consensus_info()");
328+
ASSERT(p[0] >= 0.5 && "p must be >= 0.5 in consensus_info()");
329329

330330
// Peek at tree size to choose stack vs heap allocation for the work buffer
331331
ClusterTable temp_table(Rcpp::List(trees(0)));

src/hmi.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,8 @@ double hierarchical_self_info(const std::vector<TreeDist::HNode>& nodes, size_t
139139
double HMI_xptr(SEXP ptr1, SEXP ptr2) {
140140
Rcpp::XPtr<TreeDist::HPart> hp1(ptr1);
141141
Rcpp::XPtr<TreeDist::HPart> hp2(ptr2);
142-
if (hp1->nodes[hp1->root].n_tip != hp2->nodes[hp2->root].n_tip) {
143-
Rcpp::stop("Trees must have the same number of leaves");
144-
}
142+
ASSERT(hp1->nodes[hp1->root].n_tip == hp2->nodes[hp2->root].n_tip
143+
&& "Trees must have the same number of leaves");
145144
return TreeDist::hierarchical_mutual_info(hp1->nodes, hp1->root,
146145
hp2->nodes, hp2->root).second;
147146
}
@@ -170,12 +169,8 @@ Rcpp::NumericVector EHMI_xptr(SEXP hp1_ptr, SEXP hp2_ptr,
170169
double tolerance = 0.01,
171170
int minResample = 36) {
172171

173-
if (minResample < 2) {
174-
Rcpp::stop("Must perform at least one resampling");
175-
}
176-
if (tolerance < 1e-8) {
177-
Rcpp::stop("Tolerance too low");
178-
}
172+
ASSERT(minResample >= 2 && "Must perform at least one resampling");
173+
ASSERT(tolerance >= 1e-8 && "Tolerance too low");
179174

180175
Rcpp::XPtr<TreeDist::HPart> hp1(hp1_ptr);
181176
Rcpp::XPtr<TreeDist::HPart> hp2(hp2_ptr);

src/hpart.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ size_t build_node_from_list(const RObject& node,
126126
if (Rf_isInteger(node) || Rf_isReal(node)) {
127127
const IntegerVector leaf_vec(node);
128128
if (leaf_vec.size() != 1) {
129-
Rcpp::stop("List must only contain integers, not vectors of integers"); // #nocov
129+
ASSERT(false && "List must only contain integers, not vectors of integers"); // #nocov
130130
}
131131
const int leaf_label = leaf_vec[0]; // 1-based R leaf label
132132
const size_t leaf_idx = leaf_label - 1; // 0-based label for HNode
@@ -172,7 +172,7 @@ size_t build_node_from_list(const RObject& node,
172172
}
173173

174174
// Invalid node type
175-
Rcpp::stop("Invalid node type"); // #nocov
175+
ASSERT(false && "Invalid node type"); // #nocov
176176
}
177177

178178

src/hpart_relabel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void recompute_bitsets_postorder(TreeDist::HPart &hpart, const size_t node_idx,
1313
if (node.children.empty()) {
1414
// Leaf node
1515
if (node.leaf_count != 1) {
16-
Rcpp::stop("Leaf node has leaf_count != 1"); // #nocov
16+
ASSERT(node.leaf_count == 1 && "Leaf node has leaf_count != 1"); // #nocov
1717
}
1818
int new_index = mapping[node.label]; // mapping is 0-based
1919
node.label = new_index;

src/information.h

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define _TREEDIST_INFO_H
33

44
#include <Rcpp/Lightest>
5-
#include <cassert> /* for log2() */
5+
#include <TreeTools/assert.h>
66
#include <cmath> /* for log2() */
77
#include "ints.h" /* for int16 */
88

@@ -48,14 +48,12 @@ __attribute__((constructor))
4848

4949
inline double split_phylo_info(const int32 n_in, const int32 *n_tip,
5050
const double p) {
51-
if (*n_tip > CT_MAX_LEAVES) {
52-
Rcpp::stop("This many leaves are not yet supported.");
53-
}
51+
ASSERT(*n_tip <= CT_MAX_LEAVES && "This many leaves are not yet supported.");
5452
const int32 n_out = *n_tip - n_in;
55-
assert(p > 0);
56-
assert(p <= 1);
57-
assert(n_in > 1);
58-
assert(n_out > 1);
53+
ASSERT(p > 0);
54+
ASSERT(p <= 1);
55+
ASSERT(n_in > 1);
56+
ASSERT(n_out > 1);
5957
if (p == 1) {
6058
return (l2unrooted[*n_tip] - l2rooted[n_in] - l2rooted[n_out]);
6159
} else {
@@ -74,14 +72,12 @@ inline double split_phylo_info(const int32 n_in, const int32 *n_tip,
7472

7573
inline double split_clust_info(const int32 n_in, const int32 *n_tip,
7674
const double p) {
77-
if (*n_tip > CT_MAX_LEAVES) {
78-
Rcpp::stop("This many leaves are not yet supported.");
79-
}
75+
ASSERT(*n_tip <= CT_MAX_LEAVES && "This many leaves are not yet supported.");
8076
const int32 n_out = *n_tip - n_in;
81-
assert(p > 0);
82-
assert(p <= 1);
83-
assert(n_in > 1);
84-
assert(n_out > 1);
77+
ASSERT(p > 0);
78+
ASSERT(p <= 1);
79+
ASSERT(n_in > 1);
80+
ASSERT(n_out > 1);
8581
return -p * (
8682
(((n_in * l2[n_in]) + (n_out * l2[n_out])) / *n_tip) - l2[*n_tip]
8783
);

src/mast.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <cstdlib>
22
#include <Rcpp/Lightest>
3+
#include <TreeTools/assert.h>
34
#include "ints.h"
45
using namespace Rcpp;
56

@@ -63,12 +64,8 @@ int cpp_mast (IntegerMatrix edge1, IntegerMatrix edge2, IntegerVector nTip) {
6364
n_internal = n_tip - 1,
6465
n_all_nodes = n_tip + n_internal,
6566
n_edge = edge1.nrow();
66-
if (edge2.nrow() != n_edge) {
67-
Rcpp::stop("Both trees must contain the same number of edges.");
68-
}
69-
if (n_tip > MAST_MAX_TIP) {
70-
Rcpp::stop("Tree too large; please contact maintainer for advice.");
71-
}
67+
ASSERT(edge2.nrow() == n_edge && "Both trees must contain the same number of edges.");
68+
ASSERT(n_tip <= MAST_MAX_TIP && "Tree too large; please contact maintainer for advice.");
7269

7370
int16
7471
t1_left[MAST_MAX_NODE] = {}, t1_right[MAST_MAX_NODE] = {},

src/nni_distance.cpp

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -226,16 +226,7 @@ grf_match nni_rf_matching (
226226

227227
ASSERT(n_splits > 0);
228228
ASSERT(n_tips > 3);
229-
if (n_tips > NNI_MAX_TIPS) {
230-
Rcpp::stop("Cannot calculate NNI distance for trees with so many tips.");
231-
}
232-
233-
// #nocov start
234-
if (static_cast<int64_t>(n_splits) * static_cast<int64_t>(n_bins) >
235-
static_cast<int64_t>(std::numeric_limits<int32_t>::max())) {
236-
Rcpp::stop("Cannot calculate NNI distance for trees with so many splits.");
237-
}
238-
// #nocov end
229+
ASSERT(n_tips <= NNI_MAX_TIPS); // Validated by exported wrapper
239230

240231
const int32_t last_bin = n_bins - 1;
241232
const int32_t unset_tips = (n_tips % SL_BIN_SIZE) ?
@@ -298,9 +289,7 @@ IntegerVector cpp_nni_distance(const IntegerMatrix& edge1,
298289
const IntegerMatrix& edge2,
299290
const IntegerVector& nTip) {
300291

301-
if (nTip[0] > NNI_MAX_TIPS) {
302-
Rcpp::stop("Cannot calculate NNI distance for trees with so many tips.");
303-
}
292+
ASSERT(nTip[0] <= NNI_MAX_TIPS && "Cannot calculate NNI distance for trees with so many tips.");
304293
const int32_t n_tip = static_cast<int32_t>(nTip[0]);
305294
const int32_t node_0 = n_tip;
306295
const int32_t node_0_r = n_tip + 1;
@@ -315,10 +304,8 @@ IntegerVector cpp_nni_distance(const IntegerMatrix& edge1,
315304
int32_t fack_score_bound = 0;
316305
int32_t li_score_bound = 0;
317306

318-
if (n_edge != int32_t(edge2.nrow())) {
319-
Rcpp::stop("Both trees must have the same number of edges. "
320-
"Is one rooted and the other unrooted?");
321-
}
307+
ASSERT(n_edge == int32_t(edge2.nrow())
308+
&& "Both trees must have the same number of edges.");
322309

323310
if (n_tip < 4) {
324311
return(IntegerVector::create(Named("lower") = 0,
@@ -348,7 +335,7 @@ IntegerVector cpp_nni_distance(const IntegerMatrix& edge1,
348335
const int32_t n_splits = n_distinct_edge - n_tip;
349336

350337
if (n_splits < 1) {
351-
Rcpp::stop("NNI distance is undefined for trees with no splits"); // #nocov
338+
ASSERT(false && "NNI distance is undefined for trees with no splits"); // #nocov
352339
}
353340

354341
std::unique_ptr<uint64_t[]> splits1(new uint64_t[n_splits * n_bin]);

src/spr.cpp

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,11 @@ IntegerVector calc_mismatch_size(const RawMatrix& x, const RawMatrix& y) {
4747

4848
// [[Rcpp::export]]
4949
IntegerVector mismatch_size (const RawMatrix& x, const RawMatrix& y) {
50-
if (x.rows() != y.rows()) {
51-
Rcpp::stop("`x` and `y` differ in number of splits.");
52-
}
53-
if (!x.hasAttribute("nTip")) {
54-
Rcpp::stop("`x` lacks nTip attribute");
55-
}
56-
if (!y.hasAttribute("nTip")) {
57-
Rcpp::stop("`y` lacks nTip attribute");
58-
}
59-
if (static_cast<int>(x.attr("nTip")) != static_cast<int>(y.attr("nTip"))) {
60-
Rcpp::stop("`x` and `y` differ in `nTip`");
61-
}
50+
ASSERT(x.rows() == y.rows() && "`x` and `y` differ in number of splits.");
51+
ASSERT(x.hasAttribute("nTip") && "`x` lacks nTip attribute");
52+
ASSERT(y.hasAttribute("nTip") && "`y` lacks nTip attribute");
53+
ASSERT(static_cast<int>(x.attr("nTip")) == static_cast<int>(y.attr("nTip"))
54+
&& "`x` and `y` differ in `nTip`");
6255
return calc_mismatch_size(x, y);
6356
}
6457

@@ -102,25 +95,17 @@ IntegerVector calc_confusion(const RawMatrix &x, const RawMatrix &y) {
10295

10396
// [[Rcpp::export]]
10497
IntegerVector confusion(const RawMatrix& x, const RawMatrix& y) {
105-
if (x.rows() != y.rows()) {
106-
Rcpp::stop("Input splits must contain same number of splits.");
107-
}
108-
if (!x.hasAttribute("nTip")) {
109-
Rcpp::stop("`x` lacks nTip attribute");
110-
}
111-
if (!y.hasAttribute("nTip")) {
112-
Rcpp::stop("`y` lacks nTip attribute");
113-
}
114-
if (static_cast<int>(x.attr("nTip")) != static_cast<int>(y.attr("nTip"))) {
115-
Rcpp::stop("`x` and `y` differ in `nTip`");
116-
}
98+
ASSERT(x.rows() == y.rows() && "Input splits must contain same number of splits.");
99+
ASSERT(x.hasAttribute("nTip") && "`x` lacks nTip attribute");
100+
ASSERT(y.hasAttribute("nTip") && "`y` lacks nTip attribute");
101+
ASSERT(static_cast<int>(x.attr("nTip")) == static_cast<int>(y.attr("nTip"))
102+
&& "`x` and `y` differ in `nTip`");
117103
return calc_confusion(x, y);
118104
}
119105

120106
IntegerMatrix reverse (const IntegerMatrix x) {
121-
if (double(x.nrow()) > double(std::numeric_limits<intx>::max())) {
122-
Rcpp::stop("This many edges are not (yet) supported.");
123-
}
107+
ASSERT(double(x.nrow()) <= double(std::numeric_limits<intx>::max())
108+
&& "This many edges are not (yet) supported.");
124109
const intx n_edge = intx(x.nrow());
125110
ASSERT(n_edge % 2 == 0); // Tree is binary
126111
IntegerMatrix ret(n_edge, 2);

src/spr_lookup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ int lookup_7(const SplitSet7& sp1, const SplitSet7& sp2) {
223223

224224
inline SplitSet7 read_splits(const Rcpp::RawVector& r) {
225225
if (r.size() != 4)
226-
Rcpp::stop("Expected a length-4 raw vector of splits");
226+
ASSERT(false && "Expected a length-4 raw vector of splits");
227227

228228
SplitSet7 sp{};
229229
for (int i = 0; i < 4; ++i) {

src/transfer_distance.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313

1414
#include <TreeTools/SplitList.h>
15+
#include <TreeTools/assert.h>
1516
#include <Rcpp/Lightest>
1617
#include <algorithm>
1718
#include <cstdint>
@@ -145,9 +146,8 @@ List cpp_transfer_dist(
145146
const RawMatrix& x, const RawMatrix& y,
146147
const IntegerVector& nTip
147148
) {
148-
if (x.cols() != y.cols()) {
149-
Rcpp::stop("Input splits must address same number of tips.");
150-
}
149+
// Validated by R caller (.ValidateSplitArgs or as.Splits with shared tipLabels)
150+
ASSERT(x.cols() == y.cols() && "Input splits must address same number of tips.");
151151

152152
const int n_tip = nTip[0];
153153
SplitList sl_x(x);
@@ -204,9 +204,8 @@ List cpp_transfer_dist_scored(
204204
const RawMatrix& x, const RawMatrix& y,
205205
const IntegerVector& nTip, bool scale
206206
) {
207-
if (x.cols() != y.cols()) {
208-
Rcpp::stop("Input splits must address same number of tips.");
209-
}
207+
// Validated by R caller (.ValidateSplitArgs or as.Splits with shared tipLabels)
208+
ASSERT(x.cols() == y.cols() && "Input splits must address same number of tips.");
210209

211210
const int n_tip = nTip[0];
212211
SplitList sl_x(x);

0 commit comments

Comments
 (0)