Page 1 of 1

How to make deep copy of a bio-formats reader in Matlab

PostPosted: Wed Aug 15, 2018 2:46 pm
by Ajaxel
Hello,
I have the following example, where I would like to make a copy of a bio-format reader. Unfortunately, Matlab copies only the handle and does not allow me to make a deep copy of the reader.
Is there a way to duplicate the reader somehow, so that the code works?

Code: Select all
reader1 = bfGetReader('myfile.ext');     % get a reader
reader2 = reader1;             % duplicate the reader;
                                         % the 'copy' function does not work on
                                         % loci.formats.ChannelSeparator class
plane1 = bfGetPlane(reader1, 1);         % get a plane
plane2 = bfGetPlane(reader2, 1);        % get a plane
reader1.close();                                 % close the first reader
plane2 = bfGetPlane(reader2, 1);        % does not work


Thanks!

Re: How to make deep copy of a bio-formats reader in Matlab

PostPosted: Thu Aug 16, 2018 8:25 am
by sbesson
Hi,

At the moment, there is no easy way to deep copy a reader as you said but you should be able to create and initialize multiple readers against the same file. It might useful to know what you would like to achieve with the copy of the readers.

In the case of creating multiple readers, if the initialization time is the major issue, you should be able to use the memoization API to store the state of an initialized reader to disk and reuse this cache file for initializing new readers. See https://docs.openmicroscopy.org/bio-for ... erformance for an example of doing so.

Best,
Sebastien

Re: How to make deep copy of a bio-formats reader in Matlab

PostPosted: Thu Aug 16, 2018 9:24 am
by Ajaxel
Hi Sebastien,
Thank you for suggestion!
I was just hoping that there is an easy way to duplicate the reader but otherwise the following codes seems to work

WITH MEMORIZER
Code: Select all
pathToFile = 'd:\myfile.tmp';
seriesNumber = 1;
% Construct a Bio-Formats reader decorated with the Memoizer wrapper
reader1 = loci.formats.Memoizer(bfGetReader(), 0);
% Initialize the reader with an input file to cache the reader
reader1.setId(pathToFile);
reader1.setSeries(seriesNumber-1);
% get a plane
plane1 = bfGetPlane(reader1, 1);       
% Initialize a new reader
reader2 = javaObject('loci.formats.Memoizer', bfGetReader(), 0);
% Initialization should use the memo file
reader2.setId(pathToFile);
reader2.setSeries(seriesNumber-1);
% Close reader 1
reader1.close();
plane2 = bfGetPlane(reader2, 1);         % get a plane
% Close reader 2
reader2.close();


WITHOUT MEMORIZER
Code: Select all
pathToFile = 'd:\myfile.tmp';
seriesNumber = 1;
% Construct a Bio-Formats reader
reader1 = bfGetReader(pathToFile);
reader1.setSeries(seriesNumber-1);
% get a plane
plane1 = bfGetPlane(reader1, 1);       
% Initialize a new reader
reader2 = bfGetReader(pathToFile);
reader2.setSeries(seriesNumber-1);
% Close reader 1
reader1.close();
plane2 = bfGetPlane(reader2, 1);         % get a plane
% Close reader 2
reader2.close();


Thanks!