Commit 89a67cbc authored by VIGNET Pierre's avatar VIGNET Pierre
Browse files

Move solutions_2_json to solution_sort module

parent b0cc9362
......@@ -82,11 +82,11 @@ def solutions_2_json(args):
"""
# Module import
import solution_repr
import solution_sort
params = args_to_param(args)
params['output'] = params['output'] if params['output'][-1] == '/' \
else params['output'] + '/'
solution_repr.solutions_2_json(
solution_sort.solutions_2_json(
params['output'],
params['chart_file'],
params['path'],
......
......@@ -63,8 +63,6 @@ import datetime as dt
import networkx as nx
import networkx.algorithms.isomorphism as iso
import json
import os
import glob
import csv
from urllib import quote as urllib_quote
# Remove matplotlib dependency
......@@ -281,64 +279,7 @@ def test_main():
print(transitions)
def solutions_2_json(output_dir, model_file, solution_path, conditions=True):
"""Entry point for solutions_2_json
This functions tests if the given path is a directory or a file.
:param output_dir: Output path.
:param model_file: Filepath of the model.
:param solution_path: Filepath/directory of a complete solution file.
:param conditions: (Optional) If False, conditions of transitions will not
be present in the JSON file. This allows to have only places/entities
used inside trajectories; thus, inhibitors are avoided.
:type output_dir: <str>
:type model_file: <str>
:type solution_path: <str>
:type conditions: <boolean>
"""
def write_json_file(decompiled_filename, decomp_solutions):
# Write file
with open(decompiled_filename, 'w') as f_d:
json.dump(decomp_solutions, f_d, sort_keys=True, indent=4)
# Get transitions from the model
model_transitions = get_transitions_from_model_file(model_file)
if os.path.isfile(solution_path):
# The given path is a solution file
# Add _decomp to the solution filename
filename = os.path.basename(os.path.splitext(solution_path)[0])
decompiled_filename = output_dir + filename + '_decomp.txt'
decomp_solutions = get_json_solutions(
load_solutions(solution_path),
model_transitions,
conditions=conditions,
)
write_json_file(decompiled_filename, decomp_solutions)
elif os.path.isdir(solution_path):
# The given path is a directory
solution_path = solution_path if solution_path[-1] == '/' \
else solution_path + '/'
# Decompilation of all files in the directory
for file_number, solution_file in \
enumerate(glob.glob(solution_path + '*cam_complete.txt'), 1):
# Add _decomp to the solution filename
filename = os.path.basename(os.path.splitext(solution_file)[0])
decompiled_filename = output_dir + filename + '_decomp.txt'
decomp_solutions = get_json_solutions(
load_solutions(solution_file),
model_transitions,
conditions=conditions,
)
write_json_file(decompiled_filename, decomp_solutions)
LOGGER.info("Files processed: " + str(file_number))
def parse_trajectories_main(output_dir, model_file, solution_file):
......
......@@ -27,10 +27,20 @@ files of Cadbiom.
from __future__ import unicode_literals
from __future__ import print_function
# Standard imports
import json
import os
import glob
# Library imports
from tools.solutions import get_solutions
from tools.models import get_transitions_from_model_file
from tools.solutions import load_solutions, get_json_solutions
import cadbiom.commons as cm
LOGGER = cm.logger()
## Sort functions ##############################################################
......@@ -93,3 +103,64 @@ def sort_solutions(path):
[sort_solutions_in_file(file) for file in glob.glob(path + '*cam*')]
else:
sort_solutions_in_file(path)
## Conversion functions ########################################################
def solutions_2_json(output_dir, model_file, solution_path, conditions=True):
"""Entry point for solutions_2_json
This functions tests if the given path is a directory or a file.
:param output_dir: Output path.
:param model_file: Filepath of the model.
:param solution_path: Filepath/directory of a complete solution file.
:param conditions: (Optional) If False, conditions of transitions will not
be present in the JSON file. This allows to have only places/entities
used inside trajectories; thus, inhibitors are avoided.
:type output_dir: <str>
:type model_file: <str>
:type solution_path: <str>
:type conditions: <boolean>
"""
def write_json_file(decompiled_filename, decomp_solutions):
# Write file
with open(decompiled_filename, 'w') as f_d:
json.dump(decomp_solutions, f_d, sort_keys=True, indent=4)
# Get transitions from the model
model_transitions = get_transitions_from_model_file(model_file)
if os.path.isfile(solution_path):
# The given path is a solution file
# Add _decomp to the solution filename
filename = os.path.basename(os.path.splitext(solution_path)[0])
decompiled_filename = output_dir + filename + '_decomp.txt'
decomp_solutions = get_json_solutions(
load_solutions(solution_path),
model_transitions,
conditions=conditions,
)
write_json_file(decompiled_filename, decomp_solutions)
elif os.path.isdir(solution_path):
# The given path is a directory
solution_path = solution_path if solution_path[-1] == '/' \
else solution_path + '/'
# Decompilation of all files in the directory
for file_number, solution_file in \
enumerate(glob.glob(solution_path + '*cam_complete.txt'), 1):
# Add _decomp to the solution filename
filename = os.path.basename(os.path.splitext(solution_file)[0])
decompiled_filename = output_dir + filename + '_decomp.txt'
decomp_solutions = get_json_solutions(
load_solutions(solution_file),
model_transitions,
conditions=conditions,
)
write_json_file(decompiled_filename, decomp_solutions)
LOGGER.info("Files processed: " + str(file_number))
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