File: comparison_plot.py

package info (click to toggle)
pynn 0.10.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,156 kB
  • sloc: python: 25,612; cpp: 320; makefile: 117; sh: 80
file content (78 lines) | stat: -rw-r--r-- 2,867 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
A fairly generic script to produce plots that compare two or more Neo data sets.

Usage:

    python comparison_plot.py <datafile1>, <datafile2>, etc.

"""

import argparse
import os
from datetime import datetime
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import numpy as np
import neo
from neo.io import get_io
from pyNN.utility.plotting import comparison_plot


def variable_names(segment):
    return set(signal.name for signal in segment.analogsignals)


def plot_signal(panel, signal, index, colour='b', linewidth='1', label=''):
    label = "%s (Neuron %d)" % (label, signal.array_annotations["channel_index"][index])
    panel.plot(signal.times, signal[:, index], colour, linewidth=linewidth, label=label)
    panel.set_ylabel("%s (%s)" % (signal.name, signal.units._dimensionality.string))
    plt.setp(plt.gca().get_xticklabels(), visible=False)


def plot(datafiles, output_file, extra_annotation=None):
    print(datafiles)
    print(output_file)
    # load data
    blocks = [get_io(datafile).read_block() for datafile in datafiles]
    #   note: Neo needs a pretty printer that is not tied to IPython
#    for block in blocks:
#        print(block.describe())
    #   for now take only the first segment
    segments = [block.segments[0] for block in blocks]
    labels = [block.annotations['simulator'] for block in blocks]

    # build annotations
    script_name = blocks[0].annotations.get('script_name', '')
    if script_name:
        for block in blocks[1:]:
            assert block.annotations['script_name'] == script_name

    #   also consider adding metadata to PNG file - see http://stackoverflow.com/questions/10532614/can-matplotlib-add-metadata-to-saved-figures
    context = ["Generated by: %s" % __file__,
               "Working directory: %s" % os.getcwd(),
               "Timestamp: %s" % datetime.now().strftime("%Y-%m-%d %H:%M:%S%z"),
               "Output file: %s" % output_file,
               "Input file(s): %s" % "\n                    ".join(datafiles)]
    if extra_annotation:
        context.append(extra_annotation)
    annotations = "\n".join(context)

    # create and save plot
    fig = comparison_plot(segments, labels, title=script_name,
                          annotations=annotations)
    fig.save(output_file)


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("datafiles", metavar="datafile", nargs="+",
                        help="a list of data files in a Neo-supported format")
    parser.add_argument("-o", "--output-file", default="output.png",
                        help="output filename")
    parser.add_argument("-a", "--annotation", help="additional annotation (optional)")
    args = parser.parse_args()
    plot(args.datafiles, output_file=args.output_file, extra_annotation=args.annotation)


if __name__ == "__main__":
    main()