Ejemplo n.º 1
0
import os
import sys
from neuromllite.NetworkGenerator import check_to_generate_or_run

os.chdir("..")
sys.path.append(".")

sys.argv.append("-jnmlnrn")

from Example7 import generate

sim, net = generate()

check_to_generate_or_run(sys.argv, sim)
Ejemplo n.º 2
0
def generate(reference, 
             only_areas_matching=None, 
             only_ids_matching=None,
             include_contra=False,
             include_connections=True,
             include_detailed_cells=False):

    colors = {}
    centres = {}
    pop_ids = []
    used_ids = {}
    names = {}
    areas = {}

    with open('nature13186-s2_1.csv', 'rb') as csvfile:
        reader = csv.reader(csvfile, delimiter=',', quotechar='"')
        for w in reader:
            print w
            if w[0] != 'id':
                short = w[3].replace(', ','_')
                name = w[4].strip('"')
                names[short]=name

    with open('nature13186-s2_2.csv', 'rb') as csvfile:
        reader = csv.reader(csvfile, delimiter=',', quotechar='"')
        for w in reader:
            print w
            if w[0] != 'ID':
                short = w[2].replace(', ','_')
                area = w[4].strip('"')
                areas[short]=area

    for n in names: print('%s: \t%s'%(n,names[n]))
    for a in areas: print('%s: \t%s'%(a,areas[a]))
    #exit()

    ################################################################################
    ###   Build a new network

    net = Network(id=reference)
    net.notes = "NOTE: this is only a quick demo!! Do not use it for your research assuming an accurate conversion of the source data!!! "

    #cell = Cell(id='dummycell', pynn_cell='IF_cond_alpha')
    #cell.parameters = { "tau_refrac":5, "i_offset":.1 }
    cell = Cell(id='dummycell', neuroml2_source_file='passiveSingleCompCell.cell.nml')
    
    
    net.cells.append(cell)

    net.synapses.append(Synapse(id='ampa', 
                                pynn_receptor_type='excitatory', 
                                pynn_synapse_type='cond_alpha', 
                                parameters={'e_rev':-10, 'tau_syn':2}))

    '''                            
    r1 = RectangularRegion(id='region1', x=0,y=0,z=0,width=1000,height=100,depth=1000)
    net.regions.append(r1)
    default_cell = Cell(id='L23PyrRS', neuroml2_source_file='TestSmall/L23PyrRS.cell.nml')
    net.cells.append(default_cell)
    p0 = Population(id='pop0', size=5, component=default_cell.id, properties={'color':'0 .8 0'})
    net.populations.append(p0)
    net.populations[0].random_layout = RandomLayout(region=r1.id)'''

    detailed_cells = ['AA0289'] if include_detailed_cells else []
    
    for dc in detailed_cells:
        
        ll = SingleLocation()
        ll.location = Location(x=0,y=0,z=0)
        orig_file='%s_active.cell.nml'%dc
        
        new_ref, new_cell_file = get_oriented_cell(orig_file, math.pi,math.pi/2, 5500, 5300, 700)
        
        print("Translated %s to %s"%(orig_file, new_cell_file))

        mo = Cell(id=dc, neuroml2_source_file='%s_active.cell.nml'%dc)
        net.cells.append(mo)
        p1 = Population(id='pop_%s'%dc, 
                        size=1, 
                        component=mo.id, 
                        properties={'color':'.8 0 0'})
        p1.single_location=ll 
        #net.populations.append(p1)
        
        mo = Cell(id=new_ref, neuroml2_source_file=new_cell_file)
        net.cells.append(mo)
        p1 = Population(id='pop_%s'%new_ref, 
                        size=1, 
                        component=new_ref, 
                        properties={'color':'0 0.8 0'})
        p1.single_location=ll 
        net.populations.append(p1)


    f = open('ABA12.tsv')
    for l in f:
        w = l.split()
        print w
        pre_id = w[0].replace('-','_').replace('/','_')
        if pre_id != '[0]':

            match = False
            if only_ids_matching==None:
                match = True
            else:
                for i in only_ids_matching:
                    if i=='*' or i in pre_id:
                        match = True


            if match:

                scale = 1000
                x0 = float(w[2])*scale
                all = [ (pre_id, x0)]
                if include_contra:
                    all = [ (pre_id, x0), ('CONTRA_%s'%pre_id, x0*-1)]

                for a in all:
                    id = a[0]
                    x = a[1]
                    centres[id] = (x,float(w[3])*scale,float(w[4])*scale)
                    colors[id] = w[1]

                    repl = id
                    name = names[w[0]]
                    short_name3 = w[0][:3]
                    short_name4 = w[0][:4]
                    short_name5 = w[0][:5]
                    short_name6 = w[0][:6]
                    short_name7 = w[0][:7]

                    if w[0] in areas:
                        area = areas[w[0]]  
                    elif short_name7 in areas:
                           area = areas[short_name7]  
                    elif short_name6 in areas:
                           area = areas[short_name6]  
                    elif short_name5 in areas:
                           area = areas[short_name5]  
                    elif short_name4 in areas:
                           area = areas[short_name4]  
                    elif short_name3 in areas:
                           area = areas[short_name3]  
                    else:
                        area = '???'

                    match = False
                    if only_areas_matching==None:
                        match = True
                    else:
                        for a in only_areas_matching:
                            if a in area:
                                match = True


                    if match:

                        p = centres[repl]
                        used_ids[id] = '_%s'%repl if repl[0].isdigit() else repl

                        region_name = name.split(',')[0].replace(' ','_')
                        region_name = used_ids[id]
                        r = RectangularRegion(id=region_name, x=p[0],y=p[1],z=p[2],width=1,height=1,depth=1)
                        net.regions.append(r)


                        color = '.8 .8 .8'
                        if 'Thalamus' in area:
                            color = '.3 .3 .3'
                        if 'Isocortex' in area:
                            color = occ.L23_PRINCIPAL_CELL
                        if 'bulb' in area:
                            color = '0 0 1'
                        if 'Cerebe' in area:
                            color = '.6 .6 .6'
                        #if 'Hippocampal' in area:
                        #    color = occ.L6_PRINCIPAL_CELL

                        if '1' in id:
                            color = occ.THALAMUS_2
                        if '2_3' in id:
                            color = occ.L23_PRINCIPAL_CELL
                        if '4' in id:
                            color = occ.L4_PRINCIPAL_CELL
                        if '5' in id:
                            color = occ.L5_PRINCIPAL_CELL
                        if '6' in id:
                            color = occ.L6_PRINCIPAL_CELL


                        p0 = Population(id=used_ids[id], 
                                        size=1, 
                                        component=cell.id, 
                                        properties={'color':'%s'%(color),
                                                    'radius':50,
                                                    'name':name,
                                                    'area':area},
                                        random_layout = RandomLayout(region=r.id))

                        net.populations.append(p0)
                        pop_ids.append(id)

    #print centres.keys()


    if include_connections:
        with open('nature13186-s4_W_ipsi.csv', 'rb') as csvfile:
            reader = csv.reader(csvfile, delimiter=',', quotechar='"')
            indices = {}
            for w in reader:
                #print w
                if w[0]=='ROOT':
                    for i in range(len(w)):
                        indices[i]=w[i]
                    print indices
                else:
                    pre = w[0]
                    for i in range(len(w)):
                        if i!=0:
                            weight = float(w[i])
                            if weight>0:
                                post = indices[i]
                                print('Connection %s -> %s of %s'%(pre, post, weight))

                                if weight>0.0:

                                    if pre in used_ids and post in used_ids:
                                        print('Adding conn from %s -> %s of %s'%(pre, post, weight))


                                        ################################################################################
                                        ###   Add a projection

                                        net.projections.append(Projection(id='proj_%s_%s'%(used_ids[pre],used_ids[post]),
                                                                          presynaptic=used_ids[pre], 
                                                                          postsynaptic=used_ids[post],
                                                                          synapse='ampa',
                                                                          weight=weight,
                                                                          random_connectivity=RandomConnectivity(probability=1)))




    from neuromllite import Simulation
    #print(net)

    print(net.to_json())
    new_file = net.to_json_file('%s.json'%net.id)

    sim = Simulation(id='Sim_%s'%net.id,
                     network=new_file,
                     duration='1000',
                     dt='0.025',
                     recordTraces={'all':'*'})


    ################################################################################
    ###   Export to some formats
    ###   Try:
    ###        python Example1.py -graph2

    from neuromllite.NetworkGenerator import check_to_generate_or_run
    import sys

    check_to_generate_or_run(sys.argv, sim)
