Skip to content

Commit 7a9144c

Browse files
Copilotkrivit
andcommitted
Address code review feedback: fix na.omit handling and remove network.density
Co-authored-by: krivit <15682462+krivit@users.noreply.github.com>
1 parent 48f0a07 commit 7a9144c

2 files changed

Lines changed: 33 additions & 59 deletions

File tree

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ S3method(list.edge.attributes,networkLite)
4646
S3method(list.network.attributes,networkLite)
4747
S3method(list.vertex.attributes,networkLite)
4848
S3method(mixingmatrix,networkLite)
49-
S3method(network.density,networkLite)
5049
S3method(network.edgecount,networkLite)
5150
S3method(network.naedgecount,networkLite)
5251
S3method(networkLite,edgelist)

R/network_methods.R

Lines changed: 33 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ get.edgeIDs.networkLite <- function(x, v, alter = NULL,
130130

131131
# Filter out missing edges if na.omit is TRUE
132132
if (na.omit && length(eids) > 0) {
133-
eids <- eids[!NVL(x$el$na[eids], FALSE)]
133+
# Check each edge's na attribute
134+
na_vals <- sapply(eids, function(i) isTRUE(x$el$na[[i]]))
135+
eids <- eids[!na_vals]
134136
}
135137

136138
return(eids)
@@ -158,8 +160,11 @@ get.edgeIDs.networkLite <- function(x, v, alter = NULL,
158160
eid <- which(x$el$.tail == v & x$el$.head == alter)
159161

160162
# If na.omit is TRUE, exclude edges with na = TRUE
161-
if (na.omit && length(eid) > 0 && isTRUE(x$el$na[eid])) {
162-
return(numeric(0))
163+
if (na.omit && length(eid) > 0) {
164+
na_val <- x$el$na[[eid]] # Use [[]] to get the scalar value from list
165+
if (isTRUE(na_val)) {
166+
return(numeric(0))
167+
}
163168
}
164169

165170
return(eid)
@@ -260,7 +265,9 @@ get.edges.networkLite <- function(x, v, alter, neighborhood = c("combined", "out
260265

261266
# Filter out missing edges if na.omit is TRUE
262267
if (na.omit && length(eids) > 0) {
263-
eids <- eids[!NVL(x$el$na[eids], FALSE)]
268+
# Check each edge's na attribute
269+
na_vals <- sapply(eids, function(i) isTRUE(x$el$na[[i]]))
270+
eids <- eids[!na_vals]
264271
}
265272

266273
return(eids)
@@ -298,7 +305,9 @@ get.neighborhood.networkLite <- function(x, v, type = c("combined", "out", "in")
298305
if (type == "out" || type == "combined") {
299306
out_idx <- which(x$el$.tail %in% v)
300307
if (na.omit) {
301-
out_idx <- out_idx[!NVL(x$el$na[out_idx], FALSE)]
308+
# Filter out missing edges
309+
na_vals <- sapply(out_idx, function(i) isTRUE(x$el$na[[i]]))
310+
out_idx <- out_idx[!na_vals]
302311
}
303312
neighbors <- c(neighbors, x$el$.head[out_idx])
304313
}
@@ -307,7 +316,9 @@ get.neighborhood.networkLite <- function(x, v, type = c("combined", "out", "in")
307316
if (type == "in" || type == "combined") {
308317
in_idx <- which(x$el$.head %in% v)
309318
if (na.omit) {
310-
in_idx <- in_idx[!NVL(x$el$na[in_idx], FALSE)]
319+
# Filter out missing edges
320+
na_vals <- sapply(in_idx, function(i) isTRUE(x$el$na[[i]]))
321+
in_idx <- in_idx[!na_vals]
311322
}
312323
neighbors <- c(neighbors, x$el$.tail[in_idx])
313324
}
@@ -324,6 +335,7 @@ get.neighborhood.networkLite <- function(x, v, type = c("combined", "out", "in")
324335
#' @param x A `networkLite` object.
325336
#' @param vi,vj Vertex IDs.
326337
#' @param na.omit Logical; whether to treat missing edges as non-existent.
338+
#' Default is FALSE to match network package behavior.
327339
#' @param ... additional arguments.
328340
#'
329341
#' @return Logical indicating whether an edge exists from vi to vj (or
@@ -332,65 +344,20 @@ get.neighborhood.networkLite <- function(x, v, type = c("combined", "out", "in")
332344
#' @details
333345
#' Tests whether an edge exists between the specified vertices. For directed
334346
#' networks, tests for an edge from vi to vj. For undirected networks, tests
335-
#' for an edge between vi and vj (order does not matter).
347+
#' for an edge between vi and vj (order does not matter). Note that the
348+
#' default for na.omit is FALSE, meaning missing edges are treated as
349+
#' present by default (consistent with network package behavior).
336350
#'
337351
#' @export
338352
#'
339353
is.adjacent.networkLite <- function(x, vi, vj, na.omit = FALSE, ...) {
354+
# Explicitly pass na.omit to get.edgeIDs
355+
# Note: get.edgeIDs has default na.omit=TRUE, but we override it here
340356
eid <- get.edgeIDs(x, vi, vj, na.omit = na.omit)
341357
length(eid) > 0
342358
}
343359

344360

345-
#' @rdname network.density
346-
#'
347-
#' @title Calculate Network Density
348-
#'
349-
#' @param x A `networkLite` object.
350-
#' @param na.omit Logical; whether to exclude missing edges from the calculation.
351-
#' @param discount.bipartite Logical; for bipartite networks, whether to compute
352-
#' density based on within-mode edges (if FALSE) or only between-mode edges (if TRUE).
353-
#' @param ... additional arguments.
354-
#'
355-
#' @return The network density (proportion of possible edges that are present).
356-
#'
357-
#' @details
358-
#' Calculates the density of the network as the ratio of the number of edges
359-
#' to the number of possible edges. For directed networks, the number of
360-
#' possible edges is n*(n-1). For undirected networks, it is n*(n-1)/2,
361-
#' where n is the network size. For bipartite networks, the number of
362-
#' possible edges is n1*n2 when discount.bipartite = FALSE, where n1 and n2
363-
#' are the sizes of the two modes.
364-
#'
365-
#' @export
366-
#'
367-
network.density.networkLite <- function(x, na.omit = TRUE, discount.bipartite = FALSE, ...) {
368-
n <- network.size(x)
369-
370-
if (n == 0) {
371-
return(NaN)
372-
}
373-
374-
edge_count <- network.edgecount(x, na.omit = na.omit)
375-
376-
if (is.bipartite(x) && !discount.bipartite) {
377-
b1 <- x %n% "bipartite"
378-
b2 <- n - b1
379-
max_edges <- b1 * b2
380-
} else if (is.directed(x)) {
381-
max_edges <- n * (n - 1)
382-
} else {
383-
max_edges <- n * (n - 1) / 2
384-
}
385-
386-
if (max_edges == 0) {
387-
return(NaN)
388-
}
389-
390-
edge_count / max_edges
391-
}
392-
393-
394361
#' @rdname has.edges
395362
#'
396363
#' @title Test for Edge Existence in Network
@@ -415,6 +382,14 @@ has.edges.networkLite <- function(net, v = seq_len(network.size(net)), ...) {
415382
v <- as.integer(v)
416383

417384
# Check if any edges involve the specified vertices
418-
any(net$el$.tail %in% v | net$el$.head %in% v) &&
419-
any(!NVL(net$el$na[net$el$.tail %in% v | net$el$.head %in% v], FALSE))
385+
if (length(v) > 0 && network.edgecount(net, na.omit = TRUE) > 0) {
386+
edge_indices <- which(net$el$.tail %in% v | net$el$.head %in% v)
387+
if (length(edge_indices) > 0) {
388+
# Check if any of these edges are not missing
389+
na_vals <- sapply(edge_indices, function(i) isTRUE(net$el$na[[i]]))
390+
return(any(!na_vals))
391+
}
392+
}
393+
394+
return(FALSE)
420395
}

0 commit comments

Comments
 (0)