Skip to content

Commit

Permalink
Safer URNs for all parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
bclenet committed Oct 3, 2024
1 parent 5b9d420 commit f740415
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
14 changes: 11 additions & 3 deletions bids_prov/afni/afni_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from bids_prov.fsl.fsl_parser import get_entities
from bids_prov.utils import (
get_default_graph, CONTEXT_URL, label_mapping, compute_sha_256_entity,
get_activity_urn, get_agent_urn, get_entity_urn,
get_activity_urn, get_agent_urn, get_entity_urn, make_alnum, get_uuid,
writing_jsonld
)

Expand Down Expand Up @@ -206,7 +206,11 @@ def build_records(commands_block: list, agent_id: str, verbose: bool = False):
}

for input_path in inputs:
input_id = get_entity_urn(input_path)
# Deal with not human readable paths
if not make_alnum(input_path):
input_id = 'urn:uuid:' + get_uuid()
else:
input_id = get_entity_urn(input_path)
existing_input = next(
(entity for entity in records["Entities"] if entity["AtLocation"] == input_path), None)

Expand All @@ -227,9 +231,13 @@ def build_records(commands_block: list, agent_id: str, verbose: bool = False):
activity["Used"] = sorted(set(activity["Used"]))

for output_path in outputs:
if not make_alnum(output_path):
output_id = 'urn:uuid:' + get_uuid()
else:
output_id = get_entity_urn(output_path)
records["Entities"].append(
{
"@id": get_entity_urn(output_path),
"@id": output_id,
"Label": os.path.split(output_path)[1],
"AtLocation": output_path,
"GeneratedBy": activity["@id"],
Expand Down
14 changes: 11 additions & 3 deletions bids_prov/fsl/fsl_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from bids_prov.utils import (
get_default_graph, CONTEXT_URL, label_mapping, compute_sha_256_entity,
get_activity_urn, get_agent_urn, get_entity_urn,
get_activity_urn, get_agent_urn, get_entity_urn, make_alnum, get_uuid,
writing_jsonld
)

Expand Down Expand Up @@ -502,7 +502,10 @@ def build_records(groups: Mapping[str, List[str]], agent_id: str, verbose: bool

for input_path in inputs:
# input_name = input_path.replace("/", "_") # TODO
input_id = get_entity_urn(input_path)
if not make_alnum(input_path):
input_id = 'urn:uuid:' + get_uuid()
else:
input_id = get_entity_urn(input_path)

existing_input = next(
(e for e in records["Entities"] if e["AtLocation"] == input_path), None)
Expand All @@ -522,9 +525,14 @@ def build_records(groups: Mapping[str, List[str]], agent_id: str, verbose: bool

for output_path in outputs:
# output_name = output_path.replace("/", "_") # TODO
if not make_alnum(output_path):
output_id = 'urn:uuid:' + get_uuid()
else:
output_id = get_entity_urn(output_path)

records["Entities"].append(
{
"@id": get_entity_urn(output_path),
"@id": output_id,
"Label": os.path.split(output_path)[1],
"AtLocation": output_path,
"GeneratedBy": activity["@id"],
Expand Down
5 changes: 2 additions & 3 deletions bids_prov/spm/spm_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,11 @@ def get_input_entity(right: str) -> List[dict]:
entity_label_short = "_".join(file_location.split("/")[-2:]) # Sub01_con_0001.nii
entity = {
"@id": get_entity_urn(
"/"+"/".join(file_location.split("/")[1:]),
file_location.split("/")[0]),
"/"+"/".join(file_location.strip("/").split("/")[1:]),
file_location.strip("/").split("/")[0]),
"Label": label_mapping(entity_label_short, "spm/spm_activity_labels.json"),
"AtLocation": file_location
}

entities.append(entity)

return entities
Expand Down

0 comments on commit f740415

Please sign in to comment.