I would like to convert data from csv files into several stacks.
I have a batch of input files, say a number of
Z csv files, one for each slice. Each file has 3 columns and
double_01, double_02, double_03 double_11, double_12, double_13 ... double_n1, double_n2, double_n3
My task is to produce a stack with given dimensions
W, H, Z from each column, according to the following pattern:The
W first rows of a column go into the first line (y=0) of the current slice of the stack, then the following W rows go into the 2nd line (y=1), and so on. Here’s a sketch for a single file/slice:
This is somehow like the “reshape” function of Matlab, but between a text file and an image. The output stacks are ~2002501000 vox. 32 bits, so memory is an issue.
Before going any further: I did it, my code works. I’m just wondering if it can run faster.
My algorithm goes like this:
- Create 3 empty stacks
- For each csv file (z)
- scan the file line by line
- convert the line into 3 pixel values
- put 1st value at (x,y,z) in the 1st stack
- put 2nd value at (x,y,z) in the 2nd stack
- put 3rd value at (x,y,z) in the 3rd stack
- Go to next line
- Go to the next file/slice
- Save the stacks
So, I’m putting pixel values one by one into each image. My question is: Is there a more efficient option?
For example, I noticed there exists a
putrow method. Does anyone know if it could run faster?
Or would it be more efficient to first scan the whole csv file, load it into an Array or a List, process it line by line, and write-append each slice into TIF files?
Thank you for your help!