Improve exception handling

This commit is contained in:
yagudin 2021-06-03 00:46:01 +03:00
parent 0ceec7a819
commit e2798436b3
2 changed files with 20 additions and 18 deletions

View File

@ -1,3 +1,4 @@
import logging
import numpy as np
@ -20,7 +21,18 @@ def calibration_curve(y_true, y_prob, *, n_bins=5, strategy="uniform"):
"must be either 'quantile' or 'uniform'."
)
try:
binids = np.digitize(y_prob, bins) - 1
except Exception as e:
np.set_printoptions(threshold=sys.maxsize)
logging.info("=" * 40)
logging.info(f"n_bins={n_bins}, strategy={strategy}")
logging.info(f"y_true={repr(y_prob)}")
logging.info(f"bins={repr(bins)}")
logging.info("=" * 40)
raise e
bin_sums = np.bincount(binids, weights=y_prob, minlength=len(bins))
bin_true = np.bincount(binids, weights=y_true, minlength=len(bins))

View File

@ -165,24 +165,14 @@ if __name__ == "__main__":
# ---
for strategy in ['uniform', 'quantile']:
for n_bins in range(1, 300):
try:
fig = plotly_calibration(y_true, y_pred, n_bins=n_bins, strategy=strategy)
# st.plotly_chart(fig, use_container_width=True)
st.plotly_chart(fig, use_container_width=True)
fig = plotly_calibration_odds(y_true, y_pred, n_bins=n_bins, strategy=strategy)
# st.plotly_chart(fig, use_container_width=True)
except:
st.warning("Hey! Unfortunately, there occured a mysterious error, which I haven't been able to reproduce locally. Try refreshing the page or changing the number of bins a bit. <3")
np.set_printoptions(threshold=sys.maxsize)
logging.info("=" * 40)
logging.info(f"n_bins={n_bins}, strategy={strategy}")
logging.info(f"true={repr(y_true)}")
logging.info(f"y_pred={repr(y_pred)}")
logging.info("=" * 40)
st.plotly_chart(fig, use_container_width=True)
except Exception as e:
st.warning("Hey! Unfortunately, a very mysterious error occured. Try refreshing the page or changing the number of bins a bit.")
# overconf = overconfidence(y_true, y_pred)
# st.write(f"Your over/under- confidence score is {overconf:.2f}.")