From 3f8f91ba23a2c00b7300f96445e85cf3fe78ad23 Mon Sep 17 00:00:00 2001 From: muralivnv Date: Mon, 21 Sep 2020 20:35:29 -0400 Subject: [PATCH] updated distplot example; added conditional to block server script exit when exception occurs --- examples/for_seaborn/distplot.cpp | 51 +++++++++++++++++++------------ include/cppyplot_server.py | 11 ++++++- 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/examples/for_seaborn/distplot.cpp b/examples/for_seaborn/distplot.cpp index 80d67c8..cb104e2 100644 --- a/examples/for_seaborn/distplot.cpp +++ b/examples/for_seaborn/distplot.cpp @@ -12,46 +12,57 @@ int main() { - Eigen::Matrix mat; - mat.setRandom(); - Cppyplot::cppyplot pyp; - pyp.raw(R"pyp( - fig = plt.figure(figsize=(6,5)) - ax = sns.distplot(mat, kde=False, - hist_kws={"histtype": "bar", "rwidth": 3, - "alpha": 0.6, "color": "b"}) - sns.distplot(mat, kde=False, - hist_kws={"histtype": "step", "rwidth": 2, - "alpha": 0.9, "color": "orange"}) - ax.set_xlabel("Data bins", fontsize=12) - ax.set_ylabel("Frequency", fontsize=12) - ax.set_title("Histogram of C++ Eigen container", fontsize=14) - plt.show() - )pyp"); - pyp.data_args(_p(mat)); + Eigen::Matrix mat; // bivariate plot from https://seaborn.pydata.org/examples/layered_bivariate_plot.html - std::vector> rand_mat (1000, std::vector(2)); + std::vector> rand_mat (5000, std::vector(2)); std::random_device seed; std::mt19937 gen(seed()); - std::normal_distribution norm(0.0, 10.0); + std::normal_distribution norm(0.0, 5.0); - // fill matrix + // fill 2D vector for (auto& row: rand_mat) { for (auto& elem: row) { elem = norm(gen); } } + // fill eigen matrix + for (auto& row: mat.rowwise()) + { + for (auto& elem : row) + { elem = norm(gen); } + } + + // Using Eigen matrix pyp.raw(R"pyp( sns.set_theme(style="dark") f, ax = plt.subplots(figsize=(6,6)) + sns.scatterplot(x=mat[:,0], y=mat[:,1], s=5, color="0.15") + sns.histplot(x=mat[:,0], y=mat[:,1], bins=50, pthresh=0.1, cmap="mako") + sns.kdeplot(x=mat[:,0], y=mat[:,1], levels=5, color="w", linewidths=1) + plt.grid(True) + plt.xlabel("X", fontsize=12) + plt.ylabel("Y", fontsize=12) + plt.title("Numpy type slicing on C++ Eigen matrix", fontsize=14) + plt.show(block=False) + )pyp"); + + pyp.data_args(_p(mat)); + + // Using 2D std-vector + pyp.raw(R"pyp( + f, ax = plt.subplots(figsize=(6,6)) sns.scatterplot(x=rand_mat[:,0], y=rand_mat[:,1], s=5, color="0.15") sns.histplot(x=rand_mat[:,0], y=rand_mat[:,1], bins=50, pthresh=0.1, cmap="mako") sns.kdeplot(x=rand_mat[:,0], y=rand_mat[:,1], levels=5, color="w", linewidths=1) + plt.grid(True) + plt.xlabel("X", fontsize=12) + plt.ylabel("Y", fontsize=12) + plt.title("Numpy type slicing on C++ 2D vector", fontsize=14) plt.show() )pyp"); diff --git a/include/cppyplot_server.py b/include/cppyplot_server.py index 0fa185f..9a3b0e5 100644 --- a/include/cppyplot_server.py +++ b/include/cppyplot_server.py @@ -26,6 +26,10 @@ # import seaborn as sns # lib_sym['sns'] = sns +## Import pandas and register pandas object in the symbol table +# import pandas as pd +# lib_sym['pd'] = pd + # Import bokeh and register bokeh objects in the symbol table # from bokeh.layouts import column, row # lib_sym['column'] = column @@ -69,11 +73,16 @@ def parse_shape(shape_str:str)->tuple: data_payload = socket.recv() data_payload = np.array((struct.unpack("="+(data_type*data_len), data_payload))) plot_data[data_info[1]] = np.reshape(data_payload, data_shape) - elif(zmq_message[0:8] == b"finalize"): sym_table = {**lib_sym, **plot_data} aeval = Interpreter(usersyms=sym_table) aeval.eval(plot_cmd) + + # Some error happened pause execution by creating sample matplotlib windows + if aeval.error_msg != None: + plt.figure(figsize=(6,5)) + plt.title("Exception from ASTEVAL, check stdout", fontsize=14) + plt.show() plot_cmd = None plot_data = {} elif(zmq_message == b"exit"):