From 47c5b90c54e05c23fbb088a8e542954d21292f98 Mon Sep 17 00:00:00 2001 From: Iason Date: Mon, 1 Mar 2021 14:40:11 +0200 Subject: [PATCH] Added python script for optimizing a set of full patterns to match some reduced patterns. --- src/main.py | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/main.py diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..837401b --- /dev/null +++ b/src/main.py @@ -0,0 +1,83 @@ +# This is a sample Python script. + +# Press Shift+F10 to execute it or replace it with your code. +# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. + + +import multiprocessing +import subprocess +import os +from os import listdir +from os.path import isfile, join +import itertools +import shutil + +numberOfOptimizedPatterns=0 +manager = multiprocessing.Manager() +q = manager.Queue() + +def listener(q): + '''listens for messages on the q, writes to file. ''' + + with open("Results/OptimizationResults/results.csv", 'ab') as f: + while 1: + m = q.get() + if m == 'kill': + # f.write('killed') + break + f.write(m) + f.flush() + global numberOfOptimizedPatterns + numberOfOptimizedPatterns=numberOfOptimizedPatterns+1 + print("Optimized patterns:"+ str(numberOfOptimizedPatterns)) + +def optimize(fullPatternFilepath, reducedPatternFilepath): + """Call run(), catch exceptions.""" + dirname = os.path.abspath(os.path.dirname(__file__)) + executableFilepath = os.path.join(dirname, 'ReducedModelOptimization') + try: + p = args = (executableFilepath, + fullPatternFilepath, + reducedPatternFilepath, + "1500",'Results/OptimizationResults/') + # print("Optimizing " + fullPatternFilepath + "@" + reducedPatternFilepath) + popen = subprocess.Popen(args, stdout=subprocess.PIPE) + popen.wait() + output = popen.stdout.read() + q.put(output) + + + except Exception as e: + print("error: %s run(*%r, **%r)" % (e, fullPatternFilepath,reducedPatternFilepath)) + +# Press the green button in the gutter to run the script. +if __name__ == '__main__': + fullPatternDirectory= "TestSet/FullPatterns" + reducedPatternDirectory= "TestSet/ReducedPatterns" + # Create results folder + resultsDir = 'Results/OptimizationResults' + if os.path.exists(resultsDir): + shutil.rmtree(resultsDir) + os.makedirs(resultsDir) + # Write results + fullPatternFilepaths=[join(fullPatternDirectory, fullPatternFilepath) for fullPatternFilepath in listdir(fullPatternDirectory) if isfile(join(fullPatternDirectory, fullPatternFilepath))] + # print(fullPatternFilepaths) + reducedPatternFilepaths=[join(reducedPatternDirectory,reducedPatternFilepath) for reducedPatternFilepath in listdir(reducedPatternDirectory) if isfile(join(reducedPatternDirectory, reducedPatternFilepath))] + # print(reducedPatternFilepaths) + #optimizationPairs=list(itertools.product(fullPatternFilepaths,reducedPatternFilepaths)) + optimizationPairs=list(itertools.product(fullPatternFilepaths,['TestSet/ReducedPatterns/CW_reduced.ply'])) + #print(optimizationPairs) + pool=multiprocessing.Pool(23) + + watcher = pool.apply_async(listener, (q,)) + pool.starmap(optimize,optimizationPairs) + + # f.close() + q.put('kill') + pool.close() + pool.join() + # + # Or just: + # args = "bin/bar -c somefile.xml -d text.txt -r aString -f anotherString".split() + +# See PyCharm help at https://www.jetbrains.com/help/pycharm/