Skip to content

Commit d3acf7e

Browse files
committed
fixDataTypeErrors: refactor Matlab DataType handling
1 parent e95c2b6 commit d3acf7e

File tree

11 files changed

+183
-146
lines changed

11 files changed

+183
-146
lines changed

+nix/Block.m

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,28 @@
5353
'Block::openDataArray', id_or_name, @nix.DataArray);
5454
end;
5555

56-
%-- As "datatype" provide one of the nix.DataTypes. Alternatively
57-
%-- a string stating one of the datatypes supported by nix can be provided.
5856
function da = create_data_array(obj, name, nixtype, datatype, shape)
59-
handle = nix_mx('Block::createDataArray', obj.nix_handle, ...
60-
name, nixtype, datatype, shape);
61-
da = nix.DataArray(handle);
62-
obj.dataArraysCache.lastUpdate = 0;
57+
if(~isa(datatype, 'nix.DataType'))
58+
error('Please provide a valid nix.DataType');
59+
else
60+
handle = nix_mx('Block::createDataArray', obj.nix_handle, ...
61+
name, nixtype, lower(datatype.char), shape);
62+
da = nix.DataArray(handle);
63+
obj.dataArraysCache.lastUpdate = 0;
64+
end;
6365
end
6466

6567
function da = create_data_array_from_data(obj, name, nixtype, data)
6668
shape = size(data);
67-
dtype = class(data);
69+
if(ischar(data))
70+
dtype = nix.DataType.String;
71+
elseif(islogical(data))
72+
dtype = nix.DataType.Bool;
73+
elseif(isnumeric(data))
74+
dtype = nix.DataType.Double;
75+
else
76+
error('Could not determine DataType of data');
77+
end;
6878

6979
da = obj.create_data_array(name, nixtype, dtype, shape);
7080
da.write_all(data);

+nix/DataType.m

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
classdef DataType
2-
%DATATYPE datatypes supported by nix
2+
% DataTypes supported by nix.
33

4-
properties (Constant)
5-
Boolean = 'bool';
6-
String = 'string';
7-
Double = 'double';
8-
Char = 'char';
9-
Float = 'float';
10-
Int8 = 'int8';
11-
Int16 = 'int16';
12-
Int32 = 'int32';
13-
Int64 = 'int64';
14-
UInt8 = 'uint8';
15-
UInt16 = 'uint16';
16-
UInt32 = 'uint32';
17-
UInt64 = 'uint64';
4+
enumeration
5+
Bool;
6+
String;
7+
Double;
8+
Char;
9+
Float;
10+
Int8;
11+
Int16;
12+
Int32;
13+
Int64;
14+
UInt8;
15+
UInt16;
16+
UInt32;
17+
UInt64;
1818
end
19-
19+
2020
end
21+

+nix/Section.m

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,14 @@
8787
% Property methods
8888
% ----------------
8989

90-
%-- As "datatype" provide one of the nix.DataTypes. Alternatively
91-
%-- a string stating one of the datatypes supported by nix can be provided.
9290
function p = create_property(obj, name, datatype)
93-
p = nix.Property(nix_mx('Section::createProperty', ...
94-
obj.nix_handle, name, datatype));
95-
obj.propsCache.lastUpdate = 0;
91+
if(~isa(datatype, 'nix.DataType'))
92+
error('Please provide a valid nix.DataType');
93+
else
94+
p = nix.Property(nix_mx('Section::createProperty', ...
95+
obj.nix_handle, name, lower(datatype.char)));
96+
obj.propsCache.lastUpdate = 0;
97+
end;
9698
end;
9799

98100
function p = create_property_with_value(obj, name, val)

tests/TestBlock.m

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
assert(isempty(b.dataArrays));
6262

63-
d1 = b.create_data_array('foo', 'bar', 'double', [2 3]);
63+
d1 = b.create_data_array('foo', 'bar', nix.DataType.Double, [2 3]);
6464

6565
assert(strcmp(d1.name, 'foo'));
6666
assert(strcmp(d1.type, 'bar'));
@@ -95,7 +95,7 @@
9595
function [] = test_delete_data_array( varargin )
9696
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
9797
b = f.createBlock('arraytest', 'nixBlock');
98-
tmp = b.create_data_array('dataArrayTest1', 'nixDataArray', 'double', [1 2]);
98+
tmp = b.create_data_array('dataArrayTest1', 'nixDataArray', nix.DataType.Double, [1 2]);
9999
tmp = b.create_data_array('dataArrayTest2', 'nixDataArray', nix.DataType.Double, [3 4]);
100100

