Metadata-Version: 2.2
Name: aicsshparam
Version: 0.1.11
Summary: Spherical harmonics parametrization for 3D starlike shapes
Author-email: Matheus Viana <matheus.viana@alleninstitute.org>
License: Allen Institute Software License – This software license is the 2-clause BSD
        license plus a third clause that prohibits redistribution and use for
        commercial purposes without further permission.
        
        Copyright © 2021
        Matheus Viana, Allen Institute.  All rights reserved.
        
        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are met:
        
        1. Redistributions of source code must retain the above copyright notice, this
        list of conditions and the following disclaimer.
        
        2. Redistributions in binary form must reproduce the above copyright notice,
        this list of conditions and the following disclaimer in the documentation
        and/or other materials provided with the distribution.
        
        3. Redistributions and use for commercial purposes are not permitted without
        the Allen Institute’s written permission. For purposes of this license,
        commercial purposes are the incorporation of the Allen Institute's software
        into anything for which you will charge fees or other compensation or use of
        the software to perform a commercial service for a third party. Contact
        terms@alleninstitute.org for commercial licensing opportunities.
        
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
        ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
        WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
        FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
        SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
        CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
        OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
        OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        
        
        
Project-URL: Homepage, https://github.com/AllenCell/aics-shparam
Keywords: aicsshparam
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Science/Research
Classifier: License :: Free for non-commercial use
Classifier: Natural Language :: English
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy
Requires-Dist: scipy
Requires-Dist: scikit-image
Requires-Dist: scikit-learn
Requires-Dist: vtk
Requires-Dist: pyshtools
Provides-Extra: setup
Requires-Dist: pytest-runner>=5.2; extra == "setup"
Provides-Extra: test
Requires-Dist: black; extra == "test"
Requires-Dist: flake8; extra == "test"
Requires-Dist: flake8-debugger; extra == "test"
Requires-Dist: pytest; extra == "test"
Requires-Dist: pytest-cov; extra == "test"
Requires-Dist: pytest-raises; extra == "test"
Provides-Extra: dev
Requires-Dist: pytest-runner>=5.2; extra == "dev"
Requires-Dist: black; extra == "dev"
Requires-Dist: flake8; extra == "dev"
Requires-Dist: flake8-debugger; extra == "dev"
Requires-Dist: pytest; extra == "dev"
Requires-Dist: pytest-cov; extra == "dev"
Requires-Dist: pytest-raises; extra == "dev"
Requires-Dist: bump2version; extra == "dev"
Requires-Dist: coverage; extra == "dev"
Requires-Dist: ipython; extra == "dev"
Requires-Dist: myst-parser; extra == "dev"
Requires-Dist: sphinx; extra == "dev"
Requires-Dist: furo; extra == "dev"
Requires-Dist: tox; extra == "dev"
Requires-Dist: twine; extra == "dev"
Requires-Dist: wheel; extra == "dev"
Provides-Extra: all
Requires-Dist: numpy; extra == "all"
Requires-Dist: scipy; extra == "all"
Requires-Dist: scikit-image; extra == "all"
Requires-Dist: scikit-learn; extra == "all"
Requires-Dist: vtk; extra == "all"
Requires-Dist: pyshtools; extra == "all"
Requires-Dist: pytest-runner>=5.2; extra == "all"
Requires-Dist: black; extra == "all"
Requires-Dist: flake8; extra == "all"
Requires-Dist: flake8-debugger; extra == "all"
Requires-Dist: pytest; extra == "all"
Requires-Dist: pytest-cov; extra == "all"
Requires-Dist: pytest-raises; extra == "all"
Requires-Dist: bump2version; extra == "all"
Requires-Dist: coverage; extra == "all"
Requires-Dist: ipython; extra == "all"
Requires-Dist: myst-parser; extra == "all"
Requires-Dist: sphinx; extra == "all"
Requires-Dist: furo; extra == "all"
Requires-Dist: tox; extra == "all"
Requires-Dist: twine; extra == "all"
Requires-Dist: wheel; extra == "all"

# 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:

## 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:

```bash
conda install -c conda-forge fftw openblas cmake pkg-config
```

**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_
