From 91cc8e7d69415c346f75606bb220edcd6b1416e8 Mon Sep 17 00:00:00 2001 From: "K. Shankari" Date: Wed, 8 May 2024 01:31:17 -0700 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=20Minor=20fixes=20to=20th?= =?UTF-8?q?e=20survey=20metrics?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - use `.get` to check whether this is an enketo survey so that it works for older deployments that predate the `survey_info` functionality as well. This has no functional difference since, if there is no `survey_info`, this is not a survey. But we get a better exception and avoid confusion later. - improve error handling for survey_responses, by splitting the attribute and name errors from the more complex errors --- viz_scripts/survey_metrics.ipynb | 2 +- viz_scripts/survey_responses.ipynb | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/viz_scripts/survey_metrics.ipynb b/viz_scripts/survey_metrics.ipynb index 19bc0e0..60886ca 100644 --- a/viz_scripts/survey_metrics.ipynb +++ b/viz_scripts/survey_metrics.ipynb @@ -72,7 +72,7 @@ "outputs": [], "source": [ "# Do not run this notebook at all unless it is for a survey configuration; nbclient will run up through this cell\n", - "if not survey_info['trip-labels'] == 'ENKETO':\n", + "if not survey_info.get('trip-labels', None) == 'ENKETO':\n", " ipython = get_ipython()\n", " ipython._showtraceback = scaffolding.no_traceback_handler\n", " raise Exception(\"The plots in this notebook are only relevant to deployments with trip-level surveys\")" diff --git a/viz_scripts/survey_responses.ipynb b/viz_scripts/survey_responses.ipynb index 08f205b..28d7c3f 100644 --- a/viz_scripts/survey_responses.ipynb +++ b/viz_scripts/survey_responses.ipynb @@ -53,7 +53,7 @@ "outputs": [], "source": [ "# Do not run this notebook at all unless it is for a survey configuration; nbclient will run up through this cell\n", - "if not survey_info['trip-labels'] == 'ENKETO':\n", + "if not survey_info.get('trip-labels', None) == 'ENKETO':\n", " ipython = get_ipython()\n", " ipython._showtraceback = scaffolding.no_traceback_handler\n", " raise Exception(\"The plots in this notebook are only relevant to deployments with trip-level surveys\")" @@ -372,10 +372,14 @@ " \n", " plot_and_text_stacked_bar_chart(plot_df, \"Responses\", ax, text_results, get_survey_colors(list(plot_df.index.values) ,color_map), debug_df)\n", " set_title_and_save(fig, text_results, plot_title, filename)\n", - "\n", - " except:\n", + " except (AttributeError, KeyError, pd.errors.UndefinedVariableError) as e:\n", + " plt.clf()\n", " generate_missing_plot(plot_title_no_quality, debug_df, filename)\n", - " alt_text = store_alt_text_missing(debug_df, filename, plot_title_no_quality)" + " alt_text = store_alt_text_missing(debug_df, filename, plot_title_no_quality) \n", + " alt_html = store_alt_html_missing(debug_df, filename, plot_title_no_quality)\n", + " except Exception as e:\n", + " fig, ax = plt.subplots()\n", + " plot_and_text_error(e, ax, filename)" ] } ],