Message Boards Message Boards

Iteratively write to file.


I have a program that fits a model to data. At the end of the program I export a file that contains list of the best parameters gathered by the Sow and Reap commands. What I would like to do, is have the program write out the parameters to a file while the program is actually running. This way if anything were to happen to the program while it was running, I would be able to start again using the written parameters. Instead of having to start all the way from scratch. I've tried using OpenWrite for this, but have failed miserably. Can someone help with this? I'll attach a snippet of code that contains the portion that Sows the parameters, if anything else is needed please let me know.

If[u[[i]] < v[[i]],(*If u<v always accept that parameter set, else accept with probability u<v*)
  ka[[i + 1]] = ka[[i]];
  kb[[i + 1]] = kb[[i]];
  Sow[{True, decayscore[[i]], v[[i]], ka[[i]], kb[[i]], i}],(* These quantities are written to ParamSets *)
  ka[[i]] = ka[[i - 1]];
  kb[[i]] = kb[[i - 1]];
3 years ago
Perhaps you can adapt this
 In[1]:= stream = OpenWrite["Log.txt"];
 Write[stream, "Logging"];  (*Make the file exist so we can append later*)
   For[i = 1, i <= 3, i++,
    stream = OpenAppend["Log.txt"];
    Write[stream, i];
  ][[2, 1]]

Out[4]= {1, 2, 3}

and the Log.txt file will appear in the default location, unless you want to specify the path to where you want to put that.

The OpenAppend and Write and Close should safely ensure the data is isolated from other calculations going on, until you repeat that.
POSTED BY: Bill Simpson
3 years ago
Thanks Bill!

This does work perfectly!
Thank You!
3 years ago

Group Abstract Group Abstract