{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Demo of the instrument database API" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Imports " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "jupyter": { "outputs_hidden": false }, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "from instrumentdatabaseapi import instrumentdatabaseapi as API" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## Initialize the API" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setup the repository with defaults" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "jupyter": { "outputs_hidden": false }, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "repo = API.Repository()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Get informations about the repository" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "URL: https://github.com/PaNOSC-ViNYL/instrument_database.git\n", "local_dir: /dev/shm/instrumentDBAPI/\n" ] } ], "source": [ " print(repo)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Initialize the repository making a copy in local" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "ename": "GitCommandError", "evalue": "Cmd('git') failed due to: exit code(128)\n cmdline: git clone -v --branch=shervin https://github.com/PaNOSC-ViNYL/instrument_database.git ./local_repo/\n stderr: 'fatal: destination path './local_repo' already exists and is not an empty directory.\n'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mGitCommandError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mrepo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlocal_repo\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"./local_repo/\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/tmp/instrumentDataBaseAPI/instrumentdatabaseapi/instrumentdatabaseapi.py\u001b[0m in \u001b[0;36minit\u001b[0;34m(self, url, local_repo, branch)\u001b[0m\n\u001b[1;32m 53\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__local_repo\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlocal_repo\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 54\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 55\u001b[0;31m \u001b[0mrepo\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgit\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mRepo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclone_from\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__url\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__local_repo\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbranch\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mbranch\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 56\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mrepo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbare\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 57\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__institutes_absdir\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/.local/lib/python3.6/site-packages/git/repo/base.py\u001b[0m in \u001b[0;36mclone_from\u001b[0;34m(cls, url, to_path, progress, env, multi_options, **kwargs)\u001b[0m\n\u001b[1;32m 1109\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0menv\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1110\u001b[0m \u001b[0mgit\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mupdate_environment\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0menv\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1111\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mcls\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_clone\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgit\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mto_path\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mGitCmdObjectDB\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprogress\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmulti_options\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1112\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1113\u001b[0m def archive(self, ostream: Union[TextIO, BinaryIO], treeish: Optional[str] = None,\n", "\u001b[0;32m~/.local/lib/python3.6/site-packages/git/repo/base.py\u001b[0m in \u001b[0;36m_clone\u001b[0;34m(cls, git, url, path, odb_default_type, progress, multi_options, **kwargs)\u001b[0m\n\u001b[1;32m 1047\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1048\u001b[0m \u001b[0mlog\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdebug\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Cmd(%s)'s unused stdout: %s\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcmdline\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstdout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1049\u001b[0;31m \u001b[0mfinalize_process\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mproc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstderr\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mstderr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1050\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1051\u001b[0m \u001b[0;31m# our git command could have a different working dir than our actual\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/.local/lib/python3.6/site-packages/git/util.py\u001b[0m in \u001b[0;36mfinalize_process\u001b[0;34m(proc, **kwargs)\u001b[0m\n\u001b[1;32m 368\u001b[0m \u001b[0;34m\"\"\"Wait for the process (clone, fetch, pull or push) and handle its errors accordingly\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 369\u001b[0m \u001b[0;31m## TODO: No close proc-streams??\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 370\u001b[0;31m \u001b[0mproc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwait\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 371\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 372\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/.local/lib/python3.6/site-packages/git/cmd.py\u001b[0m in \u001b[0;36mwait\u001b[0;34m(self, stderr)\u001b[0m\n\u001b[1;32m 445\u001b[0m \u001b[0merrstr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mread_all_from_possibly_closed_stream\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mproc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstderr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 446\u001b[0m \u001b[0mlog\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdebug\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'AutoInterrupt wait stderr: %r'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0merrstr\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 447\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mGitCommandError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mremove_password_if_present\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstatus\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0merrstr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 448\u001b[0m \u001b[0;31m# END status handling\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 449\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mstatus\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mGitCommandError\u001b[0m: Cmd('git') failed due to: exit code(128)\n cmdline: git clone -v --branch=shervin https://github.com/PaNOSC-ViNYL/instrument_database.git ./local_repo/\n stderr: 'fatal: destination path './local_repo' already exists and is not an empty directory.\n'" ] } ], "source": [ "repo.init(local_repo=\"./local_repo/\")" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "URL: https://github.com/PaNOSC-ViNYL/instrument_database.git\n", "local_dir: /opt/panosc/instrument_database/\n" ] } ], "source": [ "repo = API.Repository(local_repo=\"/opt/panosc/instrument_database/\")\n", "print(repo)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Print list of available institutes" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "jupyter": { "outputs_hidden": false }, "pycharm": { "name": "#%%\n" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " - ILL\n" ] } ], "source": [ "repo.ls_institutes()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Run the backengine" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "jupyter": { "outputs_hidden": false }, "pycharm": { "name": "#%%\n" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "calculator.backengine()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Look at the data and store as hdf5" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "jupyter": { "outputs_hidden": false }, "pycharm": { "name": "#%%\n" } }, "outputs": [ { "data": { "text/plain": [ "array([[0.07471297, 0.00703423, 0.92835525, ..., 0.83050226, 0.93011749,\n", " 0.10575778],\n", " [0.30465388, 0.36400513, 0.48903381, ..., 0.0438568 , 0.39087367,\n", " 0.97940832],\n", " [0.61994805, 0.84566645, 0.42535347, ..., 0.94919735, 0.17939005,\n", " 0.74872113],\n", " ...,\n", " [0.22103305, 0.07844426, 0.8127275 , ..., 0.4273249 , 0.78210725,\n", " 0.59653636],\n", " [0.00889755, 0.40566176, 0.33960702, ..., 0.2634355 , 0.34068678,\n", " 0.99275201],\n", " [0.99495603, 0.18621833, 0.25057866, ..., 0.33598942, 0.10660242,\n", " 0.20565293]])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "calculator.data" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "jupyter": { "outputs_hidden": false }, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "calculator.saveH5(calculator.output_path)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Save the parameters to a human readable json file." ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "jupyter": { "outputs_hidden": false }, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "parameters.to_json(\"my_parameters.json\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Save calculator to binary dump." ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "jupyter": { "outputs_hidden": false }, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "dumpfile = calculator.dump()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load back parameters" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "jupyter": { "outputs_hidden": false }, "pycharm": { "name": "#%%\n" } }, "outputs": [ { "data": { "text/plain": [ "6000.0" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_parameters = Parameters.from_json(\"my_parameters.json\")" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "### Look ath the photon energy of the restored parameters." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "outputs_hidden": false }, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "new_parameters.photon_energy" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "### Reconstruct the dumped calculator." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "outputs_hidden": false }, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "reloaded_calculator = RandomImageCalculator(dumpfile=dumpfile)" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "### Query the data from the reconstructed calculator." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "outputs_hidden": false }, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "reloaded_calculator.data" ] }, { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "### Look at the photon energy of the reconstructed calculator." ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "jupyter": { "outputs_hidden": false }, "pycharm": { "name": "#%%\n" } }, "outputs": [ { "data": { "text/plain": [ "6000.0" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reloaded_calculator.parameters.photon_energy\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "outputs_hidden": false }, "pycharm": { "name": "#%%\n" } }, "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.6.8" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }