Skip to content

Commit

Permalink
Adds additional exercises (#54)
Browse files Browse the repository at this point in the history
* Add Basic UI problems

* Reactivity exercises

* Add dynamic UI exercises

* Reactive event and calc exercises
  • Loading branch information
Gordon Shotwell authored Apr 3, 2024
1 parent f9d3276 commit 51fd0ad
Show file tree
Hide file tree
Showing 84 changed files with 10,579 additions and 9,243 deletions.
1 change: 1 addition & 0 deletions apps/problem-sets/2-basic-ui/2.1-sidebar/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use `ui.sidebar()` to put the Account dropdown in a sidebar.
31 changes: 31 additions & 0 deletions apps/problem-sets/2-basic-ui/2.1-sidebar/app-solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from shiny.express import render, ui, input
from data_import import df
from plots import plot_auc_curve, plot_precision_recall_curve
from shinywidgets import render_plotly

with ui.sidebar():
ui.input_select(
"account",
"Account",
choices=[
"Berge & Berge",
"Fritsch & Fritsch",
"Hintz & Hintz",
"Mosciski and Sons",
"Wolff Ltd",
],
)


@render_plotly
def precision_recall_plot():
account_subset = df[df["account"] == input.account()]
return plot_precision_recall_curve(
account_subset, "is_electronics", "training_score"
)


@render_plotly
def auc_plot():
account_subset = df[df["account"] == input.account()]
return plot_auc_curve(account_subset, "is_electronics", "training_score")
30 changes: 30 additions & 0 deletions apps/problem-sets/2-basic-ui/2.1-sidebar/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from shiny.express import render, ui, input
from data_import import df
from plots import plot_auc_curve, plot_precision_recall_curve
from shinywidgets import render_plotly

ui.input_select(
"account",
"Account",
choices=[
"Berge & Berge",
"Fritsch & Fritsch",
"Hintz & Hintz",
"Mosciski and Sons",
"Wolff Ltd",
],
)


@render_plotly
def precision_recall_plot():
account_subset = df[df["account"] == input.account()]
return plot_precision_recall_curve(
account_subset, "is_electronics", "training_score"
)


@render_plotly
def auc_plot():
account_subset = df[df["account"] == input.account()]
return plot_auc_curve(account_subset, "is_electronics", "training_score")
8 changes: 8 additions & 0 deletions apps/problem-sets/2-basic-ui/2.1-sidebar/data_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from pathlib import Path
import pandas as pd
import numpy as np

file_path = Path(__file__).parent / "simulated-data.csv"

df = pd.read_csv(file_path, dtype={"sub_account": str})
df["date"] = pd.to_datetime(df["date"], errors="coerce")
62 changes: 62 additions & 0 deletions apps/problem-sets/2-basic-ui/2.1-sidebar/plots.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import plotly.express as px
from pandas import DataFrame
import pandas as pd
from sklearn.metrics import roc_curve, auc, precision_recall_curve
import numpy as np


import plotly.io as pio

# Set the default plotly theme to resemble ggplot's theme_light
pio.templates.default = "plotly_white"


def plot_score_distribution(df: DataFrame):
fig = px.histogram(df, x="training_score", nbins=50, title="Model scores")
fig.update_layout(xaxis_title="Score", yaxis_title="Density")
return fig


def plot_auc_curve(df: DataFrame, true_col: str, pred_col: str):
fpr, tpr, _ = roc_curve(df[true_col], df[pred_col])
roc_auc = auc(fpr, tpr)

roc_df = DataFrame({"False Positive Rate": fpr, "True Positive Rate": tpr})

fig = px.line(
roc_df,
x="False Positive Rate",
y="True Positive Rate",
title=f"Receiver Operating Characteristic (ROC) - AUC: {roc_auc.round(2)}",
labels={
"False Positive Rate": "False Positive Rate",
"True Positive Rate": "True Positive Rate",
},
)
fig.add_shape(type="line", line=dict(dash="dash"), x0=0, x1=1, y0=0, y1=1)
return fig


def plot_precision_recall_curve(df: DataFrame, true_col: str, pred_col: str):
precision, recall, _ = precision_recall_curve(df[true_col], df[pred_col])

pr_df = DataFrame({"Recall": recall, "Precision": precision})

fig = px.line(
pr_df,
x="Recall",
y="Precision",
title="Precision-Recall Curve",
labels={"Recall": "Recall", "Precision": "Precision"},
)
return fig


def plot_api_response(df):
account = df["account"].unique()

data = np.random.lognormal(0, 1 / len(account), 10000)
df = pd.DataFrame({"Value": data})
fig = px.histogram(df, x="Value", nbins=50, title="API response time")
fig.update_layout(xaxis_title="Seconds", yaxis_title="Density")
return fig
1,001 changes: 1,001 additions & 0 deletions apps/problem-sets/2-basic-ui/2.1-sidebar/simulated-data.csv

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions apps/problem-sets/2-basic-ui/2.2-cards/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Wrap the two plots in cards.
34 changes: 34 additions & 0 deletions apps/problem-sets/2-basic-ui/2.2-cards/app-solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from shiny.express import render, ui, input
from data_import import df
from plots import plot_auc_curve, plot_precision_recall_curve
from shinywidgets import render_plotly

with ui.sidebar():
ui.input_select(
"account",
"Account",
choices=[
"Berge & Berge",
"Fritsch & Fritsch",
"Hintz & Hintz",
"Mosciski and Sons",
"Wolff Ltd",
],
)

with ui.card():

@render_plotly
def precision_recall_plot():
account_subset = df[df["account"] == input.account()]
return plot_precision_recall_curve(
account_subset, "is_electronics", "training_score"
)


with ui.card():

@render_plotly
def auc_plot():
account_subset = df[df["account"] == input.account()]
return plot_auc_curve(account_subset, "is_electronics", "training_score")
31 changes: 31 additions & 0 deletions apps/problem-sets/2-basic-ui/2.2-cards/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from shiny.express import render, ui, input
from data_import import df
from plots import plot_auc_curve, plot_precision_recall_curve
from shinywidgets import render_plotly

with ui.sidebar():
ui.input_select(
"account",
"Account",
choices=[
"Berge & Berge",
"Fritsch & Fritsch",
"Hintz & Hintz",
"Mosciski and Sons",
"Wolff Ltd",
],
)


@render_plotly
def precision_recall_plot():
account_subset = df[df["account"] == input.account()]
return plot_precision_recall_curve(
account_subset, "is_electronics", "training_score"
)


@render_plotly
def auc_plot():
account_subset = df[df["account"] == input.account()]
return plot_auc_curve(account_subset, "is_electronics", "training_score")
8 changes: 8 additions & 0 deletions apps/problem-sets/2-basic-ui/2.2-cards/data_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from pathlib import Path
import pandas as pd
import numpy as np

file_path = Path(__file__).parent / "simulated-data.csv"

df = pd.read_csv(file_path, dtype={"sub_account": str})
df["date"] = pd.to_datetime(df["date"], errors="coerce")
62 changes: 62 additions & 0 deletions apps/problem-sets/2-basic-ui/2.2-cards/plots.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import plotly.express as px
from pandas import DataFrame
import pandas as pd
from sklearn.metrics import roc_curve, auc, precision_recall_curve
import numpy as np


import plotly.io as pio

# Set the default plotly theme to resemble ggplot's theme_light
pio.templates.default = "plotly_white"


def plot_score_distribution(df: DataFrame):
fig = px.histogram(df, x="training_score", nbins=50, title="Model scores")
fig.update_layout(xaxis_title="Score", yaxis_title="Density")
return fig


def plot_auc_curve(df: DataFrame, true_col: str, pred_col: str):
fpr, tpr, _ = roc_curve(df[true_col], df[pred_col])
roc_auc = auc(fpr, tpr)

roc_df = DataFrame({"False Positive Rate": fpr, "True Positive Rate": tpr})

fig = px.line(
roc_df,
x="False Positive Rate",
y="True Positive Rate",
title=f"Receiver Operating Characteristic (ROC) - AUC: {roc_auc.round(2)}",
labels={
"False Positive Rate": "False Positive Rate",
"True Positive Rate": "True Positive Rate",
},
)
fig.add_shape(type="line", line=dict(dash="dash"), x0=0, x1=1, y0=0, y1=1)
return fig


def plot_precision_recall_curve(df: DataFrame, true_col: str, pred_col: str):
precision, recall, _ = precision_recall_curve(df[true_col], df[pred_col])

pr_df = DataFrame({"Recall": recall, "Precision": precision})

fig = px.line(
pr_df,
x="Recall",
y="Precision",
title="Precision-Recall Curve",
labels={"Recall": "Recall", "Precision": "Precision"},
)
return fig


def plot_api_response(df):
account = df["account"].unique()

data = np.random.lognormal(0, 1 / len(account), 10000)
df = pd.DataFrame({"Value": data})
fig = px.histogram(df, x="Value", nbins=50, title="API response time")
fig.update_layout(xaxis_title="Seconds", yaxis_title="Density")
return fig
Loading

0 comments on commit 51fd0ad

Please sign in to comment.