{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Requirements\n", "\n", "- Installed git. More on how to use and install git can be found [here][git]. \n", "- A zip of the certificate. Particularly, you'll need the crt and key files.\n", "- A labkey access configuration file. A sample for [linux][linuxConfig] or [Windows][windowsConfig]. The file paths point to locations of extracted files from certificate zip on your disk.\n", "\n", "[git]: https://git-scm.com/\n", "[linuxConfig]: https://git0.fmf.uni-lj.si/studen/labkeyInterface/src/master/network-config-sample.json\n", "[windowsConfig]: https://git0.fmf.uni-lj.si/studen/labkeyInterface/src/master/network-config-sample.json" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "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']}, '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" ] } ], "source": [ "#load required libraries\n", "import sys\n", "import os\n", "import SimpleITK\n", "import numpy\n", "import matplotlib.pyplot\n", "import chardet\n", "import json\n", "\n", "#you should get nixSuite via git clone https://git0.fmf.uni-lj.si/studen/nixSuite.git\n", "#if you don't put it to $HOME/software/src/, you should update the path\n", "nixSuite=os.path.join(os.path.expanduser('~'),'software','src','nixSuite')\n", "sys.path.append(os.path.join(nixSuite,'wrapper'))\n", "import nixWrapper\n", "nixWrapper.loadLibrary('labkeyInterface')\n", "import labkeyInterface\n", "import labkeyDatabaseBrowser\n", "import labkeyFileBrowser\n", "\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "User: andrej studen CSRF: 47d5908e07de9bd6284a4b4e0b275f46\n" ] }, { "data": { "text/plain": [ "'47d5908e07de9bd6284a4b4e0b275f46'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#check connectivity. This checks the configuration in $HOME/.labkey/network.json, \n", "#where paths to certificates are stored\n", "net=labkeyInterface.labkeyInterface()\n", "#fconfig=os.path.join(os.path.expanduser('~'),'.labkey','network.json')\n", "fconfig=os.path.join(os.path.expanduser('~'),'.labkey','labkey-public.json')\n", "net.init(fconfig)\n", "#this reports the certificate used\n", "#print('Using: {}'.format(net.connectionConfig['SSL']['user']))\n", "#This gets a deafult CSRF code; It should report user name plus a long string of random hex numbers\n", "net.getCSRF()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'_labkeyurl_GIToxicity': '/labkey/hypoAfrica/Study/list-details.view?listId=118&pk=1', 'GIToxicity': '1 - Increased frequency', 'Key': 2}\n", "User: andrej studen CSRF: 14590d2dec9a9d81f40f1e27d737ae36\n", "b'{\\n \"rowsAffected\" : 1,\\n \"queryName\" : \"enumGIToxicity\",\\n \"schemaName\" : \"lists\",\\n \"containerPath\" : \"/hypoAfrica/Study\",\\n \"rows\" : [ {\\n \"EntityId\" : \"e4f318fc-2d41-103b-b6f1-9caba13b9e14\",\\n \"GIToxicity\" : \"1 - Increased frequency\",\\n \"Key\" : 2\\n } ],\\n \"command\" : \"insert\"\\n}'\n", "{'_labkeyurl_GIToxicity': '/labkey/hypoAfrica/Study/list-details.view?listId=118&pk=2', 'GIToxicity': '1 - Rectal discomfort not requiring analgesics', 'Key': 3}\n", "User: andrej studen CSRF: 71cad45af24fdc3a7ef6c65c36f3dd04\n", "b'{\\n \"rowsAffected\" : 1,\\n \"queryName\" : \"enumGIToxicity\",\\n \"schemaName\" : \"lists\",\\n \"containerPath\" : \"/hypoAfrica/Study\",\\n \"rows\" : [ {\\n \"EntityId\" : \"e4f31912-2d41-103b-b6f1-9caba13b9e14\",\\n \"GIToxicity\" : \"1 - Rectal discomfort not requiring analgesics\",\\n \"Key\" : 3\\n } ],\\n \"command\" : \"insert\"\\n}'\n", "{'_labkeyurl_GIToxicity': '/labkey/hypoAfrica/Study/list-details.view?listId=118&pk=3', 'GIToxicity': '2 - Diarrhoea requiring parasympatholytic drugs', 'Key': 4}\n", "User: andrej studen CSRF: 66737420b224b3bab5fc861654a31b9e\n", "b'{\\n \"rowsAffected\" : 1,\\n \"queryName\" : \"enumGIToxicity\",\\n \"schemaName\" : \"lists\",\\n \"containerPath\" : \"/hypoAfrica/Study\",\\n \"rows\" : [ {\\n \"EntityId\" : \"e4f31928-2d41-103b-b6f1-9caba13b9e14\",\\n \"GIToxicity\" : \"2 - Diarrhoea requiring parasympatholytic drugs\",\\n \"Key\" : 4\\n } ],\\n \"command\" : \"insert\"\\n}'\n", "{'_labkeyurl_GIToxicity': '/labkey/hypoAfrica/Study/list-details.view?listId=118&pk=4', 'GIToxicity': '2 - Mucous discharge not requiring sanitary pads', 'Key': 5}\n", "User: andrej studen CSRF: af98c30b8b12d59103f8e30cb6c7df75\n", "b'{\\n \"rowsAffected\" : 1,\\n \"queryName\" : \"enumGIToxicity\",\\n \"schemaName\" : \"lists\",\\n \"containerPath\" : \"/hypoAfrica/Study\",\\n \"rows\" : [ {\\n \"EntityId\" : \"e4f3193e-2d41-103b-b6f1-9caba13b9e14\",\\n \"GIToxicity\" : \"2 - Mucous discharge not requiring sanitary pads\",\\n \"Key\" : 5\\n } ],\\n \"command\" : \"insert\"\\n}'\n", "{'_labkeyurl_GIToxicity': '/labkey/hypoAfrica/Study/list-details.view?listId=118&pk=5', 'GIToxicity': '2 - Rectal or abdominal pains requiring analgesics', 'Key': 6}\n", "User: andrej studen CSRF: 7a0f888542075e3333362ebbfcf15ca5\n", "b'{\\n \"rowsAffected\" : 1,\\n \"queryName\" : \"enumGIToxicity\",\\n \"schemaName\" : \"lists\",\\n \"containerPath\" : \"/hypoAfrica/Study\",\\n \"rows\" : [ {\\n \"EntityId\" : \"e4f31954-2d41-103b-b6f1-9caba13b9e14\",\\n \"GIToxicity\" : \"2 - Rectal or abdominal pains requiring analgesics\",\\n \"Key\" : 6\\n } ],\\n \"command\" : \"insert\"\\n}'\n", "{'_labkeyurl_GIToxicity': '/labkey/hypoAfrica/Study/list-details.view?listId=118&pk=6', 'GIToxicity': '3 - Diarrhoea requiring parenteral support', 'Key': 7}\n", "User: andrej studen CSRF: f1d7b6d7555fbb9ec759c092708ec82b\n", "b'{\\n \"rowsAffected\" : 1,\\n \"queryName\" : \"enumGIToxicity\",\\n \"schemaName\" : \"lists\",\\n \"containerPath\" : \"/hypoAfrica/Study\",\\n \"rows\" : [ {\\n \"EntityId\" : \"e4f3196a-2d41-103b-b6f1-9caba13b9e14\",\\n \"GIToxicity\" : \"3 - Diarrhoea requiring parenteral support\",\\n \"Key\" : 7\\n } ],\\n \"command\" : \"insert\"\\n}'\n", "{'_labkeyurl_GIToxicity': '/labkey/hypoAfrica/Study/list-details.view?listId=118&pk=7', 'GIToxicity': '3 - Severe mucous or blood discharge necessitating use of sanitary pads', 'Key': 8}\n", "User: andrej studen CSRF: a77d179cf73e8e9bbad4fc096a4154ca\n", "b'{\\n \"rowsAffected\" : 1,\\n \"queryName\" : \"enumGIToxicity\",\\n \"schemaName\" : \"lists\",\\n \"containerPath\" : \"/hypoAfrica/Study\",\\n \"rows\" : [ {\\n \"EntityId\" : \"e4f31980-2d41-103b-b6f1-9caba13b9e14\",\\n \"GIToxicity\" : \"3 - Severe mucous or blood discharge necessitating use of sanitary pads\",\\n \"Key\" : 8\\n } ],\\n \"command\" : \"insert\"\\n}'\n", "{'_labkeyurl_GIToxicity': '/labkey/hypoAfrica/Study/list-details.view?listId=118&pk=8', 'GIToxicity': '3 - Abdominal distension (X ray distended loops)', 'Key': 9}\n", "User: andrej studen CSRF: 94bd81cb9315cfdfc466fec3fdb03840\n", "b'{\\n \"rowsAffected\" : 1,\\n \"queryName\" : \"enumGIToxicity\",\\n \"schemaName\" : \"lists\",\\n \"containerPath\" : \"/hypoAfrica/Study\",\\n \"rows\" : [ {\\n \"EntityId\" : \"e4f31996-2d41-103b-b6f1-9caba13b9e14\",\\n \"GIToxicity\" : \"3 - Abdominal distension (X ray distended loops)\",\\n \"Key\" : 9\\n } ],\\n \"command\" : \"insert\"\\n}'\n", "{'_labkeyurl_GIToxicity': '/labkey/hypoAfrica/Study/list-details.view?listId=118&pk=9', 'GIToxicity': '4 - Obstruction', 'Key': 10}\n", "User: andrej studen CSRF: 0743498a5495fa1215d56e5df6ae8e96\n", "b'{\\n \"rowsAffected\" : 1,\\n \"queryName\" : \"enumGIToxicity\",\\n \"schemaName\" : \"lists\",\\n \"containerPath\" : \"/hypoAfrica/Study\",\\n \"rows\" : [ {\\n \"EntityId\" : \"e4f319ac-2d41-103b-b6f1-9caba13b9e14\",\\n \"GIToxicity\" : \"4 - Obstruction\",\\n \"Key\" : 10\\n } ],\\n \"command\" : \"insert\"\\n}'\n", "{'_labkeyurl_GIToxicity': '/labkey/hypoAfrica/Study/list-details.view?listId=118&pk=10', 'GIToxicity': '4 - Fistula formation', 'Key': 11}\n", "User: andrej studen CSRF: 0097d446ce5f0c279a39a65d476d9557\n", "b'{\\n \"rowsAffected\" : 1,\\n \"queryName\" : \"enumGIToxicity\",\\n \"schemaName\" : \"lists\",\\n \"containerPath\" : \"/hypoAfrica/Study\",\\n \"rows\" : [ {\\n \"EntityId\" : \"e4f319c2-2d41-103b-b6f1-9caba13b9e14\",\\n \"GIToxicity\" : \"4 - Fistula formation\",\\n \"Key\" : 11\\n } ],\\n \"command\" : \"insert\"\\n}'\n", "{'_labkeyurl_GIToxicity': '/labkey/hypoAfrica/Study/list-details.view?listId=118&pk=11', 'GIToxicity': '4 - GI bleeding requiring blood transfusion', 'Key': 12}\n", "User: andrej studen CSRF: e65984a9fee115e23f8e68c4d0854b07\n", "b'{\\n \"rowsAffected\" : 1,\\n \"queryName\" : \"enumGIToxicity\",\\n \"schemaName\" : \"lists\",\\n \"containerPath\" : \"/hypoAfrica/Study\",\\n \"rows\" : [ {\\n \"EntityId\" : \"e4f319d8-2d41-103b-b6f1-9caba13b9e14\",\\n \"GIToxicity\" : \"4 - GI bleeding requiring blood transfusion\",\\n \"Key\" : 12\\n } ],\\n \"command\" : \"insert\"\\n}'\n", "{'_labkeyurl_GIToxicity': '/labkey/hypoAfrica/Study/list-details.view?listId=118&pk=12', 'GIToxicity': '4 - Abdominal pains/tenesmus requiring decompression /bowel diversion', 'Key': 13}\n", "User: andrej studen CSRF: 28e37edbcf0a9f874b2528b376e4c101\n", "b'{\\n \"rowsAffected\" : 1,\\n \"queryName\" : \"enumGIToxicity\",\\n \"schemaName\" : \"lists\",\\n \"containerPath\" : \"/hypoAfrica/Study\",\\n \"rows\" : [ {\\n \"EntityId\" : \"e4f319ee-2d41-103b-b6f1-9caba13b9e14\",\\n \"GIToxicity\" : \"4 - Abdominal pains/tenesmus requiring decompression /bowel diversion\",\\n \"Key\" : 13\\n } ],\\n \"command\" : \"insert\"\\n}'\n", "{'_labkeyurl_GIToxicity': '/labkey/hypoAfrica/Study/list-details.view?listId=118&pk=13', 'GIToxicity': '1 - Change in quality of bowel habits not requiring medications', 'Key': 14}\n", "User: andrej studen CSRF: ff16decddf8b73067feebbafb0f216cc\n", "b'{\\n \"rowsAffected\" : 1,\\n \"queryName\" : \"enumGIToxicity\",\\n \"schemaName\" : \"lists\",\\n \"containerPath\" : \"/hypoAfrica/Study\",\\n \"rows\" : [ {\\n \"EntityId\" : \"b8349014-2de2-103b-b6f1-9caba13b9e14\",\\n \"GIToxicity\" : \"1 - Change in quality of bowel habits not requiring medications\",\\n \"Key\" : 14\\n } ],\\n \"command\" : \"insert\"\\n}'\n" ] } ], "source": [ "db=labkeyDatabaseBrowser.labkeyDB(net)\n", "fb=labkeyFileBrowser.labkeyFileBrowser(net)\n", "#select a project\n", "schema='lists'\n", "project='hypoAfrica/Study'\n", "query='enumGIToxicity1'\n", "queryOut='enumGIToxicity'\n", "#idFilter={'variable':'ParticipantId','value':'VUB_PA3','oper':'eq'}\n", "#empty filter\n", "qFilter=[]\n", "#qFilter can be a list of filters used in conjugation (logical AND)\n", "#qFilter=[idFilter]\n", "ds=db.selectRows(project,schema,query,qFilter)\n", "rows=ds['rows']\n", "lut={i+1:i+2 for i in range(len(rows))}\n", "for row in rows:\n", " #row as a dictionary\n", " #print(row)\n", " #update row\n", " #depending on field type adjust newValue\n", " #row['crfSponsorMode']='READ'\n", " #fn=row['formName']\n", " #lut={4:1,5:11,6:3,7:4,8:5,9:6,10:2}\n", " #for v in lut:\n", " # if fn==v:\n", " # row['formName']=lut[v]\n", " # break\n", " row['Key']=lut[int(row['Key'])]\n", " #print the new row\n", " print(row)\n", " #change the value of the field in database \n", " #if field is not in database, it will NOT be added and no changes will occur\n", " st=db.modifyRows('insert',project,schema,queryOut,[row]) \n", " print(st)\n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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 }