Improve code quality: proper logging, error handling, and optional de…#5507
Improve code quality: proper logging, error handling, and optional de…#5507Alanperry1 wants to merge 3 commits intofacebookresearch:mainfrom
Conversation
…pendencies - Replace print() statements with logger.info() for better logging control - Convert assert statements to explicit ValueError/FileNotFoundError with descriptive messages - Move matplotlib from required dependencies to optional "viz" extra - Add try-except block for matplotlib import with helpful installation message - Move logging calls to after default_setup() configuration in train_net.py - Move import statements to top of files following Python conventions Fixes all issues identified in code review.
There was a problem hiding this comment.
Pull request overview
This PR improves code quality by replacing print() statements with proper logging, converting assert statements to explicit error handling with descriptive error messages, and moving matplotlib from a required dependency to an optional visualization dependency.
Changes:
- Replaced
print()calls withlogger.info()in visualization and training tools - Converted
assertstatements to explicit exceptions (ValueError,FileNotFoundError) with clear error messages in demo.py - Moved matplotlib to
extras_require["viz"]in setup.py and added import handling in visualizer.py
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tools/visualize_data.py | Replaced print statements with logger.info for file output messages |
| tools/train_net.py | Moved command line args logging to after default_setup() for proper configuration |
| setup.py | Moved matplotlib from install_requires to extras_require["viz"] |
| detectron2/utils/visualizer.py | Added try-except for matplotlib imports with helpful error message |
| demo/demo.py | Converted assert statements to explicit exceptions with descriptive messages |
| datasets/prepare_panoptic_fpn.py | Added logging import and replaced print statements with logger.info |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if not os.path.isfile(args.video_input): | ||
| raise FileNotFoundError(f"Video input file not found: {args.video_input}") |
There was a problem hiding this comment.
The video file existence check is performed after the output file setup (lines 164-180). This means if the video input file doesn't exist, the code will still create the output file writer first, potentially creating an empty output file before failing. The video file existence check should be moved earlier, before any output file setup, to fail fast and avoid creating unnecessary files.
There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on this feedback
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Summary
This PR improves code quality across the codebase by replacing
print()statements with proper logging, convertingassertstatements to explicit error handling with clear error messages, and moving matplotlib from a required dependency to an optional visualization dependency.Changes
Logging Improvements
default_setup()is called, ensuring proper logging configurationprint()calls withlogger.info()for file output messagesloggingimport at the top of the file (proper Python convention)print()statements withlogger.info()for progress messagesError Handling
assertstatements to explicitValueError/FileNotFoundErrorexceptions with descriptive error messages:assert os.path.isdir(args.output)check that was logically impossibleDependency Management
matplotlibfrominstall_requirestoextras_require["viz"], completing the TODOdetectron2[viz]Benefits
pip install detectron2for core functionality orpip install detectron2[viz]for visualization featuresBreaking Changes
Users who don't install the
vizextra will need to runpip install detectron2[viz]to use visualization features.