Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/afiore/gexf.rb
Ruby library for parsing, serializing, and manipulating GEXF graphs
https://github.com/afiore/gexf.rb
Last synced: 3 months ago
JSON representation
Ruby library for parsing, serializing, and manipulating GEXF graphs
- Host: GitHub
- URL: https://github.com/afiore/gexf.rb
- Owner: afiore
- Created: 2012-07-14T17:48:31.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2014-01-31T11:57:06.000Z (almost 11 years ago)
- Last Synced: 2024-07-28T10:03:16.617Z (4 months ago)
- Language: Ruby
- Homepage:
- Size: 166 KB
- Stars: 21
- Watchers: 1
- Forks: 13
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Gexf.rb
A Ruby library for generating, parsing, and serializing graphs expressed in the [GEXF](http://gexf.net) format.
Currently, this project implements only a subset of the GEXF specification: the definition of a basic graph topology,
and the association of data attributes to nodes and edges. I will possibly implement the rest of the specification later on
(i.e. dynamics, hyrarchy, and Phylogeny), as I consolidate the code.## Notice
This gem is not under active development anymore. However, you are wellcome to
contribute pull requests, I will do my best to find the time for reviewing and possibly merging them.## Installation
gem install gexf
## Usage
The following snippet initializes a GEXF graph, and defines three node attributes:
````ruby
require 'rubygems'
require 'gexf'graph = GEXF::Graph.new
graph.define_node_attribute(:url)
graph.define_node_attribute(:indegree, :type => GEXF::Attribute::INTEGER)
graph.define_node_attribute(:frog, :type => GEXF::Attribute::BOOLEAN,
:default => true)
````Attribute values can be associated to nodes or edges by using the same syntax used
to get/set Ruby Hash keys (symbols are automatically converted into strings).````ruby
gephi = graph.create_node(:label => 'Gephi')
gephi[:url] = 'http://gephi.org'
gephi[:indegree] = 1webatlas = graph.create_node(:label => 'WebAtlas')
webatlas[:url] = 'http://webatlas.fr'
webatlas[:indegree] = 2rtgi = graph.create_node(:label => 'RTGI')
rtgi[:url] = 'http://rtgi.fr'
rtgi[:indegree] = 1blab = graph.create_node(:label => 'BarabasiLab')
blab[:url] = "http://barabasilab.com"
blab[:indegree] = 1
blab[:frog] = false
````Once associated to a graph, nodes and edges behave as collections,
implementing and exposing most of the methods in Ruby's _Enumerable_ module:````ruby
graph.nodes.select { |node| !node[:frog] }.map(&:label)
=> 'BarabasiLab'
````Edges can be created by calling the `graph.create_edges`, or more coincisely, by calling
connect on the source node.````ruby
gephi.connect_to(webatlas)
gephi.connect_to(rtgi)
webatlas.connect_to(gephi)
rtgi.connect_to(webatlas)
gephi.connect_to(blab)
````As it is the case for `graph.nodes`, also edges are enumerable:
````ruby
graph.edges.count
=> 5
````The complete set of edges can be accessed from the main graph object, or fetched
on a single node basis:````ruby
webatlas.incoming_connections.map { |edge| edge.source.label }
=> ["Gephi", "RTGI"]
````### Parsing a GEXF document
Gexf.rb provides a basic SAX parser which allows to import GEXF documents into a
graph objects suitable to be queried and manipulated. To parse a GEXF file into a graph, just
call the GEXF helper method (which is a shortcat to `GEXF::Document.parse(file)`)````ruby
require 'gexf'
require 'open-uri'file = File.open('http://gexf.net/data/data.gexf', 'r')
graph = GEXF(file)
file.closegraph.nodes.count
=> 4
````### Exporting a graph into an XML document
A graph object can be easily serialized to XML by just calling:
````ruby
graph.to_xml
=> "\n"
````Alternatively, one can obtain the same output by instantiating GEXF::XmlSerializer and calling the `serialize!` method.
````ruby
serializer = GEXF::XmlSerializer.new(graph)
serializer.serialize!
````## Unit tests
Gexf.rb comes with a fairly decent RSpec test suite. The suite can
be run from the project directory by issuing the following command:bundle exec spec -f d spec
### Contributors
- Andrea Fiore
- Erik Doernenburg
- Thiago Bueno