PythonLibrary Python NumPy 2019年 1月 10日
ベクトル版の Pixel iterator が作成できたので,これを行列に拡張する.
with such.A('MonoImage class') as it:
(省略)
@it.should('generate integer histogram')
def test():
correct = MonoImage.create_with_array([0, 2, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], np.uint32, 32);
### vector test
vec = MonoImage.create_with_array([3, 1, 4, 1, 5, 9], np.uint8, 4);
it.assertEqual(vec.generate_with_int_hist(), correct)
# Check if the method is called twice
it.assertEqual(vec.generate_with_int_hist(), correct)
### matrix test
mat = MonoImage.create_with_array([[3, 1, 4], [1, 5, 9]], np.uint8, 4);
it.assertEqual(mat.generate_with_int_hist(), correct)
# Check if the method is called twice
it.assertEqual(mat.generate_with_int_hist(), correct)
class MonoImagePixelIterator:
def __init__(self, mono_image):
self._mono_image = mono_image
self._width = mono_image.width
self._height = mono_image.height
self._f = self.vector_iterator_func if mono_image.ndim == 1 else self.matrix_iterator_func
self._getter = self.vector_getter_func if mono_image.ndim == 1 else self.matrix_getter_func
self._setter = self.vector_setter_func if mono_image.ndim == 1 else self.matrix_setter_func
class MonoImagePixelIterator:
(中略)
def matrix_iterator_func(self):
self._nowr = 0
self._nowc = 0
while (self._nowr != -1):
yield self
self._nowc += 1
if self._nowc == self._width:
self._nowc = 0
self._nowr += 1
if self._nowr == self._height:
self._nowr = -1
def matrix_getter_func(self):
return self._mono_image._array[self._nowr, self._nowc]
def matrix_setter_func(self, v):
self._mono_image._array[self._nowr, self._nowc] = v
Rubyist なため,効率よりもどうしたら DRY (Don't repeat yourself) になるかをつい考えてしまう. vector 版でかなり DRY になるよう努力したため,matrix 版は非常に少ない追加記述だけですんだ. これも NumPy の柔軟さのおかげである.
あまり長くならなかったが今日はここまで.