{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7fb27b941602401d91542211134fc71a",
   "metadata": {},
   "source": [
    "# HSI Surface Mapping\n",
    "Segment a synthetic HSI cube, extract ROI spectra, and run RQ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "acae54e37e7d407bbb7b55eff062a284",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "from foodspec.protocol_engine import ProtocolConfig, ProtocolRunner\n",
    "from foodspec.spectral_dataset import HyperspectralDataset\n",
    "\n",
    "npz = np.load('examples/data/hsi_synthetic.npz')\n",
    "cube = npz['cube']\n",
    "wn = npz['wavenumbers']\n",
    "meta = pd.DataFrame({'pixel_id': np.arange(cube.shape[0]*cube.shape[1])})\n",
    "hsi = HyperspectralDataset.from_cube(cube, wn, meta)\n",
    "proto = {\n",
    "    'name': 'hsi_demo',\n",
    "    'steps': [\n",
    "        {\"type\":\"hsi_segment\",\"params\":{\"method\":\"kmeans\",\"n_clusters\":2}},\n",
    "        {\"type\":\"hsi_roi_to_1d\",\"params\":{\"peaks\":[{\"name\":\"I_1000\",\"wavenumber\":1000.0},{\"name\":\"I_1100\",\"wavenumber\":1100.0}],\"ratios\":[{\"name\":\"1000/1100\",\"numerator\":\"I_1000\",\"denominator\":\"I_1100\"}],\"run_rq\":True,\"oil_col\":\"roi_label\"}},\n",
    "        {\"type\":\"output\",\"params\":{\"output_dir\":\"runs_notebook\"}}\n",
    "    ]\n",
    "}\n",
    "cfg = ProtocolConfig.from_dict(proto)\n",
    "runner = ProtocolRunner(cfg)\n",
    "res = runner.run([hsi])\n",
    "print('Run dir:', res.run_dir)\n",
    "if 'hsi_roi_peaks' in res.tables:\n",
    "    print(res.tables['hsi_roi_peaks'].head())\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
