Commit b9abf758 authored by Adam Paquette's avatar Adam Paquette
Browse files

Added new dispatch method and appropriate test

parent 5a91d4a1
Loading
Loading
Loading
Loading
+10 −11
Original line number Diff line number Diff line
from functools import wraps
from functools import wraps, singledispatch
import warnings
from collections import MutableMapping

@@ -11,6 +11,7 @@ from scipy.spatial.distance import cdist
import autocnet
from autocnet.graph.node import Node
from autocnet.utils import utils
# from autocnet.utils.utils import methdispatch
from autocnet.matcher import cpu_outlier_detector as od
from autocnet.matcher import suppression_funcs as spf
from autocnet.matcher import subpixel as sp
@@ -199,20 +200,18 @@ class Edge(dict, MutableMapping):
            # Set the initial state of the fundamental mask in the masks
            self.masks[maskname] = mask

    @utils.methdispatch
    def get_keypoints(self, node, index=None, homogeneous=False):
        print(type(node))
        if type(node) is str:
            node = node.lower()
            if node == "source" or node == "destination":
                node = getattr(self, node)
            else:
                raise KeyError
        elif type(node) is not Node:
        if not hasattr(index, '__iter__') and index is not None:
            raise TypeError
        print(hasattr(index, '__iter__'))
        print(index is not None)
        return node.get_keypoint_coordinates(index=index, homogeneous=homogeneous)

    @get_keypoints.register(str)
    def _(self, node, index=None, homogeneous=False):
        if not hasattr(index, '__iter__') and index is not None:
            raise TypeError
        node = node.lower()
        node = getattr(self, node)
        return node.get_keypoint_coordinates(index=index, homogeneous=homogeneous)

    def compute_fundamental_error(self, clean_keys=[]):
+2 −2
Original line number Diff line number Diff line
@@ -170,10 +170,10 @@ class TestEdge(unittest.TestCase):
        # Assert type-checking in method throws proper errors
        with self.assertRaises(TypeError):
            e.get_keypoints("source", index = 456)
        with self.assertRaises(TypeError):
        with self.assertRaises(AttributeError):
            e.get_keypoints(1)
        # Check key error thrown when string arg != "source" or "destination"
        with self.assertRaises(KeyError):
        with self.assertRaises(AttributeError):
            e.get_keypoints("string")

    def test_eq(self):
+19 −0
Original line number Diff line number Diff line
@@ -140,3 +140,22 @@ class TestUtils(unittest.TestCase):

        self.assertIsInstance(geom1, ogr.Geometry)
        self.assertRaises(ValueError, utils.array_to_poly, array2)

    def test_dispatch(self):
        class Patchwork(object):

            def __init__(self, **kwargs):
                for k, v in kwargs.items():
                    setattr(self, k, v)

            @utils.methdispatch
            def get(self, arg):
                return getattr(self, arg, None)

            @get.register(list)
            def _(self, arg):
                return [self.get(x) for x in arg]

        patchwork = Patchwork(a=1, b=2, c=3)
        self.assertEqual(patchwork.get(['a', 'b']), [1, 2])
        self.assertEqual(patchwork.get('c'), 3)
+26 −1
Original line number Diff line number Diff line
import json

from functools import reduce
from functools import reduce, singledispatch, update_wrapper

import numpy as np
import pandas as pd
@@ -341,3 +341,28 @@ def array_to_poly(array):
    poly = ogr.CreateGeometryFromJson(json.dumps(geom))
    return poly


def methdispatch(func):
    """
    New dispatch decorator that looks at the second argument to
    avoid self

    Parameters
    ----------
    func : Object
        Function object to be dispatched

    Returns
    wrapper : Object
        Wrapped function call chosen by the dispatcher
    ----------

    """
    dispatcher = singledispatch(func)

    def wrapper(*args, **kw):
        return dispatcher.dispatch(args[1].__class__)(*args, **kw)

    wrapper.register = dispatcher.register
    update_wrapper(wrapper, dispatcher)
    return wrapper