From f43b688755b82c6e1597e476b41abf0d5c2788fc Mon Sep 17 00:00:00 2001 From: Igor Tatarnikov Date: Tue, 22 Oct 2024 15:48:04 +0100 Subject: [PATCH 1/7] Exposed the invert_dim parameter --- brainrender/actors/neurons.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/brainrender/actors/neurons.py b/brainrender/actors/neurons.py index 6e3aa5bc..9b5262fe 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,7 @@ def __init__( :param color: str, :param neuron_radius: float, radius of axon/dendrites :param soma_radius: float, radius of soma + :param invert_dims: bool, invert dimensions of neuron :param name: str, actor name """ logger.debug("Creating a Neuron actor") @@ -61,7 +63,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 +88,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 +101,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) ) From 783b74f803ef23e2a6ccb9026d964fbc261b1f10 Mon Sep 17 00:00:00 2001 From: Igor Tatarnikov Date: Tue, 22 Oct 2024 16:06:55 +0100 Subject: [PATCH 2/7] Fixed catch block for mouselightapi example --- examples/neurons.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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! From e6bece4153f83fd61af1d198ba246c24a91ee299 Mon Sep 17 00:00:00 2001 From: Igor Tatarnikov Date: Wed, 23 Oct 2024 10:27:05 +0100 Subject: [PATCH 3/7] Add delay and retry on gene API fetch failure --- .../allen_brain_atlas/gene_expression/api.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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..ec9e5654 100644 --- a/brainrender/atlas_specific/allen_brain_atlas/gene_expression/api.py +++ b/brainrender/atlas_specific/allen_brain_atlas/gene_expression/api.py @@ -2,6 +2,7 @@ import sys import pandas as pd +import requests from loguru import logger from brainrender import base_dir @@ -86,7 +87,16 @@ 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 = 5 + delay = 4 + + 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}") + delay *= 2 if not len(data): print(f"No experiment found for gene {gene}") From 1cb2e9dadd46ff32af66c470375a7e7c8d1e0337 Mon Sep 17 00:00:00 2001 From: Igor Tatarnikov Date: Wed, 23 Oct 2024 10:35:53 +0100 Subject: [PATCH 4/7] Increase max retries to 8 --- .../atlas_specific/allen_brain_atlas/gene_expression/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 ec9e5654..85ed4327 100644 --- a/brainrender/atlas_specific/allen_brain_atlas/gene_expression/api.py +++ b/brainrender/atlas_specific/allen_brain_atlas/gene_expression/api.py @@ -87,7 +87,7 @@ def get_gene_experiments(self, gene): :param gene_symbol: str """ url = self.gene_experiments_url.replace("-GENE_SYMBOL-", gene) - max_retries = 5 + max_retries = 8 delay = 4 for i in range(max_retries): From 98c56798f5bba481956c943bc349de7c6ffc4fe4 Mon Sep 17 00:00:00 2001 From: Igor Tatarnikov Date: Wed, 23 Oct 2024 11:00:17 +0100 Subject: [PATCH 5/7] Added sleep call --- .../atlas_specific/allen_brain_atlas/gene_expression/api.py | 3 +++ 1 file changed, 3 insertions(+) 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 85ed4327..b044be05 100644 --- a/brainrender/atlas_specific/allen_brain_atlas/gene_expression/api.py +++ b/brainrender/atlas_specific/allen_brain_atlas/gene_expression/api.py @@ -1,5 +1,6 @@ import os import sys +from time import sleep import pandas as pd import requests @@ -89,6 +90,7 @@ def get_gene_experiments(self, gene): url = self.gene_experiments_url.replace("-GENE_SYMBOL-", gene) max_retries = 8 delay = 4 + data = None for i in range(max_retries): try: @@ -97,6 +99,7 @@ def get_gene_experiments(self, gene): except requests.exceptions.JSONDecodeError: print(f"Unable to connect to Allen API, retrying in {delay}") delay *= 2 + sleep(delay) if not len(data): print(f"No experiment found for gene {gene}") From 36b51b8c268a24fa0e71c6e318cf84e2a9ccef29 Mon Sep 17 00:00:00 2001 From: Igor Tatarnikov Date: Wed, 23 Oct 2024 11:29:20 +0100 Subject: [PATCH 6/7] Changed order of sleep and delay --- .../atlas_specific/allen_brain_atlas/gene_expression/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 b044be05..a07481f2 100644 --- a/brainrender/atlas_specific/allen_brain_atlas/gene_expression/api.py +++ b/brainrender/atlas_specific/allen_brain_atlas/gene_expression/api.py @@ -98,8 +98,8 @@ def get_gene_experiments(self, gene): break except requests.exceptions.JSONDecodeError: print(f"Unable to connect to Allen API, retrying in {delay}") - delay *= 2 sleep(delay) + delay *= 2 if not len(data): print(f"No experiment found for gene {gene}") From 9fc523468918a5cb9b3fcd7fc1bd80b1a9921134 Mon Sep 17 00:00:00 2001 From: Igor Tatarnikov Date: Wed, 23 Oct 2024 17:59:13 +0100 Subject: [PATCH 7/7] Updated docstring --- brainrender/actors/neurons.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/brainrender/actors/neurons.py b/brainrender/actors/neurons.py index 9b5262fe..87226860 100644 --- a/brainrender/actors/neurons.py +++ b/brainrender/actors/neurons.py @@ -51,7 +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, invert dimensions of neuron + :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")