NeuroMorphoClient#
- class braincell.io.NeuroMorphoClient(session=None, *, timeout=30.0, cache_dir=None, retries=3, backoff_base=0.5)[source]#
Stateful client for the NeuroMorpho.Org REST API.
The client wraps a
requests.Session(lazily imported on first construction so the package can be imported withoutrequestsinstalled) and adds retry/backoff for transient failures, an optionalNeuroMorphoCache, and typed return values.- Parameters:
session (
Any) – Session-like object exposingget(url, params=..., timeout=..., stream=...). Pass a configuredrequests.Sessionto control proxies, headers, or HTTPS verification.Noneconstructs a freshrequests.Session(importingrequestslazily).timeout (
float) – Per-request timeout in seconds. Defaults toDEFAULT_TIMEOUT.cache_dir (
str|Path|None) – Root directory for cached downloads. When provided, the client creates aNeuroMorphoCacheaccessible viacache.retries (
int) – Number of attempts for JSON API calls. Must be>= 1. Streaming file downloads are not retried.backoff_base (
float) – Base delay (seconds) for exponential backoff between retries.
- Variables:
session (object)
timeout (float)
cache (NeuroMorphoCache or None) – Set when
cache_dirwas provided.retries (int)
backoff_base (float)
Examples
>>> client = NeuroMorphoClient(cache_dir="~/data/neuromorpho") >>> page = client.search("species:mouse", size=5) >>> for neuron in client.iter_search("species:mouse", limit=20): ... client.download(neuron)
- property cache: NeuroMorphoCache | None#
Return the attached
NeuroMorphoCache, if configured.
- describe(neuron, *, include_measurement=True)[source]#
Return an aggregate
NeuroMorphoDetailfor a neuron.Combines
get_neuron()(when an id is passed),get_measurement(),get_urls(), andget_cache_status()in one call.- Parameters:
neuron (
NeuroMorphoNeuron|int)include_measurement (
bool) – IfFalse, themeasurementfield of the result isNoneand no measurement request is issued.
- Return type:
- download(neuron, output_dir=None, *, mode='both', overwrite=False, dry_run=False)[source]#
Download files for neuron into the cache layout.
When
output_dirisNonethe client falls back tocache_dir. Eitheroutput_diror a configured cacheis required.
- Parameters:
neuron (
NeuroMorphoNeuron|int)output_dir (
str|Path|None) – Cache root.Noneuses the client’s configured cache.mode (
Literal['standard','original','both'])overwrite (
bool) – Re-download files that are already on disk.dry_run (
bool) – IfTrue, do not touch the network or filesystem. Returns a populatedNeuroMorphoDownloadRecordwhose items havedownloaded_now=Falseandreason="dry_run".
- Return type:
- Raises:
ValueError – If neither
output_dirnor a client cache is provided.
- file_plan(neuron, *, mode='both')[source]#
Build the typed download plan for neuron.
- Parameters:
neuron (
NeuroMorphoNeuron)mode (
Literal['standard','original','both'])
- Return type:
- get_cache_status(neuron)[source]#
Return on-disk cache status for neuron.
- Parameters:
neuron (
NeuroMorphoNeuron|int)- Returns:
configured=Falsewhen this client has no cache attached.- Return type:
- get_measurement(neuron)[source]#
Fetch the morphometry record for a neuron.
- Parameters:
neuron (
NeuroMorphoNeuron|int) – Either a previously-fetched neuron or its id.- Return type:
- get_neuron(neuron_id)[source]#
Fetch the neuron record for neuron_id.
- Parameters:
neuron_id (
int)- Return type:
- Raises:
NeuroMorphoNotFoundError – If the upstream returns 404 for the id.
- get_urls(neuron)[source]#
Return the resolved URLs for neuron.
- Parameters:
neuron (
NeuroMorphoNeuron)- Return type:
- iter_search(query, *, fq=None, size=20, limit=None, start_page=0, sort='neuron_id,asc')[source]#
Yield every neuron matching query, paging lazily.
Stops when the upstream reports the last page, when an empty page is returned, or when
limitneurons have been yielded. Duplicates are filtered out across pages.
- search(query, *, fq=None, size=20, page=0, sort='neuron_id,asc')[source]#
Run a single search request against
/api/neuron/select.- Parameters:
query (
str|NeuroMorphoQuery) – Either a raw Solrqstring or a typedNeuroMorphoQuery. When a query object is passed, itsraw_fqis merged with the explicitfqargument.fq (
list[str] |None) – Extra Solr filter strings to append tofq.size (
int) – Page size requested from the API.page (
int) – Zero-based page index.sort (
str) – Sort string forwarded to the API.
- Return type: