Skip to content

Commit f142c3c

Browse files
committed
fixDataTypeErrors: refactor string/logical handling
1 parent d3acf7e commit f142c3c

File tree

7 files changed

+149
-59
lines changed

7 files changed

+149
-59
lines changed

+nix/Block.m

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,13 @@
5454
end;
5555

5656
function da = create_data_array(obj, name, nixtype, datatype, shape)
57+
errorStruct.identifier = 'Block:unsupportedDataType';
5758
if(~isa(datatype, 'nix.DataType'))
58-
error('Please provide a valid nix.DataType');
59+
errorStruct.message = 'Please provide a valid nix.DataType';
60+
error(errorStruct);
61+
elseif(isequal(datatype, nix.DataType.String))
62+
errorStruct.message = 'Writing Strings to DataArrays is not supported as of yet.';
63+
error(errorStruct);
5964
else
6065
handle = nix_mx('Block::createDataArray', obj.nix_handle, ...
6166
name, nixtype, lower(datatype.char), shape);
@@ -65,17 +70,20 @@
6570
end
6671

6772
function da = create_data_array_from_data(obj, name, nixtype, data)
68-
shape = size(data);
73+
errorStruct.identifier = 'Block:unsupportedDataType';
6974
if(ischar(data))
70-
dtype = nix.DataType.String;
75+
errorStruct.message = 'Writing Strings to DataArrays is not supported as of yet.';
76+
error(errorStruct);
7177
elseif(islogical(data))
7278
dtype = nix.DataType.Bool;
7379
elseif(isnumeric(data))
7480
dtype = nix.DataType.Double;
7581
else
76-
error('Could not determine DataType of data');
82+
errorStruct.message = 'DataType of provided data is not supported.';
83+
error(errorStruct);
7784
end;
78-
85+
86+
shape = size(data);
7987
da = obj.create_data_array(name, nixtype, dtype, shape);
8088
da.write_all(data);
8189
end
@@ -168,4 +176,4 @@
168176
del, 'nix.MultiTag', 'Block::deleteMultiTag', obj.multiTagsCache);
169177
end;
170178
end;
171-
end
179+
end

+nix/DataArray.m

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,26 @@
8989
end;
9090

9191
%-- TODO add (optional) offset
92+
%-- If a DataArray has been created as boolean or numeric,
93+
%-- provide that only values of the proper DataType can be written.
9294
function write_all(obj, data)
93-
%-- Bugifx: if a data array has been created as
94-
%-- boolean or char, provide check that only logical or char
95-
%-- values can be written, otherwise the
96-
%-- error message from nix is too cryptic.
95+
errorStruct.identifier = 'DataArray:improperDataType';
9796
if(islogical(obj.read_all) && ~islogical(data))
98-
error(strcat('Trying to write', 32, class(data), ...
99-
' to a logical DataArray. Provide data as logical.'));
100-
elseif(ischar(obj.read_all) && ~ischar(data))
101-
error(strcat('Trying to write', 32, class(data), ...
102-
' to a char DataArray. Provide data as char.'));
97+
errorStruct.message = strcat('Trying to write', ...
98+
32, class(data), ' to a logical DataArray.');
99+
error(errorStruct);
100+
elseif(isnumeric(obj.read_all) && ~isnumeric(data))
101+
errorStruct.message = strcat('Trying to write', ...
102+
32, class(data), ' to a ', 32, class(obj.read_all), ...
103+
' DataArray.');
104+
error(errorStruct);
105+
elseif(ischar(data))
106+
%-- Should actually not be reachable at the moment,
107+
%-- since writing Strings to DataArrays is not supported,
108+
%-- but safety first.
109+
errorStruct.identifier = 'DataArray:unsupportedDataType';
110+
errorStruct.message = ('Writing char/string DataArrays is not supported as of yet.');
111+
error(errorStruct);
103112
else
104113
% data must agree with file & dimensions
105114
% see mkarray.cc(42)

+nix/DataType.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Bool;
66
String;
77
Double;
8+
%-- remove
89
Char;
910
Float;
1011
Int8;

