Skip to content

Commit

Permalink
add reference table to SIMPLEDB class super
Browse files Browse the repository at this point in the history
  • Loading branch information
Will-Cooper committed Mar 12, 2024
1 parent 7017846 commit 5dc8f67
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 26 deletions.
34 changes: 19 additions & 15 deletions simple_app/app_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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')
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion simple_app/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
4 changes: 2 additions & 2 deletions simple_app/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
# local packages
from ..utils import *

db_name = 'temp.db'
db_name = 'temp.sqlite'
db_cs = f'sqlite:///{db_name}'


@pytest.fixture(scope='module')
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)
Expand Down
22 changes: 14 additions & 8 deletions simple_app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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', <bandname>]
"""
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
Expand Down

0 comments on commit 5dc8f67

Please sign in to comment.