Re: Some questions on dealing with multi-page tiffs

8 views
Skip to first unread message

Alexandre Santos

unread,
Sep 30, 2009, 11:46:22 AM9/30/09
to prii...@googlegroups.com
So to recap, for the benefit of the list:

I'm working with 16-bit TIFF grey stack files, produced with a
microscope. I image two channels, but build one stack per channel. The
stack corresponds to a time series, during which the imaged tissue is
subjected to a set of conditions presented in a random order.

For instance, for an hypothetical stack of ten frames, the conditions'
sequence could be represented by the randomArray = [5, 4, 1, 7, 9, 2,
8, 3, 6, 0].

I would like to open the randomStack, seek frame 0 (which is in the
10th position), copy it to the new file orderedStack, and repeat with
frame 1 at 3rd position, etc.

Ideally, I would use something similar to this:
for i len(randomArray):
randomStack.seek(randomArray.index(i))
orderedStack.append(randomStack.copy)

Except that those methods do not exist, so I'll need to turn the 2D
sections into arrays.

> However, the saveTiffMultipageFromSeq does not require to the "input"
> array to be 3d ndarray, but instead it just iterates over the 2d
> section one after the other.
> That should be the function you want.

Ok, that settles it. I was not sure if it would produce a single stack
or a set of tiff files.

> This way you also don't need ever have the whole array in memory.
How would that work? You load chunks of the array into memory? Your
load functions seem to load the entire file into memory. Among your
load functions (load(), loadImg(), loadImg_iterSec(), loadImg_seq()),
it seems loadImg_iterSec() is the one to use in my application.

> You could just learn form that functions code how to make use of my
> PIL patch and then work completely without converting to a numpy
> array.
That's the aim.

> How big are your data files anyway !?
Each stack is about 100MB, which is peanuts for my workstation, but
hopefully I would like to keep analysis viable in my netbook, if
possible (one of the things that encourages me to look at python
instead of Matlab is that I don't have a personal licence to run on my
private laptop). But maybe it does not make any sense, and it's just
faster and easier to load the whole thing at once.

Best,
Alex

2009/9/30 Sebastian Haase <seb....@gmail.com>:
>> - You also have saveTiffMultipageFromSeq, which is probably not
>> required for my purposes (I did not really get what it does, neither
>> the concept behind Seq).
>
> by "Seq" I mean sequence, i.e. a python list or tuple or ndarray.
> Generally I load images or image stacks or image sequences into a 3D ndarray.
> I am talking single-channel images here.
> Are you working with RGB or grey-scale (single channel) ?
>  ((( I just re-read your email - you are using 16-bit grey-scale,
> right ?  that should work , i.e. be preserved when saving again....)))
>
>
> However, the saveTiffMultipageFromSeq does not require to the "input"
> array to be 3d ndarray, but instead it just iterates over the 2d
> section one after the other.
> That should be the function you want.
> This way you also don't need ever have the whole array in memory.
> You could just learn form that functions code how to make use of my
> PIL patch and then work completely without converting to a numpy
> array.
> How big are your data files anyway !?
>
> Cheers,
> Sebastian
>
> PS: why did you not post the Priithon list ?
>
>
>
>
>
>
> On Wed, Sep 30, 2009 at 3:57 PM, Alexandre Santos <ochi...@gmail.com> wrote:
>> Hi Sebastian,
>>
>> I was reading parts of Priithon and the handbook to try to understand
>> how your functions work and how I can achieve what I want.
>>
>> Here is what I want to do: Let's say I have a 10 frame multitiff
>> stack, in which frames have been randomized, such that the order is
>> described by the randomArray = [5, 4, 1, 7, 9, 2, 8, 3, 6, 0].
>>
>> I would like to open the randomStack, seek frame 0 (which is in the
>> 10th position), copy it to the new file orderedStack, and repeat with
>> frame 1 at 3rd position, etc.
>>
>> Ideally, I would use something similar to this:
>> for i len(randomArray):
>>  randomStack.seek(randomArray.index(i))
>>  orderedStack.append(randomStack.copy)
>>
>> However I see that there is no append frame method for tiffs (I don't
>> even see how to create a new frame to a tiff file), and it seems that
>> you turn the frames into arrays before converting them into an image
>> using the Image.fromstring method in array2image.
>>
>> So I guess that what I need to do is to turn each frame into a string,
>> concatenate it to the stack string, and at the end save the string
>> into an image.
>>
>> My questions are the following:
>> - what should I use to turn the current frame into a string? The
>> handbook mentions U.loadImg(filename). Checking useful.py,I see
>> loadImg_seq(), but this is for a set of tiff files. what should I use
>> for a tiff stack, will loadImg() or loadImg_iterSec work? Is it
>> possible to only load a frame, or do you have to load the full stack?
>>
>> - in the same vein, I guess it's also impossible to append frames into
>> an existing stack file without having in in memory as an array?
>>
>> - I suppose that to save the ordered array saveImg() is fine, since it
>> "also supports multipage TIFF". I guess the format "I;16" is also
>> preserved.
>>
>> - You also have saveTiffMultipageFromSeq, which is probably not
>> required for my purposes (I did not really get what it does, neither
>> the concept behind Seq).
>>
>> - since probably the full stack array must be in memory, do you know
>> how I could reorder it without creating a second array, duplicating
>> memory needs? I'm browsing through the python documentation, and I
>> guess I should go with the dictionary data structure and somehow play
>> with the keys? would that fit with your functions?
>>
>> This is it for the moment, sorry for the verbose mail, but I'm still
>> struggling to learn python, not to mention usage of the PIL, Numpy,
>> etc libraries.
>>
>> Best,
>>
>> Alexandre Santos
>>
>

Sebastian Haase

unread,
Sep 30, 2009, 11:59:41 AM9/30/09
to prii...@googlegroups.com
Alexandre,

Stay with us, and you won't need Matlab anymore ;-)


> Ideally, I would use something similar to this:
> for i len(randomArray):
> randomStack.seek(randomArray.index(i))
> orderedStack.append(randomStack.copy)
>
> Except that those methods do not exist, so I'll need to turn the 2D
> sections into arrays.
>

If you really insist to do the analysis in matlab and just use Python
to resave the data, you could get by by using
something like:
im = Image.open()
loop:
im.seek()
<my way of appanding - as you learn from the
saveMultipageTiff_seq function >

no need to convert to array.

100MB data files should be OK to work even with a netbook --
Python+numpy is much more memory conservative than Matlab. But it
depends of course how much analysis you really want to do - especially
if you need to do many 3D operations. But your problem sounds like
you could work with one section at a time.

-Sebastian

PS: thanks for the recap and background info.

Reply all
Reply to author
Forward
0 new messages