Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Beaumont committed Dec 7, 2013
1 parent 5439f1f commit 1196f15
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[report]

omit = toasty/*tests/*
exclude_lines =
pragma: no cover
if __name__ == .__main__.:
raise NotImplementedError
31 changes: 30 additions & 1 deletion toasty/norm.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
# This file and its tests is adapted from Glue (http://glueviz.org).

# Copyright (c) 2013, Glue Developers
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice, this
# list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
# * Neither the name of the Glue project nor the names of its contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import numpy as np


Expand Down Expand Up @@ -86,7 +115,7 @@ def asinh_warp(x, vmin, vmax, bias, contrast):
squared=squared_warp,
arcsinh=asinh_warp)

def normalize(value, vmin, vmax, bias, contrast, stretch):
def normalize(value, vmin, vmax, bias=.5, contrast=1, stretch='linear'):
inverted = vmax <= vmin

hi, lo = max(vmin, vmax), min(vmin, vmax)
Expand Down
91 changes: 91 additions & 0 deletions toasty/tests/test_norm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import numpy as np
import pytest

from ..norm import *


def test_log_warp():
x = np.array([0, 1, 10, 100, 101])
y = log_warp(x, 1, 100, .5, 1)
yexp = np.array([0, 0, .654, 1, 1])
np.testing.assert_array_almost_equal(y, yexp, 3)


def test_sqrt_warp():
x = np.array([0, 1, 10, 100, 101])
y = sqrt_warp(x, 1, 100, .5, 1)
yexp = np.array([0, 0, .3015, 1, 1])
np.testing.assert_array_almost_equal(y, yexp, 3)


def test_pow_warp():
x = np.array([0, 1, 10, 100, 101])
y = pow_warp(x, 1, 100, .5, 1)
yexp = np.array([0, 0, .00087, 1, 1])
np.testing.assert_array_almost_equal(y, yexp, 3)


def test_squared_warp():
x = np.array([0, 1, 10, 100, 101])
y = squared_warp(x, 1, 100, .5, 1)
yexp = np.array([0, 0, .008264, 1, 1])
np.testing.assert_array_almost_equal(y, yexp, 3)


def test_asinh_warp():
x = np.array([0, 1, 10, 100, 101])
y = asinh_warp(x, 1, 100, .5, 1)
yexp = np.array([0, 0, .27187, 1, 1])
np.testing.assert_array_almost_equal(y, yexp, 3)


def test_linear_warp():
x = np.array([0, 1, 10, 100, 101])
y = linear_warp(x, 1, 100, .5, 1)
yexp = np.array([0, 0, 9. / 99., 1, 1])
np.testing.assert_array_almost_equal(y, yexp, 3)


def test_bias():
x = np.array([0, .4, .5, .6, 1])

y = cscale(x.copy(), .5, 1)
np.testing.assert_array_almost_equal(x, y)

y = cscale(x.copy(), .5, 2)
yexp = np.array([0, .3, .5, .7, 1])
np.testing.assert_array_almost_equal(y, yexp)

y = cscale(x.copy(), .5, 0)
yexp = np.array([.5, .5, .5, .5, .5])
np.testing.assert_array_almost_equal(y, yexp)

y = cscale(x.copy(), .5, 0)
yexp = np.array([.5, .5, .5, .5, .5])
np.testing.assert_array_almost_equal(y, yexp)

y = cscale(x.copy(), .4, 1)
yexp = np.array([.1, .5, .6, .7, 1])
np.testing.assert_array_almost_equal(y, yexp)

y = cscale(x.copy(), .6, 1)
yexp = np.array([0, .3, .4, .5, .9])
np.testing.assert_array_almost_equal(y, yexp)


class TestNormalize(object):

def test_input_unmodified(self):
x = np.array([1, 2, 3])
y = normalize(x, 1, 3, contrast=100)
assert np.abs(x - y).max() > .1 #they are different
np.testing.assert_array_almost_equal(x, [1, 2, 3]) # x is not

def test_call_default(self):
x = np.array([1, 2, 3])
np.testing.assert_array_almost_equal(normalize(x, 1, 3), [0, 127, 255])

def test_call_invert(self):
x = np.array([1, 2, 3])
y = normalize(x, vmin=3, vmax=1)
np.testing.assert_array_almost_equal(y, [255, 127, 0])

0 comments on commit 1196f15

Please sign in to comment.