Skip to content

Commit e579e7b

Browse files
authored
Merge pull request #93 from pyiron/hashed_structure_h5
Hashed structure h5
2 parents 873c572 + b4053af commit e579e7b

File tree

4 files changed

+107
-15
lines changed

4 files changed

+107
-15
lines changed

pybis_aixtended/OpenbisAixTended.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -777,13 +777,15 @@ def download(
777777
filename = self.file_list[0].split("/")[-1]
778778
if destination is None:
779779
destination = "data/" + self.openbis.download_prefix
780-
if not os.path.exists(destination):
781-
os.makedirs(destination)
782780

783781
if create_default_folders:
784782
filename_dest = os.path.join(destination, self.permId, "original", filename)
785783
else:
786784
filename_dest = os.path.join(destination, filename)
785+
786+
destination = os.path.dirname(filename_dest)
787+
if not os.path.exists(destination):
788+
os.makedirs(destination)
787789
try:
788790
max_retries = 5
789791
retries = 0

pyiron_rdm/classic.py

Lines changed: 90 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import hashlib
2+
3+
14
def classic_structure(
25
pr, structure, structure_name, options, is_init_struct: bool, init_structure=None
36
):
@@ -8,6 +11,8 @@ def classic_structure(
811

912
hdf = FileHDFio(structure_path + structure_name + ".h5")
1013
structure.to_hdf(hdf)
14+
with open(hdf.file_name, "rb", buffering=0) as f:
15+
hdf5_hash = hashlib.file_digest(f, "md5").hexdigest()
1116

1217
from pyiron_rdm.concept_dict import (
1318
get_unit_cell_parameters,
@@ -31,6 +36,7 @@ def classic_structure(
3136
structure_path=structure_path,
3237
structure_parameters=struct_params,
3338
options=options,
39+
md5hash=hdf5_hash,
3440
)
3541

3642
return struct_cdict
@@ -144,7 +150,7 @@ def validate_upload_options(ot_module: str, options: dict):
144150

145151
importlib.import_module(ot_module).validate_options(**options)
146152

147-
if "materials" in options and not isinstance(materials, list):
153+
if "materials" in options and not isinstance(options["materials"], list):
148154
options["materials"] = [options["materials"]]
149155
if "pseudopotentials" in options and not isinstance(
150156
options["pseudopotentials"], list
@@ -260,17 +266,13 @@ def get_cdicts_to_validate(
260266
return cdicts_to_validate
261267

262268

263-
def upload_classic_pyiron(
269+
def create_concept_dicts(
264270
job,
265271
o,
266-
space: str,
267-
project: str,
268-
collection: str | None = None,
269272
export_env_file: bool = True,
270273
is_init_struct: bool = True,
271274
init_structure=None,
272275
options: dict | None = None,
273-
require_parents: bool = True,
274276
):
275277
# TODO should this return anything?
276278
if options is not None:
@@ -291,12 +293,6 @@ def upload_classic_pyiron(
291293

292294
export_env(pr.path + pr.name)
293295

294-
if collection is None:
295-
collection = pr.name
296-
space = space.upper()
297-
project = project.upper()
298-
collection = collection.upper()
299-
300296
# ------------------------------------VALIDATION----------------------------------------------
301297
is_sfb = get_datamodel(o) == "sfb1394"
302298

@@ -309,18 +305,50 @@ def upload_classic_pyiron(
309305
upload_final_struct=is_sfb,
310306
)
311307

312-
from pyiron_rdm.ob_upload import openbis_validate, validate_ob_destination
308+
return cdicts_to_validate
309+
310+
311+
def validate_openbis_destination(o, space: str, project: str, collection: str):
312+
from pyiron_rdm.ob_upload import validate_ob_destination
313+
314+
space = space.upper()
315+
project = project.upper()
316+
collection = collection.upper()
313317

314318
validate_ob_destination(o=o, space=space, project=project, collection=collection)
319+
return space, project, collection
320+
321+
322+
def validate_concept_dicts(
323+
cdicts_to_validate: list[dict],
324+
o,
325+
options: dict | None = None,
326+
require_parents: bool = True,
327+
):
328+
329+
from pyiron_rdm.ob_upload import openbis_validate
330+
331+
options = options if options is not None else {}
315332

316333
validated_to_upload = openbis_validate(
317334
o=o,
318335
concept_dicts=cdicts_to_validate,
319336
options=options,
320337
require_parents=require_parents,
321338
)
339+
340+
return validated_to_upload
322341
# ---------------------------------------------------------------------------------------------
323342

343+
344+
def upload_cdicts_to_openbis(
345+
validated_to_upload: list[dict],
346+
o,
347+
space: str,
348+
project: str,
349+
collection: str | None = None,
350+
):
351+
324352
# --------------------------------------UPLOAD-------------------------------------------------
325353
from pyiron_rdm.ob_upload import openbis_upload_validated
326354

@@ -332,6 +360,7 @@ def upload_classic_pyiron(
332360
**validated_to_upload["structure"],
333361
)
334362

363+
is_sfb = get_datamodel(o) == "sfb1394"
335364
if is_sfb:
336365
job_parents = None # job does not have init structure as parent
337366
str_parent = ob_structure_id # equil structure has init as parent
@@ -386,3 +415,51 @@ def upload_classic_pyiron(
386415
**validated_to_upload["final_structure"],
387416
parent_ids=[ob_structure_id, ob_job_id],
388417
)
418+
419+
420+
def upload_classic_pyiron(
421+
job,
422+
o,
423+
space: str,
424+
project: str,
425+
collection: str | None = None,
426+
export_env_file: bool = True,
427+
is_init_struct: bool = True,
428+
init_structure=None,
429+
options: dict | None = None,
430+
require_parents: bool = True,
431+
):
432+
# Create JSON-LD conceptual dictionaries for the job
433+
cdicts_to_validate = create_concept_dicts(
434+
job,
435+
o,
436+
export_env_file=export_env_file,
437+
is_init_struct=is_init_struct,
438+
init_structure=init_structure,
439+
options=options,
440+
)
441+
442+
# Set sensible default if collection is not given
443+
if collection is None:
444+
collection = job.pr.name
445+
446+
# Validate openbis destination
447+
space, project, collection = validate_openbis_destination(
448+
o, space=space, project=project, collection=collection
449+
)
450+
451+
# Validate conceptual dictionary compatibility to openBIS instance
452+
validated_to_upload = validate_concept_dicts(
453+
cdicts_to_validate=cdicts_to_validate,
454+
o=o,
455+
options=options,
456+
require_parents=require_parents,
457+
)
458+
459+
upload_cdicts_to_openbis(
460+
validated_to_upload=validated_to_upload,
461+
o=o,
462+
space=space,
463+
project=project,
464+
collection=collection,
465+
)

pyiron_rdm/concept_dict.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def process_structure_crystal(
5454
structure_path,
5555
structure_parameters: dict = None,
5656
options=None,
57+
md5hash=None,
5758
) -> dict:
5859
if options is None:
5960
options = {}
@@ -70,6 +71,8 @@ def process_structure_crystal(
7071
sample_dict["defects"] = options["defects"]
7172
if options.get("comments"):
7273
sample_dict["comments"] = options["comments"]
74+
if md5hash is not None:
75+
sample_dict["md5hash"] = md5hash
7376
json_file_name = structure_path + structure_name + "_concept_dict.json"
7477
with open(json_file_name, "w") as f:
7578
json.dump(sample_dict, f, indent=2)

pyiron_rdm/ob_upload.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ def openbis_validate(
8888
)
8989
object_name = concept_dict["job_details"][0]["value"]
9090

91+
ob_parents = [parent for parent in ob_parents if parent is not None]
92+
9193
outputs[key] = {
9294
"cdict": cdict,
9395
"props_dict": props_dict,
@@ -126,6 +128,14 @@ def openbis_upload_validated(
126128
obj.identifier for obj in ob_coll.get_objects() if obj.p["$name"] == object_name
127129
]
128130

131+
structure_type = "CRYS-STRUCT_DATA"
132+
if ds_types == structure_type:
133+
hashdict = {
134+
ds.MD5_HASH: ds.identifier for ds in o.get_datasets(type=structure_type)
135+
}
136+
if "md5hash" in props_dict and props_dict["md5hash"] in hashdict:
137+
return hashdict[props_dict["md5hash"]]
138+
129139
if found_objects_ids:
130140
print("===================\n")
131141
print(

0 commit comments

Comments
 (0)