Ejemplo n.º 3
0
        if fln>0.0:
            net.projections.append(Projection(id='proj_%s_%s'%(src,tgt),
                                              presynaptic=src, 
                                              postsynaptic=tgt,
                                              synapse='ampa',
                                              weight=fln))

            #net.projections[0].random_connectivity=RandomConnectivity(probability=0.5)



print(net)
net.id = 'TestNetwork'

print(net.to_json())
new_file = net.to_json_file('Example1_%s.json'%net.id)


################################################################################
###   Export to some formats
###   Try:
###        python Example1.py -graph2

from neuromllite.NetworkGenerator import check_to_generate_or_run
from neuromllite import Simulation
import sys

check_to_generate_or_run(sys.argv, Simulation(id='SimExample1',network=new_file))

Ejemplo n.º 4
0
    )
)
net.synapses.append(
    Synapse(
        id="ampa", neuroml2_source_file="../../../examples/test_files/ampa.synapse.nml"
    )
)


print(net.to_json())
new_file = net.to_json_file("%s.json" % net.id)


################################################################################
###   Use a handler which just prints info on positions, etc.

def_handler = DefaultNetworkHandler()

generate_network(net, def_handler)


################################################################################
###   Export to some formats, e.g. try:
###        python Example2.py -graph2

from neuromllite.NetworkGenerator import check_to_generate_or_run
from neuromllite import Simulation
import sys

