Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I use

f = OpenRead["largefile.m"]
x = Read[f];
Close[f];

to read in a Mathematica expression from a large file largefile.m (about 3 GB) and assign it to x. The Read[f] step takes very long time. How to monitor its progress? BTW, if the file largefile.m is larger than 4.1 GB (as ls -la displays), will this approach work?

share|improve this question
    
Out of curiosity: have you tried using Get? –  Mr.Wizard Apr 23 '12 at 6:29
    
Also, what does your expression look like and how was it generated? Could you write it in a format that is more easily read in parts rather than all at once? –  Mr.Wizard Apr 23 '12 at 6:35
    
I get Get:bigfile error using Get[]. –  Problemaniac Apr 23 '12 at 6:44
    
I could if that's the best approach. Right now it is {{...}, {...}} where ... is a long sequence of expressions. –  Problemaniac Apr 23 '12 at 6:45
    
Last year at the Wolfram Tech Conference, I attended a presentation about the various ways to deal with files. You might want to look at the presentation that can be found here –  Sjoerd C. de Vries Apr 23 '12 at 8:42
add comment

2 Answers 2

up vote 9 down vote accepted

I am fairly sure that for files of this size, you'll have problems loading them into Mathematica (unless you have truly huge amount of RAM on your machine, but perhaps even in this case). If your file contains newline-terminated strings, or if you otherwise know its structure (types stored there), consider using ReadList or BinaryReadList. I gave one example in my post here. In this way, you can attach something like ProgressIndicator to it, and monitor the progress. Also, I would recommend to use something similar to large data framework from this answer, to convert the contents of your file / parts of resulting expression, to a file-backed form, since operating on the entire dataset in-memory might not be possible and / or efficient for such huge amounts of data.

share|improve this answer
add comment

I've been using BinaryReadList to load reasonably large 100-500MB files and its been working well.

However, I've never had a way to watch the progress. Based @Leonid's answer I hacked together a ProgressIndicator for the function.

In my case my large data files consist of sets of 4 32bit (big endian) floats. Here I read the file in 100 steps. Timing is similiar to reading the file all at once.

ImportProgress[filename_] := Module[{str, data, n},
  (*open stream*)
  str = OpenRead[filename, BinaryFormat -> True];
  data = {}; n = 1;
  (*display the dynamic progress indicator*)
  Print[ProgressIndicator[Dynamic[n/100]]];
  (*read data*)
  While[n < 100,
   AppendTo[data, 
    BinaryReadList[str, {"Real32", "Real32", "Real32", "Real32"}, 
     Ceiling[FileByteCount[filename]/16/100], ByteOrdering -> +1]];
   n++];
  (*close stream*)
  Close[str];
  (*return the data*)
  data
]

(*read the data*)
data = ImportProgress["R06_14539-v01.pos"];  

Here is a more elegant alternative using Monitor where the ProgressIndicator disappears after the file finishes loading:

ImportProgressMonitor[filename_] := Monitor[
  Module[{str, data},
   (*open stream*)
   str = OpenRead[filename, BinaryFormat -> True];
   data = {};
   (*read data*)
   n = 1;
   While[n < 100,
    AppendTo[data, 
     BinaryReadList[str, {"Real32", "Real32", "Real32", "Real32"}, 
      Ceiling[FileByteCount[filename]/16/100], ByteOrdering -> +1]];
    n++];
   (*close stream*)
   Close[str];
   data
   ],
  (*monitor the progress*)
  ProgressIndicator[n/100]
]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.