Skip to content

Commit

Permalink
Merge pull request #246 from snlab-ch/develop
Browse files Browse the repository at this point in the history
migraph 0.12.0
  • Loading branch information
jhollway authored Sep 27, 2022
2 parents a916526 + 5a2d233 commit decbde5
Show file tree
Hide file tree
Showing 100 changed files with 2,372 additions and 1,539 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: migraph
Title: Tools for Multimodal Network Analysis
Version: 0.11.3
Date: 2022-09-13
Version: 0.12.0
Date: 2022-09-23
Description: A set of tools for analysing multimodal networks.
All functions operate with matrices, edge lists,
and 'igraph', 'network', and 'tidygraph' objects,
Expand Down
34 changes: 34 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,16 @@ S3method(plot,graph_test)
S3method(plot,matrix)
S3method(plot,netlm)
S3method(plot,netlogit)
S3method(plot,network_test)
S3method(plot,node_measure)
S3method(plot,node_member)
S3method(plot,tie_measure)
S3method(print,graph_measure)
S3method(print,graph_motif)
S3method(print,graph_test)
S3method(print,network_measure)
S3method(print,network_motif)
S3method(print,network_test)
S3method(print,node_mark)
S3method(print,node_measure)
S3method(print,node_member)
Expand Down Expand Up @@ -305,7 +309,37 @@ export(layout_tbl_graph_ladder)
export(layout_tbl_graph_railway)
export(layout_tbl_graph_stressgrid)
export(mutate)
export(network_adhesion)
export(network_assortativity)
export(network_balance)
export(network_betweenness)
export(network_closeness)
export(network_cohesion)
export(network_components)
export(network_congruency)
export(network_core)
export(network_degree)
export(network_density)
export(network_diameter)
export(network_dims)
export(network_diversity)
export(network_dyad_census)
export(network_eigenvector)
export(network_equivalency)
export(network_factions)
export(network_homophily)
export(network_length)
export(network_mixed_census)
export(network_modularity)
export(network_node_attributes)
export(network_nodes)
export(network_reciprocity)
export(network_reg)
export(network_smallworld)
export(network_tie_attributes)
export(network_ties)
export(network_transitivity)
export(network_triad_census)
export(node_attribute)
export(node_automorphic_equivalence)
export(node_betweenness)
Expand Down
1,157 changes: 543 additions & 614 deletions NEWS.md

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions R/class_measures.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ make_tie_measure <- function(out, object) {
out
}

