Skip to content

Commit 4a20566

Browse files
committed
Merge pull request #67 from mpsonntag/createDelete
Various create / delete methods for Dataarray and Tag entities
2 parents 81acba2 + 4aae7a0 commit 4a20566

File tree

10 files changed

+180
-24
lines changed

10 files changed

+180
-24
lines changed

+nix/DataArray.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ function write_all(obj, data) % TODO add (optional) offset
9393
% Sources methods
9494
% -----------------
9595

96+
function [] = add_source(obj, add_this)
97+
obj.sourcesCache = nix.Utils.add_entity(obj, ...
98+
add_this, 'nix.Source', 'DataArray::addSource', obj.sourcesCache);
99+
end;
100+
101+
function delCheck = remove_source(obj, del)
102+
[delCheck, obj.sourcesCache] = nix.Utils.delete_entity(obj, ...
103+
del, 'nix.Source', 'DataArray::removeSource', obj.sourcesCache);
104+
end;
105+
96106
function sources = get.sources(obj)
97107
[obj.sourcesCache, sources] = nix.Utils.fetchObjList(obj.updatedAt, ...
98108
'DataArray::sources', obj.nix_handle, obj.sourcesCache, @nix.Source);

+nix/LinkType.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
classdef LinkType
2+
%LINKTYPE nix link types
3+
4+
properties (Constant)
5+
Tagged = uint8(0)
6+
Untagged = uint8(1)
7+
Indexed = uint8(2)
8+
end
9+
10+
end

+nix/Tag.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@
5555
% Features methods
5656
% ------------------
5757

58+
function retObj = add_feature(obj, add_this, link_type)
59+
if(strcmp(class(add_this), 'nix.DataArray'))
60+
addID = add_this.id;
61+
else
62+
addID = add_this;
63+
end;
64+
retObj = nix.Feature(nix_mx('Tag::createFeature', obj.nix_handle, addID, link_type));
65+
obj.featuresCache.lastUpdate = 0;
66+
end;
67+
68+
function delCheck = remove_feature(obj, del)
69+
[delCheck, obj.featuresCache] = nix.Utils.delete_entity(obj, ...
70+
del, 'nix.Feature', 'Tag::deleteFeature', obj.featuresCache);
71+
end;
72+
5873
function retObj = open_feature(obj, id_or_name)
5974
retObj = nix.Utils.open_entity(obj, ...
6075
'Tag::openFeature', id_or_name, @nix.Feature);

