File size: 3,432 Bytes
774658e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from typing import Optional

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go


def validate_columns(df: pd.DataFrame, required_columns: list[str]) -> None:
    """
    Validate that required columns exist in a DataFrame.
    """
    missing_columns = [column for column in required_columns if column not in df.columns]

    if missing_columns:
        raise ValueError(f"Missing required column(s): {missing_columns}")


def bar_chart_top_values(
    df: pd.DataFrame,
    x_col: str,
    y_col: str,
    title: str,
    x_label: Optional[str] = None,
    y_label: Optional[str] = None,
) -> go.Figure:
    """
    Create a bar chart for ranked or grouped values.

    Example use cases:
    - Top providers by claim count
    - Claim counts by state
    - Average reimbursement by chronic condition
    """
    validate_columns(df, [x_col, y_col])

    fig = px.bar(
        df,
        x=x_col,
        y=y_col,
        title=title,
        labels={
            x_col: x_label or x_col,
            y_col: y_label or y_col,
        },
    )

    fig.update_layout(
        xaxis_title=x_label or x_col,
        yaxis_title=y_label or y_col,
        margin=dict(l=40, r=40, t=70, b=40),
    )

    return fig


def horizontal_bar_chart(
    df: pd.DataFrame,
    x_col: str,
    y_col: str,
    title: str,
    x_label: Optional[str] = None,
    y_label: Optional[str] = None,
) -> go.Figure:
    """
    Create a horizontal bar chart.

    Useful when category labels are long, such as Provider IDs.
    """
    validate_columns(df, [x_col, y_col])

    fig = px.bar(
        df,
        x=x_col,
        y=y_col,
        orientation="h",
        title=title,
        labels={
            x_col: x_label or x_col,
            y_col: y_label or y_col,
        },
    )

    fig.update_layout(
        xaxis_title=x_label or x_col,
        yaxis_title=y_label or y_col,
        yaxis=dict(autorange="reversed"),
        margin=dict(l=40, r=40, t=70, b=40),
    )

    return fig


def histogram(
    df: pd.DataFrame,
    column: str,
    title: str,
    x_label: Optional[str] = None,
    nbins: int = 30,
) -> go.Figure:
    """
    Create a histogram for numeric distributions.

    Example use cases:
    - Distribution of inpatient reimbursement
    - Distribution of beneficiary age
    """
    validate_columns(df, [column])

    fig = px.histogram(
        df,
        x=column,
        nbins=nbins,
        title=title,
        labels={column: x_label or column},
    )

    fig.update_layout(
        xaxis_title=x_label or column,
        yaxis_title="Count",
        margin=dict(l=40, r=40, t=70, b=40),
    )

    return fig


def box_plot(
    df: pd.DataFrame,
    x_col: str,
    y_col: str,
    title: str,
    x_label: Optional[str] = None,
    y_label: Optional[str] = None,
) -> go.Figure:
    """
    Create a box plot for comparing distributions across groups.

    Example use cases:
    - Reimbursement by chronic condition
    - Claim amount distribution by state
    """
    validate_columns(df, [x_col, y_col])

    fig = px.box(
        df,
        x=x_col,
        y=y_col,
        title=title,
        labels={
            x_col: x_label or x_col,
            y_col: y_label or y_col,
        },
    )

    fig.update_layout(
        xaxis_title=x_label or x_col,
        yaxis_title=y_label or y_col,
        margin=dict(l=40, r=40, t=70, b=40),
    )

    return fig