Skip to content

Commit 0549027

Browse files
committed
Merge pull request #57 from asobolev/master
Create data array / write_all methods LGTM
2 parents 99b8f13 + 7e1454c commit 0549027

File tree

12 files changed

+131
-5
lines changed

12 files changed

+131
-5
lines changed

+nix/Block.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@
3737
end;
3838
end;
3939

40+
function da = create_data_array(obj, name, nixtype, dtype, shape)
41+
handle = nix_mx('Block::createDataArray', obj.nix_handle, ...
42+
name, nixtype, dtype, shape);
43+
da = nix.DataArray(handle);
44+
obj.dataArraysCache.lastUpdate = 0;
45+
end
46+
47+
function da = create_data_array_from_data(obj, name, nixtype, data)
48+
shape = size(data);
49+
dtype = class(data);
50+
51+
da = obj.create_data_array(name, nixtype, dtype, shape);
52+
da.write_all(data);
53+
end
54+
4055
% -----------------
4156
% Sources methods
4257
% -----------------

+nix/DataArray.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,24 @@
7171
polynom_coefficients = obj.info.polynom_coefficients;
7272
end;
7373

74+
% -----------------
75+
% Data access methods
76+
% -----------------
77+
7478
function data = read_all(obj)
7579
tmp = nix_mx('DataArray::readAll', obj.nix_handle);
7680
% data must agree with file & dimensions
7781
% see mkarray.cc(42)
7882
data = permute(tmp, length(size(tmp)):-1:1);
7983
end;
8084

85+
function write_all(obj, data) % TODO add (optional) offset
86+
% data must agree with file & dimensions
87+
% see mkarray.cc(42)
88+
tmp = permute(data, length(size(data)):-1:1);
89+
nix_mx('DataArray::writeAll', obj.nix_handle, tmp);
90+
end;
91+
8192
% -----------------
8293
% Sources methods
8394
% -----------------

nix_mx.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ void mexFunction(int nlhs,
142142

143143
methods = new registry{};
144144

145-
methods->add("Entity::destory", entity_destroy);
145+
methods->add("Entity::destroy", entity_destroy);
146146
methods->add("Entity::updatedAt", entity_updated_at);
147147

148148
classdef<nix::File>("File", methods)
@@ -157,7 +157,10 @@ void mexFunction(int nlhs,
157157

158158
classdef<nix::Block>("Block", methods)
159159
.reg("dataArrays", &nix::Block::dataArrays)
160+
.reg("createSource", &nix::Block::createSource)
161+
//.reg("createDataArray", static_cast<nix::DataArray(nix::Block::*)(const std::string &, const std::string &, nix::DataType, const nix::NDSize &)>(&nix::Block::createDataArray))
160162
.reg("createTag", &nix::Block::createTag)
163+
.reg("createMultiTag", &nix::Block::createMultiTag)
161164
.reg("sources", &nix::Block::sources)
162165
.reg("tags", &nix::Block::tags)
163166
.reg("multiTags", &nix::Block::multiTags)
@@ -168,10 +171,12 @@ void mexFunction(int nlhs,
168171
.reg("openTag", GETBYSTR(nix::Tag, nix::Block, getTag))
169172
.reg("openMultiTag", GETBYSTR(nix::MultiTag, nix::Block, getMultiTag))
170173
.reg("openMetadataSection", GETCONTENT(nix::Section, nix::Block, metadata));
174+
methods->add("Block::createDataArray", nixblock::create_data_array);
171175

172176
classdef<nix::DataArray>("DataArray", methods)
173177
.reg("sources", GETSOURCES(IDataArray))
174178
.reg("openMetadataSection", GETMETADATA(IDataArray));
179+
methods->add("DataArray::writeAll", nixdataarray::write_all);
175180

176181
classdef<nix::Source>("Source", methods)
177182
.reg("sources", &nix::Source::sources)

src/nixblock.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ namespace nixblock {
2525
output.set(0, sb.array());
2626
}
2727

28+
void create_data_array(const extractor &input, infusor &output)
29+
{
30+
nix::Block block = input.entity<nix::Block>(1);
31+
32+
std::string name = input.str(2);
33+
std::string type = input.str(3);
34+
nix::DataType dtype = nix::DataType::Double; // FIXME nix::string_to_data_type(input.str(3));
35+
nix::NDSize size = input.ndsize(5);
36+
37+
nix::DataArray dt = block.createDataArray(name, type, dtype, size);
38+
output.set(0, dt);
39+
}
40+
2841
void list_data_arrays(const extractor &input, infusor &output)
2942
{
3043
nix::Block block = input.entity<nix::Block>(1);

src/nixblock.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace nixblock {
77

88
void describe(const extractor &input, infusor &output);
99

10+
void create_data_array(const extractor &input, infusor &output);
11+
1012
void list_data_arrays(const extractor &input, infusor &output);
1113

1214
void list_sources(const extractor &input, infusor &output);

src/nixdataarray.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,16 @@ namespace nixdataarray {
6363
output.set(0, data);
6464
}
6565

66+
void write_all(const extractor &input, infusor &output)
67+
{
68+
nix::DataArray da = input.entity<nix::DataArray>(1);
69+
70+
nix::DataType dtype = input.dtype(2);
71+
nix::NDSize count = input.ndsize(2);
72+
nix::NDSize offset(0);
73+
double *ptr = input.get_raw(2);
74+
75+
da.setData(dtype, ptr, count, offset);
76+
}
77+
6678
} // namespace nixdataarray

src/nixdataarray.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace nixdataarray {
99

1010
void read_all(const extractor &input, infusor &output);
1111

12+
void write_all(const extractor &input, infusor &output);
13+
1214
} // namespace nixdataarray
1315

1416
#endif

src/utils/arguments.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "handle.h"
66
#include "datatypes.h"
77
#include "mkarray.h"
8-
8+
#include "nix2mx.h"
99
#include <stdexcept>
1010

1111
// *** argument helpers ***
@@ -98,6 +98,14 @@ class extractor : public argument_helper<const mxArray> {
9898
return res;
9999
}
100100

101+
nix::NDSize ndsize(size_t pos) const {
102+
return mx_array_to_ndsize(array[pos]);
103+
}
104+
105+
nix::DataType dtype(size_t pos) const {
106+
return dtype_mex2nix(array[pos]);
107+
}
108+
101109
bool logical(size_t pos) const {
102110
check_arg_type(pos, nix::DataType::Bool);
103111

@@ -124,6 +132,10 @@ class extractor : public argument_helper<const mxArray> {
124132
return category == mxCHAR_CLASS;
125133
}
126134

135+
double* get_raw(size_t pos) const {
136+
return mxGetPr(array[pos]);
137+
}
138+
127139
private:
128140
};
129141

src/utils/nix2mx.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ mxArray *dim_to_struct(nix::RangeDimension dim) {
5454
return sb.array();
5555
}
5656

57-
/*
5857
nix::NDSize mx_array_to_ndsize(const mxArray *arr) {
5958

6059
size_t m = mxGetM(arr);
@@ -72,4 +71,4 @@ nix::NDSize mx_array_to_ndsize(const mxArray *arr) {
7271

7372
return size;
7473
}
75-
*/
74+

src/utils/nix2mx.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "mex.h"
55
#include <nix.hpp>
66

7-
//nix::NDSize mx_array_to_ndsize(const mxArray *arr);
7+
nix::NDSize mx_array_to_ndsize(const mxArray *arr);
88

99
mxArray *nmCreateScalar(uint32_t val);
1010

0 commit comments

Comments
 (0)