diff --git a/bids_prov/utils.py b/bids_prov/utils.py index dcd51ba03..3e57116a2 100644 --- a/bids_prov/utils.py +++ b/bids_prov/utils.py @@ -5,6 +5,7 @@ import shutil import uuid from typing import Mapping, Union, Tuple +import re CONTEXT_URL = "https://raw.githubusercontent.com/bids-standard/BEP028_BIDSprov/master/context.json" @@ -120,6 +121,44 @@ def compute_sha_256_entity(entities: dict): shutil.rmtree(directory) +def simplify_urns(graph: str) -> str: + """ + Replace URNs of a json-ld graph with simpler (not random) values defined by their order of appearance in the graph. + + Parameters + ---------- + graph : str + The json-ld graph dumped as a string + + Returns + ------- + str + The input string with simplified urns. + """ + + # Get all occurrences of ("urn:" + 36 following chars) + urn_occurrences = [i for i in re.finditer(r'urn:[a-zA-Z0-9\-]{36}', graph)] + + # Prepare a dict in which : + # - key is an urn as present in the original graph + # - value is an id corresponding to the order of appearance of the urn in the graph. + known_urns = dict() + counter = 0 + + # Assign one simpler id to each urn + for urn in urn_occurrences: + if urn.group() not in known_urns: + known_urns[urn.group()] = f"urn:{str(counter).zfill(36)}" + counter += 1 + + # Replace all urns in the input graph with their assigned id + output_graph = graph + for key, value in known_urns.items(): + output_graph = output_graph.replace(key, value) + + return output_graph + + def writing_jsonld(graph, indent, output_file): """ Write a json-ld in memory unless it already exists and contains the same content @@ -140,10 +179,12 @@ def writing_jsonld(graph, indent, output_file): """ if os.path.isfile(output_file): with open(output_file, "r") as f: - existing_content = f.read() + existing_content = simplify_urns(f.read()) + + new_content = simplify_urns(json.dumps(graph, indent=indent)) - if existing_content == json.dumps(graph, indent=indent): - return True + if existing_content == new_content: + return True with open(output_file, "w") as fd: json.dump(graph, fd, indent=indent) diff --git a/examples/from_parsers/context_2024_06_13_07h55m05s.txt b/examples/from_parsers/context_2024_06_13_07h55m05s.txt new file mode 100644 index 000000000..0c0d575b3 --- /dev/null +++ b/examples/from_parsers/context_2024_06_13_07h55m05s.txt @@ -0,0 +1,72 @@ +Date : 2024_06_13_07h55m05s +Processing files... + file= nidmresults-examples/afni_alt_onesided_proc.sub_001 + file= nidmresults-examples/afni_alt_onesided_proc.sub_001 + file= nidmresults-examples/afni_bi_sided_t_test_proc.sub_001 + file= nidmresults-examples/afni_bi_sided_t_test_proc.sub_001 + file= nidmresults-examples/afni_clustconn_18_proc.sub_001 + file= nidmresults-examples/afni_clustconn_18_proc.sub_001 + file= nidmresults-examples/afni_clustconn_26_proc.sub_001 + file= nidmresults-examples/afni_clustconn_26_proc.sub_001 + file= nidmresults-examples/afni_con_f_proc.sub_001 + file= nidmresults-examples/afni_con_f_proc.sub_001 + file= nidmresults-examples/afni_default_proc.sub_001 + file= nidmresults-examples/afni_default_proc.sub_001 + file= nidmresults-examples/afni_gam_proc.sub_001 + file= nidmresults-examples/afni_gam_proc.sub_001 + file= nidmresults-examples/afni_hrf_gammadiff_proc.sub_001 + file= nidmresults-examples/afni_hrf_gammadiff_proc.sub_001 + file= nidmresults-examples/afni_hrf_tent_proc.sub_001 + file= nidmresults-examples/afni_hrf_tent_proc.sub_001 + file= nidmresults-examples/afni_other_template_proc.sub_001 + file= nidmresults-examples/afni_other_template_proc.sub_001 + file= nidmresults-examples/afni_thr_clustfwep05_proc.sub_001 + file= nidmresults-examples/afni_thr_clustfwep05_proc.sub_001 + file= nidmresults-examples/afni_thr_clustunck10_proc.sub_001 + file= nidmresults-examples/afni_thr_clustunck10_proc.sub_001 + file= nidmresults-examples/afni_thr_voxelfdrp05_proc.sub_001 + file= nidmresults-examples/afni_thr_voxelfdrp05_proc.sub_001 + file= nidmresults-examples/afni_thr_voxelunct4_proc.sub_001 + file= nidmresults-examples/afni_thr_voxelunct4_proc.sub_001 + file= nidmresults-examples/fsl_con_f_multiple_report_log.html + file= nidmresults-examples/fsl_con_f_report_log.html + file= nidmresults-examples/fsl_contrast_mask_report_log.html + file= nidmresults-examples/fsl_default_report_log.html + file= nidmresults-examples/fsl_full_examples001_report_log.html + file= nidmresults-examples/fsl_gamma_basis_report_log.html + file= nidmresults-examples/fsl_gaussian_report_log.html + file= nidmresults-examples/fsl_group_btw_report_log.html + file= nidmresults-examples/fsl_group_ols_report_log.html + file= nidmresults-examples/fsl_group_wls_report_log.html + file= nidmresults-examples/fsl_hrf_fir_report_log.html + file= nidmresults-examples/fsl_hrf_gammadiff_report_log.html + file= nidmresults-examples/fsl_motion_reg_der_report_log.html + file= nidmresults-examples/fsl_motion_reg_report_log.html + file= nidmresults-examples/fsl_thr_clustfwep05_report_log.html + file= nidmresults-examples/fsl_thr_voxelfwep05_report_log.html + file= nidmresults-examples/narps_do_02_pc_with_sign0.tcsh + file= nidmresults-examples/narps_do_02_pc_with_sign0.tcsh + file= nidmresults-examples/narps_do_04_make_imgs_A.tcsh + file= nidmresults-examples/narps_do_04_make_imgs_A.tcsh + file= nidmresults-examples/narps_do_13_view_zoom.tcsh + file= nidmresults-examples/narps_do_13_view_zoom.tcsh + file= nidmresults-examples/spm_2_t_test_batch.m + file= nidmresults-examples/spm_HRF_informed_basis_batch.m + file= nidmresults-examples/spm_con_f_batch.m + file= nidmresults-examples/spm_conjunction_batch.m + file= nidmresults-examples/spm_contrast_mask_batch.m + file= nidmresults-examples/spm_covariate_batch.m + file= nidmresults-examples/spm_default_batch.m + file= nidmresults-examples/spm_explicit_mask_batch.m + file= nidmresults-examples/spm_group_ols_batch.m + file= nidmresults-examples/spm_group_wls_batch.m + file= nidmresults-examples/spm_hrf_fir_batch.m + file= nidmresults-examples/spm_non_sphericity_batch.m + file= nidmresults-examples/spm_partial_conjunction_batch.m + file= nidmresults-examples/spm_temporal_derivative_batch.m + file= nidmresults-examples/spm_thr_clustfwep05_batch.m + file= nidmresults-examples/spm_thr_clustunck10_batch.m + file= nidmresults-examples/spm_thr_voxelfdrp05_batch.m + file= nidmresults-examples/spm_thr_voxelfwep05_batch.m + file= nidmresults-examples/spm_thr_voxelunct4_batch.m +End of processed files. Results in dir : 'examples/from_parsers'. Time required: 0:00:02.365113