Skip to content

Commit

Permalink
Optimize path_distance computation: use path_distances_2
Browse files Browse the repository at this point in the history
  • Loading branch information
lidakanari committed Dec 14, 2018
1 parent 2f0dc5b commit b42c5de
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
19 changes: 10 additions & 9 deletions examples/distances_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@
phs2 = [tmd.methods.get_ph_neuron(n, neurite_type='basal') for n in pop2.neurons]

# Normalize the limits
xlims, ylims = tmd.analysis.define_limits(phs1 + phs2)
xlims, ylims = tmd.analysis.get_limits(phs1 + phs2)

# Create average images for populations
imgs1 = [tmd.analysis.persistence_image_data(p, xlims=xlims, ylims=ylims) for p in phs1]
IMG1 = tmd.analysis.average_image(phs1, xlims=xlims, ylims=ylims)
imgs2 = [tmd.analysis.persistence_image_data(p, xlims=xlims, ylims=ylims) for p in phs2]
IMG2 = tmd.analysis.average_image(phs2, xlims=xlims, ylims=ylims)
imgs1 = [tmd.analysis.get_persistence_image_data(p, xlims=xlims, ylims=ylims) for p in phs1]
IMG1 = tmd.analysis.get_average_persistence_image(phs1, xlims=xlims, ylims=ylims)
imgs2 = [tmd.analysis.get_persistence_image_data(p, xlims=xlims, ylims=ylims) for p in phs2]
IMG2 = tmd.analysis.get_average_persistence_image(phs2, xlims=xlims, ylims=ylims)

# You can plot the images if you want to create pretty figures
average_figure1 = view.plot.plot_img_basic(IMG1, title='', xlims=xlims, ylims=ylims, cmap=cm.jet)
average_figure2 = view.plot.plot_img_basic(IMG2, title='', xlims=xlims, ylims=ylims, cmap=cm.jet)
average_figure1 = view.common.plot_img_basic(IMG1, title='', xlims=xlims, ylims=ylims, cmap=cm.jet)
average_figure2 = view.common.plot_img_basic(IMG2, title='', xlims=xlims, ylims=ylims, cmap=cm.jet)

# Create the diffence between the two images
DIMG = tmd.analysis.img_diff_data(IMG1, IMG2) # subtracts IMG2 from IMG1 so anything red IMG1 has more of it and anything blue IMG2 has more of it - or that's how it is supposed to be :)
DIMG = tmd.analysis.get_image_diff_data(IMG1, IMG2) # subtracts IMG2 from IMG1 so anything red IMG1 has more of it and anything blue IMG2 has more of it - or that's how it is supposed to be :)

# Plot the difference between them
diff_image = view.plot.plot_img_basic(DIMG, vmin=-1.0, vmax=1.0) # vmin, vmax important to see changes
diff_image = view.common.plot_img_basic(DIMG, vmin=-1.0, vmax=1.0) # vmin, vmax important to see changes
# Quantify the absolute distance between the two averages
dist = np.sum(np.abs(DIMG))
16 changes: 6 additions & 10 deletions examples/extract_ph.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Example to extract the persistence diagram from a neuronal tree

# Step 1: Import the tmd module

import tmd

# Step 2: Load your morphology
Expand All @@ -22,22 +21,19 @@
ph_basal = tmd.methods.get_ph_neuron(neu, neurite_type='basal')

# Step 6: Plot the extracted topological data with three different ways
import view
from tmd.view import view, plot

# Visualize the neuron
view.view.neuron(neu)
view.neuron(neu)

# Visualize a selected neurite type or multiple of them
view.view.neuron(neu, neurite_type=['apical'])
view.neuron(neu, neurite_type=['apical'])

# Visualize the persistence diagram
view.plot.ph_diagram(ph_apical)
plot.diagram(ph_apical)

# Visualize the persistence barcode
view.plot.barcode(ph_apical)
plot.barcode(ph_apical)

# Visualize the persistence image
view.plot.ph_image(ph_apical)

# Create an overview figure for the topology of a tree

plot.persistence_image(ph_apical)
1 change: 1 addition & 0 deletions tmd/Tree/Tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Tree(object):
from tmd.Tree.methods import get_point_radial_distances_time
from tmd.Tree.methods import get_point_weighted_radial_distances
from tmd.Tree.methods import get_point_path_distances
from tmd.Tree.methods import get_point_path_distances_2
from tmd.Tree.methods import get_point_projection
from tmd.Tree.methods import get_point_section_lengths
from tmd.Tree.methods import get_point_section_branch_orders
Expand Down
26 changes: 24 additions & 2 deletions tmd/Tree/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def _rd_w(p1, p2, w=(1., 1., 1.), normed=True):
'''
if normed:
w = (_np.array(w) / _np.linalg.norm(w))

return _np.dot(w, (_np.subtract(p1, p2)))


Expand Down Expand Up @@ -96,7 +95,7 @@ def get_segment_lengths(tree):
return seg_len


# Points features
# Points features to be used for topological extraction
def get_point_radial_distances(self, point=None, dim='xyz'):
'''Tree method to get radial distances from a point.
If point is None, the soma surface -defined by
Expand Down Expand Up @@ -180,6 +179,21 @@ def path_length(seg_id):
return _np.array([path_length(i) for i in range(size(self))])


def get_point_path_distances_2(self):
'''Tree method to get path distances from the root.
'''
import copy

seg_len = get_segment_lengths(self)
path_lengths = _np.append(0, copy.deepcopy(seg_len))
children = get_children(self)

for i in children.keys():
path_lengths[children[i]] = path_lengths[children[i]] + path_lengths[i]

return path_lengths


def get_point_section_lengths(self):
'''Tree method to get section lengths.
'''
Expand Down Expand Up @@ -365,6 +379,14 @@ def get_way_to_root(tree, sec_id=0):
return way


def get_children(tree):
'''Returns a dictionary of children
for each node of the tree
'''
from collections import OrderedDict
return OrderedDict({i: _np.where(tree.p == i)[0] for i in xrange(len(tree.p))})


# PCA
def get_pca(self, plane='xy', component=0):
'''Returns the i-th principal
Expand Down

0 comments on commit b42c5de

Please sign in to comment.