Skip to content

Commit

Permalink
Add polar plots code and example
Browse files Browse the repository at this point in the history
  • Loading branch information
lidakanari committed Jun 21, 2018
1 parent d4d6cb3 commit 942fce0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
19 changes: 19 additions & 0 deletions examples/polar_plot_generation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import tmd
from view import polar_plots

filename = './'

pop = tmd.io.load_population(filename)

# Get the data for a single cell
res = polar_plots.get_histogram_polar_coordinates(pop.neurons[0], neurite_type='basal', N=30)

# Plot the data for a single cell
polar_plots.plot_polar_coordinates(res)

# Extract the polar plots of all neurons in the population and save images
# in a selected directory
for n in pop.neurons:
res = polar_plots.get_histogram_polar_coordinates(n, neurite_type='basal', N=30)
polar_plots.plot_polar_coordinates(res, output_path='./PolarPlots/', output_name=n.name.split('/')[-1])
plt.close()
49 changes: 49 additions & 0 deletions view/polar_plots.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import numpy as np
from matplotlib import pylab as plt

def get_histogram_polar_coordinates(neuron, neurite_type='basal', N=25):
'''
'''
def seg_angle(seg):
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(neuron, neurite_type):
segs = segs + tr.get_segments()

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

for tr in getattr(neuron, neurite_type):
lens = lens + tr.get_segment_lengths().tolist()

angles = np.array(angles)
lens = np.array(lens)

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

results = []

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

return results


def plot_polar_coordinates(input_data, output_name=None, output_path=None, output_format='png'):
'''
'''
fig = 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)

if output_path is not None:
plt.savefig(output_path + '/Polar_' + output_name, format=output_format)

0 comments on commit 942fce0

Please sign in to comment.