Convert file operations to context managers in FSL GLM pipeline#49
Merged
shawntz merged 12 commits intonew-advanced-featuresfrom Dec 20, 2025
Merged
Convert file operations to context managers in FSL GLM pipeline#49shawntz merged 12 commits intonew-advanced-featuresfrom
shawntz merged 12 commits intonew-advanced-featuresfrom
Conversation
Co-authored-by: shawntz <[email protected]>
Co-authored-by: shawntz <[email protected]>
Co-authored-by: shawntz <[email protected]>
…l file operations Co-authored-by: shawntz <[email protected]>
…e file operations Co-authored-by: shawntz <[email protected]>
…ations Co-authored-by: shawntz <[email protected]>
…ssues Co-authored-by: shawntz <[email protected]>
…gs_fromstub logic Co-authored-by: shawntz <[email protected]>
Copilot
AI
changed the title
[WIP] Update FSL GLM analysis pipeline based on review feedback
Convert file operations to context managers in FSL GLM pipeline
Dec 20, 2025
Contributor
There was a problem hiding this comment.
Pull request overview
This PR converts file operations to context managers across the FSL GLM pipeline to prevent potential file descriptor leaks, and fixes a pre-existing bug in mk_level3_fsf.py where a dictionary value was incorrectly overwritten.
Key Changes
- Converted 16
open()calls to use context managers (withstatements) for automatic resource cleanup - Fixed bug in
mk_level3_fsf.pywherecustomsettings[setting]was being overwritten with a boolean instead of using a separate tracking dictionary - Reformatted several multi-line
json.load()calls to single lines for improved readability
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
08-fsl-glm/openfmri_utils.py |
Moved dictionary initialization inside context managers for 5 utility functions; no behavioral changes |
08-fsl-glm/mk_level3_fsf.py |
Fixed customsettings_fromstub bug and converted 3 file operations to context managers with proper nesting |
08-fsl-glm/mk_level2_fsf.py |
Converted 4 file operations to context managers with proper nesting of stub/outfile operations |
08-fsl-glm/mk_level1_fsf_bbr.py |
Converted 5 file operations to context managers; introduced two new unused variables (projdir, qadir) and has inconsistent indentation (tabs vs spaces) |
Comments suppressed due to low confidence (2)
08-fsl-glm/mk_level1_fsf_bbr.py:111
- Variable projdir is not used.
projdir=os.path.join(a.basedir,a.studyid)
08-fsl-glm/mk_level1_fsf_bbr.py:325
- Variable qadir is not used.
qadir='%s/QA'%(fmriprep_subdir)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <[email protected]>
- Removed unused variable `projdir` (line 111) - Removed unused commented-out variable `qadir` (line 324) - Fixed inconsistent indentation to use 4-space multiples throughout lines 433-585 - All indentation now follows PEP 8 style guide Co-authored-by: shawntz <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR addresses code review feedback from PR #39 regarding file handle management in Python scripts by converting all
open()calls to use Python context managers (withstatements).Changes Made
Core Improvements
✅ All file operations now use context managers to ensure proper resource cleanup even when exceptions occur
Files Modified
08-fsl-glm/mk_level2_fsf.py
open()andjson.load(open())calls to context managerswithblocks08-fsl-glm/mk_level1_fsf_bbr.py
open()andjson.load(open())calls to context managerswithblocks for outfile and stubfileprojdir,qadir)08-fsl-glm/mk_level3_fsf.py
open()andjson.load(open())calls to context managerscustomsettings_fromstub[setting]now correctly set toTrueinstead of overwriting withcustomsettings[setting]08-fsl-glm/openfmri_utils.py
open()calls across multiple utility functions to use context managers08-fsl-glm/setup_utils.py
json.load(open())pattern to use context managerTesting
✅ All 15 Python files in
08-fsl-glm/directory compile successfully without syntax errors✅ All indentation follows PEP 8 style guide (4-space multiples)
Known Pre-Existing Issues (Not Addressed)
A duplicate function definition exists in
openfmri_utils.pywhereload_fsl_design_conappears twice with different parameters (featdirvsinfile). This issue existed in the original code and is outside the scope of this file handle management task.✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.