Skip to content

Commit

Permalink
Add new configuration object (#401)
Browse files Browse the repository at this point in the history
* Added set_data_path, and updated get_filepath to use static variable

* Added tests for set_data_path

* Linted

* Added config class and updated PrimitiveBase.get_filepath to use config

* rename config class

* made config importable from featuretools. config module now called config_init

* Update test_feature_base.py

* use primitives instead of features in set_data_path test
  • Loading branch information
CharlesBradshaw committed Feb 5, 2019
1 parent 56b6f91 commit de2ff0e
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 5 deletions.
2 changes: 1 addition & 1 deletion featuretools/__init__.py
@@ -1,6 +1,6 @@
from __future__ import absolute_import
# flake8: noqa
from . import config
from .config_init import config
from . import variable_types
from .entityset.api import *
from . import primitives
Expand Down
26 changes: 26 additions & 0 deletions featuretools/config.py → featuretools/config_init.py
@@ -1,3 +1,4 @@
import copy
import logging
import os
import sys
Expand Down Expand Up @@ -44,3 +45,28 @@ def initialize_logging():


initialize_logging()


class Config():
def __init__(self):
self._data = {}
self.set_to_default()

def set_to_default(self):
PWD = os.path.dirname(__file__)
primitive_data_folder = os.path.join(PWD, "primitives/data")
self._data = {
"primitive_data_folder": primitive_data_folder,
}

def get(self, key):
return copy.deepcopy(self._data[key])

def get_all(self):
return copy.deepcopy(self._data)

def set(self, values):
self._data.update(values)


config = Config()
6 changes: 3 additions & 3 deletions featuretools/primitives/base/primitive_base.py
Expand Up @@ -4,6 +4,8 @@

import numpy as np

from featuretools import config


class PrimitiveBase(object):
"""Base class for all primitives."""
Expand Down Expand Up @@ -39,6 +41,4 @@ def get_function(self):
raise NotImplementedError("Subclass must implement")

def get_filepath(self, filename):
PWD = os.path.dirname(__file__)
path = os.path.join(PWD, "../data", filename)
return path
return os.path.join(config.get("primitive_data_folder"), filename)
Empty file.
30 changes: 30 additions & 0 deletions featuretools/tests/config_tests/test_config.py
@@ -0,0 +1,30 @@
from featuretools import config


def test_get_default_config_does_not_change():
old_config = config.get_all()

key = "primitive_data_folder"
value = "This is an example string"
config.set({key: value})
config.set_to_default()

assert config.get(key) != value

config.set(old_config)


def test_set_and_get_config():

key = "primitive_data_folder"
old_value = config.get(key)
value = "This is an example string"

config.set({key: value})
assert config.get(key) == value

config.set({key: old_value})


def test_get_all():
assert config.get_all() == config._data
33 changes: 33 additions & 0 deletions featuretools/tests/primitive_tests/test_feature_base.py
@@ -1,9 +1,12 @@
import os.path

import pytest
from pympler.asizeof import asizeof

from ..testing_utils import make_ecommerce_entityset

import featuretools as ft
from featuretools import config
from featuretools.feature_base import IdentityFeature
from featuretools.primitives import Last, Mode, Sum
from featuretools.variable_types import Categorical, Datetime, Id, Numeric
Expand Down Expand Up @@ -109,3 +112,33 @@ def test_return_type_inference_id(es):
# also test direct feature of aggregation
mode_direct = ft.Feature(mode, es["sessions"])
assert mode_direct.variable_type == Categorical


def test_set_data_path(es):
key = "primitive_data_folder"

# Don't change orig_path
orig_path = config.get(key)
new_path = "/example/new/directory"
filename = "test.csv"

# Test that default path works
sum_prim = Sum()
assert sum_prim.get_filepath(filename) == os.path.join(orig_path, filename)

# Test that new path works
config.set({key: new_path})
assert sum_prim.get_filepath(filename) == os.path.join(new_path, filename)

# Test that new path with trailing / works
new_path += "/"
config.set({key: new_path})
assert sum_prim.get_filepath(filename) == os.path.join(new_path, filename)

# Test that the path is correct on newly defined feature
sum_prim2 = Sum()
assert sum_prim2.get_filepath(filename) == os.path.join(new_path, filename)

# Ensure path was reset
config.set({key: orig_path})
assert config.get(key) == orig_path
2 changes: 1 addition & 1 deletion featuretools/tests/utils_tests/test_config.py
@@ -1,7 +1,7 @@
import logging
import os

from featuretools.config import initialize_logging
from featuretools.config_init import initialize_logging

logging_env_vars = {'FEATURETOOLS_LOG_LEVEL': "debug",
'FEATURETOOLS_ES_LOG_LEVEL': "critical",
Expand Down

0 comments on commit de2ff0e

Please sign in to comment.