diff --git a/brainrender/actors/neurons.py b/brainrender/actors/neurons.py index 6e3aa5bc..87226860 100644 --- a/brainrender/actors/neurons.py +++ b/brainrender/actors/neurons.py @@ -41,6 +41,7 @@ def __init__( alpha=1, neurite_radius=8, soma_radius=15, + invert_dims=True, name=None, ): """ @@ -50,6 +51,8 @@ def __init__( :param color: str, :param neuron_radius: float, radius of axon/dendrites :param soma_radius: float, radius of soma + :param invert_dims: bool, exchange the first and last dimension coordinates + when loading from a .swc file. e.g going from (x, y, z) to (z, y, x). :param name: str, actor name """ logger.debug("Creating a Neuron actor") @@ -61,7 +64,7 @@ def __init__( self.name = None if isinstance(neuron, (str, Path)): - mesh = self._from_file(neuron) + mesh = self._from_file(neuron, invert_dims) elif isinstance(neuron, (Mesh)): mesh = neuron elif isinstance(neuron, Actor): @@ -86,7 +89,7 @@ def _from_morphapi_neuron(self, neuron: MorphoNeuron): )[1] return mesh - def _from_file(self, neuron: (str, Path)): + def _from_file(self, neuron: (str, Path), invert_dims): path = Path(neuron) if not path.exists(): raise FileExistsError(f"Neuron file doesn't exist: {path}") @@ -99,5 +102,5 @@ def _from_file(self, neuron: (str, Path)): self.name = self.name or path.name return self._from_morphapi_neuron( - MorphoNeuron(data_file=neuron, invert_dims=True) + MorphoNeuron(data_file=neuron, invert_dims=invert_dims) ) diff --git a/brainrender/atlas_specific/allen_brain_atlas/gene_expression/api.py b/brainrender/atlas_specific/allen_brain_atlas/gene_expression/api.py index 57340dae..a07481f2 100644 --- a/brainrender/atlas_specific/allen_brain_atlas/gene_expression/api.py +++ b/brainrender/atlas_specific/allen_brain_atlas/gene_expression/api.py @@ -1,7 +1,9 @@ import os import sys +from time import sleep import pandas as pd +import requests from loguru import logger from brainrender import base_dir @@ -86,7 +88,18 @@ def get_gene_experiments(self, gene): :param gene_symbol: str """ url = self.gene_experiments_url.replace("-GENE_SYMBOL-", gene) - data = request(url).json()["msg"] + max_retries = 8 + delay = 4 + data = None + + for i in range(max_retries): + try: + data = request(url).json()["msg"] + break + except requests.exceptions.JSONDecodeError: + print(f"Unable to connect to Allen API, retrying in {delay}") + sleep(delay) + delay *= 2 if not len(data): print(f"No experiment found for gene {gene}") diff --git a/examples/neurons.py b/examples/neurons.py index 58881166..250b7361 100644 --- a/examples/neurons.py +++ b/examples/neurons.py @@ -4,6 +4,7 @@ from morphapi.api.mouselight import MouseLightAPI from myterial import orange from rich import print +from urllib3.exceptions import NewConnectionError, MaxRetryError from brainrender import Scene from brainrender.actors import Neuron, make_neurons @@ -28,7 +29,12 @@ to_add = [neurons_metadata[47], neurons_metadata[51]] neurons = mlapi.download_neurons(to_add) neurons = scene.add(*make_neurons(*neurons, neurite_radius=12)) -except ConnectionError or requests.exceptions.ReadTimeout as e: +except ( + NewConnectionError, + MaxRetryError, + requests.exceptions.ConnectionError, + requests.exceptions.ReadTimeout, +) as e: print("Failed to download neurons data from neuromorpho.org.") # Render!