diff --git a/bids_prov/afni/afni_parser.py b/bids_prov/afni/afni_parser.py index 554ce6949..a724fe0ad 100644 --- a/bids_prov/afni/afni_parser.py +++ b/bids_prov/afni/afni_parser.py @@ -193,45 +193,45 @@ def build_records(commands_bloc: list, agent_id: str, verbose: bool = False): label = f"{os.path.split(a_name)[1]}" activity = { - "@id": f"urn:{get_id()}", - "label": label_mapping(label, "afni/afni_labels.json"), - "associatedWith": "urn:" + agent_id, - "command": cmd, - "parameters": param_dic, - "used": list(), + "Id": f"urn:{get_id()}", + "Label": label_mapping(label, "afni/afni_labels.json"), + "AssociatedWith": "urn:" + agent_id, + "Command": cmd, + "Parameters": param_dic, + "Used": list(), } for input_path in inputs: input_id = f"urn:{get_id()}" # def format_id existing_input = next( - (entity for entity in records["prov:Entity"] if entity["prov:atLocation"] == input_path), None) + (entity for entity in records["Entities"] if entity["AtLocation"] == input_path), None) if existing_input is None: new_label = os.path.split(input_path)[1] new_label_rename = clean_label_suffix(new_label) ent = { - "@id": input_id, - "label": new_label_rename, - "prov:atLocation": input_path, + "Id": input_id, + "Label": new_label_rename, + "AtLocation": input_path, } - records["prov:Entity"].append(ent) - activity["used"].append(input_id) + records["Entities"].append(ent) + activity["Used"].append(input_id) else: - activity["used"].append(existing_input["@id"]) + activity["Used"].append(existing_input["Id"]) for output_path in outputs: records["prov:Entity"].append( { - "@id": f"urn:{get_id()}", - "label": os.path.split(output_path)[1], - "prov:atLocation": output_path, - "generatedBy": activity["@id"], + "Id": f"urn:{get_id()}", + "Label": os.path.split(output_path)[1], + "AtLocation": output_path, + "GeneratedBy": activity["Id"], # "derivedFrom": input_id, } ) - bloc_act.append((bloc, activity["@id"])) + bloc_act.append((bloc, activity["Id"])) - records["prov:Activity"].append(activity) + records["Activities"].append(activity) if verbose: print('-------------------------') @@ -328,8 +328,8 @@ def get_activities_by_ids(graph, ids): list of activities """ activities = [] - for activity in graph["records"]["prov:Activity"]: - if activity["@id"] in ids: + for activity in graph["Records"]["Activities"]: + if activity["Id"] in ids: activities.append(activity) return activities @@ -356,15 +356,15 @@ def fusion_activities(activities, label): command = "" for activity in activities: - used_entities.extend(activity["used"]) - command += activity["command"] + "; " + used_entities.extend(activity["Used"]) + command += activity["Command"] + "; " return { - "@id": f"urn:{get_id()}", - "label": label, - "associatedWith": activities[0]["associatedWith"], - "command": command, - "used": used_entities, + "Id": f"urn:{get_id()}", + "Label": label, + "AssociatedWith": activities[0]["associatedWith"], + "Command": command, + "Used": used_entities, } @@ -389,25 +389,25 @@ def get_extern_entities_from_activities(graph, activities, id_fusion_activity): List extern entities """ if len(activities) > 0: - activities_ids = [act["@id"] for act in activities] + activities_ids = [act["Id"] for act in activities] used_ents_ids = [] for act in activities: - used_ents_ids.extend(act["used"]) + used_ents_ids.extend(act["Used"]) used_ents_ids = set(used_ents_ids) used_ents = [] generated_entities = [] - for ent in graph["records"]["prov:Entity"]: - if ent["@id"] in used_ents_ids: - if "generatedBy" in ent: - if ent["generatedBy"] not in activities_ids: + for ent in graph["Records"]["Entities"]: + if ent["Id"] in used_ents_ids: + if "GeneratedBy" in ent: + if ent["GeneratedBy"] not in activities_ids: used_ents.append(ent) else: used_ents.append(ent) - if "generatedBy" in ent: - if ent["generatedBy"] in activities_ids: - if ent["@id"] not in used_ents_ids: + if "GeneratedBy" in ent: + if ent["GeneratedBy"] in activities_ids: + if ent["Id"] not in used_ents_ids: generated_entities.append(ent) # for ent in used_ents: @@ -415,8 +415,8 @@ def get_extern_entities_from_activities(graph, activities, id_fusion_activity): # ent["generatedBy"] = id_fusion_activity for ent in generated_entities: - if "generatedBy" in ent: - ent["generatedBy"] = id_fusion_activity + if "GeneratedBy" in ent: + ent["GeneratedBy"] = id_fusion_activity return used_ents + generated_entities @@ -454,8 +454,8 @@ def afni_to_bids_prov(filename: str, context_url=CONTEXT_URL, output_file=None, graph, agent_id = get_default_graph(label="AFNI", context_url=context_url, soft_ver=soft_ver) records, bloc_act = build_records(commands_bloc, agent_id, verbose=verbose) - graph["records"].update(records) - compute_sha_256_entity(graph["records"]["prov:Entity"]) + graph["Records"].update(records) + compute_sha_256_entity(graph["Records"]["Entities"]) if with_blocs: bl_name = list(OrderedDict.fromkeys(bl for (bl, id) in bloc_act)) @@ -470,18 +470,18 @@ def afni_to_bids_prov(filename: str, context_url=CONTEXT_URL, output_file=None, activities = get_activities_by_ids(graph_bloc, bloc["act_ids"]) fus_activities = fusion_activities(activities, bloc["bloc_name"]) ext_entities = get_extern_entities_from_activities( - graph_bloc, activities, fus_activities["@id"]) + graph_bloc, activities, fus_activities["Id"]) for ent in ext_entities: - if ent["@id"] not in entities_blocs: + if ent["Id"] not in entities_blocs: entities_blocs.append(ent) - for ent_used in fus_activities["used"]: - if ent_used not in [id_["@id"] for id_ in ext_entities]: - fus_activities["used"].remove(ent_used) + for ent_used in fus_activities["Used"]: + if ent_used not in [id_["Id"] for id_ in ext_entities]: + fus_activities["Used"].remove(ent_used) activities_blocs.append(fus_activities) - graph_bloc["records"]["prov:Activity"] = activities_blocs - graph_bloc["records"]["prov:Entity"] = entities_blocs + graph_bloc["Records"]["Activities"] = activities_blocs + graph_bloc["Records"]["Entities"] = entities_blocs return writing_jsonld(graph_bloc, indent, output_file) diff --git a/bids_prov/fsl/fsl_parser.py b/bids_prov/fsl/fsl_parser.py index 6740df940..bb725e4dd 100644 --- a/bids_prov/fsl/fsl_parser.py +++ b/bids_prov/fsl/fsl_parser.py @@ -332,17 +332,17 @@ def get_entities(cmd_s, parameters): # print("\n\n cmd_s change", cmd_s) - if "used" in parameters: + if "Used" in parameters: add_argument_list, inputs_kwarg = _get_kwarg( - parameters["used"]) + parameters["Used"]) for kwarg in add_argument_list: arg = kwarg.pop("arg") parser.add_argument(arg, **kwarg) - if "generatedBy" in parameters: + if "GeneratedBy" in parameters: add_argument_list, outputs_kwarg = _get_kwarg( - parameters["generatedBy"]) + parameters["GeneratedBy"]) for kwarg in add_argument_list: arg = kwarg.pop("arg") parser.add_argument(arg, **kwarg) @@ -382,11 +382,11 @@ def get_entities(cmd_s, parameters): params = _get_entities_from_kwarg(params, opts, parameters_value) params = _get_entities_from_kwarg(params, opts, parameters_no_value) - if "used" in parameters: - inputs.extend(_get_arg(parameters["used"], arg_rest)) + if "Used" in parameters: + inputs.extend(_get_arg(parameters["Used"], arg_rest)) - if "generatedBy" in parameters: - outputs.extend(_get_arg(parameters["generatedBy"], arg_rest)) + if "GeneratedBy" in parameters: + outputs.extend(_get_arg(parameters["GeneratedBy"], arg_rest)) # print("\n\n inputs", inputs) # print("\n\n outputs", outputs) @@ -475,14 +475,14 @@ def build_records(groups: Mapping[str, List[str]], agent_id: str): label = f"{os.path.split(a_name)[1]}" a = { - "@id": f"urn:{get_id()}", - "label": label_mapping(label, "fsl/fsl_labels.json"), - "associatedWith": "urn:" + agent_id, - "command": cmd, + "Id": f"urn:{get_id()}", + "Label": label_mapping(label, "fsl/fsl_labels.json"), + "AssociatedWith": "urn:" + agent_id, + "Command": cmd, # "attributes": [ # {k: v if len(v) > 1 else v[0]} for k, v in attributes.items() # ], - "used": list(), + "Used": list(), } for input_path in inputs: @@ -490,31 +490,31 @@ def build_records(groups: Mapping[str, List[str]], agent_id: str): input_id = f"urn:{get_id()}" # def format_id existing_input = next( - (entity for entity in records["prov:Entity"] if entity["prov:atLocation"] == input_path), None) + (entity for entity in records["Entities"] if entity["AtLocation"] == input_path), None) if existing_input is None: e = { - "@id": input_id, - "label": os.path.split(input_path)[1], - "prov:atLocation": input_path, + "Id": input_id, + "Label": os.path.split(input_path)[1], + "AtLocation": input_path, } - records["prov:Entity"].append(e) - a["used"].append(input_id) + records["Entities"].append(e) + a["Used"].append(input_id) else: - a["used"].append(existing_input["@id"]) + a["Used"].append(existing_input["Id"]) for output_path in outputs: # output_name = output_path.replace("/", "_") # TODO - records["prov:Entity"].append( + records["Entities"].append( { - "@id": f"urn:{get_id()}", - "label": os.path.split(output_path)[1], - "prov:atLocation": output_path, - "generatedBy": a["@id"], + "Id": f"urn:{get_id()}", + "Label": os.path.split(output_path)[1], + "AtLocation": output_path, + "GeneratedBy": a["Id"], # "derivedFrom": input_id, } ) - records["prov:Activity"].append(a) + records["Activities"].append(a) return dict(records) diff --git a/bids_prov/spm/spm_parser.py b/bids_prov/spm/spm_parser.py index 81bb8a3d7..85010aca0 100644 --- a/bids_prov/spm/spm_parser.py +++ b/bids_prov/spm/spm_parser.py @@ -170,10 +170,10 @@ def get_entities_from_ext_config(conf_dic: dict, activity_name: str, activity_id # activity_id), None) for output in conf_dic[activity]['outputs']: name = conf_dic[activity]['name'] - entity = {"@id": "urn:" + get_id(), - "label": label_mapping(name, "spm/spm_activity_labels.json"), - "prov:atLocation": output, - "generatedBy": activity_id, + entity = {"Id": "urn:" + get_id(), + "Label": label_mapping(name, "spm/spm_activity_labels.json"), + "Atlocation": output, + "GeneratedBy": activity_id, } output_entities.append(entity) # stop for loop at first match in if statement (match activity in conf_dic) @@ -219,10 +219,10 @@ def find_output_id_from_closest(closest_activity: dict, records: dict) -> str: ------- output_id : entity id, if such one has been generated by the closest activity, else new id """ - for entity in records["prov:Entity"]: - if "generatedBy" in entity: - if entity["generatedBy"] == closest_activity["@id"]: # entity["label"] == parts[-1] - output_id = entity["@id"] + for entity in records["Entities"]: + if "GeneratedBy" in entity: + if entity["GeneratedBy"] == closest_activity["Id"]: # entity["label"] == parts[-1] + output_id = entity["Id"] break else: output_id = "urn:" + get_id() @@ -274,7 +274,7 @@ def find_entity_from_id(idx, entities): """ input_entity = None # next((entity for entity in entities ), None) for entity in entities: - if entity["@id"] == idx: + if entity["Id"] == idx: input_entity = entity break @@ -310,11 +310,11 @@ def get_records(task_groups: dict, agent_id: str, verbose=False) -> dict: command += '\n'.join([command_prefix + c for c in end_line_list]) activity_id = "urn:" + get_id() - activity = {"@id": activity_id, - "label": format_activity_name(common_prefix_act), - "used": list(), - "associatedWith": "urn:" + agent_id, - "command": command + activity = {"Id": activity_id, + "Label": format_activity_name(common_prefix_act), + "Used": list(), + "AssociatedWith": "urn:" + agent_id, + "Command": command } output_entities, input_entities, params = list(), list(), {} @@ -341,32 +341,32 @@ def get_records(task_groups: dict, agent_id: str, verbose=False) -> dict: # to a function, the common part will be full and so left will be empty if dependency is not None: name_cfg_dep = str(dependency.group(1)) - closest_activity = find_closest_activity(records["prov:Activity"], right) + closest_activity = find_closest_activity(records["Activities"], right) output_id = find_output_id_from_closest(closest_activity, records) - activity["used"].append(output_id) + activity["Used"].append(output_id) # adds to the current activity the fact that it has used the previous entity # if output_ext_entities and add_ext_entity == 0: for idx, entity in enumerate(output_ext_entities): - input_ent = find_entity_from_id(output_id, records["prov:Entity"]) - entity['label'] = f"{entity['label']}{str(idx + 1)}" - entity['prov:atLocation'] = f"c{str(idx + 1)}{input_ent['label']}" + input_ent = find_entity_from_id(output_id, records["Entities"]) + entity['Label'] = f"{entity['Label']}{str(idx + 1)}" + entity['AtLocation'] = f"c{str(idx + 1)}{input_ent['Label']}" add_ext_entity = 1 # TO DO whatif multiple - if closest_activity["used"]: - id_closest = closest_activity["used"][0] - input_entity = find_entity_from_id(id_closest, records["prov:Entity"]) - filename = input_entity['label'] + if closest_activity["Used"]: + id_closest = closest_activity["Used"][0] + input_entity = find_entity_from_id(id_closest, records["Entities"]) + filename = input_entity['Label'] else: filename = '' # for activity that creates SPM.mat out_label = mapping_label_entity(name_cfg_dep, filename) output_entity = { # output for closest activity but input for current one - "@id": output_id, - "label": out_label, # label_mapping(parts[-1], "spm/spm_labels.json"), + "Id": output_id, + "Label": out_label, # label_mapping(parts[-1], "spm/spm_labels.json"), # "prov:atLocation": TODO - "generatedBy": closest_activity["@id"], + "GeneratedBy": closest_activity["Id"], } output_entities.append(output_entity) @@ -382,20 +382,20 @@ def get_records(task_groups: dict, agent_id: str, verbose=False) -> dict: params[param_name] = param_value # example : [4 2] becomes [4, 2] if params: - activity["parameters"] = params + activity["Parameters"] = params if input_entities: - used_entities = [entity["@id"] for entity in input_entities] - activity["used"].extend(used_entities) # we add entities from input_entities + used_entities = [entity["Id"] for entity in input_entities] + activity["Used"].extend(used_entities) # we add entities from input_entities entities = input_entities + output_entities for entity in entities: - if entity["@id"] not in entities_ids: - records["prov:Entity"].append(entity) # ADD entity if not present - entities_ids.add(entity["@id"]) + if entity["Id"] not in entities_ids: + records["Entities"].append(entity) # ADD entity if not present + entities_ids.add(entity["Id"]) - records["prov:Activity"].append(activity) # ADD activity + records["Activities"].append(activity) # ADD activity return records @@ -428,13 +428,13 @@ def spm_to_bids_prov(filename: str, context_url=CONTEXT_URL, output_file=None, s lines = readlines(filename) tasks = group_lines(lines) # same as list(lines) to expand generator records = get_records(tasks, agent_id, verbose=verbose) - graph["records"].update(records) + graph["Records"].update(records) # Post-processing - for activity in records["prov:Activity"]: # Remove each activity number from the activity labels - activity["label"] = re.sub(r'._\d+$', '', activity["label"]) + for activity in records["Activities"]: # Remove each activity number from the activity labels + activity["Label"] = re.sub(r'._\d+$', '', activity["Label"]) - compute_sha_256_entity(records["prov:Entity"]) + compute_sha_256_entity(records["Entities"]) if output_file is None: output_file = os.path.splitext(filename)[0] + '.jsonld' # replace extension .m by .jsonld