Skip to content

Commit

Permalink
Merge pull request #310 from pegasystems/IH_analysis_improvements
Browse files Browse the repository at this point in the history
Improved method signatures and examples
  • Loading branch information
operdeck authored Jan 3, 2025
2 parents 2157a90 + 7f8bc3a commit 5f6a5c6
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 125 deletions.
39 changes: 11 additions & 28 deletions examples/ih/Conversion_Reporting.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,12 @@
"from pathlib import Path\n",
"import polars as pl\n",
"\n",
"ih_export_file = Path(\n",
" \"./Data-pxStrategyResult_InteractionFiles_20241213T091932_GMT.zip\"\n",
")\n",
"\n",
"if not ih_export_file.exists():\n",
" ih = IH.from_mock_data()\n",
"else:\n",
" ih = IH.from_ds_export(\n",
" ih_export_file,\n",
" query=pl.col.ExperimentGroup.is_not_null() & (pl.col.ExperimentGroup != \"\"),\n",
" )\n",
"ih = IH.from_mock_data()\n",
"# ih = IH.from_ds_export(\n",
"# \"./Data-pxStrategyResult_InteractionFiles_20241213T091932_GMT.zip\",\n",
"# query=pl.col.ExperimentGroup.is_not_null() & (pl.col.ExperimentGroup != \"\"),\n",
"# )\n",
"\n",
"ih.aggregates.summary_success_rates(by=[\"ExperimentGroup\", \"Channel\"]).drop(\n",
" \"Outcomes\"\n",
Expand Down Expand Up @@ -79,7 +74,7 @@
"metadata": {},
"outputs": [],
"source": [
"ih.plot.success_rates_tree_map(metric=\"Conversion\")\n"
"ih.plot.success_rate_tree_map(metric=\"Conversion\")\n"
]
},
{
Expand All @@ -97,20 +92,7 @@
"metadata": {},
"outputs": [],
"source": [
"ih.plot.success_rates_trend_bar(\n",
" metric=\"Conversion\",\n",
" condition=\"ExperimentGroup\",\n",
" every=\"1w\",\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ih.plot.success_rates_trend(metric=\"Conversion\", every=\"1d\")"
"ih.plot.success_rate(metric=\"Conversion\", every=\"1d\")"
]
},
{
Expand Down Expand Up @@ -139,8 +121,8 @@
"metadata": {},
"outputs": [],
"source": [
"ih.plot.success_rates_trend(\n",
" by=\"Channel\"\n",
"ih.plot.success_rate(\n",
" facet=\"Channel\"\n",
")"
]
}
Expand All @@ -160,7 +142,8 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
Expand Down
75 changes: 51 additions & 24 deletions examples/ih/Example_IH_Analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"ih = IH.from_ds_export(\n",
" \"../../data/Data-pxStrategyResult_pxInteractionHistory_20210101T010000_GMT.zip\"\n",
")"
"# ih = IH.from_ds_export(\n",
"# \"../../data/Data-pxStrategyResult_pxInteractionHistory_20210101T010000_GMT.zip\"\n",
"# )\n",
"ih = IH.from_mock_data()"
]
},
{
Expand All @@ -57,6 +58,11 @@
"ih.data.head().collect()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -115,7 +121,7 @@
"metadata": {},
"outputs": [],
"source": [
"ih.plot.response_counts(every=\"1d\")"
"ih.plot.response_count(every=\"1d\")"
]
},
{
Expand All @@ -131,8 +137,8 @@
"metadata": {},
"outputs": [],
"source": [
"ih.plot.response_counts(\n",
" by=\"Channel\",\n",
"ih.plot.response_count(\n",
" facet=\"Channel\",\n",
" query=pl.col.Channel != \"\",\n",
")"
]
Expand All @@ -159,8 +165,8 @@
"metadata": {},
"outputs": [],
"source": [
"ih.plot.success_rates_trend(\n",
" by=\"Channel\", query=pl.col.Channel.is_not_null() & (pl.col.Channel != \"\")\n",
"ih.plot.success_rate(\n",
" facet=\"Channel\", query=pl.col.Channel.is_not_null() & (pl.col.Channel != \"\")\n",
")"
]
},
Expand Down Expand Up @@ -218,10 +224,9 @@
" .to_list()\n",
" for c in channels\n",
"]\n",
"\n",
"fig = ff.create_distplot(plot_data, group_labels=channels, show_hist=False)\n",
"fig.update_layout(title=\"Propensity Distribution\")\n",
"fig"
"fig.update_layout(title=\"Propensity Distribution\", yaxis=dict(showticklabels=False))\n",
"fig\n"
]
},
{
Expand Down Expand Up @@ -250,23 +255,44 @@
" .to_list()\n",
" if c is not None and c != \"\"\n",
"]\n",
"plot_data=ih.data.filter(pl.col.OutcomeTime.is_not_null()).group_by(\"InteractionID\").agg(\n",
" [pl.col.OutcomeTime.min().alias(\"Decision_Time\")]+\n",
" [pl.col.OutcomeTime.filter(pl.col.Outcome == o).max().alias(o) for o in outcomes],\n",
").collect().unpivot(\n",
" index=[\"InteractionID\", \"Decision_Time\"],\n",
" variable_name=\"Outcome\",\n",
" value_name=\"Time\",\n",
").with_columns(\n",
" Duration = (pl.col.Time - pl.col.Decision_Time).dt.total_seconds()\n",
").filter(pl.col.Duration > 0)\n",
"plot_data = (\n",
" ih.data.filter(pl.col.OutcomeTime.is_not_null())\n",
" .group_by(\"InteractionID\")\n",
" .agg(\n",
" [pl.col.OutcomeTime.min().alias(\"Decision_Time\")]\n",
" + [\n",
" pl.col.OutcomeTime.filter(pl.col.Outcome == o).max().alias(o)\n",
" for o in outcomes\n",
" ],\n",
" )\n",
" .collect()\n",
" .unpivot(\n",
" index=[\"InteractionID\", \"Decision_Time\"],\n",
" variable_name=\"Outcome\",\n",
" value_name=\"Time\",\n",
" )\n",
" .with_columns(Duration=(pl.col.Time - pl.col.Decision_Time).dt.total_seconds())\n",
" .filter(pl.col.Duration > 0)\n",
")\n",
"\n",
"ordered_outcomes = (\n",
" plot_data.group_by(\"Outcome\")\n",
" .agg(Duration=pl.col(\"Duration\").median())\n",
" .sort(\"Duration\")[\"Outcome\"]\n",
" .to_list()\n",
")\n",
"\n",
"fig = px.box(\n",
" plot_data,\n",
" x=\"Duration\",\n",
" y=\"Outcome\",\n",
" color=\"Outcome\",\n",
" template=\"pega\"\n",
" template=\"pega\",\n",
" category_orders={\"Outcome\": ordered_outcomes},\n",
" points=False,\n",
" title=\"Duration of Responses\"\n",
")\n",
"fig.update_layout(xaxis_title=\"Duration (seconds)\")\n",
"fig"
]
}
Expand All @@ -286,7 +312,8 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3"
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 5f6a5c6

Please sign in to comment.