From 82dc2b4fc83860daf87a72f029f67cf4a46fbd6f Mon Sep 17 00:00:00 2001 From: Rob Rau Date: Thu, 20 Aug 2020 12:13:10 -0400 Subject: [PATCH 1/5] Arrays are now converted to numpy arrays before passing to matplotlib functions as some of them expect numpy arrays, not python lists --- source/matplotlibd/core/translate.d | 7 +++++++ source/matplotlibd/pyplot.d | 8 ++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/source/matplotlibd/core/translate.d b/source/matplotlibd/core/translate.d index 1819117..04fdab6 100644 --- a/source/matplotlibd/core/translate.d +++ b/source/matplotlibd/core/translate.d @@ -1,5 +1,7 @@ module matplotlibd.core.translate; +import std.traits : isArray; + alias immutable bool PyBool; alias immutable (void*) PyNone; @@ -19,6 +21,9 @@ string d2py(T)(T v) { else static if (is(typeof(v) : string)) return format("\"%s\"", v); + else static if(isArray!T) + return format("np.array(%s)", v); + else return format("%s", v); } @@ -33,6 +38,8 @@ unittest { assert(d2py(false) == "False"); assert(d2py("Hello!") == "\"Hello!\""); assert(d2py(5.iota) == "[0, 1, 2, 3, 4]"); + import std.array : array; + assert(d2py(5.iota.array) == "np.array([0, 1, 2, 3, 4])"); } diff --git a/source/matplotlibd/pyplot.d b/source/matplotlibd/pyplot.d index 5d7949f..69d20bc 100644 --- a/source/matplotlibd/pyplot.d +++ b/source/matplotlibd/pyplot.d @@ -4,7 +4,7 @@ import matplotlibd.core.translate; private: -string py_script = "import matplotlib.pyplot as plt\n"; +string py_script = "import matplotlib.pyplot as plt\nimport numpy as np\n"; immutable string plt_funcs = (){ import std.string: splitLines; @@ -35,16 +35,16 @@ import matplotlibd.core.translate: False, True, None; void clear() { - py_script = "import matplotlib.pyplot as plt\n"; + py_script = "import matplotlib.pyplot as plt\nimport numpy as np\n"; } mixin(plt_funcs); unittest { import std.string; - auto script = py_script ~ "plt.plot([1, 2],[2, 4],\"r-\",lw=2)\n"; + auto script = py_script ~ "plt.plot(np.array([1, 2]),np.array([2, 4]),\"r-\",lw=2)\n"; plot([1, 2], [2, 4], "r-", ["lw": 2]); assert(script == py_script); clear(); - assert(py_script == "import matplotlib.pyplot as plt\n"); + assert(py_script == "import matplotlib.pyplot as plt\nimport numpy as np\n"); } From f6c690b008bb7b718614419fd566f189c2f8903f Mon Sep 17 00:00:00 2001 From: Rob Rau Date: Wed, 1 Sep 2021 15:58:03 -0400 Subject: [PATCH 2/5] Made it so we can do 3d plots --- source/matplotlibd/pyplot.d | 4 ++-- views/pyplot_functions.txt | 20 ++------------------ 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/source/matplotlibd/pyplot.d b/source/matplotlibd/pyplot.d index 69d20bc..78e8d09 100644 --- a/source/matplotlibd/pyplot.d +++ b/source/matplotlibd/pyplot.d @@ -4,7 +4,7 @@ import matplotlibd.core.translate; private: -string py_script = "import matplotlib.pyplot as plt\nimport numpy as np\n"; +string py_script = "import matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\nimport numpy as np\n"; immutable string plt_funcs = (){ import std.string: splitLines; @@ -35,7 +35,7 @@ import matplotlibd.core.translate: False, True, None; void clear() { - py_script = "import matplotlib.pyplot as plt\nimport numpy as np\n"; + py_script = "import matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\nimport numpy as np\n"; } mixin(plt_funcs); diff --git a/views/pyplot_functions.txt b/views/pyplot_functions.txt index 0aec43d..d2a3fd5 100644 --- a/views/pyplot_functions.txt +++ b/views/pyplot_functions.txt @@ -26,6 +26,7 @@ MultipleLocator Normalize NullFormatter NullLocator +Number PolarAxes Polygon Rectangle @@ -36,15 +37,6 @@ SubplotTool Text TickHelper Widget -_auto_draw_if_interactive -_autogen_docstring -_backend_selection -_imread -_imsave -_setp -_setup_pyplot_info_docstrings -_show -_string_to_bool acorr angle_spectrum annotate @@ -72,7 +64,6 @@ close cohere colorbar colormaps -colors connect contour contourf @@ -80,7 +71,6 @@ cool copper csd cycler -dedent delaxes disconnect draw @@ -119,7 +109,6 @@ hexbin hist hist2d hlines -hold hot hsv imread @@ -130,9 +119,6 @@ install_repl_displayhook interactive ioff ion -is_numlike -is_string_like -ishold isinteractive jet legend @@ -145,7 +131,7 @@ matshow minorticks_off minorticks_on new_figure_manager -over +nipy_spectral pause pcolor pcolormesh @@ -177,9 +163,7 @@ semilogy set_cmap setp show -silent_list specgram -spectral spring spy stackplot From cf1d21923824f8a4d4f3cfa7c9733b7d46a2ba6f Mon Sep 17 00:00:00 2001 From: Rob Rau Date: Wed, 1 Sep 2021 15:58:20 -0400 Subject: [PATCH 3/5] Removed pyplot_functions as it gets regenerated at build --- views/pyplot_functions.txt | 209 ------------------------------------- 1 file changed, 209 deletions(-) delete mode 100644 views/pyplot_functions.txt diff --git a/views/pyplot_functions.txt b/views/pyplot_functions.txt deleted file mode 100644 index d2a3fd5..0000000 --- a/views/pyplot_functions.txt +++ /dev/null @@ -1,209 +0,0 @@ -Annotation -Arrow -Artist -AutoLocator -Axes -Button -Circle -Figure -FigureCanvasBase -FixedFormatter -FixedLocator -FormatStrFormatter -Formatter -FuncFormatter -GridSpec -IndexLocator -Line2D -LinearLocator -Locator -LogFormatter -LogFormatterExponent -LogFormatterMathtext -LogLocator -MaxNLocator -MultipleLocator -Normalize -NullFormatter -NullLocator -Number -PolarAxes -Polygon -Rectangle -ScalarFormatter -Slider -Subplot -SubplotTool -Text -TickHelper -Widget -acorr -angle_spectrum -annotate -arrow -autoscale -autumn -axes -axhline -axhspan -axis -axvline -axvspan -bar -barbs -barh -bone -box -boxplot -broken_barh -cla -clabel -clf -clim -close -cohere -colorbar -colormaps -connect -contour -contourf -cool -copper -csd -cycler -delaxes -disconnect -draw -draw_all -draw_if_interactive -errorbar -eventplot -figaspect -figimage -figlegend -fignum_exists -figtext -figure -fill -fill_between -fill_betweenx -findobj -flag -gca -gcf -gci -get -get_backend -get_cmap -get_current_fig_manager -get_figlabels -get_fignums -get_plot_commands -get_scale_docs -get_scale_names -getp -ginput -gray -grid -hexbin -hist -hist2d -hlines -hot -hsv -imread -imsave -imshow -inferno -install_repl_displayhook -interactive -ioff -ion -isinteractive -jet -legend -locator_params -loglog -magma -magnitude_spectrum -margins -matshow -minorticks_off -minorticks_on -new_figure_manager -nipy_spectral -pause -pcolor -pcolormesh -phase_spectrum -pie -pink -plasma -plot -plot_date -plotfile -plotting -polar -prism -psd -pylab_setup -quiver -quiverkey -rc -rc_context -rcdefaults -register_cmap -rgrids -savefig -sca -scatter -sci -semilogx -semilogy -set_cmap -setp -show -specgram -spring -spy -stackplot -stem -step -streamplot -subplot -subplot2grid -subplot_tool -subplots -subplots_adjust -summer -suptitle -switch_backend -table -text -thetagrids -tick_params -ticklabel_format -tight_layout -title -tricontour -tricontourf -tripcolor -triplot -twinx -twiny -uninstall_repl_displayhook -violinplot -viridis -vlines -waitforbuttonpress -winter -xcorr -xkcd -xlabel -xlim -xscale -xticks -ylabel -ylim -yscale -yticks \ No newline at end of file From e270f5be1d5e1f79a524075f63cc14d50804d562 Mon Sep 17 00:00:00 2001 From: Rob Rau Date: Wed, 1 Sep 2021 16:02:32 -0400 Subject: [PATCH 4/5] Should create pyplot_functions.txt if it does not exist --- python/prebuild.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/prebuild.py b/python/prebuild.py index 4fda696..00db531 100644 --- a/python/prebuild.py +++ b/python/prebuild.py @@ -29,7 +29,7 @@ def gen_pyplot_functions(dub_root): functions = filter(lambda i: i[0] != '_' or i[0] in ascii_lowercase, extract_function_names(matplotlib.pyplot)) - with open(dub_root + "/views/pyplot_functions.txt", "w") as f: + with open(dub_root + "/views/pyplot_functions.txt", "w+") as f: f.write("\n".join(functions)) From b4797c9f9b1715151e03396c3effb890e3ce6b4c Mon Sep 17 00:00:00 2001 From: Rob Rau Date: Wed, 1 Sep 2021 16:05:03 -0400 Subject: [PATCH 5/5] Added a keepme so the views dir doesn't go away --- views/keepme | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 views/keepme diff --git a/views/keepme b/views/keepme new file mode 100644 index 0000000..e69de29