{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyOqoPlJxEzXB/rGffrk47H3",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
""
]
},
{
"cell_type": "markdown",
"source": [
"# Ring Dataset for Calibration of Classification & Manifold Models"
],
"metadata": {
"id": "H-BUP1FKiX4R"
}
},
{
"cell_type": "markdown",
"source": [
"## defs"
],
"metadata": {
"id": "PBkKn7m0ydvB"
}
},
{
"cell_type": "markdown",
"source": [
"### optional download from github repo"
],
"metadata": {
"id": "t2ypmzGg6PaJ"
}
},
{
"cell_type": "code",
"source": [
"try:\n",
" from gym_rings import get_rings\n",
"except:\n",
" !wget https://raw.githubusercontent.com/jcandane/gym_rings/main/gym_rings.py\n",
" from gym_rings import get_rings"
],
"metadata": {
"id": "0MWhz-V36ZjT"
},
"execution_count": 1,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"### raw code"
],
"metadata": {
"id": "JiZ9NcKW6Xpi"
}
},
{
"cell_type": "code",
"source": [
"import numpy as np\n",
"\n",
"def define_ring(A, radii):\n",
" \"\"\"\n",
" define a Boolean ring, such that the image is made from \"True\" or \"1\".\n",
" A:np.ndarray (initial array)\n",
" radius_range:tuple defined by (minimum radius, maximum radius)\n",
" GET>\n",
" A:np.ndarray (initial array with an additional ring with radius_range)\n",
" \"\"\"\n",
" height, width = A.shape\n",
" radii = radii.reshape(-1,2).T\n",
"\n",
" center = np.array([width / 2, height / 2])\n",
" I_xi = np.indices((height, width)).reshape(2, -1) ## meshgrid -> flatten image\n",
" I_xi = np.vstack((I_xi[1], I_xi[0]))\n",
"\n",
" ### RINGS\n",
" mask_i = (np.linalg.norm(I_xi-center[:,None], axis=0)[:,None] >= radii[0][None,:]) ##&\n",
" mask_i &= (np.linalg.norm(I_xi-center[:,None], axis=0)[:,None] < radii[1][None,:])\n",
" mask_i = np.any(mask_i, axis=-1)\n",
"\n",
" A = A.reshape(-1)\n",
" A[mask_i] = 1\n",
" return A.reshape(height, width)\n",
"\n",
"def ring_notch(image_shape, θ:np.ndarray, radius_range, notch_width=5.):\n",
" \"\"\"\n",
" GIVEN>\n",
" image_shape:tuple (The shape of the image (height, width))\n",
" θ:np.ndarray[1d] (list of angles, θ.shape=(samples,))\n",
" radius_range:np.ndarray (for a given ring, [R_min,R_max])\n",
" notch_width:float (in degrees)\n",
" GET>\n",
" np.ndarray (A boolean mask for the notches)\n",
" \"\"\"\n",
" height, width = image_shape\n",
"\n",
" θ = (θ).astype(float)\n",
" θ *= -1.\n",
" #θ -= 1.*notch_width\n",
" θ_min = np.deg2rad(θ - notch_width)\n",
" θ_max = np.deg2rad(θ + notch_width)\n",
"\n",
" # Create a grid of x and y coordinates\n",
" y, x = np.ogrid[:height, :width]\n",
"\n",
" # Calculate the distances and angles\n",
" y = y - width / 2\n",
" x = x - height / 2\n",
" distance = np.sqrt(x**2 + y**2)\n",
" angle = np.arctan2(y, x) ## every pixel associated an angle\n",
"\n",
" # Normalize angles to [0, 2*pi)\n",
" angle = np.mod(angle, 2 * np.pi)\n",
" θ_min = np.mod(θ_min, 2 * np.pi)\n",
" θ_max = np.mod(θ_max, 2 * np.pi)\n",
"\n",
" # Create the mask for the angle range\n",
" R_min, R_max = radius_range\n",
" R_min = float(R_min)-0.01\n",
" R_max = float(R_max)+0.01\n",
"\n",
" shifted_angle = (angle[:,:,None] - θ_min[None,None,:]) % 360 ### why not % 2*np.pi ???\n",
" shifted_end_angle = (θ_max - θ_min) % 360\n",
" angle_mask = (shifted_angle <= shifted_end_angle[None,None,:])\n",
"\n",
" radius_mask = (distance <= R_max) & (distance > R_min)\n",
" mask = angle_mask & radius_mask[:,:,None]\n",
" return mask.swapaxes(0,2).swapaxes(1,2) ### s.t. snapshots, pixels\n",
"\n",
"def get_rings(N:int, θs:np.ndarray, radii, notch_width=5.): ##\n",
" \"\"\"\n",
" GIVEN>\n",
" N:int (image side size, such that image.shape=(N,N))\n",
" θs:np.ndarray[2d] (list of angles for each ring, with θs.shape=(ring count, samples) )\n",
" radii:np.ndarray[2] (with shape=(ring count, 2), i.e. axis=1 is the R_min, R_max for each ring)\n",
" notch_width:float (notch angle, default=5 degrees, this can be generalized)\n",
" GET>\n",
" np.ndarray (dataset of shape=(θs.shape[1], N, N))\n",
" \"\"\"\n",
" img_shape = (N,N)\n",
"\n",
" R = define_ring(np.zeros(img_shape, dtype=bool), radii)\n",
" mask = np.asarray([ ring_notch(img_shape, θs[r], radii[r], notch_width=notch_width) for r in range(len(radii))])\n",
" mask = np.sum(mask, axis=0)\n",
"\n",
" return R[None,:,:]*np.logical_not( mask )"
],
"metadata": {
"id": "53embXj5iqV6"
},
"execution_count": 2,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"## Rings test"
],
"metadata": {
"id": "Jm3p1JlLqxO5"
}
},
{
"cell_type": "code",
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"### input\n",
"N = 256\n",
"angles = np.array([[90,270,180.,0,360,65,182, 234], [45,86,243,360,97,12, 311, 34], [90,270,90,5,156,65,182, 211]])\n",
"radii = np.array([[30,50],[70,90],[110,130]])\n",
"\n",
"### ouput\n",
"A = get_rings(N, angles, radii)\n",
"\n",
"plt.imshow(A[-8], interpolation='none', cmap='Greys')\n",
"plt.axis(\"off\")\n",
"plt.show()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 406
},
"id": "2by18XdLq1bh",
"outputId": "e71e882d-e45e-479b-fb72-bb49f74efe99"
},
"execution_count": 3,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"