PythonLibrary Python NumPy 2019年 1月 4日
with such.A('MonoImage class') as it:
(中略)
def compare_two_mono_image_list(array0, array1):
for (x, y) in zip(array0, array1):
it.assertEqual(x, y)
it.assertEqual(x, y)
AssertionError: <hkob_pyimage.mono_image.MonoImage object at 0x11586cc88> != <hkob_pyimage.mono_image.MonoImage object at 0x10a539198>
class MonoImage:
(中略)
def __repr__(self):
return "shape = {0}, bits = {1}, signed = {2}, array = {3}".format(self.shape, self.bits, self.signed, self._array)
これから記述するテストでは MonoImage オブジェクトを用意して比較することが多くなる. 毎回,NumPy オブジェクトから作成すると面倒なので,Python の配列から MonoImage を作るコンストラクタラッパーを用意しておきたい. ここでは,コンストラクタラッパーのテスト及び MonoImage の比較テストを実施する.
with such.A('MonoImage class') as it:
(中略)
@it.should('have a constracter wrapper method and a generator constracter method from python array.')
def test():
ui8a = [[10, 20, 30], [40, 50, 60]]
i16a = [[1, -2], [-3, 4], [5, -6]]
da = [3.1, 1.4, 1.5, 9.2, 6.5, 3.5]
compare_two_mono_image_list([MonoImage.create_with_array(*x) for x in ((ui8a, np.uint8), (i16a, np.int16, 16, True), (da, np.double, 0))], it.marray)
class MonoImage:
(中略)
@classmethod
def create_with_array(self, array, dtype, bits=None, signed=False):
nparray = np.array(array, dtype)
if bits is None:
bits = nparray.itemsize * 8
return MonoImage(nparray, bits, signed)
change:mono_image.py
test 0000: should have correct getter properties. (test_mono_image.A MonoImage class) ... ok
test 0001: should have a constracter wrapper method from python array (test_mono_image.A MonoImage class) ... FAIL
======================================================================
FAIL: test 0001: should have a constracter wrapper method from python array (test_mono_image.A MonoImage class)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/hkob/python3/hkob_pyimage/tests/test_mono_image.py", line 40, in test
compare_two_mono_image_list([MonoImage.create_with_array(*x) for x in ((ui8a, np.uint8), (i16a, np.int16, 16, True), (da, np.double, 0))], it.marray)
File "/Users/hkob/python3/hkob_pyimage/tests/test_mono_image.py", line 20, in compare_two_mono_image_list
it.assertEqual(x, y)
AssertionError: shape = (2, 3), bits = 8, signed = False, array = [[10 20 30] [40 50 60]] != shape = (2, 3), bits = 8, signed = False, array = [[10 20 30] [40 50 60]]
----------------------------------------------------------------------
Ran 2 tests in 0.001s
FAILED (failures=1)
class MonoImage:
(中略)
# methods
def __eq__(self, other):
return self._bits == other._bits and self._signed == other._signed and (self._array == other._array).all()
change:tests/test_mono_image.py
test 0000: should have correct getter properties. (test_mono_image.A MonoImage class) ... ok
test 0001: should have a constracter wrapper method from python array (test_mono_image.A MonoImage class) ... ok
----------------------------------------------------------------------
Ran 2 tests in 0.002s
OK
compare_two_mono_image_list([mi.generate_with_array(a) for (mi, a) in zip(it.marray, (ui8a, i16a, da))], it.marray)
class MonoImage:
(中略)
def generate_with_array(self, array):
return MonoImage.create_with_array(array, self.dtype, self.bits, self.signed)
長くなったので今日はここまで.