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

Plotting/pyopenms viz #78

Merged
merged 10 commits into from
Oct 10, 2024
Merged

Plotting/pyopenms viz #78

merged 10 commits into from
Oct 10, 2024

Conversation

singjc
Copy link
Contributor

@singjc singjc commented Aug 24, 2024

User description

I updated all the plotting to use pyopenms_viz. This PR is related and depends on these to PRs for pyopenms_viz to be merged first: pyopenms_viz PR#19 and pyopenms_viz PR#20


PR Type

enhancement, other


Description

  • Refactored the plotting logic to utilize the ms_plotly backend, enhancing the visualization capabilities.
  • Added new UI options for spectrum plotting, including peak binning and number of bins selection.
  • Removed the BasePlotter and MSExperimentPlotter classes, along with their associated methods and configurations.
  • Updated the view module to integrate the new plotting logic, replacing old plotting functions with the new API.

Changes walkthrough 📝

Relevant files
Enhancement
common.py
Add spectrum plotting and binning options                               

src/common.py

  • Added spectrum plotting options in the UI.
  • Introduced binning options for spectrum peaks.
  • +6/-0     
    view.py
    Update view plotting to use ms_plotly backend                       

    src/view.py

  • Updated plotting methods to use ms_plotly backend.
  • Integrated new plotting logic for chromatograms and spectra.
  • Removed old plotting functions and replaced with new API.
  • +112/-88
    Other
    BasePlotter.py
    Remove BasePlotter abstract class and utilities                   

    src/plotting/BasePlotter.py

  • Removed the BasePlotter abstract class.
  • Removed associated configuration and utility methods.
  • +0/-58   
    MSExperimentPlotter.py
    Remove MSExperimentPlotter class and API                                 

    src/plotting/MSExperimentPlotter.py

  • Removed MSExperimentPlotter class and its methods.
  • Removed functional API for plotting MS experiments.
  • +0/-221 

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Copy link

    PR-Agent was enabled for this repository. To continue using it, please link your git user with your CodiumAI identity here.

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Performance Concern
    The plot_ms_spectrum function is decorated with @st.cache_resource, which may not be the most appropriate caching mechanism for this use case. Consider using @st.cache_data instead for better performance and memory management.

    Code Duplication
    The plotting logic for TIC, BPC, and XIC is very similar and could be refactored into a single function to reduce code duplication and improve maintainability.

    Copy link

    codiumai-pr-agent-pro bot commented Aug 24, 2024

    PR-Agent was enabled for this repository. To continue using it, please link your git user with your CodiumAI identity here.

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Maintainability
    Refactor repetitive plotting code into a separate function to reduce duplication

    The code for plotting TIC, BPC, and EIC is very similar and repetitive. Consider
    refactoring this into a separate function to reduce code duplication and improve
    maintainability.

    src/view.py [71-103]

    -if st.session_state.view_tic:
    -    df = st.session_state.view_ms1.groupby("RT").sum().reset_index()
    -    df["type"] = "TIC"
    -    if df["inty"].max() > max_int:
    -        max_int = df["inty"].max()
    +def plot_chromatogram(df, plot_type, color):
    +    df["type"] = plot_type
         fig = df.plot(
             backend="ms_plotly",
             kind="chromatogram",
             fig=fig,
             x="RT",
             y="inty",
             by="type",
    -        line_color="#f24c5c",
    +        line_color=color,
             show_plot=False,
             grid=False,
         )
    -    fig = fig.fig
    +    return fig.fig, df["inty"].max()
    +
    +if st.session_state.view_tic:
    +    df = st.session_state.view_ms1.groupby("RT").sum().reset_index()
    +    fig, max_tic = plot_chromatogram(df, "TIC", "#f24c5c")
    +    max_int = max(max_int, max_tic)
    +
     if st.session_state.view_bpc:
         df = st.session_state.view_ms1.groupby("RT").max().reset_index()
    -    df["type"] = "BPC"
    -    if df["inty"].max() > max_int:
    -        max_int = df["inty"].max()
    -    fig = df.plot(
    -        backend="ms_plotly",
    -        kind="chromatogram",
    -        fig=fig,
    -        x="RT",
    -        y="inty",
    -        by="type",
    -        line_color="#2d3a9d",
    -        show_plot=False,
    -        grid=False,
    -    )
    -    fig = fig.fig
    +    fig, max_bpc = plot_chromatogram(df, "BPC", "#2d3a9d")
    +    max_int = max(max_int, max_bpc)
     
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: Refactoring repetitive code into a function significantly improves code maintainability and reduces the risk of errors, making future modifications easier.

    9
    Use constants for color values to improve maintainability and consistency

    Consider using a constant or configuration variable for the color values ("#f24c5c",
    "#2d3a9d", "#f6bf26") instead of hardcoding them in multiple places. This would
    improve maintainability and consistency across the codebase.

    src/view.py [76-87]

    +COLOR_TIC = "#f24c5c"
    +COLOR_BPC = "#2d3a9d"
    +COLOR_XIC = "#f6bf26"
    +
     fig = df.plot(
         backend="ms_plotly",
         kind="chromatogram",
         fig=fig,
         x="RT",
         y="inty",
         by="type",
    -    line_color="#f24c5c",
    +    line_color=COLOR_TIC,
         show_plot=False,
         grid=False,
     )
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Using constants for color values improves maintainability and consistency across the codebase, making it easier to update and manage color schemes in the future.

    7
    Enhancement
    Simplify the logic for finding the maximum intensity across different plot types

    The max_int variable is being updated in multiple if statements. Consider using the
    max() function with a list comprehension to simplify the logic and reduce code
    duplication.

    src/view.py [70-92]

    -max_int = 0
    +intensity_maxes = []
     if st.session_state.view_tic:
         df = st.session_state.view_ms1.groupby("RT").sum().reset_index()
         df["type"] = "TIC"
    -    if df["inty"].max() > max_int:
    -        max_int = df["inty"].max()
    +    intensity_maxes.append(df["inty"].max())
         fig = df.plot(
             backend="ms_plotly",
             kind="chromatogram",
             fig=fig,
             x="RT",
             y="inty",
             by="type",
             line_color="#f24c5c",
             show_plot=False,
             grid=False,
         )
         fig = fig.fig
     if st.session_state.view_bpc:
         df = st.session_state.view_ms1.groupby("RT").max().reset_index()
         df["type"] = "BPC"
    -    if df["inty"].max() > max_int:
    -        max_int = df["inty"].max()
    +    intensity_maxes.append(df["inty"].max())
     
    +max_int = max(intensity_maxes) if intensity_maxes else 0
    +
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: This suggestion reduces code duplication and simplifies the logic for determining the maximum intensity, which enhances code readability and maintainability.

    8
    Error handling
    Improve error handling for EIC plotting to handle invalid inputs more gracefully

    The error handling for the EIC plotting could be improved. Currently, it catches a
    ValueError and displays an error message, but the function continues execution.
    Consider returning from the function or raising a custom exception to handle this
    case more gracefully.

    src/view.py [109-135]

     try:
         target_value = float(target_value)
         ppm_tolerance = st.session_state.view_eic_ppm
         tolerance = (target_value * ppm_tolerance) / 1e6
     
         # Filter the DataFrame
         df_eic = df[
             (df["mz"] >= target_value - tolerance)
             & (df["mz"] <= target_value + tolerance)
         ]
         if not df_eic.empty:
             df_eic["type"] = "XIC"
             if df_eic["inty"].max() > max_int:
                 max_int = df_eic["inty"].max()
             fig = df_eic.plot(
                 backend="ms_plotly",
                 kind="chromatogram",
                 fig=fig,
                 x="RT",
                 y="inty",
                 by="type",
                 line_color="#f6bf26",
                 show_plot=False,
                 grid=False,
             )
             fig = fig.fig
    +    else:
    +        st.warning("No data found for the specified m/z value and tolerance.")
     except ValueError:
         st.error("Invalid m/z value for XIC provided. Please enter a valid number.")
    +    return None  # or raise a custom exception
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Enhancing error handling by providing warnings and potentially stopping execution on invalid input improves user experience and prevents further errors in the code execution.

    8

    @axelwalter
    Copy link
    Collaborator

    Requires pyopenms_viz and pandas needs to be installed together with pyopenms_viz (not pyopenms) in order to set the backends.

    @axelwalter axelwalter merged commit 36d93ae into OpenMS:main Oct 10, 2024
    6 checks passed
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants