Esempio n. 1
0
def test_move_positions():
    origin = [10, -10]
    positions = {1: [0, 0], 2: [3, -3]}
    moved_positions = dm.move_positions(positions, origin)
    assert list(positions.keys()) == list(moved_positions.keys())
    assert [10, -10] == moved_positions[1].tolist()
    assert [13, -13] == moved_positions[2].tolist()
Esempio n. 2
0
def test_move_positions():
    origin = [10, -10]
    positions = {1: [0, 0], 2: [3, -3]}
    moved_positions = dm.move_positions(positions, origin)
    nt.assert_list_equal(list(positions.keys()), list(moved_positions.keys()))
    nt.assert_list_equal([10, -10], moved_positions[1].tolist())
    nt.assert_list_equal([13, -13], moved_positions[2].tolist())
Esempio n. 3
0
def draw(morphology, synapses=None, neuron_node_id=None):
    """Draw dendrogram with synapses.

    Args:
        morphology (Neurite|Morphology): a Morphology instance of NeuroM package.
        synapses (DataFrame): synapses dataframe.
        neuron_node_id (int|None): node id of ``morphology``. If None then it is taken from
            ``synapses[TARGET_NODE_ID]``.

    Returns:
        plotly.graph_objects.Figure: plotly figure

    Example
        .. literalinclude:: ../../../examples/dendrogram.py
            :pyobject: plain_example

    """
    assert (is_integer_dtype(synapses[TARGET_NODE_ID].dtype) and
            is_integer_dtype(synapses[SOURCE_NODE_ID].dtype) and
            is_integer_dtype(synapses[POST_SECTION_ID].dtype) and
            is_integer_dtype(synapses[PRE_SECTION_ID].dtype)), \
        'Section ids and nodes columns of `synapses` arg must be integers'

    dendrogram = SynDendrogram(morphology)
    positions = layout_dendrogram(dendrogram, np.array([0, 0]))
    w, h = get_size(positions)
    positions = move_positions(positions, np.array([.5 * w, 0]))
    if not synapses.empty:
        if neuron_node_id is None:
            neuron_node_id = _get_default_neuron_node_id(synapses)
        _position_synapses(positions, synapses, neuron_node_id)
        synapses['synapse_id'] = synapses.index.values
        # convert 'target node' to string for discrete colormap
        synapses = synapses.astype({TARGET_NODE_ID: str})
        fig = px.scatter(synapses,
                         x='x',
                         y='y',
                         color=TARGET_NODE_ID,
                         color_discrete_map=_get_synapse_colormap(synapses),
                         hover_data=synapses.columns)
    else:
        fig = graph_objects.Figure()
    fig.update_xaxes(range=[-.05 * w, 1.05 * w], zeroline=False)
    fig.update_yaxes(range=[-.05 * h, 1.05 * h], zeroline=False)
    shapes = _get_draw_shapes(dendrogram, positions)
    fig.update_layout(shapes=shapes)
    _add_neurite_legend(fig, dendrogram)
    return fig
Esempio n. 4
0
def draw(neuron, synapses=None, neuron_node_id=None):
    """Draw dendrogram with synapses.

    Args:
        neuron (Neurite|Neuron): a Neurite or a Neuron instance of NeuroM package.
        synapses (DataFrame): synapses dataframe.
        neuron_node_id (int|None): node id of ``neuron``. If None then it is taken from
            ``synapses[TARGET_NODE_ID]``.

    Returns:
        plotly.graph_objects.Figure: plotly figure
    """
    synapses = synapses.astype({
        '@target_node': int,
        '@source_node': int,
        'afferent_section_id': int,
        'efferent_section_id': int
    })
    dendrogram = SynDendrogram(neuron)
    positions = layout_dendrogram(dendrogram, np.array([0, 0]))
    w, h = get_size(positions)
    positions = move_positions(positions, np.array([.5 * w, 0]))
    if not synapses.empty:
        if neuron_node_id is None:
            neuron_node_id = _get_default_neuron_node_id(synapses)
        _position_synapses(positions, synapses, neuron_node_id)
        synapses['synapse_id'] = synapses.index.values
        # convert 'target node' to string for discrete colormap
        synapses = synapses.astype({TARGET_NODE_ID: str})
        fig = px.scatter(synapses,
                         x='x',
                         y='y',
                         color=TARGET_NODE_ID,
                         color_discrete_map=_get_synapse_colormap(synapses),
                         hover_data=synapses.columns)
    else:
        fig = graph_objects.Figure()
    fig.update_xaxes(range=[-.05 * w, 1.05 * w], zeroline=False)
    fig.update_yaxes(range=[-.05 * h, 1.05 * h], zeroline=False)
    shapes = _get_draw_shapes(dendrogram, positions)
    fig.update_layout(shapes=shapes)
    _add_neurite_legend(fig, dendrogram)
    return fig
Esempio n. 5
0
def plot_dendrogram(ax, obj, show_diameters=True):
    """Plots Dendrogram of `obj`.

    Args:
        ax: matplotlib axes
        obj (neurom.Neuron, neurom.Tree): neuron or tree
        show_diameters (bool): whether to show node diameters or not
    """
    dendrogram = Dendrogram(obj)
    positions = layout_dendrogram(dendrogram, np.array([0, 0]))
    w, h = get_size(positions)
    positions = move_positions(positions, np.array([.5 * w, 0]))
    ax.set_xlim([-.05 * w, 1.05 * w])
    ax.set_ylim([-.05 * h, 1.05 * h])
    ax.set_title('Morphology Dendrogram')
    ax.set_xlabel('micrometers (um)')
    ax.set_ylabel('micrometers (um)')
    shapes = _get_dendrogram_shapes(dendrogram, positions, show_diameters)
    ax.add_collection(PatchCollection(shapes, match_original=True))

    ax.set_aspect('auto')
    ax.legend(handles=_get_dendrogram_legend(dendrogram))