When running this code, the kernel quits after a few countries. But as I run it again, it makes progress, showing that there is some caching going on. After about 20 runs, I can get the full result.
Countries = #[[2]] & /@
EntityList[EntityClass["Country", "Countries"]];
test[country_] := Module[{polygons, meshes, result},
Print[country];
polygons = Entity["Country", country]["Polygon"][[1, 1]] ;
meshes =
BoundaryMeshRegion[#, Line[Join[Range[Length[#]], {1}]] ] & /@
polygons;
result =
TriangulateMesh[RegionUnion @@ meshes, MaxCellMeasure -> 5];
];
test /@ Countries
Any idea on how I could easily encapsulates this to guarantee the result in a single session (so I can execute this from a batch file as needed…)?
Thanks
Hi Luc,
Not sure which version / OS you are running on. I can reproduce the kernel crash on 12.1.1 for Mac OS X x86 (64-bit) (June 19, 2020). Looks like a bug. You should report it to WRI.
I see the same odd behaviour as you all have seen but was able to get all the country mesh data by inserting it into an Association, as follows:
Countries = #[[2]] & /@
EntityList[EntityClass["Country", "Countries"]] ;
test[country_] := Module[{polygons, meshes},
polygons = Entity["Country", country]["Polygon"][[1, 1]];
meshes =
BoundaryMeshRegion[#, Line[Join[Range[Length[#]], {1}]]] & /@
polygons;
TriangulateMesh[RegionUnion @@ meshes, MaxCellMeasure -> 5]] ;
reg = Association[Map[(# -> test[#] ) &, Countries]]; (* runs in 34 seconds on my iMac running MMA v12.1.1 *)
Put[reg, "countrydata.m"] (* saves all meshes in a file, about 168
MB *)
{reg["France"], reg["Canada"],
reg["Iceland"]} (* read a few meshes out of memory *)
This sometimes works and sometimes kills the Kernel so the underlying trouble is still here.
To tidy up the previous post - if writing all the country meshes to a file has worked then at your convenience you can read it back into memory and access the elements of the Association rather quickly, possibly quickly enough for “production” work. Note that the time for a single country mesh from the previously defined “test” function (with a decent internet connection) is:
Timing[test["Russia"]] (* approx .5 seconds on my iMac *)
Now exit and launch a fresh session.
reg = Get["countrydata`"]; (* if the file countrydata.m was created, as above, load the Association reg into memory *)
Normal[reg][[All, 1]] (* check that all 249 countries are there *)
Timing[reg["Russia"]] (* approx 10 microseconds on my iMac *)
So you get a nice speed gain, if you have enough memory:
MemoryInUse[]
325510952
Mac OSX 10.15.5
Mathematica “12.1.0 for Mac OS X x86 (64-bit) (March 18, 2020)”
Christopher,
I saw the same behavior. I don’t believe it is a benefit of the Association. I suspect it is a benefit of the caching done by one of the computational geometry call.
There is a file cache somewhere in Application data for Mathematica, and because there is some randomness in the crash, as I run it again and again, it succeeds where it has previously failed, and we perceive it as a big performance win.
Makes sense?
Still the crash is very problematic. I suspect Wolfram created this whole work around because they use an external library, and they can’t fix it, so they built this workaround. Maybe there is a way to do this in a parallel call which guarantees completion… ?