Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies = [
"polarstate==0.1.8",
]
name = "rtichoke"
version = "0.1.13"
version = "0.1.14"
description = "interactive visualizations for performance of predictive models"
readme = "README.md"

Expand Down
129 changes: 70 additions & 59 deletions src/rtichoke/discrimination/gains.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
"""
A module for Gains Curves
A module for Gains Curves using Plotly helpers
"""

from typing import Dict, List, Optional
from pandas import DataFrame
from typing import Dict, List, Sequence, Union
from plotly.graph_objs._figure import Figure
from rtichoke.helpers.send_post_request_to_r_rtichoke import create_rtichoke_curve
from rtichoke.helpers.send_post_request_to_r_rtichoke import plot_rtichoke_curve
from rtichoke.helpers.plotly_helper_functions import (
_create_rtichoke_plotly_curve_binary,
_plot_rtichoke_curve_binary,
)
import numpy as np
import polars as pl


def create_gains_curve(
probs: Dict[str, List[float]],
reals: Dict[str, List[int]],
probs: Dict[str, np.ndarray],
reals: Union[np.ndarray, Dict[str, np.ndarray]],
by: float = 0.01,
stratified_by: str = "probability_threshold",
size: Optional[int] = None,
stratified_by: Sequence[str] = ["probability_threshold"],
size: int = 600,
color_values: List[str] = [
"#1b9e77",
"#d95f02",
Expand All @@ -37,78 +40,86 @@ def create_gains_curve(
"#D1603D",
"#585123",
],
url_api: str = "http://localhost:4242/",
) -> Figure:
"""Create Gains Curve
"""Create Gains Curve.

Args:
probs (Dict[str, List[float]]): _description_
reals (Dict[str, List[int]]): _description_
by (float, optional): _description_. Defaults to 0.01.
stratified_by (str, optional): _description_. Defaults to "probability_threshold".
size (Optional[int], optional): _description_. Defaults to None.
color_values (List[str], optional): _description_. Defaults to None.
url_api (_type_, optional): _description_. Defaults to "http://localhost:4242/".
Parameters
----------
probs : Dict[str, np.ndarray]
Dictionary mapping a label or group name to an array of predicted
probabilities for the positive class.
reals : Union[np.ndarray, Dict[str, np.ndarray]]
Ground-truth binary labels (0/1) as a single array, or a dictionary
mapping the same label/group keys used in ``probs`` to arrays of
ground-truth labels.
by : float, optional
Resolution for probability thresholds when computing the curve
(step size). Default is 0.01.
stratified_by : Sequence[str], optional
Sequence of column names to stratify the performance data by.
Default is ["probability_threshold"].
size : int, optional
Plot size in pixels (width and height). Default is 600.
color_values : List[str], optional
List of color hex strings to use for the plotted lines. If not
provided, a default palette is used.

Returns:
Figure: _description_
Returns
-------
Figure
A Plotly ``Figure`` containing the Gains curve(s).

Notes
-----
The function delegates computation and plotting to
``_create_rtichoke_plotly_curve_binary`` and returns the resulting
Plotly figure.
"""
fig = create_rtichoke_curve(
fig = _create_rtichoke_plotly_curve_binary(
probs,
reals,
by=by,
stratified_by=stratified_by,
size=size,
color_values=color_values,
url_api=url_api,
curve="gains",
)
return fig


def plot_gains_curve(
performance_data: DataFrame,
size: Optional[int] = None,
color_values: List[str] = [
"#1b9e77",
"#d95f02",
"#7570b3",
"#e7298a",
"#07004D",
"#E6AB02",
"#FE5F55",
"#54494B",
"#006E90",
"#BC96E6",
"#52050A",
"#1F271B",
"#BE7C4D",
"#63768D",
"#08A045",
"#320A28",
"#82FF9E",
"#2176FF",
"#D1603D",
"#585123",
],
url_api: str = "http://localhost:4242/",
performance_data: pl.DataFrame,
stratified_by: Sequence[str] = ["probability_threshold"],
size: int = 600,
) -> Figure:
"""Plot Gains Curve
"""Plot Gains curve from performance data.

Parameters
----------
performance_data : pl.DataFrame
A Polars DataFrame containing performance metrics for the Gains curve.
Expected columns include (but may not be limited to)
``probability_threshold`` and gains-related metrics, plus any
stratification columns.
stratified_by : Sequence[str], optional
Sequence of column names used for stratification in the
``performance_data``. Default is ["probability_threshold"].
size : int, optional
Plot size in pixels (width and height). Default is 600.

Args:
performance_data (DataFrame): _description_
size (Optional[int], optional): _description_. Defaults to None.
color_values (List[str], optional): _description_. Defaults to None.
url_api (_type_, optional): _description_. Defaults to "http://localhost:4242/".
Returns
-------
Figure
A Plotly ``Figure`` containing the Gains plot.

Returns:
Figure: _description_
Notes
-----
This function wraps ``_plot_rtichoke_curve_binary`` to produce a
ready-to-render Plotly figure from precomputed performance data.
"""
fig = plot_rtichoke_curve(
fig = _plot_rtichoke_curve_binary(
performance_data,
size=size,
color_values=color_values,
url_api=url_api,
curve="gains",
)
return fig
129 changes: 70 additions & 59 deletions src/rtichoke/discrimination/lift.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
"""
A module for Lift Curves
A module for Lift Curves using Plotly helpers
"""

from typing import Dict, List, Optional
from typing import Dict, List, Sequence, Union
from plotly.graph_objs._figure import Figure
from pandas import DataFrame
from rtichoke.helpers.send_post_request_to_r_rtichoke import create_rtichoke_curve
from rtichoke.helpers.send_post_request_to_r_rtichoke import plot_rtichoke_curve
from rtichoke.helpers.plotly_helper_functions import (
_create_rtichoke_plotly_curve_binary,
_plot_rtichoke_curve_binary,
)
import numpy as np
import polars as pl


def create_lift_curve(
probs: Dict[str, List[float]],
reals: Dict[str, List[int]],
probs: Dict[str, np.ndarray],
reals: Union[np.ndarray, Dict[str, np.ndarray]],
by: float = 0.01,
stratified_by: str = "probability_threshold",
size: Optional[int] = None,
stratified_by: Sequence[str] = ["probability_threshold"],
size: int = 600,
color_values: List[str] = [
"#1b9e77",
"#d95f02",
Expand All @@ -37,78 +40,86 @@ def create_lift_curve(
"#D1603D",
"#585123",
],
url_api: str = "http://localhost:4242/",
) -> Figure:
"""Create Lift Curve
"""Create Lift Curve.

Args:
probs (Dict[str, List[float]]): _description_
reals (Dict[str, List[int]]): _description_
by (float, optional): _description_. Defaults to 0.01.
stratified_by (str, optional): _description_. Defaults to "probability_threshold".
size (Optional[int], optional): _description_. Defaults to None.
color_values (List[str], optional): _description_. Defaults to None.
url_api (_type_, optional): _description_. Defaults to "http://localhost:4242/".
Parameters
----------
probs : Dict[str, np.ndarray]
Dictionary mapping a label or group name to an array of predicted
probabilities for the positive class.
reals : Union[np.ndarray, Dict[str, np.ndarray]]
Ground-truth binary labels (0/1) as a single array, or a dictionary
mapping the same label/group keys used in ``probs`` to arrays of
ground-truth labels.
by : float, optional
Resolution for probability thresholds when computing the curve
(step size). Default is 0.01.
stratified_by : Sequence[str], optional
Sequence of column names to stratify the performance data by.
Default is ["probability_threshold"].
size : int, optional
Plot size in pixels (width and height). Default is 600.
color_values : List[str], optional
List of color hex strings to use for the plotted lines. If not
provided, a default palette is used.

Returns:
Figure: _description_
Returns
-------
Figure
A Plotly ``Figure`` containing the Lift curve(s).

Notes
-----
The function delegates computation and plotting to
``_create_rtichoke_plotly_curve_binary`` and returns the resulting
Plotly figure.
"""
fig = create_rtichoke_curve(
fig = _create_rtichoke_plotly_curve_binary(
probs,
reals,
by=by,
stratified_by=stratified_by,
size=size,
color_values=color_values,
url_api=url_api,
curve="lift",
)
return fig


def plot_lift_curve(
performance_data: DataFrame,
size: Optional[int] = None,
color_values: List[str] = [
"#1b9e77",
"#d95f02",
"#7570b3",
"#e7298a",
"#07004D",
"#E6AB02",
"#FE5F55",
"#54494B",
"#006E90",
"#BC96E6",
"#52050A",
"#1F271B",
"#BE7C4D",
"#63768D",
"#08A045",
"#320A28",
"#82FF9E",
"#2176FF",
"#D1603D",
"#585123",
],
url_api: str = "http://localhost:4242/",
performance_data: pl.DataFrame,
stratified_by: Sequence[str] = ["probability_threshold"],
size: int = 600,
) -> Figure:
"""Plot Lift Curve
"""Plot Lift curve from performance data.

Parameters
----------
performance_data : pl.DataFrame
A Polars DataFrame containing performance metrics for the Lift curve.
Expected columns include (but may not be limited to)
``probability_threshold`` and lift-related metrics, plus any
stratification columns.
stratified_by : Sequence[str], optional
Sequence of column names used for stratification in the
``performance_data``. Default is ["probability_threshold"].
size : int, optional
Plot size in pixels (width and height). Default is 600.

Args:
performance_data (DataFrame): _description_
size (Optional[int], optional): _description_. Defaults to None.
color_values (List[str], optional): _description_. Defaults to None.
url_api (_type_, optional): _description_. Defaults to "http://localhost:4242/".
Returns
-------
Figure
A Plotly ``Figure`` containing the Lift plot.

Returns:
Figure: _description_
Notes
-----
This function wraps ``_plot_rtichoke_curve_binary`` to produce a
ready-to-render Plotly figure from precomputed performance data.
"""
fig = plot_rtichoke_curve(
fig = _plot_rtichoke_curve_binary(
performance_data,
size=size,
color_values=color_values,
url_api=url_api,
curve="lift",
)
return fig
Loading