pdstools.decision_analyzer.utils

Attributes

Functions

apply_filter(df[, filters])

Apply a global set of filters. Kept outside of the DecisionData class as

filtered_action_counts(→ polars.LazyFrame)

Returns a DataFrame with action counts filtered based on the given propensity and priority thresholds.

area_under_curve(df, col_x, col_y)

gini_coefficient(df, col_x, col_y)

get_first_level_stats(interaction_data[, filters])

Returns some first level stats of a dataframe. Used to

get_git_version_and_date()

determine_extract_type(raw_data)

rename_and_cast_types(→ polars.LazyFrame)

get_table_definition(table)

get_schema(→ Dict[str, Type[polars.DataType]])

Build type mapping for dataframe columns based on table definition.

create_hierarchical_selectors(→ Dict[str, Dict[str, ...)

Create hierarchical filter options and calculate indices for selectbox widgets.

get_scope_config(→ Dict[str, Union[str, polars.Expr, ...)

Generate scope configuration for lever application and plotting based on user selections.

Module Contents

NBADScope_Mapping
apply_filter(df: polars.LazyFrame, filters: polars.Expr | List[polars.Expr] | None = None)

Apply a global set of filters. Kept outside of the DecisionData class as this is really more of a utility function, not bound to that class at all.

Parameters:
  • df (polars.LazyFrame)

  • filters (Optional[Union[polars.Expr, List[polars.Expr]]])

filtered_action_counts(df: polars.LazyFrame, groupby_cols: list, propensityTH: float = None, priorityTH: float = None) polars.LazyFrame

Returns a DataFrame with action counts filtered based on the given propensity and priority thresholds.

Parameters:
  • df (pl.LazyFrame) – The input dataframe.

  • groupby_cols (list) – The list of column names to group by([“pxEngagementStage”, “pxInteractionID”]).

  • propensityTH (float) – The propensity threshold.

  • priorityTH (float) – The priority threshold.

Returns:

A DataFrame with action counts filtered based on the given propensity and priority thresholds.

Return type:

pl.LazyFrame

area_under_curve(df: polars.DataFrame, col_x: str, col_y: str)
Parameters:
  • df (polars.DataFrame)

  • col_x (str)

  • col_y (str)

gini_coefficient(df: polars.DataFrame, col_x: str, col_y: str)
Parameters:
  • df (polars.DataFrame)

  • col_x (str)

  • col_y (str)

get_first_level_stats(interaction_data: polars.LazyFrame, filters: List[polars.Expr] = None)

Returns some first level stats of a dataframe. Used to show effects of user data filters.

Parameters:
  • interaction_data (polars.LazyFrame)

  • filters (List[polars.Expr])

get_git_version_and_date()
determine_extract_type(raw_data)
rename_and_cast_types(df: polars.LazyFrame, table: Literal['decision_analyzer', 'explainability_extract'], include_cols: Iterable[str] | None = None) polars.LazyFrame
Parameters:
  • df (polars.LazyFrame)

  • table (Literal['decision_analyzer', 'explainability_extract'])

  • include_cols (Optional[Iterable[str]])

Return type:

polars.LazyFrame

get_table_definition(table: str)
Parameters:

table (str)

get_schema(df: polars.LazyFrame, table: str, include_cols: Iterable[str]) Dict[str, Type[polars.DataType]]

Build type mapping for dataframe columns based on table definition.

Parameters:
  • df (polars.LazyFrame)

  • table (str)

  • include_cols (Iterable[str])

Return type:

Dict[str, Type[polars.DataType]]

create_hierarchical_selectors(data: polars.LazyFrame, selected_issue: str | None = None, selected_group: str | None = None, selected_action: str | None = None) Dict[str, Dict[str, List[str] | int]]

Create hierarchical filter options and calculate indices for selectbox widgets.

Args:

data: LazyFrame with hierarchical data (should be pre-filtered to desired stage) selected_issue: Currently selected issue (optional) selected_group: Currently selected group (optional) selected_action: Currently selected action (optional)

Returns:

Dict with structure: {

“issues”: {“options”: […], “index”: 0}, “groups”: {“options”: [“All”, …], “index”: 0}, “actions”: {“options”: [“All”, …], “index”: 0}

}

Parameters:
  • data (polars.LazyFrame)

  • selected_issue (Optional[str])

  • selected_group (Optional[str])

  • selected_action (Optional[str])

Return type:

Dict[str, Dict[str, Union[List[str], int]]]

get_scope_config(selected_issue: str, selected_group: str, selected_action: str) Dict[str, str | polars.Expr | List[str]]

Generate scope configuration for lever application and plotting based on user selections.

This utility function determines the appropriate scope level (Issue, Group, or Action) based on hierarchical user selections and returns configuration needed for both lever condition generation and plotting.

Parameters:
  • selected_issue (str) – Selected issue value from dropdown (can be “All”)

  • selected_group (str) – Selected group value from dropdown (can be “All”)

  • selected_action (str) – Selected action value from dropdown (can be “All”)

Returns:

Configuration dictionary containing: - level: “Action”, “Group”, or “Issue” indicating scope level - lever_condition: Polars expression for filtering selected actions - group_cols: List of column names for grouping operations - x_col: Column name to use for x-axis in plots - selected_value: The actual selected value for highlighting - plot_title_prefix: Prefix for plot titles

Return type:

Dict[str, Union[str, pl.Expr, List[str]]]

Notes

The function follows hierarchical logic: - If action != “All”: Action-level scope - Elif group != “All”: Group-level scope - Else: Issue-level scope

Examples

Action-level selection: >>> config = get_scope_config(“Service”, “Cards”, “SpecificAction”) >>> config[“level”] # “Action” >>> config[“lever_condition”] # pl.col(“pyName”) == “SpecificAction”

Group-level selection: >>> config = get_scope_config(“Service”, “Cards”, “All”) >>> config[“level”] # “Group” >>> config[“lever_condition”] # (pl.col(“pyIssue”) == “Service”) & (pl.col(“pyGroup”) == “Cards”)