Commit 9e7301bc authored by VIGNET Pierre's avatar VIGNET Pierre
Browse files

Add merge_cams module to argparse; fix bug in solution_repr call

parent 19ade83b
......@@ -478,143 +478,45 @@ def launch_researchs(args):
formula on each line).
"""
# Module import
import solution_search
params = args_to_param(args)
solution_search.launch_researchs(params) # !
# No input file
if params['final_prop']:
compute_macs(params)
# Multiple properties in input file
# => multiprocessing: 1 process for each property
else:
with open(params['input_file'], 'r') as fd:
g = (line.rstrip('\n') for line in fd)
final_properties = [prop for prop in g if prop != '']
if params['combinations']:
# If input_file is set, we can compute all combinations of
# final_properties. default: False
final_properties = compute_combinations(final_properties)
LOGGER.debug("Final properties: " + str(final_properties))
def update_params(prop):
d = params.copy()
d['final_prop'] = prop
return d
# Fix number of processes
# PS: the new solver is optimized for 8 threads
nb_cpu_required = mp.cpu_count() / 8
nb_cpu_required = 1 if nb_cpu_required == 0 else nb_cpu_required
with ProcessPoolExecutor(max_workers=nb_cpu_required) as e:
futures_and_output = {e.submit(compute_macs,
update_params(job_property)
):job_property \
for job_property in final_properties} # Job name
nb_errors = 0
nb_done = 0
for future in as_completed(futures_and_output):
job_name = futures_and_output[future]
# On affiche les résultats si les futures en contiennent.
# Si elles contiennent une exception, on affiche l'exception.
if future.exception() is not None:
LOGGER.error("{} generated an exception: \n{}".format(
job_name,
future.exception())
)
nb_errors += 1
else:
# The end
LOGGER.info("{}... \t\t[Done]".format(job_name))
nb_done += 1
LOGGER.info("Ending: {} errors, {} done\nbye.".format(
nb_errors,
nb_done)
)
def compute_macs(params):
"""Launch Cadbiom search of MACs (Minimal Activation Conditions).
This function is called 1 or multiple times according to the necessity
to use multiprocessing (Cf launch_researchs()).
.. note:: Previous result files will be deleted.
"""
# Limit recursion
sys.setrecursionlimit(10000)
# QUERY PARAMETERS
# Todo beware with type of separators in path..
MODEL_NAME = params['chart_file'].split('/')[-1][:-4]
# FILES
# Add trailing '/' if not present
output = params['output'] if params['output'][-1] == '/' \
else params['output'] + '/'
CAM_FILE_PREFIX = output + MODEL_NAME + \
'_' + 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"
# Reset files
def remove_file(file):
try:
os.remove(file)
except:
pass
remove_file(cam_file)
remove_file(cam_step_file)
remove_file(cam_complete_file)
remove_file(cam_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
params['steps'], params['final_prop'],
params['start_prop'], params['inv_prop'],
params['all_macs'],
)
def launch_sort(args):
"""Parse a solution file and sort all frontier places in alphabetical order.
"""
# Module import
import solution_sort
params = args_to_param(args)
solution_sort.sort_solutions(params['sol_file'])
def parse_trajectories(args):
"""Parse a complete solution file and make a representation of trajectories.
.. note:: Requires the model file.
"""
# Module import
import solution_repr
params = args_to_param(args)
solution_repr.main(params['chart_file'], params['sol_file'])
def merge_cams(args):
"""Merge solutions to a csv file.
.. note:: CSV file: <Final property formula>;<cam>
"""
# Module import
import solution_merge
params = args_to_param(args)
solution_repr.main(params['sol_file'])
solution_merge.merge_cams_to_csv(params['solutions_directory'],
params['output'])
def args_to_param(args):
......@@ -715,7 +617,7 @@ if __name__ == "__main__":
# subparser: Representation of the trajectories of MACs in a complete file.
# Model file (xml : cadbiom language)
# Solution file (complete)
# Solution file (cam_complete)
parser_trajectories = subparsers.add_parser('parse_trajectories',
help=parse_trajectories.__doc__)
parser_trajectories.add_argument('chart_file')
......@@ -723,8 +625,21 @@ if __name__ == "__main__":
parser_trajectories.set_defaults(func=parse_trajectories)
# subparser: Merge solutions to a csv file
# Solution file (cam)
# Output (csv)
parser_merge_cams = subparsers.add_parser('merge_cams',
help=merge_cams.__doc__)
parser_merge_cams.add_argument('solutions_directory', nargs='?',
default='result/')
parser_merge_cams.add_argument('--output', nargs='?',
default='result/merged_cams.csv')
parser_merge_cams.set_defaults(func=merge_cams)
# get program args and launch associated command
args = parser.parse_args()
# Set log level
LOGGER.setLevel(cm.LOG_LEVELS[vars(args)['verbose']])
# launch associated command
......
......@@ -29,7 +29,7 @@ def merge_cams_to_csv(directory, csvfile='merged_cams.csv'):
csv_data.append([[formula] + [line.rstrip('\n')] for line in fd])
# Write the final csv
with open(directory + csvfile, 'w') as fd:
with open(csvfile, 'w') as fd:
writer = csv.writer(fd, delimiter=str(';'))
writer.writerows(it.chain(*csv_data))
......
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