Skip to content

Commit 0e9741b

Browse files
committed
[c++/m] Add Tag sourcesFilter
1 parent 75a3e03 commit 0e9741b

File tree

4 files changed

+91
-2
lines changed

4 files changed

+91
-2
lines changed

nix_mx.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ void mexFunction(int nlhs,
323323
methods->add("Tag::openFeatureIdx", nixtag::openFeatureIdx);
324324
methods->add("Tag::openSourceIdx", nixtag::openSourceIdx);
325325
methods->add("Tag::compare", nixtag::compare);
326+
methods->add("Tag::sourcesFiltered", nixtag::sourcesFiltered);
326327

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

src/nixtag.cc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
// LICENSE file in the root of the Project.
88

99
#include "nixtag.h"
10-
#include "mkarray.h"
1110

1211
#include "mex.h"
1312

1413
#include <nix.hpp>
1514

16-
#include "handle.h"
1715
#include "arguments.h"
16+
#include "filters.h"
17+
#include "handle.h"
18+
#include "mkarray.h"
1819
#include "struct.h"
1920

2021
namespace nixtag {
@@ -102,4 +103,13 @@ namespace nixtag {
102103
output.set(0, currObj.compare(other));
103104
}
104105

106+
void sourcesFiltered(const extractor &input, infusor &output) {
107+
nix::Tag currObj = input.entity<nix::Tag>(1);
108+
std::vector<nix::Source> res = filterEntity<nix::Source>(input,
109+
[currObj](const nix::util::Filter<nix::Source>::type &filter) {
110+
return currObj.sources(filter);
111+
});
112+
output.set(0, res);
113+
}
114+
105115
} // namespace nixtag

src/nixtag.h

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

3838
void compare(const extractor &input, infusor &output);
3939

40+
void sourcesFiltered(const extractor &input, infusor &output);
41+
4042
} // namespace nixtag
4143

4244
#endif

tests/TestTag.m

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
funcs{end+1} = @test_has_feature;
4141
funcs{end+1} = @test_has_reference;
4242
funcs{end+1} = @test_compare;
43+
funcs{end+1} = @test_filter_source;
4344
end
4445

4546
%% Test: Add sources by entity and id
@@ -652,3 +653,78 @@
652653
assert(t2.compare(t1) > 0);
653654
assert(t1.compare(t3) ~= 0);
654655
end
656+
657+
%% Test: filter sources
658+
function [] = test_filter_source( varargin )
659+
filterName = 'filterMe';
660+
filterType = 'filterType';
661+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
662+
b = f.create_block('testBlock', 'nixBlock');
663+
t = b.create_tag('testTag', 'nixTag', [1 2 3]);
664+
s = b.create_source(filterName, 'nixSource');
665+
t.add_source(s);
666+
filterID = s.id;
667+
s = b.create_source('testSource1', filterType);
668+
t.add_source(s);
669+
filterIDs = {filterID, s.id};
670+
s = b.create_source('testSource2', filterType);
671+
t.add_source(s);
672+
673+
% test empty id filter
674+
assert(isempty(f.blocks{1}.tags{1}.filter_sources(nix.Filter.id, 'IdoNotExist')));
675+
676+
% test nix.Filter.accept_all
677+
filtered = f.blocks{1}.tags{1}.filter_sources(nix.Filter.accept_all, '');
678+
assert(size(filtered, 1) == 3);
679+
680+
% test nix.Filter.id
681+
filtered = f.blocks{1}.tags{1}.filter_sources(nix.Filter.id, filterID);
682+
assert(size(filtered, 1) == 1);
683+
assert(strcmp(filtered{1}.id, filterID));
684+
685+
% test nix.Filter.ids
686+
filtered = f.blocks{1}.tags{1}.filter_sources(nix.Filter.ids, filterIDs);
687+
assert(size(filtered, 1) == 2);
688+
assert(strcmp(filtered{1}.id, filterIDs{1}) || strcmp(filtered{1}.id, filterIDs{2}));
689+
690+
% test nix.Filter.name
691+
filtered = f.blocks{1}.tags{1}.filter_sources(nix.Filter.name, filterName);
692+
assert(size(filtered, 1) == 1);
693+
assert(strcmp(filtered{1}.name, filterName));
694+
695+
% test nix.Filter.type
696+
filtered = f.blocks{1}.tags{1}.filter_sources(nix.Filter.type, filterType);
697+
assert(size(filtered, 1) == 2);
698+
699+
% test nix.Filter.metadata
700+
mainName = 'testSubSection';
701+
mainSource = b.create_source(mainName, 'nixSource');
702+
t.add_source(mainSource);
703+
subName = 'testSubSection1';
704+
s = f.create_section(subName, 'nixSection');
705+
mainSource.set_metadata(s);
706+
subID = s.id;
707+
708+
assert(isempty(f.blocks{1}.tags{1}.filter_sources(nix.Filter.metadata, 'Do not exist')));
709+
filtered = f.blocks{1}.tags{1}.filter_sources(nix.Filter.metadata, subID);
710+
assert(size(filtered, 1) == 1);
711+
assert(strcmp(filtered{1}.name, mainName));
712+
713+
% test nix.Filter.source
714+
mainName = 'testSubSource';
715+
main = b.create_source(mainName, 'nixSource');
716+
t.add_source(main);
717+
mainID = main.id;
718+
subName = 'testSubSource1';
719+
s = main.create_source(subName, 'nixSource');
720+
subID = s.id;
721+
722+
assert(isempty(f.blocks{1}.tags{1}.filter_sources(nix.Filter.source, 'Do not exist')));
723+
filtered = f.blocks{1}.tags{1}.filter_sources(nix.Filter.source, subName);
724+
assert(size(filtered, 1) == 1);
725+
assert(strcmp(filtered{1}.id, mainID));
726+
727+
filtered = f.blocks{1}.tags{1}.filter_sources(nix.Filter.source, subID);
728+
assert(size(filtered, 1) == 1);
729+
assert(strcmp(filtered{1}.name, mainName));
730+
end

0 commit comments

Comments
 (0)