Skip to content

Commit

Permalink
Format examples
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-berchet committed Apr 16, 2024
1 parent c9ca160 commit 924665e
Show file tree
Hide file tree
Showing 14 changed files with 141 additions and 132 deletions.
28 changes: 14 additions & 14 deletions examples/density_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@
PACKAGE_DIR = Path(__file__).resolve().parent.parent


def extract_density(population, plane='xy', bins=100, neurite_type=NeuriteType.basal_dendrite):
def extract_density(population, plane="xy", bins=100, neurite_type=NeuriteType.basal_dendrite):
"""Extracts the 2d histogram of the center
coordinates of segments in the selected plane.
"""
segment_midpoints = np.array(
get_feat('segment_midpoints', population, neurite_type=neurite_type)
get_feat("segment_midpoints", population, neurite_type=neurite_type)
)
horiz = segment_midpoints[:, 'xyz'.index(plane[0])]
vert = segment_midpoints[:, 'xyz'.index(plane[1])]
horiz = segment_midpoints[:, "xyz".index(plane[0])]
vert = segment_midpoints[:, "xyz".index(plane[1])]
return np.histogram2d(np.array(horiz), np.array(vert), bins=(bins, bins))


Expand All @@ -60,10 +60,10 @@ def plot_density(
new_fig=True,
subplot=111,
levels=None,
plane='xy',
colorlabel='Nodes per unit area',
plane="xy",
colorlabel="Nodes per unit area",
labelfontsize=16,
color_map='Reds',
color_map="Reds",
no_colorbar=False,
threshold=0.01,
neurite_type=NeuriteType.basal_dendrite,
Expand All @@ -82,7 +82,7 @@ def plot_density(
H2 = np.ma.masked_array(H1, mask)

colormap = mpl.cm.get_cmap(color_map).copy()
colormap.set_bad(color='white', alpha=None)
colormap.set_bad(color="white", alpha=None)

plots = ax.contourf(
(xedges1[:-1] + xedges1[1:]) / 2,
Expand All @@ -96,9 +96,9 @@ def plot_density(
cbar = plt.colorbar(plots)
cbar.ax.set_ylabel(colorlabel, fontsize=labelfontsize)

kwargs['title'] = kwargs.get('title', '')
kwargs['xlabel'] = kwargs.get('xlabel', plane[0])
kwargs['ylabel'] = kwargs.get('ylabel', plane[1])
kwargs["title"] = kwargs.get("title", "")
kwargs["xlabel"] = kwargs.get("xlabel", plane[0])
kwargs["ylabel"] = kwargs.get("ylabel", plane[1])

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

Expand All @@ -109,10 +109,10 @@ def plot_neuron_on_density(
new_fig=True,
subplot=111,
levels=None,
plane='xy',
colorlabel='Nodes per unit area',
plane="xy",
colorlabel="Nodes per unit area",
labelfontsize=16,
color_map='Reds',
color_map="Reds",
no_colorbar=False,
threshold=0.01,
neurite_type=NeuriteType.basal_dendrite,
Expand Down
16 changes: 8 additions & 8 deletions examples/end_to_end_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def make_end_to_end_distance_plot(nb_segments, end_to_end_distance, neurite_type
plt.figure()
plt.plot(nb_segments, end_to_end_distance)
plt.title(neurite_type)
plt.xlabel('Number of segments')
plt.ylabel('End-to-end distance')
plt.xlabel("Number of segments")
plt.ylabel("End-to-end distance")
# uncomment to show
# plt.show()

Expand All @@ -79,24 +79,24 @@ def _dist(seg):

def main():
# load a neuron from an SWC file
filename = Path(PACKAGE_DIR, 'tests/data/swc/Neuron_3_random_walker_branches.swc')
filename = Path(PACKAGE_DIR, "tests/data/swc/Neuron_3_random_walker_branches.swc")
m = nm.load_morphology(filename)

# print mean end-to-end distance per neurite type
print(
'Mean end-to-end distance for axons: ',
"Mean end-to-end distance for axons: ",
mean_end_to_end_dist(n for n in m.neurites if n.type == nm.AXON),
)
print(
'Mean end-to-end distance for basal dendrites: ',
"Mean end-to-end distance for basal dendrites: ",
mean_end_to_end_dist(n for n in m.neurites if n.type == nm.BASAL_DENDRITE),
)
print(
'Mean end-to-end distance for apical dendrites: ',
"Mean end-to-end distance for apical dendrites: ",
mean_end_to_end_dist(n for n in m.neurites if n.type == nm.APICAL_DENDRITE),
)

print('End-to-end distance per neurite (nb segments, end-to-end distance, neurite type):')
print("End-to-end distance per neurite (nb segments, end-to-end distance, neurite type):")
for nrte in m.neurites:
# plot end-to-end distance for increasingly larger parts of neurite
calculate_and_plot_end_to_end_distance(nrte)
Expand All @@ -108,5 +108,5 @@ def main():
)


if __name__ == '__main__':
if __name__ == "__main__":
main()
4 changes: 2 additions & 2 deletions examples/extract_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def main():
find_optimal_distribution(population_directory, "section_lengths")
)

print(json.dumps(result, indent=2, separators=(',', ': '), cls=NeuromJSON))
print(json.dumps(result, indent=2, separators=(",", ": "), cls=NeuromJSON))


if __name__ == '__main__':
if __name__ == "__main__":
main()
4 changes: 2 additions & 2 deletions examples/features_graph_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
def stylize(ax, name, feature):
"""Stylization modifications to the plots"""
ax.set_ylabel(feature)
ax.set_title(name, fontsize='small')
ax.set_title(name, fontsize="small")


def histogram(neuron, feature, ax, bins=15, normed=True, cumulative=False):
Expand Down Expand Up @@ -104,5 +104,5 @@ def main():
)


if __name__ == '__main__':
if __name__ == "__main__":
main()
86 changes: 43 additions & 43 deletions examples/get_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ def stats(data):
min and max of data
"""
return {
'len': len(data),
'mean': np.mean(data),
'sum': np.sum(data),
'std': np.std(data),
'min': np.min(data),
'max': np.max(data),
"len": len(data),
"mean": np.mean(data),
"sum": np.sum(data),
"std": np.std(data),
"min": np.min(data),
"max": np.max(data),
}


Expand All @@ -66,15 +66,15 @@ def pprint_stats(data):


def main():
filename = Path(PACKAGE_DIR, 'tests/data/swc/Neuron.swc')
filename = Path(PACKAGE_DIR, "tests/data/swc/Neuron.swc")

# load a neuron from an SWC file
m = nm.load_morphology(filename)

# Get some soma information
# Soma radius and surface area
print("Soma radius", nm.get('soma_radius', m))
print("Soma surface area", nm.get('soma_surface_area', m))
print("Soma radius", nm.get("soma_radius", m))
print("Soma surface area", nm.get("soma_surface_area", m))

# Get information about neurites
# Most neurite data can be queried for a particular type of neurite.
Expand All @@ -85,48 +85,48 @@ def main():
# to warm up...

# number of neurites
print('Number of neurites (all):', nm.get('number_of_neurites', m))
print("Number of neurites (all):", nm.get("number_of_neurites", m))
print(
'Number of neurites (axons):',
nm.get('number_of_neurites', m, neurite_type=nm.NeuriteType.axon),
"Number of neurites (axons):",
nm.get("number_of_neurites", m, neurite_type=nm.NeuriteType.axon),
)
print(
'Number of neurites (apical dendrites):',
nm.get('number_of_neurites', m, neurite_type=nm.NeuriteType.apical_dendrite),
"Number of neurites (apical dendrites):",
nm.get("number_of_neurites", m, neurite_type=nm.NeuriteType.apical_dendrite),
)
print(
'Number of neurites (basal dendrites):',
nm.get('number_of_neurites', m, neurite_type=nm.NeuriteType.basal_dendrite),
"Number of neurites (basal dendrites):",
nm.get("number_of_neurites", m, neurite_type=nm.NeuriteType.basal_dendrite),
)

# number of sections
print('Number of sections:', nm.get('number_of_sections', m))
print("Number of sections:", nm.get("number_of_sections", m))
print(
'Number of sections (axons):',
nm.get('number_of_sections', m, neurite_type=nm.NeuriteType.axon),
"Number of sections (axons):",
nm.get("number_of_sections", m, neurite_type=nm.NeuriteType.axon),
)
print(
'Number of sections (apical dendrites):',
nm.get('number_of_sections', m, neurite_type=nm.NeuriteType.apical_dendrite),
"Number of sections (apical dendrites):",
nm.get("number_of_sections", m, neurite_type=nm.NeuriteType.apical_dendrite),
)
print(
'Number of sections (basal dendrites):',
nm.get('number_of_sections', m, neurite_type=nm.NeuriteType.basal_dendrite),
"Number of sections (basal dendrites):",
nm.get("number_of_sections", m, neurite_type=nm.NeuriteType.basal_dendrite),
)

# number of sections per neurite
print('Number of sections per neurite:', nm.get('number_of_sections_per_neurite', m))
print("Number of sections per neurite:", nm.get("number_of_sections_per_neurite", m))
print(
'Number of sections per neurite (axons):',
nm.get('number_of_sections_per_neurite', m, neurite_type=nm.NeuriteType.axon),
"Number of sections per neurite (axons):",
nm.get("number_of_sections_per_neurite", m, neurite_type=nm.NeuriteType.axon),
)
print(
'Number of sections per neurite (apical dendrites):',
nm.get('number_of_sections_per_neurite', m, neurite_type=nm.NeuriteType.apical_dendrite),
"Number of sections per neurite (apical dendrites):",
nm.get("number_of_sections_per_neurite", m, neurite_type=nm.NeuriteType.apical_dendrite),
)
print(
'Number of sections per neurite (basal dendrites):',
nm.get('number_of_sections_per_neurite', m, neurite_type=nm.NeuriteType.apical_dendrite),
"Number of sections per neurite (basal dendrites):",
nm.get("number_of_sections_per_neurite", m, neurite_type=nm.NeuriteType.apical_dendrite),
)

# OK, this is getting repetitive, so lets loop over valid neurite types.
Expand All @@ -135,42 +135,42 @@ def main():

# Section lengths for all and different types of neurite
for ttype in nm.NEURITE_TYPES:
sec_len = nm.get('section_lengths', m, neurite_type=ttype)
print('Section lengths (', ttype, '):', sep='')
sec_len = nm.get("section_lengths", m, neurite_type=ttype)
print("Section lengths (", ttype, "):", sep="")
pprint_stats(sec_len)

# Segment lengths for all and different types of neurite
for ttype in nm.NEURITE_TYPES:
seg_len = nm.get('segment_lengths', m, neurite_type=ttype)
print('Segment lengths (', ttype, '):', sep='')
seg_len = nm.get("segment_lengths", m, neurite_type=ttype)
print("Segment lengths (", ttype, "):", sep="")
pprint_stats(seg_len)

# Section radial distances for all and different types of neurite
# Careful! Here we need to pass tree type as a named argument
for ttype in nm.NEURITE_TYPES:
sec_rad_dist = nm.get('section_radial_distances', m, neurite_type=ttype)
print('Section radial distance (', ttype, '):', sep='')
sec_rad_dist = nm.get("section_radial_distances", m, neurite_type=ttype)
print("Section radial distance (", ttype, "):", sep="")
pprint_stats(sec_rad_dist)

# Section path distances for all and different types of neurite
# Careful! Here we need to pass tree type as a named argument
for ttype in nm.NEURITE_TYPES:
sec_path_dist = nm.get('section_path_distances', m, neurite_type=ttype)
print('Section path distance (', ttype, '):', sep='')
sec_path_dist = nm.get("section_path_distances", m, neurite_type=ttype)
print("Section path distance (", ttype, "):", sep="")
pprint_stats(sec_path_dist)

# Local bifurcation angles for all and different types of neurite
for ttype in nm.NEURITE_TYPES:
local_bifangles = nm.get('local_bifurcation_angles', m, neurite_type=ttype)
print('Local bifurcation angles (', ttype, '):', sep='')
local_bifangles = nm.get("local_bifurcation_angles", m, neurite_type=ttype)
print("Local bifurcation angles (", ttype, "):", sep="")
pprint_stats(local_bifangles)

# Remote bifurcation angles for all and different types of neurite
for ttype in nm.NEURITE_TYPES:
rem_bifangles = nm.get('remote_bifurcation_angles', m, neurite_type=ttype)
print('Local bifurcation angles (', ttype, '):', sep='')
rem_bifangles = nm.get("remote_bifurcation_angles", m, neurite_type=ttype)
print("Local bifurcation angles (", ttype, "):", sep="")
pprint_stats(rem_bifangles)


if __name__ == '__main__':
if __name__ == "__main__":
main()
24 changes: 12 additions & 12 deletions examples/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,24 @@ def histogram(neurons, feature, new_fig=True, subplot=111, normed=False, **kwarg
figure file.
"""

bins = kwargs.get('bins', 25)
cumulative = kwargs.get('cumulative', False)
bins = kwargs.get("bins", 25)
cumulative = kwargs.get("cumulative", False)

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

kwargs['xlabel'] = kwargs.get('xlabel', feature)
kwargs["xlabel"] = kwargs.get("xlabel", feature)

kwargs['ylabel'] = kwargs.get('ylabel', feature + ' fraction')
kwargs["ylabel"] = kwargs.get("ylabel", feature + " fraction")

kwargs['title'] = kwargs.get('title', feature + ' histogram')
kwargs["title"] = kwargs.get("title", feature + " histogram")

feature_values = [neurom.features.get(feature, neu) for neu in neurons]

neu_labels = [neu.name for neu in neurons]

ax.hist(feature_values, bins=bins, cumulative=cumulative, label=neu_labels, density=normed)

kwargs['no_legend'] = len(neu_labels) == 1
kwargs["no_legend"] = len(neu_labels) == 1

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

Expand Down Expand Up @@ -149,16 +149,16 @@ def population_histogram(pops, feature, new_fig=True, normed=False, subplot=111,
figure file.
"""

bins = kwargs.get('bins', 25)
cumulative = kwargs.get('cumulative', False)
bins = kwargs.get("bins", 25)
cumulative = kwargs.get("cumulative", False)

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

kwargs['xlabel'] = kwargs.get('xlabel', feature)
kwargs["xlabel"] = kwargs.get("xlabel", feature)

kwargs['ylabel'] = kwargs.get('ylabel', feature + ' fraction')
kwargs["ylabel"] = kwargs.get("ylabel", feature + " fraction")

kwargs['title'] = kwargs.get('title', feature + ' histogram')
kwargs["title"] = kwargs.get("title", feature + " histogram")

pops_feature_values = population_feature_values(pops, feature)

Expand All @@ -168,7 +168,7 @@ def population_histogram(pops, feature, new_fig=True, normed=False, subplot=111,
pops_feature_values, bins=bins, cumulative=cumulative, label=pops_labels, density=normed
)

kwargs['no_legend'] = len(pops_labels) == 1
kwargs["no_legend"] = len(pops_labels) == 1

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

Expand Down
Loading

0 comments on commit 924665e

Please sign in to comment.