Skip to content

Commit

Permalink
Clean up pep n pylint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lidakanari committed Feb 23, 2018
1 parent a724985 commit 7e9f6b8
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 200 deletions.
2 changes: 1 addition & 1 deletion tmd/Neuron/Neuron.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def append_tree(self, new_tree, td):
"""
from tmd.Tree import Tree
import numpy as np
#from tmd.utils import tree_type as td
# from tmd.utils import tree_type as td

if isinstance(new_tree, Tree.Tree):

Expand Down
20 changes: 9 additions & 11 deletions tmd/Neuron/tests/test_neuron.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from tmd.Neuron import Neuron
from tmd.Soma import Soma
from tmd.Tree import Tree
from tmd.utils import tree_type as td

soma_test = Soma.Soma([0.],[0.],[0.],[12.])
soma_test1 = Soma.Soma([0.],[0.],[0.],[6.])
Expand All @@ -16,7 +17,7 @@

neu_test = Neuron.Neuron()
neu_test.set_soma(soma_test)
neu_test.append_tree(apical_test)
neu_test.append_tree(apical_test, td)

def test_neuron_init_():
neu1 = Neuron.Neuron()
Expand All @@ -43,21 +44,21 @@ def test_copy_neuron():
def test_neuron_is_equal():
neu1 = Neuron.Neuron()
neu1.set_soma(soma_test)
neu1.append_tree(apical_test)
neu1.append_tree(apical_test, td)
nt.ok_(neu1.is_equal(neu_test))
neu1 = Neuron.Neuron()
neu1.set_soma(soma_test1)
neu1.append_tree(apical_test)
neu1.append_tree(apical_test, td)
nt.ok_(not neu1.is_equal(neu_test))
neu1 = Neuron.Neuron()
neu1.set_soma(soma_test)
neu1.append_tree(basal_test)
neu1.append_tree(basal_test, td)
nt.ok_(not neu1.is_equal(neu_test))

def test_neuron_is_same():
neu1 = Neuron.Neuron()
neu1.set_soma(soma_test)
neu1.append_tree(apical_test)
neu1.append_tree(apical_test, td)
nt.ok_(neu1.is_same(neu_test))
neu1.name = 'test_not_same'
nt.ok_(not neu1.is_same(neu_test))
Expand All @@ -69,14 +70,11 @@ def test_neuron_set_soma():

def test_append_tree():
neu1 = Neuron.Neuron()
neu1.append_tree(apical_test)
neu1.append_tree(apical_test, td)
nt.ok_(len(neu1.neurites) == 1)
neu1.append_tree(basal_test)
neu1.append_tree(axon_test)
neu1.append_tree(basal_test, td)
neu1.append_tree(axon_test, td)
nt.ok_(len(neu1.neurites) == 3)
nt.ok_(len(neu1.basal) == 1)
nt.ok_(len(neu1.axon) == 1)
nt.ok_(len(neu1.apical) == 1)
nt.ok_(neu1.basal[0].is_equal(basal_test))
nt.ok_(neu1.axon[0].is_equal(axon_test))
nt.ok_(neu1.apical[0].is_equal(apical_test))
11 changes: 0 additions & 11 deletions tmd/Neuron/tests/test_neuron_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,3 @@

# Filenames for testing
sample_file = os.path.join(DATA_PATH, 'sample.swc')

neu1 = io.load_neuron(sample_file)

def test_size():
nt.ok_(neu1.size() == 2)
nt.ok_(neu1.size(neurite_type='axon') == 1)

def test_section_lengths():
neu1.get_section_lengths()
neu1.get_section_lengths(neurite_type='basal')

12 changes: 7 additions & 5 deletions tmd/Population/Population.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def append_neuron(self, new_neuron):
if isinstance(new_neuron, Neuron.Neuron):
self.neurons.append(new_neuron)

def extract_ph(self, neurite_type='all', output_folder='./', feature='radial_distances'):
def extract_ph(self, neurite_type='all', output_folder='./',
feature='radial_distances'):
"""Extract the persistent homology of all
neurites in the population and saves
them in files according to the tree type.
Expand Down Expand Up @@ -60,7 +61,8 @@ def try_except(tree, ntree, feature, output_folder, ttype='basal'):
_ = [try_except(ax, enax, feature, output_folder, ttype=neurite_type)
for enax, ax in enumerate(getattr(self, neurite_type + 's'))]

