Added python script for optimizing a set of full patterns to match some reduced patterns.

This commit is contained in:
Iason 2021-03-01 14:40:11 +02:00
parent ed29578f13
commit 47c5b90c54
1 changed files with 83 additions and 0 deletions

83
src/main.py Normal file
View File

@ -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/