Skip to content

Commit 58ba11d

Browse files
authored
Updating docs to use the term lookup tables instead of reference tables (#102)
* Updating documentation to use the term lookup tables instead of reference tables * Renaming reference to lookup in the code base, deprecating reference_tables argument * ignoring felis import lint warning
1 parent 237bf8d commit 58ba11d

File tree

3 files changed

+32
-27
lines changed

3 files changed

+32
-27
lines changed

astrodbkit/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# Global variables
2323

2424
# These describe the various database tables and their links
25-
REFERENCE_TABLES = [
25+
LOOKUP_TABLES = [
2626
"Publications",
2727
"Telescopes",
2828
"Instruments",
@@ -39,8 +39,10 @@
3939
"CompanionList",
4040
"SourceTypeList",
4141
]
42+
REFERENCE_TABLES = LOOKUP_TABLES # prior name, for backwards compatibility
4243
# REFERENCE_TABLES is a list of tables that do not link to the primary table.
4344
# These are treated separately from the other data tables that are all assumed to be linked to the primary table.
45+
# There are also known as lookup tables.
4446
PRIMARY_TABLE = "Sources" # the primary table used for storing objects
4547
PRIMARY_TABLE_KEY = "source" # the name of the primary key in the primary table; this is used for joining tables
4648
FOREIGN_KEY = "source" # the name of the foreign key in other tables that refer back to the primary

astrodbkit/astrodb.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from sqlalchemy.schema import CreateSchema
2222
from tqdm import tqdm
2323

24-
from . import FOREIGN_KEY, PRIMARY_TABLE, PRIMARY_TABLE_KEY, REFERENCE_TABLES
24+
from . import FOREIGN_KEY, PRIMARY_TABLE, PRIMARY_TABLE_KEY, LOOKUP_TABLES
2525
from .spectra import load_spectrum
2626
from .utils import datetime_json_parser, deprecated_alias, get_simbad_names, json_serializer
2727

@@ -210,8 +210,8 @@ def create_database(connection_string, drop_tables=False, felis_schema=None):
210210

211211
if felis_schema is not None:
212212
# Felis loader requires felis_schema
213-
from felis.datamodel import Schema
214-
from felis.metadata import MetaDataBuilder
213+
from felis.datamodel import Schema # noqa: PLC0415
214+
from felis.metadata import MetaDataBuilder # noqa: PLC0415
215215

216216
# Load and validate the felis-formatted schema
217217
data = yaml.safe_load(open(felis_schema, "r"))
@@ -306,10 +306,11 @@ def copy_database_schema(
306306
class Database:
307307
"""Database handler class"""
308308

309+
@deprecated_alias(reference_tables="lookup_tables")
309310
def __init__(
310311
self,
311312
connection_string,
312-
reference_tables=REFERENCE_TABLES,
313+
lookup_tables=LOOKUP_TABLES,
313314
primary_table=PRIMARY_TABLE,
314315
primary_table_key=PRIMARY_TABLE_KEY,
315316
foreign_key=FOREIGN_KEY,
@@ -325,9 +326,9 @@ def __init__(
325326
----------
326327
connection_string : str
327328
Connection string to establish a database connection
328-
reference_tables : list
329-
List of reference tables; these are treated separately from data tables.
330-
Default: ['Publications', 'Telescopes', 'Instruments']
329+
lookup_tables : list
330+
List of lookup tables; these are treated separately from data tables as they represent many-to-many relationships (eg, filter or telescope names).
331+
See __init__.LOOKUP_TABLES for the default.
331332
primary_table : str
332333
Name of the primary source table. Default: Sources
333334
primary_table_key : str
@@ -369,7 +370,7 @@ def __init__(
369370
with self.engine.connect() as conn:
370371
self.metadata.reflect(conn)
371372

372-
self._reference_tables = reference_tables
373+
self._lookup_tables = lookup_tables
373374
self._primary_table = primary_table
374375
self._primary_table_key = primary_table_key
375376
self._foreign_key = foreign_key
@@ -482,7 +483,7 @@ def inventory(self, name, pretty_print=False):
482483
# Loop over tables (not reference tables) and gather the information. Start with the primary table, though
483484
self._inventory_query(data_dict, self._primary_table, name)
484485
for table in self.metadata.tables:
485-
if table in self._reference_tables + [self._primary_table]:
486+
if table in self._lookup_tables + [self._primary_table]:
486487
continue
487488
self._inventory_query(data_dict, table, name)
488489

@@ -851,7 +852,7 @@ def save_database(self, directory: str, clear_first: bool=True, reference_direct
851852

852853
# Output reference tables
853854
print(f"Storing reference tables to {os.path.join(directory, reference_directory)}...")
854-
for table in self._reference_tables:
855+
for table in self._lookup_tables:
855856
# Skip reference tables that are not actually in the database
856857
if table not in self.metadata.tables.keys():
857858
continue
@@ -995,7 +996,7 @@ def load_database(self, directory: str, verbose: bool=False, reference_directory
995996
conn.execute(self.metadata.tables[table.name].delete())
996997

997998
# Load reference tables first
998-
for table in self._reference_tables:
999+
for table in self._lookup_tables:
9991000
if verbose:
10001001
print(f"Loading {table} table")
10011002
# Check if the reference table is in the sub-directory
@@ -1018,7 +1019,7 @@ def load_database(self, directory: str, verbose: bool=False, reference_directory
10181019
for file in tqdm(os.listdir(directory_of_sources)):
10191020
# Skip reference tables
10201021
core_name = file.replace(".json", "")
1021-
if core_name in self._reference_tables:
1022+
if core_name in self._lookup_tables:
10221023
continue
10231024

10241025
# Skip non-JSON files or hidden files

docs/index.rst

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ For example, the `SIMPLE database <https://github.com/SIMPLE-AstroDB/SIMPLE-db>`
2222

2323
- the primary Sources table, with coordinate information for each target
2424
- several object data tables, like Photometry, Spectra, etc, that contain information for each target
25-
- reference tables, like Publications, Telescopes, etc, that list other information that is used throughout the database, but doesn't refer to a particular target
25+
- lookup tables, like Publications, Telescopes, etc, that list other information that is used throughout the database, but doesn't refer to a particular target. These are sometimes referred to as lookup or reference tables.
2626

2727
The goal of **AstrodbKit** is to link together the object tables together in order
28-
to express them as a single entity, while still retaining the information for other reference tables.
28+
to express them as a single entity, while still retaining the information for other lookup tables.
2929
**AstrodbKit** can read and write out an entire target's data as a single JSON file for ease of transport and version
30-
control. Reference tables are also written as JSON files, but organized differently-
30+
control. Lookup tables are also written as JSON files, but organized differently-
3131
a single file per table with multiple records.
3232
An **AstrodbKit**-supported database can thus be exported to two types of JSON files:
33-
individual target files and reference table files
33+
individual target files and lookup table files
3434
If your database is constructed in a similar fashion, it will work well with **AstrodbKit**.
3535
Other databases can still benefit from some of the functionality of **AstrodbKit**,
3636
but they might not work properly if attempting to use the save/load methods.
@@ -90,30 +90,32 @@ then initialize the database with the :py:class:`astrodbkit.astrodb.Database()`
9090
The database is now read to be used. If the database is empty, see below how to populate it.
9191

9292
.. note:: The :py:class:`astrodbkit.astrodb.Database()` class has many parameters that can be set to
93-
control the names of primary/reference tables. By default, these match the SIMPLE database, but users can
93+
control the names of primary/lookup tables. By default, these match the SIMPLE database, but users can
9494
configure them for their own needs and can pass them here or modify their __init__.py file.
9595

9696

9797
When using PostgreSQL databases, it may be useful to pass along connection_arguments that specify the schema to use. For example::
9898

9999
CONNECTION_STRING = "postgresql+psycopg2://user:password@server:port/database"
100-
REFERENCE_TABLES = [
100+
LOOKUP_TABLES = [
101101
"Publications",
102102
"Surveys",
103103
]
104104

105105
db = Database(CONNECTION_STRING,
106-
reference_tables=REFERENCE_TABLES,
106+
lookup_tables=LOOKUP_TABLES,
107107
connection_arguments={'options': '-csearch_path=my_schema'}
108108
)
109109
# This will use my_schema as the default schema for this connection
110110

111+
.. note:: For historical reasons, lookup tables are referred internally as reference tables.
112+
111113
Loading the Database
112114
--------------------
113115

114116
**Astrodbkit2** contains methods to output the full contents of the database as a list of JSON files.
115117
It can likewise read in a directory of these files to populate the database.
116-
By default, reference tables (eg, Publications, Telescopes, etc) and source tables are respectively stored in `reference/` and `source/` sub-directories of `data/`.
118+
By default, lookup tables (eg, Publications, Telescopes, etc) and source tables are respectively stored in `reference/` and `source/` sub-directories of `data/`.
117119
This is how SIMPLE is currently version controlled.
118120

119121
To load a database of this form, do the following::
@@ -271,7 +273,7 @@ Full String Search
271273
Similar to the Identifier Search above, one can perform a case-insensitive search for
272274
any string against every string column in the database with :py:meth:`~astrodbkit.astrodb.Database.search_string`.
273275
The output is a dictionary with keys for each table that matched results.
274-
This can be useful to find all results matching a particular reference regardless of table::
276+
This can be useful to find all results matching a particular publication regardless of table::
275277

276278
db.search_string('twa') # search for any records with 'twa' anywhere in the database
277279
db.search_string('Cruz18', fuzzy_search=False) # search for strings exactly matching Cruz19 anywhere in the database
@@ -438,8 +440,8 @@ Saving the Database
438440
===================
439441

440442
If users perform changes to a database, they will want to output this to disk to be version controlled.
441-
**Astrodbkit** provides methods to save an individual source or reference table as well as all of the data stored in the database.
442-
By default, reference tables are stored in a sub-directory of `data/` called "reference"; this can be overwritten by
443+
**Astrodbkit** provides methods to save an individual source or lookup table as well as all of the data stored in the database.
444+
By default, lookup/reference tables are stored in a sub-directory of `data/` called "reference"; this can be overwritten by
443445
supplying a `reference_directory` variable into `save_database` or `save_reference_table`.
444446
Similarly, source/object tables are stored in a sub-directory of `data/` called "source" which can be overwritten by supplying a `source_directory` variable.
445447

@@ -448,7 +450,7 @@ We recommend using `save_database` as that outputs the entire database contents
448450
# Save single object
449451
db.save_json('2MASS J13571237+1428398', 'data')
450452

451-
# Save single reference table
453+
# Save single lookup/reference table
452454
db.save_reference_table('Publications', 'data')
453455

454456
# Save entire database to directory 'data/' with 'reference/' and 'source/' subdirectories.
@@ -517,7 +519,7 @@ Here we provide useful tips or guidance when working with **AstrodbKit**.
517519
Handling Relationships Between Object Tables
518520
--------------------------------------------
519521

520-
Becuase **AstrodbKit** expects a single primary table, object tables that point back to it, and any number of reference tables, it can be difficult to handle relationships between object tables.
522+
Becuase **AstrodbKit** expects a single primary table, object tables that point back to it, and any number of lookup/reference tables, it can be difficult to handle relationships between object tables.
521523

522524
As an example, consider the scenario where you want to store companion information to your sources, such as a table to store the relationship with orbital separation and a separate one to store general parameters.
523525
You may be calling these CompanionRelationship and CompanionParameters, respectively.
@@ -527,7 +529,7 @@ You might find it attempting to load CompanionParameters only to find that Compa
527529

528530
The better approach is to define a lookup table that will store the companion identifiers which will be used as the foreign keys.
529531
For example, a CompanionList table that both CompanionParameters and CompanionRelationship can refer to.
530-
This would be a reference table, similar to Telescopes or Publications, while CompanionParameters and CompanionRelationship would be object tables that require tying back to a specific source in the Sources table.
532+
This would be a lookup table, similar to Telescopes or Publications, while CompanionParameters and CompanionRelationship would be object tables that require tying back to a specific source in the Sources table.
531533
Essentially, this is normalizing the database a bit further and serves to avoid some common issues with foreign keys.
532534

533535
Reference/API

0 commit comments

Comments
 (0)