make_graph_measure <- function(out, object) {
class(out) <- c("graph_measure", class(out))
attr(out, "mode") <- graph_dims(object)
make_network_measure <- function(out, object) {
class(out) <- c("network_measure", class(out))
attr(out, "mode") <- network_dims(object)
out
}

Expand Down Expand Up @@ -41,7 +41,7 @@ print.tie_measure <- function(x, ...,
}

#' @export
print.graph_measure <- function(x, ...,
print.network_measure <- function(x, ...,
digits = 3) {
if (length(attr(x, "mode")) == 1) {
print(as.numeric(x), digits = digits)
Expand Down
8 changes: 4 additions & 4 deletions R/class_motifs.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ make_node_motif <- function(out, object) {
out
}

make_graph_motif <- function(out, object) {
class(out) <- c("graph_motif", class(out))
attr(out, "mode") <- graph_dims(object)
make_network_motif <- function(out, object) {
class(out) <- c("network_motif", class(out))
attr(out, "mode") <- network_dims(object)
out
}

Expand Down Expand Up @@ -37,7 +37,7 @@ summary.node_motif <- function(object, ...,
}

#' @export
print.graph_motif <- function(x, ...) {
print.network_motif <- function(x, ...) {
names <- list(names(x))
x <- as.numeric(x)
mat <- matrix(x, dimnames = names)
Expand Down
1 change: 0 additions & 1 deletion R/data_ison.R
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@
#' ggsave("man/figures/isonnetworkers.png", width = 5, height = 5, dpi = "screen")
#' ggsave("man/figures/isonnetworkers.pdf", width = 5, height = 5)
#' ```
#' \if{latex}{\figure{isonnetworkers.pdf}{options: width=7cm}}
#' @docType data
#' @keywords datasets
#' @name ison_networkers
Expand Down
4 changes: 2 additions & 2 deletions R/make_create.R
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ create_tree <- function(n,
#' @export
create_lattice <- function(n,
directed = FALSE) {
if (is_migraph(n)) n <- graph_dims(n)
if (is_migraph(n)) n <- network_dims(n)
igraph::make_lattice(n, directed = directed)
}

Expand Down Expand Up @@ -296,7 +296,7 @@ create_core <- function(n, membership = NULL) {
# Helper functions ------------------

infer_n <- function(n) {
if (is_migraph(n)) n <- graph_dims(n)
if (is_migraph(n)) n <- network_dims(n)
if (length(n) > 2) stop(paste("`n` should be a single integer for a one-mode network or",
"a vector of two integers for a two-mode network."))
n
Expand Down
14 changes: 7 additions & 7 deletions R/make_generate.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ NULL
#' @export
generate_random <- function(n, p = 0.5, directed = FALSE, with_attr = TRUE) {
if(is_migraph(n)){
m <- graph_ties(n)
m <- network_ties(n)
directed <- is_directed(n)
if(is_twomode(n)){
g <- igraph::sample_bipartite(graph_dims(n)[1],
graph_dims(n)[2],
g <- igraph::sample_bipartite(network_dims(n)[1],
network_dims(n)[2],
m = m, type = "gnm",
directed = directed,
mode = "out")
} else {
g <- igraph::sample_gnm(graph_nodes(n),
g <- igraph::sample_gnm(network_nodes(n),
m = m,
directed = directed)
}
Expand Down Expand Up @@ -152,7 +152,7 @@ generate_permutation <- function(object, with_attr = TRUE) {
# Helper functions ------------------

r1perm <- function(m) {
n <- sample(1:dim(m)[1])
n <- sample(seq_len(dim(m)[1]))
if(is_labelled(m)){
p <- matrix(data = m[n, n], nrow = dim(m)[1], ncol = dim(m)[2],
dimnames = dimnames(m))
Expand All @@ -163,8 +163,8 @@ r1perm <- function(m) {
}

r2perm <- function(m) {
n <- sample(1:dim(m)[1])
o <- sample(1:dim(m)[2])
n <- sample(seq_len(dim(m)[1]))
o <- sample(seq_len(dim(m)[2]))
if(is_labelled(m)){
p <- matrix(data = m[n, o], nrow = dim(m)[1], ncol = dim(m)[2],
dimnames = dimnames(m))
Expand Down
14 changes: 7 additions & 7 deletions R/manip_add.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ NULL
#' add_node_attribute(mpn_elite_usa_advice, "wealth", 1:14)
#' @export
add_node_attribute <- function(object, attr_name, vector){
if(length(vector)!=graph_nodes(object)){
if(is_twomode(object) && any(length(vector) == graph_dims(object))){
if(length(vector) == graph_dims(object)[1]){
vector <- c(vector, rep(NA, graph_dims(object)[2]))
} else if (length(vector) == graph_dims(object)[2]){
vector <- c(rep(NA, graph_dims(object)[1]), vector)
if(length(vector)!=network_nodes(object)){
if(is_twomode(object) && any(length(vector) == network_dims(object))){
if(length(vector) == network_dims(object)[1]){
vector <- c(vector, rep(NA, network_dims(object)[2]))
} else if (length(vector) == network_dims(object)[2]){
vector <- c(rep(NA, network_dims(object)[1]), vector)
}
} else
stop("Attribute vector must be same length as nodes in object.")
Expand All @@ -49,7 +49,7 @@ add_tie_attribute <- function(object, attr_name, vector){
#' @describeIn add Copies node attributes from a given graph into specified graph
#' @export
copy_node_attributes <- function(object, object2){
if(graph_nodes(object) != graph_nodes(object2))
if(network_nodes(object) != network_nodes(object2))
stop("Objects need to be of compatible dimensions.")
object <- as_igraph(object)
object2 <- as_igraph(object2)
Expand Down
4 changes: 2 additions & 2 deletions R/manip_as.R
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ as_igraph.siena <- function(object, twomode = NULL) {
}
} else {
# add names for one-mode y
y <- add_node_attribute(y, "name", as.character(seq_len(graph_nodes(y))))
y <- add_node_attribute(y, "name", as.character(seq_len(network_nodes(y))))
# join ties
if (isTRUE(is_twomode(x))) { # x is twomode but y is onemode
y <- as_edgelist(y)
Expand Down Expand Up @@ -510,7 +510,7 @@ as_igraph.siena <- function(object, twomode = NULL) {
# Add in first network as base and add names
out <- object$depvars[[ddvs[1]]][,,1] # first wave
if (is_twomode(out) == FALSE) {
out <- add_node_attribute(out, "name", as.character(seq_len(graph_nodes(out))))
out <- add_node_attribute(out, "name", as.character(seq_len(network_nodes(out))))
} else {
rownames(out) <- as.character(seq_len(nrow(out)))
colnames(out) <- as.character(paste0("N", seq_len(ncol(out))))
Expand Down
30 changes: 15 additions & 15 deletions R/manip_grab.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' from given network data.
#' They are also useful as helpers within other functions.
#'
#' `graph_*()` functions always relate to the overall graph or network,
#' `network_*()` functions always relate to the overall graph or network,
#' usually returning a scalar.
#' `node_*()` and `tie_*()` always return vectors the same length
#' as the number of nodes or edges in the network, respectively.
Expand All @@ -30,7 +30,7 @@ node_mode <- function(object){
if(is_twomode(object)){
out <- igraph::get.vertex.attribute(as_igraph(object), "type")
} else{
out <- rep(FALSE, graph_nodes(object))
out <- rep(FALSE, network_nodes(object))
}
# cannot use make_node_mark here because then eternal loop
class(out) <- c("node_mark", class(out))
Expand Down Expand Up @@ -63,7 +63,7 @@ tie_attribute <- function(object, attribute){
tie_weights <- function(object){
object <- as_igraph(object)
out <- igraph::get.edge.attribute(object, "weight")
if(is.null(out)) out <- rep(1, graph_ties(object))
if(is.null(out)) out <- rep(1, network_ties(object))
out
}

Expand All @@ -74,33 +74,33 @@ tie_weights <- function(object){
tie_signs <- function(object){
object <- as_igraph(object)
out <- igraph::get.edge.attribute(object, "sign")
if(is.null(out)) out <- rep(1, graph_ties(object))
if(is.null(out)) out <- rep(1, network_ties(object))
out
}

#' @describeIn grab Returns the total number of nodes (of any mode) in a network.
#' @examples
#' graph_nodes(ison_southern_women)
#' network_nodes(ison_southern_women)
#' @export
graph_nodes <- function(object){
network_nodes <- function(object){
igraph::vcount(as_igraph(object))
}

#' @describeIn grab Returns the number of edges in a network.
#' @examples
#' graph_ties(ison_southern_women)
#' network_ties(ison_southern_women)
#' @export
graph_ties <- function(object){
network_ties <- function(object){
igraph::ecount(as_igraph(object))
}

#' @describeIn grab Returns the dimensions of a network in a vector
#' as long as the number of modes in the network.
#' @examples
#' graph_dims(ison_southern_women)
#' graph_dims(to_mode1(ison_southern_women))
#' network_dims(ison_southern_women)
#' network_dims(to_mode1(ison_southern_women))
#' @export
graph_dims <- function(object){
network_dims <- function(object){
if(is_twomode(object)){
c(sum(!igraph::V(as_igraph(object))$type),
sum(igraph::V(as_igraph(object))$type))
Expand All @@ -112,17 +112,17 @@ graph_dims <- function(object){
#' @describeIn grab Returns a vector of nodal attributes in a network
#' @importFrom igraph list.vertex.attributes
#' @examples
#' graph_node_attributes(mpn_elite_mex)
#' network_node_attributes(mpn_elite_mex)
#' @export
graph_node_attributes <- function(object){
network_node_attributes <- function(object){
igraph::list.vertex.attributes(as_igraph(object))
}

#' @describeIn grab Returns a vector of edge attributes in a network
#' @importFrom igraph list.edge.attributes
#' @examples
#' graph_tie_attributes(mpn_elite_mex)
#' network_tie_attributes(mpn_elite_mex)
#' @export
graph_tie_attributes <- function(object){
network_tie_attributes <- function(object){
igraph::list.edge.attributes(as_igraph(object))
}
10 changes: 5 additions & 5 deletions R/manip_to.R
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ to_named.tbl_graph <- function(object, names = NULL) {
object <- object %>% mutate(name = names)
} else {
object <- object %>% mutate(name = sample(baby_names,
graph_nodes(object)))
network_nodes(object)))
}
object
}
Expand All @@ -329,7 +329,7 @@ to_named.igraph <- function(object, names = NULL) {
igraph::V(object)$name <- names
} else {
igraph::V(object)$name <- sample(baby_names,
graph_nodes(object))
network_nodes(object))
}
object
}
Expand All @@ -341,17 +341,17 @@ to_named.data.frame <- function(object, names = NULL) {
object[,2] <- names[as.numeric(object[,2])]
} else {
object[,1] <- sample(baby_names,
graph_nodes(object))[as.numeric(object[,1])]
network_nodes(object))[as.numeric(object[,1])]
object[,2] <- sample(baby_names,
graph_nodes(object))[as.numeric(object[,2])]
network_nodes(object))[as.numeric(object[,2])]
}
object
}

#' @export
to_named.matrix <- function(object, names = NULL) {
if(is.null(names)) names <- sample(baby_names,
graph_nodes(object))
network_nodes(object))
if(is_twomode(object)){
rownames(object) <- names[seq_len(nrow(object))]
colnames(object) <- names[(nrow(object)+1):length(names)]
Expand Down
8 changes: 4 additions & 4 deletions R/mark_is.R
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ is_directed <- function(object) UseMethod("is_directed")

#' @export
is_directed.data.frame <- function(object) {
!(graph_reciprocity(object) == 0 |
graph_reciprocity(object) == 1)
!(network_reciprocity(object) == 0 |
network_reciprocity(object) == 1)
}

#' @export
Expand Down Expand Up @@ -322,13 +322,13 @@ is_multiplex.matrix <- function(object){
#' @export
is_multiplex.tbl_graph <- function(object){
igraph::any_multiple(object) |
length(graph_tie_attributes(object)) > 1
length(network_tie_attributes(object)) > 1
}

#' @export
is_multiplex.igraph <- function(object){
igraph::any_multiple(object) |
length(graph_tie_attributes(object)) > 1
length(network_tie_attributes(object)) > 1
}

#' @export
Expand Down
2 changes: 1 addition & 1 deletion R/mark_nodes.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ node_is_cutpoint <- function(object){
"names")
names(out) <- node_names(object)
} else {
out <- 1:graph_nodes(object) %in%
out <- 1:network_nodes(object) %in%
igraph::articulation_points(as_igraph(object))
}
make_node_mark(out, object)
Expand Down
2 changes: 1 addition & 1 deletion R/mark_ties.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ tie_is_reciprocated <- function(object){
#' @export
tie_is_bridge <- function(object){
num_comp <- length( igraph::decompose(as_igraph(object)) )
out <- vapply(seq_len(graph_ties(object)), function(x){
out <- vapply(seq_len(network_ties(object)), function(x){
length( igraph::decompose(igraph::delete.edges(object, x)) ) > num_comp
}, FUN.VALUE = logical(1))
if(is_labelled(object))
Expand Down
Loading

0 comments on commit decbde5

Please sign in to comment.