44import json
55import uuid
66import zipfile
7- from importlib import resources
87from pathlib import Path
98from typing import Any , TypedDict
109
11- from rdflib import Graph
1210from rocrate .rocrate import ROCrate
13- from provenance import ProvenanceAnalyzer
1411import semantic_benchmark
1512
1613DEFAULT_BENCHMARK_FILE = (
1714 "/Users/mahdi/Documents/GitHub/NFDI4IngModelValidationPlatform/examples/"
1815 "linear-elastic-plate-with-hole/benchmark.json"
1916)
2017DEFAULT_SIMULATION_RESULT_PATH = (
21- "/Users/mahdi/Downloads/fenics_provenance_linear -elastic-plate-with-hole"
18+ "/Users/mahdi/Downloads/kratos_provenance_linear -elastic-plate-with-hole"
2219)
20+ DEFAULT_ROCRATE_FILENAME = "RoCrate.zip"
21+ DEFAULT_SOFTWARE_NAME = "FENICS"
2322M4I_HAS_KIND_OF_QUANTITY = "http://w3id.org/nfdi4ing/metadata4ing#hasKindOfQuantity"
2423ROCRATE_CONFORMS_TO = [
2524 {"@id" : "https://w3id.org/ro/crate/1.1" },
@@ -423,20 +422,11 @@ def _add_profile_creative_works(crate: ROCrate) -> None:
423422 crate .add_jsonld (creative_work )
424423
425424
426- def unzip_rocrate (ro_zip_path : str = "RO.zip" , extract_dir : str = "RO" ) -> Path :
427- zip_path = Path (ro_zip_path )
428- if not zip_path .exists ():
429- raise FileNotFoundError (f"RO-Crate zip not found: { zip_path } " )
430-
431- output_dir = Path (extract_dir )
432- output_dir .mkdir (parents = True , exist_ok = True )
433- with zipfile .ZipFile (zip_path , "r" ) as archive :
434- archive .extractall (output_dir )
435- return output_dir
436-
437-
438425def create_main_ro (
439- path : str , benchmark_object : semantic_benchmark .SemanticBenchmark
426+ path : str ,
427+ benchmark_object : semantic_benchmark .SemanticBenchmark ,
428+ rocrate_filename : str = DEFAULT_ROCRATE_FILENAME ,
429+ software_name : str = DEFAULT_SOFTWARE_NAME ,
440430) -> None :
441431 crate = ROCrate (version = "1.1" )
442432 input_path = Path (path )
@@ -475,7 +465,7 @@ def create_main_ro(
475465 _configure_crate_metadata (crate , snakemake_id )
476466
477467 crate .add_jsonld (
478- {"@id" : software_id , "@type" : "SoftwareApplication" , "name" : "FENICS" }
468+ {"@id" : software_id , "@type" : "SoftwareApplication" , "name" : software_name }
479469 )
480470 _add_profile_creative_works (crate )
481471
@@ -484,53 +474,12 @@ def create_main_ro(
484474 lang = "snakemake" ,
485475 properties = {"hasPart" : {"@id" : software_id }},
486476 )
487- crate .write_zip ("RoCrate.Zip" )
488-
489-
490- def run_sparql_query (graph : Graph ) -> None :
491- query = """
492- PREFIX schema: <http://schema.org/>
493-
494- SELECT ?elementSize ?maxVonMisesStressNodes ?l2ErrorDisplacement
495- WHERE {
496- ?runAction a schema:CreateAction ;
497- schema:object ?object ;
498- schema:result ?metric1 ;
499- schema:result ?metric2 .
500-
501- ?object a schema:PropertyValue ;
502- schema:exampleOfWork ?param .
503-
504- ?param a <https://bioschemas.org/FormalParameter> ;
505- schema:name "element-size" ;
506- schema:defaultValue ?elementSize .
507-
508- ?metric1 a schema:PropertyValue ;
509- schema:name "max_von_mises_stress_nodes" ;
510- schema:defaultValue ?maxVonMisesStressNodes .
511-
512- ?metric2 a schema:PropertyValue ;
513- schema:name "l2_error_displacement" ;
514- schema:defaultValue ?l2ErrorDisplacement .
515- }
516- ORDER BY ?elementSize
517- """
518- results = graph .query (query )
519- variables = [str (var ) for var in results .vars ]
520- if variables :
521- print (" | " .join (variables ))
522- print ("-" * max (3 , len (" | " .join (variables ))))
523- row_count = 0
524- for row in results :
525- values = [str (value ) if value is not None else "" for value in row ]
526- print (" | " .join (values ))
527- row_count += 1
528- print (f"\n Rows: { row_count } " )
477+ crate .write_zip (rocrate_filename )
529478
530479
531- def main () -> None :
480+ def parse_args () -> argparse . Namespace :
532481 parser = argparse .ArgumentParser (
533- description = "Create RO-Crate and run SPARQL queries on ro-crate-metadata.json "
482+ description = "Create a benchmark provenance RO-Crate from simulation results. "
534483 )
535484 parser .add_argument (
536485 "--benchmark-file" ,
@@ -540,30 +489,31 @@ def main() -> None:
540489 parser .add_argument (
541490 "--simulation-result-path" ,
542491 default = DEFAULT_SIMULATION_RESULT_PATH ,
543- help = "Path containing run folders and SubCrate. zip files" ,
492+ help = "Path containing simulation result subfolders with RoCrate zip files" ,
544493 )
545494 parser .add_argument (
546- "--ro-zip" , default = "RoCrate.Zip" , help = "Path to RO-Crate zip file"
495+ "--rocrate-filename" ,
496+ default = DEFAULT_ROCRATE_FILENAME ,
497+ help = "Filename for the generated RO-Crate zip file" ,
547498 )
548499 parser .add_argument (
549- "--extract-dir " ,
550- default = "RO" ,
551- help = "Directory where RoCrate.Zip should be extracted " ,
500+ "--software-name " ,
501+ default = DEFAULT_SOFTWARE_NAME ,
502+ help = "Name of the software application recorded in the generated RO-Crate " ,
552503 )
553- args = parser .parse_args ()
554504
555- benchmark_object = semantic_benchmark .BenchmarkLoader (args .benchmark_file ).load ()
556- create_main_ro (args .simulation_result_path , benchmark_object )
505+ return parser .parse_args ()
557506
558- extracted_dir = unzip_rocrate (args .ro_zip , args .extract_dir )
559507
560- proveance = ProvenanceAnalyzer (provenance_folderpath = extracted_dir )
561- query = proveance .build_dynamic_rocrate_query (
562- parameters = ["element-size" ],
563- metrics = ["max_von_mises_stress_nodes" , "max_von_mises_stress_gauss_points" ],
508+ def main () -> None :
509+ args = parse_args ()
510+ benchmark_object = semantic_benchmark .BenchmarkLoader (args .benchmark_file ).load ()
511+ create_main_ro (
512+ args .simulation_result_path ,
513+ benchmark_object ,
514+ rocrate_filename = args .rocrate_filename ,
515+ software_name = args .software_name ,
564516 )
565- proveance .plot_provenance_graph ()
566- print (query )
567517
568518
569519if __name__ == "__main__" :
0 commit comments