# AICS Spherical Harmonics Parametrization
[![Build Status](https://github.com/AllenCell/aics-shparam/workflows/Build%20Main/badge.svg)](https://github.com/AllenCell/aics-shparam/actions)
[![Documentation](https://github.com/AllenCell/aics-shparam/workflows/Documentation/badge.svg)](https://AllenCell.github.io/aics-shparam/)
### Spherical harmonics parametrization for 3D starlike shapes.
![Parameterization of cell and nuclear shape](https://github.com/AllenCell/aics-shparam/blob/main/docs/logo.gif?raw=true)
## Installation:
**Stable Release**: `pip install aicsshparam`
**Build from source to make customization**:
```console
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.
```python
# 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
```
```python
# 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)
```
![Coefficients dataframe](https://github.com/AllenCell/aics-shparam/blob/main/docs/table1.jpg?raw=true)
```python
# 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)
```
![PCA dataframe](https://github.com/AllenCell/aics-shparam/blob/main/docs/table2.jpg?raw=true)
```python
# 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()
```
![PC1 vs. PC2](https://github.com/AllenCell/aics-shparam/blob/main/docs/pc12.png?raw=true)
## 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](https://www.biorxiv.org/content/10.1101/2020.12.08.415562v1).
## Development
See [CONTRIBUTING.md](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/](https://forum.allencell.org/).
_Free software: Allen Institute Software License_
Raw data
{
"_id": null,
"home_page": "https://github.com/AllenCell/aics-shparam",
"name": "aicsshparam",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "aicsshparam",
"author": "Matheus Viana",
"author_email": "matheus.viana@alleninstitute.org",
"download_url": "https://files.pythonhosted.org/packages/cb/17/2cb2cb7b636db144d025ba01933c007e2271e73f41c01e896d5b2170a8c7/aicsshparam-0.1.10.tar.gz",
"platform": null,
"description": "# AICS Spherical Harmonics Parametrization\n\n[![Build Status](https://github.com/AllenCell/aics-shparam/workflows/Build%20Main/badge.svg)](https://github.com/AllenCell/aics-shparam/actions)\n[![Documentation](https://github.com/AllenCell/aics-shparam/workflows/Documentation/badge.svg)](https://AllenCell.github.io/aics-shparam/)\n\n### Spherical harmonics parametrization for 3D starlike shapes.\n\n![Parameterization of cell and nuclear shape](https://github.com/AllenCell/aics-shparam/blob/main/docs/logo.gif?raw=true)\n\n## Installation:\n\n**Stable Release**: `pip install aicsshparam`\n\n**Build from source to make customization**:\n\n```console\ngit clone git@github.com:AllenCell/aics-shparam.git\ncd aics-shparam\npip install -e .\n```\n\n## How to use\n\nHere 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.\n\n```python\n# Import required packages\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.decomposition import PCA\nfrom aicsshparam import shtools, shparam\nfrom skimage.morphology import ball, cube, octahedron\nnp.random.seed(42) # for reproducibility\n```\n\n```python\n# Function that returns binary images containing one of the three\n# shapes: cubes, spheres octahedrons of random sizes.\ndef get_random_3d_shape():\n idx = np.random.choice([0, 1, 2], 1)[0]\n element = [ball, cube, octahedron][idx]\n label = ['ball', 'cube', 'octahedron'][idx]\n img = element(10 + int(10 * np.random.rand()))\n img = np.pad(img, ((1, 1), (1, 1), (1, 1)))\n img = img.reshape(1, *img.shape)\n # Rotate shapes to increase dataset variability.\n img = shtools.rotate_image_2d(\n image=img,\n angle=360 * np.random.rand()\n ).squeeze()\n return label, img\n\n# Compute spherical harmonics coefficients of shape and store them\n# in a pandas dataframe.\ndf_coeffs = []\nfor i in range(30):\n # Get a random shape\n label, img = get_random_3d_shape()\n # Parameterize with L=4, which corresponds to50 coefficients\n # in total\n (coeffs, _), _ = shparam.get_shcoeffs(image=img, lmax=4)\n coeffs.update({'label': label})\n df_coeffs.append(coeffs)\ndf_coeffs = pd.DataFrame(df_coeffs)\n\n# Vizualize the resulting dataframe\nwith pd.option_context('display.max_rows', 5, 'display.max_columns', 5):\n display(df_coeffs)\n```\n\n![Coefficients dataframe](https://github.com/AllenCell/aics-shparam/blob/main/docs/table1.jpg?raw=true)\n\n```python\n# Let's use PCA to reduce the dimensionality of the coefficients\n# dataframe from 51 down to 2.\npca = PCA(n_components=2)\ntrans = pca.fit_transform(df_coeffs.drop(columns=['label']))\ndf_trans = pd.DataFrame(trans)\ndf_trans.columns = ['PC1', 'PC2']\ndf_trans['label'] = df_coeffs.label\n\n# Vizualize the resulting dataframe\nwith pd.option_context('display.max_rows', 5, 'display.max_columns', 5):\n display(df_trans)\n```\n\n![PCA dataframe](https://github.com/AllenCell/aics-shparam/blob/main/docs/table2.jpg?raw=true)\n\n```python\n# Scatter plot to show how similar shapes are grouped together.\nfig, ax = plt.subplots(1,1, figsize=(3,3))\nfor label, df_label in df_trans.groupby('label'):\n ax.scatter(df_label.PC1, df_label.PC2, label=label, s=50)\nplt.legend(loc='upper left', bbox_to_anchor=(1.05, 1))\nplt.xlabel('PC1')\nplt.ylabel('PC2')\nplt.show()\n```\n\n![PC1 vs. PC2](https://github.com/AllenCell/aics-shparam/blob/main/docs/pc12.png?raw=true)\n\n\n## Reference\n\nFor 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](https://www.biorxiv.org/content/10.1101/2020.12.08.415562v1).\n\n## Development\nSee [CONTRIBUTING.md](CONTRIBUTING.md) for information related to developing the code.\n\n\n## Questions?\n\nIf you have any questions, feel free to leave a comment in our Allen Cell forum: [https://forum.allencell.org/](https://forum.allencell.org/).\n\n\n_Free software: Allen Institute Software License_\n\n\n",
"bugtrack_url": null,
"license": "Allen Institute Software License",
"summary": "Spherical harmonics parametrization for 3D starlike shapes",
"version": "0.1.10",
"project_urls": {
"Homepage": "https://github.com/AllenCell/aics-shparam"
},
"split_keywords": [
"aicsshparam"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "311b3b8d18855df79ea4f7f6512917b28e9f59f25ec6d0421491d04753bcb0d8",
"md5": "6c4e92c2bb9c221f2470db45197d7ed4",
"sha256": "75f8c9f484cb667f78f1f049f28cf3189ab7f6e6edd241232c343b8a42462867"
},
"downloads": -1,
"filename": "aicsshparam-0.1.10-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "6c4e92c2bb9c221f2470db45197d7ed4",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": ">=3.9",
"size": 14652,
"upload_time": "2024-04-06T16:57:23",
"upload_time_iso_8601": "2024-04-06T16:57:23.545111Z",
"url": "https://files.pythonhosted.org/packages/31/1b/3b8d18855df79ea4f7f6512917b28e9f59f25ec6d0421491d04753bcb0d8/aicsshparam-0.1.10-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "cb172cb2cb7b636db144d025ba01933c007e2271e73f41c01e896d5b2170a8c7",
"md5": "ebcd28d78aafe515fc5e6c6c97589fb5",
"sha256": "89b3a9732dd87631e23e239262d96496b500b22b2f7ed4ba165dd22927129a3b"
},
"downloads": -1,
"filename": "aicsshparam-0.1.10.tar.gz",
"has_sig": false,
"md5_digest": "ebcd28d78aafe515fc5e6c6c97589fb5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 670289,
"upload_time": "2024-04-06T16:57:25",
"upload_time_iso_8601": "2024-04-06T16:57:25.059476Z",
"url": "https://files.pythonhosted.org/packages/cb/17/2cb2cb7b636db144d025ba01933c007e2271e73f41c01e896d5b2170a8c7/aicsshparam-0.1.10.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-06 16:57:25",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "AllenCell",
"github_project": "aics-shparam",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "aicsshparam"
}