Skip to content

Commit

Permalink
made cells diversity work with popParams based on cellsList and `gr…
Browse files Browse the repository at this point in the history
…idSpacing`
  • Loading branch information
vvbragin committed Feb 6, 2024
1 parent 76fc479 commit 54f6247
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

- Fix of `plotRaster` pops coloring if ordered not by gid

- Made cells diversity work with popParams based on `cellsList` and `gridSpacing`

# Version 1.0.5

**New features**
Expand Down
2 changes: 1 addition & 1 deletion netpyne/network/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def _setDiversityRanges(self):
if 'diversityFraction' in cellRule:
divFrac = cellRule['diversityFraction']
cellType = cellRule['conds'].get('cellType', None)
cellModel = cellRule['conds'].get('CellModel', None)
cellModel = cellRule['conds'].get('cellModel', None)
pop = cellRule['conds'].get('pop', None)

correction = 1e-12
Expand Down
58 changes: 43 additions & 15 deletions netpyne/network/pop.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,13 @@ def createCellsFixedNum(self):
randLocs[:, icoord] = randLocs[:, icoord] * (maxv - minv) + minv

numCells = int(sim.net.params.scale * self.tags['numCells'])

diversityFractions = self._diversityFractions(numCells)

for i in self._distributeCells(numCells)[sim.rank]:
gid = sim.net.lastGid + i
self.cellGids.append(gid) # add gid list of cells belonging to this population - not needed?
cellTags = self._createCellTags()
cellTags = self._createCellTags(i, diversityFractions)
cellTags['xnorm'] = randLocs[i, 0] # set x location (um)
cellTags['ynorm'] = randLocs[i, 1] # set y location (um)
cellTags['znorm'] = randLocs[i, 2] # set z location (um)
Expand All @@ -173,8 +176,6 @@ def createCellsFixedNum(self):
pass
else:
cellTags['params']['spkTimes'] = self.tags['spkTimes'] # 1D list (same for all)
if self.tags.get('diversity', False): # if pop has cell diversity
cellTags['fraction'] = float(i) / float(numCells)

if 'dynamicRates' in self.tags: # if NetStim, copy rates array to params
if 'rates' in self.tags['dynamicRates'] and 'times' in self.tags['dynamicRates']:
Expand Down Expand Up @@ -336,10 +337,13 @@ def createCellsDensity(self):
if sim.cfg.verbose and not funcLocs:
print('Volume=%.4f, density=%.2f, numCells=%.0f' % (volume, self.tags['density'], self.tags['numCells']))

for i in self._distributeCells(self.tags['numCells'])[sim.rank]:
numCells = self.tags['numCells']
diversityFractions = self._diversityFractions(numCells)

for i in self._distributeCells(numCells)[sim.rank]:
gid = sim.net.lastGid + i
self.cellGids.append(gid) # add gid list of cells belonging to this population - not needed?
cellTags = self._createCellTags()
cellTags = self._createCellTags(i, diversityFractions)
cellTags['xnorm'] = randLocs[i, 0] # calculate x location (um)
cellTags['ynorm'] = randLocs[i, 1] # calculate y location (um)
cellTags['znorm'] = randLocs[i, 2] # calculate z location (um)
Expand All @@ -353,7 +357,7 @@ def createCellsDensity(self):
'Cell %d/%d (gid=%d) of pop %s, pos=(%2.f, %2.f, %2.f), on node %d, '
% (
i,
self.tags['numCells'] - 1,
numCells - 1,
gid,
self.tags['pop'],
cellTags['x'],
Expand All @@ -372,14 +376,19 @@ def createCellsList(self):
"""

cells = []
self.tags['numCells'] = len(self.tags['cellsList'])
for i in self._distributeCells(len(self.tags['cellsList']))[sim.rank]:
cellsList = self.tags['cellsList']
numCells = len(cellsList)
self.tags['numCells'] = numCells

diversityFractions = self._diversityFractions(numCells)

for i in self._distributeCells(numCells)[sim.rank]:
# if 'cellModel' in self.tags['cellsList'][i]:
# self.cellModelClass = getattr(f, self.tags['cellsList'][i]['cellModel']) # select cell class to instantiate cells based on the cellModel tags
gid = sim.net.lastGid + i
self.cellGids.append(gid) # add gid list of cells belonging to this population - not needed?
cellTags = self._createCellTags()
cellTags.update(self.tags['cellsList'][i]) # add tags specific to this cells
cellTags = self._createCellTags(i, diversityFractions)
cellTags.update(cellsList[i]) # add tags specific to this cells
for coord in ['x', 'y', 'z']:
if coord in cellTags: # if absolute coord exists
cellTags[coord + 'norm'] = cellTags[coord] / getattr(
Expand All @@ -392,15 +401,15 @@ def createCellsList(self):
else:
cellTags[coord + 'norm'] = cellTags[coord] = 0
if (
'cellModel' in self.tags.keys() and self.tags['cellModel'] == 'Vecstim'
'cellModel' in self.tags.keys() and self.tags['cellModel'] == 'VecStim'
): # if VecStim, copy spike times to params
cellTags['params']['spkTimes'] = self.tags['cellsList'][i]['spkTimes']
cellTags['params']['spkTimes'] = cellsList[i]['spkTimes']
cells.append(self.cellModelClass(gid, cellTags)) # instantiate Cell object
if sim.cfg.verbose:
print(
('Cell %d/%d (gid=%d) of pop %d, on node %d, ' % (i, self.tags['numCells'] - 1, gid, i, sim.rank))
)
sim.net.lastGid = sim.net.lastGid + len(self.tags['cellsList'])
sim.net.lastGid = sim.net.lastGid + numCells
return cells

def createCellsGrid(self):
Expand Down Expand Up @@ -440,10 +449,12 @@ def createCellsGrid(self):

numCells = len(gridLocs)

diversityFractions = self._diversityFractions(numCells, shuffled=True)

for i in self._distributeCells(numCells)[sim.rank]:
gid = sim.net.lastGid + i
self.cellGids.append(gid) # add gid list of cells belonging to this population - not needed?
cellTags = self._createCellTags()
cellTags = self._createCellTags(i, diversityFractions)
cellTags['xnorm'] = gridLocs[i][0] / sim.net.params.sizeX # set x location (um)
cellTags['ynorm'] = gridLocs[i][1] / sim.net.params.sizeY # set y location (um)
cellTags['znorm'] = gridLocs[i][2] / sim.net.params.sizeZ # set z location (um)
Expand All @@ -456,11 +467,28 @@ def createCellsGrid(self):
sim.net.lastGid = sim.net.lastGid + numCells
return cells

def _createCellTags(self):
def _diversityFractions(self, numCells, shuffled=False):
"""
Calculates diversity fraction (0 to 1) for each cell. Returns list of fractions or None, if no diversity involved
"""
if not self.tags.get('diversity', False):
return None

rng = np.random.default_rng(seed=sim.cfg.seeds['cell'])
diversityFractions = np.arange(0, numCells, dtype=np.float64) / numCells
if shuffled:
rng.shuffle(diversityFractions)
return diversityFractions

def _createCellTags(self, ind=None, diversityFractions=None):

# copy all pop tags to cell tags, except those that are pop-specific
cellTags = {k: v for (k, v) in self.tags.items() if k in sim.net.params.popTagsCopiedToCells}
cellTags['pop'] = self.tags['pop']

if diversityFractions is not None:
cellTags['fraction'] = diversityFractions[ind]

return cellTags

def _setCellClass(self):
Expand Down

0 comments on commit 54f6247

Please sign in to comment.