Esempio n. 1
0
        data = pc.pyret()
        # save file to folder
        name = '{}_{}-{}_validation'.format(cell_type, data['meta']['round'],
                                            data['meta']['id'])
        cf.save_data(data, '{}/{}.json'.format(folder, name))

pc.done()  # end parallelisation

# for timing simulations
print(
    'Simulations completed (took %.0f secs).\nNow performing calculations/collating data...'
    % (time.time() - start))

# ===== combine and/or average data =====

info = cf.params_for_input(cell_type, 'clustered')
clus_info = info['clustered']

# collates data loaded from files
data_all = {}

for i, iteration in enumerate(iterations):
    round_data = {}
    for r in range(n_rounds):
        # load data
        name = name = '{}_{}-{}_validation'.format(cell_type, r, iteration)
        data = cf.load_data('{}/{}.json'.format(folder, name))
        round_data[r] = data
    # combine data
    data_all[i] = round_data
Esempio n. 2
0
    raise ValueError(
        "The requested cell type is not supported.\nOnly 'dpsn' and 'ispn' are recognised."
    )

model_iterator = list(range(
    specs[cell_type]
    ['N']))  # use model_iterator = range(specs[cell_type]['N']) for all models
# for dspn, 10 has lowest rheo, 54 has highest, 22 has mean, 41 has median; 22 is also average for experimental value
# for ispn, 8 has mean and median; 1 is average for experimental value

if pc.id() == 0:
    print('Simulating {} cell iteration(s) of type: {}'.format(len(model_iterator),cell_type), \
          flush=True)

# stimulation details
stim_info = cf.params_for_input(cell_type, 'clustered')
target = stim_info['clustered']['target']
target_labels = stim_info['clustered']['label']
stim_data = stim_info['clustered']['params']

# open library (channel distributions etc)
with open(specs[cell_type]['lib'], 'rb') as f:
    model_sets = pickle.load(f, encoding="latin1")

# ===== simulate model(s) =====

data = {}

# model information to pass to simulations
model_data = {'specs':specs[cell_type], 'cell_type':cell_type, 'model_sets':model_sets, \
              'target':target, 'target_labels':target_labels}
Esempio n. 3
0
    }
}

# chose cell type ('ispn' or 'dspn') and model id(s) to simulate...
cell_type = 'ispn'  # 'dspn'/'ispn'
model_iterator = list(
    range(specs[cell_type]['N'])
)  # range(specs[cell_type]['N']) gives all models; must be a list for saving data!
# for dspn, 10 has lowest rheo, 54 has highest, 22 has mean, 41 has median; 22 is also average for experimental value
# for ispn, 8 has mean and median; 1 is average for experimental value
if pc.id() == 0:
    print('Simulating {} cell(s) of type: {}'.format(len(model_iterator),
                                                     cell_type))

# stimulation details
stim_data = cf.params_for_input(cell_type, 'ACh')

# open library (channel distributions etc)
with open(specs[cell_type]['lib'], 'rb') as f:
    model_sets = pickle.load(f, encoding="latin1")

# model information to pass to simulations
model_data = {
    'specs': specs[cell_type],
    'cell_type': cell_type,
    'model_sets': model_sets
}

# ===== gets cholinergic modulation factors =====

