2

I generated a very large graph, too large to simply display and manipulate with copy/paste operations. How can I somehow save the graph to a file to allow it to be rapidly reloaded after I quit out of the kernel and restart Mathematica?

  • 1
    Save or DumpSave might be of help. With DumpSave you will have the object saved in an mx file so the size will be smaller and will load faster. – Spawn1701D Apr 21 '13 at 00:44
  • @Spawn1701D So I could simply write Save["SaveFile.mx",G], where G is my graph? – ThermalDance Apr 21 '13 at 00:48
  • 1
    and Get["SaveFile.mx"] or <<SaveFile.mx to load it. Make sure the file is located inside the path of Mathematica and keep in mind that the file can be loaded only to computers with the same kind of OS, so if you make the mx on windows it won't load in Mac OS. – Spawn1701D Apr 21 '13 at 00:54
  • Instead of DumpSave, I recommend Export, which can also write MX files, but doesn't store variable names inside (only the data). Or just use Export["name.mz", Compress[graph], "String"]. See also here – Szabolcs Apr 21 '13 at 00:57
  • @Szabolcs Interesting... I did not know that about Export/Import and .mx files. I typically use DumpSave and Get with a dedicated internal variable and a wrapper function in my packages i.e. something like save[file_, var_] := Block[{$var}, DumpSave[file, $var = var];] and load[file_] := Block[{$var} = Get@file]. Oh well... I'll probably keep them the way they are, since it works. – rm -rf Apr 21 '13 at 01:21
  • Why not Export["graph.txt", Compress[g]] and then Import["graph.txt"] // Uncompress ? I did not see any significant difference in files size and export/import times with .mz . – Vitaliy Kaurov Apr 21 '13 at 01:28
  • @Vitaliy MX is far faster than anything for me, but the Compress approach came in as second. And Compress is both cross platform and cross version compatible, so that's what I use. Yves however said that exporting (?) with this Compress method took too much memory for him. If would really be good if Import/Export had a dedicated format for Mathematica expressions that's fast and small and reasonably compatible between versions. WDX is small but definitely not fast (takes ages to import for large data). – Szabolcs Apr 21 '13 at 01:44

1 Answers1

2

From the comments of Spawn1710D and Szabolcs:

Save or DumpSave might be of help. With DumpSave you will have the object saved in an mx file so the size will be smaller and will load faster.

You use

Save["SaveFile.mx",G]

, where G is your graph to save and

Get["SaveFile.mx"] 

or

 <<SaveFile.mx

to load it. Make sure the file is located inside the path of Mathematica and keep in mind that the file can be loaded only to computers with the same kind of OS, so if you make the mx on windows it won't load in Mac OS.

Instead of DumpSave, Export can be used. It can also write MX files, but doesn't store variable names inside (only the data). Or just use

Export["name.mz", Compress[graph], "String"].
Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323