|
| 1 | +""" |
| 2 | +Tests for the utility functions wrapping PIL functions |
| 3 | +
|
| 4 | +This is a local version of the tests for utility functions from scipy that |
| 5 | +are wrapping PIL functionality. These functions are deprecated in scipy 1.0.0 |
| 6 | +and will be removed in scipy 1.2.0. |
| 7 | +
|
| 8 | +Copyright (c) 2001, 2002 Enthought, Inc. |
| 9 | +All rights reserved. |
| 10 | +
|
| 11 | +Copyright (c) 2003-2017 SciPy Developers. |
| 12 | +All rights reserved. |
| 13 | +
|
| 14 | +Redistribution and use in source and binary forms, with or without |
| 15 | +modification, are permitted provided that the following conditions are met: |
| 16 | +
|
| 17 | <
8000
td data-grid-cell-id="diff-92b0946ba1a27c0ca53422fe871a860b6b296917c194a680e56485517eb9cd11-empty-17-2" data-line-anchor="diff-92b0946ba1a27c0ca53422fe871a860b6b296917c194a680e56485517eb9cd11R17" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionLine-bgColor, var(--diffBlob-addition-bgColor-line));padding-right:24px" tabindex="-1" valign="top" class="focusable-grid-cell diff-text-cell right-side-diff-cell left-side">+ a. Redistributions of source code must retain the above copyright notice,
| 18 | + this list of conditions and the following disclaimer. |
| 19 | + b. Redistributions in binary form must reproduce the above copyright |
| 20 | + notice, this list of conditions and the following disclaimer in the |
| 21 | + documentation and/or other materials provided with the distribution. |
| 22 | + c. Neither the name of Enthought nor the names of the SciPy Developers |
| 23 | + may be used to endorse or promote products derived from this software |
| 24 | + without specific prior written permission. |
| 25 | +
|
| 26 | +
|
| 27 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 28 | +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 29 | +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 30 | +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS |
| 31 | +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, |
| 32 | +OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 33 | +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 34 | +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 35 | +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 36 | +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 37 | +THE POSSIBILITY OF SUCH DAMAGE. |
| 38 | +""" |
| 39 | + |
| 40 | +from __future__ import division, print_function, absolute_import |
| 41 | + |
| 42 | +import os.path |
| 43 | +import numpy as np |
| 44 | + |
| 45 | +import pytest |
| 46 | +from numpy.testing import (assert_equal, assert_array_equal, assert_) |
| 47 | +from numpy.ma.testutils import assert_mask_equal |
| 48 | + |
| 49 | +from sklearn.utils._pilutil import (_bytescale, _imread, _imresize, |
| 50 | + _fromimage) |
| 51 | + |
| 52 | +_have_image = True |
| 53 | +try: |
| 54 | + try: |
| 55 | + from PIL import Image |
| 56 | + except ImportError: |
| 57 | + import Image |
| 58 | +except ImportError: |
| 59 | + _have_image = False |
| 60 | + |
| 61 | +# Function / method decorator for skipping PIL tests on import failure |
| 62 | +_pilskip = pytest.mark.skipif( |
| 63 | + not _have_image, reason='Need to import PIL for this test') |
| 64 | + |
| 65 | +datapath = os.path.dirname(__file__) |
| 66 | + |
| 67 | + |
| 68 | +class TestPILUtil(object): |
| 69 | + def test_imresize(self): |
| 70 | + im = np.random.random((10, 20)) |
| 71 | + for T in np.sctypes['float'] + [float]: |
| 72 | + # 1.1 rounds to below 1.1 for float16, 1.101 works |
| 73 | + im1 = _imresize(im, T(1.101)) |
| 74 | + assert_equal(im1.shape, (11, 22)) |
| 75 | + |
| 76 | + @_pilskip |
| 77 | + def test_bytescale(self): |
| 78 | + x = np.array([0, 1, 2], np.uint8) |
| 79 | + y = np.array([0, 1, 2]) |
| 80 | + assert_equal(_bytescale(x), x) |
| 81 | + assert_equal(_bytescale(y), [0, 128, 255]) |
| 82 | + |
| 83 | + @_pilskip |
| 84 | + def test_bytescale_mask(self): |
| 85 | + a = np.ma.MaskedArray(data=[1, 2, 3], mask=[False, False, True]) |
| 86 | + actual = _bytescale(a) |
| 87 | + expected = [0, 255, 3] |
| 88 | + assert_equal(expected, actual) |
| 89 | + assert_mask_equal(a.mask, actual.mask) |
| 90 | + assert_(isinstance(actual, np.ma.MaskedArray)) |
| 91 | + |
| 92 | + |
| 93 | +def check_fromimage(filename, irange, shape): |
| 94 | + fp = open(filename, "rb") |
| 95 | + img = _fromimage(Image.open(fp)) |
| 96 | + fp.close() |
| 97 | + imin, imax = irange |
| 98 | + assert_equal(img.min(), imin) |
| 99 | + assert_equal(img.max(), imax) |
| 100 | + assert_equal(img.shape, shape) |
| 101 | + |
| 102 | + |
| 103 | +@_pilskip |
| 104 | +def test_fromimage(): |
| 105 | + # Test generator for parametric tests |
| 106 | + # Tuples in the list are (filename, (datamin, datamax), shape). |
| 107 | + files = [('icon.png', (0, 255), (48, 48, 4)), |
| 108 | + ('icon_mono.png', (0, 255), (48, 48, 4)), |
| 109 | + ('icon_mono_flat.png', (0, 255), (48, 48, 3))] |
| 110 | + for fn, irange, shape in files: |
| 111 | + check_fromimage(os.path.join(datapath, 'data', fn), irange, shape) |
| 112 | + |
| 113 | + |
| 114 | +@_pilskip |
| 115 | +def test_imread_indexed_png(): |
| 116 | + # The file `foo3x5x4indexed.png` was created with this array |
| 117 | + # (3x5 is (height)x(width)): |
| 118 | + data = np.array([[[127, 0, 255, 255], |
| 119 | + [127, 0, 255, 255], |
| 120 | + [127, 0, 255, 255], |
| 121 | + [127, 0, 255, 255], |
| 122 | + [127, 0, 255, 255]], |
| 123 | + [[192, 192, 255, 0], |
| 124 | + [192, 192, 255, 0], |
| 125 | + [0, 0, 255, 0], |
| 126 | + [0, 0, 255, 0], |
| 127 | + [0, 0, 255, 0]], |
| 128 | + [[0, 31, 255, 255], |
| 129 | + [0, 31, 255, 255], |
| 130 | + [0, 31, 255, 255], |
| 131 | + [0, 31, 255, 255], |
| 132 | + [0, 31, 255, 255]]], dtype=np.uint8) |
| 133 | + |
| 134 | + filename = os.path.join(datapath, 'data', 'foo3x5x4indexed.png') |
| 135 | + with open(filename, 'rb') as f: |
| 136 | + im = _imread(f) |
| 137 | + assert_array_equal(im, data) |
| 138 | + |
| 139 | + |
| 140 | +@_pilskip |
| 141 | +def test_imread_1bit(): |
| 142 | + # box1.png is a 48x48 grayscale image with bit depth 1. |
| 143 | + # The border pixels are 1 and the rest are 0
F987
. |
| 144 | + filename = os.path.join(datapath, 'data', 'box1.png') |
| 145 | + with open(filename, 'rb') as f: |
| 146 | + im = _imread(f) |
| 147 | + assert_equal(im.dtype, np.uint8) |
| 148 | + expected = np.zeros((48, 48), dtype=np.uint8) |
| 149 | + # When scaled up from 1 bit to 8 bits, 1 becomes 255. |
| 150 | + expected[:, 0] = 255 |
| 151 | + expected[:, -1] = 255 |
| 152 | + expected[0, :] = 255 |
| 153 | + expected[-1, :] = 255 |
| 154 | + assert_equal(im, expected) |
| 155 | + |
| 156 | + |
| 157 | +@_pilskip |
| 158 | +def test_imread_2bit(): |
| 159 | + # blocks2bit.png is a 12x12 grayscale image with bit depth 2. |
| 160 | + # The pattern is 4 square subblocks of size 6x6. Upper left |
| 161 | + # is all 0, upper right is all 1, lower left is all 2, lower |
| 162 | + # right is all 3. |
| 163 | + # When scaled up to 8 bits, the values become [0, 85, 170, 255]. |
| 164 | + filename = os.path.join(datapath, 'data', 'blocks2bit.png') |
| 165 | + with open(filename, 'rb') as f: |
| 166 | + im = _imread(f) |
| 167 | + assert_equal(im.dtype, np.uint8) |
| 168 | + expected = np.zeros((12, 12), dtype=np.uint8) |
| 169 | + expected[:6, 6:] = 85 |
| 170 | + expected[6:, :6] = 170 |
| 171 | + expected[6:, 6:] = 255 |
| 172 | + assert_equal(im, expected) |
| 173 | + |
| 174 | + |
| 175 | +@_pilskip |
| 176 | +def test_imread_4bit(): |
| 177 | + # pattern4bit.png is a 12(h) x 31(w) grayscale image with bit depth 4. |
| 178 | + # The value in row j and column i is maximum(j, i) % 16. |
| 179 | + # When scaled up to 8 bits, the values become [0, 17, 34, ..., 255]. |
| 180 | + filename = os.path.join(datapath, 'data', 'pattern4bit.png') |
| 181 | + with open(filename, 'rb') as f: |
| 182 | + im = _imread(f) |
| 183 | + assert_equal(im.dtype, np.uint8) |
| 184 | + j, i = np.meshgrid(np.arange(12), np.arange(31), indexing='ij') |
| 185 | + expected = 17 * (np.maximum(j, i) % 16).astype(np.uint8) |
| 186 | + assert_equal(im, expected) |
0 commit comments