ToL-EDA / eol_realign /scripts /match_owners.py
egrace479's picture
v3.3-meta-link (#10)
8af2e1d verified
import pandas as pd
from pathlib import Path
import argparse
import sys
# NOTE: column names are hard-coded to the files provided in MANIFEST_PATH & OWNER_PATH
# It is intended to just add the extra licensing information to the combined manifest
# Owner fix file: https://huggingface.co/datasets/imageomics/eol/blob/main/owners_info_fix/media_manifest_missing_licenses_jul26_owners.csv
MANIFEST_PATH = "data/eol-cargo-archive_catalog_combined-manifest-checksums_links.csv"
OWNER_PATH = "data/media_manifest_missing_licenses_jul26_owners.csv"
def get_owners_titles(manifest, owners):
"""
Function to attach owner names and image titles to manifest entries in combined manifest.
Fills empty "Copyright Owner" and "Title" values with "not provided".
Parameters:
-----------
manifest - DataFrame composed of manifest entries aligned on MD5s from three different manifests,
includes columns "md5_combined_manifest", "combined_id_full_manifest".
owners - DataFrame of July 26, 2023 media manifest entries that had missing owners, updated with their information.
Returns:
--------
full_owner_manifest - Media manifest DataFrame with missing owners resolved.
num_matched - Number of records matched with the owner fix file.
"""
# First merge owners
owners_manifest = pd.merge(owners,
manifest,
left_on="combined_id_owner",
right_on = "combined_id_full_manifest",
how = "inner")
# Set "copyright_owner" column to owners patched in owner CSV
owners_manifest["copyright_owner"] = owners_manifest["Copyright Owner"]
# Preserve columns that match the combined manifest
license_cols = list(manifest.columns)
license_cols.append("title")
owners_manifest = owners_manifest[license_cols]
# Get matched owner IDs
rematched_owner_ids = list(owners_manifest.combined_id_full_manifest.unique())
num_matched = len(rematched_owner_ids)
# Reduce manifest to just records not matched in owner file
un_matched_owner_manifest = manifest.loc[~manifest.combined_id_full_manifest.isin(rematched_owner_ids)]
un_matched_owner_manifest["title"] = "not provided"
# Align the matched owners with the unmatched
full_owner_manifest = pd.concat([un_matched_owner_manifest, owners_manifest], ignore_index=True)
# Fill null "copyright_owner" and "Title" values with "not provided"
full_owner_manifest["copyright_owner"].fillna("not provided", inplace=True)
full_owner_manifest["title"].fillna("not provided", inplace=True)
return full_owner_manifest, num_matched
def update_owners(manifest, owners, filepath):
"""
Function to fetch and attach the missing owner and title information to EOL catalog entries and save catalog-media file.
Parameters:
-----------
manifest - DataFrame composed of manifest entries aligned on MD5s from three different manifests,
includes columns "md5_combined_manifest", "combined_id_full_manifest".
owners - DataFrame of July 26, 2023 media manifest entries that had missing owners, updated with their information.
"""
# Need combined IDs for the owner match file
# EOL content & page IDs read in as int64
owners["combined_id_owner"] = owners["EOL content ID"].astype(str) + "_" + owners["EOL page ID"].astype(str)
updated_manifest, num_matched = get_owners_titles(manifest, owners)
# Save updated combined manifest file to chosen location
updated_manifest.to_csv(filepath, index=False)
# Print counts of licenses for which owner info was not resolved
print(f"Of the {manifest.shape[0]} entries in manifest, {num_matched} were matched to the owner fix file.")
print(
"Licenses still missing Copyright Owners: \n",
manifest.loc[
manifest["copyright_owner"].isna(), "license_name"
].value_counts(),
)
def main(dest_dir):
# Read in CSVs
try:
# Read in media manifest and owner fix CSVs with EOL content and page IDs as type "int64".
print("Reading combined media manifest CSV")
manifest = pd.read_csv(
MANIFEST_PATH,
low_memory=False,
)
print("Reading owner fix CSV")
owners = pd.read_csv(
OWNER_PATH,
dtype={"EOL content ID": "int64", "EOL page ID": "int64"},
low_memory=False,
)
except Exception as e:
sys.exit(e)
# Check filepath given by user
dest_dir_path = Path(dest_dir)
if not dest_dir_path.is_absolute():
# Use ToL-EDA as reference folder
base_path = Path(__file__).parent.parent.resolve()
filepath = base_path / dest_dir_path
else:
filepath = dest_dir_path
filepath.mkdir(parents=True, exist_ok=True)
# Make and save updated manifest to chosen filepath
update_owners(manifest, owners, str(filepath / "combined_manifest.csv"))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Attach missing owner info to combined manifest")
parser.add_argument(
"--output_path",
default="data",
required=False,
help="Path to the folder for output combined manifest file",
)
args = parser.parse_args()
main(args.output_path)