This repository has been archived by the owner on Jun 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Write tests for new adapter syntax * Refactor adapter * Improve handling of caches and logs in tests * Fix minor issues mentioned in PR comments * Rewrite tests in pytest framework * Move adapting to seperate class, alter behaviour * Correction: mutable object as default argument in Step initializer
- Loading branch information
Showing
8 changed files
with
355 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,16 @@ | ||
# IDE files | ||
.idea/ | ||
.ipynb_checkpoints/ | ||
|
||
# Generated files | ||
*.pyc | ||
*.log | ||
.pytest_cache | ||
|
||
# Working directories | ||
examples/cache/ | ||
tests/.cache | ||
|
||
# Unwanted notebook files | ||
Untitled*.ipynb | ||
.ipynb_checkpoints/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from typing import Dict, Union, Tuple, List, Dict, Any, NamedTuple | ||
|
||
E = NamedTuple('E', [('input_name', str), ('key', str)]) | ||
|
||
AdaptingRecipe = Any | ||
Results = Dict[str, Any] | ||
AllInputs = Dict[str, Any] | ||
|
||
|
||
class AdapterError(Exception): | ||
pass | ||
|
||
|
||
class Adapter: | ||
def __init__(self, adapting_recipes: Dict[str, AdaptingRecipe]): | ||
self.adapting_recipes = adapting_recipes | ||
|
||
def adapt(self, all_inputs: AllInputs) -> Dict[str, Any]: | ||
adapted = {} | ||
for name, recipe in self.adapting_recipes.items(): | ||
adapted[name] = self._construct(all_inputs, recipe) | ||
return adapted | ||
|
||
def _construct(self, all_inputs: AllInputs, recipe: AdaptingRecipe) -> Any: | ||
return { | ||
E: self._construct_element, | ||
tuple: self._construct_tuple, | ||
list: self._construct_list, | ||
dict: self._construct_dict, | ||
}.get(recipe.__class__, self._construct_constant)(all_inputs, recipe) | ||
|
||
def _construct_constant(self, _: AllInputs, constant) -> Any: | ||
return constant | ||
|
||
def _construct_element(self, all_inputs: AllInputs, element: E): | ||
input_name = element.input_name | ||
key = element.key | ||
try: | ||
input_results = all_inputs[input_name] | ||
try: | ||
return input_results[key] | ||
except KeyError: | ||
msg = "Input '{}' didn't have '{}' in its result.".format(input_name, key) | ||
raise AdapterError(msg) | ||
except KeyError: | ||
msg = "No such input: '{}'".format(input_name) | ||
raise AdapterError(msg) | ||
|
||
def _construct_list(self, all_inputs: AllInputs, lst: List[AdaptingRecipe]): | ||
return [self._construct(all_inputs, recipe) for recipe in lst] | ||
|
||
def _construct_tuple(self, all_inputs: AllInputs, tup: Tuple): | ||
return tuple(self._construct(all_inputs, recipe) for recipe in tup) | ||
|
||
def _construct_dict(self, all_inputs: AllInputs, dic: Dict[AdaptingRecipe, AdaptingRecipe]): | ||
return {self._construct(all_inputs, k): self._construct(all_inputs, v) | ||
for k, v in dic.items()} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import steps.base # To make sure logger is initialized before running prepare_steps_logger | ||
|
||
from .steps_test_utils import prepare_steps_logger, remove_cache | ||
|
||
|
||
def pytest_sessionstart(session): | ||
prepare_steps_logger() | ||
|
||
|
||
def pytest_runtest_setup(item): | ||
remove_cache() | ||
|
||
|
||
def pytest_runtest_teardown(item): | ||
remove_cache() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import logging | ||
import os | ||
import shutil | ||
from pathlib import Path | ||
|
||
|
||
CACHE_DIRPATH = '.cache' | ||
LOGS_PATH = 'steps.log' | ||
|
||
|
||
def remove_cache(): | ||
if Path(CACHE_DIRPATH).exists(): | ||
shutil.rmtree(CACHE_DIRPATH) | ||
|
||
|
||
def remove_logs(): | ||
if Path(LOGS_PATH).exists(): | ||
os.remove(LOGS_PATH) | ||
|
||
|
||
def prepare_steps_logger(): | ||
print("Redirecting logging to {}.".format(LOGS_PATH)) | ||
remove_logs() | ||
logger = logging.getLogger('steps') | ||
for h in logger.handlers: | ||
logger.removeHandler(h) | ||
message_format = logging.Formatter(fmt='%(asctime)s %(name)s >>> %(message)s', | ||
datefmt='%Y-%m-%d %H:%M:%S') | ||
fh = logging.FileHandler(LOGS_PATH) | ||
fh.setLevel(logging.INFO) | ||
fh.setFormatter(fmt=message_format) | ||
logger.addHandler(fh) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import pytest | ||
import numpy as np | ||
|
||
from steps.adapter import Adapter, E | ||
|
||
|
||
@pytest.fixture | ||
def data(): | ||
return { | ||
'input_1': { | ||
'features': np.array([ | ||
[1, 6], | ||
[2, 5], | ||
[3, 4] | ||
]), | ||
'labels': np.array([2, 5, 3]) | ||
}, | ||
'input_2': { | ||
'extra_features': np.array([ | ||
[5, 7, 3], | ||
[67, 4, 5], | ||
[6, 13, 14] | ||
]) | ||
}, | ||
'input_3': { | ||
'images': np.array([ | ||
[[0, 255], [255, 0]], | ||
[[255, 0], [0, 255]], | ||
[[255, 255], [0, 0]], | ||
]), | ||
'labels': np.array([1, 1, 0]) | ||
} | ||
} | ||
|
||
|
||
def test_adapter_creates_defined_keys(data): | ||
adapter = Adapter({ | ||
'X': [E('input_1', 'features')], | ||
'Y': [E('input_2', 'extra_features')] | ||
}) | ||
res = adapter.adapt(data) | ||
assert {'X', 'Y'} == set(res.keys()) | ||
|
||
|
||
def test_recipe_with_single_item(data): | ||
adapter = Adapter({ | ||
'X': E('input_1', 'labels'), | ||
'Y': E('input_3', 'labels'), | ||
}) | ||
res = adapter.adapt(data) | ||
assert np.array_equal(res['X'], data['input_1']['labels']) | ||
assert np.array_equal(res['Y'], data['input_3']['labels']) | ||
|
||
|
||
def test_recipe_with_list(data): | ||
adapter = Adapter({ | ||
'X': [], | ||
'Y': [E('input_1', 'features')], | ||
'Z': [E('input_1', 'features'), E('input_2', 'extra_features')] | ||
}) | ||
res = adapter.adapt(data) | ||
|
||
for i, key in enumerate(('X', 'Y', 'Z')): | ||
assert isinstance(res[key], list) | ||
assert len(res[key]) == i | ||
|
||
assert res['X'] == [] | ||
|
||
assert np.array_equal(res['Y'][0], data['input_1']['features']) | ||
|
||
assert np.array_equal(res['Z'][0], data['input_1']['features']) | ||
assert np.array_equal(res['Z'][1], data['input_2']['extra_features']) | ||
|
||
|
||
def test_recipe_with_tuple(data): | ||
adapter = Adapter({ | ||
'X': (), | ||
'Y': (E('input_1', 'features'),), | ||
'Z': (E('input_1', 'features'), E('input_2', 'extra_features')) | ||
}) | ||
res = adapter.adapt(data) | ||
|
||
for i, key in enumerate(('X', 'Y', 'Z')): | ||
assert isinstance(res[key], tuple) | ||
assert len(res[key]) == i | ||
|
||
assert res['X'] == () | ||
|
||
assert np.array_equal(res['Y'][0], data['input_1']['features']) | ||
|
||
assert np.array_equal(res['Z'][0], data['input_1']['features']) | ||
assert np.array_equal(res['Z'][1], data['input_2']['extra_features']) | ||
|
||
|
||
def test_recipe_with_dictionary(data): | ||
adapter = Adapter({ | ||
'X': {}, | ||
'Y': {'a': E('input_1', 'features')}, | ||
'Z': {'a': E('input_1', 'features'), 'b': E('input_2', 'extra_features')} | ||
}) | ||
res = adapter.adapt(data) | ||
|
||
for i, key in enumerate(('X', 'Y', 'Z')): | ||
assert isinstance(res[key], dict) | ||
assert len(res[key]) == i | ||
|
||
assert res['X'] == {} | ||
|
||
assert np.array_equal(res['Y']['a'], data['input_1']['features']) | ||
|
||
assert np.array_equal(res['Z']['a'], data['input_1']['features']) | ||
assert np.array_equal(res['Z']['b'], data['input_2']['extra_features']) | ||
|
||
|
||
def test_recipe_with_constants(data): | ||
adapter = Adapter({ | ||
'A': 112358, | ||
'B': 3.14, | ||
'C': "lorem ipsum", | ||
'D': ('input_1', 'features'), | ||
'E': {112358: 112358, 'a': 'a', 3.14: 3.14}, | ||
'F': [112358, 3.14, "lorem ipsum", ('input_1', 'features')] | ||
}) | ||
res = adapter.adapt(data) | ||
|
||
assert res['A'] == 112358 | ||
assert res['B'] == 3.14 | ||
assert res['C'] == "lorem ipsum" | ||
assert res['D'] == ('input_1', 'features') | ||
assert res['E'] == {112358: 112358, 'a': 'a', 3.14: 3.14} | ||
assert res['F'] == [112358, 3.14, "lorem ipsum", ('input_1', 'features')] | ||
|
||
|
||
def test_nested_recipes(data): | ||
adapter = Adapter({ | ||
'X': [{'a': [E('input_1', 'features')]}], | ||
'Y': {'a': [{'b': E('input_2', 'extra_features')}]} | ||
}) | ||
res = adapter.adapt(data) | ||
|
||
assert res['X'] == [{'a': [data['input_1']['features']]}] | ||
assert res['Y'] == {'a': [{'b': data['input_2']['extra_features']}]} |
Oops, something went wrong.