Debian Codegraph @

matlab/+ismrmrd/Dataset.m

raw
1 classdef Dataset
2
3 properties
4 fid = -1;
5 filename = '';
6 datapath = '';
7 waveformpath = '';
8 xmlpath = '';
9 htypes = [];
10 end
11
12 methods
13
14 function obj = Dataset(filename,groupname)
15
16 % Set the hdf types
17 obj.htypes = ismrmrd.util.hdf5_datatypes;
18
19 % If the file exists, open it for read/write
20 % otherwise, create it
21 if exist(filename,'file')
22 obj.fid = H5F.open(filename,'H5F_ACC_RDWR','H5P_DEFAULT');
23 else
24 fcpl = H5P.create('H5P_FILE_CREATE');
25 obj.fid = H5F.create(filename,'H5F_ACC_TRUNC',fcpl,'H5P_DEFAULT');
26 H5P.close(fcpl);
27 end
28
29 % Set the filename
30 obj.filename = filename;
31
32 % Set the group name
33 % default is dataset
34 if nargin == 1
35 groupname = 'dataset';
36 end
37 % Set the paths
38 grouppath = ['/' groupname];
39 obj.xmlpath = ['/' groupname '/xml'];
40 obj.datapath = ['/' groupname '/data'];
41 obj.waveformpath= ['/' groupname '/waveforms'];
42
43 % Check if the group exists
44 lapl_id=H5P.create('H5P_LINK_ACCESS');
45 if (H5L.exists(obj.fid,grouppath,lapl_id) == 0)
46 % group does not exist, create it
47 group_id = H5G.create(obj.fid, grouppath, 0);
48 H5G.close(group_id);
49 end
50 H5P.close(lapl_id);
51
52 end
53
54 function obj = close(obj)
55 % close the file
56 H5F.close(obj.fid);
57 end
58
59 function xmlstring = readxml(obj)
60 % Check if the XML header exists
61 lapl_id=H5P.create('H5P_LINK_ACCESS');
62 if (H5L.exists(obj.fid,obj.xmlpath,lapl_id) == 0)
63 error('No XML header found.');
64 end
65 H5P.close(lapl_id);
66
67 % Open
68 xml_id = H5D.open(obj.fid, obj.xmlpath);
69
70 % Get the type
71 xml_dtype = H5D.get_type(xml_id);
72
73 % Read the data
74 hdr = H5D.read(xml_id, xml_dtype, 'H5S_ALL', 'H5S_ALL', 'H5P_DEFAULT');
75
76 % Output depends on whether or not the stored string was variale length
77 if (H5T.is_variable_str(xml_dtype))
78 xmlstring = hdr{1};
79 else
80 xmlstring = hdr';
81 end
82
83 % Close the XML
84 H5T.close(xml_dtype);
85 H5D.close (xml_id);
86 end
87
88 function writexml(obj,xmlstring)
89 % No validation is performed. You're on your own.
90 % make sure it's a char
91 xmlstring = char(xmlstring);
92
93 % TODO: add error checking on the write and return a status
94 % TODO: if the matlab variable length string bug is resolved
95 % then we should change this logic to just modify the length
96 % and overwrite.
97
98 % Check if the XML header exists
99 % if it does not exist, create it
100 % if it exists, modify the size appropriately
101 lapl_id=H5P.create('H5P_LINK_ACCESS');
102 if (H5L.exists(obj.fid,obj.xmlpath,lapl_id) == 1)
103 % Delete it
104 H5L.delete(obj.fid, obj.xmlpath,'H5P_DEFAULT');
105 end
106 H5P.close(lapl_id);
107
108 % Set variable length string type
109 xml_dtype = H5T.copy('H5T_C_S1');
110 % Matlab is having trouble writing variable length strings
111 % that are longer that 512 characters. Switched to fixed
112 % length.
113 H5T.set_size(xml_dtype,'H5T_VARIABLE');
114 %H5T.set_size(xml_dtype, length(xmlstring));
115 xml_space_id = H5S.create_simple (1, 1, []);
116 xml_id = H5D.create(obj.fid, obj.xmlpath, xml_dtype, ....
117 xml_space_id, 'H5P_DEFAULT');
118 H5S.close(xml_space_id);
119
120 % Write the data
121 H5D.write(xml_id, xml_dtype, ...
122 'H5S_ALL', 'H5S_ALL', 'H5P_DEFAULT', {xmlstring});
123
124 % Close the XML
125 H5D.close(xml_id);
126 H5T.close(xml_dtype);
127 end
128
129 function nacq = getNumberOfAcquisitions(obj)
130
131 % Check if the Data exists
132 lapl_id=H5P.create('H5P_LINK_ACCESS');
133 if (H5L.exists(obj.fid, obj.datapath, lapl_id) == 0)
134 error([obj.datapath ' does not exist in the HDF5 dataset.']);
135 end
136 dset = H5D.open(obj.fid, obj.datapath);
137 space = H5D.get_space(dset);
138 H5S.get_simple_extent_dims(space);
139 [~,dims,~] = H5S.get_simple_extent_dims(space);
140 nacq = dims(1);
141 H5S.close(space);
142 H5D.close(dset);
143 H5P.close(lapl_id);
144 end
145
146 function block = readAcquisition(obj, start, stop)
147 if nargin == 1
148 % Read all the acquisitions
149 start = 1;
150 stop = -1;
151 elseif nargin == 2
152 % Read a single acquisition
153 stop = start;
154 end
155
156 % Check if the Data exists
157 lapl=H5P.create('H5P_LINK_ACCESS');
158 if (H5L.exists(obj.fid, obj.datapath, lapl) == 0)
159 error([obj.datapath ' does not exist in the HDF5 dataset.']);
160 end
161 H5P.close(lapl);
162
163 % Open the data
164 dset = H5D.open(obj.fid, obj.datapath);
165
166 % Open the data space
167 space = H5D.get_space(dset);
168
169 % Get the size
170 [~,dims,~] = H5S.get_simple_extent_dims(space);
171 nacq = dims(1);
172
173 % Create a mem_space for reading
174 if (stop >= start)
175 offset = [start-1];
176 dims = [stop-start+1];
177 mem_space = H5S.create_simple(1,dims,[]);
178 else
179 offset = [0];
180 dims = [nacq];
181 mem_space = H5S.create_simple(1,dims,[]);
182 end
183
184 % Read the desired acquisitions
185 H5S.select_hyperslab(space,'H5S_SELECT_SET',offset,[1],[1],dims);
186 d = H5D.read(dset, obj.htypes.T_Acquisition, ...
187 mem_space, space, 'H5P_DEFAULT');
188
189 % Pack'em
190 block = ismrmrd.Acquisition(d.head, d.traj, d.data);
191
192 % Clean up
193 H5S.close(mem_space);
194 H5S.close(space);
195 H5D.close(dset);
196 end
197
198 function appendAcquisition(obj, acq)
199 % Append an acquisition
200
201 % TODO: Check the type of the input
202
203 % The number of acquisitions that we are going to append
204 N = acq.getNumber();
205
206 % Check if the Data exists
207 % if it does not exist, create it
208 % if it does exist increase it's size
209 lapl_id=H5P.create('H5P_LINK_ACCESS');
210 if (H5L.exists(obj.fid, obj.datapath, lapl_id) == 0)
211 % Data does not exist
212 % create with rank 1, unlimited, and set the chunk size
213 dims = [N];
214 maxdims = [H5ML.get_constant_value('H5S_UNLIMITED')];
215 file_space_id = H5S.create_simple(1, dims, maxdims);
216
217 dcpl = H5P.create('H5P_DATASET_CREATE');
218 chunk = [1];
219 H5P.set_chunk (dcpl, chunk);
220 data_id = H5D.create(obj.fid, obj.datapath, ...
221 obj.htypes.T_Acquisition, ...
222 file_space_id, dcpl);
223 H5P.close(dcpl);
224 H5S.close(file_space_id);
225
226 else
227 % Open the data
228 data_id = H5D.open(obj.fid, obj.datapath);
229
230 % Open the data space
231 file_space_id = H5D.get_space(data_id);
232
233 % Get the size, increment by N
234 H5S.get_simple_extent_dims(file_space_id);
235 [~,dims,~] = H5S.get_simple_extent_dims(file_space_id);
236 dims = [dims(1)+N];
237 H5D.set_extent (data_id, dims);
238 H5S.close(file_space_id);
239
240 end
241 H5P.close(lapl_id);
242
243 % Get the file space
244 file_space_id = H5D.get_space(data_id);
245 [~,dims,~] = H5S.get_simple_extent_dims(file_space_id);
246
247 % Select the last N block
248 offset = [dims(1)-N];
249 H5S.select_hyperslab(file_space_id,'H5S_SELECT_SET',offset,[1],[1],[N]);
250
251 % Mem space
252 mem_space_id = H5S.create_simple(1,[N],[]);
253
254 % Check and fix the acquisition header types
255 acq.head.check();
256 % TODO: Error checking on the sizes of the data and trajectories.
257
258 % Pack the acquisition into the correct struct for writing
259 d = struct();
260 d.head = acq.head.toStruct();
261 d.traj = acq.trajToFloat();
262 d.data = acq.dataToFloat();
263
264 % Write
265 H5D.write(data_id, obj.htypes.T_Acquisition, ...
266 mem_space_id, file_space_id, 'H5P_DEFAULT', d);
267
268 % Clean up
269 H5S.close(mem_space_id);
270 H5S.close(file_space_id);
271 H5D.close(data_id);
272 end
273
274
275 function nacq = getNumberOfWaveforms(obj)
276
277 % Check if the Data exists
278 lapl_id=H5P.create('H5P_LINK_ACCESS');
279 if (H5L.exists(obj.fid, obj.waveformpath, lapl_id) == 0)
280 error([obj.datapath ' does not exist in the HDF5 dataset.']);
281 end
282 dset = H5D.open(obj.fid, obj.waveformpath);
283 space = H5D.get_space(dset);
284 H5S.get_simple_extent_dims(space);
285 [~,dims,~] = H5S.get_simple_extent_dims(space);
286 nacq = dims(1);
287 H5S.close(space);
288 H5D.close(dset);
289 H5P.close(lapl_id);
290 end
291
292 function block = readWaveform(obj, start, stop)
293 if nargin == 1
294 % Read all the acquisitions
295 start = 1;
296 stop = -1;
297 elseif nargin == 2
298 % Read a single acquisition
299 stop = start;
300 end
301
302 % Check if the Data exists
303 lapl=H5P.create('H5P_LINK_ACCESS');
304 if (H5L.exists(obj.fid, obj.waveformpath, lapl) == 0)
305 error([obj.waveformpath ' does not exist in the HDF5 dataset.']);
306 end
307 H5P.close(lapl);
308
309 % Open the data
310 dset = H5D.open(obj.fid, obj.waveformpath);
311
312 % Open the data space
313 space = H5D.get_space(dset);
314
315 % Get the size
316 [~,dims,~] = H5S.get_simple_extent_dims(space);
317 nacq = dims(1);
318
319 % Create a mem_space for reading
320 if (stop >= start)
321 offset = [start-1];
322 dims = [stop-start+1];
323 mem_space = H5S.create_simple(1,dims,[]);
324 else
325 offset = [0];
326 dims = [nacq];
327 mem_space = H5S.create_simple(1,dims,[]);
328 end
329
330 % Read the desired acquisitions
331 H5S.select_hyperslab(space,'H5S_SELECT_SET',offset,[1],[1],dims);
332 d = H5D.read(dset, obj.htypes.T_Waveform, ...
333 mem_space, space, 'H5P_DEFAULT');
334
335 % Pack'em
336 block = ismrmrd.Waveform(d.head, d.data);
337
338 % Clean up
339 H5S.close(mem_space);
340 H5S.close(space);
341 H5D.close(dset);
342 end
343
344 function appendWaveform(obj, wav)
345 % Append an acquisition
346
347 % TODO: Check the type of the input
348
349 % The number of acquisitions that we are going to append
350 N = wav.getNumber();
351
352 % Check if the Data exists
353 % if it does not exist, create it
354 % if it does exist increase it's size
355 lapl_id=H5P.create('H5P_LINK_ACCESS');
356 if (H5L.exists(obj.fid, obj.waveformpath, lapl_id) == 0)
357 % Data does not exist
358 % create with rank 1, unlimited, and set the chunk size
359 dims = [N];
360 maxdims = [H5ML.get_constant_value('H5S_UNLIMITED')];
361 file_space_id = H5S.create_simple(1, dims, maxdims);
362
363 dcpl = H5P.create('H5P_DATASET_CREATE');
364 chunk = [1];
365 H5P.set_chunk (dcpl, chunk);
366 data_id = H5D.create(obj.fid, obj.waveformpath, ...
367 obj.htypes.T_Waveform, ...
368 file_space_id, dcpl);
369 H5P.close(dcpl);
370 H5S.close(file_space_id);
371
372 else
373 % Open the data
374 data_id = H5D.open(obj.fid, obj.waveformpath);
375
376 % Open the data space
377 file_space_id = H5D.get_space(data_id);
378
379 % Get the size, increment by N
380 H5S.get_simple_extent_dims(file_space_id);
381 [~,dims,~] = H5S.get_simple_extent_dims(file_space_id);
382 dims = [dims(1)+N];
383 H5D.set_extent (data_id, dims);
384 H5S.close(file_space_id);
385
386 end
387 H5P.close(lapl_id);
388
389 % Get the file space
390 file_space_id = H5D.get_space(data_id);
391 [~,dims,~] = H5S.get_simple_extent_dims(file_space_id);
392
393 % Select the last N block
394 offset = [dims(1)-N];
395 H5S.select_hyperslab(file_space_id,'H5S_SELECT_SET',offset,[1],[1],[N]);
396
397 % Mem space
398 mem_space_id = H5S.create_simple(1,[N],[]);
399
400 % Check and fix the acquisition header types
401
402 % TODO: Error checking on the sizes of the data and trajectories.
403
404 % Pack the acquisition into the correct struct for writing
405 d = struct();
406 d.head = wav.head.toStruct();
407 d.data = cellfun(@uint32,wav.data,'UniformOutput',false);
408
409
410 % Write
411 H5D.write(data_id, obj.htypes.T_Waveform, ...
412 mem_space_id, file_space_id, 'H5P_DEFAULT', d);
413
414 % Clean up
415 H5S.close(mem_space_id);
416 H5S.close(file_space_id);
417 H5D.close(data_id);
418 end
419
420
421 function delete(obj)
422 H5F.close(obj.fid);
423 end
424
425
426 end
427
428 end