Commit 7a22ce95 authored by John Chodera (MSKCC)'s avatar John Chodera (MSKCC)
Browse files

Rework and streamline deepcopy and pickle tests

parent 2b66a71b
...@@ -27,44 +27,46 @@ class TestPickle(unittest.TestCase): ...@@ -27,44 +27,46 @@ class TestPickle(unittest.TestCase):
self.pdb2 = PDBFile('systems/alanine-dipeptide-implicit.pdb') self.pdb2 = PDBFile('systems/alanine-dipeptide-implicit.pdb')
self.forcefield2 = ForceField('amber99sb.xml', 'amber99_obc.xml') self.forcefield2 = ForceField('amber99sb.xml', 'amber99_obc.xml')
def test_force_deepcopy(self): def check_copy(self, object, object_copy):
"""Test that deep copying of forces works correctly.""" """Check that an object's copy is an accurate replica."""
force = NonbondedForce()
force_copy = copy.deepcopy(force)
# Check class name is same. # Check class name is same.
self.assertEqual(force.__class__.__name__, force_copy.__class__.__name__) self.assertEqual(object.__class__.__name__, object_copy.__class__.__name__)
# Check Force object contents are the same. # Check serialized contents are the same.
self.assertEqual(XmlSerializer.serialize(force), XmlSerializer.serialize(force_copy)) self.assertEqual(XmlSerializer.serialize(object), XmlSerializer.serialize(object_copy))
def test_deepcopy(self): def test_deepcopy(self):
"""Test that serialization/deserialization works (via deepcopy).""" """Test that serialization/deserialization works (via deepcopy)."""
# Create system, integrator, and state.
system = self.forcefield1.createSystem(self.pdb1.topology) system = self.forcefield1.createSystem(self.pdb1.topology)
integrator = VerletIntegrator(2*femtosecond) integrator = VerletIntegrator(2*femtosecond)
context = Context(system, integrator) context = Context(system, integrator)
context.setPositions(self.pdb1.positions) context.setPositions(self.pdb1.positions)
state = context.getState(getPositions=True, getForces=True, getEnergy=True) state = context.getState(getPositions=True, getForces=True, getEnergy=True)
system2 = copy.deepcopy(system) #
integrator2 = copy.deepcopy(integrator) # Test deepcopy
state2 = copy.deepcopy(state) #
str_state = pickle.dumps(state) self.check_copy(system, copy.deepcopy(system))
str_integrator = pickle.dumps(integrator) self.check_copy(integrator, copy.deepcopy(integrator))
self.check_copy(state, copy.deepcopy(state))
state3 = pickle.loads(str_state) for force_index in range(system.getNumForces()):
context.setState(state3) force = system.getForce(force_index)
force_copy = copy.deepcopy(force)
self.check_copy(force, force_copy)
del context, integrator #
# Test pickle
#
# Check deep copy of each force. self.check_copy(system, pickle.loads(pickle.dumps(system)))
forces = [ system.getForce(index) for index in range(system.getNumForces()) ] self.check_copy(integrator, pickle.loads(pickle.dumps(integrator)))
for force in forces: self.check_copy(state, pickle.loads(pickle.dumps(state)))
force_copy = copy.deepcopy(force) for force_index in range(system.getNumForces()):
# Check class name is same. force = system.getForce(force_index)
self.assertEqual(force.__class__.__name__, force_copy.__class__.__name__) force_copy = pickle.loads(pickle.dumps(force))
# Check Force object contents are the same. self.check_copy(force, force_copy)
self.assertEqual(XmlSerializer.serialize(force), XmlSerializer.serialize(force_copy))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
......
Markdown is supported
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