Browse Source

Fix bugs for enum and add test for median preimage generator.

v0.2.x
jajupmochi 5 years ago
parent
commit
9a365fac98
4 changed files with 84 additions and 11 deletions
  1. +6
    -5
      gklearn/ged/env/common_types.py
  2. +1
    -1
      gklearn/ged/median/median_graph_estimator.py
  3. +71
    -0
      gklearn/tests/test_median_preimage_generator.py
  4. +6
    -5
      gklearn/utils/utils.py

+ 6
- 5
gklearn/ged/env/common_types.py View File

@@ -6,12 +6,13 @@ Created on Thu Mar 19 18:17:38 2020
@author: ljia @author: ljia
""" """


from enum import Enum, auto
from enum import Enum, unique


@unique
class AlgorithmState(Enum): class AlgorithmState(Enum):
"""can be used to specify the state of an algorithm. """can be used to specify the state of an algorithm.
""" """
CALLED = auto # The algorithm has been called.
INITIALIZED = auto # The algorithm has been initialized.
CONVERGED = auto # The algorithm has converged.
TERMINATED = auto # The algorithm has terminated.
CALLED = 1 # The algorithm has been called.
INITIALIZED = 2 # The algorithm has been initialized.
CONVERGED = 3 # The algorithm has converged.
TERMINATED = 4 # The algorithm has terminated.

+ 1
- 1
gklearn/ged/median/median_graph_estimator.py View File

@@ -302,7 +302,7 @@ class MedianGraphEstimator(object): # @todo: differ dummy_node from undifined no
self.__median_id = gen_median_id self.__median_id = gen_median_id
self.__state = AlgorithmState.TERMINATED self.__state = AlgorithmState.TERMINATED
# Get ExchangeGraph representations of the input graphs.
# Get NetworkX graph representations of the input graphs.
graphs = {} graphs = {}
for graph_id in graph_ids: for graph_id in graph_ids:
# @todo: get_nx_graph() function may need to be modified according to the coming code. # @todo: get_nx_graph() function may need to be modified according to the coming code.


+ 71
- 0
gklearn/tests/test_median_preimage_generator.py View File

@@ -0,0 +1,71 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 14 15:39:29 2020

@author: ljia
"""
import multiprocessing
import functools
from gklearn.preimage.utils import generate_median_preimages_by_class

def test_median_preimage_generator():
"""MAO, Treelet, using CONSTANT, symbolic only.
"""
from gklearn.utils.kernels import polynomialkernel
# set parameters.
ds_name = 'MAO' #
mpg_options = {'fit_method': 'k-graphs',
'init_ecc': [4, 4, 2, 1, 1, 1], #
'ds_name': ds_name,
'parallel': True, # False
'time_limit_in_sec': 0,
'max_itrs': 3, #
'max_itrs_without_update': 3,
'epsilon_residual': 0.01,
'epsilon_ec': 0.1,
'verbose': 2}
pkernel = functools.partial(polynomialkernel, d=4, c=1e+7)
kernel_options = {'name': 'Treelet', #
'sub_kernel': pkernel,
'parallel': 'imap_unordered',
# 'parallel': None,
'n_jobs': multiprocessing.cpu_count(),
'normalize': True,
'verbose': 2}
ged_options = {'method': 'IPFP',
'initialization_method': 'RANDOM', # 'NODE'
'initial_solutions': 1, # 1
'edit_cost': 'CONSTANT', #
'attr_distance': 'euclidean',
'ratio_runs_from_initial_solutions': 1,
'threads': multiprocessing.cpu_count(),
'init_option': 'EAGER_WITHOUT_SHUFFLED_COPIES'}
mge_options = {'init_type': 'MEDOID',
'random_inits': 10,
'time_limit': 600,
'verbose': 2,
'refine': False}
save_results = True
dir_save = ds_name + '.' + kernel_options['name'] + '.symb.pytest/'
irrelevant_labels = {'node_attrs': ['x', 'y', 'z'], 'edge_labels': ['bond_stereo']} #
edge_required = False #
# print settings.
print('parameters:')
print('dataset name:', ds_name)
print('mpg_options:', mpg_options)
print('kernel_options:', kernel_options)
print('ged_options:', ged_options)
print('mge_options:', mge_options)
print('save_results:', save_results)
print('irrelevant_labels:', irrelevant_labels)
print()
# generate preimages.
for fit_method in ['k-graphs', 'expert', 'random']:
print('\n-------------------------------------')
print('fit method:', fit_method, '\n')
mpg_options['fit_method'] = fit_method
generate_median_preimages_by_class(ds_name, mpg_options, kernel_options, ged_options, mge_options, save_results=save_results, save_medians=True, plot_medians=True, load_gm='auto', dir_save=dir_save, irrelevant_labels=irrelevant_labels, edge_required=edge_required, cut_range=range(0, 4))

+ 6
- 5
gklearn/utils/utils.py View File

@@ -1,7 +1,7 @@
import networkx as nx import networkx as nx
import numpy as np import numpy as np
from copy import deepcopy from copy import deepcopy
from enum import Enum, auto
from enum import Enum, unique
#from itertools import product #from itertools import product


# from tqdm import tqdm # from tqdm import tqdm
@@ -467,8 +467,9 @@ def get_mlti_dim_edge_attrs(G, attr_names):
attributes.append(tuple(attrs[aname] for aname in attr_names)) attributes.append(tuple(attrs[aname] for aname in attr_names))
return attributes return attributes


@unique
class SpecialLabel(Enum): class SpecialLabel(Enum):
"""can be used to define special labels.
"""
DUMMY = auto # The dummy label.
"""can be used to define special labels.
"""
DUMMY = 1 # The dummy label.
# DUMMY = auto # enum.auto does not exist in Python 3.5.

Loading…
Cancel
Save