{ "nbformat": 4, "nbformat_minor": 5, "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.12.3" } }, "cells": [ { "cell_type": "markdown", "id": "fe716932", "metadata": {}, "source": [ "# USDA Phytochemical Database — Quickstart\n", "\n", "This notebook demonstrates 5 practical use cases with the **free 400-row sample**.\n", "\n", "**Full dataset (104,388 rows):** [ethno-api.com](https://ethno-api.com) \n", "**GitHub:** [wirthal1990-tech/USDA-Phytochemical-Database-JSON](https://github.com/wirthal1990-tech/USDA-Phytochemical-Database-JSON)\n", "\n", "---" ] }, { "cell_type": "code", "execution_count": null, "id": "cfb77598", "metadata": {}, "outputs": [], "source": [ "# Cell 2 — Load the 400-row sample\n", "import pandas as pd\n", "\n", "url = \"https://raw.githubusercontent.com/wirthal1990-tech/USDA-Phytochemical-Database-JSON/main/ethno_sample_400.json\"\n", "df = pd.read_json(url)\n", "print(f\"Records: {df.shape[0]}\")\n", "print(f\"Unique compounds: {df['chemical'].nunique()}\")\n", "print(f\"Unique species: {df['plant_species'].nunique()}\")\n", "print(f\"\\nSchema:\")\n", "print(df.dtypes)\n", "df.head()" ] }, { "cell_type": "code", "execution_count": null, "id": "28718bea", "metadata": {}, "outputs": [], "source": [ "# Cell 3 — Top 10 compounds by PubMed mentions (with bar chart)\n", "import matplotlib\n", "matplotlib.rcParams['figure.figsize'] = (10, 5)\n", "\n", "top10 = (\n", " df.groupby('chemical')['pubmed_mentions_2026']\n", " .first()\n", " .nlargest(10)\n", " .sort_values(ascending=True)\n", ")\n", "\n", "top10.plot.barh(color='#2563eb')\n", "import matplotlib.pyplot as plt\n", "plt.xlabel('PubMed Mentions (March 2026)')\n", "plt.title('Top 10 Most-Studied Phytochemicals')\n", "plt.tight_layout()\n", "plt.show()\n", "\n", "print(top10.to_frame().to_string())" ] }, { "cell_type": "code", "execution_count": null, "id": "a8e4bde5", "metadata": {}, "outputs": [], "source": [ "# Cell 4 — DuckDB: Anti-inflammatory compounds ranked by clinical evidence\n", "# pip install duckdb (once)\n", "import duckdb\n", "\n", "# Query directly from the pandas DataFrame loaded in Cell 2\n", "result = duckdb.sql(\"\"\"\n", " SELECT\n", " chemical,\n", " MAX(pubmed_mentions_2026) AS pubmed_score,\n", " COUNT(DISTINCT plant_species) AS species_count\n", " FROM df\n", " WHERE application ILIKE '%anti%inflam%'\n", " OR application ILIKE '%antiinflam%'\n", " GROUP BY chemical\n", " ORDER BY pubmed_score DESC\n", " LIMIT 15\n", "\"\"\")\n", "print(\"Top anti-inflammatory compounds by PubMed evidence:\")\n", "result.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "b5a633ab", "metadata": {}, "outputs": [], "source": [ "# Cell 5 — RAG pipeline use case: build a deterministic compound context block\n", "# This is the pattern used to ground LLMs with plant chemistry data.\n", "\n", "query_compound = \"QUERCETIN\"\n", "\n", "# Filter all records for this compound\n", "compound_records = df[df[\"chemical\"] == query_compound].copy()\n", "compound_records = compound_records.sort_values(\"pubmed_mentions_2026\", ascending=False)\n", "\n", "print(f\"=== RAG CONTEXT BLOCK FOR: {query_compound} ===\\n\")\n", "for _, row in compound_records.head(5).iterrows():\n", " block = (\n", " f\"Compound: {row['chemical']}\\n\"\n", " f\"Plant: {row['plant_species']}\\n\"\n", " f\"Application: {row['application']}\\n\"\n", " f\"Dosage: {row['dosage']}\\n\"\n", " f\"PubMed mentions (2026): {row['pubmed_mentions_2026']}\\n\"\n", " f\"---\"\n", " )\n", " print(block)\n", "\n", "print(f\"\\nTotal records for {query_compound}: {len(compound_records)}\")\n", "print(\"These structured blocks eliminate LLM hallucinations about botanical dosages.\")" ] }, { "cell_type": "code", "execution_count": null, "id": "d8220844", "metadata": {}, "outputs": [], "source": [ "# Cell 6 — Parquet vs JSON: memory and size comparison\n", "import io\n", "\n", "json_bytes = df.to_json(orient=\"records\").encode(\"utf-8\")\n", "parquet_buf = io.BytesIO()\n", "df.to_parquet(parquet_buf, engine=\"pyarrow\", compression=\"snappy\")\n", "parquet_size = parquet_buf.tell()\n", "json_size = len(json_bytes)\n", "\n", "print(f\"{'Format':<10} {'Size':>12} {'Ratio':>8}\")\n", "print(f\"{'JSON':<10} {json_size:>12,} B {'100.0%':>8}\")\n", "print(f\"{'Parquet':<10} {parquet_size:>12,} B {parquet_size/json_size:>8.1%}\")\n", "print()\n", "print(f\"Parquet is {json_size/parquet_size:.1f}× smaller than JSON for this sample.\")\n", "print()\n", "print(\"Full 104,388-row dataset:\")\n", "print(\" JSON: ~16.4 MB (ethno_dataset_v2.json)\")\n", "print(\" Parquet: ~761 KB (ethno_dataset_v2.parquet, Snappy-compressed)\")\n", "print(\" Ratio: ~22× smaller\")" ] }, { "cell_type": "markdown", "id": "c7870648", "metadata": {}, "source": [ "---\n", "\n", "## Get the Full Dataset\n", "\n", "| | Free Sample | Full Dataset |\n", "|---|---|---|\n", "| **Records** | 400 | 104,388 |\n", "| **Schema** | 8 columns | 8 columns |\n", "| **Enrichment fields** | Placeholder values | Real values (CT, ChEMBL, Patents) |\n", "| **Price** | Free | €699 one-time |\n", "\n", "**Purchase:** [ethno-api.com](https://ethno-api.com) \n", "**Questions:** founder@ethno-api.com" ] } ] }