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 #1387
  • Loading branch information
jbarnoud committed Jun 13, 2017
1 parent 7652710 commit 88ed46b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
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
54 changes: 53 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,55 @@ 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 in levels:
for level_b in levels:
if level_a != level_b:
a = getattr(u, level_a)[0:-1]
b = getattr(u, level_b)[0:-1]
yield _hash_not_equal, a, b


class TestAtomGroup(object):

@staticmethod
Expand Down

0 comments on commit 88ed46b

Please sign in to comment.