Package: ellipse
ellipseA parametrized dataset of pictures of ellipses, for Neural Network experimentation.fromellipseimport*importmatplotlib.pyplotasplt%matplotlibinlineThis example reminds us how the plots are oriented, which is not the usual math way.A=np.array([[1,2],[3,4]])print(A)plt.imshow(A,cmap='Blues')plt.colorbar()plt.show()[[1 2]
 [3 4]]Basic optionsLet's see the basic parametersplt.imshow(get_ellipse())plt.show()Change the positionplt.imshow(get_ellipse(row=40,col=10))plt.show()Change the radiiplt.imshow(get_ellipse(a=20,b=5))plt.show()Change the angle. Note that the image row,col coordinate system is still right-handed, so rotation turns anticlockwise as normal.plt.imshow(get_ellipse(a=20,b=5,theta=np.pi/12))plt.show()We can combine all the optionsplt.imshow(get_ellipse(row=48,col=16,a=16,b=2,theta=np.pi/6))plt.show()Advanced OptionsWe can adjust the downsampling to make a smoother or coarser edge. The default is 4.
For illustration, here is the edge effect of the downsamplingfig,ax=plt.subplots(1,3,figsize=(6,18),dpi=100)ax[0].imshow(get_ellipse(a=20,downsample_factor=1))ax[0].set_title("factor 1")ax[0].set_aspect(1.0)ax[1].imshow(get_ellipse(a=20,downsample_factor=10))ax[1].set_title("factor 10")ax[1].set_aspect(1.0)ax[2].imshow(get_ellipse(a=20,downsample_factor=10)-get_ellipse(a=20,downsample_factor=1))ax[2].set_title("difference")ax[2].set_aspect(1.0)fig.tight_layout()plt.show()The coordinate system wraps around, so the ellipse is really drawn on a torus.plt.imshow(get_ellipse(img_size=64,row=56,col=16,a=16,b=2,theta=np.pi/6,downsample_factor=4))plt.show()We can change the canvas size, but note that you will want to adjust the centers and radii, which chosen to reasonable values for a 64x64 canvasplt.imshow(get_ellipse(img_size=8,row=5,col=3,a=3,b=1,theta=np.pi/6,downsample_factor=1))plt.show()plt.imshow(get_ellipse(img_size=256,row=160,col=96,a=96,b=32,theta=np.pi/6,downsample_factor=1))plt.show()Sometimes, we do not want to allow that wrap-around.  Thecheck_boundsfunction is used byallow_wrap=False.
See thecheck_bounds.ipynbnotebook for more use of boundschecking.check_bounds(img_size=64,row=56,col=16,a=16,b=2,theta=np.pi/6,downsample_factor=4)Falsecheck_bounds(img_size=64,row=47,col=16,a=16,b=2,theta=np.pi/6,downsample_factor=4)TrueGenerating Random ExamplesWe can generate random examples and feed their parameters toget_ellipsep=generate(64,row_range=(0,64),col_range=(0,64),area_range=(8*np.pi,16*np.pi),logar_range=(1,2),theta_range=(0,np.pi))plt.imshow(get_ellipse(**p))plt.title(f"bounds?{check_bounds(**p)}")plt.show()fig,axs=plt.subplots(1,8,figsize=(4*8,4))foriinrange(8):p=generate(64,row_range=(0,64),col_range=(0,64),area_range=(8*np.pi,16*np.pi),logar_range=(1,2),theta_range=(0,np.pi))E=get_ellipse(**p,downsample_factor=3)axs[i].imshow(E)plt.show()Pre-defined datasetsSome datasets are particularly useful for our experiements. We have hard-coded their parameterizations.fromellipse.datasetsimporttorusX=torus(n=100,return_arrays=True)fig,ax=plt.subplots(10,10,figsize=(20,20))foriinrange(100):ax[i//10,i%10].imshow(X[i])plt.show()importellipsehelp(ellipse.get_ellipse)Help on function get_ellipse in module ellipse:

get_ellipse(img_size: int = 64, row: int = 32, col: int = 32, a: float = 16, b: float = 16, theta: float = 0.0, downsample_factor: int = 4, allow_wrap=True)
    Make a single ellipse with the given geometry.
    NOTE! We use the "upper-left" origin convention, so that plt.imgshow( ) and print( )
    give the same orientation of the image/matrix.
    
    :param img_size: Output image resolution. For example, 64 gives a 64x64 image.
    :param r: row-position of ellipse center.  Relative to upper-left of image.
    :param c: col-position of ellipse center.  Relative to upper-left of image.
    :param a: Length of semi-major axis.
    :param b: Length of semi-minor axis.
    :param theta: Angle in radians of the semi-major axis versus the row-axis, in range [0,pi]
    :param downsample_factor: Oversampling factor for downscaling image. Default=4.
    :param allow_wrap: If False, raise an exception if the ellipse wraps around the image.
    :return: numpy array of shape (img_size,img_size) suitable for `matplotlib.pyplot.imshow`