{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loadLibrary\n",
      "remoteSourcesURL https://git0.fmf.uni-lj.si/studen/nixSuite/raw/master/remoteResources/resources.json\n",
      "{'labkeyInterface': {'url': 'https://git0.fmf.uni-lj.si/studen/labkeyInterface/archive/master.zip', 'branch': 'master', 'modules': []}, 'irAEMM': {'url': 'https://git0.fmf.uni-lj.si/studen/iraemm/archive/master.zip', 'branch': 'master', 'modules': ['iraemmBrowser']}, 'SlicerLabkeyExtension': {'url': 'https://git0.fmf.uni-lj.si/studen/SlicerLabkeyExtension/archive/SlicerExtensionIndex.zip', 'branch': 'SlicerExtensionIndex', 'modules': ['labkeyBrowser']}, 'limfomiPET': {'url': 'https://git0.fmf.uni-lj.si/studen/limfomiPET/archive/master.zip', 'branch': 'master', 'modules': ['imageBrowser', 'segmentationBrowser']}, 'parseConfig': {'url': 'https://git0.fmf.uni-lj.si/studen/parseConfig/archive/master.zip', 'branch': 'master', 'modules': []}, 'orthancInterface': {'url': 'https://git0.fmf.uni-lj.si/studen/orthancInterface/archive/master.zip', 'branch': 'master', 'modules': []}}\n",
      "{'url': 'https://git0.fmf.uni-lj.si/studen/labkeyInterface/archive/master.zip', 'branch': 'master', 'modules': []}\n",
      "File  /home/studen/temp/labkeyInterface.zip: True\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import os\n",
    "import SimpleITK\n",
    "import numpy\n",
    "import matplotlib.pyplot\n",
    "import subprocess\n",
    "import json\n",
    "import re\n",
    "import config\n",
    "import getData\n",
    "import analysis\n",
    "\n",
    "import importlib\n",
    "importlib.reload(analysis)\n",
    "\n",
    "sys.path.append(os.path.join(os.path.expanduser('~'),'software','src','nixSuite','wrapper'))\n",
    "import nixWrapper\n",
    "nixWrapper.loadLibrary('labkeyInterface')\n",
    "import labkeyInterface\n",
    "import labkeyFileBrowser\n",
    "import labkeyDatabaseBrowser\n",
    "\n",
    "def connect(server,f):\n",
    "    net=labkeyInterface.labkeyInterface()\n",
    "    fconfig=os.path.join(os.path.expanduser('~'),'.labkey','{}.json'.format(server))\n",
    "    net.init(fconfig)\n",
    "    net.getCSRF()\n",
    "    return f(net)\n",
    "\n",
    "def connectDB(server):\n",
    "    return connect(server,labkeyDatabaseBrowser.labkeyDB)\n",
    "\n",
    "def connectFB(server):\n",
    "    return connect(server,labkeyFileBrowser.labkeyFileBrowser)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User: andrej studen CSRF: 5ca2747fc3908bd5e33a81ad025870c0\n",
      "User: andrej studen CSRF: 3b37eef31f9c9f9575d9a212f94df6a3\n"
     ]
    }
   ],
   "source": [
    "#setup\n",
    "fsetup='../template/cardiacSPECT.json'\n",
    "with open(fsetup,'r') as f:\n",
    "    setup=json.load(f)\n",
    "\n",
    "db=connectDB('merlin')\n",
    "fb=connectFB('merlin')\n",
    "    \n",
    "\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User: andrej studen CSRF: 7e7b8297519af11278f6488f37172742\n",
      "User: andrej studen CSRF: 6e50d5f2b859bce6a1ec3a8a8ebd44ec\n",
      "User: andrej studen CSRF: 5fb9a0b332ec7e483078994ed248a93d\n",
      "User: andrej studen CSRF: 0d361828ed40c1fa5495a8ea725de0ee\n",
      "User: andrej studen CSRF: 00d51e293aa93813c2f3b87f3a206027\n",
      "User: andrej studen CSRF: 7abed78b717ad2ac308176f531d2d486\n",
      "User: andrej studen CSRF: 4955be8909390a2b3499166f102e5ce1\n",
      "User: andrej studen CSRF: cb422e996659f3c1082a83d7ee5f74e7\n",
      "User: andrej studen CSRF: 7ab34a82eaf53459d2bdfbe746d05a7f\n",
      "User: andrej studen CSRF: 6ad6694b750cf3a7b39ab61f156d3c2f\n",
      "User: andrej studen CSRF: 31ae19cd278cec21d804885152e202c8\n",
      "User: andrej studen CSRF: 5e3657099176045a209989df75922a74\n"
     ]
    }
   ],
   "source": [
    "#doAnalysis\n",
    "\n",
    "#fill the Imaging1 fields, for multiple visits, change sequence number\n",
    "\n",
    "\n",
    "#0.) convert to NRRD \n",
    "# ~/software/src/SlicerLabkeyExtenstion/slicerScripts/runSlicer.sh convertToNRRD.py ../template/cardiacSPECT.json)\n",
    "\n",
    "#this is not performed at vangogh, use Analysis on merlin, convertNRRD.py with cardiacSPECT.json\n",
    "\n",
    "#1.) downloadFiles\n",
    "\n",
    "#getData.downloadPatientFiles(db,fb,setup)\n",
    "#this should also be done at vangogh... Figuring out how\n",
    "\n",
    "#2.) generate (c-cluster) centers\n",
    "\n",
    "#setup the analysis (class sizes, number of realizations)\n",
    "#nclass=[10,20,30]\n",
    "#nclass=[10]\n",
    "#nr=20\n",
    "#nr=1\n",
    "\n",
    "#takes multiple hours for nr=20, nclass=[10,20,30]\n",
    "#runs matlab - maybe the first time licensing will be checked and error will be returned\n",
    "#just run once\n",
    "#analysis.calculateCenters(db,setup,nclass,nr)\n",
    "\n",
    "#3.) now do analysis - global gets the average IVF fit (see point one of the slides for both branches of analysis)\n",
    "#this again takes multiple hours for four images\n",
    "#from what I see this could be shortend (since only global parameters are sought)\n",
    "#analysis.doAnalysis(db,setup,nclass,nr,'global')\n",
    "\n",
    "#4.) do cluster analysis (bullets two and three under ML analysis)\n",
    "#analysis.doAnalysis(db,setup,nclass,nr,'IVF')\n",
    "\n",
    "#5.) sort out the segmentation points -> segmentation.ipynb and generate Segmentation.txt files read by pixelAnalysis\n",
    "\n",
    "#6.) run doPixelAnalysisIVF to calculate parameters from locations\n",
    "sigma2=[0.1,1,4]\n",
    "#analysis.doPixelAnalysis(db,setup,sigma2)\n",
    "\n",
    "#7.) store summary to database\n",
    "#na=3\n",
    "#s2=0.1\n",
    "#summary=analysis.fullSummary(db,setup,nclass,nr,s2,na)\n",
    "#analysis.storeSummary(db,setup,summary,s2,na)\n",
    "\n",
    "summaryIVF=analysis.summaryPixelIVF(db,fb,setup)\n",
    "#print(summaryIVF)\n",
    "analysis.storeIVF(db,setup,summaryIVF)\n",
    "\n",
    "#8.) Plot regions through chart wizard on labkey, see also Summary R Report for error-plots on multi-class analysis\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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": 4
}