Skip to content

Commit

Permalink
Fixing viz attributes from igraph to gexf and vice versa
Browse files Browse the repository at this point in the history
  • Loading branch information
gvegayon committed Nov 27, 2017
1 parent d0ac072 commit 39d2440
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 79 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ demo/gexfplot\.R
^dependencies$
^makefile$
^LICENSE\.md$
^tests/rundemos\.R$
4 changes: 2 additions & 2 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
2017-11-27 George G. Vega Yon <g.vegayon@gmail.com>
* R/bugs.r: Function to map colors of a single node.

* R/igraph.r, R/read.gexf.r: Mapping positions, size and colors. Still a
bit buggy.
* R/igraph.r, R/read.gexf.r: Mapping positions, size and colors, ~~Still a
bit buggy.~~ and positions.


2017-11-13 George G. Vega Yon <g.vegayon@gmail.com>
Expand Down
File renamed without changes.
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Changes in rgexf version 0.16.0 (2017-11-06)
# Changes in rgexf version 0.16.0 (2017-11-27)

## New features and changes

Expand All @@ -20,6 +20,10 @@
* `gexf` has a new argument, `rescale.node.size`. When set to `TRUE`, the
`size` vector in `nodesVizAtt` is rescaled such that when calling the plot
method the largest node spans roughly 5% of the plot.

* `read.gexf` now parses viz attributes (color, position and size).

* `igraph.to.gexf` and vice versa now pass viz attributes and layout.

## New functions

