Skip to content

Feat/media#1502

Merged
SAKURA-CAT merged 5 commits intomainfrom
feat/media
Mar 15, 2026
Merged

Feat/media#1502
SAKURA-CAT merged 5 commits intomainfrom
feat/media

Conversation

@SAKURA-CAT
Copy link
Copy Markdown
Member

@SAKURA-CAT SAKURA-CAT commented Mar 15, 2026

  1. 添加 Video Image Audio等媒体类型
  2. 通过 __init__.pyi 定义一些主要的 API
  3. 将系统回调放在swanlab.run对象上
  4. 优化测试

Introduce an Image TransformMedia implementation and wire it into the transforms registry. The new Image class accepts file paths, PIL images, numpy arrays, torch tensors, and matplotlib figures, supports optional resizing and output format validation, serializes to an image buffer, and implements column_type/build_data_record/transform (writes file with sha256-based name via safe_write). Export Image from transforms.__init__ and update tests to include an Image media factory. Also extend swanlab.vendor lazy imports to expose torch, torchvision and ensure PIL.Image submodule is imported so PIL.Image usage works with the lazy loader.
Introduce a Video media transform (GIF support) with format detection, safe file writing, sha256-based filenames, and build_data_record/transform implementations. Export Video from transforms and simplify normalize_media_input to always construct media instances (enables nested/wrapped media). Add SwanLabRun convenience methods: log_image, log_audio (with sample_rate), and log_video that normalize inputs and forward to log. Include unit tests for Video behavior and parameterized media logging; update media factories in existing tests.
Consolidate top-level run-related commands into a single cmd/run.py that generates wrappers (_make_run_cmd) for SwanLabRun methods (finish, log, log_scalar, log_text, log_image, log_audio, log_video). Remove legacy cmd/finish.py and cmd/log.py and update sdk/__init__.py and top-level swanlab/__init__.py to export the new logging APIs. Add a comprehensive type-stub swanlab/__init__.pyi describing the public API. Improve SwanLabRun lifecycle handling: register atexit and sys.excepthook handlers, add private _atexit_cleanup, _excepthook and _cleanup to centralize teardown and resource reset. Update exceptions export list and remove the example main.py. Tests updated: remove old cmd tests, rename some internal run tests, and add unit tests for the finish/exception hooks to validate the new lifecycle behavior.
@SAKURA-CAT SAKURA-CAT self-assigned this Mar 15, 2026
@SAKURA-CAT SAKURA-CAT added the 💪 enhancement New feature or request label Mar 15, 2026
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 expands the SwanLab SDK's data logging capabilities by introducing dedicated support for various media types, including video, images, and audio. It also streamlines the core experiment management logic by refactoring command functions and embedding critical system hooks directly within the run object, leading to a more cohesive and resilient SDK experience.

Highlights

  • New Media Type Support: Added support for logging Video, Image, and Audio data types within the SwanLab SDK, enhancing experiment visualization capabilities.
  • API Definition via Type Stubs: The public API for these new media types and other functionalities is now formally defined through __init__.pyi type stubs, improving code clarity and enabling better static analysis.
  • Refactored Run Lifecycle Management: The finish and log functionalities have been centralized into a new swanlab.sdk.cmd.run module, and system-level exception handling (atexit and excepthook) is now integrated directly into the SwanLabRun class for more robust experiment lifecycle management.

🧠 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.

