Skip to content

Commit bef9dbe

Browse files
committed
Provenance using sidecar json field GeneratedBy
1 parent ca081c8 commit bef9dbe

File tree

10 files changed

+151
-151
lines changed

10 files changed

+151
-151
lines changed

examples/dcm2niix/README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ datalad install --recursive https://github.com/psychoinformatics-de/hirni-demo.g
1313
1414
## Experiment #1
1515

16-
The aim of the experiment is to describe the provenance records inside several files. Here we use sidecars and modality agnostic files inside the `prov/` directory, as follows:
16+
The aim of the experiment is to describe the provenance records inside several files.
17+
Here we use sidecars and modality agnostic files inside the `prov/` directory, as follows:
1718
```
1819
.
1920
├── prov
20-
│ ├── environments.prov.jsonld
21-
│ └── software.prov.jsonld
21+
│ ├── prov-dcm2niix_.prov.json
22+
│ ├── prov-dcm2niix_env.prov.json
23+
│ └── prov-dcm2niix_soft.prov.json
2224
└── sub_02
2325
├── ses_20130717141500
2426
│ └── anat
@@ -28,6 +30,16 @@ The aim of the experiment is to describe the provenance records inside several f
2830
└── sub-02_ses-20140425155335_task-oneback_run-1_bold.prov.jsonld
2931
```
3032

33+
We introduce the following BIDS suffixes that are currently not existing:
34+
* `soft`:
35+
* `env`:
36+
37+
We introduce the following BIDS entity that is currently not existing:
38+
* `prov`
39+
* Full name: Provenance traces
40+
* Format: `prov-<label>`
41+
* Definition: A grouping of provenance traces. Defining multiple provenance traces groups is appropriate when several processings have been performed on data.
42+
3143
* `sub-02_ses-20130717141500_T1w.prov.jsonld` and `sub-02_ses-20140425155335_task-oneback_run-1_bold.prov.jsonld` are sidecars defining provenance for the corresponding `.nii` files.
3244
* `environments.prov.jsonld` mutualises the declaration of software environments objects for lower level prov files
3345
* `software.prov.jsonld` mutualises the declaration of software pieces objects for lower level prov files
Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,59 @@
11
#!/usr/bin/python
22
# coding: utf-8
33

4-
""" Merge available prov JSON-LD files into one RDF graph """
4+
""" Merge available prov JSON files into one RDF graph """
55

66
import json
77
from pyld import jsonld
88

99
# List of available prov files
10-
prov_files = [
11-
'prov/software.prov.jsonld',
12-
'prov/environments.prov.jsonld',
13-
'sub_02/ses_20130717141500/anat/sub-02_ses-20130717141500_T1w.prov.jsonld',
14-
'sub_02/ses_20140425155335/func/sub-02_ses-20140425155335_task-oneback_run-1_bold.prov.jsonld'
10+
prov_soft_files = [
11+
'prov/prov-dcm2niix_ses-01_soft.prov.json',
12+
'prov/prov-dcm2niix_ses-02_soft.prov.json'
13+
]
14+
prov_env_files = [
15+
'prov/prov-dcm2niix_ses-01_env.prov.json',
16+
'prov/prov-dcm2niix_ses-02_env.prov.json'
17+
]
18+
sidecar_files = [
19+
'sub_02/ses_20130717141500/anat/sub-02_ses-20130717141500_T1w.json',
20+
'sub_02/ses_20140425155335/func/sub-02_ses-20140425155335_task-oneback_run-1_bold.json'
1521
]
1622

17-
# Generate RDF graph of each file using pyld
18-
rdf_graph = ''
19-
for prov_file in prov_files:
23+
# Base jsonld
24+
with open('prov/base.prov.jsonld', encoding = 'utf-8') as file:
25+
base_provenance = json.load(file)
26+
27+
# Parse Software
28+
for prov_file in prov_soft_files:
2029
with open(prov_file, encoding = 'utf-8') as file:
21-
rdf_graph += f'# {prov_file}\n'
22-
rdf_graph += jsonld.normalize(
23-
jsonld.flatten(json.load(file)), {'algorithm': 'URDNA2015', 'format': 'application/n-quads'})
24-
rdf_graph += '\n'
25-
26-
# Write RDF graph
27-
with open('prov/experiment_1/merged_provenance.ttl', 'w' , encoding = 'utf-8') as file:
28-
file.write(rdf_graph)
30+
data = json.load(file)
31+
for key, value in data.items():
32+
value['Id'] = key
33+
base_provenance['Records']['Software'].append(value)
34+
35+
# Parse Environments
36+
for prov_file in prov_env_files:
37+
with open(prov_file, encoding = 'utf-8') as file:
38+
data = json.load(file)
39+
for key, value in data.items():
40+
value['Id'] = key
41+
# /!\ Workaround: environments are added in the Entities list because
42+
# the Environments term is not defined in the BIDS Prov context yet
43+
base_provenance['Records']['Entities'].append(value)
44+
45+
# Parse Sidecar files
46+
for sidecar_file in sidecar_files:
47+
with open(sidecar_file, encoding = 'utf-8') as file:
48+
data = json.load(file)
49+
if 'GeneratedBy' in data:
50+
activity = data['GeneratedBy']
51+
base_provenance['Records']['Activities'].append(activity)
52+
base_provenance['Records']['Entities'].append({
53+
"Id": f"bids::{sidecar_file}",
54+
"GeneratedBy": activity["Id"]
55+
})
56+
57+
# Write jsonld
58+
with open('prov/experiment_1/merged_provenance.prov.jsonld', 'w', encoding = 'utf-8') as file:
59+
file.write(json.dumps(base_provenance, indent = 2))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"@context": "https://purl.org/nidash/bidsprov/context.json",
3+
"BIDSProvVersion": "0.0.1",
4+
"Records": {
5+
"Software": [],
6+
"Activities": [],
7+
"Entities": []
8+
}
9+
}

