예제 #1
0
def pn_fig(fig,
           loc,
           l,
           cdict,
           xlim=[0., .5],
           ylim=[0, 3.5],
           xticks=range(0, 51, 5),
           yticks=np.arange(0., 1.1, 0.2),
           clr='blue',
           label=''):
    nest.Connect(l, l, cdict)

    ax = fig.add_subplot(loc)

    conns = nest.GetConnections(l)
    dist = np.array([
        nest.Distance(l[l.index(s)], l[l.index(t)])
        for s, t in zip(conns.sources(), conns.targets())
    ])
    ax.hist(dist, bins=50, histtype='stepfilled', density=True)
    r = np.arange(0., 0.51, 0.01)

    plt.plot(r,
             2 * np.pi * r * (1 - 2 * r) * 12 / np.pi,
             'r-',
             lw=3,
             zorder=-10)

    ax.set_xlim(xlim)
    ax.set_ylim(ylim)
    """ax.set_xticks(xticks)
    ax.set_yticks(yticks)"""
    # ax.set_aspect(100, 'box')
    ax.set_xlabel('Source-target distance d')
    ax.set_ylabel('Connection probability pconn(d)')
    def _target_distances(self):
        '''Return distances from source node to connected nodes.'''

        # Distance from source node to all nodes in target population
        dist = nest.Distance(self._driver, self._lt)

        # Target nodes
        connections = nest.GetConnections(source=self._driver)
        target_nodes = connections.get('target')

        target_dist = []

        # target_nodes and lt will both be sorted, so we can iterate through
        # both to see if they match, and put the correct distance in a list
        # containig target distances if true.
        counter = 0
        for indx, nc in enumerate(self._lt):
            if nc.get('global_id') == target_nodes[counter]:
                target_dist.append(dist[indx])
                counter += 1
                # target_nodes might be shorter than lt
                if counter == len(target_nodes):
                    break

        return target_dist
예제 #3
0
    def _target_distances(self):
        """Return distances from source node to connected nodes."""

        # Distance from source node to all nodes in target population
        dist = np.array(nest.Distance(self._driver, self._lt))

        # Target nodes
        connections = nest.GetConnections(source=self._driver)
        target_array = np.array(connections.target)

        # Convert lt node IDs to a NumPy array
        lt_array = np.array(self._lt.tolist())

        # Pick distance values of connected targets only
        target_dist = dist[np.isin(lt_array, target_array)]

        return target_dist
예제 #4
0
    def test_Distance(self):
        """Interface check on distance calculations."""
        lshape = [5, 4]
        nest.ResetKernel()
        l = nest.Create('iaf_psc_alpha',
                        positions=nest.spatial.grid(shape=lshape))

        # node IDs -> node IDs, all displacements must be zero here
        d = nest.Distance(l, l)
        self.assertEqual(len(d), len(l))
        self.assertTrue(all([dd == 0. for dd in d]))

        # single node ID -> node IDs
        d = nest.Distance(l[:1], l)
        self.assertEqual(len(d), len(l))
        self.assertTrue(all([isinstance(dd, float) for dd in d]))
        self.assertTrue(all([dd >= 0. for dd in d]))

        # node IDs -> single node ID
        d = nest.Distance(l, l[:1])
        self.assertEqual(len(d), len(l))
        self.assertTrue(all([isinstance(dd, float) for dd in d]))
        self.assertTrue(all([dd >= 0. for dd in d]))

        # Distance between node ID 1 and 6. They are on the same y-axis, and
        # directly next to each other on the x-axis, so the distance should be
        # approximately dx. The same is true for distance between node ID 6 and 1.
        d = nest.Distance(l[:1], l[4:5])
        dx = 1. / lshape[0]
        self.assertAlmostEqual(d[0], dx, 3)

        d = nest.Distance(l[4:5], l[:1])
        self.assertAlmostEqual(d[0], dx, 3)

        # Distance between node ID 1 and 2. They are on the same x-axis, and
        # directly next to each other on the y-axis, so the distance should be
        # approximately dy. The same is true for distance between node ID 2 and 1.
        d = nest.Distance(l[:1], l[1:2])
        dy = 1. / lshape[1]
        self.assertAlmostEqual(d[0], dy, 3)

        d = nest.Distance(l[1:2], l[:1])
        self.assertAlmostEqual(d[0], dy, 3)

        # Test that we get correct results if to_arg and from_arg are from two
        # different layers
        l2 = nest.Create('iaf_psc_alpha',
                         positions=nest.spatial.grid(shape=lshape))
        d = nest.Distance(l[:1], l2[4:5])
        dx = 1. / lshape[0]
        self.assertAlmostEqual(d[0], dx, 3)

        d = nest.Distance(l[4:5], l2[:1])
        self.assertAlmostEqual(d[0], dx, 3)

        d = nest.Distance(l[:1], l2[1:2])
        dy = 1. / lshape[1]
        self.assertAlmostEqual(d[0], dy, 3)

        d = nest.Distance(l[1:2], l2[:1])
        self.assertAlmostEqual(d[0], dy, 3)

        # Test that an error is thrown if to_arg and from_arg have different
        # size.
        with self.assertRaises(ValueError):
            d = nest.Distance(l[1:3], l[2:7])

        # position -> node IDs
        d = nest.Distance([
            [0.0, 0.0],
        ], l)
        self.assertEqual(len(d), len(l))
        self.assertTrue(all([isinstance(dd, float) for dd in d]))
        self.assertTrue(all([dd >= 0. for dd in d]))

        # position -> node IDs
        d = nest.Distance(np.array([0.0, 0.0]), l)
        self.assertEqual(len(d), len(l))
        self.assertTrue(all([isinstance(dd, float) for dd in d]))
        self.assertTrue(all([dd >= 0. for dd in d]))

        # positions -> node IDs
        d = nest.Distance([np.array([0.0, 0.0])] * len(l), l)
        self.assertEqual(len(d), len(l))
        self.assertTrue(all([isinstance(dd, float) for dd in d]))
        self.assertTrue(all([dd >= 0. for dd in d]))
예제 #5
0
# extract position information, transpose to list of x, y and z positions
xpos, ypos, zpos = zip(*nest.GetPosition(l1))
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(xpos, ypos, zpos, s=15, facecolor='b')

# Gaussian connections in full box volume [-0.75,0.75]**3
nest.Connect(l1, l1,
             {'rule': 'pairwise_bernoulli',
              'p': nest.spatial_distributions.gaussian(nest.spatial.distance, std=0.25),
              'allow_autapses': False,
              'mask': {'box': {'lower_left': [-0.75, -0.75, -0.75],
                               'upper_right': [0.75, 0.75, 0.75]}}})

# show connections from center element
# sender shown in red, targets in green
ctr = nest.FindCenterElement(l1)
xtgt, ytgt, ztgt = zip(*nest.GetTargetPositions(ctr, l1)[0])
xctr, yctr, zctr = nest.GetPosition(ctr)
ax.scatter([xctr], [yctr], [zctr], s=40, facecolor='r')
ax.scatter(xtgt, ytgt, ztgt, s=40, facecolor='g', edgecolor='g')

tgts = nest.GetTargetNodes(ctr, l1)[0]
distances = nest.Distance(ctr, l1)
tgt_distances = [d for i, d in enumerate(distances) if i + 1 in tgts]

plt.figure()
plt.hist(tgt_distances, 25)
plt.show()
예제 #6
0
    def _all_distances(self):
        """Return distances to all nodes in target population."""

        return nest.Distance(self._driver, self._lt)