This repository has been archived by the owner on Dec 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathh5bm.m
494 lines (458 loc) · 19.8 KB
/
h5bm.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
classdef h5bm < handle
properties (Access = private)
filePath;
fileHandle;
write = false;
major;
minor;
patch;
end
properties (Constant)
versionstring = 'H5BM-v0.0.4';
versionmajor = 0;
versionminor = 0;
versionpatch = 4;
end
properties (Dependent)
date;
version;
comment;
end
methods
%% Constructor
function obj = h5bm (filePath, flags)
obj.filePath = filePath;
if strcmp(flags, 'H5F_ACC_RDONLY')
if exist(obj.filePath, 'file') ~= 2
error('File ''%s'' does not exist.', obj.filePath);
else
obj.fileHandle = H5F.open(filePath, flags, 'H5P_DEFAULT');
[~, tok] = regexp(obj.version, 'H5BM-v(\d).(\d).(\d)', 'match', 'tokens');
obj.major = str2double(tok{1}{1});
obj.minor = str2double(tok{1}{2});
obj.patch = str2double(tok{1}{3});
end
end
end
%% Destructor
function delete (obj)
H5F.close(obj.fileHandle);
end
%% Get the date
function date = get.date (obj)
try
attr_id = H5A.open(obj.fileHandle, 'date');
date = transpose(H5A.read(attr_id));
catch e
error(['The attribute ''date'' does not seem to exist: ' e.message]);
end
end
%% Get the date of the repetition
function date = getDate (obj, mode, repetition)
try
attr_id = H5A.open(obj.repetitionHandle(mode, repetition), 'date');
date = transpose(H5A.read(attr_id));
catch e
error(['The attribute ''date'' does not seem to exist: ' e.message]);
end
end
%% Get the version
function version = get.version (obj)
try
attr_id = H5A.open(obj.fileHandle, 'version');
version = transpose(H5A.read(attr_id));
catch e
error(['The attribute ''version'' does not seem to exist: ' e.message]);
end
end
%% Get the comment
function comment = get.comment (obj)
try
attr_id = H5A.open(obj.fileHandle, 'comment');
comment = transpose(H5A.read(attr_id));
catch e
error(['The attribute ''comment'' does not seem to exist: ' e.message]);
end
end
%% Get the resolution in specific direction
function resolution = getResolution (obj, mode, repetition, direction)
direction = ['resolution-' direction];
group_id = obj.payloadHandle(mode, repetition);
try
attr_id = H5A.open(group_id, direction);
resolution = transpose(H5A.read(attr_id));
catch e
error(['The attribute ' direction ' does not seem to exist: ' e.message]);
end
end
%% Get the resolution in x-direction
function resolution = getResolutionX (obj, mode, repetition)
resolution = getResolution(obj, mode, repetition, 'x');
end
%% Get the resolution in y-direction
function resolution = getResolutionY (obj, mode, repetition)
resolution = getResolution(obj, mode, repetition, 'y');
end
%% Get the resolution in z-direction
function resolution = getResolutionZ (obj, mode, repetition)
resolution = getResolution(obj, mode, repetition, 'z');
end
%% Get the positions in specific direction
function positions = getPositions (obj, mode, repetition, direction)
direction = ['positions-' direction];
try
dset_id = H5D.open(obj.payloadHandle(mode, repetition), direction);
positions = H5D.read(dset_id);
catch
error('The dataset ''%s'' cannot be found.', direction);
end
end
%% Get the positions in x-direction
function positions = getPositionsX (obj, mode, repetition)
positions = getPositions (obj, mode, repetition, 'x');
end
%% Get the positions in y-direction
function positions = getPositionsY (obj, mode, repetition)
positions = getPositions (obj, mode, repetition, 'y');
end
%% Get the positions in z-direction
function positions = getPositionsZ (obj, mode, repetition)
positions = getPositions (obj, mode, repetition, 'z');
end
%% Get the repetition names for the specified mode
function repetitions = getRepetitions (obj, mode)
% Storing multiple repetitions is only supported since H5BM-v0.0.4
if (obj.fileVersionMatches(struct('major', 0, 'minor', 0, 'patch', 4)))
[~, ~, repetitions] = H5L.iterate(obj.modeHandle(mode), 'H5_INDEX_NAME' , 'H5_ITER_INC', 0, @addMemberName, {});
else
% Otherwise we return '0'
repetitions = {'0'};
end
%%
function [status, memberNames] = addMemberName(~, memberName, memberNames)
%% Add group to array of repetitions
memberNames{length(memberNames)+1} = memberName;
status = 0;
end
end
%% Get the scale calibration data for a specific repetition
function scaleCalibration = getScaleCalibration(obj, mode, repetition)
payload_group = obj.payloadHandle(mode, repetition);
try
scaleCalibration_group = H5G.open(payload_group, 'scaleCalibration');
points = {'micrometerToPixX', 'micrometerToPixY', 'origin', ...
'pixToMicrometerX', 'pixToMicrometerY', 'positionScanner', 'positionStage'};
scaleCalibration = struct();
for jj = 1:length(points)
group = H5G.open(scaleCalibration_group, points{jj});
attr_x = H5A.open(group, 'x');
x = H5A.read(attr_x);
H5A.close(attr_x);
attr_y = H5A.open(group, 'y');
y = H5A.read(attr_y);
H5A.close(attr_y);
scaleCalibration.(points{jj}) = [x y];
end
catch e
error(['The scale calibration is invalid: ' e.message]);
end
end
%% Get the payload data for the specified mode
function data = readPayloadData (varargin)
obj = varargin{1};
mode = varargin{2};
repetition = varargin{3};
type = varargin{4};
if nargin == 7 % Brillouin mode
indx = varargin{5};
indy = varargin{6};
indz = varargin{7};
if indx > obj.getResolutionX(mode, repetition) || indy > obj.getResolutionY(mode, repetition)
error('Index exceeds matrix dimensions.');
end
if indx < 0 || indy < 0
error('Subscript indices must either be real positive integers or logicals.');
end
imageNr = ((indz-1)*(obj.getResolutionX(mode, repetition)*obj.getResolutionY(mode, repetition)) + ...
(indy-1)*obj.getResolutionX(mode, repetition) + (indx-1));
elseif nargin == 5 % ODT mode
imageNr = varargin{5};
if imageNr < 0
error('Subscript indices must either be real positive integers or logicals.');
end
elseif nargin == 4
else
return;
end
if strcmp(type, 'data')
try
dset_id = H5D.open(obj.payloadDataHandle(mode, repetition), num2str(imageNr));
data = double(H5D.read(dset_id));
% Images need to be rotated for correct orientation
data = rot90(data);
catch
error('The dataset ''%s'' cannot be found.', num2str(imageNr));
end
elseif strcmp(type, 'date')
try
dset_id = H5D.open(obj.payloadDataHandle(mode, repetition), num2str(imageNr));
attr_id = H5A.open(dset_id, 'date');
data = H5A.read(attr_id);
data = transpose(data);
catch
error('The attribute ''date'' of the dataset ''%s'' cannot be found.', num2str(imageNr));
end
elseif strcmp(type, 'channel')
try
dset_id = H5D.open(obj.payloadDataHandle(mode, repetition), num2str(imageNr));
attr_id = H5A.open(dset_id, 'channel');
data = H5A.read(attr_id);
data = transpose(data);
catch
error('The attribute ''channel'' of the dataset ''%s'' cannot be found.', num2str(imageNr));
end
elseif strcmp(type, 'memberNames')
try
[~, ~, data] = H5L.iterate(obj.payloadDataHandle(mode, repetition), ...
'H5_INDEX_NAME' , 'H5_ITER_INC', 0, @addMemberName, {});
catch
error('Could not determine the members of this group.');
end
elseif strcmp(type, 'exposure')
try
dset_id = H5D.open(obj.payloadDataHandle(mode, repetition), num2str(imageNr));
attr_id = H5A.open(dset_id, 'exposure');
data = H5A.read(attr_id);
data = transpose(data);
catch
% The exposure time is only saved for newer files, so
% we fall back to an error value.
data = NaN;
end
elseif strcmp(type, 'ROI')
try
data = struct();
attributes = {'left', 'bottom', 'width_physical', 'height_physical', ...
'right', 'top', 'width_binned', 'height_binned'};
found = false;
for jj = 1:length(attributes)
try
dset_id = H5D.open(obj.payloadDataHandle(mode, repetition), num2str(imageNr));
attr_id = H5A.open(dset_id, ['ROI_' attributes{jj}]);
data.(attributes{jj}) = double(H5A.read(attr_id));
H5A.close(attr_id);
H5D.close(dset_id);
found = true;
catch
data.(attributes{jj}) = NaN;
end
end
if ~found
throw('ROI data is not available.');
end
catch
error(['The attribute ' type ' of the dataset ''%s'' cannot be found.'], num2str(imageNr));
end
else
error('The specified data type is not supported.');
end
%%
function [status, memberNames] = addMemberName(~, memberName, memberNames)
%% Add group to array of repetitions
memberNames{length(memberNames)+1} = memberName;
status = 0;
end
end
%% Read the background data
function data = readBackgroundData (obj, mode, repetition, type)
index = 1;
if strcmp(type, 'data')
try
dset_id = H5D.open(obj.backgroundDataHandle(mode, repetition), num2str(index));
data = double(H5D.read(dset_id));
% Images need to be rotated for correct orientation
data = rot90(data);
catch
error('The dataset ''%s'' cannot be found.', num2str(index));
end
elseif strcmp(type, 'date')
try
dset_id = H5D.open(obj.backgroundDataHandle(mode, repetition), num2str(index));
attr_id = H5A.open(dset_id, 'date');
data = H5A.read(attr_id);
data = transpose(data);
catch
error('The attribute ''date'' of the dataset ''%s'' cannot be found.', num2str(index));
end
else
error('The specified data type is not supported.');
end
end
%% Read the calibration data
function data = readCalibrationData (obj, mode, repetition, type, index)
if strcmp(type, 'data')
try
dset_id = H5D.open(obj.calibrationDataHandle(mode, repetition), num2str(index));
data = double(H5D.read(dset_id));
% Images need to be rotated for correct orientation
data = rot90(data);
catch
error('The dataset ''%s'' cannot be found.', num2str(index));
end
elseif strcmp(type, 'date')
try
dset_id = H5D.open(obj.calibrationDataHandle(mode, repetition), num2str(index));
attr_id = H5A.open(dset_id, 'date');
data = H5A.read(attr_id);
data = transpose(data);
catch
error('The attribute ''date'' of the dataset ''%s'' cannot be found.', num2str(index));
end
elseif strcmp(type, 'sample')
try
dset_id = H5D.open(obj.calibrationDataHandle(mode, repetition), num2str(index));
attr_id = H5A.open(dset_id, 'sample');
data = H5A.read(attr_id);
data = transpose(data);
catch
error('The attribute ''sample'' of the dataset ''%s'' cannot be found.', num2str(index));
end
elseif strcmp(type, 'shift')
try
dset_id = H5D.open(obj.calibrationDataHandle(mode, repetition), num2str(index));
attr_id = H5A.open(dset_id, 'shift');
data = H5A.read(attr_id);
data = transpose(data);
catch
error('The attribute ''shift'' of the dataset ''%s'' cannot be found.', num2str(index));
end
else
error('The specified data type is not supported.');
end
end
end
methods (Access = private)
%% Get handle for mode group (Brillouin, ODT or Fluorescence)
function group_id = modeHandle (obj, mode)
if (obj.fileVersionMatches(struct('major', 0, 'minor', 0, 'patch', 4)))
group_id = H5G.open(obj.fileHandle, mode);
elseif strcmp(mode, 'Brillouin')
group_id = obj.fileHandle;
else
ME = MException('H5BM:modeNotSupported', ...
'This file version does not support to store %s data.', mode);
throw(ME);
end
end
%% Get handle for repetition group
function group_id = repetitionHandle (obj, mode, repetition)
if (obj.fileVersionMatches(struct('major', 0, 'minor', 0, 'patch', 4)))
try
group_id = H5G.open(obj.modeHandle(mode), num2str(repetition));
catch
ME = MException('H5BM:repetitionNotFound', ...
'The requested repetition %s was not found.', num2str(repetition));
throw(ME);
end
elseif (repetition == 0)
group_id = obj.fileHandle;
else
ME = MException('H5BM:noRepetitions', ...
'This file version does not support to store more than one repetition.');
throw(ME);
end
end
%% Get handle for payload group of specified mode
function group_id = payloadHandle(obj, mode, repetition)
try
group_id = H5G.open(obj.repetitionHandle(mode, repetition), 'payload');
catch
ME = MException('H5BM:couldNotOpenPayload', ...
'Could not open the payload handle for mode %s, repetition %s.', mode, num2str(repetition));
throw(ME);
end
end
%% Get handle for payload data
function data_id = payloadDataHandle (obj, mode, repetition)
group_id = obj.payloadHandle(mode, repetition);
try
data_id = H5G.open(group_id, 'data');
catch
end
end
%% Get handle for background data
function data_id = backgroundDataHandle (obj, mode, repetition)
try
data_id = H5G.open(obj.repetitionHandle(mode, repetition), 'background');
catch
end
end
%% Get handle for calibration group
function group_id = calibrationHandle (obj, mode, repetition)
try
group_id = H5G.open(obj.repetitionHandle(mode, repetition), 'calibration');
catch
end
end
%% Get handle for calibration data
function data_id = calibrationDataHandle (obj, mode, repetition)
group_id = obj.calibrationHandle(mode, repetition);
try
% starting with H5BM-v0.0.4 the group is called 'data'
data_id = H5G.open(group_id, 'data');
catch
data_id = H5G.open(group_id, 'calibrationData');
end
end
%% Parse datetime to ISO string
function datum = parseDate (obj, datestring)
format = obj.dateFormat(datestring);
if format
datum = datetime(datestring, 'InputFormat', format, 'TimeZone', 'UTC');
else
datum = datetime(datestring);
end
datum.TimeZone = 'local';
datum.Format = format;
datum = char(datum);
end
%% Check datestring for validity
function check = checkDate (obj, datestring)
try
format = obj.dateFormat(datestring);
if format
datetime(datestring, 'InputFormat', format, 'TimeZone', 'UTC');
else
datetime(datestring);
end
check = true;
catch
check = false;
end
end
%% Check if file version fullfils requested version number
function is = fileVersionMatches(obj, requestedVersion)
if (obj.major < requestedVersion.major)
is = false;
elseif (obj.minor < requestedVersion.minor)
is = false;
elseif (obj.patch < requestedVersion.patch)
is = false;
else
is = true;
end
end
end
methods (Static)
%% Get input date format
function format = dateFormat (datestring)
if regexp(datestring, '\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z)')
format = 'uuuu-MM-dd''T''HH:mm:ssXXX';
elseif regexp(datestring, '\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d{3}([+-][0-2]\d:[0-5]\d|Z)')
format = 'uuuu-MM-dd''T''HH:mm:ss.SSSXXX';
else
format = '';
end
end
end
end