Ejemplo n.º 1
0
def circuit_example():
    """Example that shows how to plot a morphology with synapses from a Sonata circuit.

    To make this example work, you would need a proper SONATA circuit.
    """
    from bluepysnap import Circuit
    from bluepysnap.bbp import Synapse
    circuit = Circuit('/path/to/sonata_circuit_config.json')
    edge_properties = [
        'afferent_section_id',
        'afferent_segment_id',
        'afferent_segment_offset',
        Synapse.U_SYN,
        Synapse.D_SYN,
        Synapse.F_SYN,
        Synapse.G_SYNX,
    ]
    morph_id = 110
    synapses = circuit.edges['default'].afferent_edges(morph_id,
                                                       edge_properties)
    morph_filepath = circuit.nodes['All'].morph.get_filepath(morph_id)
    m = nm.load_morphology(morph_filepath)

    fig = morphology.draw(m, synapses)
    fig.show()
Ejemplo n.º 2
0
def NeuroM_section_to_NRN_compartment_paths(morph_path: Path):
    """Returns a dictionary NeuroM section id -> path of each compartment for the section.

    Path are formed by following the section points until the pathlength of the compartment is
    reached.

    Args:
        morph_path: the morphology path

    1) Compute the cumulative pathlength along the section segments_directions
    2) Get the compartment pathlengths (compartment are of equal pathlength in a given section)
    3) Compartment by compartment, follow the points until the compartment pathlength is reached


    Example for one section::

                       (1, 2) ------ (2, 2)
                          |
                          |
                          |
        (0, 0) ------- (1, 0)


    If n_compartments == 3, three paths are returned::

        [array([[0.        , 0.        , 0.        ],
                [1.        , 0.        , 0.        ],
                [1.        , 0.33333333, 0.        ]]),

         array([[1.        , 0.33333333, 0.        ],
                [1.        , 1.66666667, 0.        ]]),

         array([[1.        , 1.66666667, 0.        ],
                [1.        , 2.        , 0.        ],
                [2.        , 2.        , 0.        ]])]
    """
    NeuroM_cell = load_morphology(morph_path)
    NRN_neuron = get_NRN_cell(morph_path)
    NRN_sections = list(NRN_neuron.icell.all)

    mapping = NeuroM_section_to_NRN_section(morph_path)

    NeuroM_to_compartment_position_mapping = {}

    for section in NeuroM_cell.sections:
        if section.type == NeuriteType.soma:
            continue

        NRN_section = NRN_sections[mapping[section.id]]

        NeuroM_to_compartment_position_mapping[
            section.id] = _compartment_paths(section.points[:, COLS.XYZ],
                                             NRN_section.nseg)

    return NeuroM_to_compartment_position_mapping
Ejemplo n.º 3
0
def NeuroM_section_to_NRN_section(filename: Path):
    """Returns a mapping from NeuroM section IDs to NRN ones."""
    NeuroM_cell = load_morphology(filename)
    NRN_cell = get_NRN_cell(filename)

    mapping = {}

    NRN_sections = list(NRN_cell.icell.all)

    def is_soma(NRN_section):
        """Is the NRN section a soma section."""
        return NRN_section.name().endswith('.soma[0]')

    # Skip soma if exists
    counter = 1 if is_soma(NRN_sections[0]) else 0

    for NeuroM_section in iter_sections(NeuroM_cell,
                                        neurite_order=NeuriteIter.NRN):
        if _zero_length_section(NeuroM_section):
            mapping[NeuroM_section.id] = None

            if not NeuroM_section.children:
                L.debug(
                    'Zero length section without children (NeuroM section id: %s)',
                    NeuroM_section.id)
                continue

            L.debug('Zero length section with children')
            NRN_section = NRN_sections[counter]
            counter -= 1

        else:
            mapping[NeuroM_section.id] = counter
            NRN_section = NRN_sections[counter]

        L.debug('NeuroM section (%s) has been mapped to NRN section (%s)',
                NeuroM_section.id, mapping[NeuroM_section.id])

        # Skip single child NeuroM_section because they have already been
        # merged in the NeuroM morphology
        while _has_single_child(NRN_section):
            L.debug('Skipping single child')
            counter += 1
            NRN_section = NRN_section.children()[0]

        counter += 1

    _validate_section_mapping(NeuroM_cell, NRN_cell, mapping)
    return mapping
