egrace479 commited on
Commit
f96af13
·
1 Parent(s): 391a60d

Add notebook to generate visuals plus paired py file for diff record.

Browse files
notebooks/BioCLIP_taxa_viz.ipynb ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bf708489236ae18c886a7485df8627ad24b237a9bd61b4e0ee28a6500b403676
3
+ size 237174723
notebooks/BioCLIP_taxa_viz.py ADDED
@@ -0,0 +1,184 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ---
2
+ # jupyter:
3
+ # jupytext:
4
+ # formats: ipynb,py:percent
5
+ # text_representation:
6
+ # extension: .py
7
+ # format_name: percent
8
+ # format_version: '1.3'
9
+ # jupytext_version: 1.15.2
10
+ # kernelspec:
11
+ # display_name: viz
12
+ # language: python
13
+ # name: python3
14
+ # ---
15
+
16
+ # %%
17
+ import pandas as pd
18
+ import seaborn as sns
19
+ import plotly.express as px
20
+
21
+ sns.set_style("whitegrid")
22
+ sns.set(rc = {'figure.figsize': (10,10)})
23
+
24
+ # %% [markdown]
25
+ # # Number of Images by Taxonomic Rank
26
+
27
+ # %%
28
+ df = pd.read_csv("../data/catalog.csv")
29
+
30
+ # %%
31
+ # Add data_source column for easier slicing
32
+ df.loc[df['inat21_filename'].notna(), 'data_source'] = 'iNat21'
33
+ df.loc[df['bioscan_filename'].notna(), 'data_source'] = 'BIOSCAN'
34
+ df.loc[df['eol_content_id'].notna(), 'data_source'] = 'EOL'
35
+
36
+ # %%
37
+ taxa = list(df.columns[8:15])
38
+ taxa
39
+
40
+ # %% [markdown]
41
+ # Shrink down to just columns we may want for graphing.
42
+
43
+ # %%
44
+ columns = taxa.copy()
45
+ columns.insert(0, 'data_source')
46
+ columns.append('common')
47
+
48
+ # %%
49
+ df_taxa = df[columns]
50
+ df_taxa.head()
51
+
52
+ # %% [markdown]
53
+ # 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.
54
+
55
+ # %%
56
+ # Drop null phylum values
57
+ df_phylum = df_taxa.loc[df_taxa.phylum.notna()]
58
+
59
+ # %%
60
+ # Fill null lower ranks with 'unknown' for graphing purposes
61
+ df_phylum = df_phylum.fillna('unknown')
62
+
63
+ # %% [markdown]
64
+ # 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.
65
+
66
+ # %%
67
+ phyla = list(df_phylum.phylum.unique())
68
+ colors = px.colors.qualitative.Bold
69
+
70
+ # %%
71
+ color_map = {}
72
+ i = 0
73
+ for phylum in phyla:
74
+ # There are only 10 colors in the sequence, so we'll need to loop through it a few times to assign all 49 phyla
75
+ i = i%10
76
+ color_map[phylum] = colors[i]
77
+ i += 1
78
+
79
+ # %%
80
+ # Distribution of Phyla and Lower Taxa (to family) in TreeOfLife10M
81
+ # Minimize margins, set aspect ratio to 2:1
82
+
83
+ fig_phyla = px.treemap(df_phylum, path = ['phylum', 'class', 'order', 'family'],
84
+ color = 'phylum',
85
+ color_discrete_map = color_map)
86
+
87
+ fig_phyla.update_scenes(aspectratio = {'x': 2, 'y': 1})
88
+ fig_phyla.update_layout(font = {'size': 14},
89
+ margin = {
90
+ 'l': 0,
91
+ 'r': 0,
92
+ 't': 0,
93
+ 'b': 0
94
+ })
95
+ fig_phyla.show()
96
+
97
+ # %%
98
+ fig_phyla.write_html("../visuals/phyla_ToL_tree.html")
99
+
100
+ # %% [markdown]
101
+ # 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.
102
+
103
+ # %%
104
+ fig_phyla.write_image("../visuals/phyla_ToL_tree.pdf", width = 900, height = 450)
105
+
106
+ # %% [markdown]
107
+ # ## Images by Kingdom
108
+
109
+ # %%
110
+ df_kingdom = df_taxa.loc[df_taxa.kingdom.notna()]
111
+ df_kingdom.head()
112
+
113
+ # %%
114
+ # Drop null kingdom values
115
+ df_kingdom = df_taxa.loc[df_taxa.kingdom.notna()]
116
+
117
+ # %%
118
+ # Fill null lower ranks with 'unknown' for graphing purposes
119
+ df_kingdom = df_kingdom.fillna('unknown')
120
+
121
+ # %% [markdown]
122
+ # 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.
123
+
124
+ # %%
125
+ kingdoms = list(df_kingdom.kingdom.unique())
126
+ #colors = px.colors.qualitative.Bold
127
+
128
+ # %%
129
+ king_color_map = {}
130
+ i = 0
131
+ for kingdom in kingdoms:
132
+ # There are only 10 colors in the sequence, so we'll need to loop through it once to assign all 12 kingdoms
133
+ i = i%10
134
+ king_color_map[kingdom] = colors[i]
135
+ i += 1
136
+
137
+ # %%
138
+ # Distribution of Kingdoms and Lower Taxa in TreeOfLife10M
139
+ # Minimize margins, set aspect ratio to 2:1
140
+
141
+ fig_king = px.treemap(df_kingdom, path = ['kingdom', 'phylum', 'class', 'order', 'family'],
142
+ color = 'kingdom',
143
+ color_discrete_map = king_color_map)
144
+
145
+ fig_king.update_scenes(aspectratio = {'x': 2, 'y': 1})
146
+ fig_king.update_layout(font = {'size': 14},
147
+ margin = {
148
+ 'l': 0,
149
+ 'r': 0,
150
+ 't': 0,
151
+ 'b': 0
152
+ })
153
+ fig_king.show()
154
+
155
+ # %%
156
+ fig_king.write_html("../visuals/kingdom_ToL_tree.html")
157
+
158
+ # %% [markdown]
159
+ # 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.
160
+
161
+ # %%
162
+ fig_king.write_image("../visuals/kingdom_ToL_tree.pdf", width = 900, height = 450)
163
+
164
+ # %% [markdown]
165
+ # ### Histograms for Kingdom
166
+
167
+ # %%
168
+ fig = px.histogram(df_kingdom,
169
+ x = 'kingdom',
170
+ #y = 'num_species',
171
+ color = 'kingdom',
172
+ color_discrete_sequence = px.colors.qualitative.Bold,
173
+ labels = {
174
+ 'kingdom': "Kingdom",
175
+ #'num_phyla' : "Number of distinct species"
176
+ },
177
+ #text_auto=False
178
+ )
179
+ fig.update_layout(title = "Number of Images by Kingdom",
180
+ yaxis_title = "Number of Images")
181
+
182
+ fig.show()
183
+
184
+ # %%