Commit 9fee1d07 authored by Evin Dunn's avatar Evin Dunn
Browse files

Removed stateful fundamental matrix lookup from Edge.masks(); Updated refs to...

Removed stateful fundamental matrix lookup from Edge.masks(); Updated refs to Edge.matches df; Added Edge.masks()/ratio_check() test cases
parent 5a02c0fd
Loading
Loading
Loading
Loading
+8 −19
Changes for autocnet/graph/edge.py: 8 added lines, 19 removed lines.
Original line number Diff line number Diff line
@@ -74,22 +74,11 @@ class Edge(dict, MutableMapping):
    def masks(self):
        mask_lookup = {'fundamental': 'fundamental_matrix'}
        if not hasattr(self, '_masks'):
            if self.matches is not None:
            if isinstance(self.matches, pd.DataFrame):
                self._masks = pd.DataFrame(True, columns=['symmetry'],
                                           index=self.matches.index)
            else:
                self._masks = pd.DataFrame()
        # If the mask is coming form another object that tracks
        # state, dynamically draw the mask from the object.
        for c in self._masks.columns:
            if c in mask_lookup:
                try:
                    truncated_mask = getattr(self, mask_lookup[c]).mask
                    self._masks[c] = False
                    self._masks[c].iloc[truncated_mask.index] = truncated_mask
                except Exception:
                    #TODO: Get rid of state
                    pass
        return self._masks

    @masks.setter
@@ -118,14 +107,14 @@ class Edge(dict, MutableMapping):
        pass

    def symmetry_check(self):
        if self.matches:
        if isinstance(self.matches, pd.DataFrame):
            mask = od.mirroring_test(self.matches)
            self.masks = ('symmetry', mask)
        else:
            raise AttributeError('No matches have been computed for this edge.')

    def ratio_check(self, clean_keys=[], **kwargs):
        if self.matches:
        if isinstance(self.matches, pd.DataFrame):
            matches, mask = self.clean(clean_keys)
            distance_mask = od.distance_ratio(matches, **kwargs)
            self.masks = ('ratio', distance_mask)
@@ -152,7 +141,7 @@ class Edge(dict, MutableMapping):
        autocnet.transformation.transformations.FundamentalMatrix

        """
        if not self.matches:
        if not isinstance(self.matches, pd.DataFrame):
            raise AttributeError('Matches have not been computed for this edge')
            return
        matches, mask = self.clean(clean_keys)
@@ -197,7 +186,7 @@ class Edge(dict, MutableMapping):
               Boolean array of the outliers
        """

        if self.matches:
        if isinstance(self.matches, pd.DataFrame):
            matches = self.matches
        else:
            raise AttributeError('Matches have not been computed for this edge')
@@ -321,7 +310,7 @@ class Edge(dict, MutableMapping):
                     of mask keys to be used to reduce the total size
                     of the matches dataframe.
        """
        if not self.matches:
        if not isinstance(self.matches, pd.DataFrame):
            raise AttributeError('This edge does not yet have any matches computed.')

        matches, mask = self.clean(clean_keys)
@@ -418,7 +407,7 @@ class Edge(dict, MutableMapping):
                                   returns the overlap area
                                   covered by the keypoints
        """
        if self.matches is None:
        if not isinstance(self.matches, pd.DataFrame):
            raise AttributeError('Edge needs to have features extracted and matched')
            return
        matches, mask = self.clean(clean_keys)
@@ -454,7 +443,7 @@ class Edge(dict, MutableMapping):
                     Of strings used to apply masks to omit correspondences

        """
        if self.matches is None:
        if not isinstance(self.matches, pd.DataFrame):
            raise AttributeError('Matches have not been computed for this edge')
        voronoi = cg.vor(self, clean_keys, **kwargs)
        self.matches = pd.concat([self.matches, voronoi[1]['vor_weights']], axis=1)
+43 −12
Changes for autocnet/graph/tests/test_edge.py: 43 added lines, 12 removed lines.
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ import numpy as np
import pandas as pd
from plio.io import io_gdal

from autocnet.matcher import outlier_detector as od
from autocnet.examples import get_path
from autocnet.graph.network import CandidateGraph
from autocnet.utils.utils import array_to_poly
@@ -45,24 +46,33 @@ class TestEdge(unittest.TestCase):

    def test_masks(self):
        self.assertIsInstance(self.edge.masks, pd.DataFrame)

        keypoint_matches = [[0, 0, 1, 4],
                            [0, 1, 1, 3],
        matches = [[0, 0, 1, 0],
                   [0, 1, 1, 1],
                   [0, 2, 1, 2],
                            [0, 3, 1, 1],
                            [0, 4, 1, 0]]

        # Test masks returns properly
        matches_df = pd.DataFrame(data=keypoint_matches, columns=['source_image', 'source_idx',
                                                                  'destination_image', 'destination_idx'])
                   [0, 3, 1, 3],
                   [0, 4, 1, 4]]
        matches_df = pd.DataFrame(data=matches,
                                  columns=['source_image', 'source_idx',
                                           'destination_image',
                                           'destination_idx'])
        e = edge.Edge()
        e.matches = matches_df

        # Test empty masks df on an edge with computed matches
        expected = pd.DataFrame(True, columns=['symmetry'],
                                index=matches_df.index)
        self.assertTrue(e.masks.equals(expected))
        self.assertTrue(expected.equals(e.masks))

        # Test the masks setter, changing a given row
        new_symmetry_rows = [True, False, True, False, True]
        e.masks = "symmetry", new_symmetry_rows

        self.assertEqual(new_symmetry_rows, list(e.masks.loc[:, "symmetry"]))

        # Test the masks setter, inserting a new row
        e.masks = "fundamental", new_symmetry_rows
        self.assertEqual(new_symmetry_rows, list(e.masks.loc[:, "fundamental"]))

    def test_masks_setter(self):
        e = edge.Edge()


    def test_compute_fundamental_matrix(self):
@@ -397,3 +407,24 @@ class TestEdge(unittest.TestCase):
        # If there are no matches, should raise attrib err
        with (self.assertRaises(AttributeError)):
            e.symmetry_check()

    def test_ratio_check(self):
        # Matches is init to None
        e = edge.Edge()
        # If there are no matches, should raise attrib err
        with (self.assertRaises(AttributeError)):
            e.ratio_check()

        # If there are matches...
        keypoint_matches = [[0, 0, 1, 4, 5],
                            [0, 1, 1, 3, 5],
                            [0, 2, 1, 2, 5],
                            [0, 3, 1, 1, 5],
                            [0, 4, 1, 0, 5]]

        matches_df = pd.DataFrame(data=keypoint_matches, columns=['source_image', 'source_idx',
                                                                  'destination_image', 'destination_idx', 'distance'])
        e.matches = matches_df
        expected = list(od.distance_ratio(matches_df))
        e.ratio_check()
        self.assertEqual(expected, list(e.masks["ratio"]))