Skip to content

Commit

Permalink
ENH: Add DataStructure.exists() to the python bindings
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Jackson <[email protected]>
  • Loading branch information
imikejackson committed Apr 17, 2024
1 parent e7c7d8e commit a640bf2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
42 changes: 27 additions & 15 deletions src/Plugins/SimplnxCore/wrapping/python/simplnxpy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,7 @@ PYBIND11_MODULE(simplnx, mod)
parameters.def("insert_linkable_parameter", &PyInsertLinkableParameter<ChoicesParameter>);
parameters.def("link_parameters", [](Parameters& self, std::string groupKey, std::string childKey, BoolParameter::ValueType value) { self.linkParameters(groupKey, childKey, value); });
parameters.def("link_parameters", [](Parameters& self, std::string groupKey, std::string childKey, ChoicesParameter::ValueType value) { self.linkParameters(groupKey, childKey, value); });
parameters.def(
"__getitem__", [](Parameters& self, std::string_view key) { return self.at(key).get(); }, py::return_value_policy::reference_internal);
parameters.def("__getitem__", [](Parameters& self, std::string_view key) { return self.at(key).get(); }, py::return_value_policy::reference_internal);

Check failure on line 524 in src/Plugins/SimplnxCore/wrapping/python/simplnxpy.cpp

View workflow job for this annotation

GitHub Actions / clang_format_pr

code should be clang-formatted [-Wclang-format-violations]

py::class_<IArrayThreshold, std::shared_ptr<IArrayThreshold>> iArrayThreshold(mod, "IArrayThreshold");

Expand Down Expand Up @@ -631,16 +630,33 @@ PYBIND11_MODULE(simplnx, mod)
}
return self.removeData(pathConversionResult.value());
});

dataStructure.def(
"exists",
[](DataStructure& self, const std::string& path) {
auto pathConversionResult = DataPath::FromString(path);
if(!pathConversionResult)
{
return false;
}
return (nullptr != self.getData(pathConversionResult.value()));
},
"Returns true if there is a DataStructure object at the given path");

dataStructure.def("exists", [](DataStructure& self, const DataPath& path) { return (nullptr != self.getData(path)); }, "Returns true if there is a DataStructure object at the given path");

Check failure on line 646 in src/Plugins/SimplnxCore/wrapping/python/simplnxpy.cpp

View workflow job for this annotation

GitHub Actions / clang_format_pr

code should be clang-formatted [-Wclang-format-violations]
dataStructure.def("hierarchy_to_str", [](DataStructure& self) {
std::stringstream ss;
self.exportHierarchyAsText(ss);
return ss.str();
});
dataStructure.def("hierarchy_to_graphviz", [](DataStructure& self) {
std::stringstream ss;
self.exportHierarchyAsGraphViz(ss);
return ss.str();
});
dataStructure.def(
"hierarchy_to_graphviz",
[](DataStructure& self) {
std::stringstream ss;
self.exportHierarchyAsGraphViz(ss);
return ss.str();
},
"Returns the DataStructure hierarchy expressed in the 'dot' language. Use a GraphViz package to render.");
dataStructure.def("get_children", [](DataStructure& self, nx::core::DataPath& parentPath) {
if(parentPath.empty())
{
Expand Down Expand Up @@ -1318,12 +1334,10 @@ PYBIND11_MODULE(simplnx, mod)
},
"name"_a, "path"_a);
pipeline.def("execute", &ExecutePipeline);
pipeline.def(
"__getitem__", [](Pipeline& self, Pipeline::index_type index) { return self.at(index); }, py::return_value_policy::reference_internal);
pipeline.def("__getitem__", [](Pipeline& self, Pipeline::index_type index) { return self.at(index); }, py::return_value_policy::reference_internal);

Check failure on line 1337 in src/Plugins/SimplnxCore/wrapping/python/simplnxpy.cpp

View workflow job for this annotation

GitHub Actions / clang_format_pr

code should be clang-formatted [-Wclang-format-violations]
pipeline.def("__len__", &Pipeline::size);
pipeline.def("size", &Pipeline::size);
pipeline.def(
"__iter__", [](Pipeline& self) { return py::make_iterator(self.begin(), self.end()); }, py::keep_alive<0, 1>());
pipeline.def("__iter__", [](Pipeline& self) { return py::make_iterator(self.begin(), self.end()); }, py::keep_alive<0, 1>());

Check failure on line 1340 in src/Plugins/SimplnxCore/wrapping/python/simplnxpy.cpp

View workflow job for this annotation

GitHub Actions / clang_format_pr

code should be clang-formatted [-Wclang-format-violations]
pipeline.def(
"insert",
[internals](Pipeline& self, Pipeline::index_type index, const IFilter& filter, const py::dict& args) {
Expand All @@ -1337,10 +1351,8 @@ PYBIND11_MODULE(simplnx, mod)
pipeline.def("remove", &Pipeline::removeAt, "index"_a);

pipelineFilter.def("get_args", [internals](PipelineFilter& self) { return ConvertArgsToDict(*internals, self.getParameters(), self.getArguments()); });
pipelineFilter.def(
"set_args", [internals](PipelineFilter& self, py::dict& args) { self.setArguments(ConvertDictToArgs(*internals, self.getParameters(), args)); }, "args"_a);
pipelineFilter.def(
"get_filter", [](PipelineFilter& self) { return self.getFilter(); }, py::return_value_policy::reference_internal);
pipelineFilter.def("set_args", [internals](PipelineFilter& self, py::dict& args) { self.setArguments(ConvertDictToArgs(*internals, self.getParameters(), args)); }, "args"_a);

Check failure on line 1354 in src/Plugins/SimplnxCore/wrapping/python/simplnxpy.cpp

View workflow job for this annotation

GitHub Actions / clang_format_pr

code should be clang-formatted [-Wclang-format-violations]
pipelineFilter.def("get_filter", [](PipelineFilter& self) { return self.getFilter(); }, py::return_value_policy::reference_internal);

Check failure on line 1355 in src/Plugins/SimplnxCore/wrapping/python/simplnxpy.cpp

View workflow job for this annotation

GitHub Actions / clang_format_pr

code should be clang-formatted [-Wclang-format-violations]

py::class_<PyFilter, IFilter> pyFilter(mod, "PyFilter");
pyFilter.def(py::init<>([](py::object object) { return std::make_unique<PyFilter>(std::move(object)); }));
Expand Down
9 changes: 9 additions & 0 deletions wrapping/python/examples/scripts/basic_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@
nxtest.check_filter_result(nx.CreateDataGroup, result)


#------------------------------------------------------------------------------
# Check if that object exists in the DataStructure
#------------------------------------------------------------------------------
value = data_structure.exists("/Some/Path/To/Group")
print(f'The path "/Some/Path/To/Group" exists: {value} ')

value = data_structure.exists("/Some/Path/To/NonExistantGroup")
print(f'The path "/Some/Path/To/NonExistantGroup" exists: {value} ')

#------------------------------------------------------------------------------
# Create 1D Array
#------------------------------------------------------------------------------
Expand Down

0 comments on commit a640bf2

Please sign in to comment.