AICS Spherical Harmonics Parametrization: Analyzing and Describing 3D Starlike Shapes
Spherical harmonics parametrization is a powerful technique for analyzing and describing complex 3D shapes. It provides a concise representation of shape features using a set of coefficients derived from the harmonics representation. The AICS Spherical Harmonics Parametrization package offers an efficient and user-friendly solution for performing shape analysis on 3D starlike shapes. In this article, we will explore the capabilities of this package and demonstrate its usage through an example.
Installation
To get started with the AICS Spherical Harmonics Parametrization package, you can install the stable release using the following command:
pip install aicsshparam
If you require customization or want to build from source, you can clone the repository and install it in editable mode:
git clone git@github.com:AllenCell/aics-shparam.git
cd aics-shparam
pip install -e .
Example Usage
Let’s walk through an example of how one could use the spherical harmonics coefficients as shape descriptors on a synthetic dataset composed of three different shapes: spheres, cubes, and octahedrons.
First, we import the required packages and set the random seed for reproducibility:
“`python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from aicsshparam import shtools, shparam
from skimage.morphology import ball, cube, octahedron
np.random.seed(42)
“`
Next, we define a function that generates binary images containing one of the three shapes randomly:
python
def get_random_3d_shape():
idx = np.random.choice([0, 1, 2], 1)[0]
element = [ball, cube, octahedron][idx]
label = ['ball', 'cube', 'octahedron'][idx]
img = element(10 + int(10 * np.random.rand()))
img = np.pad(img, ((1, 1), (1, 1), (1, 1)))
img = img.reshape(1, *img.shape)
img = shtools.rotate_image_2d(
image=img,
angle=360 * np.random.rand()
).squeeze()
return label, img
We then compute the spherical harmonics coefficients of each shape and store them in a pandas dataframe:
python
df_coeffs = pd.DataFrame([])
for i in range(30):
label, img = get_random_3d_shape()
(coeffs, _), _ = shparam.get_shcoeffs(image=img, lmax=4)
coeffs.update({'label': label})
df_coeffs = df_coeffs.append(coeffs, ignore_index=True)
Now that we have the coefficients dataframe, we can use dimensionality reduction techniques like Principal Component Analysis (PCA) to visualize the relationships between shapes:
python
pca = PCA(n_components=2)
trans = pca.fit_transform(df_coeffs.drop(columns=['label']))
df_trans = pd.DataFrame(trans)
df_trans.columns = ['PC1', 'PC2']
df_trans['label'] = df_coeffs.label
Finally, we can plot the resulting PCA dataframe:
python
fig, ax = plt.subplots(1, 1, figsize=(3, 3))
for label, df_label in df_trans.groupby('label'):
ax.scatter(df_label.PC1, df_label.PC2, label=label, s=50)
plt.legend(loc='upper left', bbox_to_anchor=(1.05, 1))
plt.xlabel('PC1')
plt.ylabel('PC2')
plt.show()
This scatter plot reveals how similar shapes are grouped together based on their spherical harmonics coefficients.
Conclusion
The AICS Spherical Harmonics Parametrization package provides a powerful tool for analyzing and describing 3D starlike shapes. By leveraging spherical harmonics coefficients and dimensionality reduction techniques like PCA, researchers can gain valuable insights into shape variations and relationships within their datasets. Whether you are working in the field of biology, physics, or computer graphics, this package offers a comprehensive solution for shape analysis. Get started today and unlock the potential of spherical harmonics parametrization!
Reference
For an example of how this package was used to analyze a dataset of over 200k single-cell images at the Allen Institute for Cell Science, please check out our paper in bioRxiv.
Questions?
If you have any questions or need further assistance, feel free to leave a comment in our Allen Cell forum at https://forum.allencell.org/.
Free software: Allen Institute Software License
Leave a Reply