src/nixdataarray.cc

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,9 @@ namespace nixdataarray {
5656
nix::NDSize offset(0);
5757
double *ptr = input.get_raw(2);
5858

59-
if (dtype == nix::DataType::String)
60-
{
61-
//da.setData(ptr);
62-
throw std::domain_error("Writing strings is not supported as of yet.");
63-
}
64-
else
65-
{
59+
if (dtype == nix::DataType::String) {
60+
throw std::domain_error("Writing Strings to DataArrays is not supported as of yet.");
61+
} else {
6662
da.setData(dtype, ptr, count, offset);
6763
}
6864
}

src/utils/mkarray.cc

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,9 @@ mxArray* make_mx_array_from_ds(const nix::DataSet &da) {
2222
}
2323

2424
mxArray *data;
25-
if (dtype.cid == mxCHAR_CLASS)
26-
{
27-
data = mxCreateCharArray(dims.size(), dims.data());
28-
// double *ptr = mxGetPr(data);
29-
// da.getData(ptr);
30-
}
31-
else
32-
{
25+
if (dtype.cid == mxCHAR_CLASS) {
26+
throw std::domain_error("String DataArrays are not supported as of yet.");
27+
} else {
3328
data = mxCreateNumericArray(dims.size(), dims.data(), dtype.cid, dtype.clx);
3429
double *ptr = mxGetPr(data);
3530
nix::NDSize offset(size.size(), 0);

tests/TestBlock.m

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,40 +53,79 @@
5353
assert(isempty(b.definition));
5454
end
5555

56-
function [] = test_create_data_array( varargin )
5756
%% Test: Create Data Array
58-
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
57+
function [] = test_create_data_array( varargin )
58+
fileName = fullfile(pwd, 'tests', 'testRW.h5');
59+
f = nix.File(fileName, nix.FileMode.Overwrite);
5960
b = f.createBlock('arraytest', 'nixblock');
6061

6162
assert(isempty(b.dataArrays));
6263

63-
d1 = b.create_data_array('foo', 'bar', nix.DataType.Double, [2 3]);
64-
65-
assert(strcmp(d1.name, 'foo'));
66-
assert(strcmp(d1.type, 'bar'));
67-
tmp = d1.read_all();
64+
dtype = 'nix.DataArray';
65+
doubleName = 'doubleDataArray';
66+
da = b.create_data_array(doubleName, dtype, nix.DataType.Double, [2 3]);
67+
assert(strcmp(da.name, doubleName));
68+
assert(strcmp(da.type, dtype));
69+
tmp = da.read_all();
6870
assert(all(tmp(:) == 0));
71+
72+
%-- TODO: add tests for all provided data types
73+
try
74+
stringName = 'stringDataArray';
75+
b.create_data_array(stringName, dtype, nix.DataType.String, [1 5]);
76+
catch ME
77+
assert(strcmp(ME.identifier, 'Block:unsupportedDataType'));
78+
end;
6979

80+
try
81+
unsupportedName = 'I will crash and burn';
82+
b.create_data_array(unsupportedName, dtype, 'Thou shalt not work!', [1 5]);
83+
catch ME
84+
assert(strcmp(ME.identifier, 'Block:unsupportedDataType'));
85+
end;
86+
7087
assert(~isempty(b.dataArrays));
7188
end
7289

73-
function [] = test_create_data_array_from_data( varargin )
7490
%% Test: Create Data Array from data
75-
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
91+
%-- TODO add tests for all supported datatypes
92+
function [] = test_create_data_array_from_data( varargin )
93+
fileName = fullfile(pwd, 'tests', 'testRW.h5');
94+
daType = 'nix.DataArray';
95+
f = nix.File(fileName, nix.FileMode.Overwrite);
7696
b = f.createBlock('arraytest', 'nixblock');
7797

7898
assert(isempty(b.dataArrays));
7999

80-
data = [1, 2, 3; 4, 5, 6];
81-
d1 = b.create_data_array_from_data('foo', 'bar', data);
82-
83-
assert(strcmp(d1.name, 'foo'));
84-
assert(strcmp(d1.type, 'bar'));
100+
numName = 'numDataArray';
101+
numData = [1, 2, 3; 4, 5, 6];
102+
da = b.create_data_array_from_data(numName, daType, numData);
103+
assert(strcmp(da.name, numName));
104+
assert(strcmp(da.type, daType));
105+
106+
tmp = da.read_all();
107+
assert(strcmp(class(tmp), class(numData)));
108+
assert(isequal(size(tmp), size(numData)));
109+
assert(isequal(tmp, numData));
110+
111+
logName = 'logicalDataArray';
112+
logData = logical([1 0 1; 0 1 0; 1 0 1]);
113+
da = b.create_data_array_from_data(logName, daType, logData);
114+
assert(islogical(da.read_all));
115+
assert(isequal(size(da.read_all), size(logData)));
116+
assert(isequal(da.read_all, logData));
117+
118+
try
119+
b.create_data_array_from_data('stringDataArray', daType, ['a' 'b']);
120+
catch ME
121+
assert(strcmp(ME.identifier, 'Block:unsupportedDataType'));
122+
end;
85123

86-
tmp = d1.read_all();
87-
assert(strcmp(class(tmp), class(data)));
88-
assert(isequal(size(tmp), size(data)));
89-
assert(isequal(tmp, data));
124+
try
125+
b.create_data_array_from_data('I will crash and burn', daType, {1 2 3});
126+
catch ME
127+
assert(strcmp(ME.identifier, 'Block:unsupportedDataType'));
128+
end;
90129

91130
assert(~isempty(b.dataArrays));
92131
end

tests/TestDataArray.m

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
funcs{end+1} = @test_set_metadata;
99
funcs{end+1} = @test_open_metadata;
1010
funcs{end+1} = @test_list_sources;
11-
funcs{end+1} = @test_set_data;
11+
funcs{end+1} = @test_write_data;
1212
funcs{end+1} = @test_add_source;
1313
funcs{end+1} = @test_remove_source;
1414
funcs{end+1} = @test_dimensions;
@@ -97,18 +97,60 @@
9797
assert(strcmp(d1.sources{1}.name, 'Unit 5'));
9898
end
9999

100-
%% Test: Set Data
101-
function [] = test_set_data( varargin )
102-
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
100+
%% Test: Write Data
101+
%-- TODO: add tests for all provided data types
102+
function [] = test_write_data( varargin )
103+
fileName = fullfile(pwd, 'tests', 'testRW.h5');
104+
typeDA = 'nix.DataArray';
105+
f = nix.File(fileName, nix.FileMode.Overwrite);
103106
b = f.createBlock('tagtest', 'nixblock');
104107

105-
d1 = b.create_data_array('foo', 'bar', nix.DataType.Double, [2 3]);
106-
tmp = d1.read_all();
107-
assert(all(tmp(:) == 0));
108+
numData = [1 2 3 4 5];
109+
logData = logical([1 0 1 0 1]);
110+
charData = ['a' 'b' 'c' 'd' 'e'];
111+
cellData = {1 2 3 4 5};
108112

109-
data = [1, 2, 3; 4, 5, 6];
110-
d1.write_all(data);
111-
assert(isequal(d1.read_all(), data));
113+
da = b.create_data_array('numericArray', typeDA, nix.DataType.Double, [1 5]);
114+
tmp = da.read_all();
115+
assert(all(tmp(:) == 0));
116+
117+
da.write_all(numData);
118+
assert(isequal(da.read_all(), numData));
119+
120+
try
121+
da.write_all(logData);
122+
catch ME
123+
assert(strcmp(ME.identifier, 'DataArray:improperDataType'));
124+
end;
125+
try
126+
da.write_all(charData);
127+
catch ME
128+
assert(strcmp(ME.identifier, 'DataArray:improperDataType'));
129+
end;
130+
try
131+
da.write_all(cellData);
132+
catch ME
133+
assert(strcmp(ME.identifier, 'DataArray:improperDataType'));
134+
end;
135+
136+
da = b.create_data_array('logicalArray', typeDA, nix.DataType.Bool, [1 5]);
137+
da.write_all(logData);
138+
assert(isequal(da.read_all, logData));
139+
try
140+
da.write_all(numData);
141+
catch ME
142+
assert(strcmp(ME.identifier, 'DataArray:improperDataType'));
143+
end;
144+
try
145+
da.write_all(charData);
146+
catch ME
147+
assert(strcmp(ME.identifier, 'DataArray:improperDataType'));
148+
end;
149+
150+
clear da b f;
151+
f = nix.File(fileName, nix.FileMode.ReadOnly);
152+
assert(isequal(f.blocks{1}.dataArrays{1}.read_all, numData));
153+
assert(isequal(f.blocks{1}.dataArrays{2}.read_all, logData));
112154
end
113155

114156
%% Test: Add sources by entity and id
@@ -180,4 +222,4 @@
180222

181223
da.delete_dimension(1);
182224
assert(isempty(da.dimensions));
183-
end
225+
end

0 commit comments

Comments
 (0)