Class: Steffi::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/steffi/graph.rb,
lib/steffi/create.rb,
lib/steffi/metrics.rb,
lib/steffi/communities.rb

Defined Under Namespace

Classes: Struct

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Graph

Returns a new instance of Graph.

Yields:

  • (_self)

Yield Parameters:

  • _self (Steffi::Graph)

    the object that the method was called on



28
29
30
31
# File 'lib/steffi/graph.rb', line 28

def initialize 
  @ptr = FFI::MemoryPointer.new Struct
  yield self if block_given?
end

Instance Attribute Details

#ptrObject (readonly)

Returns the value of attribute ptr.



26
27
28
# File 'lib/steffi/graph.rb', line 26

def ptr
  @ptr
end

Class Method Details

.atlas(i) ⇒ Object



27
28
29
# File 'lib/steffi/create.rb', line 27

def atlas i
  from :atlas, i
end

.citations(n, p) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/steffi/create.rb', line 31

def citations n, p
  types = (0...n).to_a
  types = Vector.from_a(types).ptr
  attractivity = 1.0 / p
  prefs = [attractivity] * (n+1)
  prefs = Vector.from_a(prefs).ptr
  from :cited_type_game, n, types, prefs, 1, false
end

.erdos_renyi(n, k) ⇒ Object



40
41
42
# File 'lib/steffi/create.rb', line 40

def erdos_renyi n, k
  from :erdos_renyi_game, 1, n, k, false, false
end

.famous(name) ⇒ Object



44
45
46
# File 'lib/steffi/create.rb', line 44

def famous name
  from :famous, name.to_s
end

.forest_fire(n, k) ⇒ Object



48
49
50
# File 'lib/steffi/create.rb', line 48

def forest_fire n, k
  from :forest_fire_game, n, 0.01/k, 1, 2, false
end

.full(n) ⇒ Object



52
53
54
# File 'lib/steffi/create.rb', line 52

def full n
  from :full, n, false, false
end

.load(path) ⇒ Object



20
21
22
23
24
25
# File 'lib/steffi/create.rb', line 20

def load path
  stream = C.fopen path, 'r'
  g = from :read_graph_edgelist, stream, 0, false
  C.fclose stream
  g
end

.ring(n) ⇒ Object



56
57
58
# File 'lib/steffi/create.rb', line 56

def ring n
  from :ring, n, false, false, true
end

.star(n) ⇒ Object



60
61
62
# File 'lib/steffi/create.rb', line 60

def star n
  from :star, n, 2, 0
end

.tree(n, k) ⇒ Object



64
65
66
# File 'lib/steffi/create.rb', line 64

def tree n, k
  from :tree, n, k, 2
end

Instance Method Details

#communitiesObject



8
9
10
11
12
# File 'lib/steffi/communities.rb', line 8

def communities
  membership = Vector.new
  Igraph.community_multilevel ptr, nil, membership.ptr, nil, nil
  membership
end

#d3Object



39
40
41
42
43
44
# File 'lib/steffi/graph.rb', line 39

def d3
  {
    nodes: communities.each_with_index.map { |c,i| { name: i, group: c.to_i } },
    links: edges.map { |e| { source: e.from, target: e.to, weight: 1 } }
  }
end

#diameterObject



11
12
13
14
15
# File 'lib/steffi/metrics.rb', line 11

def diameter
  d = FFI::MemoryPointer.new :int
  Igraph.diameter ptr, d, nil, nil, nil, 0, 1
  d.get_int 0
end

#dump(path) ⇒ Object



33
34
35
36
37
# File 'lib/steffi/graph.rb', line 33

def dump path
  stream = C.fopen path, 'w'
  Igraph.write_graph_edgelist ptr, stream
  C.fclose stream
end

#ecountObject



17
18
19
# File 'lib/steffi/metrics.rb', line 17

def ecount
  Igraph.ecount ptr
end

#edge(i) ⇒ Object



29
30
31
32
33
34
# File 'lib/steffi/metrics.rb', line 29

def edge i
  from = FFI::MemoryPointer.new :int
  to = FFI::MemoryPointer.new :int
  Igraph.edge ptr, i, from, to
  Edge.new self, from.get_int(0), to.get_int(0)
end

#edgesObject



36
37
38
# File 'lib/steffi/metrics.rb', line 36

def edges
  0.upto(ecount - 1).map { |i| edge i }
end

#statsObject



25
26
27
# File 'lib/steffi/metrics.rb', line 25

def stats
  Hash[ %w{ diameter ecount vcount }.map { |n| [ n, send(n) ] } ]
end

#vcountObject



21
22
23
# File 'lib/steffi/metrics.rb', line 21

def vcount
  Igraph.vcount ptr
end