Ejemplo n.º 4
0
def plain_example():
    """Example that shows how to plot a neuron dendrogram with a plain synapses dataframe."""
    # Those properties are required in synapses dataframe for positioning
    required_synapse_properties = [
        consts.SOURCE_NODE_ID,
        consts.TARGET_NODE_ID,
        consts.POST_SECTION_ID,
        consts.POST_SECTION_POS,
        consts.PRE_SECTION_ID,
        consts.PRE_SECTION_POS,
    ]
    # or use plain strings
    # required_columns = ['@source_node', '@target_node',
    #                     'afferent_section_id', 'afferent_section_pos',
    #                     'efferent_section_id', 'efferent_section_pos']
    data = np.array([
        [0, 116, 4, 0.81408846, 3, 0.7344886],
        [0, 116, 5, 0.145983203, 4, 0.24454929],
        [0, 116, 3, 0.968469656, 1, 0.4290702],
        [116, 0, 2, 0.84480673, 1, 0.29180855],
        [116, 0, 2, 0.5815143, 1, 0.68261607],
    ])
    synapses = pd.DataFrame(columns=required_synapse_properties, data=data)
    synapses = synapses.astype({
        '@target_node': int,
        '@source_node': int,
        'afferent_section_id': int,
        'efferent_section_id': int
    })
    m = nm.load_morphology('dendrogram_plain_example.swc')
    fig = dendrogram.draw(m, synapses, 116)
    fig.show()

    # If you want to show additional data with synapses then just use additional columns in you
    # dataframe. These data properties can have any names.
    synapse_data_properties = [
        'u_syn', 'depression_time', 'facilitation_time', 'conductance'
    ]
    data = np.array([
        [0.16718547, 153.8097, 8.452671, 1.9140357],
        [0.16718547, 153.8097, 8.452671, 1.9140357],
        [0.16718547, 153.8097, 8.452671, 1.9140357],
        [0.29116565, 116.06434, 10.367496, 3.1585026],
        [0.29116565, 116.06434, 10.367496, 3.1585026],
    ])
    synapses_data = pd.DataFrame(columns=synapse_data_properties, data=data)
    synapses = pd.concat([synapses, synapses_data], axis=1)
    fig = dendrogram.draw(m, synapses, 116)
    fig.show()
Ejemplo n.º 5
0
def example_afferent():
    m = nm.load_morphology('dendrogram_plain_example.swc')
    data = np.array([
        [4, 0, 0.81408846, 'additional value'],
        [6, 2, 0.545983203, 'additional value'],
        [1, 0, 0.4290702, 'additional value'],
    ])
    columns = [
        consts.POST_SECTION_ID, consts.POST_SEGMENT_ID,
        consts.POST_SEGMENT_OFFSET, 'additional_data'
    ]
    synapses = pd.DataFrame(data, columns=columns)

    fig = morphology.draw(m, synapses)
    fig.show()
Ejemplo n.º 6
0
def example_afferent_efferent():
    m = nm.load_morphology('dendrogram_plain_example.swc')
    data = np.array([
        [4, 0, 0.81408846, np.nan, np.nan, np.nan],
        [6, 2, 0.145983203, np.nan, np.nan, np.nan],
        [np.nan, np.nan, np.nan, 1, 0, 0.4290702],
        [np.nan, np.nan, np.nan, 1, 0, 0.29180855],
        [np.nan, np.nan, np.nan, 1, 0, 0.68261607],
    ])
    columns = [
        consts.POST_SECTION_ID, consts.POST_SEGMENT_ID,
        consts.POST_SEGMENT_OFFSET, consts.PRE_SECTION_ID,
        consts.PRE_SEGMENT_ID, consts.PRE_SEGMENT_OFFSET
    ]
    synapses = pd.DataFrame(data, columns=columns)

    fig = morphology.draw(m, synapses)
    fig.show()
Ejemplo n.º 7
0
def circuit_example():
    """Example that shows how to plot a neuron dendrogram with synapses from a bluepysnap circuit.

    To make this example work, you would need a proper SONATA circuit.
    """
    from bluepysnap import Circuit
    from bluepysnap.sonata_constants import Edge
    from bluepysnap.bbp import Synapse
    circuit = Circuit('/path/to/sonata_circuit_config.json')
    # you can use `bluepysnap.sonata_constants.Edge` instead of `morph_tool.draw.consts`
    edge_properties = [
        Edge.SOURCE_NODE_ID,
        Edge.TARGET_NODE_ID,
        Edge.POST_SECTION_ID,
        Edge.POST_SECTION_POS,
        Edge.PRE_SECTION_ID,
        Edge.PRE_SECTION_POS,
        Synapse.U_SYN,
        Synapse.D_SYN,
        Synapse.F_SYN,
        Synapse.G_SYNX,
    ]
    source_node_id = 0
    target_node_id = 116
    synapses1 = circuit.edges['default'].pair_edges(source_node_id,
                                                    target_node_id,
                                                    edge_properties)
    synapses2 = circuit.edges['default'].pair_edges(target_node_id,
                                                    source_node_id,
                                                    edge_properties)
    synapses = pd.concat([synapses1, synapses2])

    morph_filepath = circuit.nodes['default'].morph.get_filepath(
        target_node_id)
    m = nm.load_morphology(morph_filepath)

    fig = dendrogram.draw(m, synapses, target_node_id)
    fig.show()
Ejemplo n.º 8
0
def _create_test_neuron():
    return nm.load_morphology(DATA / 'simple.swc')