def test_cylinder_non_positive_pixels_per_len(): good_diameter = 4 # len, some positive number is good good_height = 2 * good_diameter # len, a very squat cylinder bad_pixels_per_len = 0 # should be 1 or greater with pytest.raises(ValueError): pixel_cylinder( diameter_outer=good_diameter, height=good_height, pixels_per_len=bad_pixels_per_len, )
def test_cylinder_hallow_construction(): cylinder = pixel_cylinder(height=1.0, diameter_inner=6.0, diameter_outer=12.0, pixels_per_len=1) assert cylinder # tests constructor known_mask = np.array([[ [0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0], [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1], [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1], [0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0], ]]) known_mask_vector = np.ndarray.flatten(known_mask) calc_mask = cylinder.mask calc_mask_vector = np.ndarray.flatten(calc_mask) tolerance = 1e-6 # very small value abs_error = np.abs(np.linalg.norm(known_mask_vector - calc_mask_vector)) assert abs_error < tolerance bb = cylinder.bounding_box assert bb.dx == 1 assert bb.dy == 12 assert bb.dz == 12
# Reference # https://scikit-image.org/docs/dev/auto_examples/numpy_operations/plot_structuring_elements.html#sphx-glr-auto-examples-numpy-operations-plot-structuring-elements-py import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from ptg.pixel_shape import PixelCube as pixel_cube from ptg.pixel_shape import PixelCylinder as pixel_cylinder from ptg.pixel_shape import PixelSphere as pixel_sphere struc_3d = { "PixelCube1": pixel_cube(dx=1, pixels_per_len=1), "PixelCube2": pixel_cube(dx=2, pixels_per_len=1), "PixelCube3": pixel_cube(dx=3, pixels_per_len=1), "PixelCylinder3": pixel_cylinder(diameter_outer=3, height=1, pixels_per_len=1), "PixelCylinder5": pixel_cylinder(diameter_outer=5, height=1, pixels_per_len=1), "PixelCylinder7": pixel_cylinder(diameter_outer=7, height=1, pixels_per_len=1), "PixelSphere3": pixel_sphere(diameter=3, pixels_per_len=1), "PixelSphere5": pixel_sphere(diameter=5, pixels_per_len=1), "PixelSphere7": pixel_sphere(diameter=7, pixels_per_len=1), } fig = plt.figure(figsize=(8, 8)) idx = 1
draw_cylinder = False if draw_cylinder: max_len = 12 else: max_len = 6 ppl = 3 # pixels per length if draw_cylinder: # cylinder primitive pixel_shape = pixel_cylinder( diameter_inner=6, diameter_outer=max_len, height=1, pixels_per_len=ppl, anchor_x=offsetx, anchor_y=offsety, anchor_z=offsetz, ) else: # quarter-cylinder primitive pixel_shape = pixel_quarter_cylinder( height=1, radius_inner=3, radius_outer=max_len, pixels_per_len=ppl, anchor_x=offsetx, anchor_y=offsety, anchor_z=offsetz, )
def test_cylinder_diameter_inner_exceeds_diameter_outer(): ID = 10 # inner diameter OD = 5 # outer diameter with pytest.raises(ValueError): pixel_cylinder(diameter_inner=ID, diameter_outer=OD)
def test_cylinder_non_positive_height(): good_diameter = 10 bad_height = 0 # height should be 1 or greater with pytest.raises(ValueError): pixel_cylinder(diameter_outer=good_diameter, height=bad_height)
def test_cylinder_non_positive_diameter(): bad_value = 0 # diamter should be 1 or greater with pytest.raises(ValueError): pixel_cylinder(diameter_outer=bad_value)