check_to_generate_or_run(sys.argv, Simulation(id="Sim%s" % net.id, network=new_file))
                            heatmap_all=False,
                            show_plot_already=False,
                            peak_threshold=0)

        report = ps.run()
        ps.print_report()

        #  ps.plotLines('weightInput','average_last_1percent',save_figure_to='average_last_1percent.png')
        #ps.plotLines('weightInput','mean_spike_frequency',save_figure_to='mean_spike_frequency.png')
        #ps.plotLines('rate','sspPop[0]/spike:mean_spike_frequency',save_figure_to='mean_spike_frequency.png')
        #ps.plotLines('rate','sspPop[0]/spike:mean_spike_frequency',second_param='seed',save_figure_to='mean_spike_frequency.png')
        #ps.plotLines('rate','sspPop[0]/spike:mean_spike_frequency',second_param='dt',save_figure_to='mean_spike_frequency.png')
        ps.plotLines('rateHz','sgpPop[0]/spike:mean_spike_frequency',second_param='seed',save_figure_to='mean_spike_frequency_sgp.png')
        #ps.plotLines('periodms','sgPop[0]/spike:mean_spike_frequency',second_param='seed',save_figure_to='mean_spike_frequency_sg.png')

        import matplotlib.pyplot as plt

        plt.show()
    
    else:

        sim, net = generate()

        ################################################################################
        ###   Run in some simulators

        import sys

        check_to_generate_or_run(sys.argv, sim)

Ejemplo n.º 6
0
def read_graphml(filename, color_map=[]):
    parser = GraphMLParser()
    g = parser.parse(filename)
    
    g.set_root('n0')
    
    print dir(g)
    id = '%s_%s'%(filename.split('.')[0],filename.split('.')[1])
    net = Network(id=id)
    net.notes = "NeuroMLlite conversion of %s from https://www.neurodata.io/project/connectomes/"%filename
    #net.parameters = {}

    dummy_cell = Cell(id='testcell', pynn_cell='IF_cond_alpha')
    dummy_cell.parameters = { "tau_refrac":5, "i_offset":0 }
    net.cells.append(dummy_cell)

    net.synapses.append(Synapse(id='ampa', 
                                pynn_receptor_type='excitatory', 
                                pynn_synapse_type='cond_alpha', 
                                parameters={'e_rev':0, 'tau_syn':2}))

    for node in g.nodes():
        
        info = ''
        for a in node.attributes():
            info+=node.attr[a].value+'; '
        if len(info)>2: info = info[:-4]
            
        color = '%s %s %s'%(random.random(),random.random(),random.random()) \
                    if not info in color_map else color_map[info]
                    
        print('> Node: %s (%s), color: %s'%(node.id, info, color))
        p0 = Population(id=node.id, 
                        size=1, 
                        component=dummy_cell.id, 
                        properties={'color':color})

        net.populations.append(p0)
        
    for edge in g.edges():
        #print dir(edge)
        #print edge.attributes()
        src = edge.node1.id
        tgt = edge.node2.id
        weight = float(str(edge.attr['e_weight'].value)) if 'e_weight' in edge.attr else 1
        
        #print('>> Edge from %s -> %s, weight %s'%(src, tgt, weight))
        
        net.projections.append(Projection(id='proj_%s_%s'%(src,tgt),
                                          presynaptic=src, 
                                          postsynaptic=tgt,
                                          synapse='ampa',
                                          weight=weight,
                                          random_connectivity=RandomConnectivity(probability=1)))

    #g.show()
    
    #print(net)
    
    #print(net.to_json())
    new_file = net.to_json_file('%s.json'%net.id)
    
    duration=1000
    dt = 0.1
    
    sim = Simulation(id='Sim%s'%net.id,
                     network=new_file,
                     duration=duration,
                     dt=dt,
                     recordRates={'all':'*'})
    
    check_to_generate_or_run(sys.argv, sim)