Commit 98a61e48 authored by VIGNET Pierre's avatar VIGNET Pierre
Browse files

Command to export some infos about 1 model (json, graphml, centrality measures)

parent a9824c2f
......@@ -105,6 +105,27 @@ def model_comp(args):
)
def model_infos(args):
"""Model informations.
Get number of nodes, edges, centralities (degree, closeness, betweenness).
.. note:: You can export a graphml file for model.
"""
# Module import
import solution_repr
params = args_to_param(args)
params['output'] = params['output'] if params['output'][-1] == '/' \
else params['output'] + '/'
solution_repr.graph_infos(
params['model_file'],
params['output'],
params['graph'],
params['json'],
)
def merge_cams(args):
"""Merge solutions to a csv file.
......@@ -254,6 +275,19 @@ def main():
parser_model_comparison.set_defaults(func=model_comp)
# subparser: Model infos
# 1 model
parser_model_infos = subparsers.add_parser('model_infos',
help=model_infos.__doc__)
parser_model_infos.add_argument('model_file')
# Export graphs for the 2 models; default: false
parser_model_infos.add_argument('--graph', action='store_true')
parser_model_infos.add_argument('--json', action='store_true')
parser_model_infos.add_argument('--output', action=ReadableDir,
nargs='?', default='graphs/')
parser_model_infos.set_defaults(func=model_infos)
# get program args and launch associated command
args = parser.parse_args()
......
......@@ -895,11 +895,56 @@ def graph_isomorph_test(model_file_1, model_file_2, output_dir='graphs/',
# Export to json file
if make_json:
with open(output_dir + "comp_results.json", 'a') as fd:
fd.write(json.dumps(check_state) + '\n')
fd.write(json.dumps(check_state, sort_keys=True, indent=4) + '\n')
return check_state
def graph_infos(model_file, output_dir='graphs/',
make_graph=True, make_json=True):
"""Entry point for model stats.
"""
# Load transitions in the model
# Transitions structure format:
# {u'h00': [('Ax', 'n1', {u'label': u'h00[]'}),]
transitions_1, all_places_1 = get_transitions(model_file, all_places=True)
# Get all frontier places in the models
# (places that are never in output position in all transitions)
# EDIT: why we use all_places from the model instead of
# (input_places - output_places) to get frontier places ?
# Because some nodes are only in conditions and not in transitions.
# If we don't do that, these nodes are missing when we compute
# valid paths from conditions.
front_places_1 = " ".join(get_frontier_places(transitions_1, all_places_1))
LOGGER.debug("Frontier places 1: " + str(front_places_1))
# Build graphs & get networkx object
# We give all events in the model as a list of steps
# So we simulate a cadbiom solution (with all events in the model).
res_1 = build_graph(front_places_1, [transitions_1.keys()], transitions_1)
G = res_1[0]
infos = {
'model': model_file,
'nodes': len(G.nodes()),
'edges': len(G.edges()),
'degree': nx.degree_centrality(G),
'betweenness': nx.betweenness_centrality(G),
'closeness': nx.closeness_centrality(G),
}
# Draw graph
if make_graph:
draw_graph(output_dir, front_places_1, 'stat', *res_1)
# Export to json file
if make_json:
with open(output_dir + "infos_results.json", 'w') as fd:
fd.write(json.dumps(infos, sort_keys=True, indent=4) + '\n')
if __name__ == "__main__":
LOG_DIR = "./logs/"
......
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