{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# A utility for the importing of patient cutouts from XiO\n", "\n", "This utility makes use of user provided XiO access credentials along with a patient ID and then returns the cutout X and Y coords along with applicator, energy, and server filepath for each plan found for that ID on the server.\n", "\n", "An example of using this object can be found [at the bottom of this notebook](#Example)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Copyright information\n", "\n", "Copyright © 2015 Simon Biggs\n", "\n", "This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.\n", "\n", "This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.\n", "\n", "You should have received a copy of the GNU Affero General Public License along with this program. If not, see http://www.gnu.org/licenses/." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Required modules" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import spur\n", "import numpy as np\n", "import shutil\n", "import re\n", "import os\n", "import paramiko" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# client = paramiko.client\n", "# client.load_system_host_keys()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The XiO utility object" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "class XiO_Utility(object):\n", " \n", " def __init__(self, **kwargs):\n", " \n", " self.patientID = str(kwargs['patientID'])\n", " \n", " self.hostname = kwargs['hostname']\n", " self.username = kwargs['username']\n", " self.password = kwargs['password']\n", " \n", " self.find_tel_files()\n", " self.sftp_tempfile_copy()\n", " self.parse_tel_file()\n", " self.remove_local_tel_files()\n", " \n", " \n", " def ssh_shell(self):\n", " return spur.SshShell(hostname=self.hostname, \n", " username=self.username, \n", " password=self.password)\n", " \n", " def find_tel_files(self): \n", " shell = self.ssh_shell()\n", " result = shell.run([\"find\", \"/FOCUS/rtp1/\", \"-regex\", \"^/.*\"+self.patientID+\".*.tel.1$\"])\n", " self.rtp1TelFilepaths = result.output.splitlines()\n", "\n", " shell = self.ssh_shell()\n", " result = shell.run([\"find\", \"/FOCUS/rtp2/\", \"-regex\", \"^/.*\"+self.patientID+\".*.tel.1$\"])\n", " self.rtp2TelFilepaths = result.output.splitlines()\n", "\n", " self.XiOTelFilepaths = self.rtp1TelFilepaths + self.rtp2TelFilepaths\n", " \n", " \n", " def sftp_tempfile_copy(self):\n", "\n", " self.localTelFilepaths = [0,] * len(self.XiOTelFilepaths)\n", "\n", " for i in range(len(self.XiOTelFilepaths)):\n", " self.localTelFilepaths[i] = \"tempImportedPlan_\"+str(i)\n", "\n", "\n", " for i, filepath in enumerate(self.XiOTelFilepaths):\n", " shell = self.ssh_shell()\n", " with shell.open(filepath, \"r\") as remote_file:\n", " with open(self.localTelFilepaths[i], \"w\") as local_file:\n", " shutil.copyfileobj(remote_file, local_file)\n", " \n", " \n", " def parse_tel_file(self):\n", " \n", " self.XCoords = []\n", " self.YCoords = []\n", " self.info = []\n", "\n", " for i, filepath in enumerate(self.localTelFilepaths):\n", "\n", " with open(filepath, \"r\") as file:\n", " telFileContents = np.array(file.read().splitlines())\n", "\n", " electronPlanIndex = []\n", " electronPlanEnergy = []\n", "\n", " for j, item in enumerate(telFileContents):\n", " if re.search('.*MeVSynergy.*', item):\n", " electronPlanIndex += [j]\n", " electronPlanEnergy += [item]\n", "\n", " for j, index in enumerate(electronPlanIndex):\n", " cutoutCoordsSection = telFileContents[index+41::]\n", " cutoutCoordsStop = np.where(cutoutCoordsSection=='0')[0][0]\n", "\n", " cutoutCoords = cutoutCoordsSection[:cutoutCoordsStop]\n", " singleStringCoords = \",\".join(list(cutoutCoords))\n", " coordAsArray = np.fromstring(singleStringCoords, dtype=float, count=-1, sep=',')\n", "\n", " XCoords = coordAsArray[0::2]/10\n", " YCoords = coordAsArray[1::2]/10\n", "\n", " applicator = float(telFileContents[index+7].split('x')[0])/10\n", " appString = \"%dcm Applicator\" %(applicator,)\n", "\n", " collatedInfo = np.concatenate([[self.XiOTelFilepaths[i]],\n", " [electronPlanEnergy[j]],\n", " [appString]])\n", "\n", " self.XCoords += [XCoords]\n", " self.YCoords += [YCoords]\n", " self.info += [collatedInfo]\n", " \n", " def remove_local_tel_files(self):\n", " for filepath in self.localTelFilepaths:\n", " os.remove(filepath)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Execute no further if being imported\n", "\n", "Don't want to waste time running the example if this notebook is just being imported. The following cell triggers the notebook import to stop." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "'stop nbimport'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "hostname = 'hostname'\n", "username = 'username'\n", "password = 'password'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "patientID = 'number_goes_here'\n", "\n", "example = XiO_Utility(patientID=patientID,\n", " hostname=hostname,\n", " username=username,\n", " password=password)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "example.info[0][1::]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.plot(example.XCoords[0], example.YCoords[0])\n", "plt.axis(\"equal\")" ] } ], "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.5.0" } }, "nbformat": 4, "nbformat_minor": 0 }