Skip to content

Commit

Permalink
Restructure the code folders.
Browse files Browse the repository at this point in the history
  • Loading branch information
awav committed Nov 8, 2017
1 parent 1c82a23 commit 722d09a
Show file tree
Hide file tree
Showing 29 changed files with 178 additions and 65 deletions.
114 changes: 114 additions & 0 deletions gpflow/core/compilable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Copyright 2017 Artem Artemev @awav
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import abc
import enum
import inspect


# TODO(@awav): Introducing global variable is not best idea for managing compilation, but
# necessary for context manager support.
# Inspect approach works well, except that I have a concern that it can be slow when
# nesting is too deep and it doesn't work well with context managers, because the code which
# is run inside `with` statement doesn't have an access to the frame which caused it.

class AutoBuildStatus(enum.Enum):
"""
This enum class is used for safe marking current status for global and local
autobuild settings. It must never be used outside of the project.
"""
__autobuild_enabled_global__ = True

BUILD = 1
IGNORE = 2
FOLLOW = 3


class AutoBuild(abc.ABCMeta):
"""
AutoBuild meta class is used for change behavior of all its `compilable` decendants.
Whenever an object defined with this metaclass is created - the build and initialization
methods of compilable interface are called immediately after object's __init__ function.
"""
_autobuild_arg = 'autobuild'

def __new__(mcs, name, bases, namespace, **kwargs):
new_cls = super(AutoBuild, mcs).__new__(mcs, name, bases, namespace, **kwargs)
origin_init = new_cls.__init__
def __init__(self, *args, **kwargs):
autobuild = kwargs.pop(AutoBuild._autobuild_arg, True)
__execute_autobuild__ = AutoBuildStatus.BUILD if autobuild else AutoBuildStatus.IGNORE
tag = '__execute_autobuild__'
frame = inspect.currentframe().f_back
while autobuild and frame:
if isinstance(frame.f_locals.get(tag, None), AutoBuildStatus):
__execute_autobuild__ = AutoBuildStatus.FOLLOW
break
frame = frame.f_back
origin_init(self, *args, **kwargs)
autobuild_on = __execute_autobuild__ == AutoBuildStatus.BUILD
global_autobuild_on = AutoBuildStatus.__autobuild_enabled_global__
if autobuild_on and global_autobuild_on:
self.build()
self.initialize(force=True)
setattr(new_cls, '__init__', __init__)
return new_cls


class Build(enum.Enum):
YES = 1
NO = 0 # pylint: disable=C0103
NOT_COMPATIBLE_GRAPH = None


class ICompilable(metaclass=AutoBuild):
@abc.abstractproperty
def graph(self):
raise NotImplementedError()

@abc.abstractproperty
def feeds(self):
raise NotImplementedError()

@abc.abstractproperty
def initializables(self):
raise NotImplementedError()

@abc.abstractproperty
def initializable_feeds(self):
raise NotImplementedError()

@abc.abstractmethod
def build(self):
raise NotImplementedError()

@abc.abstractmethod
def initialize(self, session=None, force=False):
raise NotImplementedError()

@abc.abstractmethod
def compile(self, session=None):
raise NotImplementedError()

@abc.abstractmethod
def is_built(self, graph):
raise NotImplementedError()

@abc.abstractmethod
def clear(self):
raise NotImplementedError()

@abc.abstractmethod
def _build(self):
raise NotImplementedError()
16 changes: 16 additions & 0 deletions gpflow/core/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright 2017 Artem Artemev @awav
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

class GPflowError(Exception):
pass
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
dependencies.append('tensorflow>={0}'.format(min_tf_version))

packages = find_packages('.')
packages.remove('testing')

setup(name='gpflow',
version=verstr,
Expand All @@ -54,7 +53,7 @@
install_requires=dependencies,
package_data={'gpflow': ['gpflow/gpflowrc']},
include_package_data=True,
test_suite='testing',
test_suite='tests',
extras_require={'tensorflow with gpu': ['tensorflow-gpu>=1.0.0'],
'Export parameters as pandas dataframes': ['pandas>=0.18.1']},
classifiers=['License :: OSI Approved :: Apache Software License',
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 4 additions & 1 deletion testing/test_autoflow.py → tests/test_autoflow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import unittest
import tensorflow as tf

import numpy as np
Expand Down Expand Up @@ -195,3 +194,7 @@ def test(self):
likelihood=gpflow.likelihoods.Gaussian(), Z=Z)
model.compile()
model.compute_log_likelihood()


if __name__ == '__main__':
tf.test.main()
9 changes: 3 additions & 6 deletions testing/test_conditionals.py → tests/test_conditionals.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from __future__ import print_function
import unittest

import tensorflow as tf

import numpy as np
Expand Down Expand Up @@ -103,7 +100,7 @@ def test_whiten(self):
with self.test_context() as sess:
Xs, X, F, k, num_data, feed_dict = self.prepare()
k.compile(session=sess)

K = k.K(X) + tf.eye(num_data, dtype=settings.np_float) * 1e-6
L = tf.cholesky(K)
V = tf.matrix_triangular_solve(L, F, lower=True)
Expand Down Expand Up @@ -151,5 +148,5 @@ def test_whiten(self):
assert_allclose(var_difference, 0, atol=4)


if __name__ == "__main__":
unittest.main()
if __name__ == '__main__':
tf.test.main()
5 changes: 2 additions & 3 deletions testing/test_config.py → tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

# pylint: disable=W0212

import unittest
import os
import tensorflow as tf
import gpflow
Expand Down Expand Up @@ -78,5 +77,5 @@ def testContextManager(self):
gpflow.settings.verbosity.hmc_verb = orig


if __name__ == "__main__":
unittest.main()
if __name__ == '__main__':
tf.test.main()
5 changes: 1 addition & 4 deletions testing/test_coregion.py → tests/test_coregion.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from __future__ import print_function
import unittest

import tensorflow as tf
import numpy as np
from numpy.testing import assert_allclose
Expand Down Expand Up @@ -162,4 +159,4 @@ def test_predicts(self):


if __name__ == '__main__':
unittest.main()
tf.test.main()
5 changes: 1 addition & 4 deletions testing/test_data_object.py → tests/test_data_object.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from __future__ import print_function
import unittest
import tensorflow as tf


import numpy as np
from numpy.testing import assert_array_equal

Expand Down Expand Up @@ -160,4 +157,4 @@ def test_vgp(self):


if __name__ == '__main__':
unittest.main()
tf.test.main()
5 changes: 4 additions & 1 deletion testing/test_ekerns.py → tests/test_ekerns.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import unittest
import numpy as np
import tensorflow as tf
import gpflow
Expand Down Expand Up @@ -592,3 +591,7 @@ def test_cross_quad(self):
tfXcov: self.Xcov}
a, b = session.run((tfa, tfb), feed_dict=feed_dict)
_assert_pdeq(self, a, b)