Changelog
  • swanlab/init.py
    • Imported new media logging functions (log_audio, log_image, log_video) and define_scalar.
    • Added Image and Video to swanlab.sdk.internal.run.transforms.
    • Updated __all__ to expose new logging functions and media types.
  • swanlab/init.pyi
    • Added type stubs for log_image, log_audio, log_video, and define_scalar.
    • Included Image and Video in the __all__ list and imports.
    • Added detailed docstrings and examples for new media logging functions and define_scalar.
  • swanlab/exceptions.py
    • Added __all__ to explicitly define public symbols.
  • swanlab/main.py
    • Removed.
  • swanlab/sdk/init.py
    • Removed direct imports of finish and log from swanlab.sdk.cmd.
    • Imported define_scalar, finish, log, log_audio, log_image, log_text, log_video from swanlab.sdk.cmd.run.
    • Updated __all__ to reflect the new logging functions and define_scalar.
  • swanlab/sdk/cmd/finish.py
    • Removed, as its functionality was moved to swanlab/sdk/cmd/run.py and SwanLabRun.
  • swanlab/sdk/cmd/log.py
    • Removed, as its functionality was moved to swanlab/sdk/cmd/run.py.
  • swanlab/sdk/cmd/run.py
    • Added new file to centralize run-related commands.
    • Implemented _make_run_cmd decorator to wrap SwanLabRun methods as top-level commands.
    • Exposed log, log_text, log_image, log_audio, log_video, define_scalar, and finish as top-level commands.
  • swanlab/sdk/internal/run/init.py
    • Imported atexit, sys, traceback, and TracebackType for system hooks.
    • Imported Image and Video from swanlab.sdk.internal.run.transforms.
    • Integrated _atexit_cleanup and _excepthook methods into SwanLabRun for automatic run finalization and exception handling.
    • Modified __init__ to register atexit and excepthook.
    • Refactored finish method to call _cleanup.
    • Added log_image, log_audio, log_video methods to SwanLabRun for media logging.
    • Updated define_scalar docstring to Chinese.
  • swanlab/sdk/internal/run/transforms/init.py
    • Imported Image and Video modules.
    • Added Image and Video to __all__.
    • Modified normalize_media_input to simplify media object creation.
  • swanlab/sdk/internal/run/transforms/image/init.py
    • Implemented Image class for handling image data.
    • Added methods for image initialization from various sources (path, PIL Image, numpy array, torch tensor, matplotlib figure).
    • Included image resizing logic and format validation.
    • Defined column_type, build_data_record, and transform methods for image processing.
  • swanlab/sdk/internal/run/transforms/video/init.py
    • Added new file implementing Video class for handling video data (currently GIF only).
    • Included methods for video initialization from various sources (path, bytes, BytesIO).
    • Implemented format detection via magic bytes.
    • Defined column_type, build_data_record, and transform methods for video processing.
  • swanlab/vendor/init.py
    • Imported PIL.Image, torch, and torchvision for media processing.
    • Added torch and torchvision to _LAZY_IMPORTS and _LAZY_IMPORT_ALIASES.
    • Added _SUBMODULE_IMPORTS to explicitly import PIL.Image.
  • tests/unit/sdk/cmd/finish/test_finish.py
    • Removed, as the finish command was refactored.
  • tests/unit/sdk/cmd/finish/test_finish_e2e.py
    • Updated import path for finish to swanlab.sdk.cmd.run.
  • tests/unit/sdk/cmd/test_log.py
    • Removed, as the log command was refactored.
  • tests/unit/sdk/internal/run/data/test_media_transform.py
    • Imported Image and Video for media transform tests.
    • Added Image and Video factories to MEDIA_FACTORIES.
    • Defined _GIF_1X1 for video testing.
  • tests/unit/sdk/internal/run/data/test_video.py
    • Added new file with unit tests for the Video class.
    • Tested initialization from bytes, BytesIO, and path.
    • Tested error handling for invalid formats and paths.
    • Tested nesting (wrapping existing Video objects).
    • Tested column_type, build_data_record, and transform methods.
  • tests/unit/sdk/internal/run/test_decorators.py
    • Renamed to tests/unit/sdk/internal/run/test_run_decorators.py.
  • tests/unit/sdk/internal/run/test_finish_hook.py
    • Added new file with unit tests for SwanLabRun's _atexit_cleanup and _excepthook methods.
    • Tested behavior for different run states and exception types.
  • tests/unit/sdk/internal/run/test_log_media.py
    • Added new file with parameterized unit tests for SwanLabRun.log_text, log_image, log_audio, log_video.
    • Tested single instance and list of instances logging.
    • Tested step and caption parameter forwarding.
    • Tested error handling when run is not active.
Activity
  • SAKURA-CAT created this pull request with the title "Feat/media" and description "1. 添加 Video Image Audio等媒体类型 2. 通过 __init__.pyi 定义一些主要的 API".
  • New media types (Video, Image, Audio) were introduced to the SDK.
  • The public API for these new media types was defined in __init__.pyi.
  • Core logging and finishing functionalities were refactored and centralized into a new run.py module.
  • System-level exception handling and atexit hooks were integrated directly into the SwanLabRun class.
  • Several new unit tests were added to cover the new media types and the refactored run lifecycle management.
  • Existing test files were updated to reflect the changes in module structure and API usage.
  • Redundant files related to the old finish and log command implementations were removed.
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
Copy Markdown
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

本次 PR 是一次重要的功能更新,引入了对 ImageAudioVideo 等多种媒体类型的日志记录支持。各种媒体类型的实现考虑周全,能够处理多种输入格式。代码结构方面,将命令行函数重构到 swanlab.sdk.cmd.run 模块,并将运行生命周期钩子集中到 SwanLabRun 类中,这些都是很好的改进,提升了代码的可维护性。此外,通过添加 .pyi 文件来定义公共 API,也为类型安全和开发者体验带来了提升。

我的审查意见主要包含一些关于文档一致性、类型提示准确性以及确保文档与实现同步的建议。总体而言,这是一次高质量的贡献。

Extend Image constructor type hints and docstring to accept vendor.torch.Tensor and vendor.matplotlib.figure.Figure (matplotlib figures and torch tensors). Also import matplotlib.figure under TYPE_CHECKING and add 'matplotlib.figure' to _SUBMODULE_IMPORTS in vendor/__init__ so the submodule is available for type references.
@SAKURA-CAT SAKURA-CAT merged commit 8ef2a43 into main Mar 15, 2026
18 checks passed
@SAKURA-CAT SAKURA-CAT deleted the feat/media branch March 15, 2026 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

💪 enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant