Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add openbis connection #587

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 30 additions & 1 deletion aiidalab_widgets_base/elns.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,39 @@ def _observe_node(self, _=None):
)
info = q.all(flat=True)[0]
except IndexError:
info = {}
structures, _ = self.get_all_structures_and_geoopts(self.node)
info = structures[-1].base.extras.all["eln"]

self.eln.set_sample_config(**info)

def get_all_structures_and_geoopts(self, node):
"""Get all atomistic models that led to the one used in the simulation"""
current_node = node
all_structures = []
all_geoopts = []

while current_node is not None:
if isinstance(current_node, orm.StructureData):
all_structures.append(current_node)
current_node = current_node.creator

elif isinstance(current_node, orm.CalcJobNode):
current_node = current_node.caller

elif isinstance(current_node, orm.CalcFunctionNode):
current_node = current_node.inputs.source_structure

elif isinstance(current_node, orm.WorkChainNode):
if "GeoOpt" in current_node.label:
all_geoopts.append(current_node)
current_node = current_node.inputs.structure
elif "ORBITALS" in current_node.label or "STM" in current_node.label:
current_node = current_node.inputs.structure
else:
current_node = current_node.caller

return all_structures, all_geoopts

def send_to_eln(self, _=None):
if self.eln and self.eln.is_connected:
self.message.value = f"\u29d7 Sending data to {self.eln.eln_instance}..."
Expand Down
7 changes: 7 additions & 0 deletions aiidalab_widgets_base/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
"parameter_name": "structure_uuid",
"description": "Optimize atomic positions and/or unit cell employing Quantum ESPRESSO. Quantum ESPRESSO is preferable for small structures with no cell dimensions larger than 15 Å. Additionally, you can choose to compute electronic properties of the material such as band structure and density of states.",
},
{
"name": "surfaces",
"calculation_type": "geo_opt",
"notebook": "submit_geometry_optimization.ipynb",
"parameter_name": "structure_uuid",
"description": "Optimize atomic positions and/or unit cell employing CP2K. CP2K is preferable for large structures with cell dimensions larger than 15 Å.",
},
{
"name": "aiidalab-lsmo",
"calculation_type": "geo_opt",
Expand Down
138 changes: 124 additions & 14 deletions notebooks/eln_import.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@
"from aiidalab_widgets_base import AiidaNodeViewWidget, OpenAiidaNodeInAppWidget, ElnImportWidget\n",
"import urllib.parse as urlparse\n",
"from aiidalab_widgets_base import viewer\n",
"from traitlets import dlink"
"from traitlets import dlink\n",
"\n",
"from jsonschema import validate, RefResolver, Draft7Validator, ValidationError\n",
"import json\n",
"from pyld import jsonld"
]
},
{
Expand All @@ -54,30 +58,134 @@
"id": "4",
"metadata": {},
"outputs": [],
"source": [
"def load_json(filepath: str) -> dict:\n",
" return json.load(open(filepath, \"r\"))\n",
"\n",
"def find_refs(obj, refs=None) -> list[str]:\n",
" if refs is None:\n",
" refs = []\n",
" \n",
" if isinstance(obj, dict):\n",
" for key, value in obj.items():\n",
" if key == '$ref':\n",
" refs.append(value[2:])\n",
" else:\n",
" find_refs(value, refs)\n",
" \n",
" return refs\n",
"\n",
"def find_ref_schemas(schemas_filenames: list[str], refs = None):\n",
" if refs is None:\n",
" refs = []\n",
" \n",
" for schema_filename in schemas_filenames:\n",
" schema = load_json(f\"/home/jovyan/aiida-openbis/Notebooks/New_Metadata_Schemas/{schema_filename}\")\n",
" ref_schemas_filenames = find_refs(schema)\n",
" \n",
" if schema_filename not in refs:\n",
" refs.append(schema_filename)\n",
" find_ref_schemas(ref_schemas_filenames, refs)\n",
" \n",
" return refs\n",
"\n",
"def set_up_validator(json_schema_filename):\n",
" schemas_filenames = [json_schema_filename]\n",
" all_schemas_filenames = find_ref_schemas(schemas_filenames)\n",
"\n",
" first_schema = load_json(f\"/home/jovyan/aiida-openbis/Notebooks/New_Metadata_Schemas/{all_schemas_filenames[0]}\")\n",
" resolver = RefResolver(base_uri=\"http://example.com/\", referrer = first_schema)\n",
"\n",
" for referenced_schema_filename in all_schemas_filenames:\n",
" referenced_schema = load_json(f\"/home/jovyan/aiida-openbis/Notebooks/New_Metadata_Schemas/{referenced_schema_filename}\")\n",
" resolver.store[referenced_schema[\"$id\"]] = referenced_schema\n",
"\n",
" validator = Draft7Validator(first_schema, resolver=resolver)\n",
" \n",
" return validator\n",
"\n",
"# Function to expand context terms\n",
"def expand_context(base_context):\n",
" expanded_context = {}\n",
" for key, value in base_context.items():\n",
" if isinstance(value, str) and ':' in value:\n",
" prefix, suffix = value.split(':', 1)\n",
" if prefix in base_context:\n",
" expanded_context[key] = base_context[prefix] + suffix\n",
" else:\n",
" expanded_context[key] = value\n",
" else:\n",
" expanded_context[key] = value\n",
" return expanded_context\n",
"\n",
"def replace_keys_recursive(data, old_key, new_key):\n",
" if isinstance(data, dict):\n",
" for key in list(data.keys()): # Create a copy of keys to avoid RuntimeError\n",
" if key == old_key:\n",
" key = new_key\n",
" data[new_key] = data.pop(old_key)\n",
" replace_keys_recursive(data[key], old_key, new_key)\n",
" elif isinstance(data, list):\n",
" for item in data:\n",
" replace_keys_recursive(item, old_key, new_key)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5",
"metadata": {},
"outputs": [],
"source": [
"url = urlparse.urlsplit(jupyter_notebook_url)\n",
"parsed_url = urlparse.parse_qs(url.query)\n",
"params = {key:value[0] for key, value in parsed_url.items()}\n",
"eln_widget = ElnImportWidget(**params)"
"\n",
"molecule_json = json.loads(params[\"molecule_info\"])\n",
"\n",
"molecule_validator = set_up_validator(\"molecule.schema.json\")\n",
"schema_context = molecule_validator.schema[\"@context\"]\n",
"schema_context = expand_context(schema_context)\n",
"json_context = expand_context(molecule_json[\"@context\"])\n",
"\n",
"if \"@context\" in molecule_json:\n",
" _ = molecule_json.pop(\"@context\")\n",
" \n",
"for key1, value1 in schema_context.items():\n",
" for key2, value2 in json_context.items():\n",
" if value1 == value2:\n",
" replace_keys_recursive(molecule_json, key2, key1)\n",
" \n",
"molecule_info_valid = False\n",
"\n",
"try:\n",
" molecule_validator.validate(instance = molecule_json)\n",
" molecule_info_valid = True\n",
" params[\"molecule_info\"] = json.dumps(molecule_json)\n",
" eln_widget = ElnImportWidget(path_to_root=\"../../\", **params)\n",
"except ValidationError as e:\n",
" message = e.schema[\"error_msg\"] if \"error_msg\" in e.schema else e.message\n",
" print(f'Invalid data: {message}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5",
"id": "6",
"metadata": {},
"outputs": [],
"source": [
"object_displayed = AiidaNodeViewWidget()\n",
"open_in_app = OpenAiidaNodeInAppWidget()\n",
"if molecule_info_valid:\n",
" object_displayed = AiidaNodeViewWidget()\n",
" open_in_app = OpenAiidaNodeInAppWidget(path_to_root=\"../../\")\n",
"\n",
"_ = dlink((eln_widget, 'node'), (object_displayed, 'node'))\n",
"_ = dlink((eln_widget, 'node'), (open_in_app, 'node'))"
" _ = dlink((eln_widget, 'node'), (object_displayed, 'node'))\n",
" _ = dlink((eln_widget, 'node'), (open_in_app, 'node'))"
]
},
{
"cell_type": "markdown",
"id": "6",
"id": "7",
"metadata": {},
"source": [
"## Selected object:"
Expand All @@ -86,17 +194,18 @@
{
"cell_type": "code",
"execution_count": null,
"id": "7",
"id": "8",
"metadata": {},
"outputs": [],
"source": [
"display(object_displayed)\n",
"display(eln_widget)"
"if molecule_info_valid:\n",
" display(object_displayed)\n",
" display(eln_widget)"
]
},
{
"cell_type": "markdown",
"id": "8",
"id": "9",
"metadata": {},
"source": [
"## What's next?"
Expand All @@ -105,11 +214,12 @@
{
"cell_type": "code",
"execution_count": null,
"id": "9",
"id": "10",
"metadata": {},
"outputs": [],
"source": [
"display(open_in_app)"
"if molecule_info_valid:\n",
" display(open_in_app)"
]
}
],
Expand Down