We start with a picture and a question. Each panel adds one idea before we turn to the equations, code and checks.
1
A filled disk
We can move around the filled region without meeting a hole.
Betti numbers count independent topological features of the filled region. Here \(\beta_0\) counts connected components and \(\beta_1\) counts holes. A filled disk has \(\beta_0=1\) and \(\beta_1=0\).
2
Remove the middle
The region is still connected. What changed?
The annulus has one hole: \(\beta_0=1,\ \beta_1=1\). Its planar Euler characteristic is \(\chi=\beta_0-\beta_1=0\).
3
Trace its boundaries
One hole, but two closed contours!
The inner and outer boundaries are both closed. Counting contours is therefore different from counting holes in the filled region.
4
Compare with the notebook
Calculation details
Bounded 2D Euler: disk=1, annulus=0; annulus boundary contours=2
Bounded 3D Euler: ball=1, shell=2, solid with one tunnel=0
All worked examples passed. Ordinary skimage calls above are NOT periodic.
Does the calculated result agree with the picture I had in mind?
Panels 1–3 are explanatory schematics. Their scales and illustrative fields do not replace the parameters or numerical results in the notebook. Panel 4 shows the notebook’s recorded result or checks.
The calculation, one step at a time
We show the purpose of each notebook cell beside its code and recorded result. The controls move through those results; they do not execute Python in the browser. The notebook remains available for running or changing the calculation.
Loading the lesson…
We construct small masks with known topology before analyzing a simulated microstructure. A mask is a Boolean array: True means occupied. In 2D we use four edge-sharing neighbors. In 3D we use six face-sharing neighbors. Distances are measured in cells. A graph loop, a hole in a filled region and a closed boundary contour are different objects.
We develop this calculation in small steps. We run a cell, inspect its result and test it before adding the next part. Run the cells in order in Jupyter. A code cell defines a part of the calculation or carries it out; defining a function does not run it.
I permit free noncommercial teaching, learning and demonstrations with acknowledgment. Research and commercial use require my written permission. Earlier licensed releases retain their permissions. Read the code-use terms.
Copyright (c) 2026 Saswata Bhattacharyya.
Step 1: Prepare the Python environment
We install the packages listed in the next cell once in the same environment as the notebook. In Jupyter or Colab, remove the comment marker from the installation line below if a package is missing. NumPy stores arrays and evaluates numerical operations. Matplotlib displays plots. Pillow reads and writes images. This installation does not change the model parameters.
# %pip install numpy scikit-image matplotlibimportnumpyasnpassertnp.isfinite(np.array([0.0,1.0])).all()print("NumPy is ready:",np.__version__)
NumPy is ready: 2.1.3
Step 2: Input values
We retain the default values from the original calculation. The output name locates the files written by this notebook.
Step 3: Prepare the next part of the calculation
We define the following operations. These are the same operations as in the original source, written on separate lines so their order is clear. Inspect their values before running the next cell.
We keep a wrapped pixel address and a lifted, unwrapped position for each visited pixel. A loop displacement divided by the grid shape is a winding vector. Its independent directions determine winding rank. Four edge-sharing neighbors define this 2D graph. A seam crossing alone does not establish wrapping.
Read this complete operation (38 lines)
defperiodic_components(mask):"""Four-neighbor components and independent periodic winding vectors. The graph measures connectivity. Its many local cycles must NOT be interpreted as holes of the filled pixel region. """mask=np.asarray(mask,dtype=bool)shape=np.array(mask.shape)assertmask.ndim==2andmin(shape)>=3seen=np.zeros(mask.shape,dtype=bool)lifts=np.zeros((*mask.shape,2),dtype=np.int64)result=[]forstartinmap(tuple,np.argwhere(mask)):ifseen[start]:continueseen[start]=Truequeue=deque([start])count,basis=(0,[])whilequeue:p=queue.popleft()count+=1forstepin((1,0),(-1,0),(0,1),(0,-1)):q=tuple((np.array(p)+step)%shape)ifnotmask[q]:continueproposed=lifts[p]+stepifnotseen[q]:seen[q]=Truelifts[q]=proposedqueue.append(q)else:difference=proposed-lifts[q]assertnp.all(difference%shape==0)w=difference//shapeifnp.any(w)and(notbasisor(len(basis)==1andbasis[0][0]*w[1]-basis[0][1]*w[0]!=0)):basis.append(tuple(map(int,w)))result.append({'pixels':count,'winding_rank':len(basis),'basis':basis})returnresult
Step 5: Our check of this result
We check finite values, conservation or the known reference result as specified below. An assertion stops the notebook if the condition fails. Inspect the quantities and tolerance rather than suppressing a failure.
Step 6: Repeat the operation and inspect each case
We repeat the operation over the specified cases. Read the loop variable first, then follow what changes on each pass. The assertions check the stated numerical or topological condition.
We check finite values, conservation or the known reference result as specified below. An assertion stops the notebook if the condition fails. Inspect the quantities and tolerance rather than suppressing a failure.
We check finite values, conservation or the known reference result as specified below. An assertion stops the notebook if the condition fails. Inspect the quantities and tolerance rather than suppressing a failure.
We check finite values, conservation or the known reference result as specified below. An assertion stops the notebook if the condition fails. Inspect the quantities and tolerance rather than suppressing a failure.
Step 10: Repeat the operation and inspect each case
We repeat the operation over the specified cases. Read the loop variable first, then follow what changes on each pass. The assertions check the stated numerical or topological condition.
We define the following operations. These are the same operations as in the original source, written on separate lines so their order is clear. Inspect their values before running the next cell.
print('Bounded 2D Euler: disk=1, annulus=0; annulus boundary contours=2')print('Bounded 3D Euler: ball=1, shell=2, solid with one tunnel=0')print('All worked examples passed. Ordinary skimage calls above are NOT periodic.')
Bounded 2D Euler: disk=1, annulus=0; annulus boundary contours=2
Bounded 3D Euler: ball=1, shell=2, solid with one tunnel=0
All worked examples passed. Ordinary skimage calls above are NOT periodic.
Step 12: Review and extend the calculation
Then we change one input, predict its effect and compare the result with that prediction. A successful check establishes the tested property; it does not establish convergence or equilibrium.
Technical details
We first restart the kernel and run all the required cells. The notebook embeds the calculation directly and does not need that script to run.