Skip to content

Commit

Permalink
More testing of ImageFile
Browse files Browse the repository at this point in the history
  • Loading branch information
gb119 committed Oct 5, 2019
1 parent f730de8 commit ed24d2e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
20 changes: 15 additions & 5 deletions Stoner/Image/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1459,7 +1459,7 @@ def _proxy(*args, **kargs):

def __dir__(self):
"""Pass through to the dir of skimage.draw."""
own = set([x for x in self.__class__.__dict__.keys() if not x.startswith("_")])
own = set(self.__class__.__dict__.keys())
d = set(dir(draw))
return list(own | d)

Expand Down Expand Up @@ -1524,8 +1524,8 @@ def rectangle(self, r, c, w, h, angle=0.0, shape=None, value=1.0):
co_ords = np.array([[x1, y1], [x2, y1], [x2, y2], [x1, y2]])
if angle != 0:
centre = np.array([r, c])
c, s, m = np.cos, np.sin, np.matmul
r = np.array([[c(angle), -s(angle)], [s(angle), c(angle)]])
cos, sin, m = np.cos, np.sin, np.matmul
r = np.array([[cos(angle), -sin(angle)], [sin(angle), cos(angle)]])
co_ords = np.array([centre + m(r, xy - centre) for xy in co_ords])
rr, cc = draw.polygon(co_ords[:, 0], co_ords[:, 1], shape=shape)
self.img[rr, cc] = value
Expand Down Expand Up @@ -1616,15 +1616,25 @@ def _proxy_call(*args, **kargs):

def __repr__(self):
"""Make a textual representation of the image."""
return repr(self._mask)
output = ""
f = np.array(["."] * self._mask.shape[1])
t = np.array(["X"] * self._mask.shape[1])
for ix in self._mask:
row = np.where(ix, t, f)
output += "".join(row) + "\n"
return output

def __str__(self):
"""Make a textual representation of the image."""
return repr(self._mask)

def __invert__(self):
"""Invert the mask."""
return np.logical_not(self._mask)

def __neg__(self):
"""Invert the mask."""
return -self._mask
return np.logical_not(self._mask)

def _repr_png_(self):
"""Provide a display function for iPython/Jupyter."""
Expand Down
29 changes: 29 additions & 0 deletions tests/Stoner/Image/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import os
import shutil
from Stoner.compat import python_v3
import matplotlib.pyplot as plt

import warnings

Expand Down Expand Up @@ -343,6 +344,8 @@ def test_methods(self):
counts={(2,7):803,(3,5):846}
expected=counts.get(version_info[0:2],871)
self.assertEqual(len(attrs),expected,"Length of ImageFile dir failed. {}:{}".format(expected,len(attrs)))
self.assertTrue(image._repr_png_private_().startswith(b'\x89PNG\r\n'),"Failed to do ImageFile png representation")


def test_mask(self):
i=np.ones((200,200),dtype="uint8")*np.linspace(1,200,200).astype("uint8")
Expand All @@ -362,6 +365,32 @@ def test_mask(self):
i.mask=False
i.mask.draw.annulus(100,50,25,35)
self.assertAlmostEqual(i.mean(), 51.0,msg="Mean after annular pass mask failed")
i.mask[:,:]=False
self.assertFalse(np.any(i.mask),"Setting Mask by index failed")
i.mask=-i.mask
self.assertTrue(np.all(i.mask),"Setting Mask by index failed")
i.mask=~i.mask
self.assertFalse(np.any(i.mask),"Setting Mask by index failed")
i.mask.draw.circle(100,100,20)
st=repr(i.mask)
self.assertEqual(st.count("X"),i.mask.sum(),"String representation of mask failed")
self.assertTrue(np.all(i.mask.image==i.mask._mask),"Failed to access mak data by image attr")
i.mask=False
i2=i.clone
i.mask.draw.rectangle(100,100,100,50,angle=np.pi/2)
i2.mask.draw.rectangle(100,100,50,100)
self.i2=i2
self.assertTrue(np.all(i.mask.image==i2.mask.image),"Drawing rectange with angle failed")
self.assertTrue(i.mask._repr_png_().startswith(b'\x89PNG\r\n'),"Failed to do mask png representation")




def test_draw(self):
i=ImageFile(np.zeros((200,200)))
attrs=[x for x in dir(i.draw) if not x.startswith("_")]
self.assertEqual(len(attrs),20,"Directory of DrawProxy failed")




Expand Down

0 comments on commit ed24d2e

Please sign in to comment.