Skip to content

PriorityScheduler: Update _get_filled_indices() to avoid 'block already occupied' ValueError#550

Open
michaelbaisch wants to merge 4 commits intoastropy:mainfrom
michaelbaisch:fix_block_already_occupied
Open

PriorityScheduler: Update _get_filled_indices() to avoid 'block already occupied' ValueError#550
michaelbaisch wants to merge 4 commits intoastropy:mainfrom
michaelbaisch:fix_block_already_occupied

Conversation

@michaelbaisch
Copy link
Contributor

Previously, for blocks with a duration equal to the time_resolution, _get_filled_indices() was unable to identify overlapping slots. This was due to the comparison of start times not accounting for these specific cases.

This has been addressed by adjusting the start_time comparison to be slightly earlier (0.5 seconds). This change enables the correct identification of filled time slots for blocks with minimal duration, thus preventing the 'block already occupied' error.

Fixes #368

Comment on lines +713 to +714
filled = np.where((start_end[0]-0.5*u.second < times) & (times < start_end[1]))
is_open_time[filled[0]] = False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd bet that fixing 0.5 s is fine in most cases, but maybe not all cases. I think it would be more correct and safer to raise a warning when np.diff(start_end) <= np.min(np.diff(times)). What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Including such a warning is probably good. Currently, I can't imagine a scenario where both time_resolution and duration are less than 1 second, which is why I opted to simply halve it. Consider revising to something like:

filled = np.where((start_end[0]-0.05*u.second <= times) & (times < start_end[1]))

Because in general I think we should mark "as filled" on one side when equal. Given that this is likely akin to floating point comparisons, subtracting a tolerance here seems logical. The addition of "<=" may provide further clarity to our intentions.

Copy link
Contributor

@bmorris3 bmorris3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add a changelog entry.

if len(filled[0]) > 0:
is_open_time[filled[0]] = False
is_open_time[min(filled[0]) - 1] = False
filled = np.where((start_end[0]-0.5*u.second < times) & (times < start_end[1]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
filled = np.where((start_end[0]-0.5*u.second < times) & (times < start_end[1]))
if np.diff(start_end) <= np.min(np.diff(times)):
warnings.warn(
"Unexpected behavior may occur when the time "
f"resolution ({np.min(np.diff(times))}) "
"is not smaller than the block duration "
f"({np.diff(start_end)}).", AstroplanWarning
)
filled = np.where((start_end[0]-0.5*u.second < times) & (times < start_end[1]))

If you accept this change, you'll also need to add the following import to this file:

from .exceptions import AstroplanWarning

michaelbaisch added a commit to michaelbaisch/astroplan that referenced this pull request Sep 21, 2023
michaelbaisch added a commit to michaelbaisch/astroplan that referenced this pull request Sep 21, 2023
@michaelbaisch michaelbaisch force-pushed the fix_block_already_occupied branch from 3e874d2 to ecca3b5 Compare September 21, 2023 08:22
…dy occupied' ValueError

Previously, for blocks with a duration equal to the time_resolution, _get_filled_indices()
was unable to identify overlapping slots. This was due to the comparison of start times not
accounting for these specific cases.

This has been addressed by adjusting the start_time comparison to be slightly earlier (0.5 seconds).
This change enables the correct identification of filled time slots for blocks with minimal duration,
thus preventing the 'block already occupied' error.
@michaelbaisch michaelbaisch force-pushed the fix_block_already_occupied branch from c28a852 to 7b015be Compare February 2, 2026 09:18
@codecov
Copy link

codecov bot commented Feb 2, 2026

Codecov Report

❌ Patch coverage is 85.71429% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 72.89%. Comparing base (a4d89a0) to head (7b015be).

Files with missing lines Patch % Lines
astroplan/scheduling.py 85.71% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #550      +/-   ##
==========================================
- Coverage   72.90%   72.89%   -0.02%     
==========================================
  Files          14       14              
  Lines        1849     1852       +3     
==========================================
+ Hits         1348     1350       +2     
- Misses        501      502       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ValueError: block already occupied

2 participants