JerryI / wolfram-js-frontend

Dynamic Notebook Environment for Wolfram Language written in Javascript
https://jerryi.github.io/wljs-docs/
GNU General Public License v3.0
300 stars 13 forks source link

DateListPlot issue #193

Closed JerryI closed 4 months ago

JerryI commented 4 months ago

Thank you, that's great! As an existing user of WLJS on macOS, if only more graphics output could be displayed inside WLJS that would be even greater! For example, right now the following plot does not get displayed, the WLJS notebook hangs indefinitely ...

plot = DateListPlot[{Transpose[{timestamp, glucose}],

Transpose[{timestamp, smoothed1}],

Transpose[{timestamp, smoothed2}]},

FrameLabel -> {None, "blood glucose [mg/dL]"},

PlotLabel -> "fasting blood glucose",

PlotLegends -> {"daily", "smoothed (short)", "smoothed (long)"},

FrameTicks -> {{All, All}, {All, All}}, PlotRange -> {80, 120},

PlotTheme -> "Detailed", ImageSize -> Full]

jvo203 commented 4 months ago

Hi and thank you. Attached is a sample of older data. The timestamp and daily glucose values are read from the CSV file. Then the short/long-smoothed values are calculated from the daily data (exponential moving averages). Anyway, apart from the timestamp the short/long smoothed blood glucose data in this example can just be made-up.

As you can see the timestamps are not proper DateTime objects, I think they are just strings. Mathematica seems to be doing proper internal conversions to dates from strings.

glucose.csv

jvo203 commented 4 months ago

After logging to the Mac Studio with Mathematica, here is the data loading part too:

data = Import[$HomeDirectory <> "/glucose.csv", "CSV", HeaderLines -> 1];
timestamp = data[[;; , 1]];
glucose = data[[;; , 2]];
smoothed1 = ExponentialMovingAverage[glucose, 0.05];
smoothed2 = ExponentialMovingAverage[glucose, 0.005];

And the plot:

plot = DateListPlot[{Transpose[{timestamp, glucose}], 
   Transpose[{timestamp, smoothed1}], 
   Transpose[{timestamp, smoothed2}]}, 
  FrameLabel -> {None, "blood glucose [mg/dL]"}, 
  PlotLabel -> "fasting blood glucose", 
  PlotLegends -> {"daily", "smoothed (short)", "smoothed (long)"}, 
  FrameTicks -> {{All, All}, {All, All}}, PlotRange -> {80, 120}, 
  PlotTheme -> "Detailed", ImageSize -> Full]
JerryI commented 4 months ago
Screenshot 2024-06-15 at 07 31 37

It turned out that Full symbol was not implemented on the frontend ;D

Screenshot 2024-06-15 at 07 32 22

When I removed an option ImageSize->Full it plotted almost correctly. Only somewhat odd DisplayFunction appeared

jvo203 commented 4 months ago

Thank you.

JerryI commented 4 months ago

I will try to fix legends, probably it somehow connected with them

JerryI commented 4 months ago

fixed with this workaround

Unprotect[Show]
Show[any_, DisplayFunction->Identity] := any 
Protect[Show]

I have uploaded this patch to wljs-editor/src/BoxesWorkarounds.wl. It will update it automatically (it checks updates every 3 days, but you can force it to check from settings menu and then - restart an app).

jvo203 commented 4 months ago

Thanks again, I would never have guessed the workaround myself. My version of WLJS is installed from Homebrew so perhaps it will be updated when a new Homebrew-based version comes out.

jvo203 commented 4 months ago

I see there is a new release 2.4.2 in Homebrew. After updating WLJS tried to run it but unfortunately, ever since the last release 2.3.7 there has always been an error connecting to the kernel (Wolfram Engine / Mathematica Kernel). I.e. trying to execute "2+2" first prompts "Select Kernel", and then after selecting the only option available: "Auto", the next message is "Alert: Kernel container is not ready!". Clicking on "Refresh window" does not help. It's been so ever since the previous Homebrew version... WLJS used to work fine before.

jvo203 commented 4 months ago

Manually trying to connect to a kernel brings up a pop-up that says "Alert No kernels are available". This in spite of having both Wolfram Mathematica 14.0.0 and Wolfram Engine installed on this M1 Ultra Apple Mac Studio.

JerryI commented 4 months ago

I moved it to another issue

jvo203 commented 4 months ago

Hi, don't know if this issue should be re-opened but, despite the WLJS being on the new version 2.4.2, and despite having dealt with the pesky Wolfram licensing issue (deleted Mathematica, made a clean install of the free Wolfram Engine + Script), the plot issue still persists (the notebook is hanging on "Loading"). A screenshot has been attached. Don't know, perhaps the Homebrew version 2.4.2 of WLJS does not include your fix yet.

