Skip to content

Commit 58f60a1

Browse files
authored
Merge pull request #111 from mpsonntag/dims
Various bug fixes LGTM
2 parents d309498 + e948800 commit 58f60a1

File tree

8 files changed

+58
-38
lines changed

8 files changed

+58
-38
lines changed

+nix/Block.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@
5353
end;
5454

5555
function da = create_data_array(obj, name, nixtype, datatype, shape)
56+
%-- Quick fix to enable alias range dimension with
57+
%-- 1D data arrays created with this function.
58+
%-- e.g. size([1 2 3]) returns shape [1 3], which would not
59+
%-- be accepted when trying to add an alias range dimension.
60+
if(shape(1) == 1)
61+
shape(2:size(shape,2));
62+
end;
63+
5664
errorStruct.identifier = 'Block:unsupportedDataType';
5765
if(~isa(datatype, 'nix.DataType'))
5866
errorStruct.message = 'Please provide a valid nix.DataType';

+nix/DataArray.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@
6565
dim = nix.RangeDimension(nix_mx(func_name, obj.nix_handle));
6666
end
6767

68-
function delCheck = delete_dimension(obj, index)
69-
func_name = strcat(obj.alias, '::delete_dimension');
70-
delCheck = nix_mx(func_name, obj.nix_handle, index);
68+
function delCheck = delete_dimensions(obj)
69+
func_name = strcat(obj.alias, '::delete_dimensions');
70+
delCheck = nix_mx(func_name, obj.nix_handle);
7171
end;
72-
72+
7373
% -----------------
7474
% Data access methods
7575
% -----------------

