Skip to content
Snippets Groups Projects
pybis_example.ipynb 8.99 KiB
Newer Older
  • Learn to ignore specific revisions
  • {
     "cells": [
      {
       "cell_type": "code",
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "execution_count": null,
    
       "metadata": {},
       "outputs": [],
       "source": [
        "from pybis import Openbis\n",
    
        "import getpass"
    
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "### Connecting to openBIS"
       ]
      },
      {
       "cell_type": "code",
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "execution_count": null,
    
       "metadata": {},
       "outputs": [],
       "source": [
    
        "o = Openbis('https://127.0.0.1/openbis:8443', verify_certificates=False)"
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "execution_count": null,
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "metadata": {},
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "outputs": [],
    
        "username = 'hluetcke'\n",
    
        "pw = getpass.getpass()"
       ]
      },
      {
       "cell_type": "code",
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "execution_count": null,
    
       "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",
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "execution_count": null,
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "metadata": {},
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "outputs": [],
    
       "source": [
        "o.token"
       ]
      },
      {
       "cell_type": "code",
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "execution_count": null,
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "metadata": {},
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "outputs": [],
    
       "source": [
        "o.is_session_active()"
       ]
      },
      {
       "cell_type": "code",
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "execution_count": null,
       "metadata": {},
       "outputs": [],
    
       "source": [
        "o.get_datastores()"
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "### Masterdata"
       ]
      },
      {
       "cell_type": "code",
    
       "execution_count": null,
       "metadata": {},
       "outputs": [],
    
       "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": [
    
        "### Samples / objects"
    
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
    
        "o.get_sample_types()"
    
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
    
        "obj = o.new_object(type='UNKNOWN', space='MATLAB_TEST', code='12345')"
    
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
    
        "obj.save()"
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "execution_count": null,
    
       "metadata": {},
       "outputs": [],
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "source": [
        "sample = o.get_sample('/MATLAB_TEST/911922')"
       ]
      },
      {
       "cell_type": "code",
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "execution_count": null,
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "metadata": {},
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "outputs": [],
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "source": [
        "sample.delete('just a test')"
       ]
    
      },
      {
       "cell_type": "markdown",
       "metadata": {},
    
        "### List and get datasets"
    
    Henry Luetcke's avatar
    Henry Luetcke committed
      {
       "cell_type": "code",
    
       "execution_count": null,
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "metadata": {},
       "outputs": [],
       "source": [
    
        "datasets = o.get_datasets(type='HISTOLOGY')"
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       ]
      },
      {
       "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('20101105142049776-6512')"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
        "ds.get_files(start_folder='original')"
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       ]
      },
    
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
    
        "### Experiments / collections"
       ]
      },
      {
       "cell_type": "code",
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "execution_count": null,
       "metadata": {},
       "outputs": [],
    
       "source": [
        "o.get_experiments(\n",
        "    project='TEST_PROJECT',\n",
        "    space='MATLAB_TEST'\n",
        ")"
       ]
      },
      {
       "cell_type": "code",
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "execution_count": null,
       "metadata": {},
       "outputs": [],
    
       "source": [
        "experiment = o.get_experiment('/MATLAB_TEST/TEST_PROJECT/E1300')\n",
        "experiment"
       ]
      },
      {
       "cell_type": "code",
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "execution_count": null,
    
       "metadata": {},
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "outputs": [],
    
       "source": [
        "experiment.props"
       ]
      },
      {
       "cell_type": "code",
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "execution_count": null,
    
       "metadata": {},
       "outputs": [],
       "source": [
    
        "experiment = o.new_experiment(type='UNKNOWN', code='EXP123456789', project='/HLUETCKE/PLAYGROUND')"
    
       ]
      },
      {
       "cell_type": "code",
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "execution_count": null,
       "metadata": {},
       "outputs": [],
    
       "source": [
    
        "experiment.save()"
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "execution_count": null,
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "outputs": [],
    
       "source": [
    
    Henry Luetcke's avatar
    Henry Luetcke committed
        "experiment.delete('bla')"
    
      },
      {
       "cell_type": "code",
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "execution_count": null,
    
       "metadata": {},
       "outputs": [],
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "source": [
        "project = o.new_project('HLUETCKE', 'test_project', 'boring project')"
       ]
      },
      {
       "cell_type": "code",
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "execution_count": null,
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "metadata": {
        "tags": []
       },
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "outputs": [],
    
       "source": [
        "project.save()"
       ]
      },
    
    Henry Luetcke's avatar
    Henry Luetcke committed
      {
       "cell_type": "code",
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "execution_count": null,
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "metadata": {},
       "outputs": [],
       "source": [
        "experiment = o.new_experiment(type='UNKNOWN', code='EXP333', project='/HLUETCKE/TEST_PROJECT')"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "source": [
        "experiment.save()"
       ]
    
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
        "o.get_objects('SIS_HLUETCKE/')"
       ]
    
      },
      {
       "cell_type": "markdown",
       "metadata": {},
    
        "### Download and import files"
    
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
    
        "ds.download(files='', 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.get_experiment_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_projects(space='MATLAB_TEST', code=None)"
       ]
      },
      {
       "cell_type": "code",
    
       "execution_count": null,
    
       "metadata": {},
       "outputs": [],
       "source": [
        "project = o.new_project(space='MATLAB_TEST', code='ANOTHER_TEST', description='TGIF')"
       ]
      },
      {
       "cell_type": "code",
    
       "execution_count": null,
       "metadata": {},
       "outputs": [],
    
       "source": [
        "project.save()"
       ]
      },
      {
       "cell_type": "code",
    
       "execution_count": null,
       "metadata": {},
       "outputs": [],
    
       "source": [
        "project.delete('just a test')"
       ]
      },
    
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
    
    Henry Luetcke's avatar
    Henry Luetcke committed
        "### Create a new dataset"
    
       ]
      },
      {
       "cell_type": "code",
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "execution_count": null,
    
       "metadata": {},
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "outputs": [],
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "source": [
        "file_list = [\n",
        "    '/Users/Henry/Data/temp/file1.dat',\n",
        "    '/Users/Henry/Data/temp/file2.dat',\n",
        "    '/Users/Henry/Data/temp/file3.dat'\n",
        "]\n",
        "file_list"
       ]
      },
      {
       "cell_type": "code",
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "execution_count": null,
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "metadata": {},
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "outputs": [],
       "source": [
        "ds_new = o.new_dataset(type='ANALYZED_DATA', experiment='/HLUETCKE/DEMO/DEMO_EXP_1', sample='/HLUETCKE/DEMO/S43', files=file_list)"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": [
    
    Henry Luetcke's avatar
    Henry Luetcke committed
        "ds_new.save()"
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "### Logout"
       ]
      },
      {
       "cell_type": "code",
    
       "execution_count": null,
    
    Henry Luetcke's avatar
    Henry Luetcke committed
       "metadata": {},
    
       "outputs": [],
       "source": [
        "o.logout()"
       ]
      },
    
      {
       "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.9.1"
    
     "nbformat_minor": 4