Skip to content

Commit

Permalink
Merge 419c4d3 into 985567b
Browse files Browse the repository at this point in the history
  • Loading branch information
JarnoRFB committed Jul 30, 2019
2 parents 985567b + 419c4d3 commit a90cd7c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 14 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ matrix:
- python: "3.7"
env: TOX_ENV=py37
- python: "3.6"
env: TOX_ENV=tensorflow-1
env: TOX_ENV=tensorflow-112
- python: "3.6"
env: TOX_ENV=tensorflow-113
- python: "3.6"
env: TOX_ENV=tensorflow-114
- python: "3.6"
env: TOX_ENV=tensorflow-2
- python: "3.7"
Expand Down
7 changes: 6 additions & 1 deletion sacred/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,12 @@ def apply_backspaces_and_linefeeds(text):

def module_exists(modname):
"""Checks if a module exists without actually importing it."""
return pkgutil.find_loader(modname) is not None
try:
return pkgutil.find_loader(modname) is not None
except ImportError:
# TODO: Temporary fix for tf 1.14.0.
# Should be removed once fixed in tf.
return True


def modules_exist(*modnames):
Expand Down
18 changes: 15 additions & 3 deletions tests/test_optional.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@
# coding=utf-8

import pytest
from sacred.optional import optional_import
from sacred.optional import optional_import, get_tensorflow, modules_exist


def test_optional_import():
has_pytest, pyt = optional_import('pytest')
has_pytest, pyt = optional_import("pytest")
assert has_pytest
assert pyt == pytest


def test_optional_import_nonexisting():
has_nonex, nonex = optional_import('clearlynonexistingpackage')
has_nonex, nonex = optional_import("clearlynonexistingpackage")
assert not has_nonex
assert nonex is None


def test_get_tensorflow():
"""Test that get_tensorflow() runs without error."""
pytest.importorskip("tensorflow")
get_tensorflow()


def test_module_exists_for_tensorflow():
"""Check that module_exist returns true if tf is there."""
pytest.importorskip("tensorflow")
assert modules_exist("tensorflow")
16 changes: 12 additions & 4 deletions tests/test_stflow/test_method_interception.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

@pytest.fixture
def ex():
return Experiment('tensorflow_tests')
return Experiment("tensorflow_tests")


@pytest.fixture()
Expand All @@ -18,6 +18,7 @@ def tf():
so `tensorflow` is not required during the tests.
"""
from sacred.optional import has_tensorflow

if has_tensorflow:
return opt.get_tensorflow()
else:
Expand All @@ -28,7 +29,10 @@ class FileWriter:
def __init__(self, logdir, graph):
self.logdir = logdir
self.graph = graph
print("Mocked FileWriter got logdir=%s, graph=%s" % (logdir, graph))
print(
"Mocked FileWriter got logdir=%s, graph=%s"
% (logdir, graph)
)

class Session:
def __init__(self):
Expand All @@ -42,6 +46,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):

# Set stflow to use the mock as the test
import sacred.stflow.method_interception

sacred.stflow.method_interception.tf = tensorflow
return tensorflow

Expand Down Expand Up @@ -90,7 +95,10 @@ def run_experiment(_run):
assert swr is not None
assert _run.info["tensorflow"]["logdirs"] == [TEST_LOG_DIR]
tf.summary.FileWriter(TEST_LOG_DIR2, s.graph)
assert _run.info["tensorflow"]["logdirs"] == [TEST_LOG_DIR, TEST_LOG_DIR2]
assert _run.info["tensorflow"]["logdirs"] == [
TEST_LOG_DIR,
TEST_LOG_DIR2,
]

# This should not be captured:
tf.summary.FileWriter("/tmp/whatever", s.graph)
Expand Down Expand Up @@ -133,7 +141,7 @@ def test_log_summary_writer_class(ex, tf):
TEST_LOG_DIR = "/dev/null"
TEST_LOG_DIR2 = "/tmp/sacred_test"

class FooClass():
class FooClass:
def __init__(self):
pass

Expand Down
28 changes: 23 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# and then run "tox" from this directory.

[tox]
envlist = py35, py36, py37, setup, flake8, tensorflow-1, tensorflow-2
envlist = py35, py36, py37, setup, flake8, tensorflow-112, tensorflow-113, tensorflow-114, tensorflow-2
# py32 does not work because of the 'wrapt' dependency
# py33 is not supported by newer versions of numpy
# py34 is not supported by newer versions of pandas
Expand All @@ -16,20 +16,38 @@ commands =
pytest \
{posargs} # substitute with tox' positional arguments

[testenv:tensorflow-1]
[testenv:tensorflow-112]
basepython = python
deps =
-rdev-requirements.txt
tensorflow==1.12.2
tensorflow==1.12.3
commands =
pytest tests/test_stflow \
pytest tests/test_stflow tests/test_optional.py \
{posargs}

[testenv:tensorflow-113]
basepython = python
deps =
-rdev-requirements.txt
tensorflow==1.13.2
commands =
pytest tests/test_stflow tests/test_optional.py \
{posargs}

[testenv:tensorflow-114]
basepython = python
deps =
-rdev-requirements.txt
tensorflow==1.14.0
commands =
pytest tests/test_stflow tests/test_optional.py \
{posargs}

[testenv:tensorflow-2]
basepython = python
deps =
-rdev-requirements.txt
tensorflow==2.0.0-alpha0
tensorflow==2.0.0-beta1
commands =
pytest tests/test_stflow \
{posargs}
Expand Down

0 comments on commit a90cd7c

Please sign in to comment.