{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "66ce7638-72f8-4ce2-8b61-981504694999",
   "metadata": {},
   "source": [
    "# IEMAP TUTORIAL\n",
    "**Python example code to use as a walkthrough in using iemap API** <br/> \n",
    "<img src=\"https://iemap-api.readthedocs.io/en/latest/_static/iemap_logo_nobg.png\"></img>\n",
    "### Full documentation is available [here](https://iemap-api.readthedocs.io/en/latest/index.html)\n",
    "> Note. API and its documentation is still in development. For any issue please report at ... "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7a7e7c7-8874-4343-83e7-38805ad1c0ef",
   "metadata": {},
   "source": [
    "## Important notes before starting\n",
    "- IEMAP API are implemented by a REST API architecture build-up by some endpoints that require authentication\n",
    "- **Prior to using IEMAP API users need to register [here](https://ai4mat.enea.it/dashboard/)**\n",
    "- It is possible to upload the project's metadata and files using the user interface (UI) at the link above or use the python module here described (**account registration is available only using the UI)**\n",
    "- The UI at the link above is temporary, a more complete and functional interface is in "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "330f5ad3-07b1-4a07-9f58-f3f5bcf7f596",
   "metadata": {},
   "source": [
    ">## Install IEMAP Python module using `pip install iemap` \n",
    "**Python version >= 3.10 is required**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d54fcaca-0396-4a55-8f67-bb1bf4619374",
   "metadata": {},
   "outputs": [],
   "source": [
    "# You can install within jupyter notebook using\n",
    "# if this is a first installation\n",
    "# ! pip install iemap\n",
    "# or to upgrade to latest release\n",
    "# ! pip install --upgrade  iemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a7843e68-f9ec-4c50-9ba0-84d9c7219e38",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import usefull modules\n",
    "import json\n",
    "import getpass\n",
    "from IPython.display import display\n",
    "import pandas as pd\n",
    "from pprint import pprint\n",
    "# from iemap_api import *\n",
    "from iemap import IEMAP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fdc27359-9004-4252-8416-f28b3ee3f2d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# To show the installed version on iemap module\n",
    "# !pip show iemap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e8b55e92-8ded-414d-b6f6-964910464619",
   "metadata": {},
   "source": [
    "### Step 1 - define account and login \n",
    "> **This is a mandatory step**, if you don't have an account yet register [here](https://ai4mat.enea.it/dashboard/)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4afda0f0-3ac5-4ae4-ab0f-e3b545b39d5c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Type password for user: sergio.ferlito@enea.it\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " ········\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "User successfully logged in!\n"
     ]
    }
   ],
   "source": [
    "# define user credentials\n",
    "user=\"sergio.ferlito@enea.it\"\n",
    "print(f\"\\nType password for user: {user}\")\n",
    "# type in your password in a masked input field\n",
    "pwd=getpass.getpass()\n",
    "api = IEMAP(user,pwd)\n",
    "# login \n",
    "api.login()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1ffa4fa-fb73-4aca-9415-d3e469974426",
   "metadata": {},
   "source": [
    "### Step 2 - Start defining a new project metadata\n",
    "> Project metadata have to be provided using a JSON file or python dict with a \"valid\" schema. Follow this [link](https://iemap-api.readthedocs.io/en/latest/getstart_partner.html#create-metadata) for the details."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8429436e-e2a3-45f3-9d4d-a6c2b5fde68c",
   "metadata": {},
   "source": [
    "> #### Note that   \n",
    "> - Main fields are: projects*,material*, process*, parameters, properties   \n",
    "> - \"project\" is build up by \"name\" and \"label\", both strings (\"name\" is required)   \n",
    "> - \"material\" is an object with several sub-fields, the only required is \"formula\"\n",
    "> - \"material\" **<sup>1</sup>** has subfields\"input\" and \"output\" both having a nested object made up by \"cell\" (List of List), \"lattice\" (Object/Dict), \"sites\" (List of List) and \"species\" (List) \n",
    "> - \"process\" is an object with fields \"method\" and \"isExperiment\" mandatory\n",
    "> - \"parameters\" and \"properties\" are both a List of objects made up by two fields \"name\"(string)  and \"value\"(string or numeric)\n",
    "\n",
    "> ***denotes mandatory fields**\n",
    "> **<sup>1</sup>** more on this in the example_metadata below"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "212e3c93-1be9-4c23-9fc3-1b020580492d",
   "metadata": {},
   "source": [
    "### Step 3 - Familiarize with the required JSON schema"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a931b9bb-52f9-43b0-b4e5-28f3598ebf54",
   "metadata": {},
   "outputs": [],
   "source": [
    "# load json example metadata\n",
    "# Opening JSON file\n",
    "f = open(\"metadata_example.json\")\n",
    "example_metadata = json.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "4775cb1c-8896-452d-8cec-e2296118a2ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'material': {'formula': 'Mn6Na12Ni6O24',\n",
      "              'input': {'cell': [[9.2958, 0.0, 0.0],\n",
      "                                 [-3.0986, 5.3669, 0.0],\n",
      "                                 [0.0, 0.0, 10.8981]],\n",
      "                        'lattice': {'a': 9.2958,\n",
      "                                    'alpha': 90.0,\n",
      "                                    'b': 6.197171739592183,\n",
      "                                    'beta': 120.0001508506647,\n",
      "                                    'c': 10.8981,\n",
      "                                    'gamma': 90.0},\n",
      "                        'sites': [[0.0, 1.789, 8.174],\n",
      "                                  [1.549, 0.894, 2.725],\n",
      "                                  [0.0, 0.0, 0.0],\n",
      "                                  [0.0, 0.0, 5.449],\n",
      "                                  [1.549, 0.894, -0.954],\n",
      "                                  [0.0, 1.789, 4.495],\n",
      "                                  [0.0, 1.789, 0.954],\n",
      "                                  [1.549, 0.894, 6.403],\n",
      "                                  [6.197, 1.789, 8.174],\n",
      "                                  [7.747, 0.894, 2.725],\n",
      "                                  [6.197, 0.0, 0.0],\n",
      "                                  [6.197, 0.0, 5.449],\n",
      "                                  [6.197, 1.789, 0.954],\n",
      "                                  [7.747, 0.894, 6.403],\n",
      "                                  [3.099, 0.0, 0.0],\n",
      "                                  [3.099, 0.0, 5.449],\n",
      "                                  [4.648, 0.894, -0.954],\n",
      "                                  [3.099, 1.789, 4.495],\n",
      "                                  [3.099, 1.789, 0.954],\n",
      "                                  [4.648, 0.894, 6.403],\n",
      "                                  [7.747, 0.894, -0.954],\n",
      "                                  [6.197, 1.789, 4.495],\n",
      "                                  [3.099, 1.789, 8.174],\n",
      "                                  [4.648, 0.894, 2.725],\n",
      "                                  [4.648, 4.472, 8.174],\n",
      "                                  [6.197, 3.578, 2.725],\n",
      "                                  [4.648, 4.472, 0.954],\n",
      "                                  [6.197, 3.578, 6.403],\n",
      "                                  [-1.549, 4.472, 8.174],\n",
      "                                  [-1.549, 4.472, 0.954],\n",
      "                                  [3.099, 3.578, -0.954],\n",
      "                                  [1.549, 4.472, 4.495],\n",
      "                                  [1.549, 4.472, 0.954],\n",
      "                                  [3.099, 3.578, 6.403],\n",
      "                                  [6.197, 3.578, -0.954],\n",
      "                                  [4.648, 4.472, 4.495],\n",
      "                                  [0.0, 3.578, -0.954],\n",
      "                                  [-1.549, 4.472, 4.495],\n",
      "                                  [0.0, 3.578, 6.403],\n",
      "                                  [4.648, 2.683, 0.0],\n",
      "                                  [4.648, 2.683, 5.449],\n",
      "                                  [-1.549, 2.683, 0.0],\n",
      "                                  [-1.549, 2.683, 5.449],\n",
      "                                  [1.549, 2.683, 0.0],\n",
      "                                  [1.549, 2.683, 5.449],\n",
      "                                  [0.0, 3.578, 2.725],\n",
      "                                  [1.549, 4.472, 8.174],\n",
      "                                  [3.099, 3.578, 2.725]],\n",
      "                        'species': ['Na',\n",
      "                                    'Na',\n",
      "                                    'Ni',\n",
      "                                    'Mn',\n",
      "                                    'O',\n",
      "                                    'O',\n",
      "                                    'O',\n",
      "                                    'O',\n",
      "                                    'Na',\n",
      "                                    'Na',\n",
      "                                    'Ni',\n",
      "                                    'Mn',\n",
      "                                    'O',\n",
      "                                    'O',\n",
      "                                    'Mn',\n",
      "                                    'Mn',\n",
      "                                    'O',\n",
      "                                    'O',\n",
      "                                    'O',\n",
      "                                    'O',\n",
      "                                    'O',\n",
      "                                    'O',\n",
      "                                    'Na',\n",
      "                                    'Na',\n",
      "                                    'Na',\n",
      "                                    'Na',\n",
      "                                    'O',\n",
      "                                    'O',\n",
      "                                    'Na',\n",
      "                                    'O',\n",
      "                                    'O',\n",
      "                                    'O',\n",
      "                                    'O',\n",
      "                                    'O',\n",
      "                                    'O',\n",
      "                                    'O',\n",
      "                                    'O',\n",
      "                                    'O',\n",
      "                                    'O',\n",
      "                                    'Ni',\n",
      "                                    'Ni',\n",
      "                                    'Ni',\n",
      "                                    'Mn',\n",
      "                                    'Ni',\n",
      "                                    'Mn',\n",
      "                                    'Na',\n",
      "                                    'Na',\n",
      "                                    'Na']},\n",
      "              'output': {'cell': [[9.4974797270795,\n",
      "                                   0.47810505533726,\n",
      "                                   0.029745479528267],\n",
      "                                  [-2.8915183359738,\n",
      "                                   5.0052744061721,\n",
      "                                   -0.00018372422247841],\n",
      "                                  [0.031023356615597,\n",
      "                                   0.019574039641932,\n",
      "                                   10.764796974635]],\n",
      "                         'lattice': {'a': 9.509552586940014,\n",
      "                                     'alpha': 89.99420734587778,\n",
      "                                     'b': 5.780454151890555,\n",
      "                                     'beta': 117.13279435162953,\n",
      "                                     'c': 10.764859474084856,\n",
      "                                     'gamma': 89.65063053116737},\n",
      "                         'sites': [[0.25047105041582,\n",
      "                                    1.8109582359381,\n",
      "                                    8.136972830203],\n",
      "                                   [1.4956739830763,\n",
      "                                    0.8752433234445,\n",
      "                                    2.6618441256294],\n",
      "                                   [-0.037434356310049,\n",
      "                                    -0.020255906549026,\n",
      "                                    0.022979802890734],\n",
      "                                   [-0.0088449869828188,\n",
      "                                    -0.0056151779914167,\n",
      "                                    5.3674672844522],\n",
      "                                   [1.7978317943815,\n",
      "                                    1.076409601038,\n",
      "                                    -1.0602541867279],\n",
      "                                   [-0.11427633034664,\n",
      "                                    1.6007688524148,\n",
      "                                    4.2413632407062],\n",
      "                                   [-0.10069151520667,\n",
      "                                    1.6101614218539,\n",
      "                                    1.0184179916057],\n",
      "                                   [1.8566867859994,\n",
      "                                    1.0700401269874,\n",
      "                                    6.5233113723973],\n",
      "                                   [6.5705892089167,\n",
      "                                    2.1316482054532,\n",
      "                                    8.1291637416267],\n",
      "                                   [7.8323247667503,\n",
      "                                    1.1845130541695,\n",
      "                                    2.6541739139826],\n",
      "                                   [6.3699611564385,\n",
      "                                    0.33815128453801,\n",
      "                                    -0.0077569406726126],\n",
      "                                   [6.3455874496557,\n",
      "                                    0.32738599026804,\n",
      "                                    5.3942053866696],\n",
      "                                   [6.271565889581,\n",
      "                                    1.9500144267605,\n",
      "                                    1.0207410673245],\n",
      "                                   [8.1854377733344,\n",
      "                                    1.3905206875294,\n",
      "                                    6.5439243738463],\n",
      "                                   [3.1644218165978,\n",
      "                                    0.1593983276004,\n",
      "                                    0.014934435219972],\n",
      "                                   [3.2090701802159,\n",
      "                                    0.1852816169679,\n",
      "                                    5.4159880980704],\n",
      "                                   [5.0208693996947,\n",
      "                                    1.2287566449379,\n",
      "                                    -1.0383885938139],\n",
      "                                   [3.135439835758,\n",
      "                                    1.8122295412542,\n",
      "                                    4.3033284454437],\n",
      "                                   [3.0595835439912,\n",
      "                                    1.8080971051525,\n",
      "                                    1.0892066314484],\n",
      "                                   [5.0114251990265,\n",
      "                                    1.2560617275225,\n",
      "                                    6.499584777545],\n",
      "                                   [8.1079415820804,\n",
      "                                    1.3414330903779,\n",
      "                                    -0.97431511224366],\n",
      "                                   [6.2138582779879,\n",
      "                                    1.9498189933516,\n",
      "                                    4.2888040924948],\n",
      "                                   [3.4330110389592,\n",
      "                                    1.9905791631101,\n",
      "                                    8.1219857959696],\n",
      "                                   [4.6802126047183,\n",
      "                                    1.043232614116,\n",
      "                                    2.6677040044378],\n",
      "                                   [5.127764001629,\n",
      "                                    4.6222661016521,\n",
      "                                    8.1312058538037],\n",
      "                                   [6.3737638483961,\n",
      "                                    3.6829383757207,\n",
      "                                    2.6756448707674],\n",
      "                                   [4.828703827049,\n",
      "                                    4.459416944584,\n",
      "                                    1.0207120981625],\n",
      "                                   [6.684837782983,\n",
      "                                    3.8597898226393,\n",
      "                                    6.5187215594717],\n",
      "                                   [-1.209421031439,\n",
      "                                    4.3106960048271,\n",
      "                                    8.1276099605995],\n",
      "                                   [-1.5832946893765,\n",
      "                                    4.0926824930576,\n",
      "                                    1.0550962080328],\n",
      "                                   [3.5427366385137,\n",
      "                                    3.7140478770453,\n",
      "                                    -1.0031761928201],\n",
      "                                   [1.6218264176766,\n",
      "                                    4.2742235001299,\n",
      "                                    4.2723648780063],\n",
      "                                   [1.6426338542945,\n",
      "                                    4.2480558404355,\n",
      "                                    1.0865591685],\n",
      "                                   [3.5911848783193,\n",
      "                                    3.7131971355319,\n",
      "                                    6.5038817574223],\n",
      "                                   [6.6655347527048,\n",
      "                                    3.849710431771,\n",
      "                                    -0.97662009324084],\n",
      "                                   [4.7913349818437,\n",
      "                                    4.4061315197553,\n",
      "                                    4.289712875576],\n",
      "                                   [0.37587398567337,\n",
      "                                    3.5142500245003,\n",
      "                                    -1.0571708084931],\n",
      "                                   [-1.5543224525221,\n",
      "                                    4.1128638223928,\n",
      "                                    4.2532757601391],\n",
      "                                   [0.41190975787867,\n",
      "                                    3.5790602843744,\n",
      "                                    6.5311078596149],\n",
      "                                   [4.9186639848981,\n",
      "                                    2.8417116297718,\n",
      "                                    -0.0038581744152532],\n",
      "                                   [4.894904681541,\n",
      "                                    2.8272031124795,\n",
      "                                    5.3914284375512],\n",
      "                                   [-1.4766790162525,\n",
      "                                    2.482561489915,\n",
      "                                    0.018230324579434],\n",
      "                                   [-1.455907973424,\n",
      "                                    2.4987894771009,\n",
      "                                    5.3649781200568],\n",
      "                                   [1.717667015787,\n",
      "                                    2.6606777976126,\n",
      "                                    0.0149484636686],\n",
      "                                   [1.7641478332608,\n",
      "                                    2.6881253756295,\n",
      "                                    5.4184073194991],\n",
      "                                   [0.051277639615097,\n",
      "                                    3.3583186607937,\n",
      "                                    2.6598247522436],\n",
      "                                   [1.9840167086678,\n",
      "                                    4.4697836797869,\n",
      "                                    8.1403583140758],\n",
      "                                   [3.2307176104643,\n",
      "                                    3.5319854613233,\n",
      "                                    2.658303674865]],\n",
      "                         'species': ['Na',\n",
      "                                     'Na',\n",
      "                                     'Ni',\n",
      "                                     'Mn',\n",
      "                                     'O',\n",
      "                                     'O',\n",
      "                                     'O',\n",
      "                                     'O',\n",
      "                                     'Na',\n",
      "                                     'Na',\n",
      "                                     'Ni',\n",
      "                                     'Mn',\n",
      "                                     'O',\n",
      "                                     'O',\n",
      "                                     'Mn',\n",
      "                                     'Mn',\n",
      "                                     'O',\n",
      "                                     'O',\n",
      "                                     'O',\n",
      "                                     'O',\n",
      "                                     'O',\n",
      "                                     'O',\n",
      "                                     'Na',\n",
      "                                     'Na',\n",
      "                                     'Na',\n",
      "                                     'Na',\n",
      "                                     'O',\n",
      "                                     'O',\n",
      "                                     'Na',\n",
      "                                     'O',\n",
      "                                     'O',\n",
      "                                     'O',\n",
      "                                     'O',\n",
      "                                     'O',\n",
      "                                     'O',\n",
      "                                     'O',\n",
      "                                     'O',\n",
      "                                     'O',\n",
      "                                     'O',\n",
      "                                     'Ni',\n",
      "                                     'Ni',\n",
      "                                     'Ni',\n",
      "                                     'Mn',\n",
      "                                     'Ni',\n",
      "                                     'Mn',\n",
      "                                     'Na',\n",
      "                                     'Na',\n",
      "                                     'Na']}},\n",
      " 'parameters': [{'name': 'calculation', 'value': 'vc-relax'},\n",
      "                {'name': 'exchange_correlation', 'value': 'pbe'}],\n",
      " 'process': {'agent': {'name': 'Quantum Espresso', 'version': '6.7'},\n",
      "             'isExperiment': False,\n",
      "             'method': 'dft'},\n",
      " 'project': {'label': 'MB', 'name': 'Materials for Batteries'},\n",
      " 'properties': [{'name': 'volume', 'value': 526.60696977387},\n",
      "                {'name': 'number_of_atoms', 'value': 48},\n",
      "                {'name': 'number_of_electrons', 'value': 402.0},\n",
      "                {'name': 'total_energy', 'value': -52098.217068389},\n",
      "                {'name': 'formation_energy_per_atom',\n",
      "                 'value': -1.7458556275646515},\n",
      "                {'name': 'energy_gap', 'value': -4.3509440708884},\n",
      "                {'name': 'fermi_energy', 'value': 7.9347},\n",
      "                {'name': 'absolute_magnetization', 'value': 31.71},\n",
      "                {'name': 'total_magnetization', 'value': 28.0}]}\n"
     ]
    }
   ],
   "source": [
    "pprint(example_metadata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "3c23c30b-517f-4da0-bdc4-c1a1c5541573",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Metadata check passed\n"
     ]
    }
   ],
   "source": [
    "# You can check if metadata has a valid schema with the code below\n",
    "api.metadata=example_metadata\n",
    "print(\"Metadata check passed\" if  not api.check_metadata() else \"Metadata check not passed\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ca164895-1389-4e96-ab0b-7ef203c7efe1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The metadata provided does note have a valid schema.\n",
      "Please check the error details below:\n",
      " 1 validation error for newProject\n",
      "project -> name\n",
      "  none is not an allowed value (type=type_error.none.not_allowed)\n",
      "Metadata check not passed\n"
     ]
    }
   ],
   "source": [
    "# Check metadata again, this time removing a required field as project name\n",
    "example_metadata['project']['name']=None\n",
    "print(\"Metadata check passed\" if  not api.check_metadata() else \"Metadata check not passed\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d0eefa1a-e36c-4a2c-8f12-13d3c429d436",
   "metadata": {},
   "outputs": [],
   "source": [
    "# reset metadata before going on as it will be provided using a JSON file\n",
    "api.metadata=None"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe20399f-927f-438b-9849-f6d57b342deb",
   "metadata": {},
   "source": [
    "### Step 4 - Save a new project \n",
    "> Having defined project metadata from a JSON file or as Python dict, to save it on DB use `.save` method.   \n",
    "> After succesfully saved a new project, the API return an ObjectID that uniquely define the saved project, this id can be retrieved later on using `.get_id()` method.   \n",
    "> No id is defined if not explicetly set with `.set_id()` or if no new project has been already saved."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "61531b9f-c40a-451a-a3ea-08e7294e9f9d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Document correctly inserted with ObjectID = 63872c25051efb193a4c3ecb\n"
     ]
    }
   ],
   "source": [
    "# save project metadata as defined in json file\n",
    "api.save(metadata='./metadata_example.json')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f62f1daf-b85d-4881-97df-8ba298866d07",
   "metadata": {},
   "source": [
    "### Step 5 - Show current project id \n",
    "> **Initially, after login, no id is set**, so you can either:\n",
    "    1. use `.set_id` and provide a valid id (e.g. from table above)\n",
    "    2. use `.save`, this will save a new project with its metadata and, evenutally, its files. This will set the project as the current one with the id returned from DB.  \n",
    "    <br>\n",
    "    <br> \n",
    "    \n",
    "    **To add project files to a previosuly saved project use `save_project_files`**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e0dfb39d-59aa-4de9-9df9-b06765928fa3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Current project id is 63872c25051efb193a4c3ecb\n"
     ]
    }
   ],
   "source": [
    "# Show current project id\n",
    "api.get_id()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8998bc73-ce7e-48e2-8af5-bbb781ca3e3d",
   "metadata": {},
   "source": [
    "### Step 6 - Show user projects\n",
    "> Note, below are only example (random) data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "9f634e62-bd58-4afe-8454-c79d6531429e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'id': '637a0e4be19d0faf6cf7e6d6',\n",
       "  'iemap_id': 'iemap-bd17d1',\n",
       "  'project_name': 'Materials for Batteries',\n",
       "  'date_creation': '2022-11-20T11:23:55.955000',\n",
       "  'experiment': False,\n",
       "  'material': 'Mn4Na12O24Ti8'},\n",
       " {'id': '637a0e4ce19d0faf6cf7e6d7',\n",
       "  'iemap_id': 'iemap-f812e3',\n",
       "  'project_name': 'Materials for Batteries',\n",
       "  'date_creation': '2022-11-20T11:23:56.165000',\n",
       "  'experiment': False,\n",
       "  'material': 'Mn4Na12O24Ti8'},\n",
       " {'id': '637a0effe19d0faf6cf7e6d8',\n",
       "  'iemap_id': 'iemap-0c167c',\n",
       "  'project_name': 'Materials for Batteries',\n",
       "  'date_creation': '2022-11-20T11:26:55.739000',\n",
       "  'experiment': False,\n",
       "  'material': 'Mn6Na12Ni6O24'},\n",
       " {'id': '637a0effe19d0faf6cf7e6d9',\n",
       "  'iemap_id': 'iemap-b22ff8',\n",
       "  'project_name': 'Materials for Batteries',\n",
       "  'date_creation': '2022-11-20T11:26:55.952000',\n",
       "  'experiment': False,\n",
       "  'material': 'Mn6Na12Ni6O24'},\n",
       " {'id': '637a3bbbe19d0faf6cf7e6da',\n",
       "  'iemap_id': 'iemap-13b771',\n",
       "  'project_name': 'Materials for Batteries',\n",
       "  'date_creation': '2022-11-20T14:37:47.295000',\n",
       "  'experiment': False,\n",
       "  'material': 'Mn6Na12Ni6O24'},\n",
       " {'id': '637a3bbbe19d0faf6cf7e6db',\n",
       "  'iemap_id': 'iemap-58f8f8',\n",
       "  'project_name': 'Materials for Batteries',\n",
       "  'date_creation': '2022-11-20T14:37:47.518000',\n",
       "  'experiment': False,\n",
       "  'material': 'Mn6Na12Ni6O24'},\n",
       " {'id': '6385ba9c051efb193a4c3ec1',\n",
       "  'iemap_id': 'iemap-cb042d',\n",
       "  'project_name': 'Materials for Batteries',\n",
       "  'date_creation': '2022-11-29T07:54:04.600000',\n",
       "  'experiment': False,\n",
       "  'material': 'Mn6Na12Ni6O24'},\n",
       " {'id': '6385bb3c051efb193a4c3ec2',\n",
       "  'iemap_id': 'iemap-489402',\n",
       "  'project_name': 'Materials for Batteries',\n",
       "  'date_creation': '2022-11-29T07:56:44.636000',\n",
       "  'experiment': False,\n",
       "  'material': 'Mn6Na12Ni6O24'},\n",
       " {'id': '6385bb6f051efb193a4c3ec3',\n",
       "  'iemap_id': 'iemap-b2b0df',\n",
       "  'project_name': 'Materials for Batteries',\n",
       "  'date_creation': '2022-11-29T07:57:35.880000',\n",
       "  'experiment': False,\n",
       "  'material': 'Mn6Na12Ni6O24'},\n",
       " {'id': '6385bbe3051efb193a4c3ec4',\n",
       "  'iemap_id': 'iemap-564ecf',\n",
       "  'project_name': 'Materials for Batteries',\n",
       "  'date_creation': '2022-11-29T07:59:31.267000',\n",
       "  'experiment': False,\n",
       "  'material': 'Mn6Na12Ni6O24'},\n",
       " {'id': '6385be27051efb193a4c3ec5',\n",
       "  'iemap_id': 'iemap-ba60eb',\n",
       "  'project_name': 'Materials for Batteries',\n",
       "  'date_creation': '2022-11-29T08:09:11.956000',\n",
       "  'experiment': False,\n",
       "  'material': 'Mn6Na12Ni6O24'},\n",
       " {'id': '63863241051efb193a4c3ec7',\n",
       "  'iemap_id': 'iemap-a86a46',\n",
       "  'project_name': 'Materials for Batteries',\n",
       "  'date_creation': '2022-11-29T16:24:33.115000',\n",
       "  'experiment': False,\n",
       "  'material': 'Mn6Na12Ni6O24'},\n",
       " {'id': '6387149c051efb193a4c3ec9',\n",
       "  'iemap_id': 'iemap-d559e1',\n",
       "  'project_name': 'Materials for Batteries',\n",
       "  'date_creation': '2022-11-30T08:30:20.631000',\n",
       "  'experiment': False,\n",
       "  'material': 'Mn6Na12Ni6O24'},\n",
       " {'id': '638714ba051efb193a4c3eca',\n",
       "  'iemap_id': 'iemap-5452c8',\n",
       "  'project_name': 'Materials for Batteries',\n",
       "  'date_creation': '2022-11-30T08:30:50.569000',\n",
       "  'experiment': False,\n",
       "  'material': 'Mn6Na12Ni6O24'},\n",
       " {'id': '63872c25051efb193a4c3ecb',\n",
       "  'iemap_id': 'iemap-40d9f0',\n",
       "  'project_name': 'Materials for Batteries',\n",
       "  'date_creation': '2022-11-30T10:10:45.132000',\n",
       "  'experiment': False,\n",
       "  'material': 'Mn6Na12Ni6O24'}]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# show user saved projects\n",
    "api.my_projects()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fbad14f0-dbf8-4497-88c1-83da399b4811",
   "metadata": {},
   "source": [
    "#### List projects in tabular format (Pandas DataFrame)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d45dbbaa-9b27-4f29-8eb0-adae130f30a5",
   "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>id</th>\n",
       "      <th>iemap_id</th>\n",
       "      <th>project_name</th>\n",
       "      <th>date_creation</th>\n",
       "      <th>experiment</th>\n",
       "      <th>material</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>637a0e4be19d0faf6cf7e6d6</td>\n",
       "      <td>iemap-bd17d1</td>\n",
       "      <td>Materials for Batteries</td>\n",
       "      <td>2022-11-20T11:23:55.955000</td>\n",
       "      <td>False</td>\n",
       "      <td>Mn4Na12O24Ti8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>637a0e4ce19d0faf6cf7e6d7</td>\n",
       "      <td>iemap-f812e3</td>\n",
       "      <td>Materials for Batteries</td>\n",
       "      <td>2022-11-20T11:23:56.165000</td>\n",
       "      <td>False</td>\n",
       "      <td>Mn4Na12O24Ti8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>637a0effe19d0faf6cf7e6d8</td>\n",
       "      <td>iemap-0c167c</td>\n",
       "      <td>Materials for Batteries</td>\n",
       "      <td>2022-11-20T11:26:55.739000</td>\n",
       "      <td>False</td>\n",
       "      <td>Mn6Na12Ni6O24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>637a0effe19d0faf6cf7e6d9</td>\n",
       "      <td>iemap-b22ff8</td>\n",
       "      <td>Materials for Batteries</td>\n",
       "      <td>2022-11-20T11:26:55.952000</td>\n",
       "      <td>False</td>\n",
       "      <td>Mn6Na12Ni6O24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>637a3bbbe19d0faf6cf7e6da</td>\n",
       "      <td>iemap-13b771</td>\n",
       "      <td>Materials for Batteries</td>\n",
       "      <td>2022-11-20T14:37:47.295000</td>\n",
       "      <td>False</td>\n",
       "      <td>Mn6Na12Ni6O24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>637a3bbbe19d0faf6cf7e6db</td>\n",
       "      <td>iemap-58f8f8</td>\n",
       "      <td>Materials for Batteries</td>\n",
       "      <td>2022-11-20T14:37:47.518000</td>\n",
       "      <td>False</td>\n",
       "      <td>Mn6Na12Ni6O24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6385ba9c051efb193a4c3ec1</td>\n",
       "      <td>iemap-cb042d</td>\n",
       "      <td>Materials for Batteries</td>\n",
       "      <td>2022-11-29T07:54:04.600000</td>\n",
       "      <td>False</td>\n",
       "      <td>Mn6Na12Ni6O24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>6385bb3c051efb193a4c3ec2</td>\n",
       "      <td>iemap-489402</td>\n",
       "      <td>Materials for Batteries</td>\n",
       "      <td>2022-11-29T07:56:44.636000</td>\n",
       "      <td>False</td>\n",
       "      <td>Mn6Na12Ni6O24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>6385bb6f051efb193a4c3ec3</td>\n",
       "      <td>iemap-b2b0df</td>\n",
       "      <td>Materials for Batteries</td>\n",
       "      <td>2022-11-29T07:57:35.880000</td>\n",
       "      <td>False</td>\n",
       "      <td>Mn6Na12Ni6O24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>6385bbe3051efb193a4c3ec4</td>\n",
       "      <td>iemap-564ecf</td>\n",
       "      <td>Materials for Batteries</td>\n",
       "      <td>2022-11-29T07:59:31.267000</td>\n",
       "      <td>False</td>\n",
       "      <td>Mn6Na12Ni6O24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>6385be27051efb193a4c3ec5</td>\n",
       "      <td>iemap-ba60eb</td>\n",
       "      <td>Materials for Batteries</td>\n",
       "      <td>2022-11-29T08:09:11.956000</td>\n",
       "      <td>False</td>\n",
       "      <td>Mn6Na12Ni6O24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>63863241051efb193a4c3ec7</td>\n",
       "      <td>iemap-a86a46</td>\n",
       "      <td>Materials for Batteries</td>\n",
       "      <td>2022-11-29T16:24:33.115000</td>\n",
       "      <td>False</td>\n",
       "      <td>Mn6Na12Ni6O24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>6387149c051efb193a4c3ec9</td>\n",
       "      <td>iemap-d559e1</td>\n",
       "      <td>Materials for Batteries</td>\n",
       "      <td>2022-11-30T08:30:20.631000</td>\n",
       "      <td>False</td>\n",
       "      <td>Mn6Na12Ni6O24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>638714ba051efb193a4c3eca</td>\n",
       "      <td>iemap-5452c8</td>\n",
       "      <td>Materials for Batteries</td>\n",
       "      <td>2022-11-30T08:30:50.569000</td>\n",
       "      <td>False</td>\n",
       "      <td>Mn6Na12Ni6O24</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>63872c25051efb193a4c3ecb</td>\n",
       "      <td>iemap-40d9f0</td>\n",
       "      <td>Materials for Batteries</td>\n",
       "      <td>2022-11-30T10:10:45.132000</td>\n",
       "      <td>False</td>\n",
       "      <td>Mn6Na12Ni6O24</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                          id      iemap_id             project_name  \\\n",
       "0   637a0e4be19d0faf6cf7e6d6  iemap-bd17d1  Materials for Batteries   \n",
       "1   637a0e4ce19d0faf6cf7e6d7  iemap-f812e3  Materials for Batteries   \n",
       "2   637a0effe19d0faf6cf7e6d8  iemap-0c167c  Materials for Batteries   \n",
       "3   637a0effe19d0faf6cf7e6d9  iemap-b22ff8  Materials for Batteries   \n",
       "4   637a3bbbe19d0faf6cf7e6da  iemap-13b771  Materials for Batteries   \n",
       "5   637a3bbbe19d0faf6cf7e6db  iemap-58f8f8  Materials for Batteries   \n",
       "6   6385ba9c051efb193a4c3ec1  iemap-cb042d  Materials for Batteries   \n",
       "7   6385bb3c051efb193a4c3ec2  iemap-489402  Materials for Batteries   \n",
       "8   6385bb6f051efb193a4c3ec3  iemap-b2b0df  Materials for Batteries   \n",
       "9   6385bbe3051efb193a4c3ec4  iemap-564ecf  Materials for Batteries   \n",
       "10  6385be27051efb193a4c3ec5  iemap-ba60eb  Materials for Batteries   \n",
       "11  63863241051efb193a4c3ec7  iemap-a86a46  Materials for Batteries   \n",
       "12  6387149c051efb193a4c3ec9  iemap-d559e1  Materials for Batteries   \n",
       "13  638714ba051efb193a4c3eca  iemap-5452c8  Materials for Batteries   \n",
       "14  63872c25051efb193a4c3ecb  iemap-40d9f0  Materials for Batteries   \n",
       "\n",
       "                 date_creation  experiment       material  \n",
       "0   2022-11-20T11:23:55.955000       False  Mn4Na12O24Ti8  \n",
       "1   2022-11-20T11:23:56.165000       False  Mn4Na12O24Ti8  \n",
       "2   2022-11-20T11:26:55.739000       False  Mn6Na12Ni6O24  \n",
       "3   2022-11-20T11:26:55.952000       False  Mn6Na12Ni6O24  \n",
       "4   2022-11-20T14:37:47.295000       False  Mn6Na12Ni6O24  \n",
       "5   2022-11-20T14:37:47.518000       False  Mn6Na12Ni6O24  \n",
       "6   2022-11-29T07:54:04.600000       False  Mn6Na12Ni6O24  \n",
       "7   2022-11-29T07:56:44.636000       False  Mn6Na12Ni6O24  \n",
       "8   2022-11-29T07:57:35.880000       False  Mn6Na12Ni6O24  \n",
       "9   2022-11-29T07:59:31.267000       False  Mn6Na12Ni6O24  \n",
       "10  2022-11-29T08:09:11.956000       False  Mn6Na12Ni6O24  \n",
       "11  2022-11-29T16:24:33.115000       False  Mn6Na12Ni6O24  \n",
       "12  2022-11-30T08:30:20.631000       False  Mn6Na12Ni6O24  \n",
       "13  2022-11-30T08:30:50.569000       False  Mn6Na12Ni6O24  \n",
       "14  2022-11-30T10:10:45.132000       False  Mn6Na12Ni6O24  "
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_proj=pd.DataFrame(api.my_projects())\n",
    "df_proj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "e81a9d7f-d441-457b-85d5-13207471937f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Updated current project id to 637a0e4be19d0faf6cf7e6d6!!\n"
     ]
    }
   ],
   "source": [
    "# grab a valid project id from previous printed list and set one\n",
    "id_first_proj=df_proj.iloc[0,0]\n",
    "api.set_id(id_first_proj)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "09821b8f-3e15-4fc9-88f3-ddd1bd65278f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Current project id is 637a0e4be19d0faf6cf7e6d6\n"
     ]
    }
   ],
   "source": [
    "# verify new id was correctly set\n",
    "api.get_id()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8786a1eb-39e8-48e2-81a1-93b637022ac8",
   "metadata": {},
   "source": [
    "### Step 7 - Add project file to existing project\n",
    "> For each user project is possible to define one or more file to upload on Server.     \n",
    "> Note: **If the file to upload is already present on server then it is not actually over written (on file system) but it is added as field on database.<br/>A note stating \"(File already present on File System)\" in this case is diplayed.**   \n",
    "> **Only some file types are allowed as: PDF,CSV, XLS, XLSX, TXT, CIF or DOC.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "066e592f-4fe6-4ee5-a16f-dd6fb9b5e210",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adding file 10_bandsdown.dat (1.21KB)...Done! (File already present on File System)\n",
      "Saving project file took 0.1410 seconds\n"
     ]
    }
   ],
   "source": [
    "# add project file to a previously save project\n",
    "_= api.save_project_files(project_files=[\"../migration/BANDs/10_bandsdown.dat\"], show_debug_info=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2290afc-5399-4144-96d0-df4e631089ff",
   "metadata": {},
   "source": [
    "### Step 8 - Define a new project metadata (with its associated files in any)\n",
    "> You can save metadata and files in a unique step using `.save` and providing both ***metadata*** and ***list_proj_files***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3cfbe306-175f-41e4-9fed-01f68c62051d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Document correctly inserted with ObjectID = 63872c37051efb193a4c3ecc\n",
      "Adding file 10.cif (5.69KB)...Done!\n",
      "Adding file 1_bandsdown.dat (1.21KB)...Done!\n",
      "Saving project file took 0.1580 seconds\n"
     ]
    }
   ],
   "source": [
    "# define a list of valid files path\n",
    "proj_files = [\"../migration/CIFs/10.cif\", \"../migration/BANDs/1_bandsdown.dat\"]\n",
    "\n",
    "# save project and file\n",
    "api.save(metadata=\"./metadata_example.json\", list_proj_files=proj_files, show_debug_info=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20e2ede7-8cab-47a1-a993-2c66ca2dacf8",
   "metadata": {},
   "source": [
    "### Step 9 - Query a specific Project id\n",
    "> **All project and its id are available as showed at step 6**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "f2f7120f-ffcb-4c61-a9c0-48a8903afed7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'iemap_id': 'iemap-40d9f0',\n",
       "  'provenance': {'email': '**********',\n",
       "   'affiliation': 'ENEA',\n",
       "   'createdAt': '2022-11-30T10:10:45.132000',\n",
       "   'updatedAt': '2022-11-30T10:10:45.132000'},\n",
       "  'project': {'name': 'Materials for Batteries', 'label': 'MB'},\n",
       "  'process': {'isExperiment': False,\n",
       "   'method': 'dft',\n",
       "   'agent': {'name': 'Quantum Espresso', 'version': '6.7'}},\n",
       "  'material': {'formula': 'Mn6Na12Ni6O24',\n",
       "   'elements': ['Mn', 'Na', 'Ni', 'O'],\n",
       "   'input': {'lattice': {'a': '9.2958',\n",
       "     'b': '6.197171739592183',\n",
       "     'c': '10.8981',\n",
       "     'alpha': '90.0',\n",
       "     'beta': '120.0001508506647',\n",
       "     'gamma': '90.0'},\n",
       "    'sites': [[0.0, 1.789, 8.174],\n",
       "     [1.549, 0.894, 2.725],\n",
       "     [0.0, 0.0, 0.0],\n",
       "     [0.0, 0.0, 5.449],\n",
       "     [1.549, 0.894, -0.954],\n",
       "     [0.0, 1.789, 4.495],\n",
       "     [0.0, 1.789, 0.954],\n",
       "     [1.549, 0.894, 6.403],\n",
       "     [6.197, 1.789, 8.174],\n",
       "     [7.747, 0.894, 2.725],\n",
       "     [6.197, 0.0, 0.0],\n",
       "     [6.197, 0.0, 5.449],\n",
       "     [6.197, 1.789, 0.954],\n",
       "     [7.747, 0.894, 6.403],\n",
       "     [3.099, 0.0, 0.0],\n",
       "     [3.099, 0.0, 5.449],\n",
       "     [4.648, 0.894, -0.954],\n",
       "     [3.099, 1.789, 4.495],\n",
       "     [3.099, 1.789, 0.954],\n",
       "     [4.648, 0.894, 6.403],\n",
       "     [7.747, 0.894, -0.954],\n",
       "     [6.197, 1.789, 4.495],\n",
       "     [3.099, 1.789, 8.174],\n",
       "     [4.648, 0.894, 2.725],\n",
       "     [4.648, 4.472, 8.174],\n",
       "     [6.197, 3.578, 2.725],\n",
       "     [4.648, 4.472, 0.954],\n",
       "     [6.197, 3.578, 6.403],\n",
       "     [-1.549, 4.472, 8.174],\n",
       "     [-1.549, 4.472, 0.954],\n",
       "     [3.099, 3.578, -0.954],\n",
       "     [1.549, 4.472, 4.495],\n",
       "     [1.549, 4.472, 0.954],\n",
       "     [3.099, 3.578, 6.403],\n",
       "     [6.197, 3.578, -0.954],\n",
       "     [4.648, 4.472, 4.495],\n",
       "     [0.0, 3.578, -0.954],\n",
       "     [-1.549, 4.472, 4.495],\n",
       "     [0.0, 3.578, 6.403],\n",
       "     [4.648, 2.683, 0.0],\n",
       "     [4.648, 2.683, 5.449],\n",
       "     [-1.549, 2.683, 0.0],\n",
       "     [-1.549, 2.683, 5.449],\n",
       "     [1.549, 2.683, 0.0],\n",
       "     [1.549, 2.683, 5.449],\n",
       "     [0.0, 3.578, 2.725],\n",
       "     [1.549, 4.472, 8.174],\n",
       "     [3.099, 3.578, 2.725]],\n",
       "    'species': ['Na',\n",
       "     'Na',\n",
       "     'Ni',\n",
       "     'Mn',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'Na',\n",
       "     'Na',\n",
       "     'Ni',\n",
       "     'Mn',\n",
       "     'O',\n",
       "     'O',\n",
       "     'Mn',\n",
       "     'Mn',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'Na',\n",
       "     'Na',\n",
       "     'Na',\n",
       "     'Na',\n",
       "     'O',\n",
       "     'O',\n",
       "     'Na',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'Ni',\n",
       "     'Ni',\n",
       "     'Ni',\n",
       "     'Mn',\n",
       "     'Ni',\n",
       "     'Mn',\n",
       "     'Na',\n",
       "     'Na',\n",
       "     'Na'],\n",
       "    'cell': [[9.2958, 0.0, 0.0], [-3.0986, 5.3669, 0.0], [0.0, 0.0, 10.8981]]},\n",
       "   'output': {'lattice': {'a': '9.509552586940014',\n",
       "     'b': '5.780454151890555',\n",
       "     'c': '10.764859474084856',\n",
       "     'alpha': '89.99420734587778',\n",
       "     'beta': '117.13279435162953',\n",
       "     'gamma': '89.65063053116737'},\n",
       "    'sites': [[0.25047105041582, 1.8109582359381, 8.136972830203],\n",
       "     [1.4956739830763, 0.8752433234445, 2.6618441256294],\n",
       "     [-0.037434356310049, -0.020255906549026, 0.022979802890734],\n",
       "     [-0.0088449869828188, -0.0056151779914167, 5.3674672844522],\n",
       "     [1.7978317943815, 1.076409601038, -1.0602541867279],\n",
       "     [-0.11427633034664, 1.6007688524148, 4.2413632407062],\n",
       "     [-0.10069151520667, 1.6101614218539, 1.0184179916057],\n",
       "     [1.8566867859994, 1.0700401269874, 6.5233113723973],\n",
       "     [6.5705892089167, 2.1316482054532, 8.1291637416267],\n",
       "     [7.8323247667503, 1.1845130541695, 2.6541739139826],\n",
       "     [6.3699611564385, 0.33815128453801, -0.0077569406726126],\n",
       "     [6.3455874496557, 0.32738599026804, 5.3942053866696],\n",
       "     [6.271565889581, 1.9500144267605, 1.0207410673245],\n",
       "     [8.1854377733344, 1.3905206875294, 6.5439243738463],\n",
       "     [3.1644218165978, 0.1593983276004, 0.014934435219972],\n",
       "     [3.2090701802159, 0.1852816169679, 5.4159880980704],\n",
       "     [5.0208693996947, 1.2287566449379, -1.0383885938139],\n",
       "     [3.135439835758, 1.8122295412542, 4.3033284454437],\n",
       "     [3.0595835439912, 1.8080971051525, 1.0892066314484],\n",
       "     [5.0114251990265, 1.2560617275225, 6.499584777545],\n",
       "     [8.1079415820804, 1.3414330903779, -0.97431511224366],\n",
       "     [6.2138582779879, 1.9498189933516, 4.2888040924948],\n",
       "     [3.4330110389592, 1.9905791631101, 8.1219857959696],\n",
       "     [4.6802126047183, 1.043232614116, 2.6677040044378],\n",
       "     [5.127764001629, 4.6222661016521, 8.1312058538037],\n",
       "     [6.3737638483961, 3.6829383757207, 2.6756448707674],\n",
       "     [4.828703827049, 4.459416944584, 1.0207120981625],\n",
       "     [6.684837782983, 3.8597898226393, 6.5187215594717],\n",
       "     [-1.209421031439, 4.3106960048271, 8.1276099605995],\n",
       "     [-1.5832946893765, 4.0926824930576, 1.0550962080328],\n",
       "     [3.5427366385137, 3.7140478770453, -1.0031761928201],\n",
       "     [1.6218264176766, 4.2742235001299, 4.2723648780063],\n",
       "     [1.6426338542945, 4.2480558404355, 1.0865591685],\n",
       "     [3.5911848783193, 3.7131971355319, 6.5038817574223],\n",
       "     [6.6655347527048, 3.849710431771, -0.97662009324084],\n",
       "     [4.7913349818437, 4.4061315197553, 4.289712875576],\n",
       "     [0.37587398567337, 3.5142500245003, -1.0571708084931],\n",
       "     [-1.5543224525221, 4.1128638223928, 4.2532757601391],\n",
       "     [0.41190975787867, 3.5790602843744, 6.5311078596149],\n",
       "     [4.9186639848981, 2.8417116297718, -0.0038581744152532],\n",
       "     [4.894904681541, 2.8272031124795, 5.3914284375512],\n",
       "     [-1.4766790162525, 2.482561489915, 0.018230324579434],\n",
       "     [-1.455907973424, 2.4987894771009, 5.3649781200568],\n",
       "     [1.717667015787, 2.6606777976126, 0.0149484636686],\n",
       "     [1.7641478332608, 2.6881253756295, 5.4184073194991],\n",
       "     [0.051277639615097, 3.3583186607937, 2.6598247522436],\n",
       "     [1.9840167086678, 4.4697836797869, 8.1403583140758],\n",
       "     [3.2307176104643, 3.5319854613233, 2.658303674865]],\n",
       "    'species': ['Na',\n",
       "     'Na',\n",
       "     'Ni',\n",
       "     'Mn',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'Na',\n",
       "     'Na',\n",
       "     'Ni',\n",
       "     'Mn',\n",
       "     'O',\n",
       "     'O',\n",
       "     'Mn',\n",
       "     'Mn',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'Na',\n",
       "     'Na',\n",
       "     'Na',\n",
       "     'Na',\n",
       "     'O',\n",
       "     'O',\n",
       "     'Na',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'O',\n",
       "     'Ni',\n",
       "     'Ni',\n",
       "     'Ni',\n",
       "     'Mn',\n",
       "     'Ni',\n",
       "     'Mn',\n",
       "     'Na',\n",
       "     'Na',\n",
       "     'Na'],\n",
       "    'cell': [[9.4974797270795, 0.47810505533726, 0.029745479528267],\n",
       "     [-2.8915183359738, 5.0052744061721, -0.00018372422247841],\n",
       "     [0.031023356615597, 0.019574039641932, 10.764796974635]]}},\n",
       "  'parameters': [{'name': 'calculation', 'value': 'vc-relax'},\n",
       "   {'name': 'exchange_correlation', 'value': 'pbe'}],\n",
       "  'properties': [{'name': 'volume', 'value': 526.60696977387},\n",
       "   {'name': 'number_of_atoms', 'value': 48.0},\n",
       "   {'name': 'number_of_electrons', 'value': 402.0},\n",
       "   {'name': 'total_energy', 'value': -52098.217068389},\n",
       "   {'name': 'formation_energy_per_atom', 'value': -1.7458556275646515},\n",
       "   {'name': 'energy_gap', 'value': -4.3509440708884},\n",
       "   {'name': 'fermi_energy', 'value': 7.9347},\n",
       "   {'name': 'absolute_magnetization', 'value': 31.71},\n",
       "   {'name': 'total_magnetization', 'value': 28.0}]}]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id_last_proj=df_proj.iloc[-1,0]\n",
    "data=api.query(id_last_proj)\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e720ac2a-4792-4fc5-9f56-35b5376f3ff3",
   "metadata": {},
   "source": [
    "### Step 10 - Show ONLY files for a specific project"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "b060ce28-b649-4d9f-a649-5054cb3ef429",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "No files associated to project iemap-40d9f0\n"
     ]
    }
   ],
   "source": [
    "# project files\n",
    "if \"files\" in data[0]:\n",
    "    files_df=pd.json_normalize(data, record_path='files')\n",
    "    display(files_df)\n",
    "else:\n",
    "    print(f\"No files associated to project {data[0]['iemap_id']}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42402a13-cdef-4e0b-ae0a-8da70128efa5",
   "metadata": {},
   "source": [
    "### Step 11  - Add project files to current project \n",
    "(or define another project id and add files to this new project id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "72219b13-f14e-4971-81db-7eb020e0c5b0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adding file 1_bandsup.dat (1.21KB)...Done!\n",
      "Adding file 7_bandsup.dat (1.21KB)...Done!\n",
      "Adding file 1_bandsdown.dat (1.21KB)...Done! (File already present on File System)\n",
      "Adding file 0.out (6.54MB)...Done!\n",
      "Adding file 0.in (4.15KB)...Done!\n",
      "Adding file 10.cif (5.69KB)...Done! (File already present on File System)\n",
      "Saving project file took 1.7720 seconds\n"
     ]
    }
   ],
   "source": [
    "additional_files_proj = [\n",
    "    \"../migration/BANDs/1_bandsup.dat\",\n",
    "    \"../migration/BANDs/7_bandsup.dat\",\n",
    "    \"../migration/BANDs/1_bandsdown.dat\",\n",
    "    \"../migration/QE/0.out\",\n",
    "    \"../migration/QE/0.in\",\n",
    "    \"../migration/CIFs/10.cif\",\n",
    "]\n",
    "\n",
    "# save additional project files to current ID\n",
    "_ =api.save_project_files(project_files=additional_files_proj)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6fdcefaa-d2c4-4cfe-be87-80025820c0af",
   "metadata": {},
   "source": [
    "### Step 12 - Associate a file to a specific property of material inside a project\n",
    "> For each project is possible to associate one or more file related to the project as a whole,   \n",
    "but is also possible to upload a file associtaed to a specific property of the material used in the project   \n",
    "using `.save_property_files` and provinding as argument the property name and its related fiel as below"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "a31f1d05-0fa8-4f07-983f-4ef49772518b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'name': 'volume', 'value': 546.81206681414},\n",
       " {'name': 'number_of_atoms', 'value': 48.0},\n",
       " {'name': 'number_of_electrons', 'value': 408.0},\n",
       " {'name': 'total_energy', 'value': -51149.096020383},\n",
       " {'name': 'formation_energy_per_atom', 'value': -2.936476648619949},\n",
       " {'name': 'energy_gap', 'value': -2.943662394163},\n",
       " {'name': 'fermi_energy', 'value': 10.4157},\n",
       " {'name': 'absolute_magnetization',\n",
       "  'value': 23.57,\n",
       "  'file': '23eb630d314515b1fd0326ac40fd128be7fbfa87.cif'},\n",
       " {'name': 'total_magnetization', 'value': 16.0}]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# First Query a specific project\n",
    "\n",
    "result=api.query(id_first_proj)\n",
    "if result != []:\n",
    "    display(result[0][\"properties\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "c0b8aa16-1cd5-4fae-a6a7-3b50beaf9143",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Updated current project id to 637a0e4be19d0faf6cf7e6d6!!\n",
      "Adding file 0.cif (5.66KB)...Done! (File already present on File System)\n"
     ]
    }
   ],
   "source": [
    "# set the project id and the property to which associate a file\n",
    "api.set_id(id_first_proj)\n",
    "api.save_property_files({\"absolute_magnetization\": \"../migration/CIFs/0.cif\"}, show_debug_info=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "3ef291ad-71a0-455b-85a5-57e7a6336882",
   "metadata": {},
   "outputs": [],
   "source": [
    "# CHECK IF FILE HAS BEEN CORRECTLY ADDED\n",
    "dataExistingProj=api.query(id_first_proj)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "eee2c59c-c86b-4de0-a844-ea967abfe190",
   "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>hash</th>\n",
       "      <th>name</th>\n",
       "      <th>extention</th>\n",
       "      <th>size</th>\n",
       "      <th>createdAt</th>\n",
       "      <th>updatedAt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>54b56034096881366c52743ca922ad8194710830</td>\n",
       "      <td>10_bandsdown.dat</td>\n",
       "      <td>dat</td>\n",
       "      <td>1.213 KB</td>\n",
       "      <td>2022-11-30T08:30:47.312000</td>\n",
       "      <td>2022-11-30T08:30:47.312000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                       hash              name extention  \\\n",
       "0  54b56034096881366c52743ca922ad8194710830  10_bandsdown.dat       dat   \n",
       "\n",
       "       size                   createdAt                   updatedAt  \n",
       "0  1.213 KB  2022-11-30T08:30:47.312000  2022-11-30T08:30:47.312000  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "if \"files\" in dataExistingProj[0]:\n",
    "    files_df=pd.json_normalize(dataExistingProj, record_path='files')\n",
    "    display(files_df)\n",
    "else:\n",
    "    print(f\"No files associated to project {data[0]['iemap_id']}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b75a80d-b8ae-4bdc-b654-4285201909c9",
   "metadata": {},
   "source": [
    "### Step 13 - Download a project (property) file\n",
    "> Knowing the full file name = hash.ext is possible to download the file using `.download_file` (uploaded by the logged in user or not)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "07523104-921f-45ef-8c6f-15420ac0b8a5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "54b56034096881366c52743ca922ad8194710830.dat\n"
     ]
    }
   ],
   "source": [
    "file_hash=files_df.iloc[0,0]\n",
    "file_ext=str(files_df.iloc[0,1]).split(\".\")[-1]\n",
    "full_file_hash=file_hash +\".\"+file_ext\n",
    "print(full_file_hash)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "3804fcc4-df1b-40c9-a0ff-611f76500090",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Download started, please wait....Done!\n"
     ]
    }
   ],
   "source": [
    "local_path_downloade_file='./downloaded_file.dat'\n",
    "api.download_file(full_file_hash,local_path_downloade_file)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50adfde4-6db3-4abf-a753-1ba4f53a1cec",
   "metadata": {},
   "source": [
    "### Step 14 - Delete a file from project\n",
    "> To delete a file from a project use `delete_project_file` method with argument the hash (only) of the file to be removed   \n",
    "> Note: **You can delete from File System only files you uploaded and if not used in any other project**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "368f88ca-ee33-4e18-8209-0b9375984d16",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Deleting file 6b39ac4704eb5e36707766fc92e442b029d80ba8\n",
      "File 6b39ac4704eb5e36707766fc92e442b029d80ba8.dat (1.214 KB), removed from project and from File System\n"
     ]
    }
   ],
   "source": [
    "api.delete_project_file(\"6b39ac4704eb5e36707766fc92e442b029d80ba8\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.8 64-bit",
   "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.8"
  },
  "vscode": {
   "interpreter": {
    "hash": "79a16214d47a4aa142693c9e22b1dd27da0bd031209179e1267f6a207b52e7d1"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
