Exemplo n.º 1
0
def test_plot_monitors():
    set_device('runtime')
    group = NeuronGroup(10, 'dv/dt = -v/(10*ms) : volt', threshold='False',
                        method='linear')
    group.v = np.linspace(0, 1, 10)*mV
    spike_mon = SpikeMonitor(group)
    rate_mon = PopulationRateMonitor(group)
    state_mon = StateMonitor(group, 'v', record=[3, 5])
    run(10*ms)

    # Just checking whether the plotting does not fail with an error and that
    # it retuns an Axis object as promised
    ax = brian_plot(spike_mon)
    assert isinstance(ax, matplotlib.axes.Axes)
    plt.close()
    ax = plot_raster(spike_mon.i, spike_mon.t)
    assert isinstance(ax, matplotlib.axes.Axes)
    plt.close()
    ax = brian_plot(rate_mon)
    assert isinstance(ax, matplotlib.axes.Axes)
    plt.close()
    ax = plot_rate(rate_mon.t, rate_mon.rate)
    assert isinstance(ax, matplotlib.axes.Axes)
    plt.close()
    ax = brian_plot(state_mon)
    assert isinstance(ax, matplotlib.axes.Axes)
    plt.close()
    ax = plot_state(state_mon.t, state_mon.v.T)
    assert isinstance(ax, matplotlib.axes.Axes)
def generate_state_mon_latex(mon):
    '''
    write StateMonitor and SpikeMonitor group into file
    '''

    if len(mon.record_variables) != 1:
        warnings.warn(
            '''brian_plot only works for a StateMonitor that records a single variable. 
                         So brian_docs will ignore StateMonitors which record more than one variables. 
                         For documentation of those StateMonitors, it is suggested to put those 
                         variables in different StateMonitors, and ensure that each StateMonitor
                         only has one variable''')
        return ''
    else:
        pic_path = 'tmp/' + mon.name + '.pdf'
        plt.close()
        brian_plot(mon)
        pp.savefig(pic_path)
        mon_name = replace_underscore(mon.name)

        text = []
        text.append('\\section{StateMonitor ' + mon_name + ':}')
        text.append('\\begin{center}')
        text.append('\\includegraphics[width=\\textwidth]{' + pic_path + '}')
        text.append('\\end{center}')
        if isinstance(mon.source, Subgroup):
            text.append('This graph records a subgroup start from ' +
                        str(mon.source.start) + ', stop at ' +
                        str(mon.source.stop) + '.')

        return '\n'.join(text)
Exemplo n.º 3
0
def test_plot_synapses():
    set_device('runtime')
    group = NeuronGroup(10, 'dv/dt = -v/(10*ms) : volt', threshold='False')
    group.v = np.linspace(0, 1, 10)*mV
    synapses = Synapses(group, group, 'w : volt', on_pre='v += w')
    synapses.connect('i != j')
    synapses.w = 'i*0.1*mV'
    # Just checking whether the plotting does not fail with an error and that
    # it retuns an Axis object as promised
    ax = brian_plot(synapses)
    assert isinstance(ax, matplotlib.axes.Axes)
    plt.close()
    ax = brian_plot(synapses.w)
    assert isinstance(ax, matplotlib.axes.Axes)
    plt.close()
    ax = brian_plot(synapses.delay)
    assert isinstance(ax, matplotlib.axes.Axes)
    plt.close()
    ax = plot_synapses(synapses.i, synapses.j)
    assert isinstance(ax, matplotlib.axes.Axes)
    plt.close()
    ax = plot_synapses(synapses.i, synapses.j, plot_type='scatter')
    assert isinstance(ax, matplotlib.axes.Axes)
    plt.close()
    ax = plot_synapses(synapses.i, synapses.j, plot_type='image')
    add_background_pattern(ax)
    assert isinstance(ax, matplotlib.axes.Axes)
    plt.close()
    ax = plot_synapses(synapses.i, synapses.j, plot_type='hexbin')
    assert isinstance(ax, matplotlib.axes.Axes)
    plt.close()
    ax = plot_synapses(synapses.i, synapses.j, synapses.w)
    assert isinstance(ax, matplotlib.axes.Axes)
    plt.close()
    ax = plot_synapses(synapses.i, synapses.j, synapses.w, plot_type='scatter')
    assert isinstance(ax, matplotlib.axes.Axes)
    plt.close()
    ax = plot_synapses(synapses.i, synapses.j, synapses.w, plot_type='image')
    assert isinstance(ax, matplotlib.axes.Axes)
    plt.close()
    ax = plot_synapses(synapses.i, synapses.j, synapses.w, plot_type='hexbin')
    assert isinstance(ax, matplotlib.axes.Axes)
    plt.close()

    synapses.connect('i > 5')  # More than one synapse per connection
    brian_plot(synapses)
    plt.close()
    # It should be possible to plot synaptic variables for multiple connections
    # with hexbin
    ax = plot_synapses(synapses.i, synapses.j, synapses.w, plot_type='hexbin')
    assert isinstance(ax, matplotlib.axes.Axes)
    plt.close()
    def plot_synapse(syn):
        pic_path = 'tmp/' + syn.name + '.pdf'
        plt.close()
        brian_plot(syn)
        pp.savefig(pic_path)
        NG_name = replace_underscore(syn.name)

        plt_string = []
        plt_string.append('\\item\n\\textbf{graph of ')
        plt_string.append(NG_name)
        plt_string.append(
            ':}\\\\\n\\begin{center}\n\\includegraphics[width=\\textwidth]{')
        plt_string.append(pic_path)
        plt_string.append('}\n\\end{center}')

        return ''.join(plt_string)
