Skip to content

Commit df52e82

Browse files
authored
Merge pull request #94 from apdavison/ancpBIDS
Replace pybids with ancpBIDS
2 parents 56aa083 + 8b4310c commit df52e82

4 files changed

Lines changed: 58 additions & 7 deletions

File tree

bids2openminds/converter.py

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,62 @@
11
import warnings
2-
from bids import BIDSLayout, BIDSValidator
3-
from openminds import Collection
42
import os
3+
import pandas as pd
4+
from ancpbids import BIDSLayout
5+
from ancpbids.query import Artifact
6+
from ancpbids.model_base import DatatypeFolder
7+
from openminds import Collection
58
import click
69
from . import main
710
from . import utility
811
from . import report
912

13+
_ENTITY_RENAMES = {"sub": "subject", "ses": "session"}
14+
15+
# Root-level BIDS files that ancpBIDS does not expose as Artifacts
16+
_ROOT_BIDS_FILES = [
17+
("dataset_description.json", "description", ".json"),
18+
("participants.tsv", "participants", ".tsv"),
19+
("participants.json", "participants", ".json"),
20+
("CHANGES", None, None),
21+
("README", None, None),
22+
("README.md", None, None),
23+
]
24+
25+
26+
def layout_to_df(layout):
27+
dataset = layout.get_dataset()
28+
rows = []
29+
30+
for obj in layout.get(return_type='object', scope='raw'):
31+
if not isinstance(obj, Artifact):
32+
continue
33+
parent = obj.get_parent()
34+
datatype = parent.name if isinstance(parent, DatatypeFolder) else None
35+
row = {
36+
"path": obj.get_absolute_path(),
37+
"suffix": obj.suffix,
38+
"datatype": datatype,
39+
"extension": obj.extension,
40+
}
41+
for entity in obj.entities:
42+
key = _ENTITY_RENAMES.get(entity.key, entity.key)
43+
row[key] = entity.value
44+
rows.append(row)
45+
46+
base_dir = os.path.abspath(dataset.base_dir_)
47+
for fname, suffix, extension in _ROOT_BIDS_FILES:
48+
path = os.path.join(base_dir, fname)
49+
if not os.path.exists(path):
50+
continue
51+
row = {"path": path, "datatype": None}
52+
if suffix is not None:
53+
row["suffix"] = suffix
54+
if extension is not None:
55+
row["extension"] = extension
56+
rows.append(row)
57+
58+
return pd.DataFrame(rows)
59+
1060

1161
def convert(input_path, save_output=False, output_path=None, multiple_files=False, include_empty_properties=False, quiet=False):
1262
if not (os.path.isdir(input_path)):
@@ -23,7 +73,7 @@ def convert(input_path, save_output=False, output_path=None, multiple_files=Fal
2373
collection = Collection()
2474
bids_layout = BIDSLayout(input_path)
2575

26-
layout_df = bids_layout.to_df()
76+
layout_df = layout_to_df(bids_layout)
2777

2878
subjects_id = bids_layout.get_subjects()
2979

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
name = "bids2openminds"
33
version = "0.2.0"
44
dependencies = [
5-
"bids-validator == 1.14.6" ,
6-
"bids",
5+
"ancpbids",
76
"openminds >= 0.2.3",
87
"click>=8.1",
98
"pandas",

test/test_bids_examples.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
# Dataset information in following order dataset_label, dataset_subject_number, dataset_subject_state_number, dataset_person_number, dataset_files_number, dataset_file_bundles_number, dataset_behavioral_protocol_number
88
example_dataset = [("ds003", 13, 13, 2, 58, 39, 1),
9-
("ds000247", 6, 10, 5, 202, 41, 2),
9+
# ancpBIDS treats CTF MEG .ds directories as single artifacts (not their internal files),
10+
# so file count is lower than with pybids (which listed each file inside .ds separately)
11+
("ds000247", 6, 10, 5, 120, 41, 2),
1012
# The authors list in 'eeg_cbm' contains non person entities 2 is not correct name (issue raied #43)
1113
("eeg_cbm", 20, 20, 2, 104, 40, 1),
1214
("asl001", 1, 1, 2, 8, 3, 0),

test/test_task.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pytest
2-
from bids import BIDSLayout
2+
from ancpbids import BIDSLayout
33
from openminds import Collection
44
import os
55
from bids2openminds.main import create_behavioral_protocol

0 commit comments

Comments
 (0)