-
Notifications
You must be signed in to change notification settings - Fork 26
/
PyTag.cpp
147 lines (114 loc) · 4.58 KB
/
PyTag.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) 2014, German Neuroinformatics Node (G-Node)
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted under the terms of the BSD License. See
// LICENSE file in the root of the Project.
#include <boost/python.hpp>
#include <boost/optional.hpp>
#include <nix.hpp>
#include <accessors.hpp>
#include <transmorgify.hpp>
#include <PyEntity.hpp>
using namespace nix;
using namespace boost::python;
namespace nixpy {
// Units
void setUnits(Tag& st, const std::vector<std::string>& units) {
if (!units.empty())
st.units(units);
else
st.units(boost::none);
}
void setExtent(Tag& st, const std::vector<double>& iextent) {
if (!iextent.empty())
st.extent(iextent);
else
st.extent(boost::none);
}
// getter for Reference
boost::optional<DataArray> getReferenceById(const Tag& st, const std::string& id) {
DataArray da = st.getReference(id);
return da ? boost::optional<DataArray>(da) : boost::none;
}
boost::optional<DataArray> getReferenceByPos(const Tag& st, size_t index) {
DataArray da = st.getReference(index);
return da ? boost::optional<DataArray>(da) : boost::none;
}
// operations for Feature
Feature createNewFeature(Tag& st, const DataArray &data, const std::string& link_type) {
LinkType lt;
if (link_type == "Tagged")
lt = LinkType::Tagged;
else if (link_type == "Untagged")
lt = LinkType::Untagged;
else if (link_type == "Indexed")
lt = LinkType::Indexed;
else
throw std::invalid_argument("Tag::createNewFeature: invalid link type (Possible types: Tagged, Untagged or Indexed)!");
return st.createFeature(data, lt);
}
boost::optional<Feature> getFeatureById(const Tag& st, const std::string& id) {
Feature f = st.getFeature(id);
return f ? boost::optional<Feature>(f) : boost::none;
}
boost::optional<Feature> getFeatureByPos(const Tag& st, size_t index) {
Feature f = st.getFeature(index);
return f ? boost::optional<Feature>(f) : boost::none;
}
DataView retrieveDataIdx(const Tag& st, size_t reference_index) {
return st.retrieveData(reference_index);
}
DataView retrieveDataStr(const Tag& st, const std::string &name_or_id) {
return st.retrieveData(name_or_id);
}
DataView retrieveFeatureDataIdx(const Tag& st, size_t feature_index) {
return st.retrieveFeatureData(feature_index);
}
DataView retrieveFeatureDataStr(const Tag& st, const std::string &name_or_id) {
return st.retrieveFeatureData(name_or_id);
}
void PyTag::do_export() {
PyEntityWithSources<base::ITag>::do_export("Tag");
class_<Tag, bases<base::EntityWithSources<base::ITag>>>("Tag")
// TODO make tuples for simple vectors like units, position, extent
.add_property("units",
GETTER(std::vector<std::string>, Tag, units),
setUnits,
doc::tag_units)
.add_property("position",
GETTER(std::vector<double>, Tag, position),
SETTER(std::vector<double> &, Tag, position),
doc::tag_position)
.add_property("extent",
GETTER(std::vector<double>, Tag, extent),
setExtent,
doc::tag_extent)
// References
.def("_add_reference_by_id", REF_SETTER(std::string, Tag, addReference))
.def("_has_reference_by_id", CHECKER(std::string, Tag, hasReference))
.def("_reference_count", &Tag::referenceCount)
.def("_get_reference_by_id", &getReferenceById)
.def("_get_reference_by_pos", &getReferenceByPos)
.def("_delete_reference_by_id", REMOVER(std::string, Tag, removeReference))
// Features
.def("create_feature", &createNewFeature, doc::tag_create_feature)
.def("_has_feature_by_id", CHECKER(std::string, Tag, hasFeature))
.def("_feature_count", &Tag::featureCount)
.def("_get_feature_by_id", &getFeatureById)
.def("_get_feature_by_pos", &getFeatureByPos)
.def("_delete_feature_by_id", REMOVER(std::string, Tag, deleteFeature))
// Data access
.def("retrieve_data", &retrieveDataIdx)
.def("retrieve_data", &retrieveDataStr)
.def("retrieve_feature_data", &retrieveFeatureDataIdx)
.def("retrieve_feature_data", &retrieveFeatureDataStr)
// Other
.def("__str__", &toStr<Tag>)
.def("__repr__", &toStr<Tag>)
;
to_python_converter<std::vector<Tag>, vector_transmogrify<Tag>>();
to_python_converter<boost::optional<Tag>, option_transmogrify<Tag>>();
}
}