nix_mx.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ void mexFunction(int nlhs,
128128
.reg("openMetadataSection", GETMETADATA(IDataArray));
129129
methods->add("DataArray::readAll", nixdataarray::read_all);
130130
methods->add("DataArray::writeAll", nixdataarray::write_all);
131+
methods->add("DataArray::addSource", nixdataarray::add_source);
132+
// REMOVER for DataArray.removeSource leads to an error, therefore use method->add for now
133+
methods->add("DataArray::removeSource", nixdataarray::remove_source);
131134

132135
classdef<nix::Source>("Source", methods)
133136
.desc(&nixsource::describe)
@@ -147,11 +150,13 @@ void mexFunction(int nlhs,
147150
.reg("openSource", GETBYSTR(nix::Source, nix::Tag, getSource))
148151
.reg("openMetadataSection", GETCONTENT(nix::Section, nix::Tag, metadata))
149152
.reg("removeReference", REMOVER(nix::DataArray, nix::Tag, removeReference))
150-
.reg("removeSource", REMOVER(nix::Source, nix::Tag, removeSource));
153+
.reg("removeSource", REMOVER(nix::Source, nix::Tag, removeSource))
154+
.reg("deleteFeature", REMOVER(nix::Feature, nix::Tag, deleteFeature));
151155
methods->add("Tag::retrieveData", nixtag::retrieve_data);
152156
methods->add("Tag::featureRetrieveData", nixtag::retrieve_feature_data);
153157
methods->add("Tag::addReference", nixtag::add_reference);
154158
methods->add("Tag::addSource", nixtag::add_source);
159+
methods->add("Tag::createFeature", nixtag::create_feature);
155160

156161
classdef<nix::MultiTag>("MultiTag", methods)
157162
.desc(&nixmultitag::describe)

src/nixdataarray.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ namespace nixdataarray {
5454
return sb.array();
5555
}
5656

57+
void add_source(const extractor &input, infusor &output)
58+
{
59+
nix::DataArray currObj = input.entity<nix::DataArray>(1);
60+
currObj.addSource(input.str(2));
61+
}
62+
63+
void remove_source(const extractor &input, infusor &output)
64+
{
65+
nix::DataArray currObj = input.entity<nix::DataArray>(1);
66+
output.set(0, currObj.removeSource(input.str(2)));
67+
}
68+
5769
void read_all(const extractor &input, infusor &output)
5870
{
5971
nix::DataArray da = input.entity<nix::DataArray>(1);

src/nixdataarray.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ namespace nixdataarray {
77

88
mxArray *describe(const nix::DataArray &da);
99

10+
void add_source(const extractor &input, infusor &output);
11+
12+
void remove_source(const extractor &input, infusor &output);
13+
1014
void read_all(const extractor &input, infusor &output);
1115

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

src/nixtag.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,24 @@ namespace nixtag {
3838
currObj.addSource(input.str(2));
3939
}
4040

41+
void create_feature(const extractor &input, infusor &output)
42+
{
43+
nix::Tag currObj = input.entity<nix::Tag>(1);
44+
45+
uint8_t ltype = input.num<uint8_t>(3);
46+
nix::LinkType link_type;
47+
48+
switch (ltype) {
49+
case 0: link_type = nix::LinkType::Tagged; break;
50+
case 1: link_type = nix::LinkType::Untagged; break;
51+
case 2: link_type = nix::LinkType::Indexed; break;
52+
default: throw std::invalid_argument("unkown link type");
53+
}
54+
55+
nix::Feature newFeat = currObj.createFeature(input.str(2), link_type);
56+
output.set(0, handle(newFeat));
57+
}
58+
4159
void retrieve_data(const extractor &input, infusor &output) {
4260
nix::Tag currObj = input.entity<nix::Tag>(1);
4361
double index = input.num<double>(2);

src/nixtag.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace nixtag {
1111

1212
void add_source(const extractor &input, infusor &output);
1313

14+
void create_feature(const extractor &input, infusor &output);
15+
1416
void retrieve_data(const extractor &input, infusor &output);
1517

1618
void retrieve_feature_data(const extractor &input, infusor &output);

tests/TestDataArray.m

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
funcs{end+1} = @test_open_metadata;
88
funcs{end+1} = @test_list_sources;
99
funcs{end+1} = @test_set_data;
10+
funcs{end+1} = @test_add_source;
11+
funcs{end+1} = @test_remove_source;
1012
end
1113

1214
%% Test: Read all data from DataArray
@@ -56,3 +58,37 @@
5658
assert(isequal(d1.read_all(), data));
5759
end
5860

61+
%% Test: Add sources by entity and id
62+
function [] = test_add_source ( varargin )
63+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
64+
b = f.createBlock('sourceTest', 'nixBlock');
65+
getSource = b.create_source('sourceTest', 'nixSource');
66+
tmp = getSource.create_source('nestedSource1', 'nixSource');
67+
tmp = getSource.create_source('nestedSource2', 'nixSource');
68+
getDataArray = b.create_data_array('sourceTestDataArray', 'nixDataArray', 'double', [1 2]);
69+
70+
assert(isempty(getDataArray.sources));
71+
getDataArray.add_source(getSource.sources{1}.id);
72+
getDataArray.add_source(getSource.sources{2});
73+
assert(size(getDataArray.sources, 1) == 2);
74+
end
75+
76+
%% Test: Remove sources by entity and id
77+
function [] = test_remove_source ( varargin )
78+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
79+
b = f.createBlock('sourceTest', 'nixBlock');
80+
getSource = b.create_source('sourceTest', 'nixSource');
81+
tmp = getSource.create_source('nestedSource1', 'nixSource');
82+
tmp = getSource.create_source('nestedSource2', 'nixSource');
83+
getDataArray = b.create_data_array('sourceTestDataArray', 'nixDataArray', 'double', [1 2]);
84+
85+
getDataArray.add_source(getSource.sources{1}.id);
86+
getDataArray.add_source(getSource.sources{2});
87+
88+
assert(getDataArray.remove_source(getSource.sources{2}));
89+
assert(getDataArray.remove_source(getSource.sources{1}.id));
90+
assert(isempty(getDataArray.sources));
91+
92+
assert(getDataArray.remove_source('I do not exist'));
93+
assert(size(getSource.sources, 1) == 2);
94+
end

tests/TestTag.m

Lines changed: 67 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
funcs{end+1} = @test_remove_source;
88
funcs{end+1} = @test_add_reference;
99
funcs{end+1} = @test_remove_reference;
10+
funcs{end+1} = @test_add_feature;
11+
funcs{end+1} = @test_remove_feature;
1012
funcs{end+1} = @test_fetch_references;
1113
funcs{end+1} = @test_fetch_sources;
1214
funcs{end+1} = @test_fetch_features;
@@ -92,6 +94,48 @@
9294
assert(size(getBlock.dataArrays, 1) == 2);
9395
end
9496

97+
%% Test: Add features by entity and id
98+
function [] = test_add_feature ( varargin )
99+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
100+
b = f.createBlock('featureTest', 'nixBlock');
101+
tmp = b.create_data_array('featureTestDataArray1', 'nixDataArray', 'double', [1 2]);
102+
tmp = b.create_data_array('featureTestDataArray2', 'nixDataArray', 'double', [3 4]);
103+
tmp = b.create_data_array('featureTestDataArray3', 'nixDataArray', 'double', [5 6]);
104+
tmp = b.create_data_array('featureTestDataArray4', 'nixDataArray', 'double', [7 8]);
105+
tmp = b.create_data_array('featureTestDataArray5', 'nixDataArray', 'double', [9 10]);
106+
tmp = b.create_data_array('featureTestDataArray6', 'nixDataArray', 'double', [11 12]);
107+
position = [1.0 1.2 1.3 15.9];
108+
getTag = b.create_tag('featureTest', 'nixTag', position);
109+
110+
assert(isempty(getTag.features));
111+
tmp = getTag.add_feature(b.dataArrays{1}.id, nix.LinkType.Tagged);
112+
tmp = getTag.add_feature(b.dataArrays{2}, nix.LinkType.Tagged);
113+
tmp = getTag.add_feature(b.dataArrays{3}.id, nix.LinkType.Untagged);
114+
tmp = getTag.add_feature(b.dataArrays{4}, nix.LinkType.Untagged);
115+
tmp = getTag.add_feature(b.dataArrays{5}.id, nix.LinkType.Indexed);
116+
tmp = getTag.add_feature(b.dataArrays{6}, nix.LinkType.Indexed);
117+
assert(size(getTag.features, 1) == 6)
118+
end
119+
120+
%% Test: Remove features by entity and id
121+
function [] = test_remove_feature ( varargin )
122+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
123+
b = f.createBlock('featureTest', 'nixBlock');
124+
tmp = b.create_data_array('featureTestDataArray1', 'nixDataArray', 'double', [1 2]);
125+
tmp = b.create_data_array('featureTestDataArray2', 'nixDataArray', 'double', [3 4]);
126+
position = [1.0 1.2 1.3 15.9];
127+
getTag = b.create_tag('featureTest', 'nixTag', position);
128+
tmp = getTag.add_feature(b.dataArrays{1}.id, nix.LinkType.Tagged);
129+
tmp = getTag.add_feature(b.dataArrays{2}, nix.LinkType.Tagged);
130+
131+
assert(getTag.remove_feature(getTag.features{2}.id));
132+
assert(getTag.remove_feature(getTag.features{1}));
133+
assert(isempty(getTag.features));
134+
135+
assert(~getTag.remove_feature('I do not exist'));
136+
assert(size(b.dataArrays, 1) == 2);
137+
end
138+
95139
%% Test: fetch references
96140
function [] = test_fetch_references( varargin )
97141
test_file = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
@@ -128,13 +172,17 @@
128172

129173
%% Test: fetch features
130174
function [] = test_fetch_features( varargin )
131-
test_file = nix.File(fullfile(pwd, 'tests', 'test.h5'), nix.FileMode.ReadOnly);
132-
getBlock = test_file.openBlock(test_file.blocks{1,1}.name);
133-
getTag = getBlock.open_tag(getBlock.tags{1,1}.id);
175+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
176+
b = f.createBlock('featureTest', 'nixBlock');
177+
tmp = b.create_data_array('featureTestDataArray1', 'nixDataArray', 'double', [1 2]);
178+
tmp = b.create_data_array('featureTestDataArray2', 'nixDataArray', 'double', [3 4]);
179+
position = [1.0 1.2 1.3 15.9];
180+
getTag = b.create_tag('featureTest', 'nixTag', position);
181+
182+
tmp = getTag.add_feature(b.dataArrays{1}, nix.LinkType.Tagged);
183+
tmp = getTag.add_feature(b.dataArrays{2}, nix.LinkType.Tagged);
134184

135-
%-- ToDo get testfile with tag referencing a source
136-
assert(size(getTag.features(),1) == 0);
137-
disp('Test Tag: fetch features ... TODO (proper testfile)');
185+
assert(size(getTag.features, 1) == 2);
138186
end
139187

140188
%% Test: Open source by ID or name
@@ -155,28 +203,24 @@
155203
assert(~isempty(getSourceByName));
156204

157205
%-- test open non existing source
158-
getNonSource = getTag.open_source('I dont exist');
206+
getNonSource = getTag.open_source('I do not exist');
159207
assert(isempty(getNonSource));
160208
end
161209

162210

163-
%% Test: Open feature by ID or name
211+
%% Test: Open feature by ID
164212
function [] = test_open_feature( varargin )
165-
test_file = nix.File(fullfile(pwd, 'tests', 'test.h5'), nix.FileMode.ReadOnly);
166-
getBlock = test_file.openBlock(test_file.blocks{1,1}.name);
167-
getTag = getBlock.open_tag(getBlock.tags{1,1}.id);
168-
169-
%-- TODO: implement testfile with feature referenced from a tag
170-
%getFeatByID = getTag.open_feature(getTag.features{1,1}.id);
171-
%assert(strcmp(getFeatByID.id, ''));
172-
disp('Test Tag: open feature by ID ... TODO (proper testfile)');
173-
174-
%getFeatByName = getTag.open_feature(getTag.features{1,1}.name);
175-
%assert(strcmp(getFeatByName.id, ''));
176-
disp('Test Tag: open feature by name ... TODO (proper testfile)');
177-
213+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
214+
b = f.createBlock('featureTest', 'nixBlock');
215+
tmp = b.create_data_array('featureTestDataArray', 'nixDataArray', 'double', [1 2]);
216+
position = [1.0 1.2 1.3 15.9];
217+
getTag = b.create_tag('featureTest', 'nixTag', position);
218+
tmp = getTag.add_feature(b.dataArrays{1}, nix.LinkType.Tagged);
219+
220+
assert(~isempty(getTag.open_feature(getTag.features{1}.id)));
221+
178222
%-- test open non existing feature
179-
getFeat = getTag.open_feature('I dont exist');
223+
getFeat = getTag.open_feature('I do not exist');
180224
assert(isempty(getFeat));
181225
end
182226

@@ -197,7 +241,7 @@
197241
assert(~isempty(getRefByName));
198242

199243
%-- test open non existing source
200-
getNonRef = getTag.open_reference('I dont exist');
244+
getNonRef = getTag.open_reference('I do not exist');
201245
assert(isempty(getNonRef));
202246
end
203247

0 commit comments

Comments
 (0)