-
Notifications
You must be signed in to change notification settings - Fork 33
Populate per-asset dataStandard for BIDS, HED, NWB, and extensions #1811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yarikoptic
wants to merge
9
commits into
master
Choose a base branch
from
hed
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a4cc526
feat: populate per-asset dataStandard for BIDS, HED, NWB, and OME/NGFF
yarikoptic 8b14fc8
feat: extract NWB extensions and HED library schemas into dataStandard
yarikoptic e4d9537
chore: git ignore uv.lock
yarikoptic 0971616
refactor: centralize dandischema compat guard as _SCHEMA_BAREASSET_HA…
yarikoptic 68edbc7
refactor: move imports to top level, document import policy in CLAUDE.md
yarikoptic 69923e8
fix: guard hed_standard import for compat with released dandischema
yarikoptic 4e49f67
fix: resolve lint (E402) and typing errors for dandischema compat
yarikoptic 7b0416f
Extract NWB spec version into dataStandard
bendichter f69a338
TEMP: install dandischema from dandi-schema#371 (enh-hed branch)
yarikoptic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,3 +16,4 @@ sandbox/ | |
| venv/ | ||
| venvs/ | ||
| .DS_Store | ||
| uv.lock | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,15 +3,32 @@ | |
| from collections import defaultdict | ||
| from dataclasses import dataclass, field | ||
| from datetime import datetime | ||
| import json | ||
| from pathlib import Path | ||
| from threading import Lock | ||
| import weakref | ||
|
|
||
| from dandischema.models import BareAsset | ||
| from dandischema.models import ( | ||
| BareAsset, | ||
| StandardsType, | ||
| bids_standard, | ||
| ome_ngff_standard, | ||
| ) | ||
|
|
||
| import dandi | ||
| from dandi.bids_validator_deno import bids_validate | ||
|
|
||
| from .bases import GenericAsset, LocalFileAsset, NWBAsset | ||
| from .bases import ( | ||
| _SCHEMA_BAREASSET_HAS_DATASTANDARD, | ||
| GenericAsset, | ||
| LocalFileAsset, | ||
| NWBAsset, | ||
| ) | ||
|
|
||
| if _SCHEMA_BAREASSET_HAS_DATASTANDARD: | ||
| from dandischema.models import hed_standard # type: ignore[attr-defined] | ||
| else: | ||
| hed_standard = None # type: ignore[assignment] | ||
| from .zarr import ZarrAsset | ||
| from ..consts import ZARR_MIME_TYPE, dandiset_metadata_file | ||
| from ..metadata.core import add_common_metadata, prepare_metadata | ||
|
|
@@ -23,10 +40,32 @@ | |
| ValidationResult, | ||
| ) | ||
|
|
||
| lgr = dandi.get_logger() | ||
|
|
||
| BIDS_ASSET_ERRORS = ("BIDS.NON_BIDS_PATH_PLACEHOLDER",) | ||
| BIDS_DATASET_ERRORS = ("BIDS.MANDATORY_FILE_MISSING_PLACEHOLDER",) | ||
|
|
||
|
|
||
| def _add_standard( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line 542 of bases appears to have similar logic |
||
| metadata: BareAsset, | ||
| standard_dict: dict, | ||
| version: str | None = None, | ||
| ) -> None: | ||
| """Add a data standard to asset metadata if the field is available.""" | ||
| if not _SCHEMA_BAREASSET_HAS_DATASTANDARD: | ||
| return | ||
| kwargs = dict(standard_dict) | ||
| if version: | ||
| kwargs["version"] = version | ||
| standard = StandardsType(**kwargs) | ||
| # TODO: use metadata.dataStandard directly once min dandischema >= 0.12.2 | ||
| cur = getattr(metadata, "dataStandard", None) | ||
| if cur is None: | ||
| setattr(metadata, "dataStandard", [standard]) | ||
| elif standard not in cur: | ||
| cur.append(standard) | ||
|
|
||
|
|
||
| @dataclass | ||
| class BIDSDatasetDescriptionAsset(LocalFileAsset): | ||
| """ | ||
|
|
@@ -192,7 +231,48 @@ | |
| assert self._dataset_errors is not None | ||
| return self._dataset_errors.copy() | ||
|
|
||
| # get_metadata(): inherit use of default metadata from LocalFileAsset | ||
| def get_metadata( | ||
| self, | ||
| digest: Digest | None = None, | ||
| ignore_errors: bool = True, | ||
| ) -> BareAsset: | ||
| metadata = super().get_metadata(digest=digest, ignore_errors=ignore_errors) | ||
| try: | ||
| with open(self.filepath) as f: | ||
| desc = json.load(f) | ||
| except (OSError, json.JSONDecodeError) as e: | ||
| lgr.warning("Failed to read %s: %s", self.filepath, e) | ||
| _add_standard(metadata, bids_standard) | ||
| return metadata | ||
| _add_standard(metadata, bids_standard, version=desc.get("BIDSVersion")) | ||
| if hed_standard and (hed_version := desc.get("HEDVersion")): | ||
| # HEDVersion can be a string or list. | ||
| # List form: ["8.2.0", "sc:1.0.0"] where first element is base | ||
| # HED version and subsequent "prefix:version" entries are library | ||
| # schemas recorded as extensions. | ||
| if isinstance(hed_version, list): | ||
| version = hed_version[0] if hed_version else None | ||
| library_entries = hed_version[1:] | ||
| else: | ||
| version = hed_version | ||
| library_entries = [] | ||
| kwargs: dict = dict(hed_standard) | ||
| if version: | ||
| kwargs["version"] = version | ||
| if library_entries: | ||
| extensions = [] | ||
| for entry in library_entries: | ||
| # Format is "prefix:version" (e.g. "sc:1.0.0") | ||
| if ":" in str(entry): | ||
| lib_name, lib_ver = str(entry).split(":", 1) | ||
| else: | ||
| lib_name, lib_ver = str(entry), None | ||
| ext = StandardsType(name=lib_name, version=lib_ver) # type: ignore[call-arg] | ||
| extensions.append(ext.model_dump(mode="json", exclude_none=True)) | ||
| if extensions: | ||
| kwargs["extensions"] = extensions | ||
| _add_standard(metadata, kwargs) | ||
| return metadata | ||
|
|
||
|
|
||
| @dataclass | ||
|
|
@@ -312,6 +392,8 @@ | |
| add_common_metadata(metadata, self.filepath, start_time, end_time, digest) | ||
| metadata.path = self.path | ||
| metadata.encodingFormat = ZARR_MIME_TYPE | ||
| if Path(self.path).suffixes == [".ome", ".zarr"]: | ||
| _add_standard(metadata, ome_ngff_standard) | ||
| return metadata | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Cyclic import Note
Copilot Autofix
AI 5 days ago
To break the cycle with minimal behavioral change, keep only the class needed for immediate class definition (
LocalFileAsset) as a top-level import, and lazily import the other.basessymbols (GenericAsset,NWBAsset,_SCHEMA_BAREASSET_HAS_DATASTANDARD) inside the methods/functions that use them.In the shown snippet, we can safely reduce the top-level
.basesimport to onlyLocalFileAsset. This removes the cycle-triggering eager dependency footprint from module import time for three symbols. Then, in regions ofdandi/files/bids.py(not shown here) whereGenericAsset,NWBAsset, or_SCHEMA_BAREASSET_HAS_DATASTANDARDare referenced, add local imports in those functions/methods. For typing-only references, preferfrom __future__ import annotations(already present) and/orif TYPE_CHECKING:imports.Within the provided lines, the concrete edit is:
from .bases import LocalFileAsset._SCHEMA_BAREASSET_HAS_DATASTANDARDat module import time (set a conservative default and load actual flag lazily where needed).