GenericMappingTools / GMT.jl

Generic Mapping Tools Library Wrapper for Julia
Other
193 stars 28 forks source link

subplots and normal plots do not show correctly in jupyter-lab environment #1362

Closed melodyjulia closed 7 months ago

melodyjulia commented 7 months ago

When drawing subplots of maps under jupyter-lab environment, there is no map to show after calling subplot("show").

For example the following map script works as an executable script under shell environment, but not under jupyter-lab environment: https://www.generic-mapping-tools.org/GMTjl_doc/gallery/ex01/

However, drawing subplots of other types, such as lines and histograms under jupyter-lab environment, they work charmingly.

joa-quim commented 7 months ago

Sorry, I'm a bit confused. You are saying that the plots with subplot show only depending on their contents?

Can you try that notebook in VScode? Note that for it show the images embedded in the nootebook one must set ENV["DISPLAY_IN_VSC"] = "a"; in a cell before the first plot.

melodyjulia commented 7 months ago

Sorry for the confusion. There are more details than my first post. The errors are dependent on the system running the notebook. Please read the following.

If running the notebook under Linux for the first time, the following errors occur:

Error: /undefined in Standard+_Encoding
Operand stack:

Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1974   1   3   %oparray_pop   1973   1   3   %oparray_pop   1961   1   3   %oparray_pop   1817   1   3   %oparray_pop   --nostringval--   %errorexec_pop   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--
Dictionary stack:
   --dict:767/1123(ro)(G)--   --dict:0/20(G)--   --dict:75/200(L)--   --dict:157/250(L)--
Current allocation mode is local
Last OS error: No such file or directory
Current file position is 22417

Something went wrong when calling the module. GMT error number = 79

Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] gmt(::String)
   @ GMT ~/.julia/packages/GMT/vHy9m/src/gmt_main.jl:163
 [3] helper_showfig4modern(show::String)
   @ GMT ~/.julia/packages/GMT/vHy9m/src/common_options.jl:4047
 [4] subplot(fim::String; stop::Bool, kwargs::@Kwargs{})
   @ GMT ~/.julia/packages/GMT/vHy9m/src/subplot.jl:124
 [5] subplot(fim::String)
   @ GMT ~/.julia/packages/GMT/vHy9m/src/subplot.jl:39
 [6] top-level scope
   @ In[2]:17

If running the notebook under macOS for the first time, the following errors occur and can be reproduced in VScode:

psconvert [ERROR]: File /var/folders/9d/z8z735090tj8sl7x67kspfjc0000gn/T/GMTjl_user.ps was not found
psconvert [ERROR]: Cannot find file /var/folders/9d/z8z735090tj8sl7x67kspfjc0000gn/T/GMTjl_user.ps

Something went wrong when calling the module. GMT error number = 72

Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:35
  [2] gmt(::String)
    @ GMT ~/.julia/packages/GMT/vHy9m/src/gmt_main.jl:163
  [3] showfig(d::Dict{Symbol, Any}, fname_ps::String, fname_ext::String, opt_T::String, K::Bool, fname::String)
    @ GMT ~/.julia/packages/GMT/vHy9m/src/common_options.jl:3973
  [4] finish_PS_module(::Dict{Symbol, Any}, ::Vector{String}, ::String, ::Bool, ::Bool, ::Bool)
    @ GMT ~/.julia/packages/GMT/vHy9m/src/common_options.jl:4280
  [5] finish_PS_module(::Dict{Symbol, Any}, ::Vector{String}, ::String, ::Bool, ::Bool, ::Bool)
    @ GMT ~/.julia/packages/GMT/vHy9m/src/common_options.jl:4179 [inlined]
  [6] showfig(; kwargs::@Kwargs{})
    @ GMT ~/.julia/packages/GMT/vHy9m/src/common_options.jl:4033
  [7] showfig()
    @ GMT ~/.julia/packages/GMT/vHy9m/src/common_options.jl:4025
  [8] helper_showfig4modern(show::String)
    @ GMT ~/.julia/packages/GMT/vHy9m/src/common_options.jl:4050
  [9] subplot(fim::Symbol; stop::Bool, kwargs::@Kwargs{})
    @ GMT ~/.julia/packages/GMT/vHy9m/src/subplot.jl:124
 [10] subplot(fim::Symbol)
    @ GMT ~/.julia/packages/GMT/vHy9m/src/subplot.jl:39
 [11] top-level scope
    @ In[9]:14

If running the notebook cell again, the above errors disappear under both systems. Instead, the maps display in the jupyter environment under Linux system, but not under macOS system. Meanwhile, there is a file GMTplot.png in the same directory where saves the notebook under macOS system, the GMTplot.png file has correct content, but not shown in the jupyter environment.

