Skip to content

Commit

Permalink
Revert "update igraph use; interrupt depht-first search in n.comp.nb,…
Browse files Browse the repository at this point in the history
… add igraph argument to n.comp.nb"

This reverts commit e64720c.
  • Loading branch information
rsbivand committed Jun 12, 2024
1 parent e64720c commit 779ef78
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 54 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: spdep
Version: 1.3-6
Date: 2024-06-11
Version: 1.3-5
Date: 2024-06-10
Title: Spatial Dependence: Weighting Schemes, Statistics
Encoding: UTF-8
Authors@R: c(person("Roger", "Bivand", role = c("cre", "aut"),
Expand Down
6 changes: 1 addition & 5 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
# Version 1.3-6 (development)

* add user interrupt to `n.comp.nb` #160

# Version 1.3-5 (2024-06-10)
# Version 1.3-5 (development)

* #157 migrate ESRI Shapefile to GPKG files; convert bhicv.shp to GPKG

Expand Down
32 changes: 6 additions & 26 deletions R/components.R
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
# Copyright 2001 by Nicholas Lewin-Koh, igraph added RSB 2024
# Copyright 2001 by Nicholas Lewin-Koh
#


n.comp.nb <- function(nb.obj, igraph=FALSE){
n.comp.nb <- function(nb.obj){
if(!inherits(nb.obj,"nb"))stop("not a neighbours list")
stopifnot(is.logical(igraph))
stopifnot(length(igraph) == 1L)
nb.sym <- is.symmetric.nb(nb.obj)
if (igraph) {
if (!requireNamespace("igraph", quietly=TRUE)) {
igraph <- !igraph
warning("igraph not available, set FALSE")
}
}
if (!igraph) {
if (!nb.sym) nb.obj <- make.sym.nb(nb.obj)
comp <- rep(0,length(nb.obj))
comp <- .Call("g_components", nb.obj, as.integer(comp), PACKAGE="spdep")
answ <- list(nc=length(unique(comp)), comp.id=comp)
} else {
stopifnot(requireNamespace("igraph", quietly=TRUE))
stopifnot(requireNamespace("spatialreg", quietly=TRUE))
B <- as(nb2listw(nb.obj, style="B", zero.policy=TRUE), "CsparseMatrix")

g1 <- igraph::graph_from_adjacency_matrix(B,
mode=ifelse(nb.sym, "undirected", "directed"))
c1 <- igraph::components(g1, mode="weak")
answ <- list(nc=c1$no, comp.id=c1$membership)
}
nb.obj <- make.sym.nb(nb.obj)
comp <- rep(0,length(nb.obj))
comp <- .Call("g_components", nb.obj, as.integer(comp), PACKAGE="spdep")
answ <- list(nc=length(unique(comp)), comp.id=comp)
answ
}

32 changes: 17 additions & 15 deletions man/compon.Rd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
% Copyright 2001-24 by Roger S. Bivand and Nicholas Lewin-Koh
% Copyright 2001 by Roger S. Bivand and Nicholas Lewin-Koh
\name{Graph Components}
\alias{n.comp.nb}
%\alias{reach.ij}
Expand All @@ -8,13 +8,11 @@
\code{n.comp.nb()} finds the number of disjoint connected subgraphs in the graph depicted by \code{nb.obj} - a spatial neighbours list object.
}
\usage{
n.comp.nb(nb.obj, igraph=FALSE)
n.comp.nb(nb.obj)
}
\arguments{
\item{nb.obj}{a neighbours list object of class \code{nb}}
\item{igraph}{default \code{FALSE}, if \code{TRUE} use \code{igraph::components} after converting the neighbour object to a sparse adjacency matrix then a graph}
}
\details{If \code{igraph=TRUE} and \code{attr(nb.obj, "sym")} is \code{FALSE}, the components of the directed graph will be found by a simple breadth-first search; if \code{igraph=FALSE} and \code{attr(nb.obj, "sym")} is \code{FALSE}, the object will be made symmetric (which may be time-consuming with large numbers of neighbours) and the components found by depth-first search. If \code{igraph=TRUE} or \code{FALSE} and \code{attr(nb.obj, "sym")} is \code{TRUE}, the components of the directed graph will be found by depth-first search.}
\value{
A list of:
\item{nc}{number of disjoint connected subgraphs}
Expand All @@ -34,11 +32,11 @@ table(res$comp.id)
plot(col2, coords, add=TRUE)
points(coords, col=res$comp.id, pch=16)
run <- FALSE
if (require("igraph", quietly=TRUE) && require("spatialreg", quietly=TRUE)) run <- TRUE
if (require(igraph, quietly=TRUE) && require(spatialreg, quietly=TRUE)) run <- TRUE
if (run) {
B <- as(nb2listw(col2, style="B", zero.policy=TRUE), "CsparseMatrix")
g1 <- graph_from_adjacency_matrix(B, mode="undirected")
c1 <- components(g1)
g1 <- graph.adjacency(B, mode="undirected")
c1 <- clusters(g1)
print(c1$no == res$nc)
}
if (run) {
Expand All @@ -48,17 +46,21 @@ if (run) {
print(all.equal(c1$csize, c(table(res$comp.id)), check.attributes=FALSE))
}
if (run) {
resi <- n.comp.nb(col2, igraph=TRUE)
print(resi$nc == res$nc)
W <- as(nb2listw(col2, style="W", zero.policy=TRUE), "CsparseMatrix")
g1W <- graph.adjacency(W, mode="directed", weighted="W")
c1W <- clusters(g1W)
print(all.equal(c1W$membership, res$comp.id, check.attributes=FALSE))
}
if (run) {
print(all.equal(resi$comp.id, res$comp.id))
ow <- options("warn")$warn
options("warn"=2L)
# Matrix 1.4-2 vulnerability work-around
B1 <- try(get.adjacency(g1), silent=TRUE)
if (!inherits(B1, "try-error")) {
#B1 <- get.adjacency(g1)
print(all.equal(B, B1))
}
if (run) {
W <- as(nb2listw(col2, style="W", zero.policy=TRUE), "CsparseMatrix")
g1W <- graph_from_adjacency_matrix(W, mode="directed", weighted="W")
c1W <- components(g1W)
print(all.equal(c1W$membership, res$comp.id, check.attributes=FALSE))
options("warn"=ow)
}
}

Expand Down
2 changes: 1 addition & 1 deletion man/lm.RStests.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ lw <- nb2listw(COL.nb)
res <- lm.RStests(oldcrime.lm, listw=lw, test="all")
summary(res)
if (require("spatialreg", quietly=TRUE)) {
oldcrime.slx <- lmSLX(CRIME ~ HOVAL + INC, data = COL.OLD, listw=lw)
oldcrime.slx <- lm(CRIME ~ HOVAL + INC, data = COL.OLD, listw=lw)
summary(lm.RStests(oldcrime.slx, listw=lw, test=c("adjRSerr", "adjRSlag")))
}
}
Expand Down
1 change: 1 addition & 0 deletions man/lm.morantest.exact.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ must be one of greater (default), less or two.sided.}
\examples{
eire <- st_read(system.file("shapes/eire.gpkg", package="spData")[1])
row.names(eire) <- as.character(eire$names)
st_crs(eire) <- "+proj=utm +zone=30 +ellps=airy +units=km"
eire.nb <- poly2nb(eire)
e.lm <- lm(OWNCONS ~ ROADACC, data=eire)
lm.morantest(e.lm, nb2listw(eire.nb))
Expand Down
1 change: 1 addition & 0 deletions man/lm.morantest.sad.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Geographical Analysis, 34, pp. 187--206; Bivand RS, Wong DWS 2018 Comparing impl
\examples{
eire <- st_read(system.file("shapes/eire.gpkg", package="spData")[1])
row.names(eire) <- as.character(eire$names)
st_crs(eire) <- "+proj=utm +zone=30 +ellps=airy +units=km"
eire.nb <- poly2nb(eire)
e.lm <- lm(OWNCONS ~ ROADACC, data=eire)
lm.morantest(e.lm, nb2listw(eire.nb))
Expand Down
1 change: 1 addition & 0 deletions man/mstree.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ mstree(nbw, ini = NULL)
### loading data
bh <- st_read(system.file("etc/shapes/bhicv.gpkg",
package="spdep")[1], quiet=TRUE)
st_crs(bh) <- "OGC:CRS84"
### data padronized
dpad <- data.frame(scale(as.data.frame(bh)[,5:8]))

Expand Down
4 changes: 2 additions & 2 deletions man/nblag.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ run <- FALSE
if (require(igraph, quietly=TRUE) && require(spatialreg, quietly=TRUE)) run <- TRUE
if (run) {
W <- as(nb2listw(col.gal.nb), "CsparseMatrix")
G <- graph_from_adjacency_matrix(W, mode="directed", weight="W")
G <- graph.adjacency(W, mode="directed", weight="W")
D <- diameter(G)
nbs <- nblag(col.gal.nb, maxlag=D)
n <- length(col.gal.nb)
lmat <- lapply(nbs, nb2mat, style="B", zero.policy=TRUE)
mat <- matrix(0, n, n)
for (i in seq(along=lmat)) mat = mat + i*lmat[[i]]
G2 <- distances(G)
G2 <- shortest.paths(G)
print(all.equal(G2, mat, check.attributes=FALSE))
}
}
Expand Down
1 change: 1 addition & 0 deletions man/skater.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ skater(edges, data, ncuts, crit, vec.crit, method = c("euclidean",
### loading data
bh <- st_read(system.file("etc/shapes/bhicv.gpkg",
package="spdep")[1], quiet=TRUE)
st_crs(bh) <- "OGC:CRS84"
### data standardized
dpad <- data.frame(scale(as.data.frame(bh)[,5:8]))

Expand Down
4 changes: 1 addition & 3 deletions src/dfs_ncomp.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* Copyright 2001 by Nicholas Lewin-Koh.
* interrupt added RSB 2024 */
/* Copyright 2001 by Nicholas Lewin-Koh. */

#include "spdep.h"

Expand Down Expand Up @@ -34,7 +33,6 @@ SEXP g_components(SEXP nblst, SEXP cmpnm){
}

for(i=0; i < nvert; i++){
R_CheckUserInterrupt();
if(INTEGER(visited)[i]==WHITE){
INTEGER(visited)[i]=BLACK;
if(INTEGER(VECTOR_ELT(nblst,i))[0]==0){
Expand Down

0 comments on commit 779ef78

Please sign in to comment.