Title: | Graph Edge Computations for Spatial Point Patterns |
---|---|
Description: | Graphs (or networks) and graph component calculations for spatial locations in 1D, 2D, 3D etc. |
Authors: | Tuomas Rajala |
Maintainer: | Tuomas Rajala <[email protected]> |
License: | GPL (>= 2) |
Version: | 3.5 |
Built: | 2025-02-10 06:15:06 UTC |
Source: | https://github.com/antiphon/spatgraphs |
Class creator
as.sg(edges = list(), type = "?", pars = NULL, note = NULL)
as.sg(edges = list(), type = "?", pars = NULL, note = NULL)
edges |
list of neighbourhoods |
type |
type |
pars |
parameters |
note |
notes |
Creator for sgadj-class
as.sgadj(edges = NULL, type = "?", pars = NULL, other = "")
as.sgadj(edges = NULL, type = "?", pars = NULL, other = "")
edges |
edge list-of-lists |
type |
of the graph |
pars |
parameters for the graph |
other |
other comments |
Creator for sgc
as.sgc(clusters, type = "?", pars = NULL, note = NULL)
as.sgc(clusters, type = "?", pars = NULL, note = NULL)
clusters |
list of clusters as point indices |
type |
type |
pars |
parameters |
note |
notes |
cut edges
## S3 method for class 'sg' cut(x, data, R, ...)
## S3 method for class 'sg' cut(x, data, R, ...)
x |
sg graph object |
data |
point pattern used for computing g |
R |
cutting length |
... |
ignored Removes edges with length > R. |
Edge lengths
edgeLengths(g, x, ...)
edgeLengths(g, x, ...)
g |
sg-object |
x |
point pattern |
... |
ignored |
Rudimentary plotting.
## S3 method for class 'sg' plot( x, data, which = NULL, add = FALSE, addPoints = FALSE, points.pch = 1, points.col = 1, points.cex = 1, max.edges = 10000, ... )
## S3 method for class 'sg' plot( x, data, which = NULL, add = FALSE, addPoints = FALSE, points.pch = 1, points.col = 1, points.cex = 1, max.edges = 10000, ... )
x |
an 'sg' graph object |
data |
The point pattern object, same as for computing the 'g' |
which |
Indices of which out-edges to plot. Default: all |
add |
Add to existing plot? (default: FALSE) |
addPoints |
Add points? Will be added if add=FALSE |
points.pch |
point styling |
points.col |
point styling |
points.cex |
point styling |
max.edges |
limit of edges to try to plot, gets very slow at high count. default 1e4 |
... |
passed to 'lines' function |
plot sgadj
## S3 method for class 'sgadj' plot(x, ...)
## S3 method for class 'sgadj' plot(x, ...)
x |
sgadj object |
... |
passed to plot.sg converts to sg and plots that. |
plot clusters
## S3 method for class 'sgc' plot(x, data, atleast = 2, add = FALSE, col, ...)
## S3 method for class 'sgc' plot(x, data, atleast = 2, add = FALSE, col, ...)
x |
spatcluster-cluster object |
data |
point pattern object used for computing the graph |
atleast |
plot only cluster with 'atleast' points in them |
add |
add or plot new |
col |
colors for clusters, chosen randomly if missing. |
... |
passed to points |
plot spectral clustering results
## S3 method for class 'sgspectral' plot(x, data, ...)
## S3 method for class 'sgspectral' plot(x, data, ...)
x |
spectral_sg result |
data |
point pattern |
... |
ignored |
Plot 3d graph
plot3_sg(x, data, which, ...)
plot3_sg(x, data, which, ...)
x |
sg object |
data |
coordinates |
which |
points of which out-edges will be plotted |
... |
passed to segments3d |
Print sg class.
## S3 method for class 'sg' print(x, ...)
## S3 method for class 'sg' print(x, ...)
x |
sg object |
... |
ignored |
Print basic info.
print method for sgadj
## S3 method for class 'sgadj' print(x, ...)
## S3 method for class 'sgadj' print(x, ...)
x |
sgadj object |
... |
ignored |
sgc print method
## S3 method for class 'sgc' print(x, ...)
## S3 method for class 'sgc' print(x, ...)
x |
sgc object |
... |
ignored |
Prune a graph
prune_sg(g, level = 1, verbose = FALSE)
prune_sg(g, level = 1, verbose = FALSE)
g |
sg object |
level |
pruning level |
verbose |
verbosity |
Remove edges from a graph by their path connectivity.
x <- matrix(runif(50*2), ncol=2) g <- spatgraph(x, "MST") gp <- prune_sg(g, level = 2) plot(g, x, lty=2) plot(gp, x, add=TRUE, col=2)
x <- matrix(runif(50*2), ncol=2) g <- spatgraph(x, "MST") gp <- prune_sg(g, level = 2) plot(g, x, lty=2) plot(gp, x, add=TRUE, col=2)
Remove the existence of particular nodes from the graph.
remove_nodes(g, i, fuse = FALSE, verb = FALSE)
remove_nodes(g, i, fuse = FALSE, verb = FALSE)
g |
sg object |
i |
indices of nodes for which to remove the edges |
fuse |
Should the neighours of removed nodes be connected? |
verb |
verbose? |
Basically, just clear the neighbourhood of selected indices. If fuse=TRUE, connect neighbours together (excluding i's). Should work over several remove nodes along a path.
Note: g should be symmetric. use sg2sym to force symmetry, it is not checked.
Warning: In development.
x <- matrix(runif(200), ncol=2) g <- spatgraph(x, "RST", c(1,0)) g <- sg2sym(g) i <- sample(100, 50) k <- setdiff(1:100, i) gs <- remove_nodes(g, i, fuse=TRUE) plot(g,x, add=FALSE) points(x[k,], pch=19, col=4) plot(gs, x, add=TRUE, lty=2, col=3)
x <- matrix(runif(200), ncol=2) g <- spatgraph(x, "RST", c(1,0)) g <- sg2sym(g) i <- sample(100, 50) k <- setdiff(1:100, i) gs <- remove_nodes(g, i, fuse=TRUE) plot(g,x, add=FALSE) points(x[k,], pch=19, col=4) plot(gs, x, add=TRUE, lty=2, col=3)
Extract the coordinate locations from the input object.
sg_parse_coordinates(x, verbose = FALSE)
sg_parse_coordinates(x, verbose = FALSE)
x |
Input object containing the coordinates in some format. |
verbose |
Print out info of the coordinates. |
Mainly for internal use.
sg_verify_parameters(coord, type, par, maxR, doDists, preGraph)
sg_verify_parameters(coord, type, par, maxR, doDists, preGraph)
coord |
Coordinates of the locations |
type |
Type of graph |
par |
Parameter(s) for the graph |
maxR |
Maximum range for edges, helps in large patterns. |
doDists |
Precompute distances? Speeds up some graphs, takes up memory. |
preGraph |
Precomputed graph, taken as a super-graph |
sg to dxf format
sg2dxf(g, x, file)
sg2dxf(g, x, file)
g |
sg object |
x |
pattern object used for computing g |
file |
filename for output |
Obsolete. Use igraph::graph_from_adj_list on the graph edges element.
sg2igraph(g, x, ...)
sg2igraph(g, x, ...)
g |
sg object |
x |
possibly the location pattern |
... |
not used Not implemented. You can use the 'graph_from_adj_list'-function in 'igraph'-package on the edges-element of the graph. |
## Not run: ix <- igraph::graph_from_adj_list(x$edges) ## End(Not run)
## Not run: ix <- igraph::graph_from_adj_list(x$edges) ## End(Not run)
Make a sparse adjacency matrix from sg-object
sg2sparse(x)
sg2sparse(x)
x |
sg-object |
Symmetrisation of sg adjacency matrix wrapper for 1way and 2way symmetrisation
sg2sym(x, way = 1)
sg2sym(x, way = 1)
x |
sg object |
way |
1: OR rule, 2: AND rule for keeping edges. |
weighted sg to weighted adjacency matrix
sg2wadj(x)
sg2wadj(x)
x |
weighted sg object |
Djikstra's algorithm
shortestPath(i, j, g, x = NULL, dbg = FALSE, checksym = TRUE)
shortestPath(i, j, g, x = NULL, dbg = FALSE, checksym = TRUE)
i |
index from |
j |
index to |
g |
sg object |
x |
optional point pattern from which g was computed |
dbg |
verbose |
checksym |
check (and force) symmetry |
If x is given, we use the point-to-point distances as edge weights. Otherwise, each edge has weight 1.
Djikstra's algorithm
shortestPath_legacy(i, j, g, x = NULL, dbg = FALSE)
shortestPath_legacy(i, j, g, x = NULL, dbg = FALSE)
i |
index from |
j |
index to |
g |
sg object |
x |
optional point pattern from which g was computed |
dbg |
verbose |
If x is given, we use the point-to-point distances as edge weights. Otherwise, each edge has weight 1.
Djikstra's algorithm
shortestPath2(i, j, g, x = NULL, dbg = FALSE, checksym = TRUE)
shortestPath2(i, j, g, x = NULL, dbg = FALSE, checksym = TRUE)
i |
index from |
j |
index to. Can be a set. |
g |
sg object |
x |
optional point pattern from which g was computed |
dbg |
verbose |
checksym |
check (and force) symmetry |
If x is given, we use the point-to-point distances as edge weights. Otherwise, each edge has weight 1.
Make an sg-object from adjacency matrix
sparse2sg(x)
sparse2sg(x)
x |
square matrix. non-0 elements are taken as edge presence. |
Compute the connected components of a graph
spatcluster(x, verbose = FALSE, sym = FALSE)
spatcluster(x, verbose = FALSE, sym = FALSE)
x |
sg-object |
verbose |
print info |
sym |
force symmetry of edges |
Given a spatial point pattern, we compute the edges of a graph (network) for a specified type of edge relationship.
spatgraph( x, type = "geometric", par = NULL, verbose = FALSE, maxR = 0, doDists = FALSE, preGraph = NULL )
spatgraph( x, type = "geometric", par = NULL, verbose = FALSE, maxR = 0, doDists = FALSE, preGraph = NULL )
x |
Input point pattern object |
type |
Type of the graph |
par |
Parameter(s) for the graph |
verbose |
Print details |
maxR |
Maximum range for edges, helps in large patterns. |
doDists |
Precompute distances? Speeds up some graphs, takes up memory. |
preGraph |
Precomputed graph, taken as a super-graph |
Several edge definitions are supported:
par=numeric>0. Geometric graph, par = connection radius.
par=integer>0. k-nearest neighbours graph, par = k.
Connect two points if ||x-y||<m(x). par=vector giving the m(x_i)'s
Connect two points if ||x-y||<m(x)+m(y). par = vector giving the m(x_i)'s
Gabriel graph. Additional parameter for allowing par=k
instead of 0 points in the circle.
Minimal spanning tree.
Spheres of Influence.
Radial spanning tree, par=origin of radiation, coordinate vector
Relative neighbourhood graph
Class-Cover-Catch, par=factor vector of point types. The factor vector is converted to integers according to R's internal representation of factors, and the points with type 1 will be the target. Use relevel to change the target.
The parameter 'maxR' can be given to bring n^3 graphs closer to n^2. k-nearest neighbours will warn if maxR is too small (<k neighbours for some points), others, like RNG, don't so be careful.
Voronoi diagram aka Delaunay triangulation is not supported as other R-packages can do it, see. e.g. package 'deldir'.
# basic example x <- matrix(runif(50*2), ncol=2) g <- spatgraph(x, "knn", par=3) plot(g, x) # bigger example xb <- matrix(runif(5000*2), ncol=2) gb <- spatgraph(xb, "RNG", maxR=0.1)
# basic example x <- matrix(runif(50*2), ncol=2) g <- spatgraph(x, "knn", par=3) plot(g, x) # bigger example xb <- matrix(runif(5000*2), ncol=2) gb <- spatgraph(xb, "RNG", maxR=0.1)
spectral clustering
spectral_sg(g, m = 2, K = 3)
spectral_sg(g, m = 2, K = 3)
g |
sg object. Should be weighted (with weight_sg-function) |
m |
levels to consider |
K |
number of assumed clusters |
sg summary
## S3 method for class 'sg' summary(object, ...)
## S3 method for class 'sg' summary(object, ...)
object |
sg object |
... |
ignored |
sgc summary
## S3 method for class 'sgc' summary(object, ...)
## S3 method for class 'sgc' summary(object, ...)
object |
sgc object |
... |
ignored |
This will transpose the adjacency matrix underlying the graph. Will transform to and from sgadj-object (see 'sg2adj')
## S3 method for class 'sg' t(x)
## S3 method for class 'sg' t(x)
x |
sg-object. |
This will transpose the adjacency matrix underlying the graph.
## S3 method for class 'sgadj' t(x)
## S3 method for class 'sgadj' t(x)
x |
sgadj object |
For each edge e(i,j) between points i,j, set the weight f(||x_i-x_j||)
weight_sg(g, x, f = function(x) exp(-x^2/scale), scale = 1, ...)
weight_sg(g, x, f = function(x) exp(-x^2/scale), scale = 1, ...)
g |
sg object |
x |
point pattern used in g |
f |
function for the weight |
scale |
additional scale parameter for the default f |
... |
ignored |
Default f(x) = exp(-x^2/scale)