Skip to content

Commit 0054c15

Browse files
Merge pull request #34 from MATLAB-Community-Toolboxes-at-INCF/dandi-hub-example
version 0.7
2 parents d9ce1dc + 7969651 commit 0054c15

File tree

4 files changed

+106
-23
lines changed

4 files changed

+106
-23
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ Try out training your own DeepInterpolation network. You can individually view (
3434

3535
<sub>(\*) This data-intensive example is recommended for use on a local machine, not for MATLAB online.</sub>
3636

37+
#### Custom Datastore for DeepInterpolation
38+
For large datasets that are too large to load entirely into memory, the custom datastore offers a solution. By initializing the datastore with a dataset's path, users can sequentially access both flanking frames and their respective center frames. This allows for easy training and inference.
39+
40+
For a detailed introduction and a practical workflow, see the customdatastore_example.
3741

3842
### Support
3943
DeepInterpolation with MATLAB is a public repository. Contributions can be made in the form of adding issues or submitting pull requests.

datastore/DeepInterpolationDataStore.m

Lines changed: 102 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
setFramesStartIndex double
3232
currentFrameIndex double
3333
allSetFrameStartIndices double
34+
datasetType string
35+
NwbDatasetPath string
3436
end
3537

3638
methods % begin methods section
@@ -39,6 +41,8 @@
3941
filePath
4042
options = struct();
4143
end
44+
assert(isfile(filePath));
45+
myds.fileName = filePath;
4246
if isfield(options, 'doAutoResize')
4347
myds.doAutoResize = logical(options.doAutoResize);
4448
else % default
@@ -63,23 +67,42 @@
6367
else % default
6468
myds.outputFrameSize = [512, 512];
6569
end
66-
assert(isfile(filePath));
67-
myds.fileName = filePath;
68-
fileInfo = imfinfo(myds.fileName);
69-
assert(strcmp(fileInfo(1).Format,'tif'), "Must be tiff format");
70+
if isfield(options, 'datasetType')
71+
if ~ismember(options.datasetType, {'nwb', 'tiff'})
72+
error('The dataset type is either "nwb" or "tiff".');
73+
end
74+
myds.datasetType = options.datasetType;
75+
else
76+
myds.datasetType = 'tiff';
77+
end
78+
if strcmp(myds.datasetType, 'nwb')
79+
if ~isfield(options, 'NwbDatasetPath')
80+
myds.NwbDatasetPath = '/acquisition/motion_corrected_stack/data';
81+
else
82+
myds.NwbDatasetPath = options.NwbDatasetPath;
83+
end
84+
try
85+
h5info(myds.fileName, myds.NwbDatasetPath);
86+
catch ME
87+
error('Cannot read the selected path from the nwb file: %s', ME.message);
88+
end
89+
end
90+
dimensions = getFileFrameDimensions(myds);
7091
if ~myds.doAutoResize
71-
assert((fileInfo(1).Width == myds.outputFrameSize(1)) ...
72-
&& (fileInfo(1).Height == myds.outputFrameSize(2)),...
92+
assert((dimensions.Width == myds.outputFrameSize(1)) ...
93+
&& (dimensions.Height == myds.outputFrameSize(2)),...
7394
"Actual frame size is not equal to specified outputFrameSize");
7495
end
7596
framePerSetCount = myds.numberOfFlankingFrames + 3;
76-
assert(numel(fileInfo) >= framePerSetCount, "Not enough frames in stack for DeepInterpolation");
77-
myds.frameCount = numel(fileInfo);
97+
assert(getNumberOfFrames(myds) >= framePerSetCount, "Not enough frames in stack for DeepInterpolation");
98+
myds.frameCount = getNumberOfFrames(myds);
7899
myds.dsSetCount = myds.frameCount - framePerSetCount + 1;
79100
myds.allSetFrameStartIndices = 1:myds.dsSetCount;
80101
reset(myds);
81102
end
82103

104+
105+
83106
function tf = hasdata(myds)
84107
% Return true if more data is available.
85108
tf = myds.setsAvailable > 0;
@@ -88,35 +111,23 @@
88111
function [data,info] = read(myds)
89112
% Read data and information about the extracted data.
90113
assert(hasdata(myds), "No more data to read");
91-
rawRefFrame = imread(myds.fileName,myds.currentFrameIndex);
92-
if myds.doAutoResize
93-
rawRefFrame = imresize(rawRefFrame,myds.outputFrameSize);
94-
end
95-
refFrame = single(rawRefFrame);
114+
refFrame = getSingleFrame(myds, myds.currentFrameIndex);
96115
flankingFrames = single( ...
97116
ones(myds.outputFrameSize(1), ...
98117
myds.outputFrameSize(2), ...
99118
myds.numberOfFlankingFrames) ...
100119
);
101120
framecount = 1;
102121
for leftFrame = 0:(myds.numberOfFlankingFrames/2) - 1
103-
rawThisFrame = imread(myds.fileName,myds.setFramesStartIndex+leftFrame);
104-
if myds.doAutoResize
105-
rawThisFrame = imresize(rawThisFrame,myds.outputFrameSize);
106-
end
107-
thisFrame = single(rawThisFrame);
122+
thisFrame = getSingleFrame(myds, myds.setFramesStartIndex+leftFrame);
108123
flankingFrames(:,:,framecount) = thisFrame;
109124
framecount = framecount + 1;
110125
end
111126
%frame myds.numberOfFlankingFrames + 1 is center frame. One
112127
%frame before and one after is left out.
113128
startRightFrame = myds.numberOfFlankingFrames/2 + 3;
114129
for rightFrame = startRightFrame:startRightFrame + myds.numberOfFlankingFrames/2 - 1
115-
rawThisFrame = imread(myds.fileName,myds.setFramesStartIndex+rightFrame);
116-
if myds.doAutoResize
117-
rawThisFrame = imresize(rawThisFrame,myds.outputFrameSize);
118-
end
119-
thisFrame = single(rawThisFrame);
130+
thisFrame = getSingleFrame(myds, myds.setFramesStartIndex+rightFrame);
120131
flankingFrames(:,:,framecount) = thisFrame;
121132
framecount = framecount + 1;
122133
end
@@ -176,6 +187,74 @@ function reset(myds)
176187
function n = maxpartitions(myds)
177188
n = myds.dsSetCount;
178189
end
190+
191+
function rawFrame = readFrameFromTiff(myds, index)
192+
rawFrame = imread(myds.fileName, index);
193+
end
194+
195+
function rawFrame = readFrameFromNwb(myds, index)
196+
dimensions = getNwbFrameDimensions(myds);
197+
index = double(index);
198+
rawFrame = h5read(myds.fileName, myds.NwbDatasetPath, [1, 1, index], [dimensions.Width, dimensions.Height, 1]);
199+
end
200+
201+
function frame = getSingleFrame(myds, index)
202+
if strcmp(myds.datasetType, 'tiff')
203+
rawFrame = readFrameFromTiff(myds, index);
204+
elseif strcmp(myds.datasetType, 'nwb')
205+
rawFrame = readFrameFromNwb(myds, index);
206+
else
207+
error('Unsupported dataset type.');
208+
end
209+
if myds.doAutoResize
210+
rawFrame = imresize(rawFrame, myds.outputFrameSize);
211+
end
212+
frame = single(rawFrame);
213+
end
214+
215+
function dimensions = getTiffFrameDimensions(myds)
216+
fileInfo = imfinfo(myds.fileName);
217+
dimensions.Width = fileInfo(1).Width;
218+
dimensions.Height = fileInfo(1).Height;
219+
end
220+
221+
function dimensions = getNwbFrameDimensions(myds)
222+
datasetInfo = h5info(myds.fileName, myds.NwbDatasetPath);
223+
datasetSize = datasetInfo.Dataspace.Size;
224+
dimensions.Width = datasetSize(1);
225+
dimensions.Height = datasetSize(2);
226+
end
227+
228+
function dimensions = getFileFrameDimensions(myds)
229+
if strcmp(myds.datasetType, 'tiff')
230+
dimensions = getTiffFrameDimensions(myds);
231+
elseif strcmp(myds.datasetType, 'nwb')
232+
dimensions = getNwbFrameDimensions(myds);
233+
else
234+
error('Unsupported dataset type.');
235+
end
236+
end
237+
238+
function numFrames = getTiffNumberOfFrames(myds)
239+
fileInfo = imfinfo(myds.fileName);
240+
numFrames = numel(fileInfo);
241+
end
242+
243+
function numFrames = getNwbNumberOfFrames(myds)
244+
datasetInfo = h5info(myds.fileName, myds.NwbDatasetPath);
245+
numFrames = datasetInfo.Dataspace.Size(3);
246+
end
247+
248+
function numFrames = getNumberOfFrames(myds)
249+
if strcmp(myds.datasetType, 'tiff')
250+
numFrames = getTiffNumberOfFrames(myds);
251+
elseif strcmp(myds.datasetType, 'nwb')
252+
numFrames = getNwbNumberOfFrames(myds);
253+
else
254+
error('Unsupported dataset type.');
255+
end
256+
end
257+
179258
end
180259

181260
end
-29.2 KB
Binary file not shown.

gettingStarted.mlx

89 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)