Skip to content

Commit

Permalink
remove dependency of prenight on scheduler pickle
Browse files Browse the repository at this point in the history
  • Loading branch information
ehneilsen committed Dec 7, 2023
1 parent 413bdc1 commit bc199c3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 101 deletions.
67 changes: 2 additions & 65 deletions schedview/app/prenight/prenight.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import pandas as pd
import panel as pn
import param
import rubin_sim.scheduler.example
from astropy.time import Time
from rubin_sim.scheduler.model_observatory import ModelObservatory
from rubin_sim.utils import survey_start_mjd
Expand All @@ -40,7 +39,6 @@
]
DEFAULT_TIMEZONE = AVAILABLE_TIMEZONES[0]
DEFAULT_CURRENT_TIME = Time(max(Time.now().mjd, survey_start_mjd()), format="mjd")
USE_EXAMPLE_SCHEDULER = False
DEFAULT_MODEL_OBSERVATORY = ModelObservatory(init_load_length=1)
DEFAULT_MODEL_OBSERVATORY.sky_model.load_length = 1

Expand Down Expand Up @@ -95,17 +93,6 @@ class Prenight(param.Parameterized):
label="OpSim output database path or URL",
)

scheduler_fname_doc = """URL or file name of the scheduler pickle file.
Such a pickle file can either be of an instance of a subclass of
rubin_sim.scheduler.schedulers.CoreScheduler, or a tuple of the form
(scheduler, conditions), where scheduler is an instance of a subclass of
rubin_sim.scheduler.schedulers.CoreScheduler, and conditions is an
instance of rubin_sim.scheduler.conditions.Conditions."""
scheduler_fname = param.String(
doc=scheduler_fname_doc,
label="URL or file name of scheduler pickle file",
)

