Skip to content

Commit

Permalink
Merge pull request #1398 from MDAnalysis/issue-1397-group-hash
Browse files Browse the repository at this point in the history
- Explicitly define __hash__ for groups:
   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
orbeckst authored Jun 14, 2017
2 parents 6e6aa9d + e3d5818 commit ebe2b84
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
7 changes: 5 additions & 2 deletions package/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@ The rules for this file:
* release numbers follow "Semantic Versioning" http://semver.org

------------------------------------------------------------------------------
mm/dd/17

* 0.16.2 richardjgowers, rathann

mm/dd/17 richardjgowers, rathann, jbarnoud

* 0.16.2

Enhancements

Fixes
* fixed GROWriter truncating long resids from the wrong end (Issue #1395)
* Fixed dtype of numpy arrays to accomodate 32 bit architectures (Issue #1362)
* 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 ebe2b84

Please sign in to comment.