diff --git a/src/vfb_connect/__init__.py b/src/vfb_connect/__init__.py index 6a8780fd..1a6e80af 100644 --- a/src/vfb_connect/__init__.py +++ b/src/vfb_connect/__init__.py @@ -1,6 +1,6 @@ from .cross_server_tools import VfbConnect # Create an instance of VfbConnect and make it available directly -vfb = VfbConnect() +vfb = VfbConnect(vfb_launch=True) __all__ = ['vfb', 'VfbConnect'] \ No newline at end of file diff --git a/src/vfb_connect/cross_server_tools.py b/src/vfb_connect/cross_server_tools.py index d0f70555..6a3b2f2b 100644 --- a/src/vfb_connect/cross_server_tools.py +++ b/src/vfb_connect/cross_server_tools.py @@ -122,7 +122,7 @@ def __init__(self, neo_endpoint=get_default_servers()['neo_endpoint'], print("\033[32mSession Established!\033[0m") print("") - print("\033[33mType \033[35mvfb. \033[33mand press \033[35mtab\033[33m to see available queries. You can run help against any query e.g. \033[35mhelp(vfb.terms)\033[0m") + print("\033[33mType \033[35mvfb. \033[33mand press \033[35mtab\033[33m to see available queries. You can run help against any query e.g. \033[35mhelp(vfb.terms)\033[0m") if vfb_launch else None def __dir__(self): return [attr for attr in list(self.__dict__.keys()) if not attr.startswith('_')] + [attr for attr in dir(self.__class__) if not attr.startswith('_') and not attr.startswith('add_')] @@ -852,6 +852,9 @@ def xref_2_vfb_id(self, acc=None, db='', id_type='', reverse_return=False, retur else: new_acc.append(xref) acc = new_acc + if isinstance(acc, list) and all(isinstance(x, int) for x in acc): + acc = [str(x) for x in acc] + print(f"Converted to strings: {acc}") if verbose else None if db in VFB_DBS_2_SYMBOLS.keys(): db = VFB_DBS_2_SYMBOLS[db] if db not in self.get_dbs(): @@ -1418,7 +1421,7 @@ def generate_lab_colors(self, num_colors, min_distance=100, verbose=False): rgb_colors = [] # Select the first color - lab_tree = KDTree([(0, 0, 0)]) # Start tree with black + lab_tree = KDTree([(255,255,255),(0, 0, 0)]) # Start tree with black and white # Pick colors that are far apart from each other and from black for lab in lab_colors[0:]: diff --git a/src/vfb_connect/neo/query_wrapper.py b/src/vfb_connect/neo/query_wrapper.py index 1df99c4b..42fc3862 100644 --- a/src/vfb_connect/neo/query_wrapper.py +++ b/src/vfb_connect/neo/query_wrapper.py @@ -4,6 +4,7 @@ import shutil from inspect import getfullargspec from string import Template +from time import sleep from xml.sax import saxutils import pandas as pd import pkg_resources @@ -609,7 +610,13 @@ def _get_Cached_TermInfo(self, short_forms: iter, summary=True, return_dataframe short_forms = list(chain.from_iterable(short_forms)) if any(isinstance(i, list) for i in short_forms) else short_forms print(f"Checking cache for results: short_forms={short_forms}") if verbose else None print(f"Looking for {len(short_forms)} results.") if verbose else None - results = self._serialize_solr_output(vfb_solr.search('*', **{'fl': 'term_info','df': 'id', 'defType': 'edismax', 'q.op': 'OR','rows': len(short_forms)+10,'fq':'{!terms f=id}'+ ','.join(short_forms)})) + try: + result = vfb_solr.search('*', **{'fl': 'term_info','df': 'id', 'defType': 'edismax', 'q.op': 'OR','rows': len(short_forms)+10,'fq':'{!terms f=id}'+ ','.join(short_forms)}) + except Exception as e: + print(f"\033[33mWarning:\033[0m Cache query failed. Error: {e}") + sleep(15) # Sleep for 15 seconds to avoid overloading the server + return self._get_Cached_TermInfo(short_forms, summary=summary, return_dataframe=return_dataframe, verbose=verbose) + results = self._serialize_solr_output(result) print(f"Got {len(results)} results.") if verbose else None if len(short_forms) != len(results): print(f"Warning: Cache didn't return all results. Got {len(results)} out of {len(short_forms)}") if verbose else None diff --git a/src/vfb_connect/owl/owlery_query_tools.py b/src/vfb_connect/owl/owlery_query_tools.py index 330545a7..5aaa678b 100644 --- a/src/vfb_connect/owl/owlery_query_tools.py +++ b/src/vfb_connect/owl/owlery_query_tools.py @@ -1,3 +1,4 @@ +from time import sleep import requests import re import json @@ -70,11 +71,16 @@ def query(self, query_type, return_type, if query_by_label: query = self.labels_2_ids(query) if verbose: - print("Running query: " + query) + print("Running query: " + query) payload = {'object': query, 'prefixes': json.dumps(self.curies), 'direct': direct} # print(payload) - r = requests.get(url=owl_endpoint, params=payload) + try: + r = requests.get(url=owl_endpoint, params=payload) + except requests.exceptions.RequestException as e: + print("\033[31mConnection Error:\033[0m " + str(e)) + sleep(15) + return query(query_type, return_type, query, query_by_label, direct, verbose) if verbose: print("Query URL: " + r.url) if r.status_code == 200: diff --git a/src/vfb_connect/schema/vfb_term.py b/src/vfb_connect/schema/vfb_term.py index 91f5ebfa..68fc1248 100644 --- a/src/vfb_connect/schema/vfb_term.py +++ b/src/vfb_connect/schema/vfb_term.py @@ -1572,7 +1572,7 @@ def __init__(self, id=None, term: Optional[Term] = None, related_terms: Optional from vfb_connect import vfb self.vfb = vfb self.debug = verbose - self._return_type = self.vfb._return_type # Default to global version but can be set to id, name (list) or full (VFBTerms) + self._return_type = self.vfb._return_type if hasattr(self.vfb, '_return_type') else "full" if id is not None: if isinstance(id, list): id = id[0] @@ -1681,7 +1681,6 @@ def __init__(self, id=None, term: Optional[Term] = None, related_terms: Optional self._instances = None self._instances_ids = None self._instances_names = None - self._return_type = self.vfb._return_type # Default to global version but can be set to id, name (list) or full (VFBTerms) self._skeleton = None self._mesh = None self._volume = None