{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "\n",
    "def read_csv_to_dict(file_path, key_column):\n",
    "    with open(file_path, mode='r', encoding='utf-8') as csvfile:\n",
    "        reader = csv.DictReader(csvfile)\n",
    "        result_dict = {row[key_column]: row for row in reader}\n",
    "    return result_dict\n",
    "\n",
    "# Usage\n",
    "csv_file_path = '../BVR_patients_recalcuted_far_PD.csv'\n",
    "key_column_name = 'far PD sid zip file name'\n",
    "topology_csv_dict = read_csv_to_dict(csv_file_path, key_column_name)\n",
    "\n",
    "print(topology_csv_dict)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from api import BigApi\n",
    "BigApi.init(\".admin.env\")\n",
    "BigApi.adminLogin()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "scanReqs = BigApi.adminGet(f\"/admin/fabricator/scan_requests\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import floor\n",
    "import numpy as np\n",
    "\n",
    "matches = []\n",
    "discrepencies = []\n",
    "misses = []\n",
    "revoked = []\n",
    "\n",
    "counts = {}\n",
    "\n",
    "# for each row in the topology_csv_dict\n",
    "for key, row in topology_csv_dict.items():\n",
    "    topology_patient_id = row['patient_id']\n",
    "    for scanReq in scanReqs[\"items\"]:\n",
    "        if scanReq[\"topologyPatientId\"] == topology_patient_id:\n",
    "            if scanReq[\"scanRequestState\"] == \"Revoked\":\n",
    "                revoked.append(scanReq[\"id\"])\n",
    "                continue\n",
    "            if (scanReq[\"ipd\"] == -1):\n",
    "                misses.append(scanReq[\"id\"])\n",
    "\n",
    "            new_ipd = int(np.round(float(row['re-calculated far pd_mm'])))\n",
    "            new_ipd = np.clip(new_ipd, 55, 72)\n",
    "\n",
    "            diff = abs(new_ipd - scanReq[\"ipd\"])\n",
    "\n",
    "            if diff not in counts:\n",
    "                counts[diff] = 0\n",
    "            counts[diff] += 1\n",
    "counts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import floor\n",
    "import numpy as np\n",
    "\n",
    "matches = []\n",
    "discrepencies = []\n",
    "misses = []\n",
    "revoked = []\n",
    "\n",
    "counts = {}\n",
    "# count duplicates\n",
    "for key, row in topology_csv_dict.items():\n",
    "    topology_patient_id = row['patient_id']\n",
    "    if topology_patient_id not in counts:\n",
    "        counts[topology_patient_id] = 0\n",
    "    counts[topology_patient_id] += 1\n",
    "\n",
    "# for each row in the topology_csv_dict\n",
    "for key, row in topology_csv_dict.items():\n",
    "    topology_patient_id = row['patient_id']\n",
    "    for scanReq in scanReqs[\"items\"]:\n",
    "        if scanReq[\"topologyPatientId\"] == topology_patient_id:\n",
    "            if scanReq[\"scanRequestState\"] == \"Revoked\":\n",
    "                revoked.append(scanReq[\"id\"])\n",
    "                continue\n",
    "\n",
    "            if (scanReq[\"ipd\"] == -1):\n",
    "                misses.append(scanReq[\"id\"])\n",
    "\n",
    "            new_ipd = int(np.round(float(row['re-calculated far pd_mm'])))\n",
    "            new_ipd = np.clip(new_ipd, 55, 72)\n",
    "\n",
    "            if (new_ipd == 70):\n",
    "                new_ipd = 69\n",
    "\n",
    "            try:\n",
    "                # Update the scan request with the new IPD\n",
    "                #data = BigApi.adminPut(f\"/admin/fabricator/scan_request/{scanReq['id']}?force=1\", {\"ipd\": f\"{new_ipd}\"})\n",
    "\n",
    "                # Refresh the IPD\n",
    "                bigOrderId = scanReq[\"bigOrderId\"]\n",
    "                data = BigApi.adminPut(f\"/admin/shop/order/{bigOrderId}\", {\n",
    "                    \"action\": \"SetStartingIPD\"\n",
    "                })\n",
    "            except:\n",
    "                BigApi.init(\".admin.env\")\n",
    "                BigApi.adminLogin()\n",
    "\n",
    "            # If there is a Big Order, force an IPD update if and only if the ipd source\n",
    "            print (topology_patient_id, scanReq[\"id\"], scanReq[\"ipd\"], new_ipd)\n",
    "            matches.append(topology_patient_id)\n",
    "            break\n",
    "\n",
    "print(\"Matches\", len(matches))\n",
    "print(\"Discrepencies\", len(discrepencies))\n",
    "print(\"Revoked\", len(revoked))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
