basic arith tests done
small improvements to python binding
This commit is contained in:
parent
666a0a6f83
commit
12304166ce
1037
python/atith.log
1037
python/atith.log
File diff suppressed because it is too large
Load Diff
@ -15,26 +15,111 @@ formats = [Vips.BandFormat.UCHAR,
|
|||||||
Vips.BandFormat.UINT,
|
Vips.BandFormat.UINT,
|
||||||
Vips.BandFormat.INT,
|
Vips.BandFormat.INT,
|
||||||
Vips.BandFormat.FLOAT,
|
Vips.BandFormat.FLOAT,
|
||||||
Vips.BandFormat.DOUBLE,
|
Vips.BandFormat.DOUBLE]
|
||||||
Vips.BandFormat.COMPLEX,
|
cformats = [Vips.BandFormat.COMPLEX,
|
||||||
Vips.BandFormat.DPCOMPLEX]
|
Vips.BandFormat.DPCOMPLEX]
|
||||||
|
all_formats = formats + cformats;
|
||||||
|
|
||||||
|
black = Vips.Image.black(100, 100)
|
||||||
|
red = black.copy()
|
||||||
|
green = black.copy()
|
||||||
|
blue = black.copy()
|
||||||
|
red.draw_circle(100, 50, 50, 40, fill = True)
|
||||||
|
green.draw_circle(128, 50, 50, 40, fill = True)
|
||||||
|
blue.draw_circle(140, 50, 50, 40, fill = True)
|
||||||
|
colour = Vips.Image.bandjoin([red, green, blue])
|
||||||
|
all_images = [green, colour]
|
||||||
|
|
||||||
|
# an expanding zip ... if either of the args is not a list, duplicate it down
|
||||||
|
# the other
|
||||||
|
def zip_expand(x, y):
|
||||||
|
if isinstance(x, list) and isinstance(y, list):
|
||||||
|
return zip(x, y)
|
||||||
|
elif isinstance(x, list):
|
||||||
|
return [[i, y] for i in x]
|
||||||
|
elif isinstance(y, list):
|
||||||
|
return [[x, j] for j in y]
|
||||||
|
else:
|
||||||
|
return [[x, y]]
|
||||||
|
|
||||||
|
# run a 2-ary function on two things -- loop over elements pairwise if the
|
||||||
|
# things are lists
|
||||||
|
def run_fn2(fn, x, y):
|
||||||
|
if isinstance(x, Vips.Image) or isinstance(y, Vips.Image):
|
||||||
|
return fn(x, y)
|
||||||
|
elif isinstance(x, list) or isinstance(y, list):
|
||||||
|
return [fn(i, j) for i, j in zip_expand(x, y)]
|
||||||
|
else:
|
||||||
|
return fn(x, y)
|
||||||
|
|
||||||
class TestArithmetic(unittest.TestCase):
|
class TestArithmetic(unittest.TestCase):
|
||||||
def setUp(self):
|
# test a pait of things which can be lists
|
||||||
black = Vips.Image.black(100, 100)
|
def assertAlmostEqualObjects(self, a, b, msg = ''):
|
||||||
mono = black.draw_circle(128, 50, 50, 50)
|
[self.assertAlmostEqual(x, y, places = 2, msg = msg)
|
||||||
colour = Vips.Image.bandjoin([black, mono, black])
|
for x, y in zip_expand(a, b)]
|
||||||
images = [black, mono, colour]
|
|
||||||
|
|
||||||
self.test_set = [x.cast(y) for x in images for y in formats]
|
# run a function on (image, constant), and on (constant, image).
|
||||||
self.avgs = [x.avg() for x in self.test_set]
|
def run_testconst(self, message, fn, im, c):
|
||||||
|
before = im.getpoint(50, 50)
|
||||||
|
im1 = fn(c, im)
|
||||||
|
im2 = fn(im, c)
|
||||||
|
after1 = im1.getpoint(50, 50)
|
||||||
|
after2 = im2.getpoint(50, 50)
|
||||||
|
c1 = run_fn2(fn, c, before)
|
||||||
|
c2 = run_fn2(fn, before, c)
|
||||||
|
self.assertAlmostEqualObjects(after1, c1, msg = message)
|
||||||
|
self.assertAlmostEqualObjects(after2, c2, msg = message)
|
||||||
|
|
||||||
def test_addconst(self):
|
def run_arith_const(self, fn, fmt = all_formats):
|
||||||
def add12(x):
|
[self.run_testconst(fn.func_name + ' scalar', fn, x.cast(y), 12)
|
||||||
return x + 12
|
for x in all_images for y in fmt]
|
||||||
|
[self.run_testconst(fn.func_name + ' vector', fn, colour.cast(y),
|
||||||
|
[12, 13, 14])
|
||||||
|
for y in formats]
|
||||||
|
|
||||||
self.assertEqual([add12(x).avg() for x in self.test_set],
|
# run a function on (image, image)
|
||||||
[add12(x) for x in self.avgs])
|
def run_testim(self, message, fn, left, right):
|
||||||
|
before_left = left.getpoint(50, 50)
|
||||||
|
before_right = right.getpoint(50, 50)
|
||||||
|
im2 = fn(left, right)
|
||||||
|
after = im2.getpoint(50, 50)
|
||||||
|
after_c = run_fn2(fn, before_left, before_right)
|
||||||
|
self.assertAlmostEqualObjects(after, after_c, msg = message)
|
||||||
|
|
||||||
|
def run_arith(self, fn, fmt = all_formats):
|
||||||
|
[self.run_testim(fn.func_name + ' image', fn, x.cast(y), x.cast(z))
|
||||||
|
for x in all_images for y in fmt for z in fmt]
|
||||||
|
|
||||||
|
def test_add(self):
|
||||||
|
def add(x, y):
|
||||||
|
return x + y
|
||||||
|
|
||||||
|
self.run_arith_const(add)
|
||||||
|
self.run_arith(add)
|
||||||
|
|
||||||
|
def test_sub(self):
|
||||||
|
def sub(x, y):
|
||||||
|
return x - y
|
||||||
|
|
||||||
|
self.run_arith_const(sub)
|
||||||
|
self.run_arith(sub)
|
||||||
|
|
||||||
|
def test_mul(self):
|
||||||
|
def mul(x, y):
|
||||||
|
return x * y
|
||||||
|
|
||||||
|
self.run_arith_const(mul)
|
||||||
|
self.run_arith(mul)
|
||||||
|
|
||||||
|
def test_div(self):
|
||||||
|
def div(x, y):
|
||||||
|
return x / y
|
||||||
|
|
||||||
|
# div(const / image) needs (image ** -1), which won't work for complex
|
||||||
|
# images ... just test with non-complex
|
||||||
|
self.run_arith_const(div, fmt = formats)
|
||||||
|
|
||||||
|
self.run_arith(div)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
@ -21,6 +21,13 @@ vips_type_blob = GObject.GType.from_name("VipsBlob")
|
|||||||
vips_type_image = GObject.GType.from_name("VipsImage")
|
vips_type_image = GObject.GType.from_name("VipsImage")
|
||||||
vips_type_operation = GObject.GType.from_name("VipsOperation")
|
vips_type_operation = GObject.GType.from_name("VipsOperation")
|
||||||
|
|
||||||
|
unpack_types = [Vips.Blob, Vips.ArrayDouble, Vips.ArrayImage, Vips.ArrayInt]
|
||||||
|
def isunpack(obj):
|
||||||
|
for t in unpack_types:
|
||||||
|
if isinstance(obj, t):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
class Error(Exception):
|
class Error(Exception):
|
||||||
|
|
||||||
"""An error from vips.
|
"""An error from vips.
|
||||||
@ -81,10 +88,10 @@ class Argument:
|
|||||||
|
|
||||||
logging.debug('read out %s from %s' % (value, self.name))
|
logging.debug('read out %s from %s' % (value, self.name))
|
||||||
|
|
||||||
# turn VipsBlobs into strings
|
# turn VipsBlobs into strings, VipsArrayDouble into lists etc.
|
||||||
# FIXME ... this will involve a copy, we should use
|
# FIXME ... this will involve a copy, we should use
|
||||||
# buffer() instead
|
# buffer() instead
|
||||||
if isinstance(value, Vips.Blob):
|
if isunpack(value):
|
||||||
value = value.get()
|
value = value.get()
|
||||||
|
|
||||||
return value
|
return value
|
||||||
@ -285,6 +292,9 @@ def vips_image_write_to_buffer(self, vips_filename, **kwargs):
|
|||||||
|
|
||||||
return _call_base(saver, [], kwargs, self, option_string)
|
return _call_base(saver, [], kwargs, self, option_string)
|
||||||
|
|
||||||
|
def vips_bandsplit(self):
|
||||||
|
return [self.extract_band(i) for i in range(0, self.bands)]
|
||||||
|
|
||||||
# apply a function to a thing, or map over a list
|
# apply a function to a thing, or map over a list
|
||||||
# we often need to do something like (1.0 / other) and need to work for lists
|
# we often need to do something like (1.0 / other) and need to work for lists
|
||||||
# as well as scalars
|
# as well as scalars
|
||||||
@ -444,8 +454,11 @@ Vips.Image.write_to_file = vips_image_write_to_file
|
|||||||
Vips.Image.write_to_buffer = vips_image_write_to_buffer
|
Vips.Image.write_to_buffer = vips_image_write_to_buffer
|
||||||
# we can use Vips.Image.write_to_memory() directly
|
# we can use Vips.Image.write_to_memory() directly
|
||||||
|
|
||||||
|
# a few useful things
|
||||||
Vips.Image.floor = vips_floor
|
Vips.Image.floor = vips_floor
|
||||||
|
Vips.Image.bandsplit = vips_bandsplit
|
||||||
|
|
||||||
|
# operator overloads
|
||||||
Vips.Image.__getattr__ = vips_image_getattr
|
Vips.Image.__getattr__ = vips_image_getattr
|
||||||
Vips.Image.__add__ = vips_add
|
Vips.Image.__add__ = vips_add
|
||||||
Vips.Image.__radd__ = vips_add
|
Vips.Image.__radd__ = vips_add
|
||||||
|
Loading…
Reference in New Issue
Block a user