1 #ifndef MHO_PyTableContainer_HH__
2 #define MHO_PyTableContainer_HH__
17 #include <pybind11/numpy.h>
18 #include <pybind11/pybind11.h>
19 #include <pybind11/stl.h>
20 namespace py = pybind11;
44 fRank = XTableType::rank::value;
45 fTable =
dynamic_cast< XTableType*
>(element);
50 std::size_t
GetRank()
const {
return fTable->GetRank(); }
52 std::size_t
GetDimension(std::size_t index)
const {
return fTable->GetDimension(index); }
54 std::string
GetClassName()
const {
return MHO_ClassName< XTableType >(); }
61 auto strides = fTable->GetStrideArray();
62 for(std::size_t i = 0; i < fRank; i++)
64 strides[i] *=
sizeof(
typename XTableType::value_type);
66 py::array_t< typename XTableType::value_type > ret_val{
67 fTable->GetDimensionArray(), strides, fTable->GetData(),
81 PyListFiller filler(&ret_val);
82 apply_at< typename XTableType::axis_pack_tuple_type, PyListFiller >(*fTable, index, filler);
86 msg_error(
"python_bindings",
"axis index: " << index <<
" exceeds table rank of: " << fRank << eom);
88 ss <<
"axis index: " << index <<
" exceeds table rank of: " << fRank;
89 py::print(
"error: ", ss.str());
97 if(axis_index < fRank)
99 PyAxisLabelModifier modifier(label_index, &label);
100 apply_at< typename XTableType::axis_pack_tuple_type, PyAxisLabelModifier >(*fTable, axis_index, modifier);
104 msg_error(
"python_bindings",
"axis index: " << axis_index <<
" exceeds table rank of: " << fRank << eom);
105 std::stringstream ss;
106 ss <<
"axis index: " << axis_index <<
" exceeds table rank of: " << fRank;
107 py::print(
"error: ", ss.str());
111 py::dict
GetMetaData() {
return GetTableTags< XTableType >(fTable); }
113 void SetMetaData(py::dict metadata) {
return SetTableTags< XTableType >(fTable, metadata); }
120 PyAxisMetaDataFiller filler(&ret_val);
121 apply_at< typename XTableType::axis_pack_tuple_type, PyAxisMetaDataFiller >(*fTable, index, filler);
125 msg_error(
"python_bindings",
"axis index: " << index <<
" exceeds table rank of: " << fRank << eom);
126 std::stringstream ss;
127 ss <<
"axis index: " << index <<
" exceeds table rank of: " << fRank;
128 py::print(
"error: ", ss.str());
140 PyAxisMetaDataSetter filler(&metadata);
141 apply_at< typename XTableType::axis_pack_tuple_type, PyAxisMetaDataSetter >(*fTable, index, filler);
145 msg_error(
"python_bindings",
"axis index: " << index <<
" exceeds table rank of: " << fRank << eom);
146 std::stringstream ss;
147 ss <<
"axis index: " << index <<
" exceeds table rank of: " << fRank;
148 py::print(
"error: ", ss.str());
153 template<
typename XDataTableType >
static py::dict GetTableTags(XDataTableType* table)
155 py::dict tags = table->GetMetaDataAsJSON();
159 template<
typename XDataTableType >
static void SetTableTags(XDataTableType* table, py::dict metadata)
162 table->SetMetaDataAsJSON(md);
169 PyListFiller(py::list*
alist): fList(
alist){};
172 template<
typename XAxisType >
void operator()(
const XAxisType&
axis)
174 for(
size_t i = 0; i <
axis.GetSize(); i++)
176 fList->append(
axis[i]);
185 class PyAxisMetaDataFiller
188 PyAxisMetaDataFiller(py::dict* adict): fDict(adict){};
189 ~PyAxisMetaDataFiller(){};
191 template<
typename XAxisType >
void operator()(
const XAxisType&
axis) { *fDict =
axis.GetMetaDataAsJSON(); }
198 class PyAxisMetaDataSetter
201 PyAxisMetaDataSetter(py::dict* adict): fDict(adict){};
202 ~PyAxisMetaDataSetter(){};
204 template<
typename XAxisType >
void operator()(XAxisType&
axis) {
axis.SetMetaDataAsJSON(*fDict); }
211 class PyAxisLabelModifier
217 PyAxisLabelModifier(std::size_t index, py::object* label_object): fIndex(index), fObject(label_object){};
218 ~PyAxisLabelModifier(){};
220 template<
typename XAxisType >
void operator()(XAxisType&
axis)
222 typename XAxisType::value_type label_value;
223 label_value = fObject->cast<
typename XAxisType::value_type >();
226 if(fIndex <
axis.GetSize())
228 axis(fIndex) = label_value;
232 msg_error(
"python_bindings",
"error axis coordinate index out of bounds: "
233 << fIndex <<
" > " <<
axis.GetSize() <<
"." << eom);
234 std::stringstream ss;
235 ss <<
"axis coordinate index out of bounds: " << fIndex <<
" > " <<
axis.GetSize();
236 py::print(
"error: ", ss.str());
246 MHO_ExtensibleElement* fElement;
255 if(pyclass_name ==
"")
257 pyclass_name = MHO_ClassName< XTableType >();
260 py::class_< MHO_PyTableContainer< XTableType > >(m, pyclass_name.c_str())
263 "get the rank (number of dimensions) of the table object")
266 "get the size of the associated table dimension", py::arg(
"dimension_index"))
268 "get a copy of the meta data attached to this table as a dictionary")
270 "replace the meta data attached to this table with the passed dictionary", py::arg(
"metadata_object"))
272 "get the underyling table data as a multidimensional numpy array")
274 "get the axis container associated with the dimension index specified", py::arg(
"index"))
276 py::return_value_policy::copy,
277 "get a copy of the meta data attached to axis associated with the dimension index specified", py::arg(
"index"))
279 "replace the meta data attached to the axis associated with the dimension index specified, with passed dictionary "
281 py::arg(
"index"), py::arg(
"metadata_object"))
283 "modify the axis (specified by the dimension_index) at the coordinate location specified by coordinate_index, "
284 "replacing the existing value with label_value",
285 py::arg(
"dimension_index"), py::arg(
"coordinate_index"), py::arg(
"label_value"));
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:244
int axis(char *y_axis, char *x_axis)
Definition: axis.c:22
Class MHO_ExtensibleElement.
Definition: MHO_ExtensibleElement.hh:60
python bindings for template MHO_TableContainer objects This extension which allows us to present the...
Definition: MHO_PyTableContainer.hh:40
MHO_PyTableContainer(MHO_ExtensibleElement *element)
Definition: MHO_PyTableContainer.hh:42
void SetCoordinateLabel(std::size_t axis_index, std::size_t label_index, py::object label)
Definition: MHO_PyTableContainer.hh:95
void SetMetaData(py::dict metadata)
Definition: MHO_PyTableContainer.hh:113
std::size_t GetDimension(std::size_t index) const
Definition: MHO_PyTableContainer.hh:52
py::list GetCoordinateAxis(size_t index)
Definition: MHO_PyTableContainer.hh:76
py::array_t< typename XTableType::value_type > GetNumpyArray()
Definition: MHO_PyTableContainer.hh:59
py::dict GetMetaData()
Definition: MHO_PyTableContainer.hh:111
void SetCoordinateAxisMetaData(std::size_t index, py::dict metadata)
Definition: MHO_PyTableContainer.hh:136
std::size_t GetRank() const
Definition: MHO_PyTableContainer.hh:50
py::dict GetCoordinateAxisMetaData(std::size_t index)
Definition: MHO_PyTableContainer.hh:115
virtual ~MHO_PyTableContainer()
Definition: MHO_PyTableContainer.hh:48
std::string GetClassName() const
Definition: MHO_PyTableContainer.hh:54
def alist(filename)
Definition: afio.py:372
Definition: MHO_ChannelLabeler.hh:17
void DeclarePyTableContainer(py::module &m, std::string pyclass_name="")
Definition: MHO_PyTableContainer.hh:253