From 9a365fac98a05e88343a9f1d7bf86a5b102da1da Mon Sep 17 00:00:00 2001 From: jajupmochi Date: Thu, 30 Apr 2020 10:31:08 +0200 Subject: [PATCH] Fix bugs for enum and add test for median preimage generator. --- gklearn/ged/env/common_types.py | 11 ++-- gklearn/ged/median/median_graph_estimator.py | 2 +- gklearn/tests/test_median_preimage_generator.py | 71 +++++++++++++++++++++++++ gklearn/utils/utils.py | 11 ++-- 4 files changed, 84 insertions(+), 11 deletions(-) create mode 100644 gklearn/tests/test_median_preimage_generator.py diff --git a/gklearn/ged/env/common_types.py b/gklearn/ged/env/common_types.py index 2face25..d195b11 100644 --- a/gklearn/ged/env/common_types.py +++ b/gklearn/ged/env/common_types.py @@ -6,12 +6,13 @@ Created on Thu Mar 19 18:17:38 2020 @author: ljia """ -from enum import Enum, auto +from enum import Enum, unique +@unique class AlgorithmState(Enum): """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. \ No newline at end of file + 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. \ No newline at end of file diff --git a/gklearn/ged/median/median_graph_estimator.py b/gklearn/ged/median/median_graph_estimator.py index 114c312..df21601 100644 --- a/gklearn/ged/median/median_graph_estimator.py +++ b/gklearn/ged/median/median_graph_estimator.py @@ -302,7 +302,7 @@ class MedianGraphEstimator(object): # @todo: differ dummy_node from undifined no self.__median_id = gen_median_id self.__state = AlgorithmState.TERMINATED - # Get ExchangeGraph representations of the input graphs. + # Get NetworkX graph representations of the input graphs. graphs = {} for graph_id in graph_ids: # @todo: get_nx_graph() function may need to be modified according to the coming code. diff --git a/gklearn/tests/test_median_preimage_generator.py b/gklearn/tests/test_median_preimage_generator.py new file mode 100644 index 0000000..c81bb7c --- /dev/null +++ b/gklearn/tests/test_median_preimage_generator.py @@ -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)) \ No newline at end of file diff --git a/gklearn/utils/utils.py b/gklearn/utils/utils.py index 9223b7a..14dc7c1 100644 --- a/gklearn/utils/utils.py +++ b/gklearn/utils/utils.py @@ -1,7 +1,7 @@ import networkx as nx import numpy as np from copy import deepcopy -from enum import Enum, auto +from enum import Enum, unique #from itertools import product # 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)) return attributes - +@unique class SpecialLabel(Enum): - """can be used to define special labels. - """ - DUMMY = auto # The dummy label. \ No newline at end of file + """can be used to define special labels. + """ + DUMMY = 1 # The dummy label. + # DUMMY = auto # enum.auto does not exist in Python 3.5. \ No newline at end of file