Skip to content

Commit

Permalink
IntRounded throw an error if it gets a NaN
Browse files Browse the repository at this point in the history
  • Loading branch information
yoachim committed Dec 5, 2023
1 parent 39142d5 commit a13cc1e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
19 changes: 11 additions & 8 deletions rubin_scheduler/scheduler/basis_functions/basis_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,12 +874,15 @@ def __init__(self, nside=None, max_airmass=2.5, penalty=np.nan):

def _calc_value(self, conditions, indx=None):
result = self.result.copy()
valid_airmass = np.isfinite(conditions.airmass)

Check warning on line 877 in rubin_scheduler/scheduler/basis_functions/basis_functions.py

View check run for this annotation

Codecov / codecov/patch

rubin_scheduler/scheduler/basis_functions/basis_functions.py#L877

Added line #L877 was not covered by tests
good_pix = np.where(
(conditions.airmass >= 1.0)
& (IntRounded(conditions.airmass) < self.max_airmass)
& (IntRounded(np.abs(conditions.az_to_sun)) < IntRounded(np.pi / 2.0))
(conditions.airmass[valid_airmass] >= 1.0)
& (IntRounded(conditions.airmass[valid_airmass]) < self.max_airmass)
& (IntRounded(np.abs(conditions.az_to_sun[valid_airmass])) < IntRounded(np.pi / 2.0))
)
result[valid_airmass][good_pix] = (

Check warning on line 883 in rubin_scheduler/scheduler/basis_functions/basis_functions.py

View check run for this annotation

Codecov / codecov/patch

rubin_scheduler/scheduler/basis_functions/basis_functions.py#L883

Added line #L883 was not covered by tests
conditions.airmass[valid_airmass][good_pix] / self.max_airmass.initial
)
result[good_pix] = conditions.airmass[good_pix] / self.max_airmass.initial
return result


Expand Down Expand Up @@ -921,15 +924,15 @@ def _calc_value(self, conditions, indx=None):
if indx is None:
indx = np.arange(result.size)
diff = conditions.mjd - self.survey_features["Last_observed"].feature[indx]
mask = np.isnan(diff)

Check warning on line 927 in rubin_scheduler/scheduler/basis_functions/basis_functions.py

View check run for this annotation

Codecov / codecov/patch

rubin_scheduler/scheduler/basis_functions/basis_functions.py#L926-L927

Added lines #L926 - L927 were not covered by tests
# remove NaNs from diff, but save mask so we exclude those values later.
diff[mask] = 0.0
ir_diff = IntRounded(diff)

Check warning on line 930 in rubin_scheduler/scheduler/basis_functions/basis_functions.py

View check run for this annotation

Codecov / codecov/patch

rubin_scheduler/scheduler/basis_functions/basis_functions.py#L929-L930

Added lines #L929 - L930 were not covered by tests
# Note! There is something afoot where different platforms evaluate
# nan's differently. Thus adding in the (~np.isnan(diff) term
# to make sure things are evaluated correctly.
good = np.where(
(ir_diff >= self.gap_min)
& (ir_diff <= self.gap_max)
& (self.survey_features["Pair_in_night"].feature[indx] < self.npairs)
& (~np.isnan(diff))
& (~mask)
)[0]
result[indx[good]] += 1.0
return result
Expand Down
13 changes: 7 additions & 6 deletions rubin_scheduler/scheduler/schedulers/core_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,13 @@ def request_observation(self, mjd=None):
)
obs_pa = _approx_altaz2pa(alt, az, self.conditions.site.latitude_rad)
rot_tel_pos_expected = (obs_pa - observation["rotSkyPos"]) % (2.0 * np.pi)
if (IntRounded(rot_tel_pos_expected) > IntRounded(self.rotator_limits[0])) & (
IntRounded(rot_tel_pos_expected) < IntRounded(self.rotator_limits[1])
):
diff = np.abs(self.rotator_limits - rot_tel_pos_expected)
limit_indx = np.min(np.where(diff == np.min(diff))[0])
observation["rotSkyPos"] = (obs_pa - self.rotator_limits[limit_indx]) % (2.0 * np.pi)
if np.isfinite(observation["rotSkyPos"]):
if (IntRounded(rot_tel_pos_expected) > IntRounded(self.rotator_limits[0])) & (

Check warning on line 244 in rubin_scheduler/scheduler/schedulers/core_scheduler.py

View check run for this annotation

Codecov / codecov/patch

rubin_scheduler/scheduler/schedulers/core_scheduler.py#L243-L244

Added lines #L243 - L244 were not covered by tests
IntRounded(rot_tel_pos_expected) < IntRounded(self.rotator_limits[1])
):
diff = np.abs(self.rotator_limits - rot_tel_pos_expected)
limit_indx = np.min(np.where(diff == np.min(diff))[0])
observation["rotSkyPos"] = (obs_pa - self.rotator_limits[limit_indx]) % (2.0 * np.pi)

Check warning on line 249 in rubin_scheduler/scheduler/schedulers/core_scheduler.py

View check run for this annotation

Codecov / codecov/patch

rubin_scheduler/scheduler/schedulers/core_scheduler.py#L247-L249

Added lines #L247 - L249 were not covered by tests
return observation

def _fill_queue(self):
Expand Down
5 changes: 5 additions & 0 deletions rubin_scheduler/scheduler/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ class IntRounded:
Class to help force comparisons be made on scaled up integers,
preventing machine precision issues cross-platforms
Note that casting a NaN to an int can have different behaviours
cross-platforms, so will throw an error if attempted.
Parameters
----------
inval : number-like thing
Expand All @@ -81,6 +84,8 @@ class IntRounded:
"""

def __init__(self, inval, scale=1e5):
if np.any(~np.isfinite(inval)):
raise ValueError("IntRounded can only take finite values.")

Check warning on line 88 in rubin_scheduler/scheduler/utils/utils.py

View check run for this annotation

Codecov / codecov/patch

rubin_scheduler/scheduler/utils/utils.py#L87-L88

Added lines #L87 - L88 were not covered by tests
self.initial = inval
self.value = np.round(inval * scale).astype(int)
self.scale = scale
Expand Down

0 comments on commit a13cc1e

Please sign in to comment.