101101
assert(size(b.dataArrays, 1) == 2);
@@ -143,8 +143,8 @@
143143
%% Test: Create multitag by data_array entity and data_array id
144144
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
145145
b = f.createBlock('mTagTestBlock', 'nixBlock');
146-
tmp = b.create_data_array('mTagTestDataArray1', 'nixDataArray', 'double', [1 2]);
147-
tmp = b.create_data_array('mTagTestDataArray2', 'nixDataArray', 'double', [3 4]);
146+
tmp = b.create_data_array('mTagTestDataArray1', 'nixDataArray', nix.DataType.Double, [1 2]);
147+
tmp = b.create_data_array('mTagTestDataArray2', 'nixDataArray', nix.DataType.Double, [3 4]);
148148
assert(isempty(b.multiTags));
149149

150150
%-- create by data_array entity
@@ -162,7 +162,7 @@
162162
function [] = test_delete_multi_tag( varargin )
163163
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
164164
b = f.createBlock('mTagTestBlock', 'nixBlock');
165-
tmp = b.create_data_array('mTagTestDataArray1', 'nixDataArray', 'double', [1 2]);
165+
tmp = b.create_data_array('mTagTestDataArray1', 'nixDataArray', nix.DataType.Double, [1 2]);
166166
tmp = b.create_multi_tag('mTagTest1', 'nixMultiTag1', b.dataArrays{1});
167167
tmp = b.create_multi_tag('mTagTest2', 'nixMultiTag2', b.dataArrays{1});
168168

@@ -210,8 +210,8 @@
210210
b = f.createBlock('arraytest', 'nixBlock');
211211

212212
assert(isempty(b.dataArrays));
213-
tmp = b.create_data_array('arrayTest1', 'nixDataArray', 'double', [1 2]);
214-
tmp = b.create_data_array('arrayTest2', 'nixDataArray', 'double', [3 4]);
213+
tmp = b.create_data_array('arrayTest1', 'nixDataArray', nix.DataType.Double, [1 2]);
214+
tmp = b.create_data_array('arrayTest2', 'nixDataArray', nix.DataType.Double, [3 4]);
215215
assert(size(b.dataArrays, 1) == 2);
216216

217217
end
@@ -243,7 +243,7 @@
243243
%% Test: fetch multitags
244244
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
245245
b = f.createBlock('mTagTestBlock', 'nixBlock');
246-
tmp = b.create_data_array('mTagTestDataArray', 'nixDataArray', 'double', [1 2]);
246+
tmp = b.create_data_array('mTagTestDataArray', 'nixDataArray', nix.DataType.Double, [1 2]);
247247

248248
assert(isempty(b.multiTags));
249249
tmp = b.create_multi_tag('mTagTest1', 'nixMultiTag', b.dataArrays{1});
@@ -258,7 +258,7 @@
258258
daName = 'arrayTest1';
259259

260260
assert(isempty(b.dataArrays));
261-
tmp = b.create_data_array(daName, 'nixDataArray', 'double', [1 2]);
261+
tmp = b.create_data_array(daName, 'nixDataArray', nix.DataType.Double, [1 2]);
262262

263263
getDataArrayByID = b.data_array(b.dataArrays{1,1}.id);
264264
assert(~isempty(getDataArrayByID));
@@ -294,7 +294,7 @@
294294
%% Test: Open multi tag by ID or name
295295
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
296296
b = f.createBlock('mTagTestBlock', 'nixBlock');
297-
tmp = b.create_data_array('mTagTestDataArray', 'nixDataArray', 'double', [1 2]);
297+
tmp = b.create_data_array('mTagTestDataArray', 'nixDataArray', nix.DataType.Double, [1 2]);
298298
mTagName = 'mTagTest1';
299299
tmp = b.create_multi_tag(mTagName, 'nixMultiTag', b.dataArrays{1});
300300