examples/dcm2niix/prov/environments.prov.jsonld

Lines changed: 0 additions & 13 deletions
This file was deleted.
-21.4 KB
Loading
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"@context": "https://purl.org/nidash/bidsprov/context.json",
3+
"BIDSProvVersion": "0.0.1",
4+
"Records": {
5+
"Software": [
6+
{
7+
"Label": "dcm2niix",
8+
"Version": "v1.0.20220720",
9+
"Id": "bids::prov/dcm2niix-v1.0.20220720"
10+
},
11+
{
12+
"Label": "fmriprep",
13+
"Version": "v8.0.5",
14+
"Id": "bids::prov/fmriprep-v8.0.5"
15+
},
16+
{
17+
"Label": "dcm2niix",
18+
"Version": "v1.0.20231025",
19+
"Id": "bids::prov/dcm2niix-v1.0.20231025"
20+
},
21+
{
22+
"Label": "fmriprep",
23+
"Version": "v8.0.5",
24+
"Id": "bids::prov/fmriprep-v8.0.5"
25+
}
26+
],
27+
"Activities": [
28+
{
29+
"Id": "urn:conversion-00f3a18f",
30+
"Type": "Activity",
31+
"Label": "Conversion",
32+
"Command": "dcm2niix -o . -f sub-%i/ses-%t/anat/sub-%i_ses-%t_T1w sourcedata/hirni-demo/acq1/dicoms/example-dicom-structural-master/dicoms",
33+
"AssociatedWith": "bids::prov/dcm2niix-v1.0.20220720",
34+
"Used": [
35+
"bids::prov/fedora-35",
36+
{
37+
"Id": "bids::sourcedata/hirni-demo/acq1/dicoms/example-dicom-structural-master/dicoms",
38+
"Type": "Entity",
39+
"Label": "Structural MRI DICOMs"
40+
}
41+
]
42+
},
43+
{
44+
"Id": "urn:conversion-5a66f5be",
45+
"Type": "Activity",
46+
"Label": "Conversion",
47+
"Command": "dcm2niix -o . -f sub-%i/ses-%t/func/sub-%i_ses-%t_task-oneback_run-1_bold sourcedata/hirni-demo/acq2/dicoms/example-dicom-functional-master/dicoms",
48+
"AssociatedWith": "bids::prov/dcm2niix-v1.0.20231025",
49+
"Used": [
50+
"bids::prov/fedora-36",
51+
{
52+
"Id": "bids::sourcedata/hirni-demo/acq2/dicoms/example-dicom-functional-master/dicoms",
53+
"Type": "Entity",
54+
"Label": "Functional MRI DICOMs"
55+
}
56+
]
57+
}
58+
],
59+
"Entities": [
60+
{
61+
"Label": "Fedora release 35 (Thirty Five)",
62+
"Id": "bids::prov/fedora-35"
63+
},
64+
{
65+
"Label": "Fedora release 36 (Thirty Six)",
66+
"OperatingSystem": "GNU/Linux 6.2.15-100.fc36.x86_64",
67+
"Id": "bids::prov/fedora-36"
68+
},
69+
{
70+
"Id": "bids::sub_02/ses_20130717141500/anat/sub-02_ses-20130717141500_T1w.json",
71+
"GeneratedBy": "urn:conversion-00f3a18f"
72+
},
73+
{
74+
"Id": "bids::sub_02/ses_20140425155335/func/sub-02_ses-20140425155335_task-oneback_run-1_bold.json",
75+
"GeneratedBy": "urn:conversion-5a66f5be"
76+
}
77+
]
78+
}
79+
}

examples/dcm2niix/prov/experiment_1/merged_provenance.ttl

Lines changed: 0 additions & 37 deletions
This file was deleted.

examples/dcm2niix/prov/software.prov.jsonld

Lines changed: 0 additions & 13 deletions
This file was deleted.

examples/dcm2niix/sub_02/ses_20130717141500/anat/sub-02_ses-20130717141500_T1w.prov.jsonld

Lines changed: 0 additions & 34 deletions
This file was deleted.

examples/dcm2niix/sub_02/ses_20140425155335/func/sub-02_ses-20140425155335_task-oneback_run-1_bold.prov.jsonld

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)