Skip to content

Commit

Permalink
Merge branch 'grid'
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Hoffman committed Jan 5, 2015
2 parents 0b06d41 + 9c0e373 commit 853009d
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[run]
branch = True
omit =
mwhutils/random/_sobol.py
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ before_install:
- "pip install python-coveralls"

script:
- "nosetests --with-coverage --cover-inclusive --cover-package=mwhutils"
- "nosetests"

after_success:
- "coveralls"
Expand Down
21 changes: 20 additions & 1 deletion mwhutils/random/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import numpy as np

# exported symbols
__all__ = ['rstate', 'uniform', 'latin', 'sobol']
__all__ = ['rstate', 'uniform', 'latin', 'sobol', 'grid']


def rstate(rng=None):
Expand Down Expand Up @@ -86,3 +86,22 @@ def sobol(bounds, n, rng=None):
X = bounds[:, 0] + w * i4_sobol_generate(d, n, skip).T

return X


def grid(bounds, n):
"""
Generate a regular grid within the specified region, given by `bounds`,
a list of [(lo,hi), ..] bounds in each dimension. `n` represents the number
of points along each dimension.
"""
bounds = np.array(bounds, ndmin=2, copy=False)
d = len(bounds)

if d == 1:
X = np.linspace(bounds[0, 0], bounds[0, 1], n)
X = np.reshape(X, (-1, 1))
else:
X = np.meshgrid(*(np.linspace(a, b, n) for a, b in bounds))
X = np.reshape(X, (d, -1)).T

return X
4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[nosetests]
with-coverage=1
cover-inclusive=1
cover-package=mwhutils
38 changes: 38 additions & 0 deletions tests/test_abc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
ABC tests.
"""

from __future__ import division
from __future__ import absolute_import
from __future__ import print_function

from mwhutils.abc import ABCMeta, abstractmethod


def test_abc():
"""Test creation of an abstract base class."""

class FooBase(object):
"""Base class."""
__metaclass__ = ABCMeta

@abstractmethod
def method1(self):
"""Test."""
pass

def method2(self):
"""Test."""
pass

class Foo(FooBase):
"""Child class."""
def method1(self):
pass

def method2(self):
pass

instance = Foo()
assert instance.method1.__doc__ == "Test."
assert instance.method2.__doc__ is None
22 changes: 19 additions & 3 deletions tests/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import numpy.testing as nt

from mwhutils.random import rstate
from mwhutils.random import uniform, latin, sobol
from mwhutils.random import uniform, latin, sobol, grid


def test_rstate():
Expand All @@ -28,11 +28,27 @@ def check_random(method):
bounds = [(0, 1), (3, 4)]
sample = method(bounds, 10)
assert sample.shape == (10, 2)
assert all(sample[:, 0] > 0) and all(sample[:, 0] < 1)
assert all(sample[:, 1] > 3) and all(sample[:, 1] < 4)
assert all(sample[:, 0] >= 0) and all(sample[:, 0] <= 1)
assert all(sample[:, 1] >= 3) and all(sample[:, 1] <= 4)

sample = grid((0, 1), 10)
assert sample.shape == (10, 1)
assert all(sample[:, 0] >= 0) and all(sample[:, 0] <= 1)


def test_random():
"""Test all the random generators."""
for method in [uniform, latin, sobol]:
yield check_random, method


def test_grid():
"""Test the non-random grid "sampler"."""
sample = grid([(0, 1), (3, 4)], 10)
assert sample.shape == (100, 2)
assert all(sample[:, 0] >= 0) and all(sample[:, 0] <= 1)
assert all(sample[:, 1] >= 3) and all(sample[:, 1] <= 4)

sample = grid((0, 1), 10)
assert sample.shape == (10, 1)
assert all(sample[:, 0] >= 0) and all(sample[:, 0] <= 1)

0 comments on commit 853009d

Please sign in to comment.