@@ -331,7 +331,7 @@
331331
%% Test: Block has multi tag by ID or name
332332
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
333333
b = f.createBlock('mTagTestBlock', 'nixBlock');
334-
tmp = b.create_data_array('mTagTestDataArray', 'nixDataArray', 'double', [1 2]);
334+
tmp = b.create_data_array('mTagTestDataArray', 'nixDataArray', nix.DataType.Double, [1 2]);
335335
tmp = b.create_multi_tag('mTagTest1', 'nixMultiTag', b.dataArrays{1});
336336

337337
assert(b.has_multi_tag(b.multiTags{1,1}.id));
@@ -384,7 +384,7 @@
384384
daName = 'hasDataArrayTest';
385385
f = nix.File(fullfile(pwd, 'tests', fileName), nix.FileMode.Overwrite);
386386
b = f.createBlock('testblock', 'nixBlock');
387-
da = b.create_data_array(daName, 'nixDataArray', 'double', [1 2]);
387+
da = b.create_data_array(daName, 'nixDataArray', nix.DataType.Double, [1 2]);
388388
daID = da.id;
389389

390390
assert(~b.has_data_array('I do not exist'));

tests/TestDataArray.m

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
%% Test: Access Attributes
1919
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
2020
b = f.createBlock('daTestBlock', 'test nixBlock');
21-
da = b.create_data_array('daTest', 'test nixDataArray', 'double', [1 2]);
21+
da = b.create_data_array('daTest', 'test nixDataArray', nix.DataType.Double, [1 2]);
2222

2323
assert(~isempty(da.id));
2424
assert(strcmp(da.name, 'daTest'));
@@ -65,7 +65,7 @@
6565
tmp = f.createSection('testSection1', 'nixSection');
6666
tmp = f.createSection('testSection2', 'nixSection');
6767
b = f.createBlock('testBlock', 'nixBlock');
68-
da = b.create_data_array('testDataArray', 'nixDataArray', 'double', [1 2]);
68+
da = b.create_data_array('testDataArray', 'nixDataArray', nix.DataType.Double, [1 2]);
6969

7070
assert(isempty(da.open_metadata));
7171
da.set_metadata(f.sections{1});
@@ -81,7 +81,7 @@
8181
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
8282
tmp = f.createSection('testSection', 'nixSection');
8383
b = f.createBlock('testBlock', 'nixBlock');
84-
da = b.create_data_array('testDataArray', 'nixDataArray', 'double', [1 2]);
84+
da = b.create_data_array('testDataArray', 'nixDataArray', nix.DataType.Double, [1 2]);
8585
da.set_metadata(f.sections{1});
8686

8787
assert(strcmp(da.open_metadata.name, 'testSection'));
@@ -102,7 +102,7 @@
102102
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
103103
b = f.createBlock('tagtest', 'nixblock');
104104

105-
d1 = b.create_data_array('foo', 'bar', 'double', [2 3]);
105+
d1 = b.create_data_array('foo', 'bar', nix.DataType.Double, [2 3]);
106106
tmp = d1.read_all();
107107
assert(all(tmp(:) == 0));
108108

@@ -118,7 +118,7 @@
118118
getSource = b.create_source('sourceTest', 'nixSource');
119119
tmp = getSource.create_source('nestedSource1', 'nixSource');
120120
tmp = getSource.create_source('nestedSource2', 'nixSource');
121-
getDataArray = b.create_data_array('sourceTestDataArray', 'nixDataArray', 'double', [1 2]);
121+
getDataArray = b.create_data_array('sourceTestDataArray', 'nixDataArray', nix.DataType.Double, [1 2]);
122122

123123
assert(isempty(getDataArray.sources));
124124
getDataArray.add_source(getSource.sources{1}.id);
@@ -133,7 +133,7 @@
133133
getSource = b.create_source('sourceTest', 'nixSource');
134134
tmp = getSource.create_source('nestedSource1', 'nixSource');
135135
tmp = getSource.create_source('nestedSource2', 'nixSource');
136-
getDataArray = b.create_data_array('sourceTestDataArray', 'nixDataArray', 'double', [1 2]);
136+
getDataArray = b.create_data_array('sourceTestDataArray', 'nixDataArray', nix.DataType.Double, [1 2]);
137137

138138
getDataArray.add_source(getSource.sources{1}.id);
139139
getDataArray.add_source(getSource.sources{2});
@@ -150,7 +150,7 @@
150150
function [] = test_dimensions( varargin )
151151
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
152152
b = f.createBlock('daTestBlock', 'test nixBlock');
153-
da = b.create_data_array('daTest', 'test nixDataArray', 'double', [1 2]);
153+
da = b.create_data_array('daTest', 'test nixDataArray', nix.DataType.Double, [1 2]);
154154

