This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dag.py
91 lines (72 loc) · 2.81 KB
/
dag.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3.4
#
# @file dag.py
# @brief A directed acyclic graph representation of a concept scheme
# @author Matthew Graham
#
# <!---------------------------------------------------------------------------
# Copyright (C) 2017 by the California Institute of Technology.
# This software is part of CASICS, the Comprehensive and Automated Software
# Inventory Creation System. For more information, visit http://casics.org.
# ------------------------------------------------------------------------- -->
import numpy as np
import json
from igraph import Graph
import sys
import os
# Main body.
# .............................................................................
class DAG(object):
'''Implements a concept scheme as a directed acyclic graph'''
def __init__(self, file):
if file.endswith(".json"):
self.dag = load_from_json(file)
elif file.endswith(".graphml"):
self.dag = load_from_graphml(file)
def non_leaf_nodes(self):
''' Returns a list of the non-leaf nodes in the DAG '''
nodes = self.dag.vs.select(_degree_gt = 1)['name']
return nodes
def get_parents(self, node):
''' Return the parents of the specified node in the DAG '''
parents = []
for p in self.dag.vs.find(node).predecessors():
parents.append(p['name'])
return parents
def get_children(self, node):
''' Return the children of the specified node in the DAG '''
children = []
for s in self.dag.vs.find(node).successors():
children.append(s['name'])
return children
def remove(self, node):
''' Remove the specified nodes and its descendants from the DAG '''
self.dag.vs.remove(node)
# Helpers
# .............................................................................
def load_from_json(filename):
'''Load a JSON representation of a concept scheme'''
# Load JSON representation to dict
dic = json.load(open(filename))
root = dic.keys()[0]
# Parse dict
g = Graph(directed = True)
g.add_vertex(root) # Root node
parse_dict(dic[root], root, g)
return g
def parse_dict(children, parent, g):
'''Parse a dictionary and recursive call if not leaf node'''
for child in children.keys():
print "%s: %s -> %s" % (child, parent, child)
g.add_vertex(child)
g.add_edge(parent, child)
if isinstance(children[child], dict):
parse_dict(children[child], child, g)
elif isinstance(children[child], list):
for item in children[child]:
g.add_vertex(item)
g.add_edge(child, item)
def load_from_graphml(filename):
'''Load a GraphML representation of a concept scheme'''
g = Graph.Read_GraphML(filename)
return g