Commit e4e6e853 authored by Martin Reinecke's avatar Martin Reinecke
Browse files

Merge branch 'gridder-config-pickle' into 'better_params'

Add pickling support to GridderConfig

See merge request !2
parents bf7017e6 72c1b21e
......@@ -1091,7 +1091,27 @@ PYBIND11_MODULE(nifty_gridder, m)
.def("grid2dirty_c", &GridderConfig<double>::grid2dirty_c, "grid"_a)
.def("dirty2grid", &GridderConfig<double>::dirty2grid, dirty2grid_DS,
.def("dirty2grid_c", &GridderConfig<double>::dirty2grid_c, "dirty"_a);
.def("dirty2grid_c", &GridderConfig<double>::dirty2grid_c, "dirty"_a)
// pickle support
// __getstate__
[](const GridderConfig<double> & gc) {
// Encode object state in tuple
return py::make_tuple(gc.Nxdirty(), gc.Nydirty(), gc.Epsilon(),
gc.Pixsize_x(), gc.Pixsize_y());
// __setstate__
[](py::tuple t) {
if(t.size() != 5)
{ throw std::runtime_error("Invalid state"); }
// Reconstruct from tuple
return GridderConfig<double>(t[0].cast<size_t>(), t[1].cast<size_t>(),
t[2].cast<double>(), t[3].cast<double>(),
m.def("getIndices", getIndices<double>, getIndices_DS, "baselines"_a,
"gconf"_a, "flags"_a, "chbegin"_a=-1, "chend"_a=-1,
"wmin"_a=-1e30, "wmax"_a=1e30);
import cPickle as pickle
except ImportError:
import pickle
import nifty_gridder as ng
# Have to use cPickle and pickle protocol 2 for this to work
# unfortunately
pickle_protocol = 2
gc = ng.GridderConfig(1024, 1024, 2e-13, 2.0, 2.0)
pickled = pickle.dumps(gc, pickle_protocol)
gc2 = pickle.loads(pickled)
assert gc is not gc2
assert gc.Nxdirty() == gc2.Nxdirty()
assert gc.Nydirty() == gc2.Nydirty()
assert gc.Epsilon() == gc2.Epsilon()
assert gc.Pixsize_x() == gc2.Pixsize_x()
assert gc.Pixsize_y() == gc2.Pixsize_y()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment