1

I'm doing a lot of simulation runs of graphs in order to compare the simulated data with the data of a real world network (links among websites), according to my calculations I have to run close to a 1000 simulations in order to get enough significance.

The simulations code is something on the line of:

<<IGraphM`
simrun1=Table[IGBarabasiAlbertGame[1032, 2], 1000];
simrun2=TableIGStaticPowerLawGame [1032,144, 2.889], 1000];

But it can be closelly approximated by:

simrun=Table[RandomGraph[BarabasiAlbertGraphDistribution[1032, 2]], 1000]

If I suppress the output using ; in the end of the lines. The problem is that I need this data to continue to be stored in the notebook after I close the nb file, so using suppressed output isn't working well for me. I really need a way for the output to persist after I close the notebook.

The only way I could find to do it, was to evaluate the code without the suppressed output and to manually transform the output into an input cell, but the only way I found to do it was to take out the ; at the end of the lines.

But this creates another problem, since it will render 2000 graphs. Using AbsoluteTimming each line spends about 0,2s on total computation time, but the the rendering and formatting of the notebook afterwards ranges from 5 to 30 minutes. It is an awful a lot of time, and in the end I have little or no use to this visualization.

I just need the graph objects in order to map functions that take measures on them, like local, global and mean clustering coefficient, mean distance, vertex degree, number of edges, centrality measures for each node, and so on. So is there a way to generate these graphs and store on a way that would persist after closing the notebook without having to render the output without wasting so much time with rendering?

Thanks in advance for your answers.

nicholas80
  • 169
  • 8
  • 5
    Don't display them. It doesn't sound like there's a need to. – Michael E2 Apr 27 '17 at 01:25
  • @Michael E2, I really don't need to, and originally did the simulations with supressed output (using ;). but I couldn't analyze everything yesterday, and even though I deactivated the evaluatable property of the cells, today the only way to get the values of the variables was evaluating everything again, since the simulated data is probabilistic in nature, the only way I've found to not loose data was to execute de simulation runs without suppressed output and them transform the cells into input cells and attribute them to a variable. Since I don't know another way to keep the output intact. – nicholas80 Apr 27 '17 at 01:55
  • I'm actualy quite new to programming and to Mathematica, just started my undergrad in physics last year, and most of what I'm learning about Mathematica comes from Wellin's book. – nicholas80 Apr 27 '17 at 01:58
  • 2
    Maybe DumpSave[] and Get[] would help. – Michael E2 Apr 27 '17 at 02:09
  • 2
    could you give small version of simulation you are doing? In general, graph analysis doesn't need to see output. – halmir Apr 27 '17 at 13:22
  • @halmir, it is nothing too fancy, the network I have the data seems to follow a power-law degree distribution, so I estimated parameters for a IGBarabasiAlbertGame and IGStaticPowerLawGame from the IGraphM package, but it isn't much different than running something like: Table[RandomGraph[BarabasiAlbertGraphDistribution[1032, 2]], 1000].The problem is to have the output on a format that I can retrieve later without evaluating the command and losing data from the simulations. – nicholas80 Apr 27 '17 at 19:35
  • 1
    so you want to store Table[RandomGraph[BarabasiAlbertGraphDistribution[1032, 2]], 1000]? – halmir Apr 28 '17 at 00:39
  • 1
    and GraphPropertyDistribution might be helpful .. for example, dist = GraphPropertyDistribution[GlobalClusteringCoefficient[g], g [Distributed] BarabasiAlbertGraphDistribution[1032, 2]]; RandomVariate[dist, 1000] or Mean[dist] – halmir Apr 28 '17 at 00:45
  • 1
    Using gencode from here, you could try things like this: With[{mygraph = Compress@RandomGraph[{100, 300}]}, gencode[myvar = Uncompress[mygraph], GeneratedCell -> False]]. – Michael E2 Apr 30 '17 at 16:12
  • @halmir, thanks for your idea. You suggestion worked extremely well for the built in functions, even did a test run with a million iterations and it took half the time than my original idea. Now I just have to automatically transform the output into input and automate it all. Unfortunately it doesn't seem to work with iGraphM package functions, since Mathematica doesn't recognize the package functions as graph distributions. At least in the case of Barabasi-Albert game, the BarabasiAlbertDistribution function works very well, but it seems there's nothing on the line of IGStaticPowerLawGame. – nicholas80 May 02 '17 at 00:43
  • @MichaelE2, thanks for your reply. I'll look more closely into it later today and see how it works. – nicholas80 May 02 '17 at 00:48
  • The question was flagged to be on hold, I've edited it in order to be according to guidelines on the help page, but how can I notice the moderators to review it and see if everything is in order now? – nicholas80 May 02 '17 at 00:50
  • If I understand you correctly, you want to keep graph that generated randomly to analyze later right? Then, you could use SeedRandom[1234];simrun = Table[RandomGraph[BarabasiAlbertGraphDistribution[1032, 2]], 1000]; and your random graph will be the same. – halmir May 02 '17 at 01:23
  • 1
    SeedRandom does not affect IGraph/M functions, but you can use IGSeedRandom instead. – Szabolcs May 02 '17 at 07:10

1 Answers1

1

So is there a way to generate these graphs and store on a way that would persist after closing the notebook without having to render the output without wasting so much time with rendering?

This question isn't really about graphs. It could be about any other kind of data. The answer is that if you are going to need the data in a later session, then you should export it to a file.

myGraphs = Table[..., {1000}];

Export["graphs.mx", myGraphs]

This will write the result into graphs.mx, and you can later re-import it using

myGraphs = Import["graphs.mx"];

The MX format has limited compatibility. It cannot be read in earlier versions or on different platforms (32 vs 64 bit).

When compatibility is a concern, I usually Compress the data and export it as "String": https://mathematica.stackexchange.com/a/1960/12


Generally, I recommend exporting to a separate file. If you are in a hurry or for some reason it is really much better to store everything in the notebook, you can use the method I blogged about here:

(Earlier version also shown here.)

I do not recommend this for data larger than a few megabytes.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • Ow! @Szabolcs, thanks a lot for your reply. This is the first time I've seen this explained on a way that makes sense to someone new to coding. I've already thought that I had to save a lot of the stuff I do (functions, simulations, etc.) on other files, I even tried to make sense of the documentation on Files, Streams, and External Operations, but it ended up more lost than before reading it. You finally put it on a way that is easy to understand. – nicholas80 May 04 '17 at 04:19
  • following on this, to add stuff to the .mx file I just need to use PutAppend, right? If I create an .mx file with multiple variables for simulation runs or with multiple functions when I use Import["graphs.mx"], do I import the whole file, or do I need to Import each variable? – nicholas80 May 04 '17 at 04:20
  • For a "simple" graph, I wonder if just saving the adjacency matrix might be better? Additionally, since it seems that the user doesn't need to display the graphs, adding GraphLayout -> None might be useful. – J. M.'s missing motivation May 11 '17 at 13:23