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

Cleanup #20

Merged
merged 1 commit into from
Mar 12, 2025
Merged

Cleanup #20

merged 1 commit into from
Mar 12, 2025

Conversation

ternaus
Copy link
Contributor

@ternaus ternaus commented Mar 12, 2025

No description provided.

Copy link

sourcery-ai bot commented Mar 12, 2025

Reviewer's Guide by Sourcery

This pull request refactors and cleans up code related to GPU information extraction and speedup plot generation. It introduces a new function for extracting GPU information, removes redundant code, improves error handling for empty DataFrames, and adds a pylint ignore rule.

Sequence diagram for extracting GPU information

sequenceDiagram
    participant compare_video_results
    participant thread_settings

    compare_video_results->>thread_settings: extract_gpu_info(thread_settings)
    alt pytorch in thread_settings
        thread_settings->>compare_video_results: pytorch_settings
        alt pytorch_settings is string
            thread_settings->>compare_video_results: Extract GPU info from string
        else pytorch_settings is dictionary
            thread_settings->>compare_video_results: Extract GPU info from dictionary
        end
        compare_video_results-->>compare_video_results: Return GPU information
    else
        compare_video_results-->>compare_video_results: Return "Unknown hardware"
    end
Loading

File-Level Changes

Change Details Files
Introduced a function to extract GPU information from thread settings.
  • Created a function extract_gpu_info to handle GPU information extraction from thread settings, including cases where settings are strings or dictionaries.
  • Refactored the GPU information extraction logic in get_hardware_info to use the new extract_gpu_info function.
tools/compare_video_results.py
Removed redundant GPU information extraction logic from compare_results.py.
  • Removed the duplicated GPU information extraction code from the get_system_summary function.
tools/compare_results.py
Improved handling of empty speedups DataFrames in generate_speedup_plots.py.
  • Ensured that max_speedup_transform and min_speedup_transform are set to 'N/A' when the speedups DataFrame is empty to avoid errors.
tools/generate_speedup_plots.py
Added a pylint ignore rule.
  • Added PLR0911 to the list of pylint ignores.
pyproject.toml

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@ternaus ternaus merged commit 149193f into main Mar 12, 2025
1 check passed
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @ternaus - I've reviewed your changes - here's some feedback:

Overall Comments:

  • The new extract_gpu_info function improves code reuse and readability.
  • Consider adding a default return value to extract_gpu_info in case none of the conditions are met.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.



def format_time(time_ms: float, std: float | None = None) -> str:
"""Format time value with optional standard deviation."""
return f"{time_ms:.2f} ± {std:.2f}" if std is not None else f"{time_ms:.2f}"


def extract_gpu_info(thread_settings: dict[str, Any]) -> str:
Copy link

Choose a reason for hiding this comment

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

issue (complexity): Consider extracting the regex-based GPU name extraction into its own helper function to simplify the nested conditional logic and improve readability through early returns and reduced nesting depth within the extract_gpu_info function, while maintaining all existing functionality.

Below is a suggestion to break up the nested logic by extracting the regex-based GPU name extraction into its own helper. This helps flatten conditionals while keeping functionality intact. For example:

def extract_gpu_name_from_string(pytorch_settings: str) -> str | None:
    import re
    gpu_name_match = re.search(r"gpu_name': '([^']+)'", pytorch_settings)
    if gpu_name_match:
        return gpu_name_match.group(1)
    gpu_device_match = re.search(r"gpu_device': ([^,}]+)", pytorch_settings)
    if gpu_device_match:
        return f"GPU {gpu_device_match.group(1)}"
    return None

def extract_gpu_info(thread_settings: dict[str, Any]) -> str:
    """Extract GPU information from thread settings."""
    if "pytorch" not in thread_settings:
        return "Unknown hardware"
    pytorch_settings = thread_settings["pytorch"]

    # Handle string format with early returns
    if isinstance(pytorch_settings, str):
        if "gpu_available': True" in pytorch_settings:
            info = extract_gpu_name_from_string(pytorch_settings)
            return info if info else "GPU (details unknown)"
        return "CPU (GPU not available)"

    # Handle dictionary format
    if pytorch_settings.get("gpu_available", False):
        gpu_name = pytorch_settings.get("gpu_name")
        if gpu_name:
            return gpu_name
        gpu_device = pytorch_settings.get("gpu_device", "Unknown")
        return f"GPU {gpu_device}"

    return "CPU (GPU not available)"

Actionable Steps:

  1. Extract Regex Logic: Create a helper like extract_gpu_name_from_string that covers the inline regex matches.
  2. Use Early Returns: Flatten the conditional flow by returning early when possible.
  3. Keep Functionality: Ensure that all branches remain intact while reducing nesting.

These steps help isolate complex parts and improve readability while keeping all existing functionality.

if "gpu_available': True" in pytorch_settings:
# Extract GPU name if available
if "gpu_name':" in pytorch_settings:
import re
Copy link

Choose a reason for hiding this comment

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

issue (code-quality): Use named expression to simplify assignment and conditional [×3] (use-named-expression)

@ternaus ternaus deleted the cleanup branch March 12, 2025 22:05
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.

1 participant