tests
This commit is contained in:
Родитель
5439f1f54a
Коммит
1196f1568e
|
@ -0,0 +1,7 @@
|
|||
[report]
|
||||
|
||||
omit = toasty/*tests/*
|
||||
exclude_lines =
|
||||
pragma: no cover
|
||||
if __name__ == .__main__.:
|
||||
raise NotImplementedError
|
|
@ -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
|
||||
|
||||
|
||||
|
@ -86,7 +115,7 @@ warpers = dict(linear=linear_warp,
|
|||
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)
|
||||
|
|
|
@ -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])
|
Загрузка…
Ссылка в новой задаче