Add scripts to add owner fix information to combined manifest.
Browse files
eol_realign/scripts/make_licenses.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
import argparse
|
| 4 |
+
import sys
|
| 5 |
+
|
| 6 |
+
# Output of match_owners.py (combined_manifest.csv) should be fed in.
|
| 7 |
+
|
| 8 |
+
CC_URL = "https://creativecommons.org/"
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def get_license_url(license_version):
|
| 12 |
+
"""
|
| 13 |
+
Function to generate the appropriate Creative Commons URL for a given license.
|
| 14 |
+
All licenses in the media manifest are "cc-XX", a variation on "publicdomain", or "No known copyright restrictions".
|
| 15 |
+
|
| 16 |
+
Parameters:
|
| 17 |
+
-----------
|
| 18 |
+
license_version - String. License (eg., "cc-by-nc 3.0").
|
| 19 |
+
|
| 20 |
+
Returns:
|
| 21 |
+
--------
|
| 22 |
+
license_url - String. Creative Commons URL associated with the license_version.
|
| 23 |
+
|
| 24 |
+
"""
|
| 25 |
+
# First check for version number and isolate it
|
| 26 |
+
if "." in license_version:
|
| 27 |
+
version = license_version.split(sep="-")[-1]
|
| 28 |
+
license_name = license_version.split(sep="-" + version)[0]
|
| 29 |
+
else:
|
| 30 |
+
# No version specified, so default to latest version (4.0)
|
| 31 |
+
license_name = license_version
|
| 32 |
+
version = "4.0"
|
| 33 |
+
# Check which type of license
|
| 34 |
+
if license_name[:5] == "cc-by":
|
| 35 |
+
by_x = license_name.split(sep="cc-")[1]
|
| 36 |
+
license_url = CC_URL + "licenses/" + by_x + "/" + version
|
| 37 |
+
elif (license_name[:4] == "cc-0") or ("public" in license_name):
|
| 38 |
+
license_url = CC_URL + "publicdomain/zero/1.0"
|
| 39 |
+
else:
|
| 40 |
+
# "No known copyright restrictions"
|
| 41 |
+
license_url = None
|
| 42 |
+
return license_url
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def main(src_csv, dest_dir):
|
| 46 |
+
# Check CSV compatibility
|
| 47 |
+
try:
|
| 48 |
+
print("Reading CSV")
|
| 49 |
+
df = pd.read_csv(src_csv, low_memory=False)
|
| 50 |
+
except Exception as e:
|
| 51 |
+
sys.exit(e)
|
| 52 |
+
|
| 53 |
+
# Check for "License Name" or "license_name" column
|
| 54 |
+
print("Processing data")
|
| 55 |
+
df_cols = list(df.columns)
|
| 56 |
+
if "License Name" not in df_cols:
|
| 57 |
+
if "license_name" not in df_cols:
|
| 58 |
+
sys.exit("Source CSV does not have a column labeled License Name or license_name.")
|
| 59 |
+
license_col = "license_name"
|
| 60 |
+
license_col = "License Name"
|
| 61 |
+
|
| 62 |
+
# Check filepath given by user
|
| 63 |
+
dest_dir_path = Path(dest_dir)
|
| 64 |
+
if not dest_dir_path.is_absolute():
|
| 65 |
+
# Use ToL-EDA as reference folder
|
| 66 |
+
base_path = Path(__file__).parent.parent.resolve()
|
| 67 |
+
filepath = base_path / dest_dir_path
|
| 68 |
+
else:
|
| 69 |
+
filepath = dest_dir_path
|
| 70 |
+
|
| 71 |
+
filepath.mkdir(parents=True, exist_ok=True)
|
| 72 |
+
|
| 73 |
+
# Add links for licenses to combined manifest
|
| 74 |
+
print("Getting URLs for licenses")
|
| 75 |
+
df["license_link"] = df[license_col].apply(get_license_url)
|
| 76 |
+
|
| 77 |
+
# Save license file
|
| 78 |
+
df.to_csv(str(filepath / "combined-manifest-licenses.csv"), index=False)
|
| 79 |
+
|
| 80 |
+
# Print counts of licenses for which URL was not resolved
|
| 81 |
+
print(
|
| 82 |
+
"Licenses with no URL: \n",
|
| 83 |
+
df.loc[df["license_link"].isna(), license_col].value_counts(),
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
parser = argparse.ArgumentParser(
|
| 89 |
+
description="Add license URLs to combined manifest file"
|
| 90 |
+
)
|
| 91 |
+
parser.add_argument("input_file", help="Path to the input CSV file")
|
| 92 |
+
parser.add_argument(
|
| 93 |
+
"--output_path",
|
| 94 |
+
default="data",
|
| 95 |
+
required=False,
|
| 96 |
+
help="Path to the folder for output license file",
|
| 97 |
+
)
|
| 98 |
+
args = parser.parse_args()
|
| 99 |
+
|
| 100 |
+
main(args.input_file, args.output_path)
|
eol_realign/scripts/match_owners.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from tqdm import tqdm
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
import argparse
|
| 5 |
+
import sys
|
| 6 |
+
|
| 7 |
+
# This file can be used on predicted-catalog or rarespecies-catalog by changing the CATALOG_PATH.
|
| 8 |
+
# It is intended to just add the extra licensing information to the combined manifest
|
| 9 |
+
# Owner fix file: https://huggingface.co/datasets/imageomics/eol/blob/main/owners_info_fix/media_manifest_missing_licenses_jul26_owners.csv
|
| 10 |
+
MANIFEST_PATH = "../data/combined_manifest_with_checksums.csv"
|
| 11 |
+
OWNER_PATH = "../data/media_manifest_missing_licenses_jul26_owners.csv"
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def get_owners_titles(manifest, owners):
|
| 15 |
+
"""
|
| 16 |
+
Function to attach owner names and image titles to manifest entries in combined manifest.
|
| 17 |
+
Fills empty "Copyright Owner" and "Title" values with "not provided".
|
| 18 |
+
|
| 19 |
+
Parameters:
|
| 20 |
+
-----------
|
| 21 |
+
manifest - DataFrame composed of manifest entries aligned on MD5s from three different manifests,
|
| 22 |
+
includes columns "md5_combined_manifest", "combined_id_full_manifest".
|
| 23 |
+
owners - DataFrame of July 26, 2023 media manifest entries that had missing owners, updated with their information.
|
| 24 |
+
|
| 25 |
+
Returns:
|
| 26 |
+
--------
|
| 27 |
+
manifest - Media manifest DataFrame with missing owners resolved.
|
| 28 |
+
"""
|
| 29 |
+
missing_owners = [
|
| 30 |
+
comb_id
|
| 31 |
+
for comb_id in list(
|
| 32 |
+
manifest.loc[manifest["Copyright Owner"].isna(), "combined_id_full_manifest"]
|
| 33 |
+
)
|
| 34 |
+
]
|
| 35 |
+
|
| 36 |
+
for comb_id in tqdm(missing_owners):
|
| 37 |
+
temp = owners.loc[owners.combined_id_owner == comb_id]
|
| 38 |
+
copyright_owner = temp["Copyright Owner"].values
|
| 39 |
+
title = temp.title.values
|
| 40 |
+
manifest.loc[manifest["combined_id_full_manifest"] == comb_id, "Copyright Owner"] = copyright_owner
|
| 41 |
+
manifest.loc[manifest["combined_id_full_manifest"] == comb_id, "title"] = title
|
| 42 |
+
|
| 43 |
+
# Print counts of licenses for which owner info was not resolved
|
| 44 |
+
print(
|
| 45 |
+
"Licenses still missing Copyright Owners: \n",
|
| 46 |
+
manifest.loc[
|
| 47 |
+
manifest["Copyright Owner"].isna(), "License Name"
|
| 48 |
+
].value_counts(),
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Fill null "Copyright Owner" and "Title" values with "not provided"
|
| 52 |
+
manifest["Copyright Owner"].fillna("not provided", inplace=True)
|
| 53 |
+
manifest["title"].fillna("not provided", inplace=True)
|
| 54 |
+
|
| 55 |
+
return manifest
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def update_owners(manifest, owners, filepath):
|
| 59 |
+
"""
|
| 60 |
+
Function to fetch and attach the missing owner and title information to EOL catalog entries and save catalog-media file.
|
| 61 |
+
|
| 62 |
+
Parameters:
|
| 63 |
+
-----------
|
| 64 |
+
manifest - DataFrame composed of manifest entries aligned on MD5s from three different manifests,
|
| 65 |
+
includes columns "md5_combined_manifest", "combined_id_full_manifest".
|
| 66 |
+
owners - DataFrame of July 26, 2023 media manifest entries that had missing owners, updated with their information.
|
| 67 |
+
"""
|
| 68 |
+
# Need combined IDs for the owner match file
|
| 69 |
+
# EOL content & page IDs read in as int64
|
| 70 |
+
owners["combined_id_owner"] = owners["eol_content_id"].astype(str) + "_" + owners["eol_page_id"].astype(str)
|
| 71 |
+
|
| 72 |
+
updated_manifest = get_owners_titles(manifest, owners)
|
| 73 |
+
|
| 74 |
+
# Save updated combined manifest file to chosen location
|
| 75 |
+
updated_manifest.to_csv(filepath, index=False)
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def main(dest_dir):
|
| 79 |
+
# Read in CSVs
|
| 80 |
+
try:
|
| 81 |
+
# Read in media manifest and owner fix CSVs with EOL content and page IDs as type "int64".
|
| 82 |
+
print("Reading combined media manifest CSV")
|
| 83 |
+
manifest = pd.read_csv(
|
| 84 |
+
MANIFEST_PATH,
|
| 85 |
+
low_memory=False,
|
| 86 |
+
)
|
| 87 |
+
print("Reading owner fix CSV")
|
| 88 |
+
owners = pd.read_csv(
|
| 89 |
+
OWNER_PATH,
|
| 90 |
+
dtype={"EOL content ID": "int64", "EOL page ID": "int64"},
|
| 91 |
+
low_memory=False,
|
| 92 |
+
)
|
| 93 |
+
except Exception as e:
|
| 94 |
+
sys.exit(e)
|
| 95 |
+
|
| 96 |
+
# Check filepath given by user
|
| 97 |
+
dest_dir_path = Path(dest_dir)
|
| 98 |
+
if not dest_dir_path.is_absolute():
|
| 99 |
+
# Use ToL-EDA as reference folder
|
| 100 |
+
base_path = Path(__file__).parent.parent.resolve()
|
| 101 |
+
filepath = base_path / dest_dir_path
|
| 102 |
+
else:
|
| 103 |
+
filepath = dest_dir_path
|
| 104 |
+
|
| 105 |
+
filepath.mkdir(parents=True, exist_ok=True)
|
| 106 |
+
|
| 107 |
+
# Make and save updated manifest to chosen filepath
|
| 108 |
+
update_owners(manifest, owners, str(filepath / "combined_manifest.csv"))
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
if __name__ == "__main__":
|
| 112 |
+
parser = argparse.ArgumentParser(description="Attach missing owner info to combined manifest")
|
| 113 |
+
|
| 114 |
+
parser.add_argument(
|
| 115 |
+
"--output_path",
|
| 116 |
+
default="data",
|
| 117 |
+
required=False,
|
| 118 |
+
help="Path to the folder for output combined manifest file",
|
| 119 |
+
)
|
| 120 |
+
args = parser.parse_args()
|
| 121 |
+
|
| 122 |
+
main(args.output_path)
|