nix_mx.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ void mexFunction(int nlhs,
184184
.reg("create_range_dimension", &nix::DataArray::createRangeDimension)
185185
.reg("create_alias_range_dimension", &nix::DataArray::createAliasRangeDimension)
186186
.reg("create_sampled_dimension", &nix::DataArray::createSampledDimension);
187-
methods->add("DataArray::delete_dimension", nixdataarray::delete_dimension);
187+
methods->add("DataArray::delete_dimensions", nixdataarray::delete_dimensions);
188188
methods->add("DataArray::readAll", nixdataarray::read_all);
189189
methods->add("DataArray::writeAll", nixdataarray::write_all);
190190
methods->add("DataArray::addSource", nixdataarray::add_source);

src/nixdataarray.cc

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,26 @@ namespace nixdataarray {
5252
nix::DataArray da = input.entity<nix::DataArray>(1);
5353
nix::DataType dtype = input.dtype(2);
5454

55-
nix::NDSize count = input.ndsize(2);
56-
nix::NDSize offset(0);
55+
const mxArray *arr = input.get_mx_array(2);
56+
57+
mwSize ndims = mxGetNumberOfDimensions(arr);
58+
const mwSize *dims = mxGetDimensions(arr);
59+
60+
mwSize tmp = ndims;
61+
62+
// Quick fix for writing data arrays that have exactly one row.
63+
// This fix does not resolve more complex arrays that have a
64+
// last dimension of exactly one row.
65+
if (dims[ndims-1] == 1) {
66+
ndims--;
67+
}
68+
nix::NDSize count(ndims);
69+
70+
for (mwSize i = 0; i < ndims; i++) {
71+
count[(ndims - i) - 1] = static_cast<nix::ndsize_t>(dims[i]);
72+
}
73+
74+
nix::NDSize offset(count.size(), 0);
5775
double *ptr = input.get_raw(2);
5876

5977
if (dtype == nix::DataType::String) {
@@ -63,11 +81,10 @@ namespace nixdataarray {
6381
}
6482
}
6583

66-
void delete_dimension(const extractor &input, infusor &output) {
84+
void delete_dimensions(const extractor &input, infusor &output) {
6785
nix::DataArray da = input.entity<nix::DataArray>(1);
6886

69-
const size_t idx = static_cast<size_t>(input.num<double>(2));
70-
bool res = da.deleteDimension(idx);
87+
bool res = da.deleteDimensions();
7188

7289
output.set(0, res);
7390
}

src/nixdataarray.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ namespace nixdataarray {
1515

1616
void write_all(const extractor &input, infusor &output);
1717

18-
void delete_dimension(const extractor &input, infusor &output);
18+
void delete_dimensions(const extractor &input, infusor &output);
1919

2020
} // namespace nixdataarray
2121

22-
#endif
22+
#endif

src/utils/arguments.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ class argument_helper {
1818

1919
bool check_size(size_t pos, bool fatal = false) const {
2020
bool res = pos + 1 > number;
21-
if (!res && fatal) {
22-
throw std::out_of_range("argument position is out of bounds");
23-
}
21+
if (!res && fatal) {
22+
throw std::out_of_range("argument position is out of bounds");
23+
}
2424
return res;
2525
}
2626

@@ -49,7 +49,7 @@ class argument_helper {
4949
size_t number;
5050
};
5151

52-
class extractor : public argument_helper<const mxArray> {
52+
class extractor : public argument_helper < const mxArray > {
5353
public:
5454
extractor(const mxArray **arr, int n) : argument_helper(arr, n) { }
5555

@@ -76,7 +76,7 @@ class extractor : public argument_helper<const mxArray> {
7676
nix::NDSize ndsize(size_t pos) const {
7777
return mx_to_ndsize(array[pos]);
7878
}
79-
79+
8080
nix::DataType dtype(size_t pos) const {
8181
return dtype_mex2nix(array[pos]);
8282
}
@@ -97,7 +97,7 @@ class extractor : public argument_helper<const mxArray> {
9797
}
9898

9999
handle hdl(size_t pos) const {
100-
handle h = handle(num<uint64_t>(pos));
100+
handle h = handle(num<uint64_t>(pos));
101101
return h;
102102
}
103103

@@ -114,24 +114,28 @@ class extractor : public argument_helper<const mxArray> {
114114
return mxGetPr(array[pos]);
115115
}
116116

117+
const mxArray *get_mx_array(size_t pos) const {
118+
return array[pos];
119+
}
120+
117121
private:
118122
};
119123

120124
template<>
121125
inline std::vector<std::string> extractor::vec(size_t pos) const {
122-
return mx_to_strings(array[pos]);
126+
return mx_to_strings(array[pos]);
123127
}
124128

125129

126130
class infusor : public argument_helper<mxArray> {
127131
public:
128132
infusor(mxArray **arr, int n) : argument_helper(arr, n) { }
129133

130-
template<typename T>
131-
void set(size_t pos, T &&value) {
132-
mxArray *array = make_mx_array(std::forward<T>(value));
133-
set(pos, array);
134-
}
134+
template<typename T>
135+
void set(size_t pos, T &&value) {
136+
mxArray *array = make_mx_array(std::forward<T>(value));
137+
set(pos, array);
138+
}
135139

136140
void set(size_t pos, mxArray *arr) {
137141
array[pos] = arr;

tests/TestDataArray.m

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
charData = ['a' 'b' 'c' 'd' 'e'];
150150
cellData = {1 2 3 4 5};
151151

152-
da = b.create_data_array('numericArray', typeDA, nix.DataType.Double, [1 5]);
152+
da = b.create_data_array('numericArray', typeDA, nix.DataType.Double, 5);
153153
da.write_all(numData);
154154
assert(isequal(da.read_all(), numData));
155155

@@ -185,7 +185,7 @@
185185
numData = [1 2 3 4 5];
186186
charData = ['a' 'b' 'c' 'd' 'e'];
187187

188-
da = b.create_data_array('logicalArray', typeDA, nix.DataType.Bool, [1 5]);
188+
da = b.create_data_array('logicalArray', typeDA, nix.DataType.Bool, 5);
189189
da.write_all(logData);
190190
assert(isequal(da.read_all, logData));
191191
try
@@ -213,7 +213,7 @@
213213

214214
numData = [1.3 2.4143 3.9878 4.1239 5];
215215

216-
da = b.create_data_array('floatArray', typeDA, nix.DataType.Float, [1 5]);
216+
da = b.create_data_array('floatArray', typeDA, nix.DataType.Float, 5);
217217
da.write_all(numData);
218218
assert(isequal(da.read_all, single(numData)));
219219

@@ -332,16 +332,7 @@
332332
assert(isequal(f.blocks{1}.dataArrays{1}.dimensions{3}.ticks, ticks));
333333
assert(~da.dimensions{3}.isAlias);
334334

335-
da.delete_dimension(2);
336-
assert(length(da.dimensions) == 2);
337-
assert(strcmp(da.dimensions{1}.dimensionType, 'set'));
338-
assert(strcmp(da.dimensions{2}.dimensionType, 'range'));
339-
340-
da.delete_dimension(1);
341-
assert(length(da.dimensions) == 1);
342-
assert(strcmp(da.dimensions{1}.dimensionType, 'range'));
343-
344-
da.delete_dimension(1);
335+
da.delete_dimensions();
345336
assert(isempty(da.dimensions));
346337

347338
try

tests/TestTag.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@
319319
function [] = test_retrieve_data( varargin )
320320
f = nix.File(fullfile(pwd, 'tests', 'test.h5'), nix.FileMode.ReadOnly);
321321
b = f.blocks{1};
322-
tag = b.tags{1};
322+
tag = b.open_tag('Arm movement epoch Trial 001');
323323

324324
data = tag.retrieve_data(1);
325325
assert(~isempty(data));

0 commit comments

Comments
 (0)