Skip to content

Commit 55a921c

Browse files
when schema is loaded selectors then get loaded. Now need to implement the hooks and adding metadata etc..
1 parent 274fd69 commit 55a921c

File tree

5 files changed

+96
-17
lines changed

5 files changed

+96
-17
lines changed

bidsbuilder/bidsDataset.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import bidsschematools, bidsschematools.schema
2-
31
from pathlib import Path
42

53
from bidsbuilder.modules import *
64
from bidsbuilder.util.util import checkPath, isDir, clearSchema
75
from bidsbuilder.util.datasetTree import FileTree
6+
from bidsbuilder.util.schema import no_cache_load_schema
87
from bidsbuilder.modules.commonFiles import resolveCoreClassType
8+
from bidsbuilder.interpreter.selectors import selectorHook
99
from typing import TYPE_CHECKING
1010

1111
if TYPE_CHECKING:
1212
from collections.abc import MutableMapping
1313

1414
class BidsDataset():
1515
initialised = False
16-
schema = bidsschematools.schema.load_schema()
17-
schema.pop("meta")
16+
17+
schema = no_cache_load_schema()
1818

1919
def __init__(self, root:str = Path.cwd()):
2020
self.root = root
@@ -48,13 +48,18 @@ def _pop_from_schema(schema:'MutableMapping'):
4848

4949
_pop_from_schema(self.schema.rules.files.common.tables)
5050
clearSchema(self.schema.rules.files.common, "tables")
51-
52-
print(self._tree_reference.children)
53-
5451
return
5552

5653
def _interpret_skeletonBIDS(self):
54+
f1 = self.tree.fetch("README")
55+
f2 = self.tree.fetch("dataset_description.json")
56+
#recursive_interpret(1, self.schema.rules.files.common)
57+
print(self.schema.rules.dataset_metadata.dataset_description.selectors.funcs[0])
58+
59+
print(self.schema.rules.dataset_metadata.dataset_description.selectors(f1))
60+
print(self.schema.rules.dataset_metadata.dataset_description.selectors(f2))
5761

62+
print("hello")
5863
return
5964

6065
@property

bidsbuilder/interpreter/fields_funcs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import re
22

33
from typing import Any, Union, TYPE_CHECKING
4-
from functools import wraps
54
from bidsbuilder.util.datasetTree import FileTree
5+
from bidsbuilder.modules.coreModule import DatasetCore
66

77
if TYPE_CHECKING:
8-
from bidsbuilder.modules.coreModule import DatasetCore
98
from bidsbuilder.bidsDataset import BidsDataset
109

1110
def schema():
@@ -17,7 +16,8 @@ def dataset(core:'DatasetCore') -> 'BidsDataset':
1716
def subject():
1817
return notImplemented()
1918

20-
def path(core:'DatasetCore') -> str:
19+
def path(core:DatasetCore) -> str:
20+
assert isinstance(core, DatasetCore)
2121
return core._tree_reference.relative_path
2222

2323
def entities():

bidsbuilder/interpreter/selectors.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ def from_raw(cls, r_selector:list[str]) -> 'selectorHook':
5555
funcs = []
5656
for selector in r_selector:
5757
parser = SelectorParser.from_raw(selector)
58-
abstract_syntax_tree = parser.parse()
59-
func = selectorFunc(abstract_syntax_tree)
60-
func.evaluate_static_nodes()
61-
funcs.append(func)
58+
sel_function = parser.parse()
59+
sel_function.evaluate_static_nodes()
60+
funcs.append(sel_function)
6261

6362
return cls(funcs)
6463

@@ -67,7 +66,7 @@ def __init__(self, funcs):
6766
return
6867

6968
def __call__(self, *args, **kwargs):
70-
69+
7170
#all(func(*args, **kwargs) for func in self.funcs) can be slower
7271
for func in self.funcs:
7372
if not func(*args, **kwargs):
@@ -430,7 +429,8 @@ def primary(self):
430429

431430
elif cur.kind == "STRING":
432431
val = self.match("STRING")
433-
return val
432+
#need to trim outer " " or ' '
433+
return val[1:-1]
434434

435435
elif cur.kind == "LPAREN":
436436
self.match("LPAREN")

bidsbuilder/util/datasetTree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def relative_path(self) -> str:
198198
Directories include trailing slashes for simpler matching.
199199
"""
200200
if self.parent is None:
201-
return ''
201+
return '/'
202202

203203
return posixpath.join(self.parent.relative_path,f'{self.name}/')
204204

bidsbuilder/util/schema.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import bidsschematools
2+
3+
from functools import lru_cache
4+
from bidsschematools.types import Namespace
5+
from bidsschematools.schema import dereference, flatten_enums, _get_bids_version, _get_schema_version
6+
from bidsschematools.utils import get_bundled_schema_path
7+
8+
from bidsbuilder.interpreter.selectors import selectorHook
9+
10+
def recursive_interpret(rec_n, schema):
11+
#recurse into a file, i.e.
12+
#rec_n = 0 means we have a file, so then loops for each selector block, and then chooses selectors
13+
#rec_n = 1 means we have a folder of files,
14+
#rec_n = 2 means we have a folder of folders that need to be iterated all
15+
if rec_n >= 1:
16+
for key in schema.keys():
17+
schema[key] = recursive_interpret((rec_n-1), schema[key])
18+
19+
else:
20+
for sel in schema.keys():
21+
t_selector = selectorHook.from_raw(schema[sel].selectors)
22+
#the raw schema is cached
23+
schema[sel]["selectors"] = t_selector
24+
25+
return schema
26+
27+
@lru_cache
28+
def parse_load_schema(schema_path=None):
29+
"""Load and Parse the schema into a dictionary.
30+
31+
This function allows the schema, like BIDS itself, to be specified in
32+
a hierarchy of directories and files.
33+
Filenames (minus extensions) and directory names become keys
34+
in the associative array (dict) of entries composed from content
35+
of files and entire directories.
36+
37+
Parameters
38+
----------
39+
schema_path : str, optional
40+
Directory containing yaml files or yaml file. If ``None``, use the
41+
default schema packaged with ``bidsschematools``.
42+
43+
Returns
44+
-------
45+
dict
46+
Schema in dictionary form.
47+
48+
Notes
49+
-----
50+
This function is cached, so it will only be called once per schema path.
51+
"""
52+
if schema_path is None:
53+
schema_path = get_bundled_schema_path()
54+
#lgr.info("No schema path specified, defaulting to the bundled schema, `%s`.", schema_path)
55+
schema = Namespace.from_directory(schema_path)
56+
if not schema.objects:
57+
raise ValueError(f"objects subdirectory path not found in {schema_path}")
58+
if not schema.rules:
59+
raise ValueError(f"rules subdirectory path not found in {schema_path}")
60+
61+
dereference(schema)
62+
flatten_enums(schema)
63+
64+
schema["bids_version"] = _get_bids_version(schema_path)
65+
schema["schema_version"] = _get_schema_version(schema_path)
66+
schema.pop("meta")
67+
schema.rules["dataset_metadata"] = recursive_interpret(0, schema.rules.dataset_metadata)
68+
#IMPORTANT, CAN ONLY SET BY DOING schema["KEY"] = , setting by doing schema.key = DOES NOT WORK
69+
#maybe look at making a wrapper class of namespace which fixes the __setitem__
70+
#recursive_interpret(1, self.schema.rules.files.common)
71+
72+
#look at iterating over the whole schema and interpreting all "selectors"
73+
74+
return schema

0 commit comments

Comments
 (0)