Skip to content

Commit c0132af

Browse files
committed
Allow local path = uri; update volume building
1 parent e65feee commit c0132af

3 files changed

Lines changed: 35 additions & 14 deletions

File tree

src/neuromaps_prime/graph/builder.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
from __future__ import annotations
1212

13-
from pathlib import Path
1413
from typing import TYPE_CHECKING, Any, cast
1514

1615
import yaml
@@ -30,6 +29,8 @@
3029
from neuromaps_prime.resources import NEUROMAPSPRIME_GRAPH
3130

3231
if TYPE_CHECKING:
32+
from pathlib import Path
33+
3334
import networkx as nx
3435

3536

@@ -201,10 +202,6 @@ def _build_volume_edge(
201202
# Generic resource parsers #
202203
# ------------------------------------------------------------------ #
203204

204-
def _resolve_path(self, path: str) -> Path:
205-
"""Prepend data_dir to path when set, otherwise return as-is."""
206-
return (self.data_dir / path) if self.data_dir else Path(path)
207-
208205
def _parse_surface_annotations(
209206
self, prefix: str, space: str, density: str, annots: dict[str, Any]
210207
) -> list[SurfaceAnnotation]:
@@ -402,24 +399,28 @@ def _parse_volume_resources(
402399
for vol_type, vol_value in types.items():
403400
if vol_type == "annotation":
404401
for annot_key, annot_dict in vol_value.items():
402+
name = f"{prefix}_{res}_{annot_key}.nii.gz"
405403
annotations.append(
406404
VolumeAnnotation(
407-
name=f"{prefix}_{res}_{annot_key}",
405+
name=name,
408406
space=space,
409407
label=annot_key,
410408
resolution=res,
411-
file_path=self._resolve_path(annot_dict.get("uri")),
409+
uri=annot_dict.get("uri"),
410+
file_path=self.data_dir / name,
412411
references=annot_dict.get("references"),
413412
notes=annot_dict.get("notes"),
414413
)
415414
)
416415
continue
417416

418417
extra = {"provider": provider} if is_transform else {}
418+
name = f"{prefix}_{res}_{vol_type}"
419419
result.append(
420420
cls(
421-
name=f"{prefix}_{res}_{vol_type}",
422-
file_path=self._resolve_path(vol_value),
421+
name=name,
422+
uri=vol_value,
423+
file_path=self.data_dir / name,
423424
resolution=res,
424425
resource_type=vol_type,
425426
references=transform_refs,

src/neuromaps_prime/graph/models.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ def fetch(self) -> Path:
3131
Raises:
3232
FileNotFoundError: if file cannot be fetched
3333
"""
34-
if not self.file_path.exists():
35-
if self.uri is None:
36-
raise FileNotFoundError("File does not exist and cannot be fetched.")
34+
if self.file_path.exists():
35+
return self.file_path
36+
if self.uri is None:
37+
raise FileNotFoundError("File does not exist and cannot be fetched.")
38+
if (local_file := Path(self.uri)).exists():
39+
self.file_path = local_file
40+
else:
3741
_logger.info(f"Fetching {self.file_path.name} from remote server.")
3842
download_and_validate(uri=self.uri, dest=self.file_path)
3943
if not self.file_path.exists():

tests/unit/graph/test_model.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
from pathlib import Path
56
from typing import TYPE_CHECKING, Any, Protocol
67
from unittest.mock import MagicMock, patch
78

@@ -11,7 +12,6 @@
1112

1213
if TYPE_CHECKING:
1314
from collections.abc import Callable
14-
from pathlib import Path
1515

1616

1717
@pytest.fixture
@@ -108,9 +108,25 @@ def test_validate_and_fetch(
108108
obj = model_cls(
109109
name="test",
110110
description="testing description",
111-
file_path=tmp_file,
111+
uri=str(tmp_file),
112+
file_path=Path("abc"),
112113
**extra_kwargs,
113114
)
115+
assert obj.file_path.name == "abc"
116+
assert obj.fetch() == tmp_file
117+
assert obj.file_path == tmp_file
118+
119+
def test_fetch_existing_file_path(self, tmp_file: Path) -> None:
120+
"""Test resource instantiation and fetching."""
121+
obj = models.SurfaceAtlas(
122+
name="test",
123+
description="testing description",
124+
space="Yerkes19",
125+
density="32k",
126+
hemisphere="left",
127+
resource_type="surf_atlas",
128+
file_path=tmp_file,
129+
)
114130
assert obj.file_path == tmp_file
115131
assert obj.fetch() == tmp_file
116132

0 commit comments

Comments
 (0)