rewards_fname = param.String(
doc="URL or file name of the rewards HDF5 file.",
label="URL or file name of rewards HDF5 file",
Expand Down Expand Up @@ -170,9 +157,6 @@ class Prenight(param.Parameterized):
# and communication overhead is reduced.
_visits_cds = param.Parameter()

# An instance of rubin_sim.scheduler.schedulers.CoreScheduler
_scheduler = param.Parameter()

_almanac_events = schedview.param.DataFrame(
None,
doc="Events from the rubin_sim alamanc",
Expand Down Expand Up @@ -422,24 +406,6 @@ def custom_hvplot3(self):
def custom_hvplot4(self):
return self.hvplot(4)

@param.depends("scheduler_fname", watch=True)
def _update_scheduler(self):
self.logger.info("Starting to update the scheduler.")
try:
(
scheduler,
conditions,
) = schedview.collect.scheduler_pickle.read_scheduler(self.scheduler_fname)

self._scheduler = scheduler
self.logger.info("Finished updating the scheduler.")
except Exception as e:
self.logger.error(f"Could not load scheduler from {self.scheduler_fname} {e}")
if USE_EXAMPLE_SCHEDULER:
self.logger.info("Starting to load example scheduler.")
self._scheduler = rubin_sim.scheduler.example.example_scheduler(nside=self._nside)
self.logger.info("Finished loading example scheduler.")

@param.depends(
"_visits_cds",
"_almanac_events",
Expand Down Expand Up @@ -543,7 +509,6 @@ def horizon_map(self):
return fig

@param.depends(
"_scheduler",
"_visits",
)
def visit_skymaps(self):
Expand All @@ -558,14 +523,10 @@ def visit_skymaps(self):
if self._visits is None:
return "No visits are loaded."

if self._scheduler is None:
return "No scheduler is loaded."

self.logger.info("Starting to update visit skymaps")

vmap, vmap_data = schedview.plot.visitmap.create_visit_skymaps(
visits=self._visits,
scheduler=self._scheduler,
night_date=self.night,
timezone=self.timezone,
observatory=self._observatory,
Expand Down Expand Up @@ -799,7 +760,6 @@ def make_app(
self,
night_date=None,
opsim_db=None,
scheduler=None,
rewards=None,
shown_tabs=None,
custom_hvplot_tab_settings_file=None,
Expand All @@ -812,8 +772,6 @@ def make_app(
The date of the night to display.
opsim_db : `str`, optional
Path to the opsim output database file.
scheduler : `str`, optional
Path to the scheduler pickle file.
rewards : `str`, optional
Path to the rewards hdf5 file.
shown_tabs : `list` [`str`], optional
Expand All @@ -834,9 +792,6 @@ def make_app(
if night_date is not None:
self.night = night_date

if scheduler is not None:
self.scheduler_fname = scheduler

if opsim_db is not None:
self.opsim_output_fname = opsim_db

Expand All @@ -857,7 +812,6 @@ def make_app(
parameters=[
"night",
"timezone",
"scheduler_fname",
"opsim_output_fname",
],
name="<h2>Parameters</h2>",
Expand Down Expand Up @@ -896,19 +850,12 @@ class RestrictedInputPrenight(Prenight):
path=f"{PACKAGE_DATA_DIR}/*opsim*.db", label="OpSim output database", default=None, allow_None=True
)

scheduler_fname = schedview.param.FileSelectorWithEmptyOption(
path=f"{PACKAGE_DATA_DIR}/*scheduler*.p*",
label="Scheduler pickle file",
default=None,
allow_None=True,
)

rewards_fname = schedview.param.FileSelectorWithEmptyOption(
path=f"{PACKAGE_DATA_DIR}/*rewards*.h5", label="rewards HDF5 file", default=None, allow_None=True
)

def __init__(self, data_dir=None, **kwargs):
# A few arguments (scheduler, opsim_db, rewards) will be used
# A few arguments (opsim_db, rewards) will be used
# later in this method to set the options for parameters, but
# are not themselves parameters. So, remove them them the
# params list before calling super().__init__().
Expand All @@ -920,7 +867,6 @@ def __init__(self, data_dir=None, **kwargs):
# they can be updated by key.
fname_params = {
"opsim_db": self.param["opsim_output_fname"],
"scheduler": self.param["scheduler_fname"],
"reward": self.param["rewards_fname"],
}

Expand All @@ -930,7 +876,6 @@ def __init__(self, data_dir=None, **kwargs):
if data_dir is not None:
fname_glob = {
"opsim_db": f"{data_dir}/*opsim*.db",
"scheduler": f"{data_dir}/*scheduler*.p*",
"reward": f"{data_dir}/*rewards*.h5",
}

Expand Down Expand Up @@ -960,7 +905,7 @@ def prenight_app(*args, **kwargs):
data_dir = None

specified_data_files = {}
data_args = set(["opsim_db", "scheduler", "rewards"]) & set(kwargs.keys())
data_args = set(["opsim_db", "rewards"]) & set(kwargs.keys())

Check warning on line 908 in schedview/app/prenight/prenight.py

View check run for this annotation

Codecov / codecov/patch

schedview/app/prenight/prenight.py#L908

Added line #L908 was not covered by tests
for data_arg in data_args:
# Fully resolve the path to the file.
file_path = Path(kwargs[data_arg]).resolve()
Expand Down Expand Up @@ -1001,14 +946,6 @@ def parse_prenight_args():
help="The path to the OpSim output database.",
)

parser.add_argument(
"--scheduler",
"-s",
type=str,
default=None,
help="The path to the scheduler pickle file.",
)

parser.add_argument(
"--rewards",
"-r",
Expand Down
30 changes: 5 additions & 25 deletions schedview/collect/footprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,12 @@
from rubin_sim.scheduler.utils.sky_area import SkyAreaGenerator


def get_footprint(scheduler=None):
"""Extract the footprint from a survey."""
# This is a hack, good enough for example code, not for production.
def get_footprint(nside=32):

Check warning on line 5 in schedview/collect/footprint.py

View check run for this annotation

Codecov / codecov/patch

schedview/collect/footprint.py#L5

Added line #L5 was not covered by tests
"""Get the survey footprint."""

# Look through the scheduler to find a blob survey that has a footprint
# basis function
footprint = None
if scheduler is None:
nside = 32
else:
nside = scheduler.nside
for survey_tier in scheduler.survey_lists:
for survey in survey_tier:
if survey.__class__.__name__ in ["GreedySurvey"]:
for basis_function in survey.basis_functions:
bf_class = basis_function.__class__
if bf_class.__name__.startswith("Footprint"):
if footprint is None:
footprint = np.sum(basis_function.footprint.footprints, axis=0)
else:
footprint += np.sum(basis_function.footprint.footprints, axis=0)

if footprint is None:
sky_area_generator = SkyAreaGenerator(nside=nside)
band_footprints, _ = sky_area_generator.return_maps()
footprint = np.sum(band_footprints[b] for b in band_footprints.dtype.fields.keys())
sky_area_generator = SkyAreaGenerator(nside=nside)
band_footprints, _ = sky_area_generator.return_maps()

Check warning on line 9 in schedview/collect/footprint.py

View check run for this annotation

Codecov / codecov/patch

schedview/collect/footprint.py#L8-L9

Added lines #L8 - L9 were not covered by tests
footprint = np.sum(band_footprints[b] for b in band_footprints.dtype.fields.keys())

footprint[footprint == 0] = np.nan
return footprint
14 changes: 5 additions & 9 deletions schedview/plot/visitmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,8 @@ def plot_visit_planisphere(

def create_visit_skymaps(
visits,
scheduler,
night_date,
nside=32,
observatory=None,
timezone="Chile/Continental",
planisphere_only=False,
Expand All @@ -418,12 +418,11 @@ def create_visit_skymaps(
If a string, the file name of the opsim database from which the
visits should be loaded.
scheduler : `CoreScheduler` or `str`
The scheduler from which to extract the footprint, or the name of
a file from which such a scheduler should be loaded.
night_date : `datetime.date`
The calendar date of the evening of the night for which
to plot the visits.
nside : `int`, optional
The healpix nside to use for the map.
observatory : `ModelObservatory`, optional
Provides the location of the observatory, used to compute
night start and end times.
Expand Down Expand Up @@ -455,14 +454,11 @@ def create_visit_skymaps(
if end_time is not None:
visits.query(f"observationStartMJD <= {Time(end_time).mjd}", inplace=True)

if isinstance(scheduler, str):
scheduler, conditions = schedview.collect.scheduler_pickle.read_scheduler(scheduler)

if observatory is None:
observatory = ModelObservatory(nside=scheduler.nside, init_load_length=1)
observatory = ModelObservatory(nside=nside, init_load_length=1)

Check warning on line 458 in schedview/plot/visitmap.py

View check run for this annotation

Codecov / codecov/patch

schedview/plot/visitmap.py#L458

Added line #L458 was not covered by tests
observatory.sky_model.load_length = 1

footprint = schedview.collect.footprint.get_footprint(scheduler)
footprint = schedview.collect.footprint.get_footprint(nside)

Check warning on line 461 in schedview/plot/visitmap.py

View check run for this annotation

Codecov / codecov/patch

schedview/plot/visitmap.py#L461

Added line #L461 was not covered by tests
observatory.mjd = end_time.mjd
conditions = observatory.return_conditions()
data = {"visits": visits, "footprint": footprint, "conditions": conditions}
Expand Down
2 changes: 0 additions & 2 deletions tests/test_prenight.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def test_prenight_app(self):
sample_data_dir = importlib.resources.files("schedview").joinpath("data")

sample_opsim_db = str(sample_data_dir.joinpath("sample_opsim.db"))
sample_scheduler_pickle = str(sample_data_dir.joinpath("sample_scheduler.pickle.xz"))
sample_rewards_h5 = str(sample_data_dir.joinpath("sample_rewards.h5"))

# Use a separate test custom settings file from the sample, because
Expand All @@ -24,7 +23,6 @@ def test_prenight_app(self):

app = prenight_app(
opsim_db=sample_opsim_db,
scheduler=sample_scheduler_pickle,
rewards=sample_rewards_h5,
custom_hvplot_tab_settings_file=custom_hvplot_tabs,
)
Expand Down

0 comments on commit bc199c3

Please sign in to comment.