Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] ENH: Custom section colors in Cell.plot_morphology #646

Merged
merged 5 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions hnn_core/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,13 +655,20 @@ def parconnect_from_src(self, gid_presyn, nc_dict, postsyn,

return nc

def plot_morphology(self, ax=None, cell_types=None, show=True):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I don't think cell_types is used anywhere? @jasmainak

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this was a method of Network object so you can loop over the cell_types in a network objec ... I remember a student was working on this but not sure if it ever got merged

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think that ended up getting dropped, the tests are passing so I think it's safe to remove for now

def plot_morphology(self, ax=None, color=None, show=True):
"""Plot the cell morphology.

Parameters
----------
ax : instance of Axes3D
Matplotlib 3D axis
color : str | dict | None
Color of cell. If str, entire cell plotted with
color indicated by str. If dict, colors of individual sections
can be specified. Must have a key for every section in cell as
defined in the `Cell.sections` attribute.
| Ex: ``{'apical_trunk': 'r', 'soma': 'b', ...}``

show : bool
If True, show the plot

Expand All @@ -670,7 +677,7 @@ def plot_morphology(self, ax=None, cell_types=None, show=True):
axes : instance of Axes3D
The matplotlib 3D axis handle.
"""
return plot_cell_morphology(self, ax=ax, show=show)
return plot_cell_morphology(self, ax=ax, color=color, show=show)

def _update_end_pts(self):
""""Create cell and copy coordinates to Section.end_pts"""
Expand Down
18 changes: 18 additions & 0 deletions hnn_core/tests/test_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ def test_network_visualization():
with pytest.raises(ValueError, match='src_gid -1 not a valid cell ID'):
plot_cell_connectivity(net, conn_idx, src_gid=-1)

# Test morphology plotting
for cell_type in net.cell_types.values():
cell_type.plot_morphology()
cell_type.plot_morphology(color='r')

sections = list(cell_type.sections.keys())
section_color = {sect_name: f'C{idx}' for
idx, sect_name in enumerate(sections)}
cell_type.plot_morphology(color=section_color)

cell_type = net.cell_types['L2_basket']
with pytest.raises(ValueError):
cell_type.plot_morphology(color='z')
with pytest.raises(ValueError):
cell_type.plot_morphology(color={'soma': 'z'})
with pytest.raises(TypeError, match='color must be'):
cell_type.plot_morphology(color=123)

plt.close('all')

# test interactive clicking updates the position of src_cell in plot
Expand Down
21 changes: 19 additions & 2 deletions hnn_core/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ def _linewidth_from_data_units(ax, linewidth):
return linewidth * (length / value_range)


def plot_cell_morphology(cell, ax, show=True):
def plot_cell_morphology(cell, ax, color=None, show=True):
"""Plot the cell morphology.

Parameters
Expand All @@ -821,6 +821,13 @@ def plot_cell_morphology(cell, ax, show=True):
Matplotlib 3D axis
show : bool
If True, show the plot
color : str | dict | None
Color of cell. If str, entire cell plotted with
color indicated by str. If dict, colors of individual sections
can be specified. Must have a key for every section in cell as
defined in the `Cell.sections` attribute.

| Ex: ``{'apical_trunk': 'r', 'soma': 'b', ...}``

Returns
-------
Expand All @@ -834,6 +841,15 @@ def plot_cell_morphology(cell, ax, show=True):
plt.figure()
ax = plt.axes(projection='3d')

_validate_type(color, (str, dict, None), 'color')

if color is None:
section_colors = {section: 'b' for section in cell.sections.keys()}
if isinstance(color, str):
section_colors = {section: color for section in cell.sections.keys()}
if isinstance(color, dict):
section_colors = color

# Cell is in XZ plane
ax.set_xlim((cell.pos[1] - 250, cell.pos[1] + 150))
ax.set_zlim((cell.pos[2] - 100, cell.pos[2] + 1200))
Expand All @@ -849,7 +865,8 @@ def plot_cell_morphology(cell, ax, show=True):
xs.append(pt[0] + dx)
ys.append(pt[1] + dz)
zs.append(pt[2] + dy)
ax.plot(xs, ys, zs, 'b-', linewidth=linewidth)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'b-' is redundant now since the color 'b' is explicitly set when None is passed to color

ax.plot(xs, ys, zs, '-', linewidth=linewidth,
color=section_colors[sec_name])
ax.view_init(0, -90)
ax.axis('off')

Expand Down
Loading