diff --git a/study.py b/study.py index 2532d13..3b956e8 100644 --- a/study.py +++ b/study.py @@ -12,7 +12,20 @@ from DAS import * # https://stackoverflow.com/questions/58026381/logging-nested-functions-using-joblib-parallel-and-delayed-calls # and https://github.com/joblib/joblib/issues/1017 -def runOnce(sim, config, shape): +def initLogger(config): + """It initializes the logger.""" + logger = logging.getLogger("Study") + logger.setLevel(config.logLevel) + ch = logging.StreamHandler() + ch.setLevel(config.logLevel) + ch.setFormatter(CustomFormatter()) + logger.addHandler(ch) + return logger + +def runOnce(config, shape): + + sim = Simulator(shape, config) + if config.deterministic: shape.setSeed(config.randomSeed+"-"+str(shape)) random.seed(shape.randomSeed) @@ -40,24 +53,24 @@ def study(): print("You need to pass a configuration file in parameter") exit(1) - shape = Shape(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) - sim = Simulator(shape, config) - sim.initLogger() + logger = initLogger(config) + format = {"entity": "Study"} + results = [] now = datetime.now() execID = now.strftime("%Y-%m-%d_%H-%M-%S_")+str(random.randint(100,999)) - sim.logger.info("Starting simulations:", extra=sim.format) + logger.info("Starting simulations:", extra=format) start = time.time() - results = Parallel(config.numJobs)(delayed(runOnce)(sim, config, shape) for shape in config.nextShape()) + results = Parallel(config.numJobs)(delayed(runOnce)(config, shape) for shape in config.nextShape()) end = time.time() - sim.logger.info("A total of %d simulations ran in %d seconds" % (len(results), end-start), extra=sim.format) + logger.info("A total of %d simulations ran in %d seconds" % (len(results), end-start), extra=format) if config.dumpXML: for res in results: res.dump(execID) - sim.logger.info("Results dumped into results/%s/" % (execID), extra=sim.format) + logger.info("Results dumped into results/%s/" % (execID), extra=format) if config.visualization: vis = Visualizer(execID)