def test_plot_morphology():
    set_device('runtime')
    # Only testing 2D plotting for now
    morpho = Soma(diameter=30 * um)
    morpho.axon = Cylinder(diameter=10 * um, n=10, length=100 * um)
    morpho.dend = Section(diameter=np.linspace(10, 1, 11) * um,
                          n=10,
                          length=np.ones(10) * 5 * um)
    morpho = morpho.generate_coordinates()

    # Just checking whether the plotting does not fail with an error and that
    # it retuns an Axis object as promised
    ax = brian_plot(morpho)
    assert isinstance(ax, matplotlib.axes.Axes)
    plt.close()
    ax = plot_dendrogram(morpho)
    assert isinstance(ax, matplotlib.axes.Axes)
    plt.close()
    ax = plot_morphology(morpho)
    assert isinstance(ax, matplotlib.axes.Axes)
    plt.close()
    ax = plot_morphology(morpho, show_diameter=True)
    assert isinstance(ax, matplotlib.axes.Axes)
    plt.close()
    ax = plot_morphology(morpho, show_compartments=True)
    assert isinstance(ax, matplotlib.axes.Axes)
    plt.close()
    ax = plot_morphology(morpho, show_diameter=True, show_compartments=True)
    assert isinstance(ax, matplotlib.axes.Axes)
    plt.close()
    def generate_SpatialNeuron_latex(NG):

        text = []
        text.append('\\item\\textbf{Cm:}')
        text.append('$' + NG.Cm[0].in_best_unit() + '$')
        text.append('\\item\\textbf{Ri:}')
        text.append('$' + str(NG.Ri.variable.get_value()[0]) + '\:' +
                    NG.Ri.unit.latexname + '$')

        if NG.morphology is not None:
            morpho = NG.morphology
            pic_path = 'tmp/' + NG.name + '.pdf'
            plt.close()
            brian_plot(morpho)
            pp.savefig(pic_path)
            NG_name = replace_underscore(NG.name)

            text.append('\\item \\textbf{Morphology:}')
            text.append('\\begin{itemize}')

            text.append('\\item\\textbf{Morphology graph of ' + NG_name + ':}')
            text.append('\\begin{center}')
            text.append('\\includegraphics[width=\\textwidth]{' + pic_path +
                        '}')
            text.append('\\end{center}')

            text.append('\\item \\textbf{class:}' + type(morpho).__name__)
            if isinstance(morpho, Cylinder):
                text.append('\\item \\textbf{diameter:}')
                text.append('$' + morpho.diameter[0].in_best_unit() + '$')
                text.append('\\item \\textbf{n:}')
                text.append(str(morpho.n))
                text.append('\\item \\textbf{length:}')
                text.append('${length!r}$'.format(length=sum(morpho._length)))
            elif isinstance(morpho, Soma):
                text.append('\\item \\textbf{diameter:}')
                text.append('$' + morpho.diameter[0].in_best_unit() + '$')

            text.append('\\end{itemize}')

        return '\n'.join(text)
def generate_spike_mon_latex(mon):
    '''
    write StateMonitor and SpikeMonitor group into file
    '''
    pic_path = 'tmp/' + mon.name + '.pdf'
    plt.close()
    brian_plot(mon)
    pp.savefig(pic_path)
    mon_name = replace_underscore(mon.name)

    text = []
    text.append('\\section{SpikeMonitor ' + mon_name + ':}')
    text.append('\\begin{center}')
    text.append('\\includegraphics[width=\\textwidth]{' + pic_path + '}')
    text.append('\\end{center}')
    if isinstance(mon.source, Subgroup):
        text.append('This graph records a subgroup start from ' +
                    str(mon.source.start) + ', stop at ' +
                    str(mon.source.stop) + '.')

    return '\n'.join(text)