Skip to content

Commit

Permalink
Improvements and new plots
Browse files Browse the repository at this point in the history
  • Loading branch information
lidakanari committed Mar 28, 2018
1 parent ef3d5c0 commit f3e34ad
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 11 deletions.
23 changes: 20 additions & 3 deletions tmd/Population/Population.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,26 @@ def __init__(self, name='Pop'):
'''
self.neurons = []
self.name = name
self.apicals = []
self.axons = []
self.basals = []

@property
def axons(self):
return [a for n in self.neurons for a in n.axon]

@property
def apicals(self):
return [a for n in self.neurons for a in n.apical]

@property
def basals(self):
return [a for n in self.neurons for a in n.basal]

@property
def neurites(self):
return self.apicals + self.axons + self.basals

@property
def dendrites(self):
return self.apicals + self.basals

def append_neuron(self, new_neuron):
"""
Expand Down
12 changes: 12 additions & 0 deletions tmd/Topology/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,15 @@ def get_image_max_diff(Z1, Z2):
diff = np.max(np.abs(img2 - img1))

return diff


def transform_to_length(ph, direction=False):
'''Transforms a persistence diagram into
a (start_point, length) equivalent diagram.
'''
if not direction:
return [[i[0], np.abs(i[1] - i[0])] for i in ph]
else:
return [[i[0], i[1] - i[0]] for i in ph]


4 changes: 0 additions & 4 deletions tmd/io/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,4 @@ def load_population(input_directory, tree_types=None):
pop.append_neuron(load_neuron(os.path.join(input_directory, i),
tree_types=tree_types))

pop.apicals = [ap for n in pop.neurons for ap in n.apical]
pop.axons = [ax for n in pop.neurons for ax in n.axon]
pop.basals = [bas for n in pop.neurons for bas in n.basal]

return pop
36 changes: 32 additions & 4 deletions view/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def ph_diagram(ph, new_fig=True, subplot=False, color='b', alpha=1.0, **kwargs):
return _cm.plot_style(fig=fig, ax=ax, **kwargs)


def gaussian_kernel(ph, new_fig=True, subplot=111, xlims=None, ylims=None, **kwargs):
def gaussian_kernel(ph, new_fig=True, subplot=111, xlims=None, ylims=None, norm_factor=None, **kwargs):
'''Plots the gaussian kernel
of the ph diagram that is given.
'''
Expand All @@ -144,7 +144,10 @@ def gaussian_kernel(ph, new_fig=True, subplot=111, xlims=None, ylims=None, **kwa
kernel = stats.gaussian_kde(values)
positions = _np.vstack([X.ravel(), Y.ravel()])
Z = _np.reshape(kernel(positions).T, X.shape)
Zn = Z / _np.max(Z)

if norm_factor is None:
norm_factor = _np.max(Z)
Zn = Z / norm_factor

fig, ax = _cm.get_figure(new_fig=new_fig, subplot=subplot)

Expand All @@ -156,7 +159,8 @@ def gaussian_kernel(ph, new_fig=True, subplot=111, xlims=None, ylims=None, **kwa
return Z, _cm.plot_style(fig=fig, ax=ax, **kwargs)


def ph_image(ph, new_fig=True, subplot=111, xlims=None, ylims=None, masked=False, cmap=_cm.plt.cm.jet, **kwargs):
def ph_image(ph, new_fig=True, subplot=111, xlims=None, ylims=None, masked=False,
norm_factor=None, cmap=_cm.plt.cm.jet, **kwargs):
'''Plots the gaussian kernel
of the ph diagram that is given.
'''
Expand All @@ -173,7 +177,10 @@ def ph_image(ph, new_fig=True, subplot=111, xlims=None, ylims=None, masked=False
kernel = stats.gaussian_kde(values)
positions = _np.vstack([X.ravel(), Y.ravel()])
Z = _np.reshape(kernel(positions).T, X.shape)
Zn = Z / _np.max(Z)

if norm_factor is None:
norm_factor = _np.max(Z)
Zn = Z / norm_factor

fig, ax = _cm.get_figure(new_fig=new_fig, subplot=subplot)

Expand Down Expand Up @@ -462,3 +469,24 @@ def plot_average(ph_list, new_fig=True, subplot=111, xlims=None, ylims=None, bin


return Zn, _cm.plot_style(fig=fig, ax=ax, **kwargs)


def start_length_plot(ph, direction=False, new_fig=True, subplot=False, color='b', alpha=1.0, **kwargs):
'''Plots the transformed ph diagram that
represents lengths and starting points of
a component.
'''
ph_transformed = _tm.analysis.transform_to_length(ph, direction=direction)

# Initialization of matplotlib figure and axes.
fig, ax = _cm.get_figure(new_fig=new_fig, subplot=subplot)

for p in ph:

ax.scatter(p[0], p[1], c=color, edgecolors='black', alpha=alpha)

kwargs['title'] = kwargs.get('title', 'Transformed Persistence diagram')
kwargs['xlabel'] = kwargs.get('xlabel', 'Start of the component')
kwargs['ylabel'] = kwargs.get('ylabel', 'Length of the component')

return _cm.plot_style(fig=fig, ax=ax, **kwargs)
73 changes: 73 additions & 0 deletions view/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,40 @@ def _get_default(variable, **kwargs):
return kwargs.get(variable, default[variable])



def _get_polar_data(population, neurite_type='neurites', bins=20):
'''Extracts the data to plot the polar length distribution
of a neuron or a population of neurons.
'''

def seg_angle(seg):
'''mean x, y coordinates of a segment'''
mean_x = _np.mean([seg[0][0], seg[1][0]])
mean_y = _np.mean([seg[0][1], seg[1][1]])
return _np.arctan2(mean_y, mean_x)

segs = []
for tr in getattr(population, neurite_type):
segs = segs + tr.get_segments()

angles = _np.array([seg_angle(s) for s in segs])

lens = []
for tr in getattr(population, neurite_type):
lens = lens + tr.get_segment_lengths().tolist()
lens = _np.array(lens)

step = 2 * _np.pi / bins
ranges = [[i * step - _np.pi, (i + 1) * step - _np.pi] for i in xrange(bins)]

results = []

for r in ranges:
results.append(r + [_np.sum(lens[_np.where((angles > r[0]) & (angles < r[1]))[0]])])

return results


def trunk(tr, plane='xy', new_fig=True, subplot=False, hadd=0.0, vadd=0.0, N=10, **kwargs):
'''Generates a 2d figure of the trunk = first N segments of the tree.
Expand Down Expand Up @@ -1350,3 +1384,42 @@ def density_cloud(obj, new_fig=True, subplot=111, new_axes=True,
soma(neu.soma, new_fig=False)

return _cm.plot_style(fig=fig, ax=ax, **kwargs)


def polar_plot(population, neurite_type='neurites', bins=20):
'''
'''
input_data = _get_polar_data(population, neurite_type=neurite_type, bins=bins)

fig = _cm.plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True)

