Skip to content

Commit

Permalink
Fix bugs in code and clean up input parameters in growers
Browse files Browse the repository at this point in the history
Change-Id: I6514a07a8d4a20f2a967d3e718b593aecf6cfa59
  • Loading branch information
lidakanari committed Jul 31, 2018
1 parent c9631dd commit 1338af8
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 17 deletions.
5 changes: 3 additions & 2 deletions tns/generate/algorithms/abstractgrower.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ class AbstractAlgo(object):
@abc.abstractmethod
def __init__(self,
input_data,
bif_method,
params,
start_point):
"""Abstract TreeGrower Algorithm initialization.
input_data: dictionary in which data used by the algorithm are stored
bif_method: bifurcation method, select from: bio_oriented, symmetric, directional
params: parameters needed for growth, it should include the bif_method
bifurcation method, select from: bio_oriented, symmetric, directional
"""

@abc.abstractmethod
Expand Down
8 changes: 5 additions & 3 deletions tns/generate/algorithms/basicgrower.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ class TrunkAlgo(AbstractAlgo):

def __init__(self,
input_data,
bif_method,
params,
start_point):
"""
input_data: saves all the data required for the growth
bif_method: selects the bifurcation method used for the growth
params: parameters needed for growth, it should include the bif_method
bifurcation method, select from: bio_oriented, symmetric, directional
"""
self.bif_method = bif_methods[bif_method]
self.bif_method = bif_methods[params["branching_method"]]
self.params = params
self.input_data = input_data
self.start_point = start_point

Expand Down
4 changes: 2 additions & 2 deletions tns/generate/algorithms/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
bif_methods = {'bio_oriented': rd.get_bif_bio_oriented,
'symmetric': rd.get_bif_symmetric,
'directional': rd.get_bif_directional,
'random': rd.get_bif_random}
'random': rd.get_bif_random,
'smooth': rd.get_bif_bio_smoothed}


def init_ph_angles(ph_angles):
Expand All @@ -18,4 +19,3 @@ def init_ph_angles(ph_angles):
bt_all = {round_num(p[1]): p[0] for p in ph_angles}

return bif, term, angles, bt_all

9 changes: 5 additions & 4 deletions tns/generate/algorithms/tmdgrower.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ class TMDAlgo(AbstractAlgo):

def __init__(self,
input_data,
bif_method,
params,
start_point):
"""
TMD basic grower
input_data: saves all the data required for the growth
bif_method: selects the bifurcation method used for the growth
params: parameters needed for growth, it should include the bif_method
bifurcation method, select from: bio_oriented, symmetric, directional
"""

self.bif_method = bif_methods[bif_method]
self.bif_method = bif_methods[params["branching_method"]]
self.params = params
self.ph_angles = sample.ph(input_data["persistence_diagram"])
self.start_point = start_point
self.bif = None
Expand Down
13 changes: 11 additions & 2 deletions tns/generate/section.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
from tns.morphmath import random_tree as rd
from tns.morphmath.sample import ph_prob

from scipy import stats

class SectionGrower(object):
'''Class for the section
Expand Down Expand Up @@ -76,6 +76,8 @@ def check_stop_ph(self, prob_function):
scale = self.params["scale_prob"]

currd = np.linalg.norm(np.subtract(self.points3D[-1], crit["ref"]))
# compute expected path length for a given target radial distance
# pathtarget = self.segs > 1.3 * np.abs(crit["bif"] - crit["term"])

# Ensure that the section has at least two points
if len(self.points3D) < 2:
Expand All @@ -87,6 +89,14 @@ def check_stop_ph(self, prob_function):
elif ph_prob(prob_function, crit["term"] - currd):
self.children = 0.
return False
# Checks in too long path length is generated
# elif pathtarget:
# # If target bif smaller
# if crit["bif"] <= crit["term"]:
# self.children = 2.
# else:
# self.children = 0.
# return False
else:
return True

Expand All @@ -108,7 +118,6 @@ def generate(self):
'''Creates a section with the selected parameters
until at least one stop criterion is fulfilled.
'''
from scipy import stats
prob_function = stats.expon(loc=0, scale=self.params["scale_prob"])

while self.check_stop_ph(prob_function):
Expand Down
3 changes: 2 additions & 1 deletion tns/generate/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self,
grow_meth = growth_algorithms[self.params["growth_method"]]

self.growth_algo = grow_meth(input_data=self.distr,
bif_method=self.params["branching_method"],
params=self.params,
start_point=self.point)

stop, num_sec = self.growth_algo.initialize()
Expand All @@ -54,6 +54,7 @@ def __init__(self,
direction=self.direction,
start_point=list(self.point),
stop=copy.deepcopy(stop),
process='major',
children=2 if num_sec > 1 else 0)

def add_section(self, parent, direction, start_point, stop, process=None, children=0):
Expand Down
6 changes: 3 additions & 3 deletions tns/morphmath/random_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ def get_bif_bio_smoothed(direction, angles):
'''Input: init_phi, init_theta, dphi, dtheta.
'''
def smoothing(ang):
if np.abs(ang) > np.pi/2.:
return ang/2
if np.abs(ang) > np.pi:
return np.abs(ang)/2
else:
return ang

Expand Down Expand Up @@ -100,7 +100,7 @@ def get_bif_directional(direction, angles):
dir1 = rt.vector_from_spherical(phi, theta)
dir2 = rt.vector_from_spherical(phi - phi1, theta - theta1)

return (np.array(dir1), np.array(dir2))
return (np.array(direction), np.array(dir2))


def get_bif_soma_repulsion(direction, angles, soma, curr_point):
Expand Down

0 comments on commit 1338af8

Please sign in to comment.