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

[ENH] implement logger for first_level_from_bids #3702

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Remi-Gau
Copy link
Collaborator

Closes #3686

Changes proposed in this pull request:

  • shows a proof of concept to implement a logger for first_level_from_bids

Comment on lines +54 to +55
logging.basicConfig(level=logging.ERROR)
logger = logging.getLogger(name="nilearn")
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

potentially create a function nilearn/_utils/logger.py ?

Copy link
Member

Choose a reason for hiding this comment

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

actually there is one 😆 it's used in a few modules but not consistently. it should probably be removed as part of a larger change if we replace current print calls with logging

@@ -1527,8 +1511,7 @@ def _make_bids_files_filter(
if extra_filter and supported_filters:
for filter_ in extra_filter:
if filter_[0] not in supported_filters:
if verbose:
warn(
logger.warning(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

should warning be handled by the logger as well?

Copy link
Member

Choose a reason for hiding this comment

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

IIUC the warning reports an error that the application developer should correct, so IMO and my understanding of the logging module's documentation it should be a warning (not a logging event), and wether we use logging or not it shouldn't be controlled by the verbose parameter

@github-actions
Copy link
Contributor

github-actions bot commented Apr 18, 2023

👋 @Remi-Gau Thanks for creating a PR!

Until this PR is ready for review, you can include the [WIP] tag in its title, or leave it as a github draft.

Please make sure it is compliant with our contributing guidelines. In particular, be sure it checks the boxes listed below.

  • PR has an interpretable title.
  • PR links to Github issue with mention Closes #XXXX (see our documentation on PR structure)
  • Code is PEP8-compliant (see our documentation on coding style)
  • Changelog or what's new entry in doc/changes/latest.rst (see our documentation on PR structure)

For new features:

  • There is at least one unit test per new function / class (see our documentation on testing)
  • The new feature is demoed in at least one relevant example.

For bug fixes:

  • There is at least one test that would fail under the original bug conditions.

We will review it as quick as possible, feel free to ping us with questions if needed.

@codecov
Copy link

codecov bot commented Apr 18, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (9a50716) 91.40% compared to head (2e29053) 91.38%.
Report is 368 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3702      +/-   ##
==========================================
- Coverage   91.40%   91.38%   -0.02%     
==========================================
  Files         133      133              
  Lines       15536    15552      +16     
  Branches     3225     3225              
==========================================
+ Hits        14200    14212      +12     
- Misses        784      787       +3     
- Partials      552      553       +1     

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

@Remi-Gau
Copy link
Collaborator Author

@jeromedockes this is just a quick dirty example of how things could look like with a logger.

Let me know what you think.

Copy link
Member

@jeromedockes jeromedockes left a comment

Choose a reason for hiding this comment

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

sorry I didn't realise implement a logger meant start using the logging standard library module. I think it's a good idea, but then we would probably want to use it consistently throughout the library and plan it a bit more... there is some discussion to be had as some people find logging confusing, and we should read through related scikit-learn discussions (linked below).

and maybe wait for the formatting stuff to be over so we don't have too many widespread changes going on at the same time

for the specific case of 1st level from bids, I wonder if we should have a class to have fewer functions with a lot of parameters, and a way to store some info like the discovered files and produce reports?

Comment on lines +54 to +55
logging.basicConfig(level=logging.ERROR)
logger = logging.getLogger(name="nilearn")
Copy link
Member

Choose a reason for hiding this comment

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

actually there is one 😆 it's used in a few modules but not consistently. it should probably be removed as part of a larger change if we replace current print calls with logging

@@ -49,6 +49,11 @@
from sklearn.cluster import KMeans


import logging

logging.basicConfig(level=logging.ERROR)
Copy link
Member

Choose a reason for hiding this comment

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

in theory libraries are not supposed to mess with the logging configuration, and definitely not add any handlers to the root logger (doc), as that is left up to the application... I guess current joblib/scikit-learn/nilearn approach of verbose and print is quite far from the standard library logging module's philosophy, and it might be worth reading the related discussions in related scikit-learn issues: roadmap, 6929, 78

Copy link
Member

Choose a reason for hiding this comment

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

also the default handlers writes to stderr while the rest of nilearn messages made with print would go to stdout, it would be a bit weird to have just the first_level_model_from_bids output go to a different stream

@@ -1154,12 +1161,12 @@ def _report_found_files(
Only one filter per field allowed.

"""
print(
Copy link
Member

Choose a reason for hiding this comment

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

is this the only thing that is controlled by the verbose parameter in our case?

@@ -1527,8 +1511,7 @@ def _make_bids_files_filter(
if extra_filter and supported_filters:
for filter_ in extra_filter:
if filter_[0] not in supported_filters:
if verbose:
warn(
logger.warning(
Copy link
Member

Choose a reason for hiding this comment

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

IIUC the warning reports an error that the application developer should correct, so IMO and my understanding of the logging module's documentation it should be a warning (not a logging event), and wether we use logging or not it shouldn't be controlled by the verbose parameter

@Remi-Gau
Copy link
Collaborator Author

sorry I didn't realise implement a logger meant start using the logging standard library module. I think it's a good idea, but then we would probably want to use it consistently throughout the library and plan it a bit more...

yeah the "should we use a more systematic and planned approach to this?" was in the back of my mind and hence why I prefer to draft things here to get early feedback.

Do you think I should maybe rename the issue #3686 to reflect a potential bigger scope for this?

there is some discussion to be had as some people find logging confusing, and we should read through related scikit-learn discussions (linked below).

thanks: will have a look

and maybe wait for the formatting stuff to be over so we don't have too many widespread changes going on at the same time

Agreed

for the specific case of 1st level from bids, I wonder if we should have a class to have fewer functions with a lot of parameters, and a way to store some info like the discovered files and produce reports?

Will look into this as well.

@Remi-Gau Remi-Gau added the GLM Issues/PRs related to the nilearn.glm module. label Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GLM Issues/PRs related to the nilearn.glm module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add a logger to "first_level_from_bids"
2 participants