CAPT / analysis /capt_validation_schema.py
bonsai44's picture
Initial CAPT paper data and analysis release
cf5bcd6 verified
Raw
History Blame Contribute Delete
13.6 kB
from __future__ import annotations
import csv
from dataclasses import asdict, dataclass, field
from pathlib import Path
from typing import Dict, List, Optional
BOOKS_MASTER_FIELDS = [
"book_id",
"asin",
"isbn13",
"title",
"author_name",
"publisher",
"publisher_tier",
"publication_date",
"format",
"list_price",
"genre_primary",
"genre_secondary",
"sample_group",
"author_central_present",
"author_central_links_count",
"youtube_link_present",
"goodreads_presence",
"social_presence_score",
"author_best_known_bsr",
"author_prior_top100_title_count",
"nyt_list_any",
"nyt_list_count",
]
BOOK_OBSERVATION_FIELDS = [
"book_id",
"observation_date",
"source_url",
"days_since_publication",
"amazon_bsr_overall",
"amazon_bsr_primary_category",
"amazon_bsr_secondary_category",
"best_visible_category_bsr",
"primary_category_name",
"secondary_category_name",
"category_count",
"category_top10_count",
"category_top100_count",
"category_top1000_count",
"launch_window_30d_flag",
"hot_new_releases_flag",
"hot_new_releases_position",
"movers_shakers_flag",
"movers_shakers_position",
"rating_average",
"rating_count",
"review_count",
"also_bought_visible_flag",
"also_bought_count",
"also_bought_bestseller_count",
"also_bought_shared_category_count",
"review_velocity_7d",
"review_velocity_30d",
"rank_change_24h",
"top100_flag",
"threshold_50_reviews_flag",
"threshold_event_flag",
"sustained_top100_60d_flag",
"ar_proxy_score",
"csd_proxy_score",
"cpa_proxy_score",
"arm_observed_score",
]
NYT_WEEKLY_FIELDS = [
"published_date",
"list_name",
"list_name_encoded",
"rank",
"rank_last_week",
"weeks_on_list",
"title",
"author",
"publisher",
"primary_isbn13",
"primary_isbn10",
"amazon_product_url",
"book_image",
"book_uri",
]
NYT_HISTORY_FIELDS = [
"query_value",
"query_type",
"title",
"author",
"publisher",
"description",
"contributor",
"contributor_note",
"price",
"age_group",
"primary_isbn13",
"primary_isbn10",
"isbn13",
"isbn10",
"history_list_name",
"history_display_name",
"bestsellers_date",
"published_date",
"rank",
"rank_last_week",
"weeks_on_list",
"asterisk",
"dagger",
]
AMAZON_OBSERVATION_INPUT_FIELDS = [
"book_id",
"observation_date",
"source_url",
"asin",
"title",
"author_name",
"days_since_publication",
"amazon_bsr_overall",
"amazon_bsr_primary_category",
"amazon_bsr_secondary_category",
"primary_category_name",
"secondary_category_name",
"category_count",
"category_top10_count",
"category_top100_count",
"category_top1000_count",
"hot_new_releases_flag",
"hot_new_releases_position",
"movers_shakers_flag",
"movers_shakers_position",
"rating_average",
"rating_count",
"review_count",
"also_bought_visible_flag",
"also_bought_count",
"also_bought_bestseller_count",
"also_bought_shared_category_count",
"notes",
]
@dataclass
class BookMasterRecord:
book_id: str
asin: str = ""
isbn13: str = ""
title: str = ""
author_name: str = ""
publisher: str = ""
publisher_tier: str = ""
publication_date: str = ""
format: str = ""
list_price: str = ""
genre_primary: str = ""
genre_secondary: str = ""
sample_group: str = ""
author_central_present: str = ""
author_central_links_count: str = ""
youtube_link_present: str = ""
goodreads_presence: str = ""
social_presence_score: str = ""
author_best_known_bsr: str = ""
author_prior_top100_title_count: str = ""
nyt_list_any: str = ""
nyt_list_count: str = ""
def to_row(self) -> Dict[str, str]:
return {field_name: str(asdict(self).get(field_name, "")) for field_name in BOOKS_MASTER_FIELDS}
@dataclass
class BookObservationRecord:
book_id: str
observation_date: str
source_url: str = ""
days_since_publication: str = ""
amazon_bsr_overall: str = ""
amazon_bsr_primary_category: str = ""
amazon_bsr_secondary_category: str = ""
best_visible_category_bsr: str = ""
primary_category_name: str = ""
secondary_category_name: str = ""
category_count: str = ""
category_top10_count: str = ""
category_top100_count: str = ""
category_top1000_count: str = ""
launch_window_30d_flag: str = ""
hot_new_releases_flag: str = ""
hot_new_releases_position: str = ""
movers_shakers_flag: str = ""
movers_shakers_position: str = ""
rating_average: str = ""
rating_count: str = ""
review_count: str = ""
also_bought_visible_flag: str = ""
also_bought_count: str = ""
also_bought_bestseller_count: str = ""
also_bought_shared_category_count: str = ""
review_velocity_7d: str = ""
review_velocity_30d: str = ""
rank_change_24h: str = ""
top100_flag: str = ""
threshold_50_reviews_flag: str = ""
threshold_event_flag: str = ""
sustained_top100_60d_flag: str = ""
ar_proxy_score: str = ""
csd_proxy_score: str = ""
cpa_proxy_score: str = ""
arm_observed_score: str = ""
def to_row(self) -> Dict[str, str]:
return {field_name: str(asdict(self).get(field_name, "")) for field_name in BOOK_OBSERVATION_FIELDS}
@dataclass
class TemplateBundle:
books_master_path: Path
observations_path: Path
nyt_weekly_path: Path
nyt_history_path: Path
amazon_input_path: Path
metadata: Dict[str, str] = field(default_factory=dict)
def ensure_directory(path: str | Path) -> Path:
directory = Path(path)
directory.mkdir(parents=True, exist_ok=True)
return directory
def write_csv_template(path: str | Path, fieldnames: List[str], example_rows: Optional[List[Dict[str, str]]] = None) -> Path:
target = Path(path)
ensure_directory(target.parent)
with target.open("w", newline="", encoding="utf-8") as handle:
writer = csv.DictWriter(handle, fieldnames=fieldnames)
writer.writeheader()
for row in example_rows or []:
writer.writerow({field_name: row.get(field_name, "") for field_name in fieldnames})
return target
def create_validation_templates(output_dir: str | Path = "validation_templates") -> TemplateBundle:
output_path = ensure_directory(output_dir)
books_master_example = BookMasterRecord(
book_id="philo_001",
asin="B0EXAMPLE01",
isbn13="9780000000001",
title="Example Philosophy Title",
author_name="Example Author",
publisher="Example Press",
publisher_tier="mid",
publication_date="2026-01-15",
format="hardcover",
list_price="28.00",
genre_primary="philosophy",
genre_secondary="self-help",
sample_group="candidate",
author_central_present="1",
author_central_links_count="2",
youtube_link_present="1",
goodreads_presence="1",
social_presence_score="2",
author_best_known_bsr="41",
author_prior_top100_title_count="2",
nyt_list_any="0",
nyt_list_count="0",
).to_row()
observation_example = BookObservationRecord(
book_id="philo_001",
observation_date="2026-04-12",
source_url="https://www.amazon.com/dp/B0EXAMPLE01",
days_since_publication="88",
amazon_bsr_overall="154",
amazon_bsr_primary_category="12",
amazon_bsr_secondary_category="47",
best_visible_category_bsr="12",
primary_category_name="Philosophy",
secondary_category_name="Ethics & Morality",
category_count="2",
category_top10_count="0",
category_top100_count="2",
category_top1000_count="2",
launch_window_30d_flag="0",
hot_new_releases_flag="0",
hot_new_releases_position="",
movers_shakers_flag="1",
movers_shakers_position="18",
rating_average="4.6",
rating_count="73",
review_count="73",
also_bought_visible_flag="1",
also_bought_count="12",
also_bought_bestseller_count="4",
also_bought_shared_category_count="7",
review_velocity_7d="5",
review_velocity_30d="19",
rank_change_24h="23",
top100_flag="0",
threshold_50_reviews_flag="1",
threshold_event_flag="0",
sustained_top100_60d_flag="",
ar_proxy_score="0.61",
csd_proxy_score="0.48",
cpa_proxy_score="0.67",
arm_observed_score="0.35",
).to_row()
nyt_example = {
"published_date": "2026-04-12",
"list_name": "Hardcover Nonfiction",
"list_name_encoded": "hardcover-nonfiction",
"rank": "8",
"rank_last_week": "0",
"weeks_on_list": "1",
"title": "Example Philosophy Title",
"author": "Example Author",
"publisher": "Example Press",
"primary_isbn13": "9780000000001",
"primary_isbn10": "0000000000",
"amazon_product_url": "https://www.amazon.com/dp/B0EXAMPLE01",
"book_image": "https://example.com/cover.jpg",
"book_uri": "nyt://books/example",
}
nyt_history_example = {
"query_value": "9780000000001",
"query_type": "isbn",
"title": "Example Philosophy Title",
"author": "Example Author",
"publisher": "Example Press",
"description": "Example retrospective history row.",
"contributor": "by Example Author",
"contributor_note": "",
"price": "28.00",
"age_group": "",
"primary_isbn13": "9780000000001",
"primary_isbn10": "0000000000",
"isbn13": "9780000000001",
"isbn10": "0000000000",
"history_list_name": "hardcover-nonfiction",
"history_display_name": "Hardcover Nonfiction",
"bestsellers_date": "2026-04-05",
"published_date": "2026-04-12",
"rank": "8",
"rank_last_week": "10",
"weeks_on_list": "2",
"asterisk": "0",
"dagger": "0",
}
amazon_input_example = {
"book_id": "philo_001",
"observation_date": "2026-04-12",
"source_url": "https://www.amazon.com/dp/B0EXAMPLE01",
"asin": "B0EXAMPLE01",
"title": "Example Philosophy Title",
"author_name": "Example Author",
"days_since_publication": "88",
"amazon_bsr_overall": "154",
"amazon_bsr_primary_category": "12",
"amazon_bsr_secondary_category": "47",
"primary_category_name": "Philosophy",
"secondary_category_name": "Ethics & Morality",
"category_count": "2",
"category_top10_count": "0",
"category_top100_count": "2",
"category_top1000_count": "2",
"hot_new_releases_flag": "0",
"hot_new_releases_position": "",
"movers_shakers_flag": "1",
"movers_shakers_position": "18",
"rating_average": "4.6",
"rating_count": "73",
"review_count": "73",
"also_bought_visible_flag": "1",
"also_bought_count": "12",
"also_bought_bestseller_count": "4",
"also_bought_shared_category_count": "7",
"notes": "Manual observation example row.",
}
books_master_path = write_csv_template(output_path / "books_master_template.csv", BOOKS_MASTER_FIELDS, [books_master_example])
observations_path = write_csv_template(output_path / "book_observations_template.csv", BOOK_OBSERVATION_FIELDS, [observation_example])
nyt_weekly_path = write_csv_template(output_path / "nyt_weekly_lists_template.csv", NYT_WEEKLY_FIELDS, [nyt_example])
nyt_history_path = write_csv_template(output_path / "nyt_bestseller_history_template.csv", NYT_HISTORY_FIELDS, [nyt_history_example])
amazon_input_path = write_csv_template(output_path / "amazon_observation_input_template.csv", AMAZON_OBSERVATION_INPUT_FIELDS, [amazon_input_example])
return TemplateBundle(
books_master_path=books_master_path,
observations_path=observations_path,
nyt_weekly_path=nyt_weekly_path,
nyt_history_path=nyt_history_path,
amazon_input_path=amazon_input_path,
metadata={
"books_master_fields": str(len(BOOKS_MASTER_FIELDS)),
"book_observation_fields": str(len(BOOK_OBSERVATION_FIELDS)),
"nyt_weekly_fields": str(len(NYT_WEEKLY_FIELDS)),
"nyt_history_fields": str(len(NYT_HISTORY_FIELDS)),
"amazon_input_fields": str(len(AMAZON_OBSERVATION_INPUT_FIELDS)),
},
)
if __name__ == "__main__":
bundle = create_validation_templates()
print("Created validation templates:")
print(f"- books_master: {bundle.books_master_path}")
print(f"- book_observations: {bundle.observations_path}")
print(f"- nyt_weekly_lists: {bundle.nyt_weekly_path}")
print(f"- nyt_bestseller_history: {bundle.nyt_history_path}")
print(f"- amazon_observation_input: {bundle.amazon_input_path}")