<reponame>elisarchodorov/ML-Recipes<filename>experiments_track/propancity/src/plots.py<gh_stars>0
import plotly.graph_objects as go
from plotly.subplots import make_subplots

["#27c1d1", "#217883", "#FF0000", "#6473ff"]


color_scheme = {"Visa": "#6473ff", "MasterCard":"#217883"}


def create_model_plots(df, feature_plots, desc):
    for feature_plot in feature_plots:
        fig = go.Figure()
        for card_name, card_df in df.groupby("formatted_credit_card_company"):
            fig.add_trace(
                go.Histogram(
                    x=card_df[feature_plot],
                    name=card_name,
                    marker_color=color_scheme.get(card_name)
                )
            )
        # Overlay both histograms
        fig.update_layout(
            barmode='overlay',
            title_text=f"{desc} {feature_plot.replace('_', ' ')}",
        )
        # Reduce opacity to see both histograms
        fig.update_traces(opacity=0.75)
        fig.show()
        fig.write_html(f"outputs/{desc}_{feature_plot}_histogram.html")


def plot_smd(smd_scores):

    fig = make_subplots(
        rows=len(smd_scores.index),
        cols=1,
                        shared_xaxes=True,
                        vertical_spacing=0.02
    )

    for row, feature in enumerate(smd_scores.index, 1):
        show_legend = True if row == 1 else False
        fig.add_trace(
            go.Scatter(
                x=smd_scores.loc[feature, ["unmatched", "matched"]],
                y=[feature, feature],
                mode='lines+markers',
                showlegend=show_legend,
                name="unmatched",
                line=dict(color="#217883"),
                marker=dict(
                    size=[20, 20],
                    color=["#27c1d1", "#217883"]
                )

            ),
                row=row,
            col=1
        )
    fig.update_layout(
        height=1000, width=1200,
        title_text="smd scores for propensity score matching"
    )
    fig.update_traces(textposition="bottom right")
    fig.show()
    fig.write_html(f"outputs/smd_scores.html")


def plot_smd_old(smd_scores):
    fig = go.Figure()
    for score in ["unmatched", "matched"]:
        fig.add_trace(
            go.Scatter(
            x=smd_scores[score],
            y=smd_scores.index,
            name="unmatched",
            mode='markers',
            marker=dict(size=16)
            )
        )

    fig.update_traces(textposition="bottom right")
    fig.write_html(f"outputs/smd_scores_old.html")



import plotly.graph_objects as go
from plotly.subplots import make_subplots

["#27c1d1", "#217883", "#FF0000", "#6473ff"]


color_scheme = {"Visa": "#6473ff", "MasterCard":"#217883"}


def create_model_plots(df, feature_plots, desc):
    for feature_plot in feature_plots:
        fig = go.Figure()
        for card_name, card_df in df.groupby("formatted_credit_card_company"):
            fig.add_trace(
                go.Histogram(
                    x=card_df[feature_plot],
                    name=card_name,
                    marker_color=color_scheme.get(card_name)
                )
            )
        # Overlay both histograms
        fig.update_layout(
            barmode='overlay',
            title_text=f"{desc} {feature_plot.replace('_', ' ')}",
        )
        # Reduce opacity to see both histograms
        fig.update_traces(opacity=0.75)
        fig.show()
        fig.write_html(f"outputs/{desc}_{feature_plot}_histogram.html")


def plot_smd(smd_scores):

    fig = make_subplots(
        rows=len(smd_scores.index),
        cols=1,
                        shared_xaxes=True,
                        vertical_spacing=0.02
    )

    for row, feature in enumerate(smd_scores.index, 1):
        show_legend = True if row == 1 else False
        fig.add_trace(
            go.Scatter(
                x=smd_scores.loc[feature, ["unmatched", "matched"]],
                y=[feature, feature],
                mode='lines+markers',
                showlegend=show_legend,
                name="unmatched",
                line=dict(color="#217883"),
                marker=dict(
                    size=[20, 20],
                    color=["#27c1d1", "#217883"]
                )

            ),
                row=row,
            col=1
        )
    fig.update_layout(
        height=1000, width=1200,
        title_text="smd scores for propensity score matching"
    )
    fig.update_traces(textposition="bottom right")
    fig.show()
    fig.write_html(f"outputs/smd_scores.html")


def plot_smd_old(smd_scores):
    fig = go.Figure()
    for score in ["unmatched", "matched"]:
        fig.add_trace(
            go.Scatter(
            x=smd_scores[score],
            y=smd_scores.index,
            name="unmatched",
            mode='markers',
            marker=dict(size=16)
            )
        )

    fig.update_traces(textposition="bottom right")
    fig.write_html(f"outputs/smd_scores_old.html")
# Overlay both histograms # Reduce opacity to see both histograms