From 34317691db3b9ef37c53281e8a6af76b120fe5cf Mon Sep 17 00:00:00 2001 From: James Hollway Date: Sat, 29 Aug 2026 07:28:30 +0200 Subject: [PATCH] Fixed `cluster_cosine()` to cluster nodes and not census features (thanks @Kaladani) --- DESCRIPTION | 2 +- NEWS.md | 6 ++++++ R/method_cluster.R | 2 +- tests/testthat/test-model_cluster.R | 12 +++++++++++- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c8a656c..3137473 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: netrics Title: Many Marks, Measures, Memberships, and Motifs for Networks -Version: 1.0.0 +Version: 1.0.1 Description: Many tools for calculating network, node, or tie marks, measures, motifs and memberships of many different types of networks. Marks identify structural positions, measures quantify network properties, diff --git a/NEWS.md b/NEWS.md index 47f738c..66e6cb0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,9 @@ +# netrics 1.0.1 + +## Methods + +- Fixed `cluster_cosine()` to cluster nodes and not census features (thanks @Kaladani) + # netrics 1.0.0 ## Package diff --git a/R/method_cluster.R b/R/method_cluster.R index a34ffa0..7d567d7 100644 --- a/R/method_cluster.R +++ b/R/method_cluster.R @@ -51,7 +51,7 @@ cluster_hierarchical <- function(motif, distance){ #' and this is given to `stats::hclust` to enable dendrogram construction etc. #' @export cluster_cosine <- function(motif, distance){ - cosines <- manynet::to_cosine(motif) + cosines <- manynet::to_cosine(t(motif)) dissimilarity <- 1 - cosines distances <- stats::dist(dissimilarity, method = distance) hc <- stats::hclust(distances) diff --git a/tests/testthat/test-model_cluster.R b/tests/testthat/test-model_cluster.R index ed5a03e..0d47360 100644 --- a/tests/testthat/test-model_cluster.R +++ b/tests/testthat/test-model_cluster.R @@ -2,7 +2,17 @@ test_that("cluster_cosine works", { expect_s3_class(cluster_cosine(node_x_triad(ison_monks), distance = "euclidean"), "hclust") }) -test_that("cluster_cosine works", { +test_that("cluster_cosine clusters nodes and not census features", { + # `ison_algebra` gives a 16 x 96 census, + # so a membership of the wrong length is visible here + expect_equal(length(node_in_structural(ison_algebra, cluster = "cosine")), + c(net_nodes(ison_algebra))) + expect_equal(nrow(as.matrix(cluster_cosine(node_x_tie(ison_algebra), + distance = "euclidean")$distances)), + c(net_nodes(ison_algebra))) +}) + +test_that("cluster_concor works", { unlab_2mode <- generate_random(c(6,6)) expect_s3_class(cluster_concor(unlab_2mode, node_x_tetrad(unlab_2mode)), "hclust") })