Skip to content

Comments

Rewrote predict_usleep()#8

Open
lamasmithueten wants to merge 14 commits intoskjerns:mainfrom
lamasmithueten:main
Open

Rewrote predict_usleep()#8
lamasmithueten wants to merge 14 commits intoskjerns:mainfrom
lamasmithueten:main

Conversation

@lamasmithueten
Copy link

The following functions where rewritten in usleep_utils.py:

  • predict_usleep() -> _score_sleep_file()
  • predict_usleep_raw() -> _score_sleep_raw()

The following functions were added:

  • score_sleep()
  • _score_usleep_denmark()
  • _score_sleepyland()

Previously usleep_utils.py had two functions predict_usleep() for EDF-files and predict_usleep_raw() for mne.io.Raw object. I have combined both of these functions into one score_sleep() function, which can take either an EDF-file or an mne.io.Raw object. Based on that either _score_sleep_raw() or _score_sleep_file() is called. You need to set either the raw or edf_file parameter to call the corresponding function. If both are set, the raw parameter has priority.

Furthermore the backend for scoring has been extended. Previously you could only send to https://sleep.ai.ku.dk/ using their usleep API to score sleep. Now you can use the internal CIMH sleepyland service as a local backend. To choose which backend you want to use, you need to set the backend variable to either denmark or sleepyland to access the corresponding backend. I also added another variable called backend_url for future backends, but isn't used as of now, because both URLs use hard coded URLs.

…alls the actual functions

Logical switches are whether you are using an EDF-file or an mne-Object, the backend you want to use and whether you want to use individual channels or channels groups
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @lamasmithueten, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors the sleep scoring utility functions to improve flexibility and expand backend options. The primary goal is to provide a single, consistent interface for users to score sleep data, regardless of whether the input is a raw MNE object or an EDF file, and to allow for easy selection between different scoring service backends. This change streamlines the API and introduces a new local processing capability.

Highlights

  • Function Renaming and Unification: The functions predict_usleep() and predict_usleep_raw() have been renamed to _score_sleep_file() and _score_sleep_raw() respectively, indicating their internal utility. A new public function, score_sleep(), has been introduced to serve as a unified entry point for both EDF files and mne.io.Raw objects.
  • New Sleep Scoring Backends: The system now supports an additional local sleep scoring backend named sleepyland, alongside the existing denmark (U-Sleep API) backend. Users can specify which backend to use via a new backend parameter.
  • New Internal Scoring Functions: Two new internal functions, _score_usleep_denmark() and _score_sleepyland(), have been added to encapsulate the specific logic for interacting with each respective scoring backend.
  • Enhanced Input Validation: The _score_sleep_file() function now includes robust checks to ensure the provided EDF file exists, has the correct extension, and contains all the required EEG and EOG channels.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the sleep scoring utilities by introducing a unified score_sleep function and adding a new sleepyland backend. The overall structure is improved, but there are several areas that need attention. I've identified issues such as debugging print statements left in the code, a critical bug involving an undefined variable, a resource leak from an unclosed file, inefficient string manipulation, and inadequate error handling for network requests. My review includes specific suggestions to enhance the code's robustness, maintainability, and correctness.

lamasmithueten and others added 6 commits January 14, 2026 12:19
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@skjerns
Copy link
Owner

skjerns commented Jan 15, 2026

@lamasmithueten let me know when it's ready for my review, please mark all outdated conversations as resolved and address the remaining gemini comments, or comment if they are not relevant or useful suggestions

lamasmithueten and others added 3 commits January 15, 2026 17:46
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@lamasmithueten
Copy link
Author

I closed all Gemini suggestions and merged the ones that make sense. I reran the changes and it worked without any error messages.

@skjerns
Copy link
Owner

skjerns commented Jan 19, 2026

New rewrite-request:

change the _score_sleep_raw function into a function that simply writes the raw to a file and returns a temporary file path. This way, we don't have multiple paths and more modularity.

new structure:

# (pseudocode)
is_temp_file = False
if raw is not None:
    assert isinstance(raw, mne.BaseRaw)
    is_temp_file = True
    edf_file = _raw_to_edf_tempfile(raw)
_score_sleep_file(edf_file, ...)

if is_temp_file:
    os.remove(edf_file)

alternatively, or even better: Use the tempfile interface to create a temporary file and then later call ".close()" on it later. This is slightly safer and gives you everything more easily

i'll do mroe comments later, please wait before working on it until my go

@lamasmithueten
Copy link
Author

I moved the logic from the wraper function into the function _score_sleep_raw() directly.

lamasmithueten and others added 3 commits February 10, 2026 10:23
Co-authored-by: Simon Kern <14980558+skjerns@users.noreply.github.com>
Co-authored-by: Simon Kern <14980558+skjerns@users.noreply.github.com>
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.

2 participants