diff --git a/src/vfb_connect/cross_server_tools.py b/src/vfb_connect/cross_server_tools.py index 21b83a57..c76c7190 100644 --- a/src/vfb_connect/cross_server_tools.py +++ b/src/vfb_connect/cross_server_tools.py @@ -1416,7 +1416,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 215a27bc..f42cf7a3 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: