Example #1
0
def test_linear_interpolate():
    p0 = np.array([-1., -1., -1.])
    p1 = np.array([1., 1., 1.])

    res = mm.linear_interpolate(p0, p1, 0.0)
    assert np.allclose(res, (-1., -1., -1.))
    res = mm.linear_interpolate(p0, p1, 0.25)
    assert np.allclose(res, (-0.5, -0.5, -0.5))
    res = mm.linear_interpolate(p0, p1, 0.5)
    assert np.allclose(res, (0., 0., 0.))
    res = mm.linear_interpolate(p0, p1, 0.75)
    assert np.allclose(res, (0.5, 0.5, 0.5))
    res = mm.linear_interpolate(p0, p1, 1.0)
    assert np.allclose(res, (1., 1., 1.))
Example #2
0
def test_linear_interpolate():
    p0 = np.array([-1.,-1.,-1.])
    p1 = np.array([1.,1.,1.])

    res = mm.linear_interpolate(p0, p1, 0.0)
    nt.assert_true(np.allclose(res, (-1., -1., -1.)))
    res = mm.linear_interpolate(p0, p1, 0.25)
    nt.assert_true(np.allclose(res, (-0.5, -0.5, -0.5)))
    res = mm.linear_interpolate(p0, p1, 0.5)
    nt.assert_true(np.allclose(res, (0., 0., 0.)))
    res = mm.linear_interpolate(p0, p1, 0.75)
    nt.assert_true(np.allclose(res, (0.5, 0.5, 0.5)))
    res = mm.linear_interpolate(p0, p1, 1.0)
    nt.assert_true(np.allclose(res, (1., 1., 1.)))
Example #3
0
def cross_section_at_fraction(segment, fraction):
    ''' Computes the point p along the line segment that connects the
    two ends of a segment p1p2 where |p1p| = fraction * |p1p2| along
    with the respective radius.
    Args:
        fraction: float between 0. and 1.

    Returns: tuple
        The 3D coordinates of the aforementioned point,
        Its respective radius
    '''
    return (mm.linear_interpolate(segment[0].value, segment[1].value, fraction),
            mm.interpolate_radius(segment[0].value[COLS.R], segment[1].value[COLS.R], fraction))
Example #4
0
def _add_coords(synapse, morphology):
    """Adds coordinates and direction fields to ``synapses`` via ``apply`` function."""
    is_pre = PRE_SECTION_ID in synapse.index and not pd.isnull(
        synapse[PRE_SECTION_ID])
    is_post = POST_SECTION_ID in synapse.index and not pd.isnull(
        synapse[POST_SECTION_ID])
    assert is_pre != is_post, 'Synapse must have either afferent or efferent section ids for the ' \
                              'morphology. It cant have both at the same time.'

    if is_post:
        sec_id = int(synapse[POST_SECTION_ID])
        seg_id = int(synapse[POST_SEGMENT_ID])
        seg_ofst = float(synapse[POST_SEGMENT_OFFSET])
        synapse['direction'] = 'afferent'
    else:
        sec_id = int(synapse[PRE_SECTION_ID])
        seg_id = int(synapse[PRE_SEGMENT_ID])
        seg_ofst = float(synapse[PRE_SEGMENT_OFFSET])
        synapse['direction'] = 'efferent'

    if sec_id == 0:
        # synapse is on soma
        p = morphology.soma.points[0]
        synapse['x'], synapse['y'], synapse['z'] = p[COLS.XYZ]
        # place synapse on surface of soma along Z axes so it won't be hidden by soma on the plot
        synapse['z'] += p[COLS.R]
        return synapse

    # NeuroM morphology counts sections from 0. Synapses count sections from 1 because section
    # id 0 is for soma.
    sec_id -= 1
    sec = morphology.sections[sec_id]
    assert sec_id == sec.id, f'Error. Synapse with section id {sec_id} must map to the same ' \
                             f'section id in `morphology` arg but maps to {sec.id}.'
    assert 0 <= seg_id <= len(sec.points), f'No such segment id {seg_id} for section id ' \
                                           f'{sec_id} of `morphology` arg'

    seg_p1, seg_p2 = sec.points[seg_id - 1], sec.points[seg_id]
    seg_len = morphmath.point_dist(seg_p1, seg_p2)
    coords = morphmath.linear_interpolate(seg_p1, seg_p2, seg_ofst / seg_len)
    synapse['x'], synapse['y'], synapse['z'] = coords
    return synapse
Example #5
0
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'''Get sections and segments by ID'''

import neurom as nm
from neurom import morphmath as mm
from neurom.core.dataformat import COLS


def get_segment(neuron, section_id, segment_id):
    '''Get a segment given a section and segment id

    Returns:
        array of two [x, y, z, r] points defining segment
    '''
    sec = neuron.sections[section_id]
    return sec.points[segment_id:segment_id + 2][:, COLS.XYZR]


if __name__ == '__main__':

    nrn = nm.load_neuron('test_data/h5/v1/Neuron.h5')

    seg = get_segment(nrn, 3, 2)
    print('Segment:\n', seg)
    print('Mid-point (x, y, z):\n', mm.linear_interpolate(seg[0], seg[1], 0.5))
    print('Mid-point R:\n',
          mm.interpolate_radius(seg[0][COLS.R], seg[1][COLS.R], 0.5))
Example #6
0
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

'''Get sections and segments by ID'''

import neurom as nm
from neurom import morphmath as mm
from neurom.core.dataformat import COLS


def get_segment(neuron, section_id, segment_id):
    '''Get a segment given a section and segment id

    Returns:
        array of two [x, y, z, r] points defining segment
    '''
    sec = neuron.sections[section_id]
    return sec.points[segment_id:segment_id + 2][:, 0:4]


if __name__ == '__main__':

    nrn = nm.load_neuron('test_data/h5/v1/Neuron.h5')

    seg = get_segment(nrn, 3, 2)
    print 'Segment:\n', seg
    print 'Mid-point (x, y, z):\n', mm.linear_interpolate(seg[0], seg[1], 0.5)
    print 'Mid-point R:\n', mm.interpolate_radius(seg[0][COLS.R], seg[1][COLS.R], 0.5)