Expand Down
76 changes: 28 additions & 48 deletions R/igraph.r
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
#' igraph1 <- gexf.to.igraph(gexf1)
#' gexf2 <- igraph.to.gexf(igraph1)
#'
#' # Now, let's do it with a layout!
#' # Now, let's do it with a layout! (although we can just use
#' # the one that comes with lesmiserables :))
#' pos <- igraph::layout_nicely(igraph1)
#' plot(
#' igraph.to.gexf(igraph1, nodesVizAtt = list(position=cbind(pos, 0))),
Expand All @@ -42,56 +43,32 @@ igraph.to.gexf <- function(igraph.obj, ...) {

# Retrive elements from igraph object
gdata <- igraph::get.data.frame(g, what="both")
tmpedges <- gdata$edges
tmpnodes <- gdata$vertices
edges <- gdata$edges
nodes <- cbind(id = 1L:igraph::vcount(g), gdata$vertices)

# If nodes have no name
if (!("name" %in% colnames(tmpnodes)))
tmpnodes <- data.frame(tmpnodes, name=1:nrow(tmpnodes))

# Nodes and edges list
# these steps are dangerous as nodes that are not connected are dropped, creating a mismatched number of rows
# nodes <- edge.list(tmpedges[,c(1,2)])
# edges <- nodes$edges
# nodes <- nodes$nodes

nodes <- tmpnodes
# change name to label
names(nodes) <- 'label'
# add a column to nodes to hold the IDs
nodes$id <- as.integer(as.factor(nodes$label))
# put them in the right order
nodes <- nodes[, c('id', 'label')]

## replace from and to in edges with their IDs
# first create a named vector of ids
nodesID <- nodes$id
names(nodesID) <- gdata$vertices$name

edges <- tmpedges
# now in the edges create the id columns
edges$source <- nodesID[edges$from]
edges$target <- nodesID[edges$to]
# keep just source and target
edges <- edges[, c('source', 'target')]
namecol <- which(colnames(nodes) == "name")
if (!length(namecol))
nodes <- data.frame(nodes, label=nodes$id)
else
nodes$label <- nodes$name

# Building nodes
if (length(tmpnodes)) {
nodes <-merge(tmpnodes,nodes, by.x="name", by.y="label")
# nodes <- nodes[,!(colnames(nodes) %in% c("label"))]
}

edges$source <- nodes$id[match(edges$from, nodes$label)]
edges$target <- nodes$id[match(edges$to, nodes$label)]
edges <- edges[, c('source', 'target')]

# Nodes Attributes
x <- igraph::list.vertex.attributes(g)
x <- x[!(x %in% c("label","color","size"))]
x <- x[!(x %in% c("label","color","size", "name"))]
if (length(x))
dots$nodesAtt <- subset(nodes, select=x)

# Edges Attributes
x <- igraph::list.edge.attributes(g)
x <- x[!(x %in% c("weight","color","edgesLabel","width"))]
if (length(x))
dots$edgesAtt <- subset(tmpedges, select=x)
dots$edgesAtt <- subset(edges, select=x)

# Edges Weights
if (!length(dots$edgesWeight) && length(igraph::E(g)$weight))
Expand All @@ -101,14 +78,14 @@ igraph.to.gexf <- function(igraph.obj, ...) {

# Nodes Viz att
if (!length(dots$nodesVizAtt$color)) {
if (length(tmpnodes$color)) {
dots$nodesVizAtt$color <- tmpnodes$color
if (length(nodes$color)) {
dots$nodesVizAtt$color <- nodes$color
} else
dots$nodesVizAtt$color <- NULL
}

if (!length(dots$nodesVizAtt$size) && length(tmpnodes$size))
dots$nodesVizAtt$size <- tmpnodes$size
if (!length(dots$nodesVizAtt$size) && length(nodes$size))
dots$nodesVizAtt$size <- nodes$size

positions <- igraph::graph_attr(g, "layout")

Expand All @@ -119,8 +96,8 @@ igraph.to.gexf <- function(igraph.obj, ...) {

# Edges Viz att
if (!length(dots$edgesVizAtt$color)) {
if (length(tmpedges$color)) {
dots$edgesVizAtt$color <- tmpedges$color
if (length(edges$color)) {
dots$edgesVizAtt$color <- edges$color
} else
dots$edgexVizAtt$color <- NULL
}
Expand All @@ -137,8 +114,8 @@ igraph.to.gexf <- function(igraph.obj, ...) {
# Building graph
do.call(gexf, c(
list(
nodes = nodes[,c("id","name")],
edges = edges
nodes = nodes[, c("id","name")],
edges = edges[, c("source", "target")]
), dots)
)

Expand Down Expand Up @@ -171,7 +148,10 @@ gexf.to.igraph <- function(gexf.obj) {
igraph::V(g2)$size <- x$value

if (length(x <- g$nodesVizAtt$position))
g2 <- igraph::set_graph_attr(g2, "layout", unname(x))
g2 <- igraph::set_graph_attr(
g2, "layout",
unname(as.matrix(x))
)

# Nodes atts
if (length(x <- g$nodes[, !(colnames(g$nodes) %in% c("id", "label"))]))
Expand Down
6 changes: 4 additions & 2 deletions R/read.gexf.r
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,13 @@ read.gexf <- function(x) {

class(graph) <- "gexf"

order <- order(as.integer(graph$nodes$id))

build.and.validate.gexf(
nodes = graph$nodes,
nodes = graph$nodes[order, , drop=FALSE],
edges = graph$edges,
atts.definitions = graph$atts.definitions,
nodesVizAtt = nodesVizAtt,
nodesVizAtt = lapply(nodesVizAtt, "[", i=order, j=, drop=FALSE),
edgesVizAtt = edgesVizAtt,
graph = graph$graph
)
Expand Down
2 changes: 1 addition & 1 deletion demo/gexfigraph.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
gexf1 <- read.gexf(system.file("gexf-graphs/lesmiserables.gexf", package="rgexf"))
igraph1 <- gexf.to.igraph(gexf1)
gexf2 <- igraph.to.gexf(igraph1)
gexf2 <- igraph.to.gexf(igraph1)
55 changes: 33 additions & 22 deletions inst/NEWS
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@


CHANGES IN RGEXF VERSION 0.16.0 (2017-11-06)
CHANGES IN RGEXF VERSION 0.16.0 (2017-11-27)


New features and changes

- Modernization of the project (roxygen, new CRAN standards, etc.)

- Updated emails.

- Remove broken links.

- Function write.gexf has a new argument for specifying the GEXF
version for now it only changes the header.

- plot.gexf method now uses gexf-js instead of sigma.js.

- Now igraph.to.gexf passes arguments to gexf. Before it was only
passing position. This way users have more flexibility specifying
attributes.
passing position. This way users have more flexibility
specifying attributes.

- gexf's nodesVizAtt now has defaults for color, size and position.
This is a requirement of gexf-js.

- gexf's nodesVizAtt color and size now support passing a default for
all the nodes. Also, color can be specified as a character scalar
(name of the) color, or as an integer (number in colors()).

- gexf has a new argument, rescale.node.size. When set to TRUE, the
size vector in nodesVizAtt is rescaled such that when calling the
plot method the largest node spans roughly 5% of the plot.

- read.gexf now parses viz attributes (color, position and size).

- igraph.to.gexf and vice versa now pass viz attributes and layout.


New functions

Expand All @@ -36,6 +38,15 @@ New functions
vers 0.17.0


Misc changes

- Modernization of the project (roxygen, new CRAN standards, etc.)

- Updated emails.

- Remove broken links.



CHANGES IN RGEXF VERSION 0.15.3 (2015-03-24)

Expand Down Expand Up @@ -70,9 +81,9 @@ Bug fixes

Development

- Option -keepFactors- default is now in -FALSE- (used to be in
-TRUE-). When set to -TRUE- and there are factors, a warning message
will appear (reported in issue 18, thanks Tim Smith!).
- Option -keepFactors- default is now in -FALSE- (used to be
in -TRUE-). When set to -TRUE- and there are factors, a warning
message will appear (reported in issue 18, thanks Tim Smith!).



Expand All @@ -81,8 +92,8 @@ CHANGES IN RGEXF VERSION 0.13.11 (2013-11-27)

Bug fixes

- Included class checks in -gexf- class functions (thanks to Samuel
Finegold).
- Included class checks in -gexf- class functions (thanks to
Samuel Finegold).

- write.gexf does not fails when dynamics different from double are
passed (thank you Samuel Finegold!).
Expand Down Expand Up @@ -120,8 +131,8 @@ CHANGES IN RGEXF VERSION 0.13.05 (2013-05-09)

Bug fixes

- sprintf error when using other formats rather than double (issue
10).
- sprintf error when using other formats rather than double
(issue 10).

- in .addNodesEdges, add support to case “!attributes &&
vizattributes” (issue 9).
Expand Down Expand Up @@ -163,8 +174,8 @@ Bug fixes
Development

- .addNodesEdges rewritten now works faster in most of CPUs (some of
them with very high speedups) (Thanks to Duncan Temple Lang, RXML
author)
them with very high speedups) (Thanks to Duncan Temple Lang,
RXML author)

- Several code routines have been extracted from “bigger functions”
and written as functions themselves.
Expand All @@ -180,8 +191,8 @@ New features and changes
rm.gexf.node and rm.gexf.edge allow to build and manipulate gexf
objects from scratch.

- New function read.gexf allows to import gexf files as gexf class
objects.
- New function read.gexf allows to import gexf files as gexf
class objects.

- gexf function now it is called write.gexf.

Expand All @@ -192,8 +203,8 @@ New features and changes

- Real-life datasets have been includded.

- New function edge.list builds a dataframe of nodes from an edge
list.
- New function edge.list builds a dataframe of nodes from an
edge list.

- New methods for gexf objects: print.gexf and summary.gexf.

Expand Down
3 changes: 2 additions & 1 deletion man/igraph.to.gexf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/testthat/test-examples.r
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# context("Examples")
context("Examples")

# test_examples()
test_examples()

0 comments on commit 39d2440

Please sign in to comment.