{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "RS_FILEPATH = \"../../rare_species/data/\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Read in Complete Rare Species Manifest" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "RangeIndex: 22146 entries, 0 to 22145\n", "Data columns (total 11 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 filename 22146 non-null object\n", " 1 md5 22146 non-null object\n", " 2 combined_id 22146 non-null object\n", " 3 eol_content_id 22146 non-null int64 \n", " 4 eol_page_id 22146 non-null int64 \n", " 5 medium_source_url 22146 non-null object\n", " 6 eol_full_size_copy_url 22146 non-null object\n", " 7 license_name 22146 non-null object\n", " 8 copyright_owner 22146 non-null object\n", " 9 title 22146 non-null object\n", " 10 license_link 22103 non-null object\n", "dtypes: int64(2), object(9)\n", "memory usage: 1.9+ MB\n" ] } ], "source": [ "rs_manifest = pd.read_csv(\"../data/rarespecies_full_manifest.csv\",\n", " low_memory=False,\n", " dtype = {\"eol_content_id\": \"int64\", \"eol_page_id\": \"int64\"})\n", "rs_manifest.info(show_counts = True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Read in Rare Species Catalog" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the original [`rarespecies-catalog`](https://huggingface.co/datasets/imageomics/ToL-EDA/blob/5db838a9db980f4cf96027b326b06e0f42df0c65/rare_species/data/rarespecies-catalog.csv) generation was done in [`rare_species_make_catalog.ipynb`](https://huggingface.co/datasets/imageomics/ToL-EDA/blob/5db838a9db980f4cf96027b326b06e0f42df0c65/rare_species/notebooks/rare_species_make_catalog.ipynb). It used [`predicted-catalog.csv`](https://huggingface.co/datasets/imageomics/ToL-EDA/blob/5db838a9db980f4cf96027b326b06e0f42df0c65/data/predicted-catalog.csv) to get the taxonomic information and align the [rare species cargo](https://huggingface.co/datasets/imageomics/ToL-EDA/blob/5db838a9db980f4cf96027b326b06e0f42df0c65/rare_species/data/rarespecies.csv) appropriately. There were also checks that the unseen subset was indeed excluded from the [catalog](https://huggingface.co/datasets/imageomics/ToL-EDA/blob/5db838a9db980f4cf96027b326b06e0f42df0c65/data/catalog.csv).\n", "\n", "We will use the [`rarespecies-catalog.csv`](https://huggingface.co/datasets/imageomics/ToL-EDA/blob/5db838a9db980f4cf96027b326b06e0f42df0c65/rare_species/data/rarespecies-catalog.csv) generated in that notebook to align taxonomic information with the full rare species manifest (renamed locally to `rarespecies-catalog-orig.csv`)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "RangeIndex: 12000 entries, 0 to 11999\n", "Data columns (total 11 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 rarespecies_id 12000 non-null object\n", " 1 eol_content_id 12000 non-null int64 \n", " 2 eol_page_id 12000 non-null int64 \n", " 3 kingdom 12000 non-null object\n", " 4 phylum 12000 non-null object\n", " 5 class 12000 non-null object\n", " 6 order 12000 non-null object\n", " 7 family 12000 non-null object\n", " 8 genus 12000 non-null object\n", " 9 species 12000 non-null object\n", " 10 sciName 12000 non-null object\n", "dtypes: int64(2), object(9)\n", "memory usage: 1.0+ MB\n" ] } ], "source": [ "rs_catalog = pd.read_csv(RS_FILEPATH + \"rarespecies-catalog-orig.csv\",\n", " low_memory=False,\n", " dtype = {\"eol_content_id\": \"int64\", \"eol_page_id\": \"int64\"})\n", "rs_catalog.info(show_counts = True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Check Number of Images per Page" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "22146" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rs_manifest[\"md5\"].nunique()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We should have at least 30 images per species, except for 5. Let's check the number of images for each species (all now included are unique, as demonstrated above)." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "num_images_by_pg = {}\n", "\n", "for pg_id in list(rs_manifest.eol_page_id.unique()):\n", " num_images = rs_manifest.loc[rs_manifest[\"eol_page_id\"] == pg_id].shape[0]\n", " num_images_by_pg[pg_id] = num_images\n", " rs_manifest.loc[rs_manifest[\"eol_page_id\"] == pg_id, \"num_images\"] = num_images\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "93" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rs_manifest.num_images.nunique()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There's a lot of variety in the number of images per page. We'll check again on the (expected 5) pages that have less than 30 images." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "page ID 485420 has less than 30 unique images; it has 24 unique images\n", "page ID 914531 has less than 30 unique images; it has 29 unique images\n", "page ID 791049 has less than 30 unique images; it has 27 unique images\n", "page ID 205714 has less than 30 unique images; it has 29 unique images\n", "page ID 1286909 has less than 30 unique images; it has 27 unique images\n" ] } ], "source": [ "for pg_id in num_images_by_pg.keys():\n", " if num_images_by_pg[pg_id] < 30:\n", " print(f\"page ID {pg_id} has less than 30 unique images; it has {num_images_by_pg[pg_id]} unique images\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "https://eol.org/pages/485420/media has only 21 true images, since 3 are post-its. These will need to be manually noted and removed." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Reduce to 30 Images per Page" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we need to reduce to just 30 images per page. We can realign the labels based on the page IDs from the `rarespecies_catalog`, but I won't be able to properly rematch the `rarespecies_id`, so that will need to be regenerated..." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "22146" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Is filename unique? --yes, 22146 is number of images in the full rarespecies manifest\n", "rs_manifest.filename.nunique()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "keep\n", "yes 11986\n", "Name: count, dtype: int64" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "img_list_by_page = {}\n", "\n", "for page_id in list(rs_manifest.eol_page_id.unique()):\n", " temp = rs_manifest.loc[rs_manifest.eol_page_id == page_id]\n", " if temp.num_images.values[0] > 30:\n", " imgs_to_keep = list(temp.filename.sample(30, random_state = 614)) #seed for reproducibility\n", " else:\n", " imgs_to_keep = list(temp.filename)\n", "\n", " rs_manifest.loc[rs_manifest[\"filename\"].isin(imgs_to_keep), \"keep\"] = \"yes\"\n", " img_list_by_page[page_id] = imgs_to_keep\n", "\n", "rs_manifest[\"keep\"].value_counts()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's 14 less than 12000 images, which matches with the number of images we'd be reduced by due to the 5 pages that have less than 30 images." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(11986, 13)\n" ] }, { "data": { "text/plain": [ "filename 11986\n", "md5 11986\n", "combined_id 11986\n", "eol_content_id 11986\n", "eol_page_id 400\n", "medium_source_url 11986\n", "eol_full_size_copy_url 11986\n", "license_name 15\n", "copyright_owner 4164\n", "title 1290\n", "license_link 10\n", "num_images 93\n", "keep 1\n", "dtype: int64" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reduced_rs_manifest = rs_manifest.loc[rs_manifest[\"keep\"] == \"yes\"]\n", "\n", "print(reduced_rs_manifest.shape)\n", "reduced_rs_manifest.nunique()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "num_images\n", "30.0 11850\n", "29.0 58\n", "27.0 54\n", "24.0 24\n", "Name: count, dtype: int64" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "for pg_id in list(reduced_rs_manifest.eol_page_id.unique()):\n", " num_images = reduced_rs_manifest.loc[reduced_rs_manifest[\"eol_page_id\"] == pg_id].shape[0]\n", " reduced_rs_manifest.loc[reduced_rs_manifest[\"eol_page_id\"] == pg_id, \"num_images\"] = num_images\n", "\n", "reduced_rs_manifest[\"num_images\"].value_counts()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One page has only 24, two have 27 & 29, the remaining 395 pages have 30.\n", "\n", "Let's track down those 3 post-it images on page 485420." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "https://eol.org/media/29856158\n", "https://eol.org/media/28605134\n", "https://eol.org/media/14677205\n", "https://eol.org/media/28605135\n", "https://eol.org/media/29856162\n", "https://eol.org/media/29856156\n", "https://eol.org/media/29856167\n", "https://eol.org/media/29856165\n", "https://eol.org/media/29856163\n", "https://eol.org/media/29856157\n", "https://eol.org/media/14677203\n", "https://eol.org/media/29856159\n", "https://eol.org/media/28605131\n", "https://eol.org/media/29856155\n", "https://eol.org/media/29856151\n", "https://eol.org/media/29856161\n", "https://eol.org/media/14677207\n", "https://eol.org/media/29856150\n", "https://eol.org/media/29856164\n", "https://eol.org/media/14677204\n", "https://eol.org/media/28605132\n", "https://eol.org/media/28605133\n", "https://eol.org/media/28605130\n", "https://eol.org/media/29856166\n" ] } ], "source": [ "for content_id in list(reduced_rs_manifest.loc[reduced_rs_manifest[\"eol_page_id\"] == 485420, \"eol_content_id\"]):\n", " print(f\"https://eol.org/media/{content_id}\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Post-it image EOL Content IDs (unique in this DataFrame):\n", "- [28605134](https://eol.org/media/28605134)\n", "- [29856159](https://eol.org/media/29856159)\n", "- [29856164](https://eol.org/media/29856164)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(11983, 13)\n" ] }, { "data": { "text/plain": [ "filename 11983\n", "md5 11983\n", "combined_id 11983\n", "eol_content_id 11983\n", "eol_page_id 400\n", "medium_source_url 11983\n", "eol_full_size_copy_url 11983\n", "license_name 15\n", "copyright_owner 4164\n", "title 1290\n", "license_link 10\n", "num_images 4\n", "keep 1\n", "dtype: int64" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "post_its = [28605134, 29856159, 29856164]\n", "\n", "rarespecies_manifest = reduced_rs_manifest.loc[~reduced_rs_manifest[\"eol_content_id\"].isin(post_its)]\n", "print(rarespecies_manifest.shape)\n", "rarespecies_manifest.nunique()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remove extra columns" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['filename', 'md5', 'combined_id', 'eol_content_id', 'eol_page_id',\n", " 'medium_source_url', 'eol_full_size_copy_url', 'license_name',\n", " 'copyright_owner', 'title', 'license_link'],\n", " dtype='object')" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rarespecies_manifest = rarespecies_manifest[list(rarespecies_manifest.columns)[:-2]]\n", "rarespecies_manifest.columns" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add `uuid` (new `rarespecies_id`)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "import uuid" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11983" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rarespecies_manifest[\"rarespecies_id\"] = [uuid.uuid4() for i in range(rarespecies_manifest.shape[0])]\n", "rarespecies_manifest.rarespecies_id.nunique()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Save Manifest with `uuid`s" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "rarespecies_manifest.to_csv(RS_FILEPATH + \"rarespecies_manifest.csv\", index = False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Make and Save License File" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "licesne_cols = [\"rarespecies_id\",\n", " \"eol_content_id\",\n", " \"eol_page_id\",\n", " \"md5\",\n", " \"medium_source_url\",\n", " \"eol_full_size_copy_url\",\n", " \"license_name\",\n", " \"copyright_owner\",\n", " \"license_link\",\n", " \"title\"]\n", "\n", "licenses = rarespecies_manifest[licesne_cols]" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "licenses.to_csv(RS_FILEPATH + \"licenses.csv\", index = False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Make Rare Species Catalog\n", "\n", "Align taxonomic information to the IDs in the manifest.\n", "\n", "Pull manifest back in; something went weird with initial catalog generation." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "RangeIndex: 11983 entries, 0 to 11982\n", "Data columns (total 12 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 filename 11983 non-null object\n", " 1 md5 11983 non-null object\n", " 2 combined_id 11983 non-null object\n", " 3 eol_content_id 11983 non-null int64 \n", " 4 eol_page_id 11983 non-null int64 \n", " 5 medium_source_url 11983 non-null object\n", " 6 eol_full_size_copy_url 11983 non-null object\n", " 7 license_name 11983 non-null object\n", " 8 copyright_owner 11983 non-null object\n", " 9 title 11983 non-null object\n", " 10 license_link 11957 non-null object\n", " 11 rarespecies_id 11983 non-null object\n", "dtypes: int64(2), object(10)\n", "memory usage: 1.1+ MB\n" ] } ], "source": [ "rarespecies_manifest = pd.read_csv(RS_FILEPATH + \"rarespecies_manifest.csv\",\n", " low_memory=False,\n", " dtype = {\"eol_content_id\": \"int64\", \"eol_page_id\": \"int64\"})\n", "rarespecies_manifest.info(show_counts = True)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "id_cols = [\"rarespecies_id\", \"eol_content_id\", \"eol_page_id\"]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['eol_page_id',\n", " 'kingdom',\n", " 'phylum',\n", " 'class',\n", " 'order',\n", " 'family',\n", " 'genus',\n", " 'species',\n", " 'sciName']" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cat_cols = list(rs_catalog.columns)[2:]\n", "cat_cols" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(359490, 11)\n" ] }, { "data": { "text/plain": [ "rarespecies_id 11983\n", "eol_content_id 11983\n", "eol_page_id 400\n", "kingdom 1\n", "phylum 5\n", "class 15\n", "order 85\n", "family 202\n", "genus 316\n", "species 385\n", "sciName 400\n", "dtype: int64" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "updated_catalog = pd.merge(rarespecies_manifest[id_cols],\n", " rs_catalog[cat_cols],\n", " on = \"eol_page_id\",\n", " how = \"left\")\n", "\n", "print(updated_catalog.shape)\n", "updated_catalog.nunique()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Uniqueness counts are as expected, but it duplicated entries for everything in the manifest (30 copies of everything since there are multiple matches per page). We need to use just the unique values." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "duplicate_species\n", "True 11600\n", "False 400\n", "Name: count, dtype: int64" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rs_catalog[\"duplicate_species\"] = rs_catalog.duplicated(subset = cat_cols, keep = \"first\")\n", "rs_catalog[\"duplicate_species\"].value_counts()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "rarespecies_id 400\n", "eol_content_id 400\n", "eol_page_id 400\n", "kingdom 1\n", "phylum 5\n", "class 15\n", "order 85\n", "family 202\n", "genus 316\n", "species 385\n", "sciName 400\n", "duplicate_species 1\n", "dtype: int64" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "unique_rs_catalog = rs_catalog.loc[~rs_catalog[\"duplicate_species\"]]\n", "unique_rs_catalog.nunique()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now the merge shouldn't produce duplicates." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(11983, 11)\n" ] }, { "data": { "text/plain": [ "rarespecies_id 11983\n", "eol_content_id 11983\n", "eol_page_id 400\n", "kingdom 1\n", "phylum 5\n", "class 15\n", "order 85\n", "family 202\n", "genus 316\n", "species 385\n", "sciName 400\n", "dtype: int64" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "updated_catalog = pd.merge(rarespecies_manifest[id_cols],\n", " unique_rs_catalog[cat_cols],\n", " on = \"eol_page_id\",\n", " how = \"left\")\n", "\n", "print(updated_catalog.shape)\n", "updated_catalog.nunique()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
rarespecies_ideol_content_ideol_page_idkingdomphylumclassorderfamilygenusspeciessciName
44620022b617-0f5a-48cf-93b4-b987bc0b28d530072827323915AnimaliaChordataMammaliaPrimatesAtelidaeAlouattacarayaAlouatta caraya
3229b14f5d3c-17d0-4d05-be0f-635c32813ecf228947251047771AnimaliaChordataAvesPasseriformesMimidaeMimusmacdonaldiMimus macdonaldi
5172a579040e-98dc-4249-b78a-94efe761770e221919281038777AnimaliaChordataMammaliaArtiodactylaBovidaeProcaprapicticaudataProcapra picticaudata
954214b76c63-f4aa-4108-bcfe-04ad7004e6152991065847363970AnimaliaChordataAvesStrigiformesStrigidaeAtheneblewittiAthene blewitti
766375e0f3c5-de1e-4e6a-96dc-1d3542dc525915263810327865AnimaliaChordataMammaliaChiropteraPhyllostomidaeChoeronycterismexicanaChoeronycteris mexicana
14189470b0d1-c077-41a1-ac09-6bfae0735e4f264745511277AnimaliaChordataAvesProcellariiformesDiomedeidaeThalassarchechlororhynchosThalassarche chlororhynchos
800807ce200b-c168-4706-9328-5aa02eee1d73229690281012078AnimaliaChordataActinopterygiiCyprinodontiformesCyprinodontidaeCyprinodonmaculariusCyprinodon macularius
6762a1b441ac-8f72-41d5-9f89-db95ad09ea432967932423668991AnimaliaChordataReptiliaSquamataDactyloidaeAnolismonteverdeAnolis monteverde
2846cd86b9c7-31dc-4906-9536-05d64e2759ca2892869245518912AnimaliaChordataAvesCharadriiformesBurhinidaeEsacusrecurvirostrisEsacus recurvirostris
811191c6b4dd-c199-45fb-97cd-6bc9e4c7fbe629033057963751AnimaliaChordataReptiliaSquamataIguanidaeCtenosaurabakeriCtenosaura bakeri
\n", "
" ], "text/plain": [ " rarespecies_id eol_content_id eol_page_id \\\n", "4462 0022b617-0f5a-48cf-93b4-b987bc0b28d5 30072827 323915 \n", "3229 b14f5d3c-17d0-4d05-be0f-635c32813ecf 22894725 1047771 \n", "5172 a579040e-98dc-4249-b78a-94efe761770e 22191928 1038777 \n", "9542 14b76c63-f4aa-4108-bcfe-04ad7004e615 29910658 47363970 \n", "7663 75e0f3c5-de1e-4e6a-96dc-1d3542dc5259 15263810 327865 \n", "1418 9470b0d1-c077-41a1-ac09-6bfae0735e4f 2647 45511277 \n", "8008 07ce200b-c168-4706-9328-5aa02eee1d73 22969028 1012078 \n", "6762 a1b441ac-8f72-41d5-9f89-db95ad09ea43 29679324 23668991 \n", "2846 cd86b9c7-31dc-4906-9536-05d64e2759ca 28928692 45518912 \n", "8111 91c6b4dd-c199-45fb-97cd-6bc9e4c7fbe6 29033057 963751 \n", "\n", " kingdom phylum class order family \\\n", "4462 Animalia Chordata Mammalia Primates Atelidae \n", "3229 Animalia Chordata Aves Passeriformes Mimidae \n", "5172 Animalia Chordata Mammalia Artiodactyla Bovidae \n", "9542 Animalia Chordata Aves Strigiformes Strigidae \n", "7663 Animalia Chordata Mammalia Chiroptera Phyllostomidae \n", "1418 Animalia Chordata Aves Procellariiformes Diomedeidae \n", "8008 Animalia Chordata Actinopterygii Cyprinodontiformes Cyprinodontidae \n", "6762 Animalia Chordata Reptilia Squamata Dactyloidae \n", "2846 Animalia Chordata Aves Charadriiformes Burhinidae \n", "8111 Animalia Chordata Reptilia Squamata Iguanidae \n", "\n", " genus species sciName \n", "4462 Alouatta caraya Alouatta caraya \n", "3229 Mimus macdonaldi Mimus macdonaldi \n", "5172 Procapra picticaudata Procapra picticaudata \n", "9542 Athene blewitti Athene blewitti \n", "7663 Choeronycteris mexicana Choeronycteris mexicana \n", "1418 Thalassarche chlororhynchos Thalassarche chlororhynchos \n", "8008 Cyprinodon macularius Cyprinodon macularius \n", "6762 Anolis monteverde Anolis monteverde \n", "2846 Esacus recurvirostris Esacus recurvirostris \n", "8111 Ctenosaura bakeri Ctenosaura bakeri " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "updated_catalog.sample(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Looks good, precisely as expected. Let's save this to a CSV now." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "updated_catalog.to_csv(RS_FILEPATH + \"rarespecies-catalog.csv\", index = False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Add common name column\n", "\n", "Common names have now been requested, so we'll pull those in (from [`predicted-catalog.csv`](https://huggingface.co/datasets/imageomics/ToL-EDA/blob/main/data/predicted-catalog.csv), [v3.3](https://huggingface.co/datasets/imageomics/ToL-EDA/commit/5260e84802d2910eedc2a206e7be035cf28f64d4)).\n", "\n", "Updating [`rarespecies-catalog.csv`](https://huggingface.co/datasets/imageomics/ToL-EDA/blob/49400c376e62866be1c5038b673d2ebb002c0540/rare_species/data/rarespecies-catalog.csv)." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# pull our updated catalog back in\n", "updated_catalog = pd.read_csv(RS_FILEPATH + \"rarespecies-catalog.csv\",\n", " low_memory=False,\n", " dtype = {\"eol_content_id\": \"int64\", \"eol_page_id\": \"int64\"})" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "pred_cat = pd.read_csv(\"../../data/predicted-catalog.csv\", low_memory=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Reduce to just images sourced from EOL, cast content and page ids to `int64` for proper matching (will check there aren't different common name labels for some entries), then reduce to just Rare Species pages." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "eol_content_id 22580\n", "eol_page_id 400\n", "kingdom 1\n", "phylum 5\n", "class 15\n", "order 85\n", "family 202\n", "genus 316\n", "species 385\n", "common 398\n", "dtype: int64" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eol_pred = pred_cat.loc[pred_cat[\"eol_content_id\"].notna()]\n", "eol_pred = eol_pred.astype({\"eol_content_id\": \"int64\", \"eol_page_id\": \"int64\"})\n", "rs_pred = eol_pred.loc[eol_pred[\"eol_page_id\"].isin(list(updated_catalog.eol_page_id.unique()))]\n", "\n", "cols_interest = list(updated_catalog.columns)[1:-1]\n", "cols_interest.append(\"common\")\n", "rs_pred[cols_interest].nunique()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We don't seem to have unique common names for all of these entries. Let's find out what's duplicated." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/var/folders/nv/f0fq1p1n1_3b11x579py_0q80000gq/T/ipykernel_63935/2578193768.py:1: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", " rs_pred[\"duplicate_page\"] = rs_pred.duplicated(\"eol_page_id\", keep = \"first\")\n" ] }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
eol_content_ideol_page_idkingdomphylumclassorderfamilygenusspeciescommonduplicate_common
535101421712745276300AnimaliaCnidariaAnthozoaScleractiniaAcroporidaeAcroporamilleporastaghorn coralTrue
9584128509006332461AnimaliaChordataAmphibiaCaudataSalamandridaeTylototritonverrucosusYunnan NewtTrue
\n", "
" ], "text/plain": [ " eol_content_id eol_page_id kingdom phylum class \\\n", "53510 14217127 45276300 Animalia Cnidaria Anthozoa \n", "95841 28509006 332461 Animalia Chordata Amphibia \n", "\n", " order family genus species common \\\n", "53510 Scleractinia Acroporidae Acropora millepora staghorn coral \n", "95841 Caudata Salamandridae Tylototriton verrucosus Yunnan Newt \n", "\n", " duplicate_common \n", "53510 True \n", "95841 True " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rs_pred[\"duplicate_page\"] = rs_pred.duplicated(\"eol_page_id\", keep = \"first\")\n", "single_page = rs_pred.loc[~rs_pred[\"duplicate_page\"]]\n", "single_page = single_page[cols_interest]\n", "single_page[\"duplicate_common\"] = rs_pred.duplicated(\"common\", keep = \"first\")\n", "\n", "single_page.loc[single_page[\"duplicate_common\"]]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(400, 11)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "single_page.shape" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
eol_content_ideol_page_idkingdomphylumclassorderfamilygenusspeciescommonduplicate_common
52802826134145276299AnimaliaCnidariaAnthozoaScleractiniaAcroporidaeAcroporaacuminatastaghorn coralFalse
170748777107331513AnimaliaChordataAmphibiaCaudataSalamandridaeTylototritonshanjingYunnan NewtFalse
535101421712745276300AnimaliaCnidariaAnthozoaScleractiniaAcroporidaeAcroporamilleporastaghorn coralTrue
9584128509006332461AnimaliaChordataAmphibiaCaudataSalamandridaeTylototritonverrucosusYunnan NewtTrue
\n", "
" ], "text/plain": [ " eol_content_id eol_page_id kingdom phylum class \\\n", "5280 28261341 45276299 Animalia Cnidaria Anthozoa \n", "17074 8777107 331513 Animalia Chordata Amphibia \n", "53510 14217127 45276300 Animalia Cnidaria Anthozoa \n", "95841 28509006 332461 Animalia Chordata Amphibia \n", "\n", " order family genus species common \\\n", "5280 Scleractinia Acroporidae Acropora acuminata staghorn coral \n", "17074 Caudata Salamandridae Tylototriton shanjing Yunnan Newt \n", "53510 Scleractinia Acroporidae Acropora millepora staghorn coral \n", "95841 Caudata Salamandridae Tylototriton verrucosus Yunnan Newt \n", "\n", " duplicate_common \n", "5280 False \n", "17074 False \n", "53510 True \n", "95841 True " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "duplicated_common_names = [\"staghorn coral\", \"Yunnan Newt\"]\n", "single_page.loc[single_page[\"common\"].isin(duplicated_common_names)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So we have two common names that are each used for two species (within the same genus). That's not terribly unusual, but good to know.\n", "\n", "### Add the Common Name Column" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "rarespecies_id 11983\n", "eol_content_id 11983\n", "eol_page_id 400\n", "kingdom 1\n", "phylum 5\n", "class 15\n", "order 85\n", "family 202\n", "genus 316\n", "species 385\n", "sciName 400\n", "common 398\n", "dtype: int64" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def get_common_name(page_id):\n", " return single_page.loc[single_page[\"eol_page_id\"] == page_id, \"common\"].values[0]\n", "\n", "updated_catalog[\"common\"] = updated_catalog[\"eol_page_id\"].apply(get_common_name)\n", "updated_catalog.nunique()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Save Updated Version with Common Names" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "updated_catalog.to_csv(RS_FILEPATH + \"rarespecies-catalog.csv\", index = False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "jupytext": { "formats": "ipynb,py:percent" }, "kernelspec": { "display_name": "tol", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.3" } }, "nbformat": 4, "nbformat_minor": 2 }