Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tickets/SP-1723: improve support for using data from consdb #115

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion schedview/collect/consdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def read_consdb(
# right dtypes, so use ObservationArray()[0:0] instead to make the
# empty recarray with the correct dtypes.
visit_records: np.recarray = (
consdb_visits.opsim.to_records() if len(consdb_visits.opsim) > 0 else ObservationArray()[0:0]
consdb_visits.merged_opsim_consdb.to_records()
if len(consdb_visits.opsim) > 0
else ObservationArray()[0:0]
)
for stacker in stackers:
visit_records = stacker.run(visit_records)
Expand Down
1 change: 0 additions & 1 deletion schedview/collect/visits.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
NIGHT_STACKERS = [
maf.HourAngleStacker(),
maf.stackers.ObservationStartDatetime64Stacker(),
maf.stackers.TeffStacker(),
maf.stackers.OverheadStacker(),
maf.stackers.DayObsISOStacker(),
]
Expand Down
23 changes: 20 additions & 3 deletions schedview/compute/maf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sqlite3
from pathlib import Path
from tempfile import TemporaryDirectory

Expand All @@ -10,11 +11,27 @@

def _visits_to_opsim(visits, opsim):
# Take advantage of the schema migration code in SchemaConverter to make
# sure we have the correct column names

# sure we have up to date column names.
schema_converter = SchemaConverter()
obs = schema_converter.opsimdf2obs(visits)
schema_converter.obs2opsim(obs, filename=opsim)
updated_opsim = schema_converter.obs2opsim(obs)

# We can't just use the update opsim as is, because it might drop columns
# we want. Instead, merge the results back into the visits passed to us.
restored_columns = set(visits.columns) - set(updated_opsim.columns)
restored_columns.add("observationId")
merged_opsim = updated_opsim.merge(
visits.loc[:, list(restored_columns)], on="observationId", suffixes=("", "_orig")
)

# If the round trip change actually changes values in an existing column
# without changing the names, the merge might not work correctly. If
# this happens, the default "inner join" performed by DataFrame.merge
# will drop visits. Double check that this hasn't happened.
assert len(merged_opsim) == len(visits)

with sqlite3.connect(opsim) as con:
merged_opsim.to_sql("observations", con)


def compute_metric(visits, metric_bundle):
Expand Down
Loading