if __name__ == "__main__":
tf.test.main()
5 changes: 2 additions & 3 deletions testing/test_gplvm.py → tests/test_gplvm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from __future__ import print_function

import tensorflow as tf
import numpy as np

import gpflow
Expand Down Expand Up @@ -164,4 +163,4 @@ def test_kernelsActiveDims(self):


if __name__ == "__main__":
unittest.main()
tf.test.main()
13 changes: 7 additions & 6 deletions testing/test_hmc.py → tests/test_hmc.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import gpflow
import unittest
import tensorflow as tf

from nose.plugins.attrib import attr

import numpy as np
from numpy.testing import assert_almost_equal, assert_allclose
from numpy.testing import assert_almost_equal

from nose.plugins.attrib import attr
import gpflow
from gpflow.test_util import GPflowTestCase


@attr(speed='slow')
class SampleGaussianTest(GPflowTestCase):
class Gauss(gpflow.models.Model):
Expand Down Expand Up @@ -174,5 +175,5 @@ def check_last_variables_state(self, m, samples):
assert_almost_equal(last[col], params[col].read_value())


if __name__ == "__main__":
unittest.main()
if __name__ == '__main__':
tf.test.main()
5 changes: 1 addition & 4 deletions testing/test_kerns.py → tests/test_kerns.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from __future__ import absolute_import, print_function

import unittest
import tensorflow as tf

import numpy as np
Expand Down Expand Up @@ -568,4 +565,4 @@ def test_MLP(self):


if __name__ == "__main__":
unittest.main()
tf.test.main()
3 changes: 1 addition & 2 deletions testing/test_likelihoods.py → tests/test_likelihoods.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import unittest
import six
import tensorflow as tf

Expand Down Expand Up @@ -393,4 +392,4 @@ def test_likelihood_checks(self):


if __name__ == "__main__":
unittest.main()
tf.test.main()
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import itertools
import tensorflow as tf
import unittest

import numpy as np
from numpy.testing import assert_allclose
Expand Down Expand Up @@ -381,4 +380,4 @@ def test(self):


if __name__ == "__main__":
unittest.main()
tf.test.main()
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.from __future__ import print_function

import unittest
import tensorflow as tf

from nose.plugins.attrib import attr
Expand Down Expand Up @@ -258,4 +257,4 @@ def test_few_inducing_points(self):


if __name__ == '__main__':
unittest.main()
tf.test.main()
3 changes: 1 addition & 2 deletions testing/test_methods.py → tests/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.from __future__ import print_function

import unittest
import tensorflow as tf

import numpy as np
Expand Down Expand Up @@ -304,4 +303,4 @@ def test_likelihoods_and_gradients(self):


if __name__ == "__main__":
unittest.main()
tf.test.main()
5 changes: 1 addition & 4 deletions testing/test_model.py → tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.from __future__ import print_function

from __future__ import print_function

import unittest
import tensorflow as tf

import numpy as np
Expand Down Expand Up @@ -276,4 +273,4 @@ def test_name(self):


if __name__ == "__main__":
unittest.main()
tf.test.main()
3 changes: 0 additions & 3 deletions testing/test_notebooks.py → tests/test_notebooks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
from __future__ import print_function

import traceback
import unittest
import sys
import time
import os
Expand Down
7 changes: 4 additions & 3 deletions testing/test_param.py → tests/test_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

# pylint: disable=E1123

import unittest

import tensorflow as tf
import numpy as np

Expand Down Expand Up @@ -241,7 +239,7 @@ def test_add_parameter_to_empty_parameterized(self):
self.m.b = gpflow.Param(20)


class ParameterizedCompileTests(GPflowTestCase):
class TestParameterizedCompile(GPflowTestCase):
def setUp(self):
self.test_graph = tf.Graph()
with self.test_context() as session:
Expand Down Expand Up @@ -684,3 +682,6 @@ def _check_trainable_flag(m, assert_true, assert_false):
if param.trainable:
assert_bool = assert_true
assert_bool(gpflow.misc.is_tensor_trainable(param.parameter_tensor))

if __name__ == '__main__':
tf.test.main()

0 comments on commit 722d09a

Please sign in to comment.