Skip to content

Commit 46a19eb

Browse files
committed
update to new r monitoring functions
1 parent ad383fb commit 46a19eb

1 file changed

Lines changed: 89 additions & 39 deletions

File tree

vetiver/monitor.py

Lines changed: 89 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import pins
2+
import matplotlib as plt
3+
import pandas as pd
24

35
# def vetiver_compute_metrics(data,
46
# date_var,
@@ -31,51 +33,99 @@
3133
# )
3234

3335

34-
def vetiver_create_pin_metrics(df_metrics,
35-
date_var,
36-
board,
37-
metrics_pin_name):
36+
def vetiver_create_pin_metrics(board,
37+
df_metrics,
38+
metrics_pin_name,
39+
#.index = .index,
40+
overwrite = True):
3841

39-
date_var <- quo_name(enquo(date_var))
42+
# date_var <- quo_name(enquo(date_var)) #enquo?
4043

4144
new_metrics = df_metrics.sort()
4245

43-
pins.pin_write(board, new_metrics, metrics_pin_name)
44-
46+
new_dates = df_metrics.index.unique()
4547

46-
def vetiver_update_pin_metrics(df_metrics,
47-
date_var,
48-
board,
49-
metrics_pin_name):
50-
51-
new_dates = df_metrics[date_var].unique()
5248
old_metrics = pins.pin_read(board, metrics_pin_name)
53-
old_metrics = vec_slice(
54-
old_metrics,
55-
old_metrics[date_var] not in new_dates
49+
overlapping_dates = old_metrics.index in new_dates
50+
51+
if overwrite is True:
52+
old_metrics = old_metrics not in overlapping_dates
53+
else:
54+
if overlapping_dates:
55+
raise ValueError(f"The new metrics overlap with dates \
56+
already stored in {repr(metrics_pin_name)} \
57+
Check the aggregated dates or use `overwrite = True`"
58+
)
59+
60+
new_metrics = old_metrics + df_metrics
61+
new_metrics <- vec_slice(
62+
new_metrics,
63+
vctrs::vec_order(new_metrics.index)
5664
)
57-
new_metrics <- vec_sort(vctrs::vec_rbind(old_metrics, df_metrics))
5865

5966
pins.pin_write(board, new_metrics, metrics_pin_name)
60-
61-
return new_metrics
62-
63-
def vetiver_plot_metrics(df_metrics,
64-
date_var,
65-
estimate = estimate,
66-
metric = metric,
67-
n = n):
68-
69-
70-
plt.plot(x = df_metrics, y = date_var, marker=".")
71-
72-
ggplot2::ggplot(data = df_metrics,
73-
ggplot2::aes({{ date_var }}, {{.estimate}})) +
74-
# ggplot2::geom_line(ggplot2::aes(color = !!.metric), alpha = 0.7) +
75-
# ggplot2::geom_point(ggplot2::aes(color = !!.metric,
76-
# size = {{n}}),
77-
# alpha = 0.9) +
78-
ggplot2::facet_wrap(ggplot2::vars(!!.metric),
79-
scales = "free_y", ncol = 1) +
80-
ggplot2::guides(color = "none") +
81-
ggplot2::labs(x = NULL, y = NULL)
67+
68+
69+
70+
def compute_metrics(data, date_var,
71+
metric_set,
72+
truth_quo,
73+
estimate_quo,
74+
*kw):
75+
index = data.date_var
76+
index = min(index)
77+
78+
n = len(data)
79+
80+
metrics = metric_set(
81+
data = data,
82+
truth = truth_quo,
83+
estimate = estimate_quo
84+
)
85+
86+
tibble::tibble(
87+
.index = index,
88+
.n = n,
89+
metrics
90+
)
91+
92+
93+
# def eval_select_one(col, data, arg, *kw, call = caller_env()):
94+
# rlang::check_installed("tidyselect")
95+
# check_dots_empty()
96+
97+
# # `col` is a quosure that has its own environment attached
98+
# env = empty_env()
99+
100+
# loc = tidyselect::eval_select(
101+
# expr = col,
102+
# data = data,
103+
# env = env,
104+
# error_call = call
105+
# )
106+
107+
# if (length(loc) != 1):
108+
# raise ValueError("`{arg}` must specify exactly one column from `data`.")
109+
110+
# return loc
111+
112+
113+
# def vetiver_plot_metrics(df_metrics,
114+
# date_var,
115+
# estimate = estimate,
116+
# metric = metric,
117+
# n = n):
118+
119+
120+
# plt.plot(x = df_metrics, y = date_var, marker=".")
121+
122+
# ggplot2::ggplot(data = df_metrics,
123+
# ggplot2::aes({{ date_var }}, {{.estimate}})) +
124+
# # ggplot2::geom_line(ggplot2::aes(color = !!.metric), alpha = 0.7) +
125+
# # ggplot2::geom_point(ggplot2::aes(color = !!.metric,
126+
# # size = {{n}}),
127+
# # alpha = 0.9) +
128+
# ggplot2::facet_wrap(ggplot2::vars(!!.metric),
129+
# scales = "free_y", ncol = 1) +
130+
# ggplot2::guides(color = "none") +
131+
# ggplot2::labs(x = NULL, y = NULL)

0 commit comments

Comments
 (0)