{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import os\n",
    "import SimpleITK\n",
    "import numpy\n",
    "import matplotlib.pyplot\n",
    "import subprocess\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",
    "net=labkeyInterface.labkeyInterface()\n",
    "fconfig=os.path.join(os.path.expanduser('~'),'.labkey','network.json')\n",
    "net.init(fconfig)\n",
    "net.getCSRF()\n",
    "import labkeyFileBrowser\n",
    "sys.path.append(os.getcwd())\n",
    "import downloadPatient\n",
    "fb=labkeyFileBrowser.labkeyFileBrowser(net)\n",
    "project='dinamic_spect/Patients'\n",
    "locDir=os.path.join(os.path.expanduser('~'),'temp','dynamicSPECT')\n",
    "import labkeyDatabaseBrowser\n",
    "db=labkeyDatabaseBrowser.labkeyDB(net)\n",
    "ds=db.selectRows(project,'study','Imaging',[])\n",
    "patients=[row['aliasID'] for row in ds['rows']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#generate centers\n",
    "import subprocess\n",
    "\n",
    "nclass=10\n",
    "nsample=20\n",
    "patients=list(set(patients))\n",
    "scriptPath=os.path.join(os.path.expanduser('~'),\\\n",
    "                        'scripts','dynamicSPECT','generateCenters.sh')\n",
    "\n",
    "for pId in patients:\n",
    "    if pId.find('8')==0:\n",
    "        continue\n",
    "    print(subprocess.run([scriptPath,pId,str(nclass),str(nsample)], \\\n",
    "                         check=True, stdout=subprocess.PIPE).stdout)\n",
    "    \n",
    "   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#doAnalysis\n",
    "import subprocess\n",
    "\n",
    "patients=list(set(patients))\n",
    "runScript='doAnalysis.sh'\n",
    "#this is if IVF is taken from clusters\n",
    "scriptPath=os.path.join(os.path.expanduser('~'),\\\n",
    "                        'scripts','dynamicSPECT',runScript)\n",
    "#patients1=['10KFMIR']\n",
    "patients1=patients\n",
    "for pId in patients1:\n",
    "    if pId.find('8')==0:\n",
    "        continue\n",
    "    for nclass in [10,20,30]:\n",
    "        for realizationId in numpy.arange(20):\n",
    "            print('{}/{} {}'.format(pId,nclass,realizationId+1))\n",
    "            subprocess.run([scriptPath,pId,str(nclass),str(realizationId)],\\\n",
    "                         check=True, stdout=subprocess.PIPE)\n",
    "    \n",
    "   "
   ]
  }
 ],
 "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}