brew info wljs-notebook
==> wljs-notebook: 2.4.2 (auto_updates)
https://github.com/JerryI/wolfram-js-frontend
Installed
/opt/homebrew/Caskroom/wljs-notebook/2.4.2 (127B)
From: https://github.com/Homebrew/homebrew-cask/blob/HEAD/Casks/w/wljs-notebook.rb
==> Name
WLJS Notebook
==> Description
Javascript frontend for Wolfram Engine
==> Artifacts
WLJS Notebook.app (App)
==> Analytics
install: 10 (30 days), 44 (90 days), 44 (365 days)
スクリーンショット 2024-06-25 12 07 18
jvo203 commented 4 months ago

A full debug log is attached. Seemingly there is nothing suspicious at all. System.log

JerryI commented 4 months ago

Probably you have tried Force reload on window…

wljs-graphics is a separate package and a new version will be fetched anyway (hope i did not break anything).

Thank you for logs! As a last thing: could you, please, also send a screenshot of browsers log? Just right click on a window and then inspect. On the console tab you should see some red text. It would be helpful to have a look at them too.

jvo203 commented 4 months ago

Hmm, I don't even know how to do "Force reload" on a wljs window. The only thing I can think of is always maximizing the wljs notebook upon a start-up as the default size is too small. Anyway, here is an inspect screenshot:

スクリーンショット 2024-06-25 15 45 05
jvo203 commented 4 months ago

symbol Full is not defined ... Removing "ImageSize -> Full" from the WLJS notebook fixed the problem. "ImageSize->Large" does not work either. It seems like WLJS does not like the "ImageSize" option being passed. The whole "ImageSize" option needs to be removed, but then the resulting image is rather small and the dates on the X axis are not legible:

スクリーンショット 2024-06-25 15 51 43
jvo203 commented 4 months ago

By the way, export to PDF does not work at all:

Export[$HomeDirectory <> "/glucose.pdf", plot, "PDF"]

results in a completely corrupted PDF file: glucose.pdf

jvo203 commented 4 months ago

Sorry perhaps the original issue could be re-phrased as "add support for the ImageSize plot option". The original fix (removing the ImageSize option completely from the plot command) results in too-small images.

JerryI commented 4 months ago

Sorry perhaps the original issue could be re-phrased as "add support for the ImageSize plot option". The original fix (removing the ImageSize option completely from the plot command) results in too-small images.

Fixed in a new release of wljs-graphics-d3! Please, force check updates from the settings menu to get new version on the next start.

JerryI commented 4 months ago

By the way, export to PDF does not work at all:

Export[$HomeDirectory <> "/glucose.pdf", plot, "PDF"]

results in a completely corrupted PDF file: glucose.pdf

Unlike Mathematica, all graphics is rendered on frontend (in browser). We could not support 100% accurate all internals of Mathematica, and to make the whole system compatible with some of Mathematica's features we had to rewire and sometimes break (not intentionally) dynamic boxes.

In this regard it is better to export any graphics using a dropdown menu located in the top-right corner of your plot.

Screenshot 2024-06-25 at 13 44 09

It exports it as svg

JerryI commented 4 months ago

the same counts for 3D graphics. It also has its own dropdown menu

jvo203 commented 4 months ago

Thank you. Despite forcing the update and likely getting it, perhaps the wljs-graphics-d3 did not get updated. Anyway, using an ImageSize option results in an empty plot with a legend:

スクリーンショット 2024-06-25 20 09 19

On the other hand, exporting a plot to SVG results in a plot but without a legend: glucose Either way the X axis could be improved as at present there is too much of an overlap between date labels (as seen in the attached SVG file). Keep up the good work!

JerryI commented 4 months ago
Screenshot 2024-06-25 at 16 18 53

I limited the maximum initial ticks to 4 in any date plots

JerryI commented 4 months ago

Unfortunately, legend is a common issue already, since Mathematica or WolframLanguage use them as sort of wrapper box for any arbitrary expression. Therefore Graphics has no idea about legends.

There is a few workarounds

  1. Take a screenshot (kinda ugly)
  2. Build a custom legend function aka legend[...] := {Rectangle[], ...} and place it directly or using Inset to keep independent scaling.
  3. Use Plotly function instead. It is basically plotly.js with the same API and only difference that all JSON-like objects were replaced with Association
  4. Export entire notebook instead (share button in the top bar). All input cells can be hidden using arrow-like button on the left side from the active cell group.
jvo203 commented 4 months ago

It's looking good now, many thanks!

スクリーンショット 2024-06-26 9 53 53

Regarding saving images, yeah might as well switch to Plotly.js and use its "Save as PNG" functionality. I think the issue can be closed.