Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
VIGNET Pierre
cadbiom
Commits
89a67cbc
Commit
89a67cbc
authored
Oct 25, 2018
by
VIGNET Pierre
Browse files
Move solutions_2_json to solution_sort module
parent
b0cc9362
Changes
3
Hide whitespace changes
Inline
Side-by-side
command_line/cadbiom_cmd/cadbiom_cmd.py
View file @
89a67cbc
...
...
@@ -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'
],
...
...
command_line/cadbiom_cmd/solution_repr.py
View file @
89a67cbc
...
...
@@ -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
):
...
...
command_line/cadbiom_cmd/solution_sort.py
View file @
89a67cbc
...
...
@@ -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
))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment