Commit 09aef4a2 authored by Adam Paquette's avatar Adam Paquette
Browse files

Merge remote-tracking branch 'upstream/master'

ge aborts
parents 97fe6527 c8bc1e90
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -237,7 +237,7 @@ class GeoDataset(object):
                num_polygon_bytes = find_in_dict(polygon_pvl, 'Bytes')

                # I too dislike the additional open here.  Not sure a good option
                with open(self.file_name, 'r+') as f:
                with open(self.file_name, 'r') as f:
                    f.seek(start_polygon_byte - 1)
                    # Sloppy unicode to string because GDAL pukes on unicode
                    stream = str(f.read(num_polygon_bytes))
+18 −0
Original line number Diff line number Diff line
import unittest
import pandas as pd
import numpy as np

from .. import utils

class TestUtils(unittest.TestCase):
    def setUp(self):
        pass

    def test_slopes(self):
        x1 = pd.DataFrame({'x': np.arange(1, 11),
                           'y': np.arange(1, 11)})
        x2 = pd.DataFrame({'x': np.arange(6, 16),
                           'y': np.arange(11, 21)})

        slope = utils.calculate_slope(x1, x2)
        self.assertEqual(slope.slope[0], 2)
+21 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ import tempfile
import os
import fnmatch
import numpy as np
import pandas as pd

def create_dir(basedir=''):
    """
@@ -34,3 +35,23 @@ def file_search(searchdir,searchstring):
            filelist.append(os.path.join(root, filename))
    filelist=np.array(filelist)
    return filelist    

def calculate_slope(x1, x2):
    """
    Calculates the 2-dimensional slope between the points in two dataframes each containing two columns ['x', 'y']
    The slope is calculated from x1 to x2.

    Parameters
    ----------
    x1 : dataframe
         Each row is a point with columns ['x', 'y']
    x2 : dataframe
        Each row is a point with columns ['x', 'y']

    Returns
    -------
    : dataframe
      A dataframe with the slope between the points in x1 and x2 for each row.
    """
    slopes = (x2.y.values - x1.y.values)/(x2.x.values-x1.x.values)
    return pd.DataFrame(slopes, columns=['slope'])
+61 −8
Original line number Diff line number Diff line
@@ -354,13 +354,13 @@ class CandidateGraph(nx.Graph):
        """
        _, self.clusters = func(self, *args, **kwargs)

    def apply_func_to_edges(self, func, *args, graph_mask_keys=[], **kwargs):
    def apply_func_to_edges(self, function, *args, graph_mask_keys=[], **kwargs):
        """
        Iterates over edges using an optional mask and and applies the given function.
        If func is not an attribute of Edge, raises AttributeError
        Parameters
        ----------
        func : string
        function : obj
                   function to be called on every edge
        graph_mask_keys : list
                          of keys in graph_masks
@@ -372,17 +372,17 @@ class CandidateGraph(nx.Graph):
        else:
            edges_to_iter = self.edges()

        if not isinstance(func, str):
            func = func.__name__
        if not isinstance(function, str):
            function = function.__name__

        for s, d in edges_to_iter:
            curr_edge = self.get_edge_data(s, d)
            try:
                function = getattr(curr_edge, func)
                func = getattr(curr_edge, function)
            except:
                raise AttributeError(func, ' is not an attribute of Edge')
                raise AttributeError(function, ' is not an attribute of Edge')
            else:
                function(*args, **kwargs)
                func(*args, **kwargs)

    def minimum_spanning_tree(self):
        """
@@ -698,3 +698,56 @@ class CandidateGraph(nx.Graph):

        H.graph = self.graph
        return H

    def subgraph_from_matches(self):
        """
        Returns a sub-graph where all edges have matches.
        (i.e. images with no matches are removed)

        Returns
        -------
        : Object
          A networkX graph object
        """

        # get all edges that have matches
        matches = [(u, v) for u, v, edge in self.edges_iter(data=True)
                   if hasattr(edge, 'matches') and
                   not edge.matches.empty]

        return self.create_edge_subgraph(matches)

    def filter_nodes(self, func, *args, **kwargs):
        """
        Filters graph and returns a sub-graph from matches. Mimics
        python's filter() function

        Parameters
        ----------
        func : function which returns bool used to filter out nodes

        Returns
        -------
        : Object
          A networkX graph object

        """
        nodes = [n for n, d in self.nodes_iter(data=True) if func(d, *args, **kwargs)]
        return self.create_node_subgraph(nodes)

    def filter_edges(self, func, *args, **kwargs):
        """
        Filters graph and returns a sub-graph from matches. Mimics
        python's filter() function

        Parameters
        ----------
        func : function which returns bool used to filter out edges

        Returns
        -------
        : Object
          A networkX graph object
        """
        edges = [(u, v) for u, v, edge in self.edges_iter(data=True) if func(edge, *args, **kwargs)]
        return self.create_edge_subgraph(edges)
+24 −0
Original line number Diff line number Diff line
@@ -114,6 +114,30 @@ class TestCandidateGraph(unittest.TestCase):
        node_sub = g.create_node_subgraph([0,1])
        self.assertEqual(len(node_sub), 2)

    def test_filter(self):
        def edge_func(edge):
            return hasattr(edge, 'matches') and not edge.matches.empty

        graph = self.graph.copy()
        test_sub_graph = graph.create_node_subgraph([0, 1])
        test_sub_graph.extract_features(extractor_parameters={'nfeatures': 500})
        test_sub_graph.match_features(k=2)

        filtered_nodes = graph.filter_nodes(lambda node: hasattr(node, 'descriptors'))
        filtered_edges = graph.filter_edges(edge_func)

        self.assertEqual(filtered_nodes.number_of_nodes(), test_sub_graph.number_of_nodes())
        self.assertEqual(filtered_edges.number_of_edges(), test_sub_graph.number_of_edges())

    def test_subgraph_from_matches(self):
        test_sub_graph = self.graph.create_node_subgraph([0, 1])
        test_sub_graph.extract_features(extractor_parameters={'nfeatures': 500})
        test_sub_graph.match_features(k=2)

        sub_graph_from_matches = self.graph.subgraph_from_matches()

        self.assertEqual(test_sub_graph.edges(), sub_graph_from_matches.edges())

    def tearDown(self):
        pass

Loading