Skip to content

Commit

Permalink
make curve and curves pickleable
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Rohde committed Jul 17, 2023
1 parent 3ffe6d6 commit c5cafc4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
A fast, scalable and light-weight C++ Fréchet distance library, exposed to python and focused on (k,l)-clustering of polygonal curves.

### DISTANCE FUNCTION FOR CLUSTERING CAN BE CHOSEN (heuristic in case of DTW)
### NOW WITH OPTIMIZED CLUSTER CENTERS
### CURVE AND CURVES CAN NOW BE PICKLED

## Ingredients
`import Fred as fred`
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def build_extension(self, ext):

setup(
name='Fred-Frechet',
version='1.12.3',
version='1.12.4',
author='Dennis Rohde',
author_email='[email protected]',
description='A fast, scalable and light-weight C++ Fréchet distance library, exposed to python and focused on (k,l)-clustering of polygonal curves.',
Expand Down
2 changes: 1 addition & 1 deletion src/clustering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void Clustering_Result::compute_assignment(const Curves &in, const bool consecut
if (consecutive_call and in.size() == distances.size()) {
for (curve_number_t i = 0; i < in.size(); ++i) assignment[_nearest_center(i, in, simplifications, center_indices, distances, distance_func)].push_back(i);
} else {
distances = Distance_Matrix(in.size(), centers.size());
if (use_distance_matrix) distances = Distance_Matrix(in.size(), centers.size());
auto ncenter_indices = Curve_Numbers(centers.size());
std::iota(ncenter_indices.begin(), ncenter_indices.end(), 0);
for (curve_number_t i = 0; i < in.size(); ++i)
Expand Down
27 changes: 27 additions & 0 deletions src/fred_python_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ PYBIND11_MODULE(backend, m) {
.def("__str__", &Curve::str)
.def("__iter__", [](Curve &v) { return py::make_iterator(v.begin(), v.end()); }, py::keep_alive<0, 1>())
.def("__repr__", &Curve::repr)
.def(py::pickle(
[](const Curve &c) {
return py::make_tuple(c.as_ndarray(), c.get_name());
},
[](py::tuple t) {
const auto coords = t[0].cast<py::array_t<coordinate_t>>();
const auto name = t[1].cast<std::string>();
return Curve(coords, name);
} ))
;

py::class_<Curves>(m, "Curves")
Expand All @@ -122,6 +131,24 @@ PYBIND11_MODULE(backend, m) {
.def("__repr__", &Curves::repr)
.def_property_readonly("values", &Curves::as_ndarray)
.def_property_readonly("dimensions", &Curves::dimensions)
.def(py::pickle(
[](const Curves &c) {
py::list l;
for (const Curve &elem : c) {
l.append(py::make_tuple(elem.as_ndarray(), elem.get_name()));
}
return l;
},
[](py::list l) {
Curves result;
for (const auto &elem : l) {
const auto t = elem.cast<py::tuple>();
const auto coords = t[0].cast<py::array_t<coordinate_t>>();
const auto name = t[1].cast<std::string>();
result.push_back(Curve(coords, name));
}
return result;
} ))
;

py::class_<fc::Distance>(m, "Continuous_Frechet_Distance")
Expand Down

0 comments on commit c5cafc4

Please sign in to comment.