155155
assert(isempty(da.dimensions));
156156

tests/TestDimensions.m

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
%% Test: set dimension
1313
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
1414
b = f.createBlock('daTestBlock', 'test nixBlock');
15-
da = b.create_data_array('daTest', 'test nixDataArray', 'double', [1 2]);
15+
da = b.create_data_array(...
16+
'daTest', 'test nixDataArray', nix.DataType.Double, [1 2]);
1617
d1 = da.append_set_dimension();
1718

1819
assert(strcmp(d1.dimensionType, 'set'));
@@ -31,7 +32,8 @@
3132
%% Test: sampled dimension
3233
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
3334
b = f.createBlock('daTestBlock', 'test nixBlock');
34-
da = b.create_data_array('daTest', 'test nixDataArray', 'double', [1 2]);
35+
da = b.create_data_array(...
36+
'daTest', 'test nixDataArray', nix.DataType.Double, [1 2]);
3537
d1 = da.append_sampled_dimension(200);
3638

3739
assert(strcmp(d1.dimensionType, 'sample'));
@@ -64,7 +66,8 @@
6466
%% Test: range dimension
6567
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
6668
b = f.createBlock('daTestBlock', 'test nixBlock');
67-
da = b.create_data_array('daTest', 'test nixDataArray', 'double', [1 2]);
69+
da = b.create_data_array(...
70+
'daTest', 'test nixDataArray', nix.DataType.Double, [1 2]);
6871
ticks = [1 2 3 4];
6972
d1 = da.append_range_dimension(ticks);
7073

@@ -87,4 +90,4 @@
8790

8891
assert(isempty(d1.label));
8992
assert(isempty(d1.unit));
90-
end
93+
end

tests/TestFeature.m

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
function [] = test_open_data ( varargin )
1313
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
1414
b = f.createBlock('featureTest', 'nixBlock');
15-
tmp = b.create_data_array('featureTestDataArray', 'nixDataArray', 'double', [1 2 3 4 5 6]);
15+
tmp = b.create_data_array('featureTestDataArray', 'nixDataArray', nix.DataType.Double, [1 2 3 4 5 6]);
1616
getTag = b.create_tag('featureTest', 'nixTag', [1, 2]);
1717
tmp = getTag.add_feature(b.dataArrays{1}, nix.LinkType.Tagged);
1818

@@ -25,7 +25,7 @@
2525
fileName = 'testRW.h5';
2626
f = nix.File(fullfile(pwd, 'tests', fileName), nix.FileMode.Overwrite);
2727
b = f.createBlock('featureTest', 'nixBlock');
28-
da = b.create_data_array('featureTestDataArray', 'nixDataArray', 'double', [1 2 3 4 5 6]);
28+
da = b.create_data_array('featureTestDataArray', 'nixDataArray', nix.DataType.Double, [1 2 3 4 5 6]);
2929
t = b.create_tag('featureTest', 'nixTag', [1, 2]);
3030
feat = t.add_feature(b.dataArrays{1}, nix.LinkType.Tagged);
3131

@@ -67,10 +67,10 @@
6767
daData = [1 2 3 4 5 6];
6868
f = nix.File(fullfile(pwd, 'tests', fileName), nix.FileMode.Overwrite);
6969
b = f.createBlock('featureTest', 'nixBlock');
70-
da1 = b.create_data_array(daName1, daType, 'double', daData);
71-
da2 = b.create_data_array(daName2, daType, 'double', daData);
72-
da3 = b.create_data_array(daName3, daType, 'double', daData);
73-
da4 = b.create_data_array(daName4, daType, 'double', daData);
70+
da1 = b.create_data_array(daName1, daType, nix.DataType.Double, daData);
71+
da2 = b.create_data_array(daName2, daType, nix.DataType.Double, daData);
72+
da3 = b.create_data_array(daName3, daType, nix.DataType.Double, daData);
73+
da4 = b.create_data_array(daName4, daType, nix.DataType.Double, daData);
7474
t = b.create_tag('featureTest', 'nixTag', [1, 2]);
7575
feat = t.add_feature(b.dataArrays{1}, nix.LinkType.Tagged);
7676

0 commit comments

Comments
 (0)