Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: pickling skeletons #90

Open
jrjbertram opened this issue Dec 22, 2021 · 1 comment
Open

Question: pickling skeletons #90

jrjbertram opened this issue Dec 22, 2021 · 1 comment

Comments

@jrjbertram
Copy link

Hi there,

I tried pickling a polygon-with-holes straight skeleton today and got an error. Example code to illustrate what I'm doing:

poly = sg.PolygonWithHoles(shell, holes )
skel = sg.skeleton.create_interior_straight_skeleton(poly)
skeleton = { 'poly': poly, 'skel': skel }
pickle.dump( skeleton, open( 'skeleton.pkl', 'wb') )

error:

TypeError: cannot pickle 'skgeom._skgeom.PolygonWithHoles' object

My hope was to cache off this data structure as it takes quite a while to compute for my problem. Is this a bug, a limitation, or just something I'm doing wrong? Or do I need to unpack everything I need out of the skel object into lists/dictionaries and pickle that instead?

Didn't see any similar issues for this project and don't see a forum identified for questions, so adding this issue here. Apologies if there is a preferred other place to ask (e.g., stack overflow, etc.)

Thank you!
Josh.

@jrjbertram
Copy link
Author

Doing some more digging on this question. It's looking like the answer is no from here:
https://docs.python.org/3/library/pickle.html#what-can-be-pickled-and-unpickled

Specifically I think this rule is probably being violated:

  • classes that are defined at the top level of a module

For example here where classes are being defined within the C++ implementation, though I'm not 100% on whether this technically violates the rule.

https://github.com/scikit-geometry/scikit-geometry/blob/master/src/skeleton.cpp#L84

And there is no fallback provided per this rule:

  • instances of such classes whose dict or the result of calling getstate() is picklable

So if I'm understanding everything correctly, to support pickling, each class would need to have a getstate member function added to it which returned a dictionary that captured its state, along with a setstate member function would would allow this state to be restored (with appropriate supporting CGAL calls assuming they are available.) Something along the lines of:

https://docs.python.org/3/library/pickle.html#handling-stateful-objects

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant