Skip to content

Commit

Permalink
Explicitly define __hash__ for groups
Browse files Browse the repository at this point in the history
Groups (AtomGroup, ResidueGroup, SegmentGroup) cannot be stored in sets
or used as dict key if they are not hashable. In python 3, the __hash__
method is not defined implicitly anymore when a class has a __eq__ method.

Fixes #1397
  • Loading branch information
jbarnoud committed Jun 13, 2017
1 parent 7652710 commit 1717886
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ The rules for this file:
* release numbers follow "Semantic Versioning" http://semver.org

------------------------------------------------------------------------------
mm/dd/yy
mm/dd/yy jbarnoud

* 0.16.2

Enhancements

Fixes
* Groups are hashable on python 3 (Issue #1397)

Changes

Expand Down
3 changes: 3 additions & 0 deletions package/MDAnalysis/core/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,9 @@ def __init__(self, *args):
self._u = u
self._cache = dict()

def __hash__(self):
return hash((self._u, self.__class__, tuple(self.ix.tolist())))

def __len__(self):
return len(self._ix)

Expand Down
63 changes: 62 additions & 1 deletion testsuite/MDAnalysisTests/core/test_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import itertools
import numpy as np
from numpy.testing import (
dec,
assert_,
assert_array_equal,
assert_equal,
Expand All @@ -34,7 +35,7 @@
import six

import MDAnalysis as mda
from MDAnalysisTests import make_Universe
from MDAnalysisTests import make_Universe, parser_not_found
from MDAnalysisTests.datafiles import PSF, DCD
from MDAnalysis.core import groups
from MDAnalysis.core.topology import Topology
Expand Down Expand Up @@ -578,6 +579,8 @@ def test_groupby_int(self):


class TestReprs(object):
@dec.skipif(parser_not_found('DCD'),
'DCD parser not available. Are you using python 3?')
def setUp(self):
self.u = mda.Universe(PSF, DCD)

Expand Down Expand Up @@ -919,6 +922,64 @@ def check_operator(op, method, level):
yield check_operator, op, method, level


class TestGroupHash(object):
"""
Groups should be hashable.
See issue #1397
"""
def test_hash_exists(self):
def _hash_type(group):
assert_(isinstance(hash(group), int))

u = make_Universe(size=(3, 3, 3))
for level in ('atoms', 'residues', 'segments'):
group = getattr(u, level)
yield _hash_type, group

def test_hash_equality(self):
def _hash_equal(a, b):
assert_equal(hash(a), hash(b))

u = make_Universe(size=(3, 3, 3))
for level in ('atoms', 'residues', 'segments'):
a = getattr(u, level)[0:-1]
b = getattr(u, level)[0:-1]
yield _hash_equal, a, b

def test_hash_difference(self):
def _hash_not_equal(a, b):
assert_(hash(a) != hash(b))

u = make_Universe(size=(3, 3, 3))
for level in ('atoms', 'residues', 'segments'):
a = getattr(u, level)[:-1]
b = getattr(u, level)[1:]
yield _hash_not_equal, a, b

def test_hash_difference_cross(self):
def _hash_not_equal(a, b):
assert_(hash(a) != hash(b))

u = make_Universe(size=(3, 3, 3))
levels = ('atoms', 'residues', 'segments')
for level_a, level_b in itertools.permutations(levels, 2):
a = getattr(u, level_a)[0:-1]
b = getattr(u, level_b)[0:-1]
yield _hash_not_equal, a, b

def test_hash_diff_cross_universe(self):
def _hash_not_equal(a, b):
assert_(hash(a) != hash(b))

u = make_Universe(size=(3, 3, 3))
u2 = make_Universe(size=(3, 3, 3))
for level in ('atoms', 'residues', 'segments'):
a = getattr(u, level)
b = getattr(u2, level)
yield _hash_not_equal, a, b


class TestAtomGroup(object):

@staticmethod
Expand Down

0 comments on commit 1717886

Please sign in to comment.