Skip to content

Commit 41a28dc

Browse files
committed
Merge pull request #107 from mpsonntag/fixDataTypeErrors
Fix data type errors
2 parents b2e40be + f60d710 commit 41a28dc

File tree

14 files changed

+462
-203
lines changed

14 files changed

+462
-203
lines changed

+nix/Block.m

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,36 @@
5252
'Block::openDataArray', id_or_name, @nix.DataArray);
5353
end;
5454

55-
%-- As "datatype" provide one of the nix.DataTypes. Alternatively
56-
%-- a string stating one of the datatypes supported by nix can be provided.
5755
function da = create_data_array(obj, name, nixtype, datatype, shape)
58-
handle = nix_mx('Block::createDataArray', obj.nix_handle, ...
59-
name, nixtype, datatype, shape);
60-
da = nix.DataArray(handle);
56+
errorStruct.identifier = 'Block:unsupportedDataType';
57+
if(~isa(datatype, 'nix.DataType'))
58+
errorStruct.message = 'Please provide a valid nix.DataType';
59+
error(errorStruct);
60+
elseif(isequal(datatype, nix.DataType.String))
61+
errorStruct.message = 'Writing Strings to DataArrays is not supported as of yet.';
62+
error(errorStruct);
63+
else
64+
handle = nix_mx('Block::createDataArray', obj.nix_handle, ...
65+
name, nixtype, lower(datatype.char), shape);
66+
da = nix.DataArray(handle);
67+
end;
6168
end
6269

6370
function da = create_data_array_from_data(obj, name, nixtype, data)
64-
shape = size(data);
65-
dtype = class(data);
66-
71+
errorStruct.identifier = 'Block:unsupportedDataType';
72+
if(ischar(data))
73+
errorStruct.message = 'Writing Strings to DataArrays is not supported as of yet.';
74+
error(errorStruct);
75+
elseif(islogical(data))
76+
dtype = nix.DataType.Bool;
77+
elseif(isnumeric(data))
78+
dtype = nix.DataType.Double;
79+
else
80+
errorStruct.message = 'DataType of provided data is not supported.';
81+
error(errorStruct);
82+
end;
83+
84+
shape = size(data);
6785
da = obj.create_data_array(name, nixtype, dtype, shape);
6886
da.write_all(data);
6987
end
@@ -137,7 +155,7 @@
137155
'Block::openMultiTag', id_or_name, @nix.MultiTag);
138156
end;
139157

140-
%-- creating a multitag requires an already existing data array
158+
%-- Creating a multitag requires an already existing data array
141159
function multitag = create_multi_tag(obj, name, type, add_data_array)
142160
if(strcmp(class(add_data_array), 'nix.DataArray'))
143161
addID = add_data_array.id;
@@ -153,6 +171,6 @@
153171
delCheck = nix.Utils.delete_entity(obj, ...
154172
del, 'nix.MultiTag', 'Block::deleteMultiTag');
155173
end;
156-
end;
157174

175+
end;
158176
end

+nix/DataArray.m

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,38 @@
7676
data = permute(tmp, length(size(tmp)):-1:1);
7777
end;
7878

79-
function write_all(obj, data) % TODO add (optional) offset
80-
% data must agree with file & dimensions
81-
% see mkarray.cc(42)
82-
tmp = permute(data, length(size(data)):-1:1);
83-
nix_mx('DataArray::writeAll', obj.nix_handle, tmp);
79+
%-- TODO add (optional) offset
80+
%-- If a DataArray has been created as boolean or numeric,
81+
%-- provide that only values of the proper DataType can be written.
82+
function write_all(obj, data)
83+
if(isinteger(obj.read_all) && isfloat(data))
84+
disp('Warning: Writing Float data to an Integer DataArray');
85+
end;
86+
87+
errorStruct.identifier = 'DataArray:improperDataType';
88+
if(islogical(obj.read_all) && ~islogical(data))
89+
errorStruct.message = strcat('Trying to write', ...
90+
32, class(data), ' to a logical DataArray.');
91+
error(errorStruct);
92+
elseif(isnumeric(obj.read_all) && ~isnumeric(data))
93+
errorStruct.message = strcat('Trying to write', ...
94+
32, class(data), ' to a ', 32, class(obj.read_all), ...
95+
' DataArray.');
96+
error(errorStruct);
97+
elseif(ischar(data))
98+
%-- Should actually not be reachable at the moment,
99+
%-- since writing Strings to DataArrays is not supported,
100+
%-- but safety first.
101+
errorStruct.identifier = 'DataArray:unsupportedDataType';
102+
errorStruct.message = ('Writing char/string DataArrays is not supported as of yet.');
103+
error(errorStruct);
104+
else
105+
% data must agree with file & dimensions
106+
% see mkarray.cc(42)
107+
tmp = permute(data, length(size(data)):-1:1);
108+
nix_mx('DataArray::writeAll', obj.nix_handle, tmp);
109+
end;
84110
end;
85-
111+
86112
end;
87113
end

