File size: 6,714 Bytes
9a71695 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | {
"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"
]
}
]
}
|