theta = _np.array(input_data)[:,0]
radii = _np.array(input_data)[:,2] / _np.max(input_data)
width = 2 * _np.pi / len(input_data)
bars = ax.bar(theta, radii, width=width, bottom=0.0, alpha=0.8)


def polar_plot_custom_color(population, bins=25, apical_color='purple', basal_color='r',
edgecolor=None, alpha=0.8):
'''
'''
fig = _cm.plt.figure()
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True)

input_data1 = _get_polar_data(population, neurite_type='basals', bins=bins)
input_data2 = _get_polar_data(population, neurite_type='apicals', bins=bins)

maximum = _np.max(_np.array(input_data1)[:,2].tolist() + _np.array(input_data2)[:,2].tolist())

theta = _np.array(input_data1)[:,0]
radii = _np.array(input_data1)[:,2] / maximum
width = 2 * _np.pi / len(input_data1)
bars = ax.bar(theta, radii, width=width, edgecolor=edgecolor,
bottom=0.0, alpha=alpha, color=basal_color)

theta = _np.array(input_data2)[:,0]
radii = _np.array(input_data2)[:,2] / maximum
width = 2 * _np.pi / len(input_data2)
bars = ax.bar(theta, radii, width=width, edgecolor=edgecolor,
bottom=0.0, alpha=alpha, color=apical_color)

0 comments on commit f3e34ad

Please sign in to comment.