def sample(self, n, rng=None): """ Randomly sample `n` cells from the Population, and return a\ PopulationView object. """ if not rng: rng = NumpyRNG() indices = rng.permutation( numpy.arange(len(self), dtype=numpy.int))[0:n] return PopulationView( self, indices, label="Random sample size {} from {}".format(n, self.label))
class Grid3D(BaseStructure): """ Represents a structure with neurons distributed on a 3D grid. Arguments: `dx`, `dy`, `dz`: distances between points in the x, y, z directions. `x0`, `y0`. `z0`: coordinates of the starting corner of the grid. `aspect_ratioXY`, `aspect_ratioXZ`: ratios of the number of grid points per side (not the ratio of the side lengths, unless ``dx == dy == dz``) `fill_order`: may be 'sequential' or 'random'. If `fill_order` is 'sequential', the z-index will be filled first, then y then x, i.e. the first cell will be at (0,0,0) (given default values for the other arguments), the second at (0,0,1), etc. """ parameter_names = ("aspect_ratios", "dx", "dy", "dz", "x0", "y0", "z0", "fill_order") def __init__(self, aspect_ratioXY=1.0, aspect_ratioXZ=1.0, dx=1.0, dy=1.0, dz=1.0, x0=0.0, y0=0.0, z0=0, fill_order="sequential", rng=None): self.aspect_ratios = (aspect_ratioXY, aspect_ratioXZ) assert fill_order in ('sequential', 'random') self.fill_order = fill_order self.rng = rng self.dx = dx; self.dy = dy; self.dz = dz self.x0 = x0; self.y0 = y0; self.z0 = z0 def calculate_size(self, n): """docstring goes here""" a, b = self.aspect_ratios nx = int(round(math.pow(n * a * b, 1 / 3.0))) ny = int(round(nx / a)) nz = int(round(nx / b)) assert nx * ny * nz == n, str((nx, ny, nz, nx * ny * nz, n, a, b)) return nx, ny, nz def generate_positions(self, n): nx, ny, nz = self.calculate_size(n) x, y, z = numpy.indices((nx, ny, nz), dtype=float) x = self.x0 + self.dx * x.flatten() y = self.y0 + self.dy * y.flatten() z = self.z0 + self.dz * z.flatten() positions = numpy.array((x, y, z)) if self.fill_order == 'sequential': return positions else: if self.rng is None: self.rng = NumpyRNG() return self.rng.permutation(positions.T).T generate_positions.__doc__ = BaseStructure.generate_positions.__doc__
class Grid2D(BaseStructure): """ Represents a structure with neurons distributed on a 2D grid. Arguments: `dx`, `dy`: distances between points in the x, y directions. `x0`, `y0`: coordinates of the starting corner of the grid. `z`: the z-coordinate of all points in the grid. `aspect_ratio`: ratio of the number of grid points per side (not the ratio of the side lengths, unless ``dx == dy``) `fill_order`: may be 'sequential' or 'random' """ parameter_names = ("aspect_ratio", "dx", "dy", "x0", "y0", "z", "fill_order") def __init__(self, aspect_ratio=1.0, dx=1.0, dy=1.0, x0=0.0, y0=0.0, z=0, fill_order="sequential", rng=None): self.aspect_ratio = aspect_ratio assert fill_order in ('sequential', 'random') self.fill_order = fill_order self.rng = rng self.dx = dx; self.dy = dy; self.x0 = x0; self.y0 = y0; self.z = z def calculate_size(self, n): """docstring goes here""" nx = math.sqrt(n * self.aspect_ratio) if n % nx != 0: raise Exception("Invalid size: n=%g, nx=%d" % (n, nx)) nx = int(round(nx)) ny = n // nx return nx, ny def generate_positions(self, n): nx, ny = self.calculate_size(n) x, y, z = numpy.indices((nx, ny, 1), dtype=float) x = self.x0 + self.dx * x.flatten() y = self.y0 + self.dy * y.flatten() z = self.z + z.flatten() positions = numpy.array((x, y, z)) # use column_stack, if we decide to switch from (3,n) to (n,3) if self.fill_order == 'sequential': return positions else: # random if self.rng is None: self.rng = NumpyRNG() return self.rng.permutation(positions.T).T generate_positions.__doc__ = BaseStructure.generate_positions.__doc__
def sample(self, n, rng=None): """ Randomly sample `n` cells from the Population view, and return a\ new PopulationView object. :param int n: The number of cells to select :param ~pyNN.random.NumpyRNG rng: Random number generator :rtype: ~spynnaker.pyNN.models.populations.PopulationView """ if not rng: rng = NumpyRNG() indices = rng.permutation(numpy.arange(len(self), dtype=numpy.int))[0:n] return PopulationView(self, indices, label="Random sample size {} from {}".format( n, self.label))