From 5dc8f6790883dcb64df86e5552d863f48c66b1db Mon Sep 17 00:00:00 2001 From: Will-Cooper Date: Tue, 12 Mar 2024 21:15:34 +0000 Subject: [PATCH] add reference table to SIMPLEDB class super --- simple_app/app_simple.py | 34 +++++++++++++++++++--------------- simple_app/plots.py | 2 +- simple_app/tests/test_utils.py | 4 ++-- simple_app/utils.py | 22 ++++++++++++++-------- 4 files changed, 36 insertions(+), 26 deletions(-) diff --git a/simple_app/app_simple.py b/simple_app/app_simple.py index c8210c2..3d4e687 100644 --- a/simple_app/app_simple.py +++ b/simple_app/app_simple.py @@ -47,7 +47,7 @@ def search(): curdoc().template_variables['query'] = query # add query to bokeh curdoc curdoc().template_variables['ref_query'] = ref_query # add query to bokeh curdoc - db = SimpleDB(db_file, connection_arguments={'check_same_thread': False}) # open database + db = SimpleDB(db_file) # open database # object search function try: @@ -97,7 +97,7 @@ def coordinate_query(): query = '' curdoc().template_variables['query'] = query - db = SimpleDB(db_file, connection_arguments={'check_same_thread': False}) + db = SimpleDB(db_file) # parse query into ra, dec, radius ra, dec, radius = form.multi_param_str_parse(query) @@ -127,7 +127,7 @@ def full_text_search(): limmaxrows = True curdoc().template_variables['query'] = query - db = SimpleDB(db_file, connection_arguments={'check_same_thread': False}) + db = SimpleDB(db_file) # search through the tables using the given query results: Dict[str, pd.DataFrame] = db.search_string(query, fmt='pandas', verbose=False) @@ -150,7 +150,7 @@ def raw_query(): """ Page for raw sql query, returning all tables """ - db = SimpleDB(db_file, connection_arguments={'check_same_thread': False}) # open database + db = SimpleDB(db_file) # open database form = SQLForm(db_file=db_file) # main query form # checks that the SQL is valid, then submits form @@ -187,12 +187,16 @@ def solo_result(query: str): The query -- full match to a main ID """ curdoc().template_variables['query'] = query - db = SimpleDB(db_file, connection_arguments={'check_same_thread': False}) + db = SimpleDB(db_file) # search database for given object - resultdict: dict = db.inventory(query) - if not len(resultdict): + try: + resultdict: dict = db.inventory(query) + if not len(resultdict): + raise KeyError + except KeyError: abort(404, f'"{query}" does match any result in SIMPLE!') + return everything = Inventory(resultdict) # create camd and spectra plots @@ -278,7 +282,7 @@ def create_file_for_download(key: str): The dataframe string """ query = curdoc().template_variables['query'] - db = SimpleDB(db_file, connection_arguments={'check_same_thread': False}) + db = SimpleDB(db_file) # search for a given object and a given key resultdict: dict = db.inventory(query) @@ -300,7 +304,7 @@ def create_files_for_solo_download(): Creates and downloads all dataframes from solo results """ query = curdoc().template_variables['query'] - db = SimpleDB(db_file, connection_arguments={'check_same_thread': False}) + db = SimpleDB(db_file) # search for a given object resultdict: dict = db.inventory(query) @@ -323,7 +327,7 @@ def create_spectra_files_for_download(): Downloads the spectra files and zips together """ query = curdoc().template_variables['query'] - db = SimpleDB(db_file, connection_arguments={'check_same_thread': False}) + db = SimpleDB(db_file) # search for a given object and specifically its spectra resultdict: dict = db.inventory(query) @@ -347,7 +351,7 @@ def create_file_for_filtered_download(): """ query = curdoc().template_variables['query'] refquery = curdoc().template_variables['ref_query'] - db = SimpleDB(db_file, connection_arguments={'check_same_thread': False}) + db = SimpleDB(db_file) # search for a given object and a full text search at the same time results: Optional[pd.DataFrame] = db.search_object(query, fmt='pandas') @@ -370,7 +374,7 @@ def create_file_for_coordinate_download(): Creates and downloads the shown dataframe when in coordinate search """ query = curdoc().template_variables['query'] - db = SimpleDB(db_file, connection_arguments={'check_same_thread': False}) + db = SimpleDB(db_file) # query the database for given coordinates and parse those coordinates form = CoordQueryForm(db_file=db_file) @@ -396,7 +400,7 @@ def create_file_for_full_download(key: str): The dataframe string """ query = curdoc().template_variables['query'] - db = SimpleDB(db_file, connection_arguments={'check_same_thread': False}) + db = SimpleDB(db_file) # search database with a free-text search and specific table resultdict: Dict[str, pd.DataFrame] = db.search_string(query, fmt='pandas', verbose=False) @@ -417,7 +421,7 @@ def create_files_for_multi_download(): Creates and downloads all dataframes from full results """ query = curdoc().template_variables['query'] - db = SimpleDB(db_file, connection_arguments={'check_same_thread': False}) + db = SimpleDB(db_file) # search with full-text search resultdict: Dict[str, pd.DataFrame] = db.search_string(query, fmt='pandas', verbose=False) @@ -434,7 +438,7 @@ def create_file_for_sql_download(): Creates and downloads the shown dataframe when in sql query """ query = curdoc().template_variables['query'] - db = SimpleDB(db_file, connection_arguments={'check_same_thread': False}) + db = SimpleDB(db_file) # query database via sql results: Optional[pd.DataFrame] = db.sql_query(query, fmt='pandas') diff --git a/simple_app/plots.py b/simple_app/plots.py index 1ac6d06..da5218c 100644 --- a/simple_app/plots.py +++ b/simple_app/plots.py @@ -129,7 +129,7 @@ def normalise() -> np.ndarray: return flux / fluxmed # query the database for the spectra - db = SimpleDB(db_file, connection_arguments={'check_same_thread': False}) # open database + db = SimpleDB(db_file) # open database t_spectra: Table = db.query(db.Spectra).\ filter(db.Spectra.c.source == query).\ table(spectra=['spectrum']) diff --git a/simple_app/tests/test_utils.py b/simple_app/tests/test_utils.py index 0bd20fb..8905f10 100644 --- a/simple_app/tests/test_utils.py +++ b/simple_app/tests/test_utils.py @@ -10,7 +10,7 @@ # local packages from ..utils import * -db_name = 'temp.db' +db_name = 'temp.sqlite' db_cs = f'sqlite:///{db_name}' @@ -18,7 +18,7 @@ def db(): if os.path.exists(db_name): os.remove(db_name) - copy('SIMPLE.db', db_name) + copy('SIMPLE.sqlite', db_name) assert os.path.exists(db_name) # Connect to the new database and confirm it has the Sources table db = SimpleDB(db_cs) diff --git a/simple_app/utils.py b/simple_app/utils.py index 9e04080..d1b6026 100644 --- a/simple_app/utils.py +++ b/simple_app/utils.py @@ -39,6 +39,12 @@ class SimpleDB(Database): # this keeps pycharm happy about unresolved reference SpectralTypes = None CompanionRelationships = None + def __init__(self, connection_string): + super().__init__(connection_string, + reference_tables=REFERENCE_TABLES, + connection_arguments={'check_same_thread': False}) + + class Inventory: """ @@ -276,7 +282,7 @@ def validate_query(self, field): """ # split search bar into 3 components (ra, dec, radius) - db = SimpleDB(self.db_file, connection_arguments={'check_same_thread': False}) # open database + db = SimpleDB(self.db_file) # open database ra, dec, radius = self.multi_param_str_parse(field.data) if not ra: # i.e. empty string, bad parse @@ -332,7 +338,7 @@ def validate_sqlfield(self, field): The data within the query form """ forbidden = ('update', 'drop', 'truncate', 'grant', 'commit', 'create', 'replace', 'alter', 'insert', 'delete') - db = SimpleDB(self.db_file, connection_arguments={'check_same_thread': False}) # open database + db = SimpleDB(self.db_file) # open database # check query field has data within if (query := field.data) is None or query.strip() == '': # content in main searchbar @@ -429,7 +435,7 @@ def get_all_sources(db_file: str): full_results The full dataframe of all the sources """ - db = SimpleDB(db_file, connection_arguments={'check_same_thread': False}) + db = SimpleDB(db_file) # get the full Sources table and just the main id list full_results: pd.DataFrame = db.query(db.Sources).pandas() @@ -451,7 +457,7 @@ def get_version(db_file: str) -> str: vstr The stringified version formatted """ - db = SimpleDB(db_file, connection_arguments={'check_same_thread': False}) + db = SimpleDB(db_file) # query Versions table and extract active version before pretty-printing v: pd.DataFrame = db.query(db.Versions).pandas() @@ -655,7 +661,7 @@ def get_all_photometry(db_file: str, photometric_filters: pd.DataFrame): all_bands: np.ndarray The unique passbands to create dropdowns by """ - db = SimpleDB(db_file, connection_arguments={'check_same_thread': False}) + db = SimpleDB(db_file) # query all photometry and extract the unique bands all_photometry: pd.DataFrame = db.query(db.Photometry).pandas() @@ -678,7 +684,7 @@ def get_all_parallaxes(db_file: str): all_parallax: pd.DataFrame The dataframe of all the parallaxes """ - db = SimpleDB(db_file, connection_arguments={'check_same_thread': False}) + db = SimpleDB(db_file) # query the database for the parallaxes and only take necessary columns all_parallaxes: pd.DataFrame = db.query(db.Parallaxes).pandas() @@ -695,7 +701,7 @@ def get_all_spectral_types(db_file: str): all_spts: pd.DataFrame The dataframe of all the spectral type numbers """ - db = SimpleDB(db_file, connection_arguments={'check_same_thread': False}) + db = SimpleDB(db_file) # query the database for the spectral types and only take necessary columns all_spts: pd.DataFrame = db.query(db.SpectralTypes).pandas() @@ -982,7 +988,7 @@ def get_filters(db_file: str) -> pd.DataFrame: phot_filters: pd.DataFrame All of the filters, access as: phot_filters.loc['effective_wavelength', ] """ - db = SimpleDB(db_file, connection_arguments={'check_same_thread': False}) + db = SimpleDB(db_file) # query the database for all of the PhotometryFilters phot_filters: pd.DataFrame = db.query(db.PhotometryFilters).pandas().set_index('band').T