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

Save particle state in a dict (Issue #3916) #4059

Closed
wants to merge 3 commits into from

Conversation

mili1247
Copy link
Contributor

#3916

Add functions:
ParticleHandle.to_dict()
ParticleSlice.to_dict()

Description of changes:
Returns the particles attributes as a dictionary, such that it can be used to save the particle data and recover it by using

p = system.part.add(...)
particle_dict = p.to_dict()
system.part.add(particle_dict)

Codes are mainly written by @RiccardoFrenner

@espresso-ci
Copy link

Your pull request does not meet our code formatting rules. Specifically, I suggest you make the following changes:

diff --git a/src/python/espressomd/particle_data.pyx b/src/python/espressomd/particle_data.pyx
index 281a2d5ed..d6afb6b02 100644
--- a/src/python/espressomd/particle_data.pyx
+++ b/src/python/espressomd/particle_data.pyx
@@ -55,9 +55,8 @@ cdef class ParticleHandle:
 
     cdef int update_particle_data(self) except -1:
         self.particle_data = &get_particle_data(self._id)
-        
-    def to_dict(self):
 
+    def to_dict(self):
         """
         Returns the particle's attributes as a dictionary.
         It includes the content of \`\`particle_attributes\`\`, minus a few exceptions:
@@ -179,7 +178,7 @@ cdef class ParticleHandle:
 
         def __get__(self):
             self.update_particle_data()
-            return make_array_locked(unfolded_position(< Vector3d > self.particle_data.r.p, < Vector3i > self.particle_data.l.i, box_geo.length()))
+            return make_array_locked(unfolded_position( < Vector3d > self.particle_data.r.p, < Vector3i > self.particle_data.l.i, box_geo.length()))
 
     property pos_folded:
         """
@@ -1701,17 +1700,16 @@ class ParticleSlice(_ParticleSliceImpl):
             raise AttributeError(
                 f"ParticleHandle does not have the attribute {name}.")
         super().__setattr__(name, value)
-        
-    def to_dict(self):
 
+    def to_dict(self):
         """
         Returns the particles attributes as a dictionary, such that it can
         be used to save the particle data and recover it easily using
-        
+
         >>> p = system.part.add(...)
         >>> particle_dict = p.to_dict()
         >>> system.part.add(particle_dict)
-        
+
         It includes the content of \`\`particle_attributes\`\`, minus a few exceptions:
 
         - :attr:\`~ParticleHandle.dip\`, :attr:\`~ParticleHandle.director\`:
@@ -1723,7 +1721,7 @@ class ParticleSlice(_ParticleSliceImpl):
 
         odict = {}
         key_list = [p.id for p in self]
-        
+
         for particle_number in key_list:
             pdict = ParticleHandle(particle_number).to_dict()
             for p_key, p_value in pdict.items():
@@ -1768,7 +1766,7 @@ cdef class ParticleList:
         - :attr:\`~ParticleHandle.image_box\`, :attr:\`~ParticleHandle.node\`
 
         """
-        
+
         odict = {}
         for p in self:
             pdict = p.to_dict()
diff --git a/testsuite/python/particle_to_dict.py b/testsuite/python/particle_to_dict.py
index ce45cdc82..1a4b60e07 100644
--- a/testsuite/python/particle_to_dict.py
+++ b/testsuite/python/particle_to_dict.py
@@ -3,17 +3,22 @@ import unittest_decorators as utx
 import numpy as np
 import espressomd
 
+
 class ParticleDictionaryTest(ut.TestCase):
     box_l = 30.
-    system = espressomd.System(box_l = 3 * [box_l])
+    system = espressomd.System(box_l=3 * [box_l])
 
     def test(self):
-        p = self.system.part.add(pos = np.random.uniform(size = (10,3)) * self.box_l)
+        p = self.system.part.add(
+            pos=np.random.uniform(
+                size=(
+                    10, 3)) * self.box_l)
         pp = str(p)
         pdict = p.to_dict()
         p.remove()
         self.system.part.add(pdict)
         self.assertEqual(str(self.system.part.select()), pp)
 
+
 if __name__ == "__main__":
     ut.main()

To apply these changes, please do one of the following:

  • You can download a patch with my suggested changes here, inspect it and make changes manually.
  • You can directly apply it to your repository by running curl https://gitlab.icp.uni-stuttgart.de/espressomd/espresso/-/jobs/265985/artifacts/raw/style.patch | git apply -.
  • You can run maintainer/CI/fix_style.sh to automatically fix your coding style. This is the same command that I have executed to generate the patch above, but it requires certain tools to be installed on your computer.

You can run gitlab-runner exec docker style afterwards to check if your changes worked out properly.

Please note that there are often multiple ways to correctly format code. As I am just a robot, I sometimes fail to identify the most aesthetically pleasing way. So please look over my suggested changes and adapt them where the style does not make sense.

@espresso-ci
Copy link

Your pull request does not meet our code style rules. Pylint summary:

************* Module particle_to_dict
testsuite/python/particle_to_dict.py:2:0: W0611: Unused unittest_decorators imported as utx (unused-import)

You can generate these warnings with maintainer/CI/fix_style.sh. This is the same command that I have executed to generate the log above.

@mili1247 mili1247 closed this Dec 18, 2020
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

Successfully merging this pull request may close these issues.

2 participants