Esempio n. 1
0
def createExportNeuroML2(netParams=None,
                         simConfig=None,
                         reference=None,
                         connections=True,
                         stimulations=True,
                         output=False):
    ''' Sequence of commands to create and export network to NeuroML2 '''
    import __main__ as top
    if not netParams: netParams = top.netParams
    if not simConfig: simConfig = top.simConfig

    sim.initialize(
        netParams,
        simConfig)  # create network object and set cfg and net params
    pops = sim.net.createPops()  # instantiate network populations
    cells = sim.net.createCells(
    )  # instantiate network cells based on defined populations
    conns = sim.net.connectCells(
    )  # create connections between cells based on params
    stims = sim.net.addStims(
    )  # add external stimulation to cells (IClamps etc)
    simData = sim.setupRecording(
    )  # setup variables to record for each cell (spikes, V traces, etc)
    sim.exportNeuroML2(
        reference, connections,
        stimulations)  # export cells and connectivity to NeuroML 2 format

    if output: return (pops, cells, conns, stims, simData)
Esempio n. 2
0
def load(filename,
         simConfig=None,
         output=False,
         instantiate=True,
         createNEURONObj=True):
    ''' Sequence of commands load, simulate and analyse network '''
    import sim
    sim.initialize()  # create network object and set cfg and net params
    sim.cfg.createNEURONObj = createNEURONObj
    sim.loadAll(filename,
                instantiate=instantiate,
                createNEURONObj=createNEURONObj)
    if simConfig:
        sim.setSimCfg(simConfig)  # set after to replace potentially loaded cfg
    if len(sim.net.cells) == 0 and instantiate:
        pops = sim.net.createPops()  # instantiate network populations
        cells = sim.net.createCells(
        )  # instantiate network cells based on defined populations
        conns = sim.net.connectCells(
        )  # create connections between cells based on params
        stims = sim.net.addStims(
        )  # add external stimulation to cells (IClamps etc)
    simData = sim.setupRecording(
    )  # setup variables to record for each cell (spikes, V traces, etc)

    if output:
        try:
            return (pops, cells, conns, stims, simData)
        except:
            pass
Esempio n. 3
0
def plotTraces (include = None, timeRange = None, overlay = False, oneFigPer = 'cell', rerun = False,
    figSize = (10,8), saveData = None, saveFig = None, showFig = True): 
    ''' 
    Plot recorded traces
        - include (['all',|'allCells','allNetStims',|,120,|,'E1'|,('L2', 56)|,('L5',[4,5,6])]): List of cells for which to plot 
            the recorded traces (default: [])
        - timeRange ([start:stop]): Time range of spikes shown; if None shows all (default: None)
        - overlay (True|False): Whether to overlay the data lines or plot in separate subplots (default: False)
        - oneFigPer ('cell'|'trace'): Whether to plot one figure per cell (showing multiple traces) 
            or per trace (showing multiple cells) (default: 'cell')
        - rerun (True|False): rerun simulation so new set of cells gets recorded (default: False)
        - figSize ((width, height)): Size of figure (default: (10,8))
        - saveData (None|True|'fileName'): File name where to save the final data used to generate the figure; 
            if set to True uses filename from simConfig (default: None)
        - saveFig (None|True|'fileName'): File name where to save the figure;
            if set to True uses filename from simConfig (default: None)
        - showFig (True|False): Whether to show the figure or not (default: True)

        - Returns figure handles
    '''

    print('Plotting recorded cell traces ...')

    if include is None: include = [] # If not defined, initialize as empty list

    # rerun simulation so new include cells get recorded from
    if rerun: 
        cellsRecord = [cell.gid for cell in sim.getCellsList(include)]
        for cellRecord in cellsRecord:
            if cellRecord not in sim.cfg.recordCells:
                sim.cfg.recordCells.append(cellRecord)
        sim.setupRecording()
        sim.simulate()


    colorList = [[0.42,0.67,0.84], [0.90,0.76,0.00], [0.42,0.83,0.59], [0.90,0.32,0.00],
                [0.34,0.67,0.67], [0.90,0.59,0.00], [0.42,0.82,0.83], [1.00,0.85,0.00],
                [0.33,0.67,0.47], [1.00,0.38,0.60], [0.57,0.67,0.33], [0.5,0.2,0.0],
                [0.71,0.82,0.41], [0.0,0.2,0.5]] 

    tracesList = sim.cfg.recordTraces.keys()
    tracesList.sort()
    cells, cellGids, _ = getCellsInclude(include)
    gidPops = {cell['gid']: cell['tags']['popLabel'] for cell in cells}

    # time range
    if timeRange is None:
        timeRange = [0,sim.cfg.duration]

    recordStep = sim.cfg.recordStep

    figs = []
    tracesData = []
    # Plot one fig per cell
    if oneFigPer == 'cell':
        for gid in cellGids:
            figs.append(figure()) # Open a new figure
            fontsiz = 12
            for itrace, trace in enumerate(tracesList):
                if 'cell_'+str(gid) in sim.allSimData[trace]:
                    data = sim.allSimData[trace]['cell_'+str(gid)][int(timeRange[0]/recordStep):int(timeRange[1]/recordStep)]
                    t = arange(timeRange[0], timeRange[1]+recordStep, recordStep)
                    tracesData.append({'t': t, 'cell_'+str(gid)+'_'+trace: data})
                    color = colorList[itrace]
                    if not overlay:
                        subplot(len(tracesList),1,itrace+1)
                        color = 'blue'
                    plot(t[:len(data)], data, linewidth=1.5, color=color, label=trace)
                    xlabel('Time (ms)', fontsize=fontsiz)
                    ylabel(trace, fontsize=fontsiz)
                    xlim(timeRange)
                    if itrace==0: title('Cell %d, Pop %s '%(int(gid), gidPops[gid]))
                    if overlay: 
                        maxLabelLen = 10
                        subplots_adjust(right=(0.9-0.012*maxLabelLen))
                        legend(fontsize=fontsiz, bbox_to_anchor=(1.04, 1), loc=2, borderaxespad=0.)


    # Plot one fig per cell
    elif oneFigPer == 'trace':
        for itrace, trace in enumerate(tracesList):
            figs.append(figure()) # Open a new figure
            fontsiz = 12
            for igid, gid in enumerate(cellGids):
                if 'cell_'+str(gid) in sim.allSimData[trace]:
                    data = sim.allSimData[trace]['cell_'+str(gid)][int(timeRange[0]/recordStep):int(timeRange[1]/recordStep)]
                    t = arange(timeRange[0], timeRange[1]+recordStep, recordStep)
                    tracesData.append({'t': t, 'cell_'+str(gid)+'_'+trace: data})
                    color = colorList[igid]
                    if not overlay:
                        subplot(len(cellGids),1,igid+1)
                        color = 'blue'
                        ylabel(trace, fontsize=fontsiz)
                    plot(t[:len(data)], data, linewidth=1.5, color=color, label='Cell %d, Pop %s '%(int(gid), gidPops[gid]))
                    xlabel('Time (ms)', fontsize=fontsiz)
                    xlim(timeRange)
                    title('Cell %d, Pop %s '%(int(gid), gidPops[gid]))
            if overlay:
                maxLabelLen = 10
                subplots_adjust(right=(0.9-0.012*maxLabelLen)) 
                legend(fontsize=fontsiz, bbox_to_anchor=(1.04, 1), loc=2, borderaxespad=0.)

    try:
        tight_layout()
    except:
        pass

    #save figure data
    if saveData:
        figData = {'tracesData': tracesData, 'include': include, 'timeRange': timeRange, 'oneFigPer': oneFigPer,
         'saveData': saveData, 'saveFig': saveFig, 'showFig': showFig}
    
        _saveFigData(figData, saveData, 'traces')
 
    # save figure
    if saveFig: 
        if isinstance(saveFig, str):
            filename = saveFig
        else:
            filename = sim.cfg.filename+'_'+'traces.png'
        savefig(filename)

    # show fig 
    if showFig: _showFigure()

    return figs