From b8724463a3ee48ed216e29f36177cb63ea1ee80a Mon Sep 17 00:00:00 2001 From: Jakob Troidl Date: Mon, 20 May 2024 16:03:44 -0400 Subject: [PATCH] fix: neuromorpho interface --- navis/interfaces/neuromorpho.py | 41 ++++++++++++++++++++++++++++----- requirements.txt | 1 + 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/navis/interfaces/neuromorpho.py b/navis/interfaces/neuromorpho.py index f291e437..3deb6480 100644 --- a/navis/interfaces/neuromorpho.py +++ b/navis/interfaces/neuromorpho.py @@ -20,22 +20,50 @@ import pandas as pd import numpy as np +import pycurl +import urllib.parse + from typing import List, Dict, Union, Optional from concurrent.futures import ThreadPoolExecutor, as_completed +from io import BytesIO from ..core import TreeNeuron, NeuronList from ..io import read_swc from .. import utils, config -baseurl = 'http://neuromorpho.org' +baseurl = 'http://cng.gmu.edu:8080/' +baseurl_swc = 'https://neuromorpho.org/' + + +def get_neuromorpho_swc( + url: str, + encode: bool = False, +) -> str: + + if encode: + url = urllib.parse.quote(url, safe="/:") + try: + buffer = BytesIO() + c = pycurl.Curl() + c.setopt(c.URL, url) + c.setopt(c.WRITEDATA, buffer) + c.setopt(c.SSL_VERIFYPEER, 0) # This disables SSL peer verification + c.setopt(c.SSL_VERIFYHOST, 0) # This disables SSL host verification + c.perform() + c.close() + + return buffer.getvalue().decode("utf-8") + except pycurl.error as e: + print(f"Error fetching data: {e}") + return None def find_neurons(page_limit: Optional[int] = None, - parallel: bool = True, - max_threads: int = 4, - **filters) -> pd.DataFrame: + parallel: bool = True, + max_threads: int = 4, + **filters) -> pd.DataFrame: """Find neurons matching by given criteria. Parameters @@ -225,9 +253,10 @@ def get_neuron(x: Union[str, int, Dict[str, str]], archive: str = info['archive'] name: str = info['neuron_name'] - url = utils.make_url(baseurl, 'dableFiles', archive.lower(), 'CNG version', name + '.CNG.swc') + url = utils.make_url(baseurl_swc, 'dableFiles', archive.lower(), 'CNG version', name + '.CNG.swc') + swc = get_neuromorpho_swc(url, encode=True) - n = read_swc(url, **kwargs) + n = read_swc(swc, **kwargs) n.id = info.get('neuron_id', n.id) n.name = info.get('neuron_name', getattr(n, 'name')) diff --git a/requirements.txt b/requirements.txt index 40774d48..143dc436 100644 --- a/requirements.txt +++ b/requirements.txt @@ -22,6 +22,7 @@ molesq>=0.2.0 rdata>=0.5 igraph!=0.10.0,!=0.10.1 skeletor>=1.0.0 +pycurl>=7.45.3 pathos>=0.2.7 #extra: pathos