Skip to content
Snippets Groups Projects
pybis_example.ipynb 16.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • {
     "cells": [
      {
       "cell_type": "code",
       "execution_count": 1,
       "metadata": {},
       "outputs": [],
       "source": [
        "from pybis import Openbis\n",
        "import getpass\n",
        "import matplotlib.pyplot as plt\n",
        "%matplotlib inline"
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "### Connecting to openBIS"
       ]
      },
      {
       "cell_type": "code",
    
       "execution_count": 14,
    
       "metadata": {},
       "outputs": [],
       "source": [
        "o = Openbis('https://limb.ethz.ch/openbis:8443', verify_certificates=False)"
       ]
      },
      {
       "cell_type": "code",
    
       "execution_count": 15,
    
       "metadata": {},
       "outputs": [
        {
         "name": "stdin",
         "output_type": "stream",
         "text": [
          " ·····················\n"
         ]
        }
       ],
       "source": [
        "username = 'hluetcke'\n",
        "pw = getpass.getpass()"
       ]
      },
      {
       "cell_type": "code",
    
       "execution_count": 16,
    
       "metadata": {},
       "outputs": [],
       "source": [
        "o.login(username, pw, save_token=True)   # saves the session token in ~/.pybis/example.com.token\n",
        "del pw"
       ]
      },
      {
       "cell_type": "code",
    
       "execution_count": 17,
    
       "metadata": {},
       "outputs": [
        {
         "data": {
          "text/plain": [
    
           "'hluetcke-181026151925182x3063F8B1C01218403A7F2B2FFC989331'"
    
         "execution_count": 17,
    
         "metadata": {},
         "output_type": "execute_result"
        }
       ],
       "source": [
        "o.token"
       ]
      },
      {
       "cell_type": "code",
    
       "execution_count": 18,
    
       "metadata": {},
       "outputs": [
        {
         "data": {
          "text/plain": [
           "True"
          ]
         },
    
         "execution_count": 18,
    
         "metadata": {},
         "output_type": "execute_result"
        }
       ],
       "source": [
        "o.is_session_active()"
       ]
      },
      {
       "cell_type": "code",
    
       "execution_count": 9,
    
       "metadata": {},
       "outputs": [
        {
         "data": {
          "text/html": [
           "<div>\n",
           "<style scoped>\n",
           "    .dataframe tbody tr th:only-of-type {\n",
           "        vertical-align: middle;\n",
           "    }\n",
           "\n",
           "    .dataframe tbody tr th {\n",
           "        vertical-align: top;\n",
           "    }\n",
           "\n",
           "    .dataframe thead th {\n",
           "        text-align: right;\n",
           "    }\n",
           "</style>\n",
           "<table border=\"1\" class=\"dataframe\">\n",
           "  <thead>\n",
           "    <tr style=\"text-align: right;\">\n",
           "      <th></th>\n",
           "      <th>code</th>\n",
           "      <th>downloadUrl</th>\n",
           "      <th>hostUrl</th>\n",
           "    </tr>\n",
           "  </thead>\n",
           "  <tbody>\n",
           "    <tr>\n",
           "      <th>0</th>\n",
           "      <td>DSS1</td>\n",
           "      <td>https://limb.ethz.ch:443/datastore_server</td>\n",
           "      <td>https://limb.ethz.ch:443</td>\n",
           "    </tr>\n",
           "  </tbody>\n",
           "</table>\n",
           "</div>"
          ],
          "text/plain": [
           "   code                                downloadUrl                   hostUrl\n",
           "0  DSS1  https://limb.ethz.ch:443/datastore_server  https://limb.ethz.ch:443"
          ]
         },
    
         "execution_count": 9,
    
         "metadata": {},
         "output_type": "execute_result"
        }
       ],
       "source": [
        "o.get_datastores()"
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "### Masterdata"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 8,
       "metadata": {},
       "outputs": [
        {
         "data": {
          "text/html": [
           "<div>\n",
           "<style scoped>\n",
           "    .dataframe tbody tr th:only-of-type {\n",
           "        vertical-align: middle;\n",
           "    }\n",
           "\n",
           "    .dataframe tbody tr th {\n",
           "        vertical-align: top;\n",
           "    }\n",
           "\n",
           "    .dataframe thead th {\n",
           "        text-align: right;\n",
           "    }\n",
           "</style>\n",
           "<table border=\"1\" class=\"dataframe\">\n",
           "  <thead>\n",
           "    <tr style=\"text-align: right;\">\n",
           "      <th></th>\n",
           "      <th>code</th>\n",
           "      <th>description</th>\n",
           "      <th>modificationDate</th>\n",
           "    </tr>\n",
           "  </thead>\n",
           "  <tbody>\n",
           "    <tr>\n",
           "      <th>0</th>\n",
           "      <td>IHC</td>\n",
           "      <td>Immunohistochemistry</td>\n",
           "      <td>2010-10-01 15:27:15</td>\n",
           "    </tr>\n",
           "    <tr>\n",
           "      <th>1</th>\n",
           "      <td>LACZ</td>\n",
           "      <td>Experiments with lacZ staining.</td>\n",
           "      <td>2010-10-06 16:24:22</td>\n",
           "    </tr>\n",
           "    <tr>\n",
           "      <th>2</th>\n",
           "      <td>CULTURE</td>\n",
           "      <td>Culture</td>\n",
           "      <td>2010-08-19 08:49:11</td>\n",
           "    </tr>\n",
           "    <tr>\n",
           "      <th>3</th>\n",
           "      <td>ISH</td>\n",
           "      <td>in situ hybridization</td>\n",
           "      <td>2010-08-19 08:49:25</td>\n",
           "    </tr>\n",
           "    <tr>\n",
           "      <th>4</th>\n",
           "      <td>SKELETAL</td>\n",
           "      <td>Skeletal</td>\n",
           "      <td>2010-08-19 08:49:34</td>\n",
           "    </tr>\n",
           "    <tr>\n",
           "      <th>5</th>\n",
           "      <td>METHODS</td>\n",
           "      <td>Folder to store protocols</td>\n",
           "      <td>2016-05-24 16:36:12</td>\n",
           "    </tr>\n",
           "    <tr>\n",
           "      <th>6</th>\n",
           "      <td>PAPER</td>\n",
           "      <td>Experiments from a paper.</td>\n",
           "      <td>2010-11-12 12:08:37</td>\n",
           "    </tr>\n",
           "    <tr>\n",
           "      <th>7</th>\n",
           "      <td>LIGHTSHEET</td>\n",
           "      <td>Lightsheet imaging</td>\n",
           "      <td>2018-05-31 13:29:04</td>\n",
           "    </tr>\n",
           "    <tr>\n",
           "      <th>8</th>\n",
           "      <td>SAMPLE</td>\n",
           "      <td>For Sample registration.</td>\n",
           "      <td>2010-11-22 10:28:53</td>\n",
           "    </tr>\n",
           "    <tr>\n",
           "      <th>9</th>\n",
           "      <td>MATERIALS</td>\n",
           "      <td>Folder to store biological and chemical samples</td>\n",
           "      <td>2016-05-24 16:35:51</td>\n",
           "    </tr>\n",
           "  </tbody>\n",
           "</table>\n",
           "</div>"
          ],
          "text/plain": [
           "    code        description                                      modificationDate\n",
           "--  ----------  -----------------------------------------------  -------------------\n",
           " 0  IHC         Immunohistochemistry                             2010-10-01 15:27:15\n",
           " 1  LACZ        Experiments with lacZ staining.                  2010-10-06 16:24:22\n",
           " 2  CULTURE     Culture                                          2010-08-19 08:49:11\n",
           " 3  ISH         in situ hybridization                            2010-08-19 08:49:25\n",
           " 4  SKELETAL    Skeletal                                         2010-08-19 08:49:34\n",
           " 5  METHODS     Folder to store protocols                        2016-05-24 16:36:12\n",
           " 6  PAPER       Experiments from a paper.                        2010-11-12 12:08:37\n",
           " 7  LIGHTSHEET  Lightsheet imaging                               2018-05-31 13:29:04\n",
           " 8  SAMPLE      For Sample registration.                         2010-11-22 10:28:53\n",
           " 9  MATERIALS   Folder to store biological and chemical samples  2016-05-24 16:35:51"
          ]
         },
         "execution_count": 8,
         "metadata": {},
         "output_type": "execute_result"
        }
       ],
       "source": [
        "o.get_experiment_types()"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
        "o.get_sample_types()"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
        "o.get_sample_type('PLATE')"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
        "o.get_material_types()"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
        "o.get_dataset_types()"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
        "o.get_terms()"
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "### List and get datasets"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
        "datasets = o.get_datasets(type='HISTOLOGY')"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
        "counter = 0\n",
        "for ds in datasets:\n",
        "    print(ds)\n",
        "    counter += 1\n",
        "    if counter > 10:\n",
        "        break"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
        "ds = o.get_dataset('20101105142920015-6525')"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
        "ds.get_files(start_folder='original')"
       ]
      },
    
    Henry Luetcke's avatar
    Henry Luetcke committed
      {
       "cell_type": "code",
       "execution_count": 11,
       "metadata": {},
       "outputs": [],
       "source": [
        "space = o.get_space('MATLAB_TEST')"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 12,
       "metadata": {},
       "outputs": [
        {
         "data": {
          "text/html": [
           "\n",
           "            <table border=\"1\" class=\"dataframe\">\n",
           "            <thead>\n",
           "                <tr style=\"text-align: right;\">\n",
           "                <th>attribute</th>\n",
           "                <th>value</th>\n",
           "                </tr>\n",
           "            </thead>\n",
           "            <tbody>\n",
           "        <tr> <td>code</td> <td>MATLAB_TEST</td> </tr><tr> <td>permId</td> <td>MATLAB_TEST</td> </tr><tr> <td>description</td> <td>test space for Matlab access to openBIS</td> </tr><tr> <td>registrator</td> <td>hluetcke</td> </tr><tr> <td>registrationDate</td> <td>2018-08-17 10:39:05</td> </tr><tr> <td>modificationDate</td> <td>2018-08-17 10:39:05</td> </tr>\n",
           "            </tbody>\n",
           "            </table>\n",
           "        "
          ],
          "text/plain": [
           "attribute         value\n",
           "----------------  ---------------------------------------\n",
           "code              MATLAB_TEST\n",
           "permId            MATLAB_TEST\n",
           "description       test space for Matlab access to openBIS\n",
           "registrator       hluetcke\n",
           "registrationDate  2018-08-17 10:39:05\n",
           "modificationDate  2018-08-17 10:39:05"
          ]
         },
         "execution_count": 12,
         "metadata": {},
         "output_type": "execute_result"
        }
       ],
       "source": [
        "space.delete"
       ]
      },
    
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "### Download and import files"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
        "ds.download(files='original/441_x40001.tif', destination='data', wait_until_finished=True)"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
        "plt.imshow(plt.imread('data/20101105142920015-6525/original/441_x40001.tif'))"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
        "o.logout()"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
        "o.get_experiment_types()"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
        "o.get_dataset_types()"
       ]
    
      },
      {
       "cell_type": "code",
       "execution_count": 28,
       "metadata": {},
       "outputs": [
        {
         "data": {
          "text/html": [
           "<div>\n",
           "<style scoped>\n",
           "    .dataframe tbody tr th:only-of-type {\n",
           "        vertical-align: middle;\n",
           "    }\n",
           "\n",
           "    .dataframe tbody tr th {\n",
           "        vertical-align: top;\n",
           "    }\n",
           "\n",
           "    .dataframe thead th {\n",
           "        text-align: right;\n",
           "    }\n",
           "</style>\n",
           "<table border=\"1\" class=\"dataframe\">\n",
           "  <thead>\n",
           "    <tr style=\"text-align: right;\">\n",
           "      <th></th>\n",
           "      <th>identifier</th>\n",
           "      <th>permId</th>\n",
           "      <th>leader</th>\n",
           "      <th>registrator</th>\n",
           "      <th>registrationDate</th>\n",
           "      <th>modifier</th>\n",
           "      <th>modificationDate</th>\n",
           "    </tr>\n",
           "  </thead>\n",
           "  <tbody>\n",
           "    <tr>\n",
           "      <th>0</th>\n",
           "      <td>/MATLAB_TEST/TEST_PROJECT</td>\n",
           "      <td>20180817104532621-9268</td>\n",
           "      <td>None</td>\n",
           "      <td>hluetcke</td>\n",
           "      <td>2018-08-17 10:45:33</td>\n",
           "      <td>hluetcke</td>\n",
           "      <td>2018-08-17 10:45:33</td>\n",
           "    </tr>\n",
           "  </tbody>\n",
           "</table>\n",
           "</div>"
          ],
          "text/plain": [
           "    identifier                 permId                  leader    registrator    registrationDate     modifier    modificationDate\n",
           "--  -------------------------  ----------------------  --------  -------------  -------------------  ----------  -------------------\n",
           " 0  /MATLAB_TEST/TEST_PROJECT  20180817104532621-9268  None      hluetcke       2018-08-17 10:45:33  hluetcke    2018-08-17 10:45:33"
          ]
         },
         "execution_count": 28,
         "metadata": {},
         "output_type": "execute_result"
        }
       ],
       "source": [
        "o.get_projects(space='MATLAB_TEST', code=None)"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 22,
       "metadata": {},
       "outputs": [],
       "source": [
        "project = o.new_project(space='MATLAB_TEST', code='ANOTHER_TEST', description='TGIF')"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 23,
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "Project successfully created.\n"
         ]
        },
        {
         "data": {
          "text/html": [
           "\n",
           "            <table border=\"1\" class=\"dataframe\">\n",
           "            <thead>\n",
           "                <tr style=\"text-align: right;\">\n",
           "                <th>attribute</th>\n",
           "                <th>value</th>\n",
           "                </tr>\n",
           "            </thead>\n",
           "            <tbody>\n",
           "        <tr> <td>code</td> <td>ANOTHER_TEST</td> </tr><tr> <td>description</td> <td>TGIF</td> </tr><tr> <td>permId</td> <td>20181026152148956-9272</td> </tr><tr> <td>identifier</td> <td>/MATLAB_TEST/ANOTHER_TEST</td> </tr><tr> <td>space</td> <td>MATLAB_TEST</td> </tr><tr> <td>leader</td> <td></td> </tr><tr> <td>registrator</td> <td>hluetcke</td> </tr><tr> <td>registrationDate</td> <td>2018-10-26 15:21:49</td> </tr><tr> <td>modifier</td> <td></td> </tr><tr> <td>modificationDate</td> <td>2018-10-26 15:21:49</td> </tr><tr><td>attachments</td><td></td></tr>\n",
           "            </tbody>\n",
           "            </table>\n",
           "        "
          ],
          "text/plain": [
           "attribute         value\n",
           "----------------  -------------------------\n",
           "code              ANOTHER_TEST\n",
           "description       TGIF\n",
           "permId            20181026152148956-9272\n",
           "identifier        /MATLAB_TEST/ANOTHER_TEST\n",
           "space             MATLAB_TEST\n",
           "leader\n",
           "registrator       hluetcke\n",
           "registrationDate  2018-10-26 15:21:49\n",
           "modifier\n",
           "modificationDate  2018-10-26 15:21:49"
          ]
         },
         "execution_count": 23,
         "metadata": {},
         "output_type": "execute_result"
        }
       ],
       "source": [
        "project.save()"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 27,
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "Project 20181026152148956-9272 successfully deleted.\n"
         ]
        }
       ],
       "source": [
        "project.delete('just a test')"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": []
    
      }
     ],
     "metadata": {
      "kernelspec": {
       "display_name": "Python [default]",
       "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.6.5"
    
      }
     },
     "nbformat": 4,
     "nbformat_minor": 2
    }