Skip to content

Commit

Permalink
Merge pull request #734 from grigorisg9gr/tiny_change_group_labels
Browse files Browse the repository at this point in the history
Change LandmarkGroup dict to OrderedDict
  • Loading branch information
Patrick Snape committed Oct 15, 2016
2 parents 0166a39 + 825e06d commit 42fd7c4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
16 changes: 11 additions & 5 deletions menpo/landmark/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def n_landmark_groups(self):


class LandmarkManager(MutableMapping, Transformable):
"""Store for :map:`LandmarkGroup` instances associated with an object
"""Store for :map:`LandmarkGroup` instances associated with an object.
Every :map:`Landmarkable` instance has an instance of this class available
at the ``.landmarks`` property. It is through this class that all access
Expand All @@ -92,14 +92,13 @@ class LandmarkManager(MutableMapping, Transformable):
:map:`LandmarkManager` - in this case ``None`` can be used as a key to
access the sole group.
Note that all landmarks stored on a :map:`Landmarkable` in it's attached
:map:`LandmarkManager` are automatically transformed and copied with their
parent object.
"""
def __init__(self):
super(LandmarkManager, self).__init__()
self._landmark_groups = {}
self._landmark_groups = OrderedDict()

@property
def n_dims(self):
Expand Down Expand Up @@ -133,7 +132,7 @@ def copy(self):

def __iter__(self):
"""
Iterate over the internal landmark group dictionary
Iterate over the internal landmark group dictionary.
"""
return iter(self._landmark_groups)

Expand Down Expand Up @@ -219,6 +218,13 @@ def __delitem__(self, group):
def __len__(self):
return len(self._landmark_groups)

def __setstate__(self, state):
# consistency with older versions imported.
if not isinstance(state['_landmark_groups'], OrderedDict):
state['_landmark_groups'] = OrderedDict(state['_landmark_groups'])

self.__dict__ = state

@property
def n_groups(self):
"""
Expand All @@ -240,7 +246,7 @@ def has_landmarks(self):
@property
def group_labels(self):
"""
All the labels for the landmark set.
All the labels for the landmark set sorted by insertion order.
:type: `list` of `str`
"""
Expand Down
16 changes: 16 additions & 0 deletions menpo/landmark/test/landmark_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,22 @@ def test_LandmarkManager_del():
assert_equal(man.n_groups, 0)


def test_LandmarkManager_group_order():
points = np.ones((10, 3))
pcloud = PointCloud(points, copy=False)
target = PointCloud(points)

man = LandmarkManager()
man['test_set'] = pcloud.copy()
man['abc'] = pcloud.copy()
man['def'] = pcloud.copy()
assert_equal(list(man._landmark_groups.keys()), ['test_set', 'abc', 'def'])
# check that after deleting and inserting the order will remain the same.
del man['test_set']
man['tt'] = pcloud.copy()
assert_equal(list(man._landmark_groups.keys()), ['abc', 'def', 'tt'])


def test_LandmarkManager_in():
points = np.ones((10, 3))
pcloud = PointCloud(points, copy=False)
Expand Down

0 comments on commit 42fd7c4

Please sign in to comment.