{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "TOL_FILEPATH = \"../../data/\"" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "tol = pd.read_csv(TOL_FILEPATH + \"catalog.csv\", low_memory=False)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Index: 6250420 entries, 956715 to 11000930\n", "Data columns (total 12 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 split 6250420 non-null object\n", " 1 treeoflife_id 6250420 non-null object\n", " 2 eol_content_id 6250420 non-null int64 \n", " 3 eol_page_id 6250420 non-null int64 \n", " 4 kingdom 5989611 non-null object\n", " 5 phylum 5991207 non-null object\n", " 6 class 5971438 non-null object\n", " 7 order 5965299 non-null object\n", " 8 family 5948728 non-null object\n", " 9 genus 5940313 non-null object\n", " 10 species 5951613 non-null object\n", " 11 common 6250420 non-null object\n", "dtypes: int64(2), object(10)\n", "memory usage: 619.9+ MB\n" ] } ], "source": [ "# Remove duplicates\n", "tol = tol.loc[tol.split != 'train_small']\n", "\n", "# Reduce to just EOL data & columns\n", "eol = tol.loc[tol[\"eol_content_id\"].notna()]\n", "eol_cols = [col for col in list(eol.columns) if \"bioscan\" not in col and \"inat\" not in col]\n", "eol_catalog = eol[eol_cols]\n", "\n", "# Cast EOL content & page IDs as ints\n", "eol_catalog = eol_catalog.astype({\"eol_content_id\": \"int64\", \"eol_page_id\": \"int64\"})\n", "eol_catalog.info(show_counts = True)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "archive = pd.read_csv(\"../data/checksums_media_cargo_archive.csv\", low_memory=False)" ] }, { "cell_type": "code", "execution_count": 6, "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", "
splittreeoflife_ideol_content_id_catalogeol_page_id_catalogkingdomphylumclassorderfamilygenusspeciescommoncombined_id_catalog
956715trainaa563519-6d97-47c3-bcda-c72cdf19a1bf14855758788082AnimaliaArthropodaInsectaDipteraCecidomyiidaeGeocryptagaliiGeocrypta galii14855758_788082
956716traine3253a67-7114-41d5-ac55-1696d6c7f6f322807070639017PlantaeTracheophytaMagnoliopsidaFabalesFabaceaePsoralealupinellaPsoralea lupinella22807070_639017
\n", "
" ], "text/plain": [ " split treeoflife_id eol_content_id_catalog \\\n", "956715 train aa563519-6d97-47c3-bcda-c72cdf19a1bf 14855758 \n", "956716 train e3253a67-7114-41d5-ac55-1696d6c7f6f3 22807070 \n", "\n", " eol_page_id_catalog kingdom phylum class order \\\n", "956715 788082 Animalia Arthropoda Insecta Diptera \n", "956716 639017 Plantae Tracheophyta Magnoliopsida Fabales \n", "\n", " family genus species common \\\n", "956715 Cecidomyiidae Geocrypta galii Geocrypta galii \n", "956716 Fabaceae Psoralea lupinella Psoralea lupinella \n", "\n", " combined_id_catalog \n", "956715 14855758_788082 \n", "956716 22807070_639017 " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# make combined ID for catalog and add suffix to eol content and page IDs\n", "eol_catalog['combined_id_catalog'] = eol_catalog['eol_content_id'].astype(str) + '_' + eol_catalog['eol_page_id'].astype(str)\n", "eol_catalog.rename(columns={'eol_content_id': 'eol_content_id_catalog', 'eol_page_id': 'eol_page_id_catalog'}, inplace=True)\n", "eol_catalog.head(2)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "archive has 6725471 entries, with columns: ['Filename', 'MD5']\n" ] } ], "source": [ "# Cargo had 6,724,838 entries\n", "print(f\"archive has {archive.shape[0]} entries, with columns: {list(archive.columns)}\")" ] }, { "cell_type": "code", "execution_count": 8, "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", "
FilenameMD5
029538374_65414274_eol-full-size-copy.jpg98b1ed1430620ad50a6126cb2f9fcc74
127793900_888015_eol-full-size-copy.jpg2de2ff12d5f339fe9d81a170749f9cb6
\n", "
" ], "text/plain": [ " Filename MD5\n", "0 29538374_65414274_eol-full-size-copy.jpg 98b1ed1430620ad50a6126cb2f9fcc74\n", "1 27793900_888015_eol-full-size-copy.jpg 2de2ff12d5f339fe9d81a170749f9cb6" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "archive.head(2)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "def get_combined_id(filename):\n", " return filename.split(\"_e\")[0]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "archive[\"combined_id\"] = archive[\"Filename\"].apply(get_combined_id)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Merge catalog with archive (sanity check on our images since the cargo didn't match with all of catalog)." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "RangeIndex: 6250420 entries, 0 to 6250419\n", "Data columns (total 16 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 split 6250420 non-null object\n", " 1 treeoflife_id 6250420 non-null object\n", " 2 eol_content_id_catalog 6250420 non-null int64 \n", " 3 eol_page_id_catalog 6250420 non-null int64 \n", " 4 kingdom 5989611 non-null object\n", " 5 phylum 5991207 non-null object\n", " 6 class 5971438 non-null object\n", " 7 order 5965299 non-null object\n", " 8 family 5948728 non-null object\n", " 9 genus 5940313 non-null object\n", " 10 species 5951613 non-null object\n", " 11 common 6250420 non-null object\n", " 12 combined_id_catalog 6250420 non-null object\n", " 13 Filename 6250420 non-null object\n", " 14 MD5 6250420 non-null object\n", " 15 combined_id 6250420 non-null object\n", "dtypes: int64(2), object(14)\n", "memory usage: 763.0+ MB\n" ] } ], "source": [ "catalog_archive = pd.merge(eol_catalog,\n", " archive,\n", " left_on = \"combined_id_catalog\",\n", " right_on = \"combined_id\",\n", " how=\"inner\"\n", " )\n", "catalog_archive.info(show_counts = True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sanity check confirmed: Everything in the catalog is captured in our cargo archive." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pull in combined manifest with checksums to merge on the archive checksums." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['filename_manifest', 'md5_combined_manifest',\n", " 'combined_id_manifest_redownload', 'eol_content_id', 'eol_page_id',\n", " 'medium_source_url', 'eol_full_size_copy_url', 'license_name',\n", " 'copyright_owner', 'expected_image_filename', 'source_0706',\n", " 'source_0726', 'source_1206', 'combined_id_full_manifest'],\n", " dtype='object')" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "manifests = pd.read_csv(\"../data/combined_manifest_with_checksums.csv\", low_memory=False)\n", "manifests.columns" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "RangeIndex: 6841202 entries, 0 to 6841201\n", "Data columns (total 30 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 split 6841202 non-null object\n", " 1 treeoflife_id 6841202 non-null object\n", " 2 eol_content_id_catalog 6841202 non-null int64 \n", " 3 eol_page_id_catalog 6841202 non-null int64 \n", " 4 kingdom 6551473 non-null object\n", " 5 phylum 6553206 non-null object\n", " 6 class 6532009 non-null object\n", " 7 order 6525621 non-null object\n", " 8 family 6506076 non-null object\n", " 9 genus 6486130 non-null object\n", " 10 species 6406307 non-null object\n", " 11 common 6841202 non-null object\n", " 12 combined_id_catalog 6841202 non-null object\n", " 13 Filename 6841202 non-null object\n", " 14 MD5 6841202 non-null object\n", " 15 combined_id 6841202 non-null object\n", " 16 filename_manifest 6841202 non-null object\n", " 17 md5_combined_manifest 6841202 non-null object\n", " 18 combined_id_manifest_redownload 6841202 non-null object\n", " 19 eol_content_id 6841202 non-null int64 \n", " 20 eol_page_id 6841202 non-null int64 \n", " 21 medium_source_url 6841202 non-null object\n", " 22 eol_full_size_copy_url 6841202 non-null object\n", " 23 license_name 6841202 non-null object\n", " 24 copyright_owner 6208498 non-null object\n", " 25 expected_image_filename 6841202 non-null object\n", " 26 source_0706 6841202 non-null bool \n", " 27 source_0726 6841202 non-null bool \n", " 28 source_1206 6841202 non-null bool \n", " 29 combined_id_full_manifest 6841202 non-null object\n", "dtypes: bool(3), int64(4), object(23)\n", "memory usage: 1.4+ GB\n" ] } ], "source": [ "catalog_archive_manifest = pd.merge(catalog_archive,\n", " manifests,\n", " left_on = \"MD5\",\n", " right_on = \"md5_combined_manifest\",\n", " how = \"inner\",\n", " suffixes=(\"\",\"_manifests\")\n", " )\n", "catalog_archive_manifest.info(show_counts = True)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6219674" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "catalog_archive_manifest.treeoflife_id.nunique()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Only 6,219,674 of our 6,250,420 images were able to be pulled through the combined manifests." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Align Owners to Manifest" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "owner_df = pd.read_csv(\"../data/media_manifest_missing_licenses_jul26_owners.csv\",\n", " low_memory=False,\n", " dtype={\"EOL content ID\": \"int64\", \"EOL page ID\": \"int64\"},)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "try merge then fill and concatenate." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['filename_manifest', 'md5_combined_manifest', 'combined_id_manifest_redownload', 'eol_content_id', 'eol_page_id', 'medium_source_url', 'eol_full_size_copy_url', 'license_name', 'copyright_owner', 'expected_image_filename', 'source_0706', 'source_0726', 'source_1206', 'combined_id_full_manifest']\n", "['EOL content ID', 'EOL page ID', 'Medium Source URL', 'EOL Full-Size Copy URL', 'License Name', 'Copyright Owner', 'license_link', 'title']\n" ] } ], "source": [ "print(list(manifests.columns))\n", "print(list(owner_df.columns))" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "There are 12330062 entries in the combined manifest\n", "There are 632043 entries in the owner match CSV\n" ] } ], "source": [ "print(f\"There are {manifests.shape[0]} entries in the combined manifest\")\n", "print(f\"There are {owner_df.shape[0]} entries in the owner match CSV\")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "owner_df[\"combined_id_owner\"] = owner_df[\"EOL content ID\"].astype(str) + \"_\" + owner_df[\"EOL page ID\"].astype(str)\n" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "RangeIndex: 630056 entries, 0 to 630055\n", "Data columns (total 23 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 EOL content ID 630056 non-null int64 \n", " 1 EOL page ID 630056 non-null int64 \n", " 2 Medium Source URL 630056 non-null object\n", " 3 EOL Full-Size Copy URL 630056 non-null object\n", " 4 License Name 630056 non-null object\n", " 5 Copyright Owner 611829 non-null object\n", " 6 license_link 619741 non-null object\n", " 7 title 604447 non-null object\n", " 8 combined_id_owner 630056 non-null object\n", " 9 filename_manifest 630056 non-null object\n", " 10 md5_combined_manifest 630056 non-null object\n", " 11 combined_id_manifest_redownload 630056 non-null object\n", " 12 eol_content_id 630056 non-null int64 \n", " 13 eol_page_id 630056 non-null int64 \n", " 14 medium_source_url 630056 non-null object\n", " 15 eol_full_size_copy_url 630056 non-null object\n", " 16 license_name 630056 non-null object\n", " 17 copyright_owner 0 non-null object\n", " 18 expected_image_filename 630056 non-null object\n", " 19 source_0706 630056 non-null bool \n", " 20 source_0726 630056 non-null bool \n", " 21 source_1206 630056 non-null bool \n", " 22 combined_id_full_manifest 630056 non-null object\n", "dtypes: bool(3), int64(4), object(16)\n", "memory usage: 97.9+ MB\n" ] } ], "source": [ "owners_manifest = pd.merge(owner_df,\n", " manifests,\n", " left_on=\"combined_id_owner\",\n", " right_on = \"combined_id_full_manifest\",\n", " how = \"inner\")\n", "owners_manifest.info()" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "RangeIndex: 630056 entries, 0 to 630055\n", "Data columns (total 23 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 EOL content ID 630056 non-null int64 \n", " 1 EOL page ID 630056 non-null int64 \n", " 2 Medium Source URL 630056 non-null object\n", " 3 EOL Full-Size Copy URL 630056 non-null object\n", " 4 License Name 630056 non-null object\n", " 5 Copyright Owner 611829 non-null object\n", " 6 license_link 619741 non-null object\n", " 7 title 604447 non-null object\n", " 8 combined_id_owner 630056 non-null object\n", " 9 filename_manifest 630056 non-null object\n", " 10 md5_combined_manifest 630056 non-null object\n", " 11 combined_id_manifest_redownload 630056 non-null object\n", " 12 eol_content_id 630056 non-null int64 \n", " 13 eol_page_id 630056 non-null int64 \n", " 14 medium_source_url 630056 non-null object\n", " 15 eol_full_size_copy_url 630056 non-null object\n", " 16 license_name 630056 non-null object\n", " 17 copyright_owner 611829 non-null object\n", " 18 expected_image_filename 630056 non-null object\n", " 19 source_0706 630056 non-null bool \n", " 20 source_0726 630056 non-null bool \n", " 21 source_1206 630056 non-null bool \n", " 22 combined_id_full_manifest 630056 non-null object\n", "dtypes: bool(3), int64(4), object(16)\n", "memory usage: 97.9+ MB\n" ] } ], "source": [ "owners_manifest[\"copyright_owner\"] = owners_manifest[\"Copyright Owner\"]\n", "owners_manifest.info(show_counts=True)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['filename_manifest', 'md5_combined_manifest',\n", " 'combined_id_manifest_redownload', 'eol_content_id', 'eol_page_id',\n", " 'medium_source_url', 'eol_full_size_copy_url', 'license_name',\n", " 'copyright_owner', 'expected_image_filename', 'source_0706',\n", " 'source_0726', 'source_1206', 'combined_id_full_manifest', 'title'],\n", " dtype='object')" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "license_cols = list(manifests.columns)\n", "license_cols.append(\"title\")\n", "owners_manifest = owners_manifest[license_cols]\n", "owners_manifest.columns" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "630056" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rematched_owner_ids = list(owners_manifest.combined_id_full_manifest.unique())\n", "len(rematched_owner_ids)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(11700006, 14)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "un_matched_owner_manifest = manifests.loc[~manifests.combined_id_full_manifest.isin(rematched_owner_ids)]\n", "un_matched_owner_manifest.shape" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/var/folders/nv/f0fq1p1n1_3b11x579py_0q80000gq/T/ipykernel_4207/1002247400.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", " un_matched_owner_manifest[\"title\"] = \"not provided\"\n" ] } ], "source": [ "un_matched_owner_manifest[\"title\"] = \"not provided\"" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "RangeIndex: 12330062 entries, 0 to 12330061\n", "Data columns (total 15 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 filename_manifest 12330062 non-null object\n", " 1 md5_combined_manifest 12330062 non-null object\n", " 2 combined_id_manifest_redownload 12330062 non-null object\n", " 3 eol_content_id 12330062 non-null int64 \n", " 4 eol_page_id 12330062 non-null int64 \n", " 5 medium_source_url 12330062 non-null object\n", " 6 eol_full_size_copy_url 12330062 non-null object\n", " 7 license_name 12330062 non-null object\n", " 8 copyright_owner 11544885 non-null object\n", " 9 expected_image_filename 12330062 non-null object\n", " 10 source_0706 12330062 non-null bool \n", " 11 source_0726 12330062 non-null bool \n", " 12 source_1206 12330062 non-null bool \n", " 13 combined_id_full_manifest 12330062 non-null object\n", " 14 title 12304453 non-null object\n", "dtypes: bool(3), int64(2), object(10)\n", "memory usage: 1.1+ GB\n" ] } ], "source": [ "full_owner_manifest = pd.concat([un_matched_owner_manifest, owners_manifest], ignore_index=True)\n", "full_owner_manifest.info(show_counts = True)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Licenses still missing Copyright Owners: \n", " license_name\n", "cc-by-3.0 262894\n", "cc-publicdomain 262803\n", "cc-by-nc-sa-3.0 122493\n", "cc-by-sa-3.0 93303\n", "No known copyright restrictions 31025\n", "cc-by-nc 12619\n", "cc-0-1.0 32\n", "cc-by-nc-2.0 5\n", "cc-by-nc-4.0 2\n", "cc-by-nc-sa 1\n", "Name: count, dtype: int64\n" ] } ], "source": [ "print(\n", " \"Licenses still missing Copyright Owners: \\n\",\n", " full_owner_manifest.loc[\n", " full_owner_manifest[\"copyright_owner\"].isna(), \"license_name\"\n", " ].value_counts(),\n", " )" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "full_owner_manifest[\"copyright_owner\"].fillna(\"not provided\", inplace=True)\n", "full_owner_manifest[\"title\"].fillna(\"not provided\", inplace=True)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "full_owner_manifest.to_csv(\"../data/full_owner_manifest.csv\", index = False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "std", "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 }