+nix/DataType.m

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
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+
Float;
9+
Int8;
10+
Int16;
11+
Int32;
12+
Int64;
13+
UInt8;
14+
UInt16;
15+
UInt32;
16+
UInt64;
1817
end
19-
18+
2019
end
20+

+nix/Section.m

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,13 @@
8484
% Property methods
8585
% ----------------
8686

87-
%-- As "datatype" provide one of the nix.DataTypes. Alternatively
88-
%-- a string stating one of the datatypes supported by nix can be provided.
8987
function p = create_property(obj, name, datatype)
90-
p = nix.Property(nix_mx('Section::createProperty', ...
91-
obj.nix_handle, name, datatype));
88+
if(~isa(datatype, 'nix.DataType'))
89+
error('Please provide a valid nix.DataType');
90+
else
91+
p = nix.Property(nix_mx('Section::createProperty', ...
92+
obj.nix_handle, name, lower(datatype.char)));
93+
end;
9294
end;
9395

9496
function p = create_property_with_value(obj, name, val)
@@ -128,5 +130,5 @@
128130
end
129131
end;
130132

131-
end
133+
end;
132134
end

src/nixdataarray.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ namespace nixdataarray {
4242

4343
void read_all(const extractor &input, infusor &output)
4444
{
45-
//nix::DataArray da = input.entity<nix::DataArray>(1);
46-
4745
nix::DataArray da = input.entity<nix::DataArray>(1);
4846
mxArray *data = make_mx_array_from_ds(da);
4947
output.set(0, data);
@@ -52,13 +50,17 @@ namespace nixdataarray {
5250
void write_all(const extractor &input, infusor &output)
5351
{
5452
nix::DataArray da = input.entity<nix::DataArray>(1);
55-
5653
nix::DataType dtype = input.dtype(2);
54+
5755
nix::NDSize count = input.ndsize(2);
5856
nix::NDSize offset(0);
5957
double *ptr = input.get_raw(2);
60-
61-
da.setData(dtype, ptr, count, offset);
58+
59+
if (dtype == nix::DataType::String) {
60+
throw std::domain_error("Writing Strings to DataArrays is not supported as of yet.");
61+
} else {
62+
da.setData(dtype, ptr, count, offset);
63+
}
6264
}
6365

6466
void delete_dimension(const extractor &input, infusor &output) {
@@ -70,4 +72,4 @@ namespace nixdataarray {
7072
output.set(0, res);
7173
}
7274

73-
} // namespace nixdataarray
75+
} // namespace nixdataarray

src/utils/mkarray.cc

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,22 @@ mxArray* make_mx_array_from_ds(const nix::DataSet &da) {
1414
for (size_t i = 0; i < len; i++) {
1515
dims[len - (i + 1)] = static_cast<mwSize>(size[i]);
1616
}
17-
1817
nix::DataType da_type = da.dataType();
1918
DType2 dtype = dtype_nix2mex(da_type);
2019

2120
if (!dtype.is_valid) {
2221
throw std::domain_error("Unsupported data type");
2322
}
2423

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

3134
return data;
3235
}
@@ -94,4 +97,3 @@ mxArray* make_mx_array(const nix::Value &v)
9497
return res;
9598

9699
}
97-

0 commit comments

Comments
 (0)