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

Explicitly define __hash__ for groups #1398

Merged
merged 1 commit into from
Jun 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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