From 18ac95785afdd3e9f5281cc34fdf072c5813f029 Mon Sep 17 00:00:00 2001 From: Fabio Lopes Date: Fri, 13 Dec 2024 15:48:53 +0100 Subject: [PATCH] Issues #5, #6, and #7. Also made the export of object graph faster by saving a history of objects. Instead of always calling openBIS for the objects. One may check inside the dictionary if it is already there. --- .../materialMLinfo.yaml | 19 +- .../.create_molecule-0.ipynb | 240 +++++++ .../.create_sample-1.ipynb | 251 ++++++++ .../.export_openbis_object-0.ipynb | 135 ++++ .../.export_openbis_object-1.ipynb | 137 ++++ .../.export_openbis_object-2.ipynb | 137 ++++ .../.export_openbis_object-3.ipynb | 192 ++++++ .../.export_openbis_object-4.ipynb | 192 ++++++ Notebooks/connection_to_openbis/.home-3.ipynb | 95 +++ Notebooks/connection_to_openbis/.home-4.ipynb | 95 +++ Notebooks/connection_to_openbis/.home-5.ipynb | 95 +++ Notebooks/connection_to_openbis/.home-6.ipynb | 95 +++ Notebooks/connection_to_openbis/.home-7.ipynb | 95 +++ .../.send_simulation_to_openbis-1.ipynb | 468 ++++++++++++++ .../connection_to_openbis/aiida_utils.py | 38 +- Notebooks/connection_to_openbis/config.json | 2 + .../create_analysis.ipynb | 8 +- .../export_openbis_object.ipynb | 87 ++- .../find_simulations.ipynb | 4 +- Notebooks/connection_to_openbis/home.ipynb | 2 +- .../sample_measurement.ipynb | 42 +- .../structures/structure.cdxml | 590 ++++++++++++++++++ .../structures/structure.png | Bin 0 -> 10099 bytes Notebooks/connection_to_openbis/utils.py | 28 +- Notebooks/connection_to_openbis/widgets.py | 2 +- 25 files changed, 2984 insertions(+), 65 deletions(-) create mode 100644 Notebooks/connection_to_openbis/.create_molecule-0.ipynb create mode 100644 Notebooks/connection_to_openbis/.create_sample-1.ipynb create mode 100644 Notebooks/connection_to_openbis/.export_openbis_object-0.ipynb create mode 100644 Notebooks/connection_to_openbis/.export_openbis_object-1.ipynb create mode 100644 Notebooks/connection_to_openbis/.export_openbis_object-2.ipynb create mode 100644 Notebooks/connection_to_openbis/.export_openbis_object-3.ipynb create mode 100644 Notebooks/connection_to_openbis/.export_openbis_object-4.ipynb create mode 100644 Notebooks/connection_to_openbis/.home-3.ipynb create mode 100644 Notebooks/connection_to_openbis/.home-4.ipynb create mode 100644 Notebooks/connection_to_openbis/.home-5.ipynb create mode 100644 Notebooks/connection_to_openbis/.home-6.ipynb create mode 100644 Notebooks/connection_to_openbis/.home-7.ipynb create mode 100644 Notebooks/connection_to_openbis/.send_simulation_to_openbis-1.ipynb create mode 100644 Notebooks/connection_to_openbis/structures/structure.cdxml create mode 100644 Notebooks/connection_to_openbis/structures/structure.png diff --git a/Notebooks/Metadata_Schemas_LinkML/materialMLinfo.yaml b/Notebooks/Metadata_Schemas_LinkML/materialMLinfo.yaml index addac793..236c0e72 100644 --- a/Notebooks/Metadata_Schemas_LinkML/materialMLinfo.yaml +++ b/Notebooks/Metadata_Schemas_LinkML/materialMLinfo.yaml @@ -103,7 +103,8 @@ classes: openbis_code: AM openbis_label: Atomistic Model slots: - - structure + - volume + - cell - dimensionality - atoms_positions - periodic_boundary_conditions @@ -1770,6 +1771,22 @@ types: # slots are first-class entities in the metamodel # declaring them here allows them to be reused elsewhere slots: + cell: + description: Cell + range: object + multivalued: false + slot_uri: + annotations: + openbis_type: JSON + openbis_label: Cell + volume: + description: Volume + range: double + multivalued: false + slot_uri: + annotations: + openbis_type: REAL + openbis_label: Volume filepath_executable: description: Filepath executable range: string diff --git a/Notebooks/connection_to_openbis/.create_molecule-0.ipynb b/Notebooks/connection_to_openbis/.create_molecule-0.ipynb new file mode 100644 index 00000000..3dd5f166 --- /dev/null +++ b/Notebooks/connection_to_openbis/.create_molecule-0.ipynb @@ -0,0 +1,240 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from IPython.display import display\n", + "import ipywidgets as ipw\n", + "import widgets\n", + "import utils\n", + "import os\n", + "import rdkit\n", + "from rdkit.Chem import AllChem, Draw, rdMolDescriptors\n", + "import io\n", + "from pathlib import Path" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "CONFIG = utils.read_json(\"config.json\")\n", + "CONFIG_ELN = utils.get_aiidalab_eln_config()\n", + "# CONFIG_ELN = utils.read_json(\"eln_config.json\")\n", + "OPENBIS_SESSION, SESSION_DATA = utils.connect_openbis(CONFIG_ELN[\"url\"], CONFIG_ELN[\"token\"])\n", + "\n", + "molecule_widgets = widgets.ObjectPropertiesWidgets(\"Molecule\")\n", + "\n", + "molecule_structure = ipw.Image(\n", + " value=utils.read_file(CONFIG[\"default_image_filepath\"]), format='jpg', \n", + " width='420px', height='450px', layout=ipw.Layout(border='solid 1px #cccccc')\n", + ")\n", + "\n", + "molecule_cdxml_file = ipw.FileUpload(multiple = False, accept = '.cdxml')\n", + "\n", + "molecule_support_files = ipw.FileUpload(multiple = True)\n", + "\n", + "increase_buttons_size = utils.HTML(data = ''.join(CONFIG[\"save_home_buttons_settings\"]))\n", + "create_button = utils.Button(\n", + " description = '', disabled = False, button_style = '', tooltip = 'Save', \n", + " icon = 'save', layout = ipw.Layout(width = '100px', height = '50px')\n", + ")\n", + "quit_button = utils.Button(\n", + " description = '', disabled = False, button_style = '', \n", + " tooltip = 'Main menu', icon = 'home', layout = ipw.Layout(width = '100px', height = '50px')\n", + ")\n", + "save_close_buttons_hbox = ipw.HBox([create_button, quit_button])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def close_notebook(b):\n", + " display(utils.Javascript(data = 'window.location.replace(\"home.ipynb\")'))\n", + "\n", + "def load_molecule_structure(change):\n", + " for filename in molecule_cdxml_file.value:\n", + " file_info = molecule_cdxml_file.value[filename]\n", + " utils.save_file(file_info['content'], \"structures/structure.cdxml\")\n", + " \n", + " cdxml_molecule = utils.read_file(\"structures/structure.cdxml\")\n", + " molecules = rdkit.Chem.MolsFromCDXML(cdxml_molecule)\n", + " \n", + " if len(molecules) == 1:\n", + " mol = molecules[0] # Get first molecule\n", + " mol_chemical_formula = rdMolDescriptors.CalcMolFormula(mol) # Sum Formula\n", + " mol_smiles = rdkit.Chem.MolToSmiles(mol) # Canonical Smiles\n", + " chem_mol = rdkit.Chem.MolFromSmiles(mol_smiles)\n", + " \n", + " if chem_mol is not None:\n", + " AllChem.Compute2DCoords(chem_mol) # Add coords to the atoms in the molecule\n", + " img = Draw.MolToImage(chem_mol)\n", + " buffer = io.BytesIO()\n", + " img.save(buffer, format=\"PNG\")\n", + " molecule_structure.value = buffer.getvalue()\n", + " img.save(\"structures/structure.png\")\n", + " else:\n", + " print(f\"Cannot generate molecule image.\")\n", + " \n", + " molecule_widgets.properties_widgets[\"sum_formula\"].value = mol_chemical_formula\n", + " molecule_widgets.properties_widgets[\"smiles\"].value = mol_smiles\n", + " \n", + " elif len(molecules) > 1:\n", + " print(f\"There are more than one molecule in the file: {filename}\") \n", + " else:\n", + " print(f\"There are no molecules in the file: {filename}\")\n", + " \n", + "def create_molecule_openbis(b):\n", + " object_properties = {}\n", + " for prop in CONFIG[\"objects\"][\"Molecule\"][\"properties\"]:\n", + " object_properties[prop] = molecule_widgets.properties_widgets[prop].value\n", + " \n", + " # Check if the molecule is already in openBIS\n", + " smiles_molecules_openbis = [molecule.props['smiles'] for molecule in utils.get_openbis_objects(OPENBIS_SESSION, type =\"MOLECULE\")]\n", + " \n", + " if object_properties[\"smiles\"] in smiles_molecules_openbis:\n", + " display(utils.Javascript(data = \"alert('Molecule is already in openBIS!')\"))\n", + " \n", + " if Path(\"structures/structure.png\").exists():\n", + " os.remove(\"structures/structure.png\")\n", + " \n", + " if Path(\"structures/structure.png\").exists():\n", + " os.remove(\"structures/structure.cdxml\")\n", + " else:\n", + " molecule_object = utils.create_openbis_object(\n", + " OPENBIS_SESSION, type=\"MOLECULE\", \n", + " collection=\"/MATERIALS/MOLECULES/MOLECULE_COLLECTION\", \n", + " props = object_properties\n", + " )\n", + " \n", + " # Send molecule image to openBIS\n", + " if Path(\"structures/structure.png\").exists():\n", + " # Upload cdxml\n", + " utils.create_openbis_dataset(OPENBIS_SESSION, type = \"ELN_PREVIEW\", object = molecule_object, file = \"structures/structure.png\")\n", + " os.remove(\"structures/structure.png\")\n", + " \n", + " if Path(\"structures/structure.png\").exists():\n", + " # Upload cdxml\n", + " utils.create_openbis_dataset(OPENBIS_SESSION, type = \"RAW_DATA\", object = molecule_object, file = \"structures/structure.cdxml\")\n", + " os.remove(\"structures/structure.cdxml\")\n", + " \n", + " # Additional support files\n", + " upload_datasets(molecule_object, molecule_support_files)\n", + " \n", + " display(utils.Javascript(data = \"alert('Upload successful!')\"))\n", + "\n", + "def upload_datasets(method_object, support_files_widget):\n", + " for filename in support_files_widget.value:\n", + " file_info = support_files_widget.value[filename]\n", + " utils.save_file(file_info['content'], filename)\n", + " utils.create_openbis_dataset(OPENBIS_SESSION, type = 'RAW_DATA', object = method_object, file = filename)\n", + " os.remove(filename)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Create molecule" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Upload CDXML file" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display(molecule_structure)\n", + "display(molecule_cdxml_file)\n", + "molecule_cdxml_file.observe(load_molecule_structure, names = \"value\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Properties" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display(molecule_widgets)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Support files" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display(molecule_support_files)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Save results" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display(save_close_buttons_hbox)\n", + "display(increase_buttons_size)\n", + "create_button.on_click(create_molecule_openbis)\n", + "quit_button.on_click(close_notebook)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Notebooks/connection_to_openbis/.create_sample-1.ipynb b/Notebooks/connection_to_openbis/.create_sample-1.ipynb new file mode 100644 index 00000000..13486b10 --- /dev/null +++ b/Notebooks/connection_to_openbis/.create_sample-1.ipynb @@ -0,0 +1,251 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from IPython.display import display, clear_output\n", + "import utils\n", + "import widgets\n", + "import ipywidgets as ipw\n", + "import shutil\n", + "import json" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "\n", + " \n", + " " + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "CONFIG = utils.read_json(\"config.json\")\n", + "CONFIG_ELN = utils.get_aiidalab_eln_config()\n", + "# CONFIG_ELN = utils.read_json(\"eln_config.json\")\n", + "OPENBIS_SESSION, SESSION_DATA = utils.connect_openbis(CONFIG_ELN[\"url\"], CONFIG_ELN[\"token\"])\n", + "\n", + "slabs_options = [object_key for object_key, object_info in CONFIG[\"objects\"].items() if object_info[\"object_type\"] == \"slab\"]\n", + "slabs_options.insert(0, \"No material\")\n", + "material_selection_radio_button = utils.Radiobuttons(\n", + " description = '', options = slabs_options, \n", + " disabled = False, layout = ipw.Layout(width = '300px'), \n", + " style = {'description_width': \"100px\"}\n", + ")\n", + "\n", + "material_selector = widgets.MaterialSelectionWidget()\n", + "\n", + "sample_out_name_textbox = utils.Text(\n", + " description = \"Name\", disabled = False, layout = ipw.Layout(width = '400px'), \n", + " placeholder = f\"Write sample name here...\", style = {'description_width': \"110px\"}\n", + ")\n", + "increase_buttons_size = utils.HTML(data = ''.join(CONFIG[\"save_home_buttons_settings\"]))\n", + "create_button = utils.Button(\n", + " description = '', disabled = False, button_style = '', tooltip = 'Save', \n", + " icon = 'save', layout = ipw.Layout(width = '100px', height = '50px')\n", + ")\n", + "quit_button = utils.Button(\n", + " description = '', disabled = False, button_style = '', \n", + " tooltip = 'Main menu', icon = 'home', layout = ipw.Layout(width = '100px', height = '50px')\n", + ")\n", + "save_close_buttons_hbox = ipw.HBox([create_button, quit_button])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def close_notebook(b):\n", + " display(utils.Javascript(data = 'window.location.replace(\"home.ipynb\")'))\n", + " \n", + "# Function to create sample object inside openBIS using information selected in the app\n", + "def create_sample_action(b):\n", + " samples_names = [sample.props[\"$name\"] for sample in OPENBIS_SESSION.get_objects(type = \"SAMPLE\")]\n", + " if sample_out_name_textbox.value in samples_names:\n", + " display(utils.Javascript(data = f\"alert('{'Sample name already exists!'}')\"))\n", + " return\n", + " else:\n", + " sample_parents = [] if material_selector.dropdown.value == -1 else [material_selector.dropdown.value]\n", + " sample_props = {\"$name\": sample_out_name_textbox.value, \"exists\": True}\n", + " utils.create_openbis_object(OPENBIS_SESSION, type=\"SAMPLE\", collection=\"/MATERIALS/SAMPLES/SAMPLE_COLLECTION\", props=sample_props, parents=sample_parents)\n", + " display(utils.Javascript(data = \"alert('Upload successful!')\"))\n", + "\n", + "def select_material_radio_change(change):\n", + " material_type = material_selection_radio_button.value\n", + " if material_type == \"No material\":\n", + " with material_selector:\n", + " clear_output()\n", + " material_selector.dropdown.value = -1\n", + " return\n", + " \n", + " material_selector.details_textbox.value = ''\n", + " material_types = {}\n", + " for object_key, object_info in CONFIG[\"objects\"].items():\n", + " if object_info[\"object_type\"] == \"slab\":\n", + " material_types[object_key] = (object_info[\"openbis_object_type\"], object_info[\"placeholder\"])\n", + " \n", + " with material_selector:\n", + " clear_output()\n", + " # Reset sample out name\n", + " sample_out_name_textbox.value = ''\n", + " display_list = [\n", + " material_selector.dropdown_boxes,\n", + " ipw.HBox([material_selector.details_textbox, material_selector.image_box])\n", + " ]\n", + " material_class, placeholder = material_types.get(material_type)\n", + " material_selector.load_dropdown_box(material_class, placeholder)\n", + " display(ipw.VBox(display_list))\n", + "\n", + "# Function to handle changes in the materials dropdown\n", + "def load_material_metadata(change):\n", + " if material_selector.dropdown_boxes.children[0].value == -1:\n", + " material_selector.details_textbox.value = ''\n", + " material_selector.image_box.value = utils.read_file(CONFIG[\"default_image_filepath\"])\n", + " return\n", + " \n", + " # Get selected object properties information from config file\n", + " selected_object = material_selection_radio_button.value\n", + " selected_object_properties = CONFIG[\"objects\"][selected_object][\"properties\"]\n", + " \n", + " # Get material object information and dataset\n", + " material_object = OPENBIS_SESSION.get_object(material_selector.dropdown_boxes.children[0].value)\n", + " material_dataset = material_object.get_datasets()[0]\n", + " \n", + " # Get the object image preview\n", + " if material_dataset:\n", + " material_dataset.download(destination=\"images\")\n", + " material_selector.image_box.value = utils.read_file(f\"images/{material_dataset.permId}/{material_dataset.file_list[0]}\")\n", + " # Erase file after downloading it\n", + " shutil.rmtree(f\"images/{material_dataset.permId}\")\n", + " else:\n", + " material_selector.image_box.value = utils.read_file(CONFIG[\"default_image_filepath\"])\n", + "\n", + " # Make a string with the property values of the object\n", + " material_metadata = material_object.props.all()\n", + " material_metadata_string = \"\"\n", + " for prop_key in selected_object_properties:\n", + " prop_title = CONFIG[\"properties\"][prop_key][\"title\"]\n", + " if CONFIG[\"properties\"][prop_key][\"property_type\"] == \"QUANTITY_VALUE\":\n", + " value = material_metadata.get(prop_key)\n", + " if value:\n", + " prop_dict = json.loads(value)\n", + " material_metadata_string += f\"{prop_title}: {prop_dict['value']} {prop_dict['unit']}\\n\"\n", + " else:\n", + " material_metadata_string += f\"{prop_title}: {value}\\n\"\n", + " else:\n", + " material_metadata_string += f\"{prop_title}: {material_metadata.get(prop_key)}\\n\"\n", + "\n", + " material_selector.details_textbox.value = material_metadata_string\n", + " sample_out_name_textbox.value = utils.convert_datetime_to_string(utils.get_current_datetime()) + f\"_{material_metadata['$name']}\"\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Create a sample object in openBIS" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Select material" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "6569a050959d477bb9bee85a1a8704e5", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "RadioButtons(layout=Layout(width='300px'), options=('No material', 'Crystal', 'Wafer substrate', '2D-layer mat…" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "material_selection_radio_button.observe(select_material_radio_change, names='value')\n", + "material_selector.dropdown_boxes.children[0].observe(load_material_metadata, names = 'value')\n", + "display(material_selection_radio_button)\n", + "display(material_selector)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Sample out" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "create_button.on_click(create_sample_action)\n", + "quit_button.on_click(close_notebook)\n", + "display(sample_out_name_textbox, save_close_buttons_hbox)\n", + "display(increase_buttons_size)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Notebooks/connection_to_openbis/.export_openbis_object-0.ipynb b/Notebooks/connection_to_openbis/.export_openbis_object-0.ipynb new file mode 100644 index 00000000..75915ddd --- /dev/null +++ b/Notebooks/connection_to_openbis/.export_openbis_object-0.ipynb @@ -0,0 +1,135 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from IPython.display import display, clear_output, Javascript, Markdown\n", + "import ipywidgets as ipw\n", + "import widgets\n", + "import utils\n", + "import os\n", + "import base64\n", + "import json" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "CONFIG = utils.read_json(\"config.json\")\n", + "CONFIG_ELN = utils.get_aiidalab_eln_config()\n", + "# CONFIG_ELN = utils.read_json(\"eln_config.json\")\n", + "OPENBIS_SESSION, SESSION_DATA = utils.connect_openbis(CONFIG_ELN[\"url\"], CONFIG_ELN[\"token\"])\n", + "\n", + "# Dropdown box for getting openBIS objects\n", + "publication_selector = widgets.PublicationSelectionWidget()\n", + "publication_selector.load_dropdown_box()\n", + "\n", + "download_button = utils.Button(\n", + " description = '', disabled = False, button_style = '', \n", + " tooltip = 'Download', icon = 'download', layout = ipw.Layout(width = '100px', height = '50px')\n", + ")\n", + "\n", + "quit_button = utils.Button(\n", + " description = '', disabled = False, button_style = '', \n", + " tooltip = 'Main menu', icon = 'home', layout = ipw.Layout(width = '100px', height = '50px')\n", + ")\n", + "\n", + "download_close_buttons_hbox = ipw.HBox([download_button, quit_button])\n", + "increase_buttons_size = utils.HTML(data = ''.join(CONFIG[\"save_home_buttons_settings\"]))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def close_notebook(b):\n", + " display(utils.Javascript(data = 'window.location.replace(\"home.ipynb\")'))\n", + "\n", + "def download_openbis_object_metadata(b):\n", + " if publication_selector.dropdown_boxes.children[0].value == -1:\n", + " return\n", + " else:\n", + " openbis_selected_object_permid = publication_selector.dropdown_boxes.children[0].value\n", + " openbis_selected_object = OPENBIS_SESSION.get_sample(openbis_selected_object_permid)\n", + "\n", + " # Collect all the information about the object from openBIS\n", + " parent_child_relationships = openbis_selected_object.props.all()\n", + " parent_child_relationships[\"perm_id\"] = openbis_selected_object_permid\n", + " parent_child_relationships[\"type\"] = str(openbis_selected_object.type)\n", + " selected_object_schema = utils.get_parent_child_relationships_nested(\n", + " OPENBIS_SESSION, openbis_selected_object, parent_child_relationships\n", + " )\n", + " \n", + " # Export to JSON\n", + " json_data = json.dumps(selected_object_schema, indent=4).encode('utf-8')\n", + " json_data = base64.b64encode(json_data).decode('utf-8')\n", + " \n", + " display(Javascript(f\"\"\"\n", + " var a = document.createElement('a');\n", + " a.href = 'data:text/json;base64,{json_data}';\n", + " a.download = 'data.json';\n", + " document.body.appendChild(a);\n", + " a.click();\n", + " document.body.removeChild(a);\n", + " \"\"\"))\n", + " display(Markdown(f\"[You .]({notebook_link})\"))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Export publication metadata" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Select publication" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display(publication_selector)\n", + "display(download_close_buttons_hbox)\n", + "display(increase_buttons_size)\n", + "download_button.on_click(download_openbis_object_metadata)\n", + "quit_button.on_click(close_notebook)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Notebooks/connection_to_openbis/.export_openbis_object-1.ipynb b/Notebooks/connection_to_openbis/.export_openbis_object-1.ipynb new file mode 100644 index 00000000..731cf4ef --- /dev/null +++ b/Notebooks/connection_to_openbis/.export_openbis_object-1.ipynb @@ -0,0 +1,137 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from IPython.display import display, clear_output, Javascript, Markdown\n", + "import ipywidgets as ipw\n", + "import widgets\n", + "import utils\n", + "import os\n", + "import base64\n", + "import json" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "CONFIG = utils.read_json(\"config.json\")\n", + "CONFIG_ELN = utils.get_aiidalab_eln_config()\n", + "# CONFIG_ELN = utils.read_json(\"eln_config.json\")\n", + "OPENBIS_SESSION, SESSION_DATA = utils.connect_openbis(CONFIG_ELN[\"url\"], CONFIG_ELN[\"token\"])\n", + "\n", + "# Dropdown box for getting openBIS objects\n", + "publication_selector = widgets.PublicationSelectionWidget()\n", + "publication_selector.load_dropdown_box()\n", + "\n", + "prompt_textarea = utils.Textarea(description = \"Prompt: \", layout = ipw.Layout(width = '900px', height = '300px'))\n", + "\n", + "download_button = utils.Button(\n", + " description = '', disabled = False, button_style = '', \n", + " tooltip = 'Download', icon = 'download', layout = ipw.Layout(width = '100px', height = '50px')\n", + ")\n", + "\n", + "quit_button = utils.Button(\n", + " description = '', disabled = False, button_style = '', \n", + " tooltip = 'Main menu', icon = 'home', layout = ipw.Layout(width = '100px', height = '50px')\n", + ")\n", + "\n", + "download_close_buttons_hbox = ipw.HBox([download_button, quit_button])\n", + "increase_buttons_size = utils.HTML(data = ''.join(CONFIG[\"save_home_buttons_settings\"]))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def close_notebook(b):\n", + " display(utils.Javascript(data = 'window.location.replace(\"home.ipynb\")'))\n", + "\n", + "def download_openbis_object_metadata(b):\n", + " if publication_selector.dropdown.value == -1:\n", + " return\n", + " else:\n", + " openbis_selected_object_permid = publication_selector.dropdown_boxes.children[0].value\n", + " openbis_selected_object = OPENBIS_SESSION.get_sample(openbis_selected_object_permid)\n", + "\n", + " # Collect all the information about the object from openBIS\n", + " parent_child_relationships = openbis_selected_object.props.all()\n", + " parent_child_relationships[\"perm_id\"] = openbis_selected_object_permid\n", + " parent_child_relationships[\"type\"] = str(openbis_selected_object.type)\n", + " selected_object_schema = utils.get_parent_child_relationships_nested(\n", + " OPENBIS_SESSION, openbis_selected_object, parent_child_relationships\n", + " )\n", + " \n", + " # Export to JSON\n", + " json_data = json.dumps(selected_object_schema, indent=4).encode('utf-8')\n", + " json_data = base64.b64encode(json_data).decode('utf-8')\n", + " \n", + " display(Javascript(f\"\"\"\n", + " var a = document.createElement('a');\n", + " a.href = 'data:text/json;base64,{json_data}';\n", + " a.download = 'data.json';\n", + " document.body.appendChild(a);\n", + " a.click();\n", + " document.body.removeChild(a);\n", + " \"\"\"))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Export publication metadata" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Select publication" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display(publication_selector)\n", + "display(prompt_textarea)\n", + "display(download_close_buttons_hbox)\n", + "display(increase_buttons_size)\n", + "download_button.on_click(download_openbis_object_metadata)\n", + "quit_button.on_click(close_notebook)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Notebooks/connection_to_openbis/.export_openbis_object-2.ipynb b/Notebooks/connection_to_openbis/.export_openbis_object-2.ipynb new file mode 100644 index 00000000..560a2589 --- /dev/null +++ b/Notebooks/connection_to_openbis/.export_openbis_object-2.ipynb @@ -0,0 +1,137 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from IPython.display import display, clear_output, Javascript, Markdown\n", + "import ipywidgets as ipw\n", + "import widgets\n", + "import utils\n", + "import os\n", + "import base64\n", + "import json" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "CONFIG = utils.read_json(\"config.json\")\n", + "CONFIG_ELN = utils.get_aiidalab_eln_config()\n", + "# CONFIG_ELN = utils.read_json(\"eln_config.json\")\n", + "OPENBIS_SESSION, SESSION_DATA = utils.connect_openbis(CONFIG_ELN[\"url\"], CONFIG_ELN[\"token\"])\n", + "\n", + "# Dropdown box for getting openBIS objects\n", + "publication_selector = widgets.PublicationSelectionWidget()\n", + "publication_selector.load_dropdown_box()\n", + "\n", + "prompt_textarea = utils.Textarea(description = \"Prompt: \")\n", + "\n", + "download_button = utils.Button(\n", + " description = '', disabled = False, button_style = '', \n", + " tooltip = 'Download', icon = 'download', layout = ipw.Layout(width = '100px', height = '50px')\n", + ")\n", + "\n", + "quit_button = utils.Button(\n", + " description = '', disabled = False, button_style = '', \n", + " tooltip = 'Main menu', icon = 'home', layout = ipw.Layout(width = '100px', height = '50px')\n", + ")\n", + "\n", + "download_close_buttons_hbox = ipw.HBox([download_button, quit_button])\n", + "increase_buttons_size = utils.HTML(data = ''.join(CONFIG[\"save_home_buttons_settings\"]))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def close_notebook(b):\n", + " display(utils.Javascript(data = 'window.location.replace(\"home.ipynb\")'))\n", + "\n", + "def download_openbis_object_metadata(b):\n", + " if publication_selector.dropdown.value == -1:\n", + " return\n", + " else:\n", + " openbis_selected_object_permid = publication_selector.dropdown_boxes.children[0].value\n", + " openbis_selected_object = OPENBIS_SESSION.get_sample(openbis_selected_object_permid)\n", + "\n", + " # Collect all the information about the object from openBIS\n", + " parent_child_relationships = openbis_selected_object.props.all()\n", + " parent_child_relationships[\"perm_id\"] = openbis_selected_object_permid\n", + " parent_child_relationships[\"type\"] = str(openbis_selected_object.type)\n", + " selected_object_schema = utils.get_parent_child_relationships_nested(\n", + " OPENBIS_SESSION, openbis_selected_object, parent_child_relationships\n", + " )\n", + " \n", + " # Export to JSON\n", + " json_data = json.dumps(selected_object_schema, indent=4).encode('utf-8')\n", + " json_data = base64.b64encode(json_data).decode('utf-8')\n", + " \n", + " display(Javascript(f\"\"\"\n", + " var a = document.createElement('a');\n", + " a.href = 'data:text/json;base64,{json_data}';\n", + " a.download = 'data.json';\n", + " document.body.appendChild(a);\n", + " a.click();\n", + " document.body.removeChild(a);\n", + " \"\"\"))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Export publication metadata" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Select publication" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display(publication_selector)\n", + "display(prompt_textarea)\n", + "display(download_close_buttons_hbox)\n", + "display(increase_buttons_size)\n", + "download_button.on_click(download_openbis_object_metadata)\n", + "quit_button.on_click(close_notebook)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Notebooks/connection_to_openbis/.export_openbis_object-3.ipynb b/Notebooks/connection_to_openbis/.export_openbis_object-3.ipynb new file mode 100644 index 00000000..55278a70 --- /dev/null +++ b/Notebooks/connection_to_openbis/.export_openbis_object-3.ipynb @@ -0,0 +1,192 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "from IPython.display import display, clear_output, Javascript, Markdown\n", + "import ipywidgets as ipw\n", + "import widgets\n", + "import utils\n", + "import os\n", + "import base64\n", + "import json\n", + "import google.generativeai as genai" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "CONFIG = utils.read_json(\"config.json\")\n", + "CONFIG_ELN = utils.get_aiidalab_eln_config()\n", + "# CONFIG_ELN = utils.read_json(\"eln_config.json\")\n", + "OPENBIS_SESSION, SESSION_DATA = utils.connect_openbis(CONFIG_ELN[\"url\"], CONFIG_ELN[\"token\"])\n", + "\n", + "# Dropdown box for getting openBIS objects\n", + "publication_selector = widgets.PublicationSelectionWidget()\n", + "publication_selector.load_dropdown_box()\n", + "\n", + "prompt_textarea = utils.Textarea(\n", + " description = \"Prompt\", \n", + " layout = ipw.Layout(width = '980px', height = '300px'),\n", + " style = {\"description_width\": \"110px\"}\n", + ")\n", + "\n", + "model_answer_textarea = utils.Textarea(\n", + " description = \"Answer\", \n", + " layout = ipw.Layout(width = '980px', height = '300px'),\n", + " style = {\"description_width\": \"110px\"}\n", + ")\n", + "\n", + "enter_button = utils.Button(\n", + " description = '', disabled = False, button_style = '', \n", + " tooltip = 'Enter', icon = 'arrow-right', layout = ipw.Layout(width = '100px', height = '50px')\n", + ")\n", + "\n", + "load_model_button = utils.Button(\n", + " description = '', disabled = False, button_style = '', \n", + " tooltip = 'Load chatbot', icon = 'arrow-down', layout = ipw.Layout(width = '100px', height = '50px')\n", + ")\n", + "\n", + "download_button = utils.Button(\n", + " description = '', disabled = False, button_style = '', \n", + " tooltip = 'Download', icon = 'download', layout = ipw.Layout(width = '100px', height = '50px')\n", + ")\n", + "\n", + "quit_button = utils.Button(\n", + " description = '', disabled = False, button_style = '', \n", + " tooltip = 'Main menu', icon = 'home', layout = ipw.Layout(width = '100px', height = '50px')\n", + ")\n", + "\n", + "load_download_buttons_hbox = ipw.HBox([load_model_button, download_button])\n", + "\n", + "increase_buttons_size = utils.HTML(data = ''.join(CONFIG[\"save_home_buttons_settings\"]))\n", + "\n", + "# Google Gemini 1.5 Flash\n", + "google_api_key = utils.read_json(\"/home/jovyan/gemini_api.json\")\n", + "genai.configure(api_key=google_api_key[\"api_key\"])\n", + "model = genai.GenerativeModel(\"gemini-1.5-flash\") # gemini-1.5-flash\n", + "\n", + "# Start the chatbot\n", + "chat = model.start_chat()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def close_notebook(b):\n", + " display(utils.Javascript(data = 'window.location.replace(\"home.ipynb\")'))\n", + "\n", + "def ask_chatbot(change):\n", + " model_answer_textarea.value = \"\"\n", + " prompt = prompt_textarea.value\n", + " response = chat.send_message(prompt, stream = True)\n", + " for chunk in response:\n", + " model_answer_textarea.value = model_answer_textarea.value + chunk.text\n", + " \n", + "def get_openbis_object_metadata():\n", + " if publication_selector.dropdown.value == -1:\n", + " return None\n", + " else:\n", + " openbis_selected_object_permid = publication_selector.dropdown_boxes.children[0].value\n", + " openbis_selected_object = OPENBIS_SESSION.get_sample(openbis_selected_object_permid)\n", + "\n", + " # Collect all the information about the object from openBIS\n", + " parent_child_relationships = openbis_selected_object.props.all()\n", + " parent_child_relationships[\"perm_id\"] = openbis_selected_object_permid\n", + " parent_child_relationships[\"type\"] = str(openbis_selected_object.type)\n", + " openbis_objects_history = {}\n", + " selected_object_schema = utils.get_parent_child_relationships_nested(\n", + " OPENBIS_SESSION, openbis_selected_object, parent_child_relationships, openbis_objects_history\n", + " )\n", + "\n", + " return selected_object_schema\n", + "\n", + "def load_chatbot(b):\n", + " data_dict = get_openbis_object_metadata()\n", + " if data_dict:\n", + " json_data = json.dumps(data_dict, indent=4)\n", + " response = chat.send_message(f\"This is the graph of my study: {json_data}\")\n", + " display(utils.Javascript(data = \"alert('Chatbot is ready!')\"))\n", + "\n", + "def download_metadata(b):\n", + " data_dict = get_openbis_object_metadata()\n", + " if data_dict:\n", + " # Export to JSON\n", + " json_data = json.dumps(data_dict, indent=4).encode('utf-8')\n", + " json_data = base64.b64encode(json_data).decode('utf-8')\n", + " \n", + " display(Javascript(f\"\"\"\n", + " var a = document.createElement('a');\n", + " a.href = 'data:text/json;base64,{json_data}';\n", + " a.download = 'data.json';\n", + " document.body.appendChild(a);\n", + " a.click();\n", + " document.body.removeChild(a);\n", + " \"\"\"))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Export publication metadata" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Select publication" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display(increase_buttons_size)\n", + "display(publication_selector)\n", + "display(load_download_buttons_hbox)\n", + "display(prompt_textarea)\n", + "display(enter_button)\n", + "display(model_answer_textarea)\n", + "display(quit_button)\n", + "load_model_button.on_click(load_chatbot)\n", + "download_button.on_click(download_metadata)\n", + "enter_button.on_click(ask_chatbot)\n", + "quit_button.on_click(close_notebook)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Notebooks/connection_to_openbis/.export_openbis_object-4.ipynb b/Notebooks/connection_to_openbis/.export_openbis_object-4.ipynb new file mode 100644 index 00000000..9f6e25a3 --- /dev/null +++ b/Notebooks/connection_to_openbis/.export_openbis_object-4.ipynb @@ -0,0 +1,192 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "from IPython.display import display, clear_output, Javascript, Markdown\n", + "import ipywidgets as ipw\n", + "import widgets\n", + "import utils\n", + "import os\n", + "import base64\n", + "import json\n", + "import google.generativeai as genai" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "CONFIG = utils.read_json(\"config.json\")\n", + "CONFIG_ELN = utils.get_aiidalab_eln_config()\n", + "# CONFIG_ELN = utils.read_json(\"eln_config.json\")\n", + "OPENBIS_SESSION, SESSION_DATA = utils.connect_openbis(CONFIG_ELN[\"url\"], CONFIG_ELN[\"token\"])\n", + "\n", + "# Dropdown box for getting openBIS objects\n", + "publication_selector = widgets.PublicationSelectionWidget()\n", + "publication_selector.load_dropdown_box()\n", + "\n", + "prompt_textarea = utils.Textarea(\n", + " description = \"Prompt\", \n", + " layout = ipw.Layout(width = '980px', height = '300px'),\n", + " style = {\"description_width\": \"110px\"}\n", + ")\n", + "\n", + "model_answer_textarea = utils.Textarea(\n", + " description = \"Answer\", \n", + " layout = ipw.Layout(width = '980px', height = '300px'),\n", + " style = {\"description_width\": \"110px\"}\n", + ")\n", + "\n", + "enter_button = utils.Button(\n", + " description = '', disabled = False, button_style = '', \n", + " tooltip = 'Enter', icon = 'arrow-right', layout = ipw.Layout(width = '100px', height = '50px')\n", + ")\n", + "\n", + "load_model_button = utils.Button(\n", + " description = '', disabled = False, button_style = '', \n", + " tooltip = 'Load chatbot', icon = 'arrow-down', layout = ipw.Layout(width = '100px', height = '50px')\n", + ")\n", + "\n", + "download_button = utils.Button(\n", + " description = '', disabled = False, button_style = '', \n", + " tooltip = 'Download', icon = 'download', layout = ipw.Layout(width = '100px', height = '50px')\n", + ")\n", + "\n", + "quit_button = utils.Button(\n", + " description = '', disabled = False, button_style = '', \n", + " tooltip = 'Main menu', icon = 'home', layout = ipw.Layout(width = '100px', height = '50px')\n", + ")\n", + "\n", + "load_download_buttons_hbox = ipw.HBox([load_model_button, download_button])\n", + "\n", + "increase_buttons_size = utils.HTML(data = ''.join(CONFIG[\"save_home_buttons_settings\"]))\n", + "\n", + "# Google Gemini 1.5 Flash\n", + "google_api_key = utils.read_json(\"/home/jovyan/gemini_api.json\")\n", + "genai.configure(api_key=google_api_key[\"api_key\"])\n", + "model = genai.GenerativeModel(\"gemini-1.5-flash\") # gemini-1.5-flash\n", + "\n", + "# Start the chatbot\n", + "chat = model.start_chat()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def close_notebook(b):\n", + " display(utils.Javascript(data = 'window.location.replace(\"home.ipynb\")'))\n", + "\n", + "def ask_chatbot(change):\n", + " model_answer_textarea.value = \"\"\n", + " prompt = prompt_textarea.value\n", + " response = chat.send_message(prompt, stream = True)\n", + " # model_answer_textarea.value = response.text\n", + " for chunk in response:\n", + " model_answer_textarea.value = model_answer_textarea.value + chunk.text\n", + " \n", + "def get_openbis_object_metadata():\n", + " if publication_selector.dropdown.value == -1:\n", + " return None\n", + " else:\n", + " openbis_selected_object_permid = publication_selector.dropdown_boxes.children[0].value\n", + " openbis_selected_object = OPENBIS_SESSION.get_sample(openbis_selected_object_permid)\n", + "\n", + " # Collect all the information about the object from openBIS\n", + " parent_child_relationships = openbis_selected_object.props.all()\n", + " parent_child_relationships[\"perm_id\"] = openbis_selected_object_permid\n", + " parent_child_relationships[\"type\"] = str(openbis_selected_object.type)\n", + " selected_object_schema = utils.get_parent_child_relationships_nested(\n", + " OPENBIS_SESSION, openbis_selected_object, parent_child_relationships\n", + " )\n", + "\n", + " return selected_object_schema\n", + "\n", + "def load_chatbot(b):\n", + " data_dict = get_openbis_object_metadata()\n", + " if data_dict:\n", + " json_data = json.dumps(data_dict, indent=4)\n", + " response = chat.send_message(f\"This is the graph of my study: {json_data}\")\n", + " display(utils.Javascript(data = \"alert('Chatbot is ready!')\"))\n", + "\n", + "def download_metadata(b):\n", + " data_dict = get_openbis_object_metadata()\n", + " if data_dict:\n", + " # Export to JSON\n", + " json_data = json.dumps(data_dict, indent=4).encode('utf-8')\n", + " json_data = base64.b64encode(json_data).decode('utf-8')\n", + " \n", + " display(Javascript(f\"\"\"\n", + " var a = document.createElement('a');\n", + " a.href = 'data:text/json;base64,{json_data}';\n", + " a.download = 'data.json';\n", + " document.body.appendChild(a);\n", + " a.click();\n", + " document.body.removeChild(a);\n", + " \"\"\"))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Export publication metadata" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Select publication" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display(increase_buttons_size)\n", + "display(publication_selector)\n", + "display(load_download_buttons_hbox)\n", + "display(prompt_textarea)\n", + "display(enter_button)\n", + "display(model_answer_textarea)\n", + "display(quit_button)\n", + "load_model_button.on_click(load_chatbot)\n", + "download_button.on_click(download_metadata)\n", + "enter_button.on_click(ask_chatbot)\n", + "quit_button.on_click(close_notebook)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Notebooks/connection_to_openbis/.home-3.ipynb b/Notebooks/connection_to_openbis/.home-3.ipynb new file mode 100644 index 00000000..50cab7c7 --- /dev/null +++ b/Notebooks/connection_to_openbis/.home-3.ipynb @@ -0,0 +1,95 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Import libraries\n", + "from IPython.display import display\n", + "import ipywidgets as ipw\n", + "import utils" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "CONFIG = utils.read_json(\"config.json\")\n", + "CONFIG_ELN = utils.get_aiidalab_eln_config()\n", + "# CONFIG_ELN = utils.read_json(\"eln_config.json\")\n", + "OPENBIS_SESSION, SESSION_DATA = utils.connect_openbis(CONFIG_ELN[\"url\"], CONFIG_ELN[\"token\"])\n", + "\n", + "openbis_connection_status_htmlbox = utils.HTMLbox(value = '')\n", + "tab_children, tab_titles = [], []\n", + "if OPENBIS_SESSION:\n", + " tabs_data = CONFIG[\"home_page\"][\"enable_links\"]\n", + " openbis_connection_status_htmlbox.value = CONFIG[\"home_page\"][\"enable_status\"]\n", + "else:\n", + " tabs_data = CONFIG[\"home_page\"][\"disable_links\"]\n", + " openbis_connection_status_htmlbox.value = CONFIG[\"home_page\"][\"disable_status\"]\n", + "\n", + "# Populate tab widget\n", + "for tab_title, html_content in tabs_data.items():\n", + " tab_titles.append(tab_title)\n", + " content = ''.join(html_content)\n", + " widgets_list = [utils.HTMLbox(value=content)]\n", + " vbox = ipw.VBox(widgets_list)\n", + " tab_children.append(vbox)\n", + "\n", + "tabs = ipw.Tab(children=tab_children)\n", + "for idx, title in enumerate(tab_titles):\n", + " tabs.set_title(idx, title)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# AiiDAlab-openBIS app" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display(openbis_connection_status_htmlbox)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display(tabs)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Notebooks/connection_to_openbis/.home-4.ipynb b/Notebooks/connection_to_openbis/.home-4.ipynb new file mode 100644 index 00000000..50cab7c7 --- /dev/null +++ b/Notebooks/connection_to_openbis/.home-4.ipynb @@ -0,0 +1,95 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Import libraries\n", + "from IPython.display import display\n", + "import ipywidgets as ipw\n", + "import utils" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "CONFIG = utils.read_json(\"config.json\")\n", + "CONFIG_ELN = utils.get_aiidalab_eln_config()\n", + "# CONFIG_ELN = utils.read_json(\"eln_config.json\")\n", + "OPENBIS_SESSION, SESSION_DATA = utils.connect_openbis(CONFIG_ELN[\"url\"], CONFIG_ELN[\"token\"])\n", + "\n", + "openbis_connection_status_htmlbox = utils.HTMLbox(value = '')\n", + "tab_children, tab_titles = [], []\n", + "if OPENBIS_SESSION:\n", + " tabs_data = CONFIG[\"home_page\"][\"enable_links\"]\n", + " openbis_connection_status_htmlbox.value = CONFIG[\"home_page\"][\"enable_status\"]\n", + "else:\n", + " tabs_data = CONFIG[\"home_page\"][\"disable_links\"]\n", + " openbis_connection_status_htmlbox.value = CONFIG[\"home_page\"][\"disable_status\"]\n", + "\n", + "# Populate tab widget\n", + "for tab_title, html_content in tabs_data.items():\n", + " tab_titles.append(tab_title)\n", + " content = ''.join(html_content)\n", + " widgets_list = [utils.HTMLbox(value=content)]\n", + " vbox = ipw.VBox(widgets_list)\n", + " tab_children.append(vbox)\n", + "\n", + "tabs = ipw.Tab(children=tab_children)\n", + "for idx, title in enumerate(tab_titles):\n", + " tabs.set_title(idx, title)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# AiiDAlab-openBIS app" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display(openbis_connection_status_htmlbox)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display(tabs)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Notebooks/connection_to_openbis/.home-5.ipynb b/Notebooks/connection_to_openbis/.home-5.ipynb new file mode 100644 index 00000000..50cab7c7 --- /dev/null +++ b/Notebooks/connection_to_openbis/.home-5.ipynb @@ -0,0 +1,95 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Import libraries\n", + "from IPython.display import display\n", + "import ipywidgets as ipw\n", + "import utils" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "CONFIG = utils.read_json(\"config.json\")\n", + "CONFIG_ELN = utils.get_aiidalab_eln_config()\n", + "# CONFIG_ELN = utils.read_json(\"eln_config.json\")\n", + "OPENBIS_SESSION, SESSION_DATA = utils.connect_openbis(CONFIG_ELN[\"url\"], CONFIG_ELN[\"token\"])\n", + "\n", + "openbis_connection_status_htmlbox = utils.HTMLbox(value = '')\n", + "tab_children, tab_titles = [], []\n", + "if OPENBIS_SESSION:\n", + " tabs_data = CONFIG[\"home_page\"][\"enable_links\"]\n", + " openbis_connection_status_htmlbox.value = CONFIG[\"home_page\"][\"enable_status\"]\n", + "else:\n", + " tabs_data = CONFIG[\"home_page\"][\"disable_links\"]\n", + " openbis_connection_status_htmlbox.value = CONFIG[\"home_page\"][\"disable_status\"]\n", + "\n", + "# Populate tab widget\n", + "for tab_title, html_content in tabs_data.items():\n", + " tab_titles.append(tab_title)\n", + " content = ''.join(html_content)\n", + " widgets_list = [utils.HTMLbox(value=content)]\n", + " vbox = ipw.VBox(widgets_list)\n", + " tab_children.append(vbox)\n", + "\n", + "tabs = ipw.Tab(children=tab_children)\n", + "for idx, title in enumerate(tab_titles):\n", + " tabs.set_title(idx, title)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# AiiDAlab-openBIS app" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display(openbis_connection_status_htmlbox)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display(tabs)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Notebooks/connection_to_openbis/.home-6.ipynb b/Notebooks/connection_to_openbis/.home-6.ipynb new file mode 100644 index 00000000..50cab7c7 --- /dev/null +++ b/Notebooks/connection_to_openbis/.home-6.ipynb @@ -0,0 +1,95 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Import libraries\n", + "from IPython.display import display\n", + "import ipywidgets as ipw\n", + "import utils" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "CONFIG = utils.read_json(\"config.json\")\n", + "CONFIG_ELN = utils.get_aiidalab_eln_config()\n", + "# CONFIG_ELN = utils.read_json(\"eln_config.json\")\n", + "OPENBIS_SESSION, SESSION_DATA = utils.connect_openbis(CONFIG_ELN[\"url\"], CONFIG_ELN[\"token\"])\n", + "\n", + "openbis_connection_status_htmlbox = utils.HTMLbox(value = '')\n", + "tab_children, tab_titles = [], []\n", + "if OPENBIS_SESSION:\n", + " tabs_data = CONFIG[\"home_page\"][\"enable_links\"]\n", + " openbis_connection_status_htmlbox.value = CONFIG[\"home_page\"][\"enable_status\"]\n", + "else:\n", + " tabs_data = CONFIG[\"home_page\"][\"disable_links\"]\n", + " openbis_connection_status_htmlbox.value = CONFIG[\"home_page\"][\"disable_status\"]\n", + "\n", + "# Populate tab widget\n", + "for tab_title, html_content in tabs_data.items():\n", + " tab_titles.append(tab_title)\n", + " content = ''.join(html_content)\n", + " widgets_list = [utils.HTMLbox(value=content)]\n", + " vbox = ipw.VBox(widgets_list)\n", + " tab_children.append(vbox)\n", + "\n", + "tabs = ipw.Tab(children=tab_children)\n", + "for idx, title in enumerate(tab_titles):\n", + " tabs.set_title(idx, title)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# AiiDAlab-openBIS app" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display(openbis_connection_status_htmlbox)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display(tabs)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Notebooks/connection_to_openbis/.home-7.ipynb b/Notebooks/connection_to_openbis/.home-7.ipynb new file mode 100644 index 00000000..fa266541 --- /dev/null +++ b/Notebooks/connection_to_openbis/.home-7.ipynb @@ -0,0 +1,95 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Import libraries\n", + "from IPython.display import display\n", + "import ipywidgets as ipw\n", + "import utils" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "CONFIG = utils.read_json(\"config.json\")\n", + "CONFIG_ELN = utils.get_aiidalab_eln_config()\n", + "# CONFIG_ELN = utils.read_json(\"eln_config.json\")\n", + "OPENBIS_SESSION, SESSION_DATA = utils.connect_openbis(CONFIG_ELN[\"url\"], CONFIG_ELN[\"token\"])\n", + "\n", + "openbis_connection_status_htmlbox = utils.HTMLbox(value = '')\n", + "tab_children, tab_titles = [], []\n", + "if OPENBIS_SESSION:\n", + " tabs_data = CONFIG[\"home_page\"][\"enable_links\"]\n", + " openbis_connection_status_htmlbox.value = CONFIG[\"home_page\"][\"enable_status\"]\n", + "else:\n", + " tabs_data = CONFIG[\"home_page\"][\"disable_links\"]\n", + " openbis_connection_status_htmlbox.value = CONFIG[\"home_page\"][\"disable_status\"]\n", + "\n", + "# Populate tab widget\n", + "for tab_title, html_content in tabs_data.items():\n", + " tab_titles.append(tab_title)\n", + " content = ''.join(html_content)\n", + " widgets_list = [utils.HTMLbox(value=content)]\n", + " vbox = ipw.VBox(widgets_list)\n", + " tab_children.append(vbox)\n", + "\n", + "tabs = ipw.Tab(children=tab_children)\n", + "for idx, title in enumerate(tab_titles):\n", + " tabs.set_title(idx, title)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# AiiDAlab-openBIS app" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display(openbis_connection_status_htmlbox)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display(tabs)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Notebooks/connection_to_openbis/.send_simulation_to_openbis-1.ipynb b/Notebooks/connection_to_openbis/.send_simulation_to_openbis-1.ipynb new file mode 100644 index 00000000..2330bda3 --- /dev/null +++ b/Notebooks/connection_to_openbis/.send_simulation_to_openbis-1.ipynb @@ -0,0 +1,468 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "%load_ext aiida\n", + "%aiida\n", + "from IPython.display import display, clear_output, Markdown\n", + "import ipywidgets as ipw\n", + "import widgets\n", + "import utils\n", + "import aiida_utils\n", + "import json\n", + "import shutil" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "CONFIG = utils.read_json(\"config.json\")\n", + "CONFIG_ELN = utils.get_aiidalab_eln_config()\n", + "# CONFIG_ELN = utils.read_json(\"eln_config.json\")\n", + "OPENBIS_SESSION, SESSION_DATA = utils.connect_openbis(CONFIG_ELN[\"url\"], CONFIG_ELN[\"token\"])\n", + "slabs_options = [object_key for object_key, object_info in CONFIG[\"objects\"].items() if object_info[\"object_type\"] == \"slab\"]\n", + "slabs_options.insert(0, \"No material\")\n", + "material_selection_radio_button = utils.Radiobuttons(\n", + " description = '', options = slabs_options, \n", + " disabled = False, layout = ipw.Layout(width = '300px'), \n", + " style = {'description_width': \"100px\"}\n", + ")\n", + "\n", + "simulation_material_dropdown = utils.Dropdown(\n", + " description = \"Simulation material\", disabled = False, \n", + " layout = ipw.Layout(width = \"300px\"), \n", + " options = [\"Select an option...\", \"Molecule and slab\", \"Reaction product\"],\n", + " value = \"Select an option...\",\n", + " style = {'description_width': \"150px\"} \n", + ")\n", + "simulation_material_properties_output = ipw.Output()\n", + "\n", + "material_selector = widgets.MaterialSelectionWidget()\n", + "\n", + "experiment_selector = widgets.ExperimentSelectionWidget()\n", + "experiment_selector.load_dropdown_box()\n", + "\n", + "molecule_selector_list = widgets.MultipleSelectorWidget(\"molecule\")\n", + "molecule_selector_list_output = ipw.Output()\n", + "\n", + "add_molecule_button = utils.Button(\n", + " description = 'Add molecule', disabled = False, button_style = '', \n", + " tooltip = 'Add molecule', layout = ipw.Layout(width = '150px', height = '25px')\n", + ")\n", + "remove_molecule_button = utils.Button(\n", + " description = 'Remove molecule', disabled = False, button_style = '', \n", + " tooltip = 'Remove molecule', layout = ipw.Layout(width = '150px', height = '25px')\n", + ")\n", + "add_remove_molecule_buttons_hbox = ipw.HBox([add_molecule_button, remove_molecule_button])\n", + "\n", + "product_selector_list = widgets.MultipleSelectorWidget(\"product\")\n", + "product_selector_list_output = ipw.Output()\n", + "\n", + "add_product_button = utils.Button(\n", + " description = 'Add product', disabled = False, button_style = '', \n", + " tooltip = 'Add product', layout = ipw.Layout(width = '150px', height = '25px')\n", + ")\n", + "remove_product_button = utils.Button(\n", + " description = 'Remove product', disabled = False, button_style = '', \n", + " tooltip = 'Remove product', layout = ipw.Layout(width = '150px', height = '25px')\n", + ")\n", + "add_remove_product_buttons_hbox = ipw.HBox([add_product_button, remove_product_button])\n", + "\n", + "is_automatic_checkbox = utils.Checkbox(\n", + " description = 'Simulation developed using AiiDA', value = False, \n", + " disabled = False, layout = ipw.Layout(width = \"250px\"), indent = False\n", + ")\n", + "\n", + "simulation_details_output = ipw.Output()\n", + "\n", + "simulation_type_dropdown = utils.Dropdown(\n", + " description = \"Simulation type\", disabled = False, \n", + " layout = ipw.Layout(width = \"300px\"), \n", + " options = [\"Select an option...\", \"Geometry Optimisation\", \"Band Structure\", \"PDOS\", \"Vibrational Spectroscopy\", \"Unclassified Simulation\"],\n", + " value = \"Select an option...\",\n", + " style = {'description_width': \"110px\"} \n", + ")\n", + "\n", + "atomistic_model_selector = widgets.AtomisticModelSelectionWidget()\n", + "atomistic_model_selector.load_dropdown_box()\n", + "\n", + "simulation_properties_output = ipw.Output()\n", + "simulation_properties_widgets = {\n", + " simulation_type: widgets.ObjectPropertiesWidgets(simulation_type) for simulation_type in simulation_type_dropdown.options[1:]\n", + "}\n", + "\n", + "simulation_selector = widgets.SimulationSelectionWidget()\n", + "simulation_selector.load_dropdown_box(\"aiidalab\")\n", + "\n", + "code_selector = widgets.CodeMultipleSelectionWidget()\n", + "code_selector.load_selector()\n", + "\n", + "simulation_dataset_upload = ipw.FileUpload(multiple = True)\n", + "simulation_preview_upload = ipw.FileUpload(multiple = False, accept = '.png, .jpg, .jpeg')\n", + "simulation_images_upload = ipw.FileUpload(multiple = True, accept = '.png, .jpg, .jpeg, .tar')\n", + "simulation_support_files_upload = ipw.FileUpload(multiple = True)\n", + "\n", + "increase_buttons_size = utils.HTML(data = ''.join(CONFIG[\"save_home_buttons_settings\"]))\n", + "create_button = utils.Button(\n", + " description = '', disabled = False, button_style = '', tooltip = 'Save', \n", + " icon = 'save', layout = ipw.Layout(width = '100px', height = '50px')\n", + ")\n", + "quit_button = utils.Button(\n", + " description = '', disabled = False, button_style = '', \n", + " tooltip = 'Main menu', icon = 'home', layout = ipw.Layout(width = '100px', height = '50px')\n", + ")\n", + "save_close_buttons_hbox = ipw.HBox([create_button, quit_button])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def close_notebook(b):\n", + " display(utils.Javascript(data = 'window.location.replace(\"home.ipynb\")'))\n", + "\n", + "def select_material_radio_change(change):\n", + " material_type = material_selection_radio_button.value\n", + " if material_type == \"No material\":\n", + " with material_selector:\n", + " clear_output()\n", + " return\n", + " \n", + " material_selector.details_textbox.value = ''\n", + " material_types = {}\n", + " for object_key, object_info in CONFIG[\"objects\"].items():\n", + " if object_info[\"object_type\"] == \"slab\":\n", + " material_types[object_key] = (object_info[\"openbis_object_type\"], object_info[\"placeholder\"])\n", + " \n", + " with material_selector:\n", + " clear_output()\n", + " display_list = [\n", + " material_selector.dropdown_boxes,\n", + " ipw.HBox([material_selector.details_textbox, material_selector.image_box])\n", + " ]\n", + " material_class, placeholder = material_types.get(material_type)\n", + " material_selector.load_dropdown_box(material_class, placeholder)\n", + " display(ipw.VBox(display_list))\n", + "\n", + "# Function to handle changes in the materials dropdown\n", + "def load_material_metadata(change):\n", + " if material_selector.dropdown_boxes.children[0].value == -1:\n", + " material_selector.details_textbox.value = ''\n", + " material_selector.image_box.value = utils.read_file(CONFIG[\"default_image_filepath\"])\n", + " return\n", + " \n", + " # Get selected object properties information from config file\n", + " selected_object = material_selection_radio_button.value\n", + " selected_object_properties = CONFIG[\"objects\"][selected_object][\"properties\"]\n", + " \n", + " # Get material object information and dataset\n", + " material_object = OPENBIS_SESSION.get_object(material_selector.dropdown_boxes.children[0].value)\n", + " material_dataset = material_object.get_datasets()[0]\n", + " \n", + " # Get the object image preview\n", + " if material_dataset:\n", + " material_dataset.download(destination=\"images\")\n", + " material_selector.image_box.value = utils.read_file(f\"images/{material_dataset.permId}/{material_dataset.file_list[0]}\")\n", + " # Erase file after downloading it\n", + " shutil.rmtree(f\"images/{material_dataset.permId}\")\n", + " else:\n", + " material_selector.image_box.value = utils.read_file(CONFIG[\"default_image_filepath\"])\n", + "\n", + " # Make a string with the property values of the object\n", + " material_metadata = material_object.props.all()\n", + " material_metadata_string = \"\"\n", + " for prop_key in selected_object_properties:\n", + " prop_title = CONFIG[\"properties\"][prop_key][\"title\"]\n", + " if CONFIG[\"properties\"][prop_key][\"property_type\"] == \"QUANTITY_VALUE\":\n", + " value = material_metadata.get(prop_key)\n", + " if value:\n", + " prop_dict = json.loads(value)\n", + " material_metadata_string += f\"{prop_title}: {prop_dict['value']} {prop_dict['unit']}\\n\"\n", + " else:\n", + " material_metadata_string += f\"{prop_title}: {value}\\n\"\n", + " else:\n", + " material_metadata_string += f\"{prop_title}: {material_metadata.get(prop_key)}\\n\"\n", + "\n", + " material_selector.details_textbox.value = material_metadata_string\n", + "\n", + "def add_molecule_widget(b):\n", + " molecule_selector_list.add_selector()\n", + " with molecule_selector_list_output:\n", + " clear_output()\n", + " display(molecule_selector_list)\n", + "\n", + "def remove_molecule_widget(b):\n", + " molecule_selector_list.remove_selector()\n", + " with molecule_selector_list_output:\n", + " clear_output()\n", + " display(molecule_selector_list)\n", + "\n", + "def add_product_widget(b):\n", + " product_selector_list.add_selector()\n", + " with product_selector_list_output:\n", + " clear_output()\n", + " display(product_selector_list)\n", + "\n", + "def remove_product_widget(b):\n", + " product_selector_list.remove_selector()\n", + " with product_selector_list_output:\n", + " clear_output()\n", + " display(product_selector_list)\n", + "\n", + "def find_atomistic_model_perm_id(data):\n", + " if isinstance(data, dict):\n", + " # Check if the current object is of type ATOMISTIC_MODEL and is at the deepest level\n", + " if data.get('type') == 'ATOMISTIC_MODEL' and (not data.get('has_part') or not isinstance(data['has_part'], list)):\n", + " return data['perm_id']\n", + " # Recurse into 'has_part' if it exists\n", + " if 'has_part' in data:\n", + " for item in data['has_part']:\n", + " result = find_atomistic_model_perm_id(item)\n", + " if result:\n", + " return result\n", + " \n", + " elif isinstance(data, list):\n", + " # Recurse into list elements\n", + " for item in data:\n", + " result = find_atomistic_model_perm_id(item)\n", + " if result:\n", + " return result\n", + " \n", + " return None\n", + " \n", + "def create_simulation_openbis(b):\n", + " if experiment_selector.dropdown.value == -1:\n", + " display(utils.Javascript(data = \"alert('Select an experiment.')\"))\n", + " return\n", + " else:\n", + " object_parents = []\n", + " if molecule_selector_list.selectors:\n", + " for molecule_selector in molecule_selector_list.selectors:\n", + " if molecule_selector.dropdown.value != -1:\n", + " object_parents.append(molecule_selector.dropdown.value)\n", + " if material_selector.dropdown.value != -1:\n", + " object_parents.append(material_selector.dropdown.value)\n", + " if product_selector_list.selectors:\n", + " for product_selector in product_selector_list.selectors:\n", + " if product_selector.dropdown.value != -1:\n", + " object_parents.append(product_selector.dropdown.value)\n", + " \n", + " if is_automatic_checkbox.value:\n", + " last_export = aiida_utils.export_workchain(\n", + " OPENBIS_SESSION, \n", + " experiment_selector.dropdown.value, \n", + " simulation_selector.dropdown.value\n", + " )\n", + " \n", + " if last_export:\n", + " last_export_metadata = utils.get_parent_child_relationships_nested(OPENBIS_SESSION, last_export)\n", + " first_atomistic_model_permid = find_atomistic_model_perm_id(last_export_metadata)\n", + " first_atomistic_model = utils.get_openbis_objects(OPENBIS_SESSION, identifier = first_atomistic_model_permid)[0]\n", + " if first_atomistic_model.parents is None:\n", + " first_atomistic_model.set_parents(object_parents)\n", + " first_atomistic_model.save()\n", + " display(utils.Javascript(data = \"alert('Upload successful!')\"))\n", + " else:\n", + " display(utils.Javascript(data = \"alert('Simulation is already in openBIS!')\"))\n", + " else:\n", + " if simulation_type_dropdown.value == \"Select an option...\":\n", + " display(utils.Javascript(data = \"alert('Select a simulation type.')\"))\n", + " return\n", + " else:\n", + " simulation_type = simulation_type_dropdown.value\n", + " simulation_openbis_type = CONFIG[\"objects\"][simulation_type][\"openbis_object_type\"]\n", + " object_properties = {}\n", + " for prop in CONFIG[\"objects\"][simulation_type][\"properties\"]:\n", + " if CONFIG[\"properties\"][prop][\"property_type\"] == \"QUANTITY_VALUE\":\n", + " object_properties[prop] = json.dumps({\"has_value\": simulation_properties_widgets[simulation_type_dropdown.value].properties_widgets[prop].children[0].value, \n", + " \"has_unit\": simulation_properties_widgets[simulation_type_dropdown.value].properties_widgets[prop].children[1].value})\n", + " elif CONFIG[\"properties\"][prop][\"property_type\"] == \"JSON\":\n", + " json_string = simulation_properties_widgets[simulation_type_dropdown.value].properties_widgets[prop].value\n", + " json_string = json_string.replace(\"'\", '\"')\n", + " if json_string:\n", + " if utils.is_valid_json(json_string):\n", + " object_properties[prop] = json_string\n", + " else:\n", + " display(utils.Javascript(data = \"alert('There is a JSON property that is not a valid JSON object.')\"))\n", + " return\n", + " else:\n", + " object_properties[prop] = simulation_properties_widgets[simulation_type_dropdown.value].properties_widgets[prop].value\n", + " \n", + " if atomistic_model_selector.dropdown.value != -1:\n", + " object_parents.append(atomistic_model_selector.dropdown.value)\n", + "\n", + " object_parents.extend(code_selector.selector.value)\n", + " \n", + " simulation_object = utils.create_openbis_object(\n", + " OPENBIS_SESSION, type=simulation_openbis_type, \n", + " collection = experiment_selector.dropdown.value, \n", + " props = object_properties,\n", + " parents = object_parents\n", + " )\n", + "\n", + " utils.upload_datasets(OPENBIS_SESSION, simulation_object, simulation_dataset_upload, \"RAW_DATA\")\n", + " utils.upload_datasets(OPENBIS_SESSION, simulation_object, simulation_preview_upload, \"ELN_PREVIEW\")\n", + " utils.upload_datasets(OPENBIS_SESSION, simulation_object, simulation_images_upload, \"RAW_DATA\")\n", + " utils.upload_datasets(OPENBIS_SESSION, simulation_object, simulation_support_files_upload, \"RAW_DATA\")\n", + " display(utils.Javascript(data = \"alert('Upload successful!')\"))\n", + "\n", + "def enter_simulation_details(change):\n", + " with simulation_details_output:\n", + " clear_output()\n", + " display(Markdown(\"### Select molecules\"))\n", + " display(molecule_selector_list_output)\n", + " with molecule_selector_list_output:\n", + " display(molecule_selector_list) \n", + " display(add_remove_molecule_buttons_hbox)\n", + " display(Markdown(\"### Select reaction products\"))\n", + " display(product_selector_list_output)\n", + " with product_selector_list_output:\n", + " display(product_selector_list) \n", + " display(add_remove_product_buttons_hbox)\n", + " display(Markdown(\"### Select slab\"))\n", + " display(material_selection_radio_button)\n", + " display(material_selector)\n", + " \n", + " if is_automatic_checkbox.value:\n", + " display(Markdown(\"### Select simulation\"))\n", + " display(simulation_selector)\n", + " else:\n", + " display(Markdown(\"### Select simulation type\"))\n", + " display(simulation_type_dropdown)\n", + " display(simulation_properties_output) \n", + " display(Markdown(\"### Select atomistic model\"))\n", + " display(atomistic_model_selector)\n", + " display(Markdown(\"### Select code\"))\n", + " display(code_selector)\n", + " display(Markdown(\"### Upload dataset\"))\n", + " display(simulation_dataset_upload)\n", + " display(Markdown(\"### Upload image preview\"))\n", + " display(simulation_preview_upload)\n", + " display(Markdown(\"### Upload other images\"))\n", + " display(simulation_images_upload)\n", + " display(Markdown(\"### Upload other support files\"))\n", + " display(simulation_support_files_upload)\n", + "\n", + "def load_simulation_properties_widgets(change):\n", + " with simulation_properties_output:\n", + " clear_output()\n", + " if simulation_type_dropdown.value != \"Select an option...\":\n", + " display(Markdown(\"### Enter simulation properties\"))\n", + " display(simulation_properties_widgets[simulation_type_dropdown.value])\n", + "\n", + "def load_structure_material(change):\n", + " if simulation_selector.dropdown.value != -1:\n", + " simulation_pk = simulation_selector.dropdown.value\n", + " simulation_uuid = load_node(simulation_pk).uuid\n", + " first_structure_uuid = aiida_utils.original_structure(simulation_uuid)\n", + " first_structure_extras = load_node(first_structure_uuid).extras\n", + " if first_structure_extras[\"eln\"][\"data_type\"] == \"cdxml\":\n", + " add_product_widget(None)\n", + " product_selector_list.selectors[0].dropdown.value = first_structure_extras[\"eln\"][\"sample_uuid\"]\n", + " else:\n", + " add_molecule_widget(None)\n", + " molecule_selector_list.selectors[0].dropdown.value = first_structure_extras[\"eln\"][\"sample_uuid\"]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Send simulation to openBIS" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Select experiment" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display(experiment_selector)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Simulation details" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display(is_automatic_checkbox)\n", + "display(simulation_details_output)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Save simulation workchain" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "display(save_close_buttons_hbox)\n", + "display(increase_buttons_size)\n", + "is_automatic_checkbox.observe(enter_simulation_details, names = 'value')\n", + "enter_simulation_details(None)\n", + "atomistic_model_selector.dropdown.observe(atomistic_model_selector.load_metadata, names = 'value')\n", + "simulation_selector.dropdown.observe(load_structure_material, names = 'value')\n", + "simulation_type_dropdown.observe(load_simulation_properties_widgets, names = 'value')\n", + "material_selection_radio_button.observe(select_material_radio_change, names='value')\n", + "material_selector.dropdown.observe(load_material_metadata, names = 'value')\n", + "add_molecule_button.on_click(add_molecule_widget)\n", + "remove_molecule_button.on_click(remove_molecule_widget)\n", + "add_product_button.on_click(add_product_widget)\n", + "remove_product_button.on_click(remove_product_widget)\n", + "create_button.on_click(create_simulation_openbis)\n", + "quit_button.on_click(close_notebook)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.13" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Notebooks/connection_to_openbis/aiida_utils.py b/Notebooks/connection_to_openbis/aiida_utils.py index 91c65cc8..df97ce2a 100644 --- a/Notebooks/connection_to_openbis/aiida_utils.py +++ b/Notebooks/connection_to_openbis/aiida_utils.py @@ -492,8 +492,10 @@ def structure_to_atomistic_model(openbis_session, structure_uuid, uuids): dictionary={ '$name': ase_geo.get_chemical_formula(), 'wfms_uuid': structure.uuid, - 'structure': json.dumps(encode(ase_geo)) + 'volume': structure.get_cell_volume(), + 'cell': json.dumps({"cell": structure.cell}) } + if dimensionality: dictionary["dimensionality"] = int(dimensionality[0]) dictionary["periodic_boundary_conditions"] = [bool(i) for i in dimensionality[1]] @@ -512,6 +514,16 @@ def structure_to_atomistic_model(openbis_session, structure_uuid, uuids): files = [geo_to_png(ase_geo)] ) + structure_json = encode(ase_geo) + utils.create_json(structure_json, "structure_json.json") + utils.create_openbis_dataset( + openbis_session, + type = "RAW_DATA", + sample = obobject, + files = ["structure_json.json"] + ) + os.remove("structure_json.json") + return obobject def create_obis_object(obtype=None,parameters=None): @@ -825,7 +837,16 @@ def Cp2kGeoOptWorkChain_export(openbis_session, experiment_id, workchain_uuid, u sys_params = workchain.inputs.sys_params.get_dict() dft_params = workchain.inputs.dft_params.get_dict() code = workchain.inputs.code.description - output_parameters = workchain.outputs.output_parameters.get_dict() + + properties=['energy','energy_scf','energy_units','bandgap_spin1_au','bandgap_spin2_au'] + try: + all_output_parameters = workchain.outputs.dft_output_parameters.get_dict() + except: + all_output_parameters = workchain.outputs.output_parameters.get_dict() + output_parameters = {key: all_output_parameters[key] for key in properties if key in all_output_parameters} + step_info={key: values[-1] for key, values in all_output_parameters['motion_step_info'].items()} + output_parameters.update(step_info) + input_parameters = {} try: input_parameters = workchain.outputs.final_input_parameters.get_dict() @@ -893,11 +914,16 @@ def Cp2kStmWorkChain_export(openbis_session, experiment_id, workchain_uuid, uuid spm_params = workchain.inputs.spm_params.get_dict() # cp2k_code = workchain.inputs.cp2k_code.description spm_code=workchain.inputs.spm_code.description - output_parameters = {} + + properties=['energy','energy_scf','energy_units','bandgap_spin1_au','bandgap_spin2_au'] try: - output_parameters = workchain.outputs.dft_output_parameters.get_dict() - except NotExistentAttributeError: - pass + all_output_parameters = workchain.outputs.dft_output_parameters.get_dict() + except: + all_output_parameters = workchain.outputs.output_parameters.get_dict() + output_parameters = {key: all_output_parameters[key] for key in properties if key in all_output_parameters} + step_info={key: values[-1] for key, values in all_output_parameters['motion_step_info'].items()} + output_parameters.update(step_info) + input_parameters = {} dft_object_parameters = get_dft_parameters_cp2k(spm_code, dft_params) diff --git a/Notebooks/connection_to_openbis/config.json b/Notebooks/connection_to_openbis/config.json index 66fe7a5c..a6cad897 100644 --- a/Notebooks/connection_to_openbis/config.json +++ b/Notebooks/connection_to_openbis/config.json @@ -1226,6 +1226,8 @@ ".fa-home {font-size: 2em !important; /* Increase icon size */}", ".fa-times {font-size: 2em !important; /* Increase icon size */}", ".fa-download {font-size: 2em !important; /* Increase icon size */}", + ".fa-arrow-right {font-size: 2em !important; /* Increase icon size */}", + ".fa-arrow-down {font-size: 2em !important; /* Increase icon size */}", "" ], "default_image_filepath": "images/white_screen.jpg" diff --git a/Notebooks/connection_to_openbis/create_analysis.ipynb b/Notebooks/connection_to_openbis/create_analysis.ipynb index adaf47c7..7ece8524 100644 --- a/Notebooks/connection_to_openbis/create_analysis.ipynb +++ b/Notebooks/connection_to_openbis/create_analysis.ipynb @@ -70,20 +70,20 @@ " analysis_props[prop] = analysis_widgets.properties_widgets[prop].value\n", " \n", " project_permid = project_selector.dropdown.value\n", - " project_results_collection = OPENBIS_SESSION.get_collections(project = project_permid, code = \"ANALYSIS_COLLECTION\")\n", + " project_analysis_collection = OPENBIS_SESSION.get_collections(project = project_permid, code = \"ANALYSIS_COLLECTION\")\n", " analysis_parents = []\n", " analysis_parents.extend(list(measurements_selector.selector.value))\n", " analysis_parents.extend(list(simulations_selector.selector.value))\n", " analysis_parents.extend(list(codes_selector.selector.value))\n", " \n", - " if len(project_results_collection) == 0:\n", + " if len(project_analysis_collection) == 0:\n", " analysis_collection = utils.create_openbis_collection(\n", " OPENBIS_SESSION, \n", " project = project_selector.dropdown.value, \n", - " code = \"ANALISYS_COLLECTION\", type = \"COLLECTION\", props = {\"$name\": \"Analysis\"}\n", + " code = \"ANALYSIS_COLLECTION\", type = \"COLLECTION\", props = {\"$name\": \"Analysis\"}\n", " )\n", " else:\n", - " analysis_collection = project_results_collection[0]\n", + " analysis_collection = project_analysis_collection[0]\n", "\n", " results_object = utils.create_openbis_object(\n", " OPENBIS_SESSION, type=\"ANALYSIS\", collection=analysis_collection, \n", diff --git a/Notebooks/connection_to_openbis/export_openbis_object.ipynb b/Notebooks/connection_to_openbis/export_openbis_object.ipynb index 75915ddd..55278a70 100644 --- a/Notebooks/connection_to_openbis/export_openbis_object.ipynb +++ b/Notebooks/connection_to_openbis/export_openbis_object.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -12,7 +12,8 @@ "import utils\n", "import os\n", "import base64\n", - "import json" + "import json\n", + "import google.generativeai as genai" ] }, { @@ -30,6 +31,28 @@ "publication_selector = widgets.PublicationSelectionWidget()\n", "publication_selector.load_dropdown_box()\n", "\n", + "prompt_textarea = utils.Textarea(\n", + " description = \"Prompt\", \n", + " layout = ipw.Layout(width = '980px', height = '300px'),\n", + " style = {\"description_width\": \"110px\"}\n", + ")\n", + "\n", + "model_answer_textarea = utils.Textarea(\n", + " description = \"Answer\", \n", + " layout = ipw.Layout(width = '980px', height = '300px'),\n", + " style = {\"description_width\": \"110px\"}\n", + ")\n", + "\n", + "enter_button = utils.Button(\n", + " description = '', disabled = False, button_style = '', \n", + " tooltip = 'Enter', icon = 'arrow-right', layout = ipw.Layout(width = '100px', height = '50px')\n", + ")\n", + "\n", + "load_model_button = utils.Button(\n", + " description = '', disabled = False, button_style = '', \n", + " tooltip = 'Load chatbot', icon = 'arrow-down', layout = ipw.Layout(width = '100px', height = '50px')\n", + ")\n", + "\n", "download_button = utils.Button(\n", " description = '', disabled = False, button_style = '', \n", " tooltip = 'Download', icon = 'download', layout = ipw.Layout(width = '100px', height = '50px')\n", @@ -40,8 +63,17 @@ " tooltip = 'Main menu', icon = 'home', layout = ipw.Layout(width = '100px', height = '50px')\n", ")\n", "\n", - "download_close_buttons_hbox = ipw.HBox([download_button, quit_button])\n", - "increase_buttons_size = utils.HTML(data = ''.join(CONFIG[\"save_home_buttons_settings\"]))" + "load_download_buttons_hbox = ipw.HBox([load_model_button, download_button])\n", + "\n", + "increase_buttons_size = utils.HTML(data = ''.join(CONFIG[\"save_home_buttons_settings\"]))\n", + "\n", + "# Google Gemini 1.5 Flash\n", + "google_api_key = utils.read_json(\"/home/jovyan/gemini_api.json\")\n", + "genai.configure(api_key=google_api_key[\"api_key\"])\n", + "model = genai.GenerativeModel(\"gemini-1.5-flash\") # gemini-1.5-flash\n", + "\n", + "# Start the chatbot\n", + "chat = model.start_chat()" ] }, { @@ -53,9 +85,16 @@ "def close_notebook(b):\n", " display(utils.Javascript(data = 'window.location.replace(\"home.ipynb\")'))\n", "\n", - "def download_openbis_object_metadata(b):\n", - " if publication_selector.dropdown_boxes.children[0].value == -1:\n", - " return\n", + "def ask_chatbot(change):\n", + " model_answer_textarea.value = \"\"\n", + " prompt = prompt_textarea.value\n", + " response = chat.send_message(prompt, stream = True)\n", + " for chunk in response:\n", + " model_answer_textarea.value = model_answer_textarea.value + chunk.text\n", + " \n", + "def get_openbis_object_metadata():\n", + " if publication_selector.dropdown.value == -1:\n", + " return None\n", " else:\n", " openbis_selected_object_permid = publication_selector.dropdown_boxes.children[0].value\n", " openbis_selected_object = OPENBIS_SESSION.get_sample(openbis_selected_object_permid)\n", @@ -64,12 +103,25 @@ " parent_child_relationships = openbis_selected_object.props.all()\n", " parent_child_relationships[\"perm_id\"] = openbis_selected_object_permid\n", " parent_child_relationships[\"type\"] = str(openbis_selected_object.type)\n", + " openbis_objects_history = {}\n", " selected_object_schema = utils.get_parent_child_relationships_nested(\n", - " OPENBIS_SESSION, openbis_selected_object, parent_child_relationships\n", + " OPENBIS_SESSION, openbis_selected_object, parent_child_relationships, openbis_objects_history\n", " )\n", - " \n", + "\n", + " return selected_object_schema\n", + "\n", + "def load_chatbot(b):\n", + " data_dict = get_openbis_object_metadata()\n", + " if data_dict:\n", + " json_data = json.dumps(data_dict, indent=4)\n", + " response = chat.send_message(f\"This is the graph of my study: {json_data}\")\n", + " display(utils.Javascript(data = \"alert('Chatbot is ready!')\"))\n", + "\n", + "def download_metadata(b):\n", + " data_dict = get_openbis_object_metadata()\n", + " if data_dict:\n", " # Export to JSON\n", - " json_data = json.dumps(selected_object_schema, indent=4).encode('utf-8')\n", + " json_data = json.dumps(data_dict, indent=4).encode('utf-8')\n", " json_data = base64.b64encode(json_data).decode('utf-8')\n", " \n", " display(Javascript(f\"\"\"\n", @@ -79,8 +131,7 @@ " document.body.appendChild(a);\n", " a.click();\n", " document.body.removeChild(a);\n", - " \"\"\"))\n", - " display(Markdown(f\"[You .]({notebook_link})\"))" + " \"\"\"))" ] }, { @@ -103,10 +154,16 @@ "metadata": {}, "outputs": [], "source": [ - "display(publication_selector)\n", - "display(download_close_buttons_hbox)\n", "display(increase_buttons_size)\n", - "download_button.on_click(download_openbis_object_metadata)\n", + "display(publication_selector)\n", + "display(load_download_buttons_hbox)\n", + "display(prompt_textarea)\n", + "display(enter_button)\n", + "display(model_answer_textarea)\n", + "display(quit_button)\n", + "load_model_button.on_click(load_chatbot)\n", + "download_button.on_click(download_metadata)\n", + "enter_button.on_click(ask_chatbot)\n", "quit_button.on_click(close_notebook)" ] } diff --git a/Notebooks/connection_to_openbis/find_simulations.ipynb b/Notebooks/connection_to_openbis/find_simulations.ipynb index bef7baba..b7e23f98 100644 --- a/Notebooks/connection_to_openbis/find_simulations.ipynb +++ b/Notebooks/connection_to_openbis/find_simulations.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -20,7 +20,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ diff --git a/Notebooks/connection_to_openbis/home.ipynb b/Notebooks/connection_to_openbis/home.ipynb index 50cab7c7..fa266541 100644 --- a/Notebooks/connection_to_openbis/home.ipynb +++ b/Notebooks/connection_to_openbis/home.ipynb @@ -73,7 +73,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, diff --git a/Notebooks/connection_to_openbis/sample_measurement.ipynb b/Notebooks/connection_to_openbis/sample_measurement.ipynb index 9ac99e67..94e35de4 100644 --- a/Notebooks/connection_to_openbis/sample_measurement.ipynb +++ b/Notebooks/connection_to_openbis/sample_measurement.ipynb @@ -143,18 +143,10 @@ "\n", "def upload_measurements_to_openbis(b):\n", " sample_object = OPENBIS_SESSION.get_object(sample_selector.dropdown_boxes.children[0].value)\n", - "\n", - " # Get measurements collection if available\n", - " measurements_collection = next(\n", - " (OPENBIS_SESSION.get_object(child_id).collection \n", - " for child_id in sample_object.children \n", - " if OPENBIS_SESSION.get_object(child_id).type in [\"1D_MEASUREMENT\", \"2D_MEASUREMENT\"]), \n", - " None\n", - " )\n", " \n", - " # Get sample project from parent\n", - " sample_project = next(\n", - " (OPENBIS_SESSION.get_object(parent_id).project \n", + " # Get sample experiment from parent\n", + " sample_experiment = next(\n", + " (OPENBIS_SESSION.get_object(parent_id).experiment\n", " for parent_id in sample_object.parents \n", " if OPENBIS_SESSION.get_object(parent_id).type in SAMPLE_PREPARATION_SAMPLE_TYPES), \n", " None\n", @@ -166,27 +158,17 @@ " correct_folder = all(f\".{filename.split('.')[-1]}\" in MEASUREMENT_FILE_EXTENSIONS for filename in data_files)\n", " \n", " if correct_folder:\n", - " if measurements_collection:\n", - " measurements_collection = f\"{sample_project.identifier}/{measurements_collection}\"\n", - " measurements_collection = OPENBIS_SESSION.get_collection(measurements_collection)\n", + " if not sample_experiment:\n", + " print(\"Sample does not belong to any project yet.\")\n", + " return\n", " else:\n", - " if not sample_project:\n", - " print(\"Sample does not belong to any project yet.\")\n", - " return\n", - " collection_props = {\"$name\": f\"Measurements from Sample {sample_object.props['$name']}\", \"$default_collection_view\": \"IMAGING_GALLERY_VIEW\"}\n", - " measurements_collection = utils.create_openbis_collection(\n", - " OPENBIS_SESSION,\n", - " code=f\"MEASUREMENTS_COLLECTION_{sample_object.code}\", \n", - " type=\"COLLECTION\", project=sample_project, \n", - " props=collection_props\n", + " nanonis_importer.upload_measurements_into_openbis(\n", + " SESSION_DATA['url'], data_folder, \n", + " sample_experiment.permId, sample_object.permId, \n", + " instrument_selector.dropdown.value\n", " )\n", - " \n", - " nanonis_importer.upload_measurements_into_openbis(\n", - " SESSION_DATA['url'], data_folder, \n", - " measurements_collection.permId, sample_object.permId, \n", - " instrument_selector.dropdown.value\n", - " )\n", - " display(utils.Javascript(data = \"alert('Upload successful!')\"))\n", + " display(utils.Javascript(data = \"alert('Upload successful!')\"))\n", + " \n", " else:\n", " output_message = f\"Folder contains unrecognised files. Please remove files with extensions other than: {', '.join(MEASUREMENT_FILE_EXTENSIONS)}.\"\n", " display(utils.Javascript(data = f\"alert({output_message})\"))\n" diff --git a/Notebooks/connection_to_openbis/structures/structure.cdxml b/Notebooks/connection_to_openbis/structures/structure.cdxml new file mode 100644 index 00000000..0b26e09e --- /dev/null +++ b/Notebooks/connection_to_openbis/structures/structure.cdxml @@ -0,0 +1,590 @@ + + + + + + + + + + + + + + + + + + + + + +BrBr \ No newline at end of file diff --git a/Notebooks/connection_to_openbis/structures/structure.png b/Notebooks/connection_to_openbis/structures/structure.png new file mode 100644 index 0000000000000000000000000000000000000000..94657db7eefc7687d07dcf3f6e514de5e8223c7a GIT binary patch literal 10099 zcmeHtRa92dx9>&_Qc)C138hmSq>+|xJ|vU|=@3bgE~SwaK^j3CDMgX)l92uYk?!V9 z{^#6rU+&v|J$nqrAz!S$)||hZJ48iE1|NqM2SE^gIoZc*2!g?K@dxV?{LdaG2@QhK z3(7qf*Laz-`o~>gW9azWhWT%CR};qGCpRy*U+$7vN>EXPdwOwHQ zU=~bgR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l>hoQhF-=H8FP0hGhL#>Ry|548g9T2;=eZ#w= z@v+lEA1tB(?Q?MS8aSZh1BV2Pb4OLuDxO&PDcjhvAUY`2T_Pei#@4=9bPs4~Xh42t zeEKw0pv|7wE;iJe-Kvcf?6Ko28*eSg8{lW=N|g9V2POOXl#ukA$c zgj=F$5_N2w3BU(TGssFO6-`hFVVdZCf_o&@MdFl4Y@LdE_tt%;Za1R)eBXwxCS5BDVeRi4iz j^ZoC`SN|VRoE?i+WL0>cn^9gx;F6P4di+_!$nU=Z24OZh literal 0 HcmV?d00001 diff --git a/Notebooks/connection_to_openbis/utils.py b/Notebooks/connection_to_openbis/utils.py index c6e21284..59905b6c 100644 --- a/Notebooks/connection_to_openbis/utils.py +++ b/Notebooks/connection_to_openbis/utils.py @@ -216,7 +216,15 @@ def get_next_experiment_code(openbis_session): def create_experiment_in_openbis(openbis_session, project_id, experiment_name): experiment_code = get_next_experiment_code(openbis_session) - experiment = openbis_session.new_experiment(code = experiment_code, type = "EXPERIMENT", project = project_id, props = {"$name": experiment_name}) + experiment = openbis_session.new_experiment( + code = experiment_code, + type = "EXPERIMENT", + project = project_id, + props = { + "$name": experiment_name, + "$default_collection_view": "IMAGING_GALLERY_VIEW" + } + ) experiment.save() def create_openbis_dataset(openbis_session, **kwargs): @@ -255,7 +263,7 @@ def is_nan(var): """Function to verify whether it is a NaN.""" return var != var -def get_parent_child_relationships_nested(openbis_session, selected_object, parent_child_relationships=None): +def get_parent_child_relationships_nested(openbis_session, selected_object, parent_child_relationships = None, object_history = None): """ Function to get all the parent-child relations together with the information about the objects from openBIS. This is a recursive function because the objects inside openBIS are like trees containing multiple @@ -276,6 +284,12 @@ def get_parent_child_relationships_nested(openbis_session, selected_object, pare """ if parent_child_relationships is None: parent_child_relationships = {} + + if object_history is None: + object_history = {} + + if selected_object.identifier not in object_history: + object_history[selected_object.identifier] = selected_object # Fetch parents of the current publication parents = selected_object.parents @@ -284,7 +298,11 @@ def get_parent_child_relationships_nested(openbis_session, selected_object, pare # Add current parents to the dictionary with child-parent relationships parent_child_relationships["has_part"] = [] for parent_id in parents: - parent = openbis_session.get_sample(parent_id) + if parent_id in object_history: + parent = object_history[parent_id] + else: + parent = openbis_session.get_sample(parent_id) + object_history[parent.identifier] = parent parent_props = parent.props.all() for prop in parent_props: if openbis_session.get_property_type(prop).dataType == "SAMPLE" and parent_props[prop] is not None: @@ -292,11 +310,11 @@ def get_parent_child_relationships_nested(openbis_session, selected_object, pare parent_props[prop] = {"perm_id": parent_props[prop]} parent_props[prop]["type"] = str(prop_object.type) parent_props[prop].update(prop_object.props.all()) - parent_props[prop] = get_parent_child_relationships_nested(openbis_session, prop_object, parent_props[prop]) + parent_props[prop] = get_parent_child_relationships_nested(openbis_session, prop_object, parent_props[prop], object_history) parent_props["perm_id"] = parent.permId parent_props["type"] = str(parent.type) - parent_props = get_parent_child_relationships_nested(openbis_session, parent, parent_props) + parent_props = get_parent_child_relationships_nested(openbis_session, parent, parent_props, object_history) parent_child_relationships["has_part"].append(parent_props) return parent_child_relationships \ No newline at end of file diff --git a/Notebooks/connection_to_openbis/widgets.py b/Notebooks/connection_to_openbis/widgets.py index 965301d9..c76594e6 100644 --- a/Notebooks/connection_to_openbis/widgets.py +++ b/Notebooks/connection_to_openbis/widgets.py @@ -1093,7 +1093,7 @@ def cancel_new_experiment_button_on_click(self, b): clear_output() def save_new_experiment_button_on_click(self, b): - utils.create_experiment_in_openbis(OPENBIS_SESSION, self.projects_dropdown_boxes.children[0].children[0].value, self.new_experiment_name_textbox.value) + utils.create_experiment_in_openbis(OPENBIS_SESSION, self.projects_dropdown_boxes.dropdown.value, self.new_experiment_name_textbox.value) self.load_dropdown_box() with self.add_experiment_output: clear_output()