From 98e01b6a3da2c4e9dc97f41c8563ec48785a0689 Mon Sep 17 00:00:00 2001 From: Francois Date: Thu, 7 Mar 2019 17:32:02 +0100 Subject: [PATCH] evaluation des env dans le yaml --- iss/data/CollectionManager.py | 14 ++++++------ iss/tools/config.py | 29 +++++++++++++++++++------ notebooks/test_collection.ipynb | 38 ++++++++++++++++++++++----------- 3 files changed, 55 insertions(+), 26 deletions(-) diff --git a/iss/data/CollectionManager.py b/iss/data/CollectionManager.py index 1b862e8..aefae49 100644 --- a/iss/data/CollectionManager.py +++ b/iss/data/CollectionManager.py @@ -8,17 +8,17 @@ import re class CollectionManagerFromDirectory: - def __init__(self, dir, config): + def __init__(self, config): self.config = config + self.dir = self.config.get('directory')['collections'] jpg_regex = re.compile(".*jpg$") - self.pictures_id = [pict for pict in os.listdir(dir) if jpg_regex.match(pict)] + self.pictures_id = [pict for pict in os.listdir(self.dir) if jpg_regex.match(pict)] - self.dir = os.path.join(self.config.project_dir, dir) - self.dir_base = os.path.join(self.config.project_dir, self.config.get('directory')['autoencoder']['base']) - self.dir_train = os.path.join(self.config.project_dir, self.config.get('directory')['autoencoder']['train']) - self.dir_test = os.path.join(self.config.project_dir, self.config.get('directory')['autoencoder']['test']) - self.dir_valid = os.path.join(self.config.project_dir, self.config.get('directory')['autoencoder']['valid']) + self.dir_base = self.config.get('directory')['autoencoder']['base'] + self.dir_train = self.config.get('directory')['autoencoder']['train'] + self.dir_test = self.config.get('directory')['autoencoder']['test'] + self.dir_valid = self.config.get('directory')['autoencoder']['valid'] self.seed = self.config.get('training')['seed'] self.proportions = self.config.get('training')['proportions'] diff --git a/iss/tools/config.py b/iss/tools/config.py index 2b05250..c2d9635 100644 --- a/iss/tools/config.py +++ b/iss/tools/config.py @@ -4,19 +4,34 @@ import os import sys import yaml from dotenv import find_dotenv, load_dotenv +import re load_dotenv(find_dotenv()) + class Config: - def __init__(self, project_dir = os.getenv("PROJECT_DIR"), mode = os.getenv("MODE")): + def __init__(self, project_dir = os.getenv("PROJECT_DIR"), mode = os.getenv("MODE")): - self.project_dir = project_dir - self.mode = mode + self.project_dir = project_dir + self.mode = mode + self.path_matcher = re.compile(r'\$\{([^}^{]+)\}') - with open(os.path.join(self.project_dir, 'config', 'config_%s.yaml' % (self.mode)), 'r') as ymlfile: - self.config = yaml.load(ymlfile) + yaml.add_implicit_resolver('!path', self.path_matcher, None, yaml.SafeLoader) + yaml.add_constructor('!path', self.path_constructor, yaml.SafeLoader) + print("ok") - def get(self, key): - return self.config[key] + with open(os.path.join(self.project_dir, 'config', 'config_%s.yaml' % (self.mode)), 'r') as ymlfile: + self.config = yaml.safe_load(ymlfile) + + def get(self, key): + return self.config[key] + + def path_constructor(self, loader, node): + ''' Extract the matched value, expand env variable, and replace the match ''' + value = node.value + match = self.path_matcher.match(value) + env_var = match.group()[2:-1] + + return os.environ.get(env_var) + value[match.end():] diff --git a/notebooks/test_collection.ipynb b/notebooks/test_collection.ipynb index 1e513e7..907066c 100644 --- a/notebooks/test_collection.ipynb +++ b/notebooks/test_collection.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": 21, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 5, "metadata": {}, "outputs": [ { @@ -31,7 +31,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -48,12 +48,19 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 7, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ok\n" + ] + } + ], "source": [ - "cfg = Config()\n", - "dir = os.path.join(cfg.project_dir, cfg.get('directory')['collections'])" + "cfg = Config()" ] }, { @@ -65,16 +72,16 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ - "collection = CollectionManagerFromDirectory(dir = dir, config = cfg)" + "collection = CollectionManagerFromDirectory(config = cfg)" ] }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -83,7 +90,7 @@ "{'total': 13625}" ] }, - "execution_count": 26, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -94,7 +101,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -492,6 +499,13 @@ "source": [ "autoencoder.fit_generator(train_generator, steps_per_epoch=1000, epochs = 2)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": {