def extract_ph_names(self, neurite_type='all', output_folder='./', feature='radial_distances'):
def extract_ph_names(self, neurite_type='all', output_folder='./',
feature='radial_distances'):
"""Extract the persistent homology of all
neurites in the population and saves
them in files according to the tree type.
Expand Down Expand Up @@ -96,7 +98,8 @@ def try_except(tree, ntree, feature, output_folder, ttype='basal'):
ttype=neurite_type + '_' + n.name.split('/')[-1])
for enax, ax in enumerate(getattr(n, neurite_type + 's'))] for n in self.neurons]

def extract_ph_neurons(self, neurite_type='all', output_folder='./', feature='radial_distances'):
def extract_ph_neurons(self, neurite_type='all', output_folder='./',
feature='radial_distances'):
"""Extract the persistent homology of all
neurites in the population and saves
them in files according to the tree type.
Expand All @@ -113,8 +116,7 @@ def try_except(neuron, feature, output_folder, ttype=neurite_type):
output_file=os.path.join(output_folder, neuron.name + '.txt'))

except ValueError:
print tree
print neuron.name

_ = [try_except(n, feature, output_folder, ttype=neurite_type)
for n in self.neurons]

2 changes: 1 addition & 1 deletion tmd/Topology/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def sort_ph(ph, reverse=True):
"""
ph_sort = []

for ip, p in enumerate(ph):
for p in ph:
ph_sort.append([p[0], p[1], np.abs(p[0] - p[1])])

ph_sort.sort(key=lambda x: x[2], reverse=reverse)
Expand Down
62 changes: 21 additions & 41 deletions tmd/Topology/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

import numpy as np


def sort_ph(ph):
'''Sorts the persistence diagram
so that birth is always before death.
'''
for i in ph:
for i in ph:
i.sort()


Expand Down Expand Up @@ -78,14 +79,10 @@ def get_persistence_diagram(tree, feature='radial_distances', **kwargs):
mx = np.argmax(abs(rd[c]))
mx_id = c[mx]

#alive.remove(mx_id)
c = np.delete(c, mx)

for ci in c:
ph.append([rd[ci], rd[p]])
#alive.remove(ci)

#alive.append(p)

rd[p] = rd[mx_id]

Expand All @@ -94,16 +91,17 @@ def get_persistence_diagram(tree, feature='radial_distances', **kwargs):
return ph


def get_persistence_diagram_rotation(tree, feature='radial_distances', plane='xy', **kwargs):
def get_persistence_diagram_rotation(tree, feature='radial_distances', **kwargs):
'''Method to extract ph from tree that contains mutlifurcations'''

ph = get_persistence_diagram(tree, feature='radial_distances', **kwargs)
ph = get_persistence_diagram(tree, feature=feature, **kwargs)

tr_pca = tree.get_pca()

def rotation(x, y, angle=0.0):
return [np.cos(angle)*x - np.sin(angle)*y,
np.sin(angle)*x + np.cos(angle)*y]
'''Rotates coordinates x-y to the selected angle'''
return [np.cos(angle) * x - np.sin(angle) * y,
np.sin(angle) * x + np.cos(angle) * y]

ph_rot = [rotation(i[0], i[1], angle=np.arctan2(*tr_pca)) for i in ph]

Expand All @@ -121,23 +119,19 @@ def phi_theta(u, v):
phi2 = np.arctan2(v[1], v[0])
theta2 = np.arccos(v[2] / np.linalg.norm(v))

delta_phi = phi2 - phi1 #np.abs(phi1 - phi2)
delta_theta = theta2 - theta1 #np.abs(theta1 - theta2)

#dphi = delta_phi if delta_phi < np.pi else 2 * np.pi - delta_phi #np.mod(delta_phi, 2 * np.pi)
#dtheta = delta_theta if delta_theta < np.pi else 2 * np.pi - delta_theta #np.mod(delta_theta, np.pi) #
delta_phi = phi2 - phi1 # np.abs(phi1 - phi2)
delta_theta = theta2 - theta1 # np.abs(theta1 - theta2)

return delta_phi, delta_theta # dphi, dtheta


def get_angles(tree, beg, end, parents, children):
def get_angles(tree, beg, parents, children):
"""Returns the angles between all the triplets (parent, child1, child2)
of the tree"""

angles = [[0,0,0,0],]
angles = [[0, 0, 0, 0], ]

for i,b in enumerate(beg[1:]):
print i,b
for b in beg[1:]:

dirP = tree.get_direction_between(start_id=parents[b], end_id=b)

Expand All @@ -150,8 +144,6 @@ def get_angles(tree, beg, end, parents, children):
phi1, theta1 = phi_theta(dirP, dirU)
phi2, theta2 = phi_theta(dirP, dirV)

#print phi1, phi2

if np.abs(phi1) < np.abs(phi2):
dphi = phi1
dtheta = theta1
Expand All @@ -161,8 +153,6 @@ def get_angles(tree, beg, end, parents, children):
dtheta = theta2
delta_phi, delta_theta = phi_theta(dirV, dirU)

#print dphi, dtheta, delta_phi, delta_theta

angles.append([dphi, dtheta, delta_phi, delta_theta])

return angles
Expand All @@ -188,7 +178,7 @@ def get_ph_angles(tree, feature='radial_distances', **kwargs):

children = {b: end[np.where(beg == b)[0]] for b in np.unique(beg)}

angles = get_angles(tree, beg, end, parents, children)
angles = get_angles(tree, beg, parents, children)

while len(np.where(active)[0]) > 1:
alive = list(np.where(active)[0])
Expand All @@ -204,20 +194,15 @@ def get_ph_angles(tree, feature='radial_distances', **kwargs):
mx = np.argmax(abs(rd[c]))
mx_id = c[mx]

#alive.remove(mx_id)
c = np.delete(c, mx)

for ci in c:
angID = np.array(angles)[np.where(beg==p)[0][0]]
angID = np.array(angles)[np.where(beg == p)[0][0]]
ph.append([rd[ci], rd[p], angID[0], angID[1], angID[2], angID[3]])

#alive.remove(ci)

#alive.append(p)

rd[p] = rd[mx_id]

ph.append([rd[np.where(active)[0][0]], 0, None, None, None, None]) # Add the last alive component
ph.append([rd[np.where(active)[0][0]], 0, None, None, None, None])

return ph

Expand Down Expand Up @@ -259,17 +244,12 @@ def get_ph_radii(tree, feature='radial_distances', **kwargs):
mx = np.argmax(abs(rd[c]))
mx_id = c[mx]

#alive.remove(mx_id)
c = np.delete(c, mx)

for ci in c:
radiiID = np.array(radii)[np.where(beg==p)[0][0]]
radiiID = np.array(radii)[np.where(beg == p)[0][0]]
ph.append([rd[ci], rd[p], radiiID])

#alive.remove(ci)

#alive.append(p)

rd[p] = rd[mx_id]

ph.append([rd[np.where(active)[0][0]], 0, radii[beg[0]]]) # Add the last alive component
Expand Down Expand Up @@ -312,10 +292,10 @@ def get_ph_neuron_rot(neuron, feature='radial_distances', neurite_type='all', **


def extract_ph(tree, feature='radial_distances', output_file='test.txt',
function='get_persistence_diagram', sort=False, **kwargs):
sort=False, **kwargs):
'''Extracts persistent homology from tree'''

ph = eval(function)(tree, feature=feature, **kwargs)
ph = get_persistence_diagram(tree, feature=feature, **kwargs)

if sort:
sort_ph(ph)
Expand All @@ -326,16 +306,16 @@ def extract_ph(tree, feature='radial_distances', output_file='test.txt',


def extract_ph_neuron(neuron, feature='radial_distances', output_file=None,
function='get_ph_neuron', neurite_type='all', sort=False, **kwargs):
neurite_type='all', sort=False, **kwargs):
'''Extracts persistent homology from tree'''

ph = eval(function)(neuron, feature=feature, neurite_type='all', **kwargs)
ph = get_ph_neuron(neuron, feature=feature, neurite_type='all', **kwargs)

if sort:
sort_ph(ph)

if output_file is None:
output_file = 'PH_' + n.name + '_' + neurite_type + '.txt'
output_file = 'PH_' + neuron.name + '_' + neurite_type + '.txt'

write_ph(ph, output_file)

Expand Down
15 changes: 6 additions & 9 deletions tmd/Topology/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,13 @@ def get_apical_point(ph):
"""
B = np.sort(get_bifurcations(ph))

fig = plt.figure()
plt.hist(B, bins=len(ph) / 4)
# fig = plt.figure()
# plt.hist(B, bins=len(ph) / 4)

heights, bins = np.histogram(B, bins=len(ph) / 4)
empty_bins = np.where(heights == 0)[0]
consecutive_empty_bins = np.split(empty_bins, np.where(np.diff(empty_bins) != 1)[0]+1)

consecutive_empty_bins = np.split(empty_bins, np.where(np.diff(empty_bins) != 1)[0] + 1)
max_separation = np.argmax([len(i) for i in consecutive_empty_bins])

#print consecutive_empty_bins[max_separation]

separation = consecutive_empty_bins[max_separation][-1]

return B[np.where(B > bins[separation + 1])[0][0]]
Expand All @@ -60,7 +56,8 @@ def nosify(var, noise=0.1):
F.write(towrite)
F.close()
'''
from scipy import stats
var_new = np.zeros(len(var))
for i,v in enumerate(var):
var_new[i] = stats.norm.rvs(v,noise)
for i, v in enumerate(var):
var_new[i] = stats.norm.rvs(v, noise)
return var_new

0 comments on commit 7e9f6b8

Please sign in to comment.