In addition to the above, there is another problem. If the above notebook contains two cells plotting different figures (for example: maps and lines). After successfully running both cells, they displays a same figure.

joa-quim commented 7 months ago

OK, let us try to fix one at a time but first let me explain the displaying is done.

GMT has two modes: classic and modern. In the first case we write the PostScript files in the directory we want but in latter files are saved in a specific dir under ~/.gmt/sessions and GMT takes care of the temporary files and of moving the plot into a final destination. Translating this to GMT.jl I tried to abstract away the difference of both modes but still have to deal with the differences. So, codes that used with a begin & end or subplots (that set the begin/end under the hood) have the figures named GMTplot.png by default and saved on current directory. All other modules use the classic mode and save the figures under system's tmp (unless user use the savefig option). Now this creates differences on how notebooks have to find the images. I thought I had fixed all this but in fact some fixes needed to be made for VScode too. I made them but so far are only available in the master version. So, please try that to see if things in VScode work well (or just better) now.

This still doesn't explain the first errors you posted that seem to originate in current directory not being set fast enough to the same dir as the .ipynb file and search the file in the GMT.jl installation dir. This absolutely makes no sense to me since files are written in a dir returned by tempdir() and I cannot reproduce it on Windows.

Last case you reported, if happening when plotting with subplot and normal plot should be solved by my fixes too, ... but needs confirmation.

melodyjulia commented 7 months ago

Thanks! I've changed the title of this issue to reflect true problem as it is not specific to subplots of maps.

Meanwhile, I simplified my notebook to contain only two cells: the first cell plots subplots with three lines, and the second cell plots a normal single line. I tried the master branch of GMT.jl under Linux system and the errors of displaying wrong figure in Jupyter environment still exist.

[5752ebe1] GMT v1.10.1 `https://github.com/GenericMappingTools/GMT.jl.git#master`

The following steps can reproduce errors:

Step 1: Delete the files GMT_user.ps and GMT_user.png in /tmp directory, and start a totally fresh run of the first cell in the notebook. The following errors prompt:

psconvert [ERROR]: File /tmp/GMTjl_user.ps was not found
psconvert [ERROR]: Cannot find file /tmp/GMTjl_user.ps

Something went wrong when calling the module. GMT error number = 72

Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:35
  [2] gmt(::String)
    @ GMT ~/.julia/packages/GMT/MTtrD/src/gmt_main.jl:163
  [3] showfig(d::Dict{Symbol, Any}, fname_ps::String, fname_ext::String, opt_T::String, K::Bool, fname::String)
    @ GMT ~/.julia/packages/GMT/MTtrD/src/common_options.jl:3973
  [4] finish_PS_module(::Dict{Symbol, Any}, ::Vector{String}, ::String, ::Bool, ::Bool, ::Bool)
    @ GMT ~/.julia/packages/GMT/MTtrD/src/common_options.jl:4293
  [5] finish_PS_module(::Dict{Symbol, Any}, ::Vector{String}, ::String, ::Bool, ::Bool, ::Bool)
    @ GMT ~/.julia/packages/GMT/MTtrD/src/common_options.jl:4179 [inlined]
  [6] showfig(; kwargs::@Kwargs{})
    @ GMT ~/.julia/packages/GMT/MTtrD/src/common_options.jl:4033
  [7] showfig()
    @ GMT ~/.julia/packages/GMT/MTtrD/src/common_options.jl:4025
  [8] helper_showfig4modern(show::String)
    @ GMT ~/.julia/packages/GMT/MTtrD/src/common_options.jl:4050
  [9] subplot(fim::Symbol; stop::Bool, kwargs::@Kwargs{})
    @ GMT ~/.julia/packages/GMT/MTtrD/src/subplot.jl:125
 [10] subplot(fim::Symbol)
    @ GMT ~/.julia/packages/GMT/MTtrD/src/subplot.jl:39
 [11] top-level scope
    @ In[26]:14

Step 2: Kick off a second run of the first cell, a correct figure displayed in the Jupyter environment.

Step 3: Run the second cell, a correct figure displayed in the Jupyter environment.

Step 4: Run the first cell again, a wrong figure displayed and it is the figure produced by the second cell.

To get correct figure for the first cell again, I have to delete the files GMT_user.ps and GMT_user.png in /tmp directory and run the first cell twice. BTW, the second cell always displays a correct figure.

joa-quim commented 7 months ago

Please update the master version and try again. I hope I've fixed that.

melodyjulia commented 7 months ago

Thank you so much. It works like a charm now!

joa-quim commented 7 months ago

Good. It was a tricky one to find because I couldn't use the debugger from the notebook.