Skip to content
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

Maint sparseindex #525

Merged
merged 8 commits into from
Dec 6, 2024
2 changes: 2 additions & 0 deletions e2e/volumes/test_sparsemap_cache_uniqueness.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import siibra
from siibra.volumes.sparsemap import SparseMap


def test_sparsemap_cache_uniqueness():
mp157 = siibra.get_map("julich 3.0", "colin 27", "statistical", spec="157")
mp175 = siibra.get_map("julich 3.0", "colin 27", "statistical", spec="175")
assert isinstance(mp157, SparseMap) and isinstance(mp175, SparseMap)
assert mp157.sparse_index.probs[0] != mp175.sparse_index.probs[0]
35 changes: 9 additions & 26 deletions siibra/retrieval/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
EbrainsRequest,
SiibraHttpRequestError,
find_suitiable_decoder,
DECODERS
DECODERS,
FileLoader
)
from .cache import CACHE

Expand Down Expand Up @@ -124,34 +125,16 @@
self._folder = pathlib.Path(folder)
assert pathlib.Path.is_dir(self._folder)

def _build_url(self, folder: str, filename: str):
return pathlib.Path.joinpath(self._folder, folder, filename)

class FileLoader:
"""
Just a loads a local file, but mimics the behaviour
of cached http requests used in other connectors.
"""
def __init__(self, file_url, decode_func):
self.url = file_url
self.func = decode_func
self.cached = True

@property
def data(self):
with open(self.url, 'rb') as f:
return self.func(f.read())
def _build_url(self, folder: str, filename: str) -> str:
return pathlib.Path.joinpath(self._folder, folder, filename).as_posix()

Check warning on line 129 in siibra/retrieval/repositories.py

View check run for this annotation

Codecov / codecov/patch

siibra/retrieval/repositories.py#L129

Added line #L129 was not covered by tests

def get_loader(self, filename, folder="", decode_func=None):
"""Get a lazy loader for a file, for loading data
only once loader.data is accessed."""
url = self._build_url(folder, filename)
if url is None:
raise RuntimeError(f"Cannot build url for ({folder}, {filename})")
if decode_func is None:
return self.FileLoader(url, lambda b: self._decode_response(b, filename))
else:
return self.FileLoader(url, decode_func)
filepath = self._build_url(folder, filename)
if not pathlib.Path(filepath).is_file():
raise RuntimeError(f"No file is found in {filepath}")
return FileLoader(filepath, decode_func)

Check warning on line 137 in siibra/retrieval/repositories.py

View check run for this annotation

Codecov / codecov/patch

siibra/retrieval/repositories.py#L134-L137

Added lines #L134 - L137 were not covered by tests

def search_files(self, folder="", suffix=None, recursive=False):
results = []
Expand All @@ -165,7 +148,7 @@
def __str__(self):
return f"{self.__class__.__name__} at {self._folder}"

def __eq__(self, other):
def __eq__(self, other: "LocalFileRepository"):
return self._folder == other._folder


Expand Down
21 changes: 19 additions & 2 deletions siibra/retrieval/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@
suitable_decoders = [
dec for sfx, dec in DECODERS.items() if urlpath.endswith(sfx)
]
if len(suitable_decoders) > 0:
assert len(suitable_decoders) == 1
if len(suitable_decoders) == 1:
return suitable_decoders[0]
else:
return None
Expand Down Expand Up @@ -270,6 +269,24 @@
return self.get()


class FileLoader(HttpRequest):
"""
Just a loads a local file, but mimics the behaviour
of cached http requests used in other connectors.
"""
def __init__(self, filepath, func=None):
HttpRequest.__init__(

Check warning on line 278 in siibra/retrieval/requests.py

View check run for this annotation

Codecov / codecov/patch

siibra/retrieval/requests.py#L278

Added line #L278 was not covered by tests
self, filepath, refresh=False,
func=func or find_suitiable_decoder(filepath)
)
self.cachefile = filepath

Check warning on line 282 in siibra/retrieval/requests.py

View check run for this annotation

Codecov / codecov/patch

siibra/retrieval/requests.py#L282

Added line #L282 was not covered by tests

def _retrieve(self, **kwargs):
if kwargs:
logger.info(f"Keywords {list(kwargs.keys())} are supplied but won't be used.")
assert os.path.isfile(self.cachefile)

Check warning on line 287 in siibra/retrieval/requests.py

View check run for this annotation

Codecov / codecov/patch

siibra/retrieval/requests.py#L285-L287

Added lines #L285 - L287 were not covered by tests


class ZipfileRequest(HttpRequest):
def __init__(self, url, filename, func=None, refresh=False):
HttpRequest.__init__(
Expand Down
Loading
Loading