Skip to content

Chih Ting's notes

FelixChihTing edited this page Oct 26, 2020 · 1 revision

Sprint1: Merge fit_spline_tree_invariant to neurons code

Oct.26,2020 pytest: test_loc()

The fit_spline_tree_invariant method in fit_spline.py does not specify input type and format,

def fit_spline_tree_invariant(self):
        """construct a spline tree based on the path lengths

        Returns:
            spline_tree (DiGraph): a parent tree with the longest path in the directed graph
        """

but its dependent methods requires inputs to be specific. For example:

def fit_spline_path(self, path):
        """calculate the knots, B-spline coefficients, and the degree of the spline according to the path

        Args:
            path (list): a list of nodes

        Returns:
            tck (tuple): (t,c,k) a tuple containing the vector of knots, the B-spline coefficients, and the degree of the spline.
            u (): An array of the values of the parameter.
        """

In fact, if we simply add nodes to the graph without specifying their attributes, fit_spline_tree_invariant() wouldn't work.

def test_loc():
    """Nodes should be defined in loc attribute
    """

    neuron=GeometricGraph()
    neuron.add_node(1)
    neuron.add_node(2)
    neuron.add_node(3)
    neuron.add_edge(1,2)
    neuron.add_edge(2,3)
    neuron.add_edge(3,1)
    spline_tree = neuron.fit_spline_tree_invariant()

The following error message will appear:

def path_length(self, path):
        """compute the distance between nodes along the path

        Args:
            path (list): list of nodes

        Returns:
            length (int): length between nodes
        """

        length = 0

        for i, node in enumerate(path):
            if i > 0:
                length = length + np.linalg.norm(
>                   self.nodes[node]["loc"] - self.nodes[path[i - 1]]["loc"]
                )
E                   KeyError: 'loc'

Because all the nodes are presumably defined under "loc" attribute in the path_length(path)(and other methods), the fit_spline_tree_invariant()cannot work. Since path_length(path) is the first method that requests the inputs from "loc", I added a raise in this method:

# check if loc is defined
hasloc=self.nodes[node].get("loc")
print(type(hasloc))
if hasloc is None:
    raise KeyError("Nodes are not defined under loc attribute")
else:
    length = length + np.linalg.norm(
        self.nodes[node]["loc"] - self.nodes[path[i - 1]]["loc"]
    )

Running the test again:

E                   KeyError: 'Nodes are not defined under loc attribute'