ToL-EDA / notebooks /BioCLIP_taxa_viz.py
egrace479's picture
v3.3 update visualizations (#4)
ba08ee6
raw
history blame
4.94 kB
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:percent
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.15.2
# kernelspec:
# display_name: viz
# language: python
# name: python3
# ---
# %%
import pandas as pd
import seaborn as sns
import plotly.express as px
sns.set_style("whitegrid")
sns.set(rc = {'figure.figsize': (10,10)})
# %% [markdown]
# # Number of Images by Taxonomic Rank
# %%
df = pd.read_csv("../data/catalog.csv")
# %%
# Add data_source column for easier slicing
df.loc[df['inat21_filename'].notna(), 'data_source'] = 'iNat21'
df.loc[df['bioscan_filename'].notna(), 'data_source'] = 'BIOSCAN'
df.loc[df['eol_content_id'].notna(), 'data_source'] = 'EOL'
# %%
taxa = list(df.columns[9:16])
taxa
# %% [markdown]
# Shrink down to just columns we may want for graphing.
# %%
columns = taxa.copy()
columns.insert(0, 'data_source')
columns.append('common')
# %%
df_taxa = df[columns]
df_taxa.head()
# %% [markdown]
# Since the pie charts didn't show much change for phylum, let's try a treemap so we also get a sense of all the diversity inside.
# %%
# Drop null phylum values
df_phylum = df_taxa.loc[df_taxa.phylum.notna()]
# %%
# Fill null lower ranks with 'unknown' for graphing purposes
df_phylum = df_phylum.fillna('unknown')
# %% [markdown]
# Get list of all phyla and set color scheme. We'll then assign a color to each phylum so they're consistent across the two charts.
# %%
phyla = list(df_phylum.phylum.unique())
colors = px.colors.qualitative.Bold
# %%
color_map = {}
i = 0
for phylum in phyla:
# There are only 10 colors in the sequence, so we'll need to loop through it a few times to assign all 49 phyla
i = i%10
color_map[phylum] = colors[i]
i += 1
# %%
# Distribution of Phyla and Lower Taxa (to family) in TreeOfLife10M
# Minimize margins, set aspect ratio to 2:1
fig_phyla = px.treemap(df_phylum, path = ['phylum', 'class', 'order', 'family'],
color = 'phylum',
color_discrete_map = color_map)
fig_phyla.update_scenes(aspectratio = {'x': 2, 'y': 1})
fig_phyla.update_layout(font = {'size': 18},
margin = {
'l': 0,
'r': 0,
't': 0,
'b': 0
})
fig_phyla.show()
# %%
fig_phyla.write_html("../visuals/phyla_ToL_tree.html")
# %% [markdown]
# Aspect ratio set in the plot doesn't work for export (unless using the png export on the graph itself), so we'll set the size manually.
# %%
fig_phyla.write_image("../visuals/phyla_ToL_tree.pdf", width = 900, height = 450)
# %% [markdown]
# ## Images by Kingdom
# %%
df_kingdom = df_taxa.loc[df_taxa.kingdom.notna()]
df_kingdom.head()
# %%
# Drop null kingdom values
df_kingdom = df_taxa.loc[df_taxa.kingdom.notna()]
# %%
# Fill null lower ranks with 'unknown' for graphing purposes
df_kingdom = df_kingdom.fillna('unknown')
# %% [markdown]
# Get list of all kingdoms and set color scheme. We'll then assign a color to each kingdom so they're consistent across the two charts.
# %%
kingdoms = list(df_kingdom.kingdom.unique())
#colors = px.colors.qualitative.Bold
# %%
king_color_map = {}
i = 0
for kingdom in kingdoms:
# There are only 10 colors in the sequence, so we'll need to loop through it once to assign all 12 kingdoms
i = i%10
king_color_map[kingdom] = colors[i]
i += 1
# %%
# Distribution of Kingdoms and Lower Taxa in TreeOfLife10M
# Minimize margins, set aspect ratio to 2:1
fig_king = px.treemap(df_kingdom, path = ['kingdom', 'phylum', 'class', 'order', 'family'],
color = 'kingdom',
color_discrete_map = king_color_map)
fig_king.update_scenes(aspectratio = {'x': 2, 'y': 1})
fig_king.update_layout(font = {'size': 14},
margin = {
'l': 0,
'r': 0,
't': 0,
'b': 0
})
fig_king.show()
# %%
fig_king.write_html("../visuals/kingdom_ToL_tree.html")
# %% [markdown]
# Aspect ratio set in the plot doesn't work for export (unless using the png export on the graph itself), so we'll set the size manually.
# %%
fig_king.write_image("../visuals/kingdom_ToL_tree.pdf", width = 900, height = 450)
# %% [markdown]
# ### Histograms for Kingdom
# %%
fig = px.histogram(df_kingdom,
x = 'kingdom',
#y = 'num_species',
color = 'kingdom',
color_discrete_sequence = px.colors.qualitative.Bold,
labels = {
'kingdom': "Kingdom",
#'num_phyla' : "Number of distinct species"
},
#text_auto=False
)
fig.update_layout(title = "Number of Images by Kingdom",
yaxis_title = "Number of Images")
fig.show()
# %%