From 4cce74e954433886cdc0f472ae7cfe017713d63a Mon Sep 17 00:00:00 2001 From: gnayuy Date: Tue, 26 May 2020 04:06:13 -0700 Subject: [PATCH 1/8] add pia/wm validation and to prune edge points if needed --- neuron_morphology/transforms/geometry.py | 29 ++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/neuron_morphology/transforms/geometry.py b/neuron_morphology/transforms/geometry.py index 75b6417d..db2d67b3 100644 --- a/neuron_morphology/transforms/geometry.py +++ b/neuron_morphology/transforms/geometry.py @@ -36,6 +36,35 @@ def get_vertices_from_two_lines(line1: List[Tuple], line2: List[Tuple]): if side1.crosses(side2): line2.reverse() + # validate the pia/wm does not cross the side lines + prune = True + while prune: + prune = False + + # update lines + line1_str = geo.LineString(line1) + line2_str = geo.LineString(line2) + + side1 = geo.LineString([line1[0], line2[-1]]) + side2 = geo.LineString([line1[-1], line2[0]]) + + # prune the edge points if there are intersections of side and pia/wm + if side1.crosses(line1_str): + line1.pop(0) + prune = True + + if side1.crosses(line2_str): + line2.pop(-1) + prune = True + + if side2.crosses(line1_str): + line1.pop(-1) + prune = True + + if side2.crosses(line2_str): + line2.pop(0) + prune = True + vertices = line1 + line2 + [line1[0]] return vertices From b236b05ef7fa5b68d14dbbfff3787cc9da6b5b0a Mon Sep 17 00:00:00 2001 From: gnayuy Date: Tue, 26 May 2020 09:01:14 -0700 Subject: [PATCH 2/8] let the pia/wm boundary check and fix be a single func --- neuron_morphology/transforms/geometry.py | 52 ++++++++++++++++-------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/neuron_morphology/transforms/geometry.py b/neuron_morphology/transforms/geometry.py index db2d67b3..24ee6c3d 100644 --- a/neuron_morphology/transforms/geometry.py +++ b/neuron_morphology/transforms/geometry.py @@ -13,29 +13,18 @@ def get_ccw_vertices_from_two_lines(line1: List[Tuple], line2: List[Tuple]): """ return get_ccw_vertices(get_vertices_from_two_lines(line1, line2)) -def get_vertices_from_two_lines(line1: List[Tuple], line2: List[Tuple]): +def prune_two_lines(line1: List[Tuple], line2: List[Tuple]): """ - Generates circular vertices from two lines + check the boundary to avoid intersections with side lines Parameters - ---------- - line1, line2: List of coordinates describing two lines - - Returns - ------- - vertices of the simple polygon created from line 1 and 2 - (first vertex = last vertex) - - 1-2-3-4 - 5-6-7-8 -> [1-2-3-4-8-7-6-5-1] + ---------- + line1, line2: List of coordinates describing two lines + Returns + ------- + line1, line2: boundary pruned if needed """ - side1 = geo.LineString([line1[0], line2[-1]]) - side2 = geo.LineString([line1[-1], line2[0]]) - - if side1.crosses(side2): - line2.reverse() - # validate the pia/wm does not cross the side lines prune = True while prune: @@ -65,6 +54,33 @@ def get_vertices_from_two_lines(line1: List[Tuple], line2: List[Tuple]): line2.pop(0) prune = True + return line1, line2 + +def get_vertices_from_two_lines(line1: List[Tuple], line2: List[Tuple]): + """ + Generates circular vertices from two lines + + Parameters + ---------- + line1, line2: List of coordinates describing two lines + + Returns + ------- + vertices of the simple polygon created from line 1 and 2 + (first vertex = last vertex) + + 1-2-3-4 + 5-6-7-8 -> [1-2-3-4-8-7-6-5-1] + + """ + side1 = geo.LineString([line1[0], line2[-1]]) + side2 = geo.LineString([line1[-1], line2[0]]) + + if side1.crosses(side2): + line2.reverse() + + line1, line2 = prune_two_lines(line1, line2) + vertices = line1 + line2 + [line1[0]] return vertices From 1a12a443e849a77f6d065b8d4066bba400b9e66c Mon Sep 17 00:00:00 2001 From: gnayuy Date: Tue, 26 May 2020 09:22:34 -0700 Subject: [PATCH 3/8] add unit test for boundary check and fix func prunt_two_lines --- tests/transforms/test_geometry.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/transforms/test_geometry.py b/tests/transforms/test_geometry.py index 50ccba8a..a8bd73b8 100644 --- a/tests/transforms/test_geometry.py +++ b/tests/transforms/test_geometry.py @@ -45,4 +45,13 @@ def test_ccw_both_cw(self): line1 = [(0, 1), (1, 1)] line2 = [(1, 0), (0, 0)] result = geo.get_ccw_vertices_from_two_lines(line1, line2) - self.assertEqual(result, [(0, 1), (0, 0), (1, 0), (1, 1), (0, 1)]) \ No newline at end of file + self.assertEqual(result, [(0, 1), (0, 0), (1, 0), (1, 1), (0, 1)]) + + def test_ccw_boundary_check(self): + """ + line1 (1, 1), (0, 1), (0.5, 1.5) -> (1, 1), (0, 1) + """ + line1 = [(1, 1), (0, 1), (0.5, 1.5)] + line2 = [(0, 0), (1, 0)] + line1_pruned, line2_pruned = geo.prune_two_lines(line1, line2) + self.assertEqual(line1_pruned, [(1, 1), (0, 1)]) From 3e3d1bce0407176068f82dfa0a46e81ceabb254f Mon Sep 17 00:00:00 2001 From: gnayuy Date: Tue, 26 May 2020 09:41:45 -0700 Subject: [PATCH 4/8] change the unit test func name --- tests/transforms/test_geometry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/transforms/test_geometry.py b/tests/transforms/test_geometry.py index a8bd73b8..bb2c9806 100644 --- a/tests/transforms/test_geometry.py +++ b/tests/transforms/test_geometry.py @@ -47,7 +47,7 @@ def test_ccw_both_cw(self): result = geo.get_ccw_vertices_from_two_lines(line1, line2) self.assertEqual(result, [(0, 1), (0, 0), (1, 0), (1, 1), (0, 1)]) - def test_ccw_boundary_check(self): + def test_prune_two_lines(self): """ line1 (1, 1), (0, 1), (0.5, 1.5) -> (1, 1), (0, 1) """ From ab74d4ab3b7bdac079197bf1e06db7f109babf01 Mon Sep 17 00:00:00 2001 From: gnayuy Date: Tue, 26 May 2020 10:11:22 -0700 Subject: [PATCH 5/8] add warnings if the lines are pruned --- neuron_morphology/transforms/geometry.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/neuron_morphology/transforms/geometry.py b/neuron_morphology/transforms/geometry.py index 24ee6c3d..d3bc5f0f 100644 --- a/neuron_morphology/transforms/geometry.py +++ b/neuron_morphology/transforms/geometry.py @@ -2,7 +2,7 @@ """ from typing import List, Tuple, Callable - +import warnings from shapely import geometry as geo @@ -54,6 +54,9 @@ def prune_two_lines(line1: List[Tuple], line2: List[Tuple]): line2.pop(0) prune = True + if prune: + warnings.warn("lines are modified by pruning boundaries", UserWarning) + return line1, line2 def get_vertices_from_two_lines(line1: List[Tuple], line2: List[Tuple]): From b3f0d803d6d365f2acb2d9bf1fe9498e3f09c09e Mon Sep 17 00:00:00 2001 From: gnayuy Date: Tue, 26 May 2020 11:10:22 -0700 Subject: [PATCH 6/8] modify the warnings --- neuron_morphology/transforms/geometry.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/neuron_morphology/transforms/geometry.py b/neuron_morphology/transforms/geometry.py index d3bc5f0f..b22be21d 100644 --- a/neuron_morphology/transforms/geometry.py +++ b/neuron_morphology/transforms/geometry.py @@ -41,21 +41,22 @@ def prune_two_lines(line1: List[Tuple], line2: List[Tuple]): if side1.crosses(line1_str): line1.pop(0) prune = True + warnings.warn("line1 are modified by pruning boundaries", line1, UserWarning) if side1.crosses(line2_str): line2.pop(-1) prune = True + warnings.warn("line2 are modified by pruning boundaries", line2, UserWarning) if side2.crosses(line1_str): line1.pop(-1) prune = True + warnings.warn("line1 are modified by pruning boundaries", line1, UserWarning) if side2.crosses(line2_str): line2.pop(0) prune = True - - if prune: - warnings.warn("lines are modified by pruning boundaries", UserWarning) + warnings.warn("line2 are modified by pruning boundaries", line2, UserWarning) return line1, line2 From 7e167e4aa60f62e8615a9aa8973dc2e19fea1943 Mon Sep 17 00:00:00 2001 From: gnayuy Date: Tue, 26 May 2020 11:13:14 -0700 Subject: [PATCH 7/8] update warnings --- neuron_morphology/transforms/geometry.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/neuron_morphology/transforms/geometry.py b/neuron_morphology/transforms/geometry.py index b22be21d..a6437a19 100644 --- a/neuron_morphology/transforms/geometry.py +++ b/neuron_morphology/transforms/geometry.py @@ -41,22 +41,21 @@ def prune_two_lines(line1: List[Tuple], line2: List[Tuple]): if side1.crosses(line1_str): line1.pop(0) prune = True - warnings.warn("line1 are modified by pruning boundaries", line1, UserWarning) if side1.crosses(line2_str): line2.pop(-1) prune = True - warnings.warn("line2 are modified by pruning boundaries", line2, UserWarning) if side2.crosses(line1_str): line1.pop(-1) prune = True - warnings.warn("line1 are modified by pruning boundaries", line1, UserWarning) if side2.crosses(line2_str): line2.pop(0) prune = True - warnings.warn("line2 are modified by pruning boundaries", line2, UserWarning) + + if prune: + warnings.warn("lines are modified by pruning boundaries", line1, line2, UserWarning) return line1, line2 From 3e495836495de6b41dc2421b4440d86ccdab09cf Mon Sep 17 00:00:00 2001 From: gnayuy Date: Tue, 26 May 2020 11:16:53 -0700 Subject: [PATCH 8/8] update warnings --- neuron_morphology/transforms/geometry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neuron_morphology/transforms/geometry.py b/neuron_morphology/transforms/geometry.py index a6437a19..90b419dd 100644 --- a/neuron_morphology/transforms/geometry.py +++ b/neuron_morphology/transforms/geometry.py @@ -55,7 +55,7 @@ def prune_two_lines(line1: List[Tuple], line2: List[Tuple]): prune = True if prune: - warnings.warn("lines are modified by pruning boundaries", line1, line2, UserWarning) + warnings.warn(f"lines are modified \nline1: {line1}\nline2: {line2}", UserWarning) return line1, line2