mod_factors = cf.draw_factors_ACh(cell_type, mode='mean')
Esempio n. 4
0
def dpp_generation(model_data,
                   cell_index,
                   run_info,
                   noise=True,
                   HFI=False,
                   HFI_delay=0,
                   dur_and_amp=True,
                   spike=False):
    '''
    Provides clustered glutamatergic input to SPNs to generate the dendritic 
    plateau potential in the absence of modulation.
    
    INPUT(S):
        - model_data: model paramaters (specification, cell type, model 
            sets, and stimulation targets) [dict]
        - stim_data: stimulation parameters (number of inputs, time of inputs,
            input inter-spike intervals, time to stop simulating) [dict]
        - cell_index: cell specification being simulated [int]
        - run_info: information about the simulation run (this simulation and 
            total number of simulations) [dict]
        - dur_and_amp: whether to calculate the duration and peak amplitude of
            the plateau potential (default True) [bool]
        
    OUTPUT(S):
        - data: simulated data including: simulation times; simulated voltages;
            distance of stimulated target to the soma; rheobase of the model; 
            half-max width of the potential; peak amplitude of the potential;
            cell specification being simulated; cell type being simulated
            [dict]
        - ncon: NetCon object
            
    Thomas Binns (author), 26/01/21
    '''

    # ===== print simulation info to monitor progress =====
    print('Simulating cell specification {} of {}'.format( \
          run_info['curr_n']+1,run_info['tot_n']),flush = True)

    # ===== gets stimulation info =====
    # clustered inputs
    clus_info = cf.params_for_input(model_data['cell_type'], 'clustered')
    clus_params = clus_info['clustered']['params']

    # noise inputs
    if noise:
        noise_info = cf.params_for_input(model_data['cell_type'], 'noise')
        noise_params = noise_info['noise']['params']

    # HFIs
    if HFI:
        HFI_info = cf.params_for_input(model_data['cell_type'], 'HFI')
        HFI_params = HFI_info['HFI']['params']

    # ===== simulation =====
    data = {}

    for i, tar in enumerate(
            clus_info['clustered']['target']):  # for each simulation target

        clus_lab = clus_info['clustered']['label'][i]  # label for input target

        # initiate cell
        cell = build.MSN(
            params=model_data['specs']['par'],
            morphology=model_data['specs']['morph'],
            variables=model_data['model_sets'][cell_index]['variables'])
        rheobase = model_data['model_sets'][cell_index]['rheobase']

        # record vectors
        tm = h.Vector()
        tm.record(h._ref_t)
        vm = h.Vector()
        vm.record(cell.soma(0.5)._ref_v)

        # add clustered inputs
        clus_syn, clus_stim, clus_ncon, clus_d2soma = cf.set_clustered_stim(
            cell,
            tar,
            n=clus_params['stim_n'],
            act_time=clus_params['stim_t'],
            ISI=clus_params['isi'])

        # add background noise
        if noise:
            '''
            noise_syn, noise_stim, noise_ncon = cf.set_noise(cell, model_data['cell_type'], 
                freq_glut=noise_params['freq_glut'], freq_gaba=noise_params['freq_gaba'],
                n_glut=noise_params['n_glut'], n_gaba=noise_params['n_gaba'], only_dend=noise_params['only dend'],
                glut_delay=noise_params['stim_t'], gaba_delay=noise_params['stim_t'])
            '''
            noise_syn, noise_stim, noise_ncon = cf.set_bg_noise(
                cell,
                model_data['cell_type'],
                fglut=noise_params['freq_glut'],
                fgaba=noise_params['freq_gaba'],
                dendOnly=noise_params['only dend'])

        # add high-frequency inputs
        if HFI:
            # adds HFI
            HFI_syn, HFI_stim, HFI_ncon, arrangement = cf.set_HFI(
                cell,
                model_data['cell_type'],
                freq=HFI_params['freq'],
                n_inputs=HFI_params['n_inputs'],
                delay=clus_params['stim_t'] +
                (clus_params['stim_n'] * clus_params['isi']) + HFI_delay,
                exclude=HFI_info['HFI']['exclude'])
            # collates data
            data['HFI'] = arrangement

        # run simulation
        h.finitialize(-80)
        while h.t < clus_params['stop_t'] + HFI_delay + (
                clus_params['stim_n'] * clus_params['isi']):
            h.fadvance()
        tm = tm.to_python()
        vm = vm.to_python()

        # collate data
        data[clus_lab] = {'vm': vm}
        data['meta'] = {
            'id': int(cell_index),
            'round': run_info['round'],
            'cell_type': model_data['cell_type'],
            'tm': tm,
            'rheo': rheobase
        }

        # calculate dpp duration and amplitude
        if dur_and_amp:
            base_t_start = tm.index(
                min(tm,
                    key=lambda x: abs(x - (clus_params['stim_t'] + clus_params[
                        'pre_t']))))
            base_t_end = tm.index(
                min(tm, key=lambda x: abs(x - clus_params['stim_t'])))
            base_vm = np.mean(vm[base_t_start:base_t_end])
            data[clus_lab]['dur'] = cf.dpp_dur(tm, vm, base_vm,
                                               clus_params['stim_t'])
            data[clus_lab]['amp'] = cf.dpp_amp(tm, vm, base_vm,
                                               clus_params['stim_t'])

        # get spike-related data
        if spike:
            spiked = 0
            thresh = 0
            # checks whether spike occured
            data[clus_lab]['spiked'] = 0
            if max(vm) > thresh:
                data[clus_lab]['spiked'] = 1
                spiked = 1
            # checks time of first spike number of spikes that occured (if any)
            data[clus_lab]['first_spike'] = []
            data[clus_lab]['spike_n'] = []
            if spiked == 1:
                data[clus_lab]['first_spike'] = tm[next(
                    i for i, x in enumerate(vm) if x > 0)]
                data[clus_lab]['spike_n'] = cf.spike_n(vm)

    return data
Esempio n. 5
0
def dpp_DA_modded(model_data,
                  cell_index,
                  run_info,
                  noise=True,
                  HFI=False,
                  HFI_delay=0,
                  dur_and_amp=True,
                  spike=False,
                  mod_tar='all'):
    '''
    Provides clustered glutamatergic input to SPNs to generate the dendritic 
    plateau potential in the absence of modulation.
    
    INPUT(S):
        - model_data: model paramaters (specification, cell type, model 
            sets, and stimulation targets) [dict]
        - stim_data: stimulation parameters (number of inputs, time of inputs,
            input inter-spike intervals, time to stop simulating) [dict]
        - cell_index: cell specification being simulated [int]
        - run_info: information about the simulation run (this simulation and 
            total number of simulations) [dict]
        - dur_and_amp: whether to calculate the duration and peak amplitude of
            the plateau potential (default True) [bool]
        
    OUTPUT(S):
        - data: simulated data including: simulation times; simulated voltages;
            distance of stimulated target to the soma; rheobase of the model; 
            half-max width of the potential; peak amplitude of the potential;
            cell specification being simulated; cell type being simulated
            [dict]
        - ncon: NetCon object
            
    Thomas Binns (author), 13/02/21
    '''

    # ===== print simulation info to monitor progress =====
    print('Simulating cell specification {} of {}'.format( \
          run_info['curr_n']+1,run_info['tot_n']),flush = True)

    # ===== gets stimulation info =====
    # clustered inputs
    clus_info = cf.params_for_input(model_data['cell_type'], 'clustered')
    clus_params = clus_info['clustered']['params']

    # modulation inputs
    mod_factors = cf.draw_factors_DA(model_data['cell_type'], mode='random')
    DA_info = cf.params_for_input(model_data['cell_type'], 'DA')
    DA_params = DA_info['DA']['params']

    # gets vector for modulation timing
    state = [1 if ht >= DA_params['stim_t'] and ht <= DA_params['stop_t'] \
             else 0 for ht in np.arange(0,clus_params['stop_t'],h.dt)]
    state = h.Vector(state)

    # collates time-dependent mechanism modulation scaling
    mech_scale = {}
    for key in mod_factors:
        mech_scale[key] = state

    # noise inputs
    if noise:
        noise_info = cf.params_for_input(model_data['cell_type'], 'noise')
        noise_params = noise_info['noise']['params']

    # HFIs
    if HFI:
        HFI_info = cf.params_for_input(model_data['cell_type'], 'HFI')
        HFI_params = HFI_info['HFI']['params']

    # ===== simulation =====
    data = {}

    for i, clus_tar in enumerate(
            clus_info['clustered']['target']):  # for each simulation target

        clus_lab = clus_info['clustered']['label'][i]  # label for input target
        data[clus_lab] = {}

        # get the targets for cholinergic input
        DA_targets = {}
        DA_targets['target'] = [clus_tar]
        DA_targets['label'] = [clus_lab]
        for j, DA_t in enumerate(DA_info['DA']['target']):
            DA_targets['target'].append(DA_t)
            DA_targets['label'].append(DA_info['DA']['label'][j])
        target_x = .5

        if mod_tar == 'all':
            DA_targets['target'] = ['all']
            DA_targets['label'] = ['all']
            target_x = 'all'

        for j, DA_t in enumerate(
                DA_targets['target']):  # for each cholinergic input target

            DA_lab = DA_targets['label'][j]

            # initiate cell
            cell = build.MSN(
                params=model_data['specs']['par'],
                morphology=model_data['specs']['morph'],
                variables=model_data['model_sets'][cell_index]['variables'])
            rheobase = model_data['model_sets'][cell_index]['rheobase']

            # record vectors
            tm = h.Vector()
            tm.record(h._ref_t)
            vm = h.Vector()
            vm.record(cell.soma(0.5)._ref_v)

            # add clustered inputs
            clus_syn, clus_stim, clus_ncon, clus_d2soma = cf.set_clustered_stim(
                cell,
                clus_tar,
                n=clus_params['stim_n'],
                act_time=clus_params['stim_t'],
                ISI=clus_params['isi'])

            # add background noise
            if noise:
                noise_syn, noise_stim, noise_ncon = cf.set_bg_noise(
                    cell,
                    model_data['cell_type'],
                    fglut=noise_params['freq_glut'],
                    fgaba=noise_params['freq_gaba'],
                    dendOnly=noise_params['only dend'])

            # add high-frequency inputs
            if HFI:
                # adds HFI
                HFI_syn, HFI_stim, HFI_ncon, arrangement = cf.set_HFI(
                    cell,
                    model_data['cell_type'],
                    freq=HFI_params['freq'],
                    n_inputs=HFI_params['n_inputs'],
                    delay=clus_params['stim_t'] +
                    (clus_params['stim_n'] * clus_params['isi']) + HFI_delay,
                    exclude=HFI_info['HFI']['exclude'])
                # collates data
                data['HFI'] = arrangement

            # get cholinergic modulation class
            modulation = modulate.set_DA(cell,
                                         mod_factors, [DA_t],
                                         target_x=target_x,
                                         play=mech_scale,
                                         dt=h.dt)

            # run simulation
            h.finitialize(-80)
            while h.t < clus_params['stop_t'] + HFI_delay + (
                    clus_params['stim_n'] * clus_params['isi']):
                h.fadvance()
            tm = tm.to_python()
            vm = vm.to_python()

            # collate data
            data[clus_lab][DA_lab] = {'vm': vm}
            data['meta'] = {
                'id': int(cell_index),
                'round': run_info['round'],
                'cell_type': model_data['cell_type'],
                'tm': tm,
                'rheo': rheobase
            }

            # calculate dpp duration and amplitude
            if dur_and_amp:
                base_t_start = tm.index(
                    min(tm,
                        key=lambda x: abs(x - (clus_params['stim_t'] +
                                               clus_params['pre_t']))))
                base_t_end = tm.index(
                    min(tm, key=lambda x: abs(x - clus_params['stim_t'])))
                base_vm = np.mean(vm[base_t_start:base_t_end])
                data[clus_lab][DA_lab]['dur'] = cf.dpp_dur(
                    tm, vm, base_vm, clus_params['stim_t'])
                data[clus_lab][DA_lab]['amp'] = cf.dpp_amp(
                    tm, vm, base_vm, clus_params['stim_t'])

            # get spike-related data
            if spike:
                thresh = 0
                # checks whether spike occured
                data[clus_lab][DA_lab]['spiked'] = 0
                if max(vm) > thresh:
                    data[clus_lab][DA_lab]['spiked'] = 1

    return data
Esempio n. 6
0
import neuron as nrn
import MSN_builder as build
import pickle
import common_functions as cf
import numpy as np
from neuron import h
from matplotlib import pyplot as plt
from matplotlib.colors import LinearSegmentedColormap as lsc

# what to visualise =============

cell_type = input("What cell type should be visualised: dspn; or ispn?")
input_type = input(
    "What input should be visualised: 'clustered'; 'HFI'; or 'ACh'?")

vis_info = cf.params_for_input(cell_type, input_type)

# sets up cell =============

#nrn.load_mechanisms('mechanisms/single')
h.load_file('stdlib.hoc')
h.load_file('import3d.hoc')

# specs
specs = {
    'dspn': {
        'N': 71,
        'lib': 'Libraries/D1_71bestFit_updRheob.pkl',
        'par': 'Params/params_dMSN.json',
        'morph': 'Morphologies/WT-dMSN_P270-20_1.02_SGA1-m24.swc'
    },