{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7da3e2b2-a64c-41fe-8c23-e77bcad7afca",
   "metadata": {},
   "source": [
    "# Land\n",
    "\n",
    "# <font color=\"red\">NOTE: NOTEBOOK 4 IS NOT FINISHED YET</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "initial_id",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Change these as desired\n",
    "\n",
    "# The slug of the locality you are currently working on\n",
    "locality = \"us-nc-guilford\"\n",
    "\n",
    "# Whether to print out a lot of stuff (can help with debugging) or stay mostly quiet\n",
    "verbose = True\n",
    "\n",
    "# Clear previous state for this notebook and start fresh\n",
    "clear_checkpoints = False\n",
    "\n",
    "# Clear cached results for model predictions and start fresh\n",
    "clear_model_results = False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "669dc42f-42ab-4695-a3b5-24946fb3263e",
   "metadata": {},
   "source": [
    "# 1. Basic Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5153b137-3faa-40ed-9239-182f752733b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import init_notebooks\n",
    "init_notebooks.setup_environment()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bfd32d5c-3da0-4dc8-806e-fcbeb9853ee2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import a bunch of stuff\n",
    "from openavmkit.pipeline import (\n",
    "    init_notebook,\n",
    "    read_pickle,\n",
    "    load_settings\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ef145a0-ebea-4ea9-a3a6-055f3ab85ebf",
   "metadata": {},
   "outputs": [],
   "source": [
    "init_notebook(locality)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35f9d53d-71a0-4898-94c8-28f5d837fc74",
   "metadata": {},
   "outputs": [],
   "source": [
    "if clear_checkpoints:\n",
    "    delete_checkpoints(\"4-land\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98147703-a0d8-454e-8827-82664c6c8a26",
   "metadata": {},
   "source": [
    "# 2. Run Land Analysis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d6a0e663-17ca-4a5b-8a6b-ab6cb7f335ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "# load the data\n",
    "sales_univ_pair = read_pickle(\"out/2-clean-sup\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c5e1a236-f94b-41b2-86c7-583446192026",
   "metadata": {},
   "outputs": [],
   "source": [
    "from openavmkit.land import run_land_analysis\n",
    "\n",
    "run_land_analysis(sales_univ_pair, load_settings(), verbose)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f94e05ec-64e8-47d3-b8c9-c0c745530b3b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "path = \"out/models/single_family/vacant/ensemble/pred_universe.parquet\"\n",
    "df = pd.read_parquet(path)\n",
    "df = df[[\"key\",\"prediction\",\"assr_land_value\",\"land_area_sqft\"]]\n",
    "df = df.merge(sales_univ_pair.universe[[\"key\",\"latitude\",\"longitude\"]], on=\"key\", how=\"left\")\n",
    "\n",
    "df = df.rename(columns={\"prediction\":\"land_value\"})\n",
    "df = df[df[\"land_value\"].gt(0) & df[\"land_area_sqft\"].gt(0)]\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "481d7f88-9b9c-40b9-b920-ea67dd90d798",
   "metadata": {},
   "outputs": [],
   "source": [
    "from openavmkit.land import fit_land_SLICE_model, LandSLICEModel\n",
    "\n",
    "slice_model = fit_land_SLICE_model(df, size_field=\"land_area_sqft\", value_field=\"land_value\", verbose=True)\n",
    "slice_model.predict(df)\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c0caedf-0045-4c7b-aed4-ef15386f24e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "from openavmkit.utilities.plotting import plot_scatterplot\n",
    "\n",
    "# def plot_scatterplot(\n",
    "#     df,\n",
    "#     x: str,\n",
    "#     y: str,\n",
    "#     xlabel: str = None,\n",
    "#     ylabel: str = None,\n",
    "#     title: str = None,\n",
    "#     out_file: str = None,\n",
    "#     style: dict = None,\n",
    "#     best_fit_line: bool = False,\n",
    "#     perfect_fit_line: bool = False,\n",
    "#     metadata_field: str = None,\n",
    "# ):\n",
    "\n",
    "df[\"land_area_acres\"] = df[\"land_area_sqft\"]/43560\n",
    "\n",
    "plot_scatterplot(df, \"land_area_acres\", \"size_factor\", \"Land size\", \"Size factor\", \"Land size -> Size factor\")\n",
    "\n",
    "\n",
    "#df[[\"size_factor\", \"land_area_sqft\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4546ff6-e84e-4509-a333-2540e19f44aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_univ = sales_univ_pair.universe.copy()\n",
    "df_univ = df_univ.merge(df[[\"key\",\"location_factor\",\"size_factor\",\"land_value\"]], on=\"key\", how=\"left\")\n",
    "\n",
    "df_univ.to_parquet(\"land_test.parquet\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61dcb945-72a4-4401-922e-e8bc4ec9ee67",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
