Skip to content

Commit

Permalink
Add simulations to streamlit page
Browse files Browse the repository at this point in the history
  • Loading branch information
jbreffle committed Feb 17, 2024
1 parent e79bdfc commit 0bfdc52
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 21 deletions.
54 changes: 53 additions & 1 deletion notebooks/3a_sim_simple.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions notebooks/3b_sim_poisson.ipynb

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions src/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,15 @@ def sim_scatter_hist(wpm, acc, fig=None, **hist_kwargs):
ax.text(
0.05,
0.95,
# f"R={wpm_acc_linregres.rvalue:.2f}, p={wpm_acc_linregres.pvalue:.2f}",
rf"$R^2={np.square(wpm_acc_linregres.rvalue):.4f},\ p={wpm_acc_linregres.pvalue:.4f}$",
transform=ax.transAxes,
verticalalignment="top",
horizontalalignment="left",
fontsize=8,
color="black",
bbox=dict(facecolor="white", alpha=0.5),
)
# Configure plot
ax.set_xlabel("WPM")
ax.set_ylabel("Accuracy")
ax.set_title("Simulated WPM vs Accuracy")
ax_histx.tick_params(axis="x", labelbottom=False)
ax_histy.tick_params(axis="y", labelleft=False)
ax_histx.set_ylabel("Count")
Expand Down
12 changes: 6 additions & 6 deletions src/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
# Functions
def run_simulation_poisson(
avg_wpm=60,
avg_acc=0.975,
avg_acc=0.95,
duration=60,
n_trials=500,
error_cost=0.0,
n_trials=1000,
error_cost=0.75,
dt=0.005,
silent=False,
):
Expand Down Expand Up @@ -88,11 +88,11 @@ def run_simulation_poisson(

def run_simulation_simple(
avg_wpm=60,
avg_acc=0.975,
avg_acc=0.95,
duration=60,
n_trials=10000,
error_mean=0.1,
error_std=0.1,
error_mean=0.5,
error_std=0.45,
silent=False,
use_lognormal=True,
):
Expand Down
67 changes: 59 additions & 8 deletions streamlit/pages/2_Simulated_typing.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt

from src import util
from src import plot

import Home


@st.cache_data
def run_poisson_sim(**kwargs):
np.random.seed(1)
if "silent" not in kwargs:
kwargs["silent"] = True
wpm, acc, n_mistakes = util.run_simulation_poisson(**kwargs)
return wpm, acc, n_mistakes


@st.cache_data
def run_simple_sim(**kwargs):
np.random.seed(1)
if "silent" not in kwargs:
kwargs["silent"] = True
wpm, acc, n_mistakes = util.run_simulation_simple(**kwargs)
return wpm, acc, n_mistakes


def main():
"""New page."""

Expand Down Expand Up @@ -32,31 +55,59 @@ def main():
st.divider()

st.subheader("Simulated typing: random mistake draws")
nb_url_1 = "https://github.com/jbreffle/monkeytype-analysis/blob/main/notebooks/3a_sim_simple.ipynb"
st.write(
f"""
One method to simulate typing is to randomly draw mistakes.
Click here
[./notebooks/3a_sim_simple.ipynb]({nb_url_1})
for plots that will eventually be included in this app.
"""
)
# TODO
avg_wpm = 60
avg_acc = 0.95
n_trials = 1000
wpm, acc, n_mistakes = run_simple_sim(
avg_wpm=avg_wpm, avg_acc=avg_acc, n_trials=n_trials
)
# Plot scatter_hist of wpm and acc
fig = plt.figure(figsize=(6, 4))
ax, ax_histx, ax_histy = plot.sim_scatter_hist(wpm, acc, fig=fig)
ax.axvline(avg_wpm, color="grey", linestyle="--", alpha=0.5)
ax.axhline(avg_acc, color="grey", linestyle="--", alpha=0.5)
ax.plot(np.mean(wpm), np.mean(acc), "ro")
st.pyplot(fig, use_container_width=True, transparent=True)
# TODO
st.divider()

st.subheader("Simulated typing: Poisson process")
nb_url_2 = "https://github.com/jbreffle/monkeytype-analysis/blob/main/notebooks/3b_sim_poisson.ipynb"
st.write(
f"""
An alternative simulation method is to use a Poisson process.
"""
)
avg_wpm = 60
avg_acc = 0.95
wpm, acc, n_mistakes = run_poisson_sim(avg_wpm=avg_wpm, avg_acc=avg_acc)
# Plot scatter_hist of wpm and acc
fig = plt.figure(figsize=(6, 4))
ax, ax_histx, ax_histy = plot.sim_scatter_hist(wpm, acc, fig=fig)
ax.axvline(avg_wpm, color="grey", linestyle="--", alpha=0.5)
ax.axhline(avg_acc, color="grey", linestyle="--", alpha=0.5)
ax.plot(np.mean(wpm), np.mean(acc), "ro")
st.pyplot(fig, use_container_width=True, transparent=True)
st.divider()

nb_url_1 = "https://github.com/jbreffle/monkeytype-analysis/blob/main/notebooks/3a_sim_simple.ipynb"
nb_url_2 = "https://github.com/jbreffle/monkeytype-analysis/blob/main/notebooks/3b_sim_poisson.ipynb"
st.write(
f"""
Click here
[./notebooks/3a_sim_simple.ipynb]({nb_url_1})
for the simple simulation method notebook.
Click here
[./notebooks/3a_sim_poisson.ipynb]({nb_url_2})
for plots that will eventually be included in this app.
for the Poisson simulation notebook.
"""
)
# TODO


if __name__ == "__main__":
Expand Down

0 comments on commit 0bfdc52

Please sign in to comment.