Commit 412efe1f authored by VIGNET Pierre's avatar VIGNET Pierre
Browse files

[command_line] Change of notation: cam -> mac

parent 36e94e87
......@@ -48,7 +48,7 @@ def solutions_search(args):
def solutions_sort(args):
"""Read a solution file or a directory containing MAC solutions files
(*cam* files), and sort all frontier places/boundaries in alphabetical order.
(*mac* files), and sort all frontier places/boundaries in alphabetical order.
"""
# Module import
......@@ -59,7 +59,7 @@ def solutions_sort(args):
def solutions_2_graph(args):
"""Create GraphML formated files containing a representation of the
trajectories for every solution in complete MAC files (*cam_complete files).
trajectories for every solution in complete MAC files (*mac_complete files).
This is a function to visualize paths taken by the solver from the boundaries
to the entities of interest.
......@@ -77,7 +77,7 @@ def solutions_2_graph(args):
def solutions_2_json(args):
"""Create a JSON formated file containing all data from complete MAC files
(*cam_complete files). The file will contain frontier places/boundaries
(*mac_complete files). The file will contain frontier places/boundaries
and decompiled steps with their respective events for each solution.
This is a function to quickly search all transition attributes involved
......@@ -116,7 +116,7 @@ def json_2_interaction_graph(args):
def solutions_2_common_graph(args):
"""Create a GraphML formated file containing a representation of **all**
trajectories for **all** solutions in complete MAC files (*cam_complete files).
trajectories for **all** solutions in complete MAC files (*mac_complete files).
This is a function to visualize paths taken by the solver from the boundaries
to the entities of interest.
......@@ -134,7 +134,7 @@ def solutions_2_common_graph(args):
def solutions_2_occcurrences_matrix(args):
"""Create a matrix of occurrences counting entities in the solutions found in
*cam.txt files in the given path.
*mac.txt files in the given path.
"""
# Module import
......@@ -211,13 +211,13 @@ def model_graph(args):
solution_repr.model_graph(**params)
def merge_cams(args):
def merge_macs(args):
"""Merge solutions to a csv file."""
# Module import
import solution_merge
params = args_to_param(args)
solution_merge.merge_cams_to_csv(params['solutions_directory'],
solution_merge.merge_macs_to_csv(params['solutions_directory'],
params['output'])
......@@ -332,13 +332,13 @@ def main():
help=solutions_sort.__doc__)
parser_solutions_sort.add_argument('path',
help="Solution file or directory with MAC solutions files "
"(*cam* files) generated with the 'solutions_search' command.")
"(*mac* files) generated with the 'solutions_search' command.")
parser_solutions_sort.set_defaults(func=solutions_sort)
# subparser: Representation of the trajectories of MACs in a complete file.
# Model file (xml : cadbiom language)
# Solution file (cam_complete)
# Solution file (mac_complete)
parser_trajectories = subparsers.add_parser(
'solutions_2_graph',
help=solutions_2_graph.__doc__,
......@@ -347,7 +347,7 @@ def main():
help="bcx model file.")
parser_trajectories.add_argument('path',
help="Complete solution file or directory with MAC solutions files "
"(*cam_complete.txt files) generated with the 'compute_macs' command.")
"(*mac_complete.txt files) generated with the 'compute_macs' command.")
parser_trajectories.add_argument('--output', action=ReadableDir,
nargs='?', default='graphs/',
help="Output directory for GraphML files.")
......@@ -356,7 +356,7 @@ def main():
# subparser: Decompilation of trajectories of MACs in a complete file/dir.
# Model file (xml : cadbiom language)
# Solution file (cam_complete)
# Solution file (mac_complete)
parser_solutions_2_json = subparsers.add_parser(
'solutions_2_json',
help=solutions_2_json.__doc__,
......@@ -365,7 +365,7 @@ def main():
help="bcx model file.")
parser_solutions_2_json.add_argument('path',
help="Complete solution file or directory with MAC solutions files "
"(*cam_complete.txt files) generated with the 'compute_macs' command.")
"(*mac_complete.txt files) generated with the 'compute_macs' command.")
parser_solutions_2_json.add_argument('--output', action=ReadableDir,
nargs='?', default='decompiled_solutions/',
help="Directory for newly created files.")
......@@ -389,7 +389,7 @@ def main():
parser_json_2_interaction_graph.add_argument('--path',
nargs='?', default='decompiled_solutions/',
help="JSON formated file containing all data from complete MAC files"
"(*cam_complete files) generated with the 'solutions_2_json' command.")
"(*mac_complete files) generated with the 'solutions_2_json' command.")
parser_json_2_interaction_graph.add_argument('--output', action=ReadableDir,
nargs='?', default='graphs/',
help="Directory for the newly created file.")
......@@ -398,7 +398,7 @@ def main():
# subparser: Common representation of the trajectories of MACs in a complete file.
# Model file (xml : cadbiom language)
# Solution file (cam_complete)
# Solution file (mac_complete)
parser_trajectories = subparsers.add_parser(
'solutions_2_common_graph',
help=solutions_2_common_graph.__doc__,
......@@ -407,7 +407,7 @@ def main():
help="bcx model file.")
parser_trajectories.add_argument('path',
help="Complete solution file or directory with MAC solutions files "
"(*cam_complete.txt files) generated with the 'compute_macs' command.")
"(*mac_complete.txt files) generated with the 'compute_macs' command.")
parser_trajectories.add_argument('--output', action=ReadableDir,
nargs='?', default='graphs/',
help="Output directory for GraphML files.")
......@@ -416,7 +416,7 @@ def main():
# subparser: Create a matrix of occurrences counting entities in the solutions.
# Model file (xml : cadbiom language)
# Solution file (cam.txt)
# Solution file (mac.txt)
parser_occurrences_matrix = subparsers.add_parser(
'solutions_2_occcurrences_matrix',
help=solutions_2_occcurrences_matrix.__doc__,
......@@ -425,7 +425,7 @@ def main():
help="bcx model file.")
parser_occurrences_matrix.add_argument('path',
help="Directory with MAC solutions files "
"(*cam.txt files) generated with the 'compute_macs' command.")
"(*mac.txt files) generated with the 'compute_macs' command.")
parser_occurrences_matrix.add_argument('--output', action=ReadableDir,
nargs='?', default='./',
help="Output directory for CSV files.")
......@@ -435,19 +435,19 @@ def main():
# subparser: Merge solutions to a csv file
# Solution file (cam)
# Solution file (mac)
# Output (csv)
parser_merge_cams = subparsers.add_parser(
'merge_cams',
help=merge_cams.__doc__,
parser_merge_macs = subparsers.add_parser(
'merge_macs',
help=merge_macs.__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser_merge_cams.add_argument('solutions_directory', nargs='?',
parser_merge_macs.add_argument('solutions_directory', nargs='?',
default='result/')
parser_merge_cams.add_argument('--output', nargs='?',
default='result/merged_cams.csv',
help="CSV file: <Final property formula>;<cam>")
parser_merge_cams.set_defaults(func=merge_cams)
parser_merge_macs.add_argument('--output', nargs='?',
default='result/merged_macs.csv',
help="CSV file: <Final property formula>;<mac>")
parser_merge_macs.set_defaults(func=merge_macs)
## Model-related commands ##################################################
......
......@@ -28,8 +28,8 @@ import glob
import csv
import itertools as it
def merge_cams_to_csv(directory, csvfile='merged_cams.csv'):
"""Merge \*cam.txt files from a directory to a csv file.
def merge_macs_to_csv(directory, csvfile='merged_macs.csv'):
"""Merge \*mac.txt files from a directory to a csv file.
:Structure of the CSV file:
......@@ -43,17 +43,17 @@ def merge_cams_to_csv(directory, csvfile='merged_cams.csv'):
csv_data = list()
# Read all files in the given directory
for filename in glob.glob(directory + '*_cam.txt'):
for filename in glob.glob(directory + '*_mac.txt'):
#print(filename)
# Extract the formula from the filename
# ex:
# # ['./result/model_name', 'TGFB1', 'cam.txt']
# # ['./result/model_name', 'TGFB1', 'mac.txt']
formula = ''.join(filename.split('_')[1:-1])
# Read the content of the cam file & memorize this content
# Read the content of the mac file & memorize this content
with open(filename) as f_d:
# Add the formula column, before each cam to futur csv file
# Add the formula column, before each mac to futur csv file
csv_data.append([[formula] + [line.rstrip('\n')] for line in f_d])
# Write the final csv
......@@ -64,4 +64,4 @@ def merge_cams_to_csv(directory, csvfile='merged_cams.csv'):
if __name__ == "__main__":
merge_cams_to_csv('result')
merge_macs_to_csv('result')
......@@ -158,8 +158,6 @@ def draw_graph(output_dir, solution, solution_index, G,
plt.show()
def test_main():
"""Test"""
......
......@@ -118,16 +118,16 @@ def make_logical_formula(previous_frontier_places, start_prop):
return prev_frontier_places_formula
return start_prop
cam_list = [logical_and(frontier_places)
mac_list = [logical_and(frontier_places)
for frontier_places in previous_frontier_places]
if cam_list:
if mac_list:
# Logical or between each line
return add_start_prop('not(' + logical_or(cam_list) + ')')
return add_start_prop('not(' + logical_or(mac_list) + ')')
return add_start_prop('')
def main2(chart_file, cam_file, cam_step_file, cam_complete_file, cam_strong_file,
def main2(chart_file, mac_file, mac_step_file, mac_complete_file, mac_strong_file,
steps, final_prop, start_prop, inv_prop, all_macs, continue_run):
"""
......@@ -183,14 +183,14 @@ def main2(chart_file, cam_file, cam_step_file, cam_complete_file, cam_strong_fil
if continue_run:
# Reload previous working files
try:
previous_frontier_places = read_cam_file(cam_file)
previous_frontier_places = read_mac_file(mac_file)
current_start_prop = make_logical_formula(previous_frontier_places,
start_prop)
LOGGER.info("%s:: Reload previous frontier places: %s",
final_prop,
len(previous_frontier_places))
except IOError:
LOGGER.warning("%s:: cam file not found!", final_prop)
LOGGER.warning("%s:: mac file not found!", final_prop)
previous_frontier_places = set()
current_start_prop = start_prop
else:
......@@ -205,7 +205,7 @@ def main2(chart_file, cam_file, cam_step_file, cam_complete_file, cam_strong_fil
print("START PROP:", current_start_prop)
ret = \
find_mac(mcla,
cam_file, cam_step_file, cam_complete_file,
mac_file, mac_step_file, mac_complete_file,
steps, final_prop, current_start_prop, inv_prop)
# EXIT
# i += 1
......@@ -243,7 +243,7 @@ def main2(chart_file, cam_file, cam_step_file, cam_complete_file, cam_strong_fil
def find_macs(mcla,
cam_file, cam_step_file, cam_complete_file,
mac_file, mac_step_file, mac_complete_file,
steps, final_prop, start_prop, inv_prop):
"""__mac_exhaustive_search, on cherche d´abord des solutions non minimales
(lfsol = self.__sq_dimacs_frontier_solutions(query, nb_step, 2))
......@@ -271,24 +271,24 @@ def find_macs(mcla,
# Save MAC and timings
LOGGER.debug("%s:: Save MAC and timings...", final_prop)
with open(cam_complete_file, 'a') as file:
with open(mac_complete_file, 'a') as file:
next_mac_object.save(file)
# Save MAC
next_mac = next_mac_object.activated_frontier
LOGGER.debug("%s:: Save next MAC: %s", final_prop, next_mac)
with open(cam_file, 'a') as file:
with open(mac_file, 'a') as file:
file.write('\t'.join(next_mac) + '\n')
# Save min steps
min_step = mcla.unfolder.get_current_step() - 1 # Magic number !
LOGGER.debug("%s:: Save minimal steps: %s", final_prop, min_step)
with open(cam_step_file, 'a') as file:
with open(mac_step_file, 'a') as file:
file.write(str(min_step)+'\n')
def find_mac(mcla,
cam_file, cam_step_file, cam_complete_file,
mac_file, mac_step_file, mac_complete_file,
steps, final_prop, start_prop, inv_prop):
"""
list, min step
......@@ -319,19 +319,19 @@ def find_mac(mcla,
# Save MAC and timings
LOGGER.debug("%s:: Save MAC and timings...", final_prop)
with open(cam_complete_file, 'a') as file:
with open(mac_complete_file, 'a') as file:
next_mac_object.save(file)
# Save MAC (in alphabetic order...)
next_mac = sorted(next_mac_object.activated_frontier)
LOGGER.debug("%s:: Save next MAC: %s", final_prop, next_mac)
with open(cam_file, 'a') as file:
with open(mac_file, 'a') as file:
file.write(" ".join(sorted(next_mac, key=lambda s: s.lower()))+'\n')
# Save min steps
min_step = mcla.unfolder.get_current_step() - 1 # Magic number !
LOGGER.debug("%s:: Save minimal steps: %s", final_prop, min_step)
with open(cam_step_file, 'a') as file:
with open(mac_step_file, 'a') as file:
file.write(str(min_step)+'\n')
return next_mac, min_step
......@@ -374,25 +374,25 @@ def detect_model_type(mclanalyser, filepath):
return build_func[extension]
#def main(chart_file, cam_file, cam_step_file, cam_complete_file, cam_strong_file,
#def main(chart_file, mac_file, mac_step_file, mac_complete_file, mac_strong_file,
# steps, final_prop, start_prop, inv_prop, all_macs):
#
# LOGGER.debug("Params: start: {}, inv: {}, final: {}".format(start_prop,
# inv_prop,
# final_prop))
#
# cam_p = None
# # forbid previous cam
# mac_p = None
# # forbid previous mac
# try :
# cam_p = camFile2notOr(cam_file)
# print("cam file:", cam_p)
# mac_p = camFile2notOr(mac_file)
# print("mac file:", mac_p)
# except :
# print('error in camFile2notOr')
#
# if start_prop and cam_p :
# start_prop += ' and ('+cam_p+')'
# elif cam_p :
# start_prop = cam_p
# if start_prop and mac_p :
# start_prop += ' and ('+mac_p+')'
# elif mac_p :
# start_prop = mac_p
#
# # BUILD MCLA
# error_reporter = ErrorRep()
......@@ -420,15 +420,15 @@ def detect_model_type(mclanalyser, filepath):
## print("DEBUG:: ", len( lsol))
#
# # minimal activation conditions
# # on_cam function:
## cam_list = self.mcla.mac_search(query, self.max_step)
## if len(cam_list)==0 :
# # on_mac function:
## mac_list = self.mcla.mac_search(query, self.max_step)
## if len(mac_list)==0 :
## ok_warn("The solver returns an empty list" +
## "\n"+" you should refine your query")
#
# # OPTIMIZE STEP RESEARCH
# if os.path.isfile(cam_step_file):
# min_step = int(get_last_line(cam_step_file))
# if os.path.isfile(mac_step_file):
# min_step = int(get_last_line(mac_step_file))
# print("min_step opti:", min_step)
# query.set_steps_before_reach(min_step-1)
#
......@@ -446,18 +446,18 @@ def detect_model_type(mclanalyser, filepath):
# if next_mac_object:
#
# # SAVE MAC AND TIMING
# with open(cam_complete_file, 'a') as file:
# with open(mac_complete_file, 'a') as file:
# next_mac_object.save(file)
#
# # SAVE MAC
# next_mac = next_mac_object.activated_frontier
# print("save next mac:", next_mac)
# write_list(next_mac, cam_file)
# write_list(next_mac, mac_file)
#
# # SAVE STEP
# min_step = mcla.unfolder.get_current_step()
# print("save min step:", min_step)
# with open(cam_step_file, 'a') as file:
# with open(mac_step_file, 'a') as file:
# file.write(str(min_step)+'\n')
#
# return 0
......@@ -619,16 +619,16 @@ def compute_macs(params):
# Add trailing '/' if not present
output = params['output'] if params['output'][-1] == '/' \
else params['output'] + '/'
cam_file_prefix = output + model_filename + \
'_' + params['final_prop'] + '_cam'
# cam_file
cam_file = cam_file_prefix + ".txt"
# cam_step_file
cam_step_file = cam_file_prefix + "_step.txt"
# cam_complete_file
cam_complete_file = cam_file_prefix + "_complete.txt"
# cam_strong_file
cam_strong_file = cam_file_prefix + "_strongA.txt"
mac_file_prefix = output + model_filename + \
'_' + params['final_prop'] + '_mac'
# mac_file
mac_file = mac_file_prefix + ".txt"
# mac_step_file
mac_step_file = mac_file_prefix + "_step.txt"
# mac_complete_file
mac_complete_file = mac_file_prefix + "_complete.txt"
# mac_strong_file
mac_strong_file = mac_file_prefix + "_strongA.txt"
def remove_file(file):
"""Reset files"""
......@@ -640,25 +640,25 @@ def compute_macs(params):
if not params['continue']:
# Reset previous working files
# PS: the reload is done in main2() function
remove_file(cam_file)
remove_file(cam_step_file)
remove_file(cam_complete_file)
remove_file(cam_strong_file)
remove_file(mac_file)
remove_file(mac_step_file)
remove_file(mac_complete_file)
remove_file(mac_strong_file)
# MAC research
main2(
params['chart_file'], # chart_file
cam_file, # cam_file
cam_step_file, # cam_step_file
cam_complete_file, # cam_complete_file
cam_strong_file, # cam_strong_file
mac_file, # mac_file
mac_step_file, # mac_step_file
mac_complete_file, # mac_complete_file
mac_strong_file, # mac_strong_file
params['steps'], params['final_prop'],
params['start_prop'], params['inv_prop'],
params['all_macs'], params['continue'],
)
def read_cam_file(file):
"""Return a list a fontier places already found in cam file
def read_mac_file(file):
"""Return a list a fontier places already found in mac file
.. note:: use make_logical_formula() to get the new start_prop of the run.
......
......@@ -81,7 +81,7 @@ import networkx as nx
from tools.solutions import get_solutions
from tools.models import get_transitions_from_model_file
from tools.solutions import load_solutions, convert_solutions_to_json, \
get_query_from_filename, get_cam_lines
get_query_from_filename, get_mac_lines
from tools.graphs import export_graph, build_graph
import cadbiom.commons as cm
......@@ -130,7 +130,7 @@ def sort_solutions_in_file(filepath):
def solutions_sort(path):
"""Entry point for sorting solutions.
Read a solution(s) file(s) (\*cam\* files) and sort all
Read a solution(s) file(s) (\*mac\* files) and sort all
frontier places/boundaries in alphabetical order.
This functions tests if the given path is a directory or a file.
......@@ -145,10 +145,10 @@ def solutions_sort(path):
assert os.path.isfile(path) or os.path.isdir(path)
if os.path.isdir(path):
# Recursive search of *cam* files
# (cam.txt, cam_complete.txt, cam_step.txt)
# Recursive search of *mac* files
# (mac.txt, mac_complete.txt, mac_step.txt)
path = path if path[-1] == '/' else path + '/'
[sort_solutions_in_file(file) for file in glob.glob(path + '*cam*')]
[sort_solutions_in_file(file) for file in glob.glob(path + '*mac*')]
else:
sort_solutions_in_file(path)
......@@ -158,7 +158,7 @@ def solutions_2_json(output_dir, model_file, path, conditions=True):
"""Entry point for solutions_2_json
Create a JSON formated file containing all data from complete MAC files
(\*cam_complete files). The file will contain frontier places/boundaries
(\*mac_complete files). The file will contain frontier places/boundaries
and decompiled steps with their respective events for each solution.
This is a function to quickly search all transition attributes involved
......@@ -210,7 +210,7 @@ def solutions_2_json(output_dir, model_file, path, conditions=True):
# Decompilation of all files in the directory
file_number = 0
for file_number, solution_file in \
enumerate(glob.glob(path + '*cam_complete.txt'), 1):
enumerate(glob.glob(path + '*mac_complete.txt'), 1):
decomp_solutions = convert_solutions_to_json(
load_solutions(solution_file),
......@@ -226,7 +226,7 @@ def solutions_2_graph(output_dir, model_file, path):
"""Entry point for solutions_2_graph
Create GraphML formated files containing a representation of the
trajectories for every solution in complete MAC files (\*cam_complete files).
trajectories for every solution in complete MAC files (\*mac_complete files).
This is a function to visualize paths taken by the solver from the boundaries
to the entities of interest.
......@@ -263,7 +263,7 @@ def solutions_2_graph(output_dir, model_file, path):
# Decompilation of all files in the directory
file_number = 0
for file_number, solution_file in \
enumerate(glob.glob(path + '*cam_complete.txt'), 1):
enumerate(glob.glob(path + '*mac_complete.txt'), 1):
convert_solution_file_to_graphs(
output_dir,
......@@ -322,7 +322,7 @@ def solutions_2_common_graph(output_dir, model_file, path):
"""Entry point for solutions_2_common_graph
Create a GraphML formated file containing a representation of **all**
trajectories for **all** solutions in complete MAC files (\*cam_complete files).
trajectories for **all** solutions in complete MAC files (\*mac_complete files).
This is a function to visualize paths taken by the solver from the boundaries
to the entities of interest.
......@@ -381,7 +381,7 @@ def solutions_2_common_graph(output_dir, model_file, path):
# Decompilation of all files in the directory
file_number = 0
for file_number, solution_file in \
enumerate(glob.glob(path + '*cam_complete.txt'), 1):
enumerate(glob.glob(path + '*mac_complete.txt'), 1):
# Get query string from the name of the solution file
query = get_query_from_filename(model_file, solution_file)
......@@ -465,9 +465,9 @@ def occurrence_matrix(output_dir, model_file, path,
matrix_filename='occurrence_matrix.csv'):
"""Make a matrix of occurrences for the solutions in the given path.
- Compute occurrences of each place in all `cam.txt` files.
- Compute occurrences of each place in all `mac.txt` files.
- Save the matrix in csv format with the following columns:
Fieldnames: "patterns (number)/places (number);cam_number;frontier places"
Fieldnames: "patterns (number)/places (number);mac_number;frontier places"
Each request (pattern) is accompanied by the number of solutions found.
.. todo:: Split the creation and writing of the matrix in 2 functions.
......@@ -488,38 +488,38 @@ def occurrence_matrix(output_dir, model_file, path,
# Key: Logical formula as input of Cadbiom
# Value: Number of each place in all solutions of the current file
matrix = defaultdict(Counter)
# All frontier places in all cam files
# All frontier places in all mac files
all_frontier_places = set()
# Compute occurrences of each place in all cam files
# Compute occurrences of each place in all mac files
file_number = 0
for file_number, filepath in enumerate(glob.glob(path + '*cam.txt'), 1):
for file_number, filepath in enumerate(glob.glob(path + '*mac.txt'), 1):
# gene pattern
# pattern = {gene for gene in genes if gene in cam}
# pattern = {gene for gene in genes if gene in mac}
# Get query string from the name of the solution file
# From: 'MODEL_NAME_PLACE1 and not PLACE2 and not PLACE3_cam.txt'
# From: 'MODEL_NAME_PLACE1 and not PLACE2 and not PLACE3_mac.txt'
# Get: 'PLACE1 and not PLACE2 and not PLACE3'
query = get_query_from_filename(model_file, filepath)
cam_number = 0
for cam_number, cam_line in enumerate(get_cam_lines(filepath), 1):
mac_number = 0
for mac_number, mac_line in enumerate(get_mac_lines(filepath), 1):
frontier_places = set(cam_line.split(' '))
frontier_places = set(mac_line.split(' '))
# Update set of all frontier places
all_frontier_places.update(frontier_places)
# Update counter of places => compute frequencies
matrix[query] += Counter(frontier_places)
# Set the cam_number for future standardization
matrix[query]["cam_number"] = cam_number
# Set the mac_number for future standardization
matrix[query]["mac_number"] = mac_number
LOGGER.info("Files processed: %s", file_number)
# Save the matrix
# columns: "patterns (number)/places (number);cam_number;frontier places"
# columns: "patterns (number)/places (number);mac_number;frontier places"
with open(output_dir + matrix_filename, 'w') as f_d:
# Forge header
......@@ -531,7 +531,7 @@ def occurrence_matrix(output_dir, model_file, path,
f_d,
delimiter=str(';'),
restval=0, # default value for frequency
fieldnames=[header, "cam_number"] + list(all_frontier_places))
fieldnames=[header, "mac_number"] + list(all_frontier_places))
writer.writeheader()
# Add a last line in the csv: total of occurrences for each place
......@@ -540,7 +540,7 @@ def occurrence_matrix(output_dir, model_file, path,