Commit 65893012 authored by Charles Thomas's avatar Charles Thomas

pushing template

parent 6418df09
# Idea files and dirs #
#######################
*.iml
**/out/
**/.idea/
*.class
# latex compilation files #
###########################
*.aux
*.log
*.dvi
*.synctex.gz
# OS generated files #
######################
.DS_Store
.DS_Store?
._*
# Python cache #
################
**/__pycache__/
*.pyc
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from .gspan import gSpan
from .graphdatabase import GraphDatabase
"""This package contains the gSpan implementation along with classes used to represent graphs and graph databases.
WARNING: The content of this package should be left unchanged."""
"""Definitions of Edge, Vertex and Graph."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import collections
import itertools
VACANT_EDGE_ID = -1
VACANT_VERTEX_ID = -1
VACANT_EDGE_LABEL = -1
VACANT_VERTEX_LABEL = -1
VACANT_GRAPH_ID = -1
AUTO_EDGE_ID = -1
class Edge(object):
"""Edge class."""
def __init__(self,
eid=VACANT_EDGE_ID,
frm=VACANT_VERTEX_ID,
to=VACANT_VERTEX_ID,
elb=VACANT_EDGE_LABEL):
"""Initialize Edge instance.
Args:
eid: edge id.
frm: source vertex id.
to: destination vertex id.
elb: edge label.
"""
self.eid = eid
self.frm = frm
self.to = to
self.elb = elb
class Vertex(object):
"""Vertex class."""
def __init__(self,
vid=VACANT_VERTEX_ID,
vlb=VACANT_VERTEX_LABEL):
"""Initialize Vertex instance.
Args:
vid: id of this vertex.
vlb: label of this vertex.
"""
self.vid = vid
self.vlb = vlb
self.edges = dict()
def add_edge(self, eid, frm, to, elb):
"""Add an outgoing edge."""
self.edges[to] = Edge(eid, frm, to, elb)
class Graph(object):
"""Graph class."""
def __init__(self,
gid=VACANT_GRAPH_ID,
is_undirected=True,
eid_auto_increment=True):
"""Initialize Graph instance.
Args:
gid: id of this graph.
is_undirected: whether this graph is directed or not.
eid_auto_increment: whether to increment edge ids automatically.
"""
self.gid = gid
self.is_undirected = is_undirected
self.vertices = dict()
self.set_of_elb = collections.defaultdict(set)
self.set_of_vlb = collections.defaultdict(set)
self.eid_auto_increment = eid_auto_increment
self.counter = itertools.count()
def get_num_vertices(self):
"""Return number of vertices in the graph."""
return len(self.vertices)
def add_vertex(self, vid, vlb):
"""Add a vertex to the graph."""
if vid in self.vertices:
return self
self.vertices[vid] = Vertex(vid, vlb)
self.set_of_vlb[vlb].add(vid)
return self
def add_edge(self, eid, frm, to, elb):
"""Add an edge to the graph."""
if (frm is self.vertices and
to in self.vertices and
to in self.vertices[frm].edges):
return self
if self.eid_auto_increment:
eid = next(self.counter)
self.vertices[frm].add_edge(eid, frm, to, elb)
self.set_of_elb[elb].add((frm, to))
if self.is_undirected:
self.vertices[to].add_edge(eid, to, frm, elb)
self.set_of_elb[elb].add((to, frm))
return self
def display(self):
"""Display the graph as text."""
display_str = ''
print('t # {}'.format(self.gid))
for vid in self.vertices:
print('v {} {}'.format(vid, self.vertices[vid].vlb))
display_str += 'v {} {} '.format(vid, self.vertices[vid].vlb)
for frm in self.vertices:
edges = self.vertices[frm].edges
for to in edges:
if self.is_undirected:
if frm < to:
print('e {} {} {}'.format(frm, to, edges[to].elb))
display_str += 'e {} {} {} '.format(
frm, to, edges[to].elb)
else:
print('e {} {} {}'.format(frm, to, edges[to].elb))
display_str += 'e {} {} {}'.format(frm, to, edges[to].elb)
return display_str
def plot(self):
"""Visualize the graph."""
try:
import networkx as nx
import matplotlib.pyplot as plt
except Exception as e:
print('Can not plot graph: {}'.format(e))
return
gnx = nx.Graph() if self.is_undirected else nx.DiGraph()
vlbs = {vid: v.vlb for vid, v in self.vertices.items()}
elbs = {}
for vid, v in self.vertices.items():
gnx.add_node(vid, label=v.vlb)
for vid, v in self.vertices.items():
for to, e in v.edges.items():
if (not self.is_undirected) or vid < to:
gnx.add_edge(vid, to, label=e.elb)
elbs[(vid, to)] = e.elb
fsize = (min(16, 1 * len(self.vertices)),
min(16, 1 * len(self.vertices)))
plt.figure(3, figsize=fsize)
pos = nx.spectral_layout(gnx)
nx.draw_networkx(gnx, pos, arrows=True, with_labels=True, labels=vlbs)
nx.draw_networkx_edge_labels(gnx, pos, edge_labels=elbs)
plt.show()
"""Implementation of a gSpan graph database with two classes."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import codecs
from .graph import AUTO_EDGE_ID
from .graph import Graph
class GraphDatabase(object):
def __init__(self):
self._graph_cnt = 0
self._graphs = dict()
def read_graphs(self, filename):
indexes = []
with codecs.open(filename, 'r', 'utf-8') as f:
lines = [line.strip() for line in f.readlines()]
tgraph = None
for i, line in enumerate(lines):
cols = line.split(' ')
if cols[0] == 't':
if tgraph is not None:
self._graphs[self._graph_cnt] = tgraph
indexes.append(self._graph_cnt)
self._graph_cnt += 1
tgraph = None
if cols[-1] == '-1':
break
tgraph = Graph(self._graph_cnt,
is_undirected=True,
eid_auto_increment=True)
elif cols[0] == 'v':
tgraph.add_vertex(cols[1], cols[2])
elif cols[0] == 'e':
tgraph.add_edge(AUTO_EDGE_ID, cols[1], cols[2], cols[3])
# adapt to input files that do not end with 't # -1'
if tgraph is not None:
self._graphs[self._graph_cnt] = tgraph
indexes.append(self._graph_cnt)
return indexes
This diff is collapsed.
"""The main program that runs gSpan. Two examples are provided"""
# -*- coding=utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import sys
import numpy
from sklearn import naive_bayes
from sklearn import metrics
from gspan_mining import gSpan
from gspan_mining import GraphDatabase
class PatternGraphs:
"""
This template class is used to define a task for the gSpan implementation.
You should not modify this class but extend it to define new tasks
"""
def __init__(self, database):
# A list of subsets of graph identifiers.
# Is used to specify different groups of graphs (classes and training/test sets).
# The gid-subsets parameter in the pruning and store function will contain for each subset, all the occurrences
# in which the examined pattern is present.
self.gid_subsets = []
self.database = database # A graphdatabase instance: contains the data for the problem.
def store(self, dfs_code, gid_subsets):
"""
Code to be executed to store the pattern, if desired.
The function will only be called for patterns that have not been pruned.
In correlated pattern mining, we may prune based on confidence, but then check further conditions before storing.
:param dfs_code: the dfs code of the pattern (as a string).
:param gid_subsets: the cover (set of graph ids in which the pattern is present) for each subset in self.gid_subsets
"""
print("Please implement the store function in a subclass for a specific mining task!")
def prune(self, gid_subsets):
"""
prune function: used by the gSpan algorithm to know if a pattern (and its children in the search tree)
should be pruned.
:param gid_subsets: A list of the cover of the pattern for each subset.
:return: true if the pattern should be pruned, false otherwise.
"""
print("Please implement the prune function in a subclass for a specific mining task!")
class FrequentPositiveGraphs(PatternGraphs):
"""
Finds the frequent (support >= minsup) subgraphs among the positive graphs.
This class provides a method to build a feature matrix for each subset.
"""
def __init__(self, minsup, database, subsets):
"""
Initialize the task.
:param minsup: the minimum positive support
:param database: the graph database
:param subsets: the subsets (train and/or test sets for positive and negative class) of graph ids.
"""
super().__init__(database)
self.patterns = [] # The patterns found in the end (as dfs codes represented by strings) with their cover (as a list of graph ids).
self.minsup = minsup
self.gid_subsets = subsets
# Stores any pattern found that has not been pruned
def store(self, dfs_code, gid_subsets):
self.patterns.append((dfs_code, gid_subsets))
# Prunes any pattern that is not frequent in the positive class
def prune(self, gid_subsets):
# first subset is the set of positive ids
return len(gid_subsets[0]) < self.minsup
# creates a column for a feature matrix
def create_fm_col(self, all_gids, subset_gids):
subset_gids = set(subset_gids)
bools = []
for i, val in enumerate(all_gids):
if val in subset_gids:
bools.append(1)
else:
bools.append(0)
return bools
# return a feature matrix for each subset of examples, in which the columns correspond to patterns
# and the rows to examples in the subset.
def get_feature_matrices(self):
matrices = [[] for _ in self.gid_subsets]
for pattern, gid_subsets in self.patterns:
for i, gid_subset in enumerate(gid_subsets):
matrices[i].append(self.create_fm_col(self.gid_subsets[i], gid_subset))
return [numpy.array(matrix).transpose() for matrix in matrices]
def example1():
"""
Runs gSpan with the specified positive and negative graphs, finds all frequent subgraphs in the positive class
with a minimum positive support of minsup and prints them.
"""
args = sys.argv
database_file_name_pos = args[1] # First parameter: path to positive class file
database_file_name_neg = args[2] # Second parameter: path to negative class file
minsup = int(args[3]) # Third parameter: minimum support
if not os.path.exists(database_file_name_pos):
print('{} does not exist.'.format(database_file_name_pos))
sys.exit()
if not os.path.exists(database_file_name_neg):
print('{} does not exist.'.format(database_file_name_neg))
sys.exit()
graph_database = GraphDatabase() # Graph database object
pos_ids = graph_database.read_graphs(database_file_name_pos) # Reading positive graphs, adding them to database and getting ids
neg_ids = graph_database.read_graphs(database_file_name_neg) # Reading negative graphs, adding them to database and getting ids
subsets = [pos_ids, neg_ids] # The ids for the positive and negative labelled graphs in the database
task = FrequentPositiveGraphs(minsup, graph_database, subsets) # Creating task
gSpan(task).run() # Running gSpan
# Printing frequent patterns along with their positive support:
for pattern, gid_subsets in task.patterns:
pos_support = len(gid_subsets[0]) # This will have to be replaced by the confidence and support on both classes
print('{} {}'.format(pattern, pos_support))
def example2():
"""
Runs gSpan with the specified positive and negative graphs; finds all frequent subgraphs in the training subset of
the positive class with a minimum support of minsup.
Uses the patterns found to train a naive bayesian classifier using Scikit-learn and evaluates its performances on
the test set.
Performs a k-fold cross-validation.
"""
args = sys.argv
database_file_name_pos = args[1] # First parameter: path to positive class file
database_file_name_neg = args[2] # Second parameter: path to negative class file
minsup = int(args[3]) # Third parameter: minimum support (note: this parameter will be k in case of top-k mining)
nfolds = int(args[4]) # Fourth parameter: number of folds to use in the k-fold cross-validation.
if not os.path.exists(database_file_name_pos):
print('{} does not exist.'.format(database_file_name_pos))
sys.exit()
if not os.path.exists(database_file_name_neg):
print('{} does not exist.'.format(database_file_name_neg))
sys.exit()
graph_database = GraphDatabase() # Graph database object
pos_ids = graph_database.read_graphs(database_file_name_pos) # Reading positive graphs, adding them to database and getting ids
neg_ids = graph_database.read_graphs(database_file_name_neg) # Reading negative graphs, adding them to database and getting ids
# If less than two folds: using the same set as training and test set (note this is not an accurate way to evaluate the performances!)
if nfolds < 2:
subsets = [
pos_ids, # Positive training set
pos_ids, # Positive test set
neg_ids, # Negative training set
neg_ids # Negative test set
]
# Printing fold number:
print('fold {}'.format(1))
train_and_evaluate(minsup, graph_database, subsets)
# Otherwise: performs k-fold cross-validation:
else:
pos_fold_size = len(pos_ids) // nfolds
neg_fold_size = len(neg_ids) // nfolds
for i in range(nfolds):
# Use fold as test set, the others as training set for each class;
# identify all the subsets to be maintained by the graph mining algorithm.
subsets = [
numpy.concatenate((pos_ids[:i * pos_fold_size], pos_ids[(i + 1) * pos_fold_size:])), # Positive training set
pos_ids[i * pos_fold_size:(i + 1) * pos_fold_size], # Positive test set
numpy.concatenate((neg_ids[:i * neg_fold_size], neg_ids[(i + 1) * neg_fold_size:])), # Negative training set
neg_ids[i * neg_fold_size:(i + 1) * neg_fold_size], # Negative test set
]
# Printing fold number:
print('fold {}'.format(i+1))
train_and_evaluate(minsup, graph_database, subsets)
def train_and_evaluate(minsup, database, subsets):
task = FrequentPositiveGraphs(minsup, database, subsets) # Creating task
gSpan(task).run() # Running gSpan
# Creating feature matrices for training and testing:
features = task.get_feature_matrices()
train_fm = numpy.concatenate((features[0], features[2])) # Training feature matrix
train_labels = numpy.concatenate((numpy.full(len(features[0]), 1, dtype=int), numpy.full(len(features[2]), -1, dtype=int))) # Training labels
test_fm = numpy.concatenate((features[1], features[3])) # Testing feature matrix
test_labels = numpy.concatenate((numpy.full(len(features[1]), 1, dtype=int), numpy.full(len(features[3]), -1, dtype=int))) # Testing labels
classifier = naive_bayes.GaussianNB() # Creating model object
classifier.fit(train_fm, train_labels) # Training model
predicted = classifier.predict(test_fm) # Using model to predict labels of testing data
accuracy = metrics.accuracy_score(test_labels, predicted) # Computing accuracy:
# Printing frequent patterns along with their positive support:
for pattern, gid_subsets in task.patterns:
pos_support = len(gid_subsets[0])
print('{} {}'.format(pattern, pos_support))
# printing classification results:
print(predicted)
print('accuracy: {}'.format(accuracy))
print() # Blank line to indicate end of fold.
if __name__ == '__main__':
example1()
# example2()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment