From 0168152807ffbbb226558eed1aebfaeea46f1754 Mon Sep 17 00:00:00 2001 From: Nikolai Hartmann <Nikolai.Hartmann@physik.uni-muenchen.de> Date: Tue, 12 Jun 2018 16:58:47 +0200 Subject: [PATCH] Switch saving options of new projects to pickle, but maintain support for json --- browse.py | 4 ++++ toolkit.py | 26 ++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/browse.py b/browse.py index ed91b86..b641459 100755 --- a/browse.py +++ b/browse.py @@ -1,8 +1,12 @@ import sys +import logging import numpy as np import matplotlib.pyplot as plt from KerasROOTClassification import * +logging.basicConfig() +logging.getLogger("KerasROOTClassification").setLevel(logging.INFO) + c = ClassificationProject(sys.argv[1]) diff --git a/toolkit.py b/toolkit.py index 75cdbc1..b8ae993 100755 --- a/toolkit.py +++ b/toolkit.py @@ -52,6 +52,19 @@ K.set_session(session) import ROOT +def byteify(input): + "From stackoverflow https://stackoverflow.com/a/13105359" + if isinstance(input, dict): + return {byteify(key): byteify(value) + for key, value in input.iteritems()} + elif isinstance(input, list): + return [byteify(element) for element in input] + elif isinstance(input, unicode): + return input.encode('utf-8') + else: + return input + + class ClassificationProject(object): """Simple framework to load data from ROOT TTrees and train Keras @@ -144,13 +157,18 @@ class ClassificationProject(object): else: # otherwise initialise new project self._init_from_args(name, *args, **kwargs) - with open(os.path.join(self.project_dir, "options.json"), "w") as of: - json.dump(dict(args=args, kwargs=kwargs), of) + with open(os.path.join(self.project_dir, "options.pickle"), "w") as of: + pickle.dump(dict(args=args, kwargs=kwargs), of) def _init_from_dir(self, dirname): - with open(os.path.join(dirname, "options.json")) as f: - options = yaml.safe_load(f) + if not os.path.exists(os.path.join(dirname, "options.pickle")): + # for backward compatibility + with open(os.path.join(dirname, "options.json")) as f: + options = byteify(json.load(f)) + else: + with open(os.path.join(dirname, "options.pickle")) as f: + options = pickle.load(f) options["kwargs"]["project_dir"] = dirname self._init_from_args(os.path.basename(dirname), *options["args"], **options["kwargs"]) -- GitLab