AICS Spherical Harmonics Parametrization
Spherical harmonics parametrization for 3D starlike shapes.
Installation:
Build Dependencies for Python 3.13.*
If you are using Python 3.13.*, you will need to install the following build dependencies:
- fftw
- openblas
- cmake
- pkg-config
One convenient way to install these dependencies is by using Conda. You can run the following command:
conda install -c conda-forge fftw openblas cmake pkg-config
Stable Release: pip install aicsshparam
Build from source to make customization:
git clone git@github.com:AllenCell/aics-shparam.git
cd aics-shparam
pip install -e .
How to use
Here we outline an example of how one could use spherical harmonics coefficients as shape descriptors on a synthetic dataset composed by 3 different shapes: spheres, cubes and octahedrons.
# Import required packages
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) # for reproducibility
# Function that returns binary images containing one of the three
# shapes: cubes, spheres octahedrons of random sizes.
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)
# Rotate shapes to increase dataset variability.
img = shtools.rotate_image_2d(
image=img,
angle=360 * np.random.rand()
).squeeze()
return label, img
# Compute spherical harmonics coefficients of shape and store them
# in a pandas dataframe.
df_coeffs = []
for i in range(30):
# Get a random shape
label, img = get_random_3d_shape()
# Parameterize with L=4, which corresponds to50 coefficients
# in total
(coeffs, _), _ = shparam.get_shcoeffs(image=img, lmax=4)
coeffs.update({'label': label})
df_coeffs.append(coeffs)
df_coeffs = pd.DataFrame(df_coeffs)
# Vizualize the resulting dataframe
with pd.option_context('display.max_rows', 5, 'display.max_columns', 5):
display(df_coeffs)
# Let's use PCA to reduce the dimensionality of the coefficients
# dataframe from 51 down to 2.
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
# Vizualize the resulting dataframe
with pd.option_context('display.max_rows', 5, 'display.max_columns', 5):
display(df_trans)
# Scatter plot to show how similar shapes are grouped together.
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()
Reference
For an example of how this package was used to analyse a dataset of over 200k single-cell images at the Allen Institute for Cell Science, please check out our paper in bioaRxiv.
Development
See CONTRIBUTING.md for information related to developing the code.
Questions?
If you have any questions, feel free to leave a comment in our Allen Cell forum: https://forum.allencell.org/.
Free software: Allen Institute Software License
Metadata
Release files for aicsshparam 0.1.11
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| aicsshparam-0.1.11.tar.gz | 672.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| aicsshparam-0.1.11-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 1.3 MB
Release files / aicsshparam-0.1.11.tar.gz
| Download URL | aicsshparam-0.1.11.tar.gz |
|---|---|
| Size | 672.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
69e944e9861a6a7434a44cbf0bb453c47e25e2c77ebe79c3a39dd8d5f41e4a09
|
|
BLAKE2b-256 checksum How to use checksums |
0c1fe11d9b4bae1feac3066f21d603685e5e452cb5e86c39fae0956e92b55a23
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.21
|
Release files / aicsshparam-0.1.11-py2.py3-none-any.whl
| Download URL | aicsshparam-0.1.11-py2.py3-none-any.whl |
|---|---|
| Size | 669.2 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
006a39d7041e464ddd15499bbed31a7bc5522d93ab2d6fab5b0a0d0e3aa0bb35
|
|
BLAKE2b-256 checksum How to use checksums |
c2b8749ca89504c144e36428c5783697ad18a64e6957c8dfc5e563cf85fddb8d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.21
|