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

Graceful failing with seeking errors #1712

Merged
merged 3 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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: 1 addition & 3 deletions sleap/nn/data/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,7 @@ def make_dataset(self) -> tf.data.Dataset:
grid in order to properly map points to image coordinates.
"""
# Grab an image to test for the dtype.
test_image = tf.convert_to_tensor(
self.video.get_frame(self.video.last_frame_idx)
)
test_image = tf.convert_to_tensor(self.video.get_frame(0))
Copy link

Choose a reason for hiding this comment

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

Using 0 as the argument for self.video.get_frame to fetch a test image is a straightforward approach to avoid seeking errors. However, it would be beneficial to add a comment explaining this choice for future maintainability.

+ # Fetching the first frame (index 0) to avoid seeking errors, especially towards the end of the video.
  test_image = tf.convert_to_tensor(self.video.get_frame(0))

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
test_image = tf.convert_to_tensor(self.video.get_frame(0))
# Fetching the first frame (index 0) to avoid seeking errors, especially towards the end of the video.
test_image = tf.convert_to_tensor(self.video.get_frame(0))

image_dtype = test_image.dtype

def py_fetch_frame(ind):
Expand Down
55 changes: 55 additions & 0 deletions sleap/nn/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -1582,6 +1582,16 @@ def _object_builder():
try:
for ex in generator:
prediction_queue.put(ex)

except KeyError as e:
# Gracefully handle seeking errors by early termination.
if "Unable to load frame" in str(e):
pass # TODO: Print warning obeying verbosity? (This code path is also
# called for interactive prediction where we don't want any spam.)
else:
raise


Copy link

Choose a reason for hiding this comment

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

The code attempts to handle seeking errors gracefully by checking for a specific error message. However, this approach might not be robust as it relies on the error message string, which could change. Consider using exception types for more reliable error handling.

- if "Unable to load frame" in str(e):
+ if isinstance(e, FrameLoadError):

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
except KeyError as e:
# Gracefully handle seeking errors by early termination.
if "Unable to load frame" in str(e):
pass # TODO: Print warning obeying verbosity? (This code path is also
# called for interactive prediction where we don't want any spam.)
else:
raise
except KeyError as e:
# Gracefully handle seeking errors by early termination.
if isinstance(e, FrameLoadError):
pass # TODO: Print warning obeying verbosity? (This code path is also
# called for interactive prediction where we don't want any spam.)
else:
raise

finally:
prediction_queue.put(None)
object_builder.join()
Expand Down Expand Up @@ -2632,6 +2642,15 @@ def _object_builder():
try:
for ex in generator:
prediction_queue.put(ex)

except KeyError as e:
# Gracefully handle seeking errors by early termination.
if "Unable to load frame" in str(e):
pass # TODO: Print warning obeying verbosity? (This code path is also
# called for interactive prediction where we don't want any spam.)
else:
raise

Copy link

Choose a reason for hiding this comment

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

Similar to the previous comment, the error handling here also relies on string matching. It's recommended to use exception types for error handling.

- if "Unable to load frame" in str(e):
+ if isinstance(e, FrameLoadError):

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
except KeyError as e:
# Gracefully handle seeking errors by early termination.
if "Unable to load frame" in str(e):
pass # TODO: Print warning obeying verbosity? (This code path is also
# called for interactive prediction where we don't want any spam.)
else:
raise
except KeyError as e:
# Gracefully handle seeking errors by early termination.
if isinstance(e, FrameLoadError):
pass # TODO: Print warning obeying verbosity? (This code path is also
# called for interactive prediction where we don't want any spam.)
else:
raise

finally:
prediction_queue.put(None)
object_builder.join()
Expand Down Expand Up @@ -3265,6 +3284,15 @@ def _object_builder():
try:
for ex in generator:
prediction_queue.put(ex)

except KeyError as e:
# Gracefully handle seeking errors by early termination.
if "Unable to load frame" in str(e):
pass # TODO: Print warning obeying verbosity? (This code path is also
# called for interactive prediction where we don't want any spam.)
else:
raise

Copy link

Choose a reason for hiding this comment

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

Again, the error handling strategy based on string matching is observed. Using exception types would enhance the robustness of the error handling mechanism.

- if "Unable to load frame" in str(e):
+ if isinstance(e, FrameLoadError):

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
except KeyError as e:
# Gracefully handle seeking errors by early termination.
if "Unable to load frame" in str(e):
pass # TODO: Print warning obeying verbosity? (This code path is also
# called for interactive prediction where we don't want any spam.)
else:
raise
except KeyError as e:
# Gracefully handle seeking errors by early termination.
if isinstance(e, FrameLoadError):
pass # TODO: Print warning obeying verbosity? (This code path is also
# called for interactive prediction where we don't want any spam.)
else:
raise

finally:
prediction_queue.put(None)
object_builder.join()
Expand Down Expand Up @@ -3770,6 +3798,15 @@ def _object_builder():
try:
for ex in generator:
prediction_queue.put(ex)

except KeyError as e:
# Gracefully handle seeking errors by early termination.
if "Unable to load frame" in str(e):
pass # TODO: Print warning obeying verbosity? (This code path is also
# called for interactive prediction where we don't want any spam.)
else:
raise

finally:
prediction_queue.put(None)
object_builder.join()
Expand Down Expand Up @@ -4457,6 +4494,15 @@ def _object_builder():
try:
for ex in generator:
prediction_queue.put(ex)

except KeyError as e:
# Gracefully handle seeking errors by early termination.
if "Unable to load frame" in str(e):
pass # TODO: Print warning obeying verbosity? (This code path is also
# called for interactive prediction where we don't want any spam.)
else:
raise

finally:
prediction_queue.put(None)
object_builder.join()
Expand Down Expand Up @@ -4734,6 +4780,15 @@ def _object_builder():
try:
for ex in generator:
prediction_queue.put(ex)

except KeyError as e:
# Gracefully handle seeking errors by early termination.
if "Unable to load frame" in str(e):
pass # TODO: Print warning obeying verbosity? (This code path is also
# called for interactive prediction where we don't want any spam.)
else:
raise

Copy link

Choose a reason for hiding this comment

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

The error handling pattern based on string matching is repeated here. It's advisable to use exception types for more reliable error handling.

- if "Unable to load frame" in str(e):
+ if isinstance(e, FrameLoadError):

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
except KeyError as e:
# Gracefully handle seeking errors by early termination.
if "Unable to load frame" in str(e):
pass # TODO: Print warning obeying verbosity? (This code path is also
# called for interactive prediction where we don't want any spam.)
else:
raise
except KeyError as e:
# Gracefully handle seeking errors by early termination.
if isinstance(e, FrameLoadError):
pass # TODO: Print warning obeying verbosity? (This code path is also
# called for interactive prediction where we don't want any spam.)
else:
raise

finally:
prediction_queue.put(None)
object_builder.join()
Expand Down
Loading