https://github.com/koonimaru/radialtree
A python module to draw a circular dendrogram
https://github.com/koonimaru/radialtree
circular clustering dendrogram hierarchical-clustering python3 radial
Last synced: 6 months ago
JSON representation
A python module to draw a circular dendrogram
- Host: GitHub
- URL: https://github.com/koonimaru/radialtree
- Owner: koonimaru
- License: gpl-3.0
- Created: 2022-06-19T23:25:29.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-11-10T01:08:23.000Z (11 months ago)
- Last Synced: 2025-03-28T23:43:28.341Z (6 months ago)
- Topics: circular, clustering, dendrogram, hierarchical-clustering, python3, radial
- Language: Python
- Homepage:
- Size: 1.35 MB
- Stars: 18
- Watchers: 1
- Forks: 9
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# radialtree
## What is radialtree
radialtree is a python module to draw a circular dendrogram using a output from scipy dendrogram.
## Install
```bash
git clone https://github.com/koonimaru/radialtree.git
cd radialtree
pip install .
```
Please note that radialtree is now integreated into [omniplot](https://github.com/koonimaru/omniplot) module with an improvement.## Example usage
```python
import scipy.cluster.hierarchy as sch
import numpy as np
import radialtree as rtnp.random.seed(1)
labels=[chr(i)*10 for i in range(97, 97+numleaf)]
x = np.random.rand(numleaf)
D = np.zeros([numleaf,numleaf])
for i in range(numleaf):
for j in range(numleaf):
D[i,j] = abs(x[i] - x[j])# Compute and plot the dendrogram.
Y = sch.linkage(D, method='single')
Z2 = sch.dendrogram(Y,labels=labels,no_plot=True)# plot a circular dendrogram
rt.plot(Z2)
```## Example usage 2 (adding color labels to the tree.)
```python
import scipy.cluster.hierarchy as sch
import numpy as np
import radialtree as rt
np.random.seed(1)
numleaf=200
_alphabets=[chr(i) for i in range(97, 97+24)]
labels=sorted(["".join(list(np.random.choice(_alphabets, 10))) for i in range(numleaf)])
x = np.random.rand(numleaf)
D = np.zeros([numleaf,numleaf])
for i in range(numleaf):
for j in range(numleaf):
D[i,j] = abs(x[i] - x[j])
#optionally leaves can be labeled by colors
type_num=12 # Assuming there are 12 known types in the sample set
_cmp=cm.get_cmap("bwr", type_num) # Setting 12 different colors
_cmp2=cm.get_cmap("hot", type_num) # Setting another 12 different colors
colors_dict={"example_color":_cmp(np.random.rand(numleaf)), # RGB color list. the order of colors must be same as the original sample order.
"example_color2":_cmp2(np.random.rand(numleaf))} # Another RGB color list.#optionally, specify the legend of the color labels.
colors_legends={"example_color":{"colors":_cmp(np.linspace(0, 1, type_num)),
"labels": ["ex1_"+str(i+1) for i in range(type_num)]},
"example_color2":{"colors":_cmp2(np.linspace(0, 1, type_num)),
"labels": ["ex2_"+str(i+1) for i in range(type_num)]}}
# Compute and plot the dendrogram.Y = sch.linkage(D, method='single')
Z2 = sch.dendrogram(Y,labels=labels,no_plot=True)
rt.plot(Z2, colorlabels=colors_dict,colorlabels_legend=colors_legends)
```
## Example usage 3 (adding color labels to the tree automatically (rather simpler than Example 2).)
```python
import scipy.cluster.hierarchy as sch
import numpy as np
import radialtree as rt
np.random.seed(1)numleaf=200
_alphabets=[chr(i) for i in range(97, 97+24)]
labels=sorted(["".join(list(np.random.choice(_alphabets, 10))) for i in range(numleaf)])
x = np.random.rand(numleaf)
D = np.zeros([numleaf,numleaf])
for i in range(numleaf):
for j in range(numleaf):
D[i,j] = abs(x[i] - x[j])
Y = sch.linkage(D, method='single')
Z2 = sch.dendrogram(Y,labels=labels,no_plot=True)
type_num=6
type_list=["ex"+str(i) for i in range(type_num)]
sample_classes={"example_color": [np.random.choice(type_list) for i in range(numleaf)]}
rt.plot(Z2, sample_classes=sample_classes)
```## Example usage 4 (Emebedding the radial tree in a figure)
```python
import scipy.cluster.hierarchy as sch
import numpy as np
import radialtree as rt
from seaborn import load_dataset
import matplotlib.pyplot as plt#get a simple dataset
iris = load_dataset("iris")
species = iris.pop("species")fig, axes = plt.subplots(2, 1, figsize=(10, 10))
# Compute and plot the dendrogram.
Y = sch.linkage(np.asarray(iris), method="average")
Z2 = sch.dendrogram(
Y,
# no_plot=True,
ax=axes[0],
color_threshold=1.0,
)axes[1].set_aspect(1)
# plot a circular dendrogram
rt.radialTreee(Z2, ax=axes[1], sample_classes={"species": species})
```