Add analysis of updated dataset (statistics.csv) and paired py file for clearer diffs.
Browse files- notebooks/ToL_stats_EDA.ipynb +0 -0
- notebooks/ToL_stats_EDA.py +534 -0
notebooks/ToL_stats_EDA.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
notebooks/ToL_stats_EDA.py
ADDED
|
@@ -0,0 +1,534 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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: Python 3 (ipykernel)
|
| 12 |
+
# language: python
|
| 13 |
+
# name: python3
|
| 14 |
+
# ---
|
| 15 |
+
|
| 16 |
+
# %%
|
| 17 |
+
import pandas as pd
|
| 18 |
+
import seaborn as sns
|
| 19 |
+
|
| 20 |
+
sns.set_style("whitegrid")
|
| 21 |
+
sns.set(rc = {'figure.figsize': (10,10)})
|
| 22 |
+
|
| 23 |
+
# %%
|
| 24 |
+
df = pd.read_csv("../data/statistics.csv")
|
| 25 |
+
|
| 26 |
+
# %%
|
| 27 |
+
df.head()
|
| 28 |
+
|
| 29 |
+
# %%
|
| 30 |
+
df.info(show_counts = True)
|
| 31 |
+
|
| 32 |
+
# %% [markdown]
|
| 33 |
+
# Original version had 10,436,521 entries; was the validation set left out (there does seem to be about 5-10% less of each source).
|
| 34 |
+
#
|
| 35 |
+
# Labeling definitely has far better coverage now.
|
| 36 |
+
|
| 37 |
+
# %%
|
| 38 |
+
df.nunique()
|
| 39 |
+
|
| 40 |
+
# %% [markdown]
|
| 41 |
+
# There are 498,053 unique EOL page IDs, suggesting 498,053 unique classes among the 5,938,235 images pulled from EOL (and maintained here). Presumably this would represent the number of species or other lowest rank taxa covered.
|
| 42 |
+
|
| 43 |
+
# %% [markdown]
|
| 44 |
+
# Notice that we have 7 unique kingdoms, when there are only...
|
| 45 |
+
|
| 46 |
+
# %%
|
| 47 |
+
df['kingdom'].value_counts()
|
| 48 |
+
|
| 49 |
+
# %% [markdown]
|
| 50 |
+
# `Metazoa` and `Archaeplastida` have been replaced by `Animalia` and `Plantae`.
|
| 51 |
+
#
|
| 52 |
+
# We now have other single-celled organisms for kingdom
|
| 53 |
+
|
| 54 |
+
# %%
|
| 55 |
+
taxa = list(df.columns[8:15])
|
| 56 |
+
taxa
|
| 57 |
+
|
| 58 |
+
# %% [markdown]
|
| 59 |
+
# Check the number of images with all 7 taxonomic labels.
|
| 60 |
+
|
| 61 |
+
# %%
|
| 62 |
+
df_all_taxa = df.dropna(subset = taxa)
|
| 63 |
+
df_all_taxa[taxa].info(show_counts = True)
|
| 64 |
+
|
| 65 |
+
# %% [markdown]
|
| 66 |
+
# We have 7,901,259 images with full taxonomic labels.
|
| 67 |
+
#
|
| 68 |
+
# Notice that we have gaps in the taxonomic hierarchy (both higher and lower values), as this is less than the number of species labels in the dataset and species had the least non-null values.
|
| 69 |
+
#
|
| 70 |
+
# We did hit the 7M+ entries with full taxonomic labels expectation, so that's good.
|
| 71 |
+
|
| 72 |
+
# %% [markdown]
|
| 73 |
+
# More detail on these missing values from [`check_taxa` script](https://github.com/Imageomics/open_clip/blob/main/scripts/evobio10m/check_taxa.py)
|
| 74 |
+
#
|
| 75 |
+
# ```
|
| 76 |
+
# [2023-10-25 10:25:28,242] [WARNING] [root] There are 7 kingdoms instead of 3.
|
| 77 |
+
# [2023-10-25 10:25:29,234] [WARNING] [root] 14795 entries are missing rank kingdom, but have genus label.
|
| 78 |
+
# [2023-10-25 10:25:29,477] [WARNING] [root] 5824 entries are missing rank phylum, but have genus label.
|
| 79 |
+
# [2023-10-25 10:25:29,719] [WARNING] [root] 15763 entries are missing rank class, but have genus label.
|
| 80 |
+
# [2023-10-25 10:25:29,958] [WARNING] [root] 10550 entries are missing rank order, but have genus label.
|
| 81 |
+
# [2023-10-25 10:25:30,203] [WARNING] [root] 7539 entries are missing rank family, but have genus label.
|
| 82 |
+
# [2023-10-25 10:25:30,470] [WARNING] [root] 296 entries are missing rank kingdom, but have family label.
|
| 83 |
+
# [2023-10-25 10:25:30,495] [WARNING] [root] 272 entries are missing rank phylum, but have family label.
|
| 84 |
+
# [2023-10-25 10:25:30,520] [WARNING] [root] 753 entries are missing rank class, but have family label.
|
| 85 |
+
# [2023-10-25 10:25:30,545] [WARNING] [root] 395 entries are missing rank order, but have family label.
|
| 86 |
+
# [2023-10-25 10:25:30,590] [WARNING] [root] 156 entries are missing rank kingdom, but have order label.
|
| 87 |
+
# [2023-10-25 10:25:30,591] [WARNING] [root] 100 entries are missing rank phylum, but have order label.
|
| 88 |
+
# [2023-10-25 10:25:30,592] [WARNING] [root] 1187 entries are missing rank class, but have order label.
|
| 89 |
+
# [2023-10-25 10:25:30,637] [WARNING] [root] 74 entries have kingdom and species labels but no genus.
|
| 90 |
+
# [2023-10-25 10:25:30,644] [WARNING] [root] 74 entries have phylum and species labels but no genus.
|
| 91 |
+
# [2023-10-25 10:25:30,650] [WARNING] [root] 74 entries have class and species labels but no genus.
|
| 92 |
+
# [2023-10-25 10:25:30,656] [WARNING] [root] 74 entries have order and species labels but no genus.
|
| 93 |
+
# [2023-10-25 10:25:30,662] [WARNING] [root] 74 entries have family and species labels but no genus.
|
| 94 |
+
# [2023-10-25 10:25:31,992] [WARNING] [root] There are 158279 samples for which the species column may have genus and species.
|
| 95 |
+
# ```
|
| 96 |
+
|
| 97 |
+
# %% [markdown]
|
| 98 |
+
# Can we get some more information on those 74 entries that are missing genus?
|
| 99 |
+
|
| 100 |
+
# %%
|
| 101 |
+
missing_genus = df.dropna(subset = ['kingdom', 'phylum', 'class', 'order', 'family', 'species'])
|
| 102 |
+
missing_genus = missing_genus.loc[missing_genus.genus.isna()]
|
| 103 |
+
missing_genus[taxa].nunique()
|
| 104 |
+
|
| 105 |
+
# %% [markdown]
|
| 106 |
+
# So it's a handful of taxa, let's take a look.
|
| 107 |
+
|
| 108 |
+
# %%
|
| 109 |
+
print("The taxa missing genus are: ")
|
| 110 |
+
for taxon in taxa:
|
| 111 |
+
print(taxon, ": ", missing_genus[taxon].unique())
|
| 112 |
+
|
| 113 |
+
# %% [markdown]
|
| 114 |
+
# Let's add a column indicating the original data source so we can also get some stats by datasource.
|
| 115 |
+
|
| 116 |
+
# %%
|
| 117 |
+
# Add data_source column for easier slicing
|
| 118 |
+
df.loc[df['inat21_filename'].notna(), 'data_source'] = 'iNat21'
|
| 119 |
+
df.loc[df['bioscan_filename'].notna(), 'data_source'] = 'BIOSCAN'
|
| 120 |
+
df.loc[df['eol_content_id'].notna(), 'data_source'] = 'EOL'
|
| 121 |
+
|
| 122 |
+
# %%
|
| 123 |
+
df.loc[df['common'].isna(), 'data_source'].unique()
|
| 124 |
+
|
| 125 |
+
# %%
|
| 126 |
+
missing_genus.data_source.unique()
|
| 127 |
+
|
| 128 |
+
# %% [markdown]
|
| 129 |
+
# Missing genus with all other taxa occurs in both BIOSCAN and EOL.
|
| 130 |
+
|
| 131 |
+
# %% [markdown]
|
| 132 |
+
# First, check their unique class values (`common`).
|
| 133 |
+
|
| 134 |
+
# %%
|
| 135 |
+
df.loc[df['data_source'] == 'EOL', 'common'].nunique()
|
| 136 |
+
|
| 137 |
+
# %%
|
| 138 |
+
df.loc[df['data_source'] == 'iNat21', 'common'].nunique()
|
| 139 |
+
|
| 140 |
+
# %%
|
| 141 |
+
df.loc[df['data_source'] == 'BIOSCAN', 'common'].nunique()
|
| 142 |
+
|
| 143 |
+
# %% [markdown]
|
| 144 |
+
# iNat's number of unique values in `common` has gone up by 6...why?
|
| 145 |
+
#
|
| 146 |
+
# BIOSCAN and EOL's counts went down, as expected (this is just training set and the common mapping was not done if scientific name was not provided, so we wouldn't see many for BIOSCAN).
|
| 147 |
+
|
| 148 |
+
# %% [markdown]
|
| 149 |
+
# Make `df_taxa` with just taxa columns (+ `common` & `data_source`) so it's smaller to process faster.
|
| 150 |
+
|
| 151 |
+
# %%
|
| 152 |
+
taxa_com = list(df.columns[8:16]) # taxa + common
|
| 153 |
+
taxa_com.insert(0, 'data_source')
|
| 154 |
+
df_taxa = df[taxa_com]
|
| 155 |
+
|
| 156 |
+
# %%
|
| 157 |
+
df_taxa.head()
|
| 158 |
+
|
| 159 |
+
# %% [markdown]
|
| 160 |
+
# Let's look a little closer at each of our three data sources.
|
| 161 |
+
|
| 162 |
+
# %%
|
| 163 |
+
inat21_df = df_taxa.loc[df_taxa.data_source == 'iNat21']
|
| 164 |
+
bioscan_df = df_taxa.loc[df_taxa.data_source == 'BIOSCAN']
|
| 165 |
+
eol_df = df_taxa.loc[df_taxa.data_source == 'EOL']
|
| 166 |
+
|
| 167 |
+
# %% [markdown]
|
| 168 |
+
# ### iNat21
|
| 169 |
+
|
| 170 |
+
# %%
|
| 171 |
+
inat21_df.info(show_counts = True)
|
| 172 |
+
|
| 173 |
+
# %% [markdown]
|
| 174 |
+
# iNat21 isn't missing anything, as expected, and we have 2,552,501 of 2,686,843 images (training, validation set is separate).
|
| 175 |
+
#
|
| 176 |
+
# Quick view of diversity in iNat21.
|
| 177 |
+
|
| 178 |
+
# %%
|
| 179 |
+
inat21_df.nunique()
|
| 180 |
+
|
| 181 |
+
# %% [markdown]
|
| 182 |
+
# Again, 6 more common values were added, but the diversity has not been altered (same numbers of unique values).
|
| 183 |
+
|
| 184 |
+
# %%
|
| 185 |
+
inat21_df['kingdom'].value_counts()
|
| 186 |
+
|
| 187 |
+
# %% [markdown]
|
| 188 |
+
# iNat21 uses `Animalia` and `Plantae`.
|
| 189 |
+
|
| 190 |
+
# %%
|
| 191 |
+
#number of unique 7-tuples in iNat21
|
| 192 |
+
inat21_df['duplicate'] = inat21_df.duplicated(subset = taxa, keep = 'first')
|
| 193 |
+
inat21_df_unique_taxa = inat21_df.loc[~inat21_df['duplicate']]
|
| 194 |
+
|
| 195 |
+
# %%
|
| 196 |
+
inat21_df_unique_taxa.info(show_counts = True)
|
| 197 |
+
|
| 198 |
+
# %% [markdown]
|
| 199 |
+
# There's the 10K unique `species` that we expect. Let's check the same information across BIOSCAN and EOL.
|
| 200 |
+
|
| 201 |
+
# %% [markdown]
|
| 202 |
+
# ### BIOSCAN
|
| 203 |
+
|
| 204 |
+
# %%
|
| 205 |
+
bioscan_df.info(show_counts = True)
|
| 206 |
+
|
| 207 |
+
# %% [markdown]
|
| 208 |
+
# All images are labeled down to `order`, most (98.6%) are labeled to the `family` level, but we only have 22.5% and 7.5% with `genus` or `species` level designation, respectively. These proportions were maintained when the validation set was removed.
|
| 209 |
+
|
| 210 |
+
# %%
|
| 211 |
+
bioscan_df.nunique()
|
| 212 |
+
|
| 213 |
+
# %% [markdown]
|
| 214 |
+
# We did lose 2 families, 37 genera, and 1,354 species to the validation set. Note, they may be (likely are) represented in EOL and/or iNat.
|
| 215 |
+
|
| 216 |
+
# %%
|
| 217 |
+
bioscan_df['kingdom'].value_counts()
|
| 218 |
+
|
| 219 |
+
# %% [markdown]
|
| 220 |
+
# BIOSCAN is all `Animalia`, as expected.
|
| 221 |
+
|
| 222 |
+
# %% [markdown]
|
| 223 |
+
# Check we're not missing `family` designation when we have `genus`.
|
| 224 |
+
|
| 225 |
+
# %%
|
| 226 |
+
bioscan_df.loc[bioscan_df['genus'].notna()].info()
|
| 227 |
+
|
| 228 |
+
# %%
|
| 229 |
+
bioscan_df.loc[bioscan_df['genus'].notna()].sample(7)
|
| 230 |
+
|
| 231 |
+
# %% [markdown]
|
| 232 |
+
# We should not have instances where `common` is labeled as `Genus genus species` this time.
|
| 233 |
+
|
| 234 |
+
# %%
|
| 235 |
+
bioscan_df.loc[bioscan_df['genus'].isna()].sample(7)
|
| 236 |
+
|
| 237 |
+
# %% [markdown]
|
| 238 |
+
# When the `genus` is null, we no longer get `common` of all higher order taxa available; though we are going to go back to this with the next iteration it seems.
|
| 239 |
+
|
| 240 |
+
# %%
|
| 241 |
+
#number of unique 7-tuples in BIOSCAN
|
| 242 |
+
bioscan_df['duplicate'] = bioscan_df.duplicated(subset = taxa, keep = 'first')
|
| 243 |
+
bioscan_df_unique_taxa = bioscan_df.loc[~bioscan_df['duplicate']]
|
| 244 |
+
|
| 245 |
+
# %%
|
| 246 |
+
bioscan_df_unique_taxa.info(show_counts = True)
|
| 247 |
+
|
| 248 |
+
# %% [markdown]
|
| 249 |
+
# We should be able to fill all in for all values of `species` that also have `genus` indicated since they are all in `Animalia`. Is `genus` labeled for all entries with `species` labeled?
|
| 250 |
+
|
| 251 |
+
# %%
|
| 252 |
+
bioscan_df.loc[bioscan_df.species.notna()].info(show_counts = True)
|
| 253 |
+
|
| 254 |
+
# %% [markdown]
|
| 255 |
+
# There are only 8 images where the `species` is labeled, but the `genus` isn't. There must be 1 in the validation set.
|
| 256 |
+
|
| 257 |
+
# %%
|
| 258 |
+
bioscan_df.loc[(bioscan_df.species.notna()) & (bioscan_df.genus.isna())]
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
# %% [markdown]
|
| 262 |
+
# It seems the `genus` is `tetramalaise01` ([genus page for tetraMalaise01](https://v3.boldsystems.org/index.php/Taxbrowser_Taxonpage?taxid=1074204)).
|
| 263 |
+
|
| 264 |
+
# %% [markdown]
|
| 265 |
+
# In general, when the species is listed in BIOSCAN it is listed as `genus-species`. This is certainly true here and would work to fill the 9 missing genera.
|
| 266 |
+
#
|
| 267 |
+
# Let's check those stats again, we did confirm it earlier, but would also like to check EOL as it had a similar issue for some entries.
|
| 268 |
+
|
| 269 |
+
# %%
|
| 270 |
+
def check_sci_name(df):
|
| 271 |
+
"""
|
| 272 |
+
This function checks the number of words in the species column for each sample.
|
| 273 |
+
Logs a warning with the number that have more than one word indicating the potential for both genus and species to be recorded.
|
| 274 |
+
Warning is printed to terminal, not saved to file.
|
| 275 |
+
|
| 276 |
+
Parameters:
|
| 277 |
+
-----------
|
| 278 |
+
df - DataFrame with taxonomic hierarchy as columns.
|
| 279 |
+
|
| 280 |
+
Returns:
|
| 281 |
+
--------
|
| 282 |
+
df - DataFrame with taxonomic hierarchy and length of species entry as columns.
|
| 283 |
+
"""
|
| 284 |
+
# Set length of species column with default = 1
|
| 285 |
+
df["len_species"] = 1
|
| 286 |
+
|
| 287 |
+
# Check for scientific name in species column (i.e., genus speices in species column, may correspond to missing genus)
|
| 288 |
+
for species in list(df.loc[df["species"].notna(), "species"].unique()):
|
| 289 |
+
len_species = len(species.split(" "))
|
| 290 |
+
if len_species > 1:
|
| 291 |
+
df.loc[df["species"] == species, "len_species"] = len_species
|
| 292 |
+
|
| 293 |
+
return df
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
|
| 297 |
+
# %%
|
| 298 |
+
bioscan_species_len_df = check_sci_name(bioscan_df)
|
| 299 |
+
|
| 300 |
+
# %%
|
| 301 |
+
bioscan_species_len_df.info(show_counts = True)
|
| 302 |
+
|
| 303 |
+
# %%
|
| 304 |
+
bioscan_long_species = bioscan_species_len_df.loc[bioscan_species_len_df["len_species"] > 1]
|
| 305 |
+
bioscan_long_species.info(show_counts = True)
|
| 306 |
+
|
| 307 |
+
# %% [markdown]
|
| 308 |
+
# Not all species indicated have length greater than 1 now.
|
| 309 |
+
|
| 310 |
+
# %%
|
| 311 |
+
bioscan_species_len_df.len_species.value_counts()
|
| 312 |
+
|
| 313 |
+
# %% [markdown]
|
| 314 |
+
# It seems the genus may have been removed, but the remaining string retained as they previously ranged from 2 to 5 "words" long.
|
| 315 |
+
|
| 316 |
+
# %%
|
| 317 |
+
bioscan_long_species.sample(7)
|
| 318 |
+
|
| 319 |
+
# %% [markdown]
|
| 320 |
+
# Ah but the whole species indicator was moved into genus for some of these.
|
| 321 |
+
|
| 322 |
+
# %%
|
| 323 |
+
bioscan_long_species.loc[bioscan_long_species['len_species'] > 2].sample(10)
|
| 324 |
+
|
| 325 |
+
# %%
|
| 326 |
+
bioscan_df.loc[bioscan_df["genus"] == "Psychoda", "species"].unique()
|
| 327 |
+
|
| 328 |
+
# %% [markdown]
|
| 329 |
+
# We can see that the case of "Psychoda" was resolved appropriately.
|
| 330 |
+
|
| 331 |
+
# %% [markdown]
|
| 332 |
+
# It would seem the "sp. ..." is used to indicate they are likely the same species, though the actual species is not designated (either unnamed or undetermined). This is information we should retain, but the duplicated genus can be removed.
|
| 333 |
+
|
| 334 |
+
# %% [markdown]
|
| 335 |
+
# ### EOL
|
| 336 |
+
|
| 337 |
+
# %%
|
| 338 |
+
eol_df.info(show_counts = True)
|
| 339 |
+
|
| 340 |
+
# %%
|
| 341 |
+
eol_df.nunique()
|
| 342 |
+
|
| 343 |
+
# %%
|
| 344 |
+
eol_df.loc[eol_df.species.isna()].nunique()
|
| 345 |
+
|
| 346 |
+
# %% [markdown]
|
| 347 |
+
# There are 498,053 (training + val 570,515) unique page IDs from EOL in this training set, which clearly represent varying levels of taxa.
|
| 348 |
+
#
|
| 349 |
+
# Unique species + unique common where species is null (197,301) does not reach this number.
|
| 350 |
+
|
| 351 |
+
# %%
|
| 352 |
+
eol_df['kingdom'].value_counts()
|
| 353 |
+
|
| 354 |
+
# %% [markdown]
|
| 355 |
+
# We have much greater kingdom variety here.
|
| 356 |
+
#
|
| 357 |
+
# We have already observed that not all ranks are filled in at the higher levels, sometimes having just one gap. This has been greatly improved from the first iteration.
|
| 358 |
+
|
| 359 |
+
# %%
|
| 360 |
+
#number of unique 7-tuples in EOL
|
| 361 |
+
eol_df['duplicate'] = eol_df.duplicated(subset = taxa, keep = 'first')
|
| 362 |
+
eol_df_unique_taxa = eol_df.loc[~eol_df['duplicate']]
|
| 363 |
+
|
| 364 |
+
# %%
|
| 365 |
+
eol_df_unique_taxa.info(show_counts = True)
|
| 366 |
+
|
| 367 |
+
# %% [markdown]
|
| 368 |
+
# The actual number of distinct creatures is likely higher, as evidenced below where we have unique common names listed in `common` but all taxa are null.
|
| 369 |
+
#
|
| 370 |
+
# We should be able to fill all in for all values of `species` that also have `genus` indicated. Is `genus` labeled for all entries with `species` labeled?
|
| 371 |
+
|
| 372 |
+
# %%
|
| 373 |
+
eol_df.loc[eol_df.species.notna()].info(show_counts = True)
|
| 374 |
+
|
| 375 |
+
# %% [markdown]
|
| 376 |
+
# Not all labeled species have higher order taxa, but they do all have `common` (likely because if there is no common name, it pulls scientific name (genus-species)).
|
| 377 |
+
|
| 378 |
+
# %% [markdown]
|
| 379 |
+
# Let's get a quick sample of the `common` column for images both with and without `species` labels.
|
| 380 |
+
|
| 381 |
+
# %%
|
| 382 |
+
# existing species label
|
| 383 |
+
eol_df.loc[eol_df.species.notna()].sample(7)
|
| 384 |
+
|
| 385 |
+
# %% [markdown]
|
| 386 |
+
# Good, some of these do have common names. We could check numbers of `common` where it doesn't match the `genus-species` form for a better count, but our random sample is promising.
|
| 387 |
+
|
| 388 |
+
# %%
|
| 389 |
+
# No species label
|
| 390 |
+
eol_df.loc[eol_df.species.isna()].sample(7)
|
| 391 |
+
|
| 392 |
+
# %% [markdown]
|
| 393 |
+
# Here are a lot of higher-order taxa missing species...let's check for the details noted below.
|
| 394 |
+
#
|
| 395 |
+
# From original check:
|
| 396 |
+
# All `common` values are filled in with predominently common names. It seems we could map these back to some level of taxa with the taxon-common matching? Though why would these EOL images have common without any other designation?
|
| 397 |
+
#
|
| 398 |
+
# Good example of strange inconsistency: the `American red raspberry` is `Rubus strigosus`, and a quick Google search easily provides the entire taxonomy.
|
| 399 |
+
#
|
| 400 |
+
# `Cremastobaeus` seems to be a genus of wasp.
|
| 401 |
+
|
| 402 |
+
# %%
|
| 403 |
+
eol_df.loc[eol_df.common == "American red raspberry"]
|
| 404 |
+
|
| 405 |
+
# %%
|
| 406 |
+
eol_df.loc[eol_df.common == "Rubus strigosus"]
|
| 407 |
+
|
| 408 |
+
# %% [markdown]
|
| 409 |
+
# We seem to have lost the raspberry...
|
| 410 |
+
|
| 411 |
+
# %%
|
| 412 |
+
eol_df.loc[eol_df.species == "strigosus"]
|
| 413 |
+
|
| 414 |
+
# %%
|
| 415 |
+
eol_df_unique_taxa.loc[eol_df_unique_taxa.species == "strigosus"]
|
| 416 |
+
|
| 417 |
+
# %% [markdown]
|
| 418 |
+
# It seems likely the row with the null values is our lost raspberry.
|
| 419 |
+
#
|
| 420 |
+
# Also good evidence of the duplication of species names across kingdoms and other taxa.
|
| 421 |
+
|
| 422 |
+
# %% [markdown]
|
| 423 |
+
# Rubus strigosus is apparently a subspecies of "Rubus idaeus", and we are not looking at the subspecies level.
|
| 424 |
+
|
| 425 |
+
# %%
|
| 426 |
+
eol_df_unique_taxa.loc[eol_df_unique_taxa.species == "idaeus"]
|
| 427 |
+
|
| 428 |
+
# %% [markdown]
|
| 429 |
+
# Though the question remains, why did we lose the common name?
|
| 430 |
+
|
| 431 |
+
# %% [markdown]
|
| 432 |
+
# Let's check the `species` length in EOL as well, we know there are some that have genus-species.
|
| 433 |
+
|
| 434 |
+
# %%
|
| 435 |
+
eol_species_len = check_sci_name(eol_df)
|
| 436 |
+
eol_species_len.info(show_counts = True)
|
| 437 |
+
|
| 438 |
+
# %%
|
| 439 |
+
eol_long_species = eol_species_len.loc[eol_species_len["len_species"] > 1]
|
| 440 |
+
eol_long_species.info(show_counts = True)
|
| 441 |
+
|
| 442 |
+
# %% [markdown]
|
| 443 |
+
# That's quite a lot with species name longer than 1 word.
|
| 444 |
+
|
| 445 |
+
# %%
|
| 446 |
+
eol_long_species.len_species.value_counts()
|
| 447 |
+
|
| 448 |
+
# %% [markdown]
|
| 449 |
+
# Observe, we did not wind up with any of length 2, so this issue was partially resolved.
|
| 450 |
+
#
|
| 451 |
+
# Why are some more than 10 words long?
|
| 452 |
+
|
| 453 |
+
# %%
|
| 454 |
+
eol_long_species.loc[eol_long_species["len_species"] > 7].sample(7)
|
| 455 |
+
|
| 456 |
+
# %%
|
| 457 |
+
eol_long_species.nunique()
|
| 458 |
+
|
| 459 |
+
# %%
|
| 460 |
+
eol_long_species.loc[eol_long_species["len_species"] < 7].sample(7)
|
| 461 |
+
|
| 462 |
+
# %% [markdown]
|
| 463 |
+
# Quick search: "Adelpha godmani" is a butterfly with species name given by Fruhstorfer in 1913 ([source](https://butterfliesofamerica.com/L/t/Adelpha_godmani_a.htm)).
|
| 464 |
+
|
| 465 |
+
# %% [markdown]
|
| 466 |
+
# ### Label Overlap Check
|
| 467 |
+
|
| 468 |
+
# %% [markdown]
|
| 469 |
+
# Checking for overlap between the three data sources should give pretty good results, now that most inconsistencies have been addressed.
|
| 470 |
+
#
|
| 471 |
+
# For now, let's just take a quick look at genera across the datasets since they are more standardized (and listed more often in BIOSCAN).
|
| 472 |
+
|
| 473 |
+
# %%
|
| 474 |
+
eol_genera = list(eol_df.loc[eol_df['genus'].notna(), 'genus'].unique())
|
| 475 |
+
inat21_genera = list(inat21_df.loc[inat21_df['genus'].notna(), 'genus'].unique())
|
| 476 |
+
bioscan_genera = list(bioscan_df.loc[bioscan_df['genus'].notna(), 'genus'].unique())
|
| 477 |
+
|
| 478 |
+
print(f"there are {len(eol_genera)} genera in EOL")
|
| 479 |
+
print(f"there are {len(inat21_genera)} genera in inat21")
|
| 480 |
+
print(f"there are {len(bioscan_genera)} genera in bioscan")
|
| 481 |
+
|
| 482 |
+
# %%
|
| 483 |
+
gen_overlap = list(set(eol_genera) & set(inat21_genera))
|
| 484 |
+
print(f"There are {len(gen_overlap)} genera shared between EOL and iNat21.")
|
| 485 |
+
print(f"There are {len(list(set(eol_genera) & set(bioscan_genera)))} genera shared between EOL and BIOSCAN.")
|
| 486 |
+
print(f"There are {len(list(set(inat21_genera) & set(bioscan_genera)))} genera shared between iNat21 and BIOSCAN.")
|
| 487 |
+
print(f"There are {len(list(set(gen_overlap) & set(bioscan_genera)))} genera shared between all three data sources.")
|
| 488 |
+
|
| 489 |
+
# %% [markdown]
|
| 490 |
+
# BIOSCAN and iNat21's overlap of genera is completely contained in EOL.
|
| 491 |
+
|
| 492 |
+
# %% [markdown]
|
| 493 |
+
# ## Overall Stats
|
| 494 |
+
#
|
| 495 |
+
# Keep in mind, this is without fixing remaining inconsistencies observed above.
|
| 496 |
+
|
| 497 |
+
# %%
|
| 498 |
+
import numpy as np
|
| 499 |
+
|
| 500 |
+
# %%
|
| 501 |
+
avgs_all_images = []
|
| 502 |
+
std_all_images = []
|
| 503 |
+
avgs_labeled_images = []
|
| 504 |
+
std_labeled_images = []
|
| 505 |
+
for taxon in taxa_com[1:]: #taxa + common
|
| 506 |
+
num_taxon = df[taxon].nunique()
|
| 507 |
+
num_img_taxon = len(df.loc[df[taxon].notna()])
|
| 508 |
+
avg_all = 10436521/num_taxon
|
| 509 |
+
std_all = np.sqrt(10436521/num_taxon)
|
| 510 |
+
avg_labeled = num_img_taxon/num_taxon
|
| 511 |
+
std_labeled = np.sqrt(num_img_taxon/num_taxon)
|
| 512 |
+
avgs_all_images.append(avg_all)
|
| 513 |
+
std_all_images.append(std_all)
|
| 514 |
+
avgs_labeled_images.append(avg_labeled)
|
| 515 |
+
std_labeled_images.append(std_labeled)
|
| 516 |
+
|
| 517 |
+
# %%
|
| 518 |
+
avg_std = pd.DataFrame(data = {'class': taxa_com[1:], 'average_all_imgs': avgs_all_images, 'standard_deviation': std_all_images,
|
| 519 |
+
'avg_labeled': avgs_labeled_images, 'std_dev_labeled': std_labeled_images })
|
| 520 |
+
avg_std
|
| 521 |
+
|
| 522 |
+
# %%
|
| 523 |
+
avg_std.to_csv("../data/stats_avg_std_byClass.csv", index = False)
|
| 524 |
+
|
| 525 |
+
# %% [markdown]
|
| 526 |
+
# Observe that the Plant and Animal `kingdom`s are actually much more heavily represented than Fungi.
|
| 527 |
+
|
| 528 |
+
# %%
|
| 529 |
+
sns.set(rc = {'figure.figsize': (10,6)})
|
| 530 |
+
|
| 531 |
+
# %%
|
| 532 |
+
sns.histplot(df_taxa, x = 'kingdom')
|
| 533 |
+
|
| 534 |
+
# %%
|