def init_pylab(): ############################ FIGURES ######################################## from NeuroTools import check_dependency HAVE_MATPLOTLIB = check_dependency('matplotlib') if HAVE_MATPLOTLIB: import matplotlib matplotlib.use("Agg") # agg-backend, so we can create figures without x-server (no PDF, just PNG etc.) HAVE_PYLAB = check_dependency('pylab') if HAVE_PYLAB: import pylab # parameters for plots fig_width_pt = 500. # Get this from LaTeX using \showthe\columnwidth inches_per_pt = 1.0/72.27 # Convert pt to inches fig_width = fig_width_pt*inches_per_pt # width in inches fontsize = 8 # pe.edge_scale_chevrons, line_width = 64., .75 params = {'backend': 'Agg', 'origin': 'upper', 'font.family': 'serif', 'font.serif': 'Times', 'font.sans-serif': 'Arial', 'text.usetex': True, # 'mathtext.fontset': 'stix', #http://matplotlib.sourceforge.net/users/mathtext.html 'interpolation':'nearest', 'axes.labelsize': fontsize, 'text.fontsize': fontsize, 'legend.fontsize': fontsize, 'figure.subplot.bottom': 0.17, 'figure.subplot.left': 0.15, 'ytick.labelsize': fontsize, 'xtick.labelsize': fontsize, 'savefig.dpi': 100, } pylab.rcParams.update(params)
def gamma_hazard(x, a, b, dt=1e-4): """ Compute the hazard function for a gamma process with parameters a,b where a and b are the parameters of the gamma PDF: y(t) = x^(a-1) \exp(-x/b) / (\Gamma(a)*b^a) Inputs: x - in units of seconds a - dimensionless b - in units of seconds See also: inh_gamma_generator """ # Used by inh_gamma_generator # Ideally, I would like to see an implementation which does not depend on RPy # but the gamma_hazard_scipy above using scipy exhibits numerical problems, as it does not # support directly returning the log. if check_dependency('rpy'): from rpy import r # scipy.special.gammaincc has numerical problems #Hpre = -log(scipy.special.gammaincc(a,(x-dt)/b)) #Hpost = -log(scipy.special.gammaincc(a,(x+dt)/b)) # reverting to the good old r.pgamma Hpre = -r.pgamma(x-dt,shape=a,scale=b,lower=False,log=True) Hpost = -r.pgamma(x+dt,shape=a,scale=b,lower=False,log=True) val = 0.5*(Hpost-Hpre)/dt return val elif check_dependency('rpy2'): from rpy2.robjects import r # scipy.special.gammaincc has numerical problems #Hpre = -log(scipy.special.gammaincc(a,(x-dt)/b)) #Hpost = -log(scipy.special.gammaincc(a,(x+dt)/b)) # reverting to the good old r.pgamma Hpre = -r.pgamma(x-dt,shape=a,scale=b,lower=False,log=True)[0] Hpost = -r.pgamma(x+dt,shape=a,scale=b,lower=False,log=True)[0] val = 0.5*(Hpost-Hpre)/dt return val elif check_dependency('scipy'): return gamma_hazard_scipy(x, a, b, dt=dt) else: raise ImportError("gamma_hazard requires SciPy, RPy or RPy2 (http://rpy.sourceforge.net/)")
def inh_gamma_generator(self, a, b, t, t_stop, array=False): """ Returns a SpikeList whose spikes are a realization of an inhomogeneous gamma process (dynamic rate). The implementation uses the thinning method, as presented in the references. Inputs: ------- a,b - arrays of the parameters of the gamma PDF where a[i] and b[i] will be active on interval [t[i],t[i+1]] t - an array specifying the time bins (in milliseconds) at which to specify the rate t_stop - length of time to simulate process (in ms) array - if True, a numpy array of sorted spikes is returned, rather than a SpikeList object. Note: ----- t_start=t[0] a is a dimensionless quantity > 0, but typically on the order of 2-10. a = 1 results in a poisson process. b is assumed to be in units of 1/Hz (seconds). References: ----------- Eilif Muller, Lars Buesing, Johannes Schemmel, and Karlheinz Meier Spike-Frequency Adapting Neural Ensembles: Beyond Mean Adaptation and Renewal Theories Neural Comput. 2007 19: 2958-3010. Devroye, L. (1986). Non-uniform random variate generation. New York: Springer-Verlag. Examples: --------- See source:trunk/examples/stgen/inh_gamma_psth.py See also: --------- inh_poisson_generator, gamma_hazard """ if not self.dep_checked: self.have_dep = check_dependency('rpy') or check_dependency('rpy2') or check_dependency('scipy') self.dep_checked = True if self.have_dep: return self._inh_gamma_generator_python(a, b, t, t_stop, array) else: raise Exception("inh_gamma_generator is disabled as dependency SciPy|RPy|RPy2 was not found.")
def __init__(self, mean=None, std=None, repr_mode='ms', **params): """ repr_mode specifies how the dist is displayed, either mean,var ('ms', the default) or a,b ('ab') """ if check_dependency('scipy'): self.next = self._next_scipy self.repr_mode = repr_mode if 'm' in params and mean == None: mean = params['m'] if 's' in params and std == None: std = params['s'] # both mean and std not specified if (mean, std) == (None, None): if 'a' in params: a = params['a'] else: a = 1.0 if 'b' in params: b = params['b'] else: b = 1.0 else: if mean == None: mean = 0.0 if std == None: std = 1.0 a = mean**2 / std**2 b = mean / a ParameterDist.__init__(self, a=a, b=b) self.dist_name = 'GammaDist'
def __init__(self,mean=None,std=None,repr_mode='ms',**params): """ repr_mode specifies how the dist is displayed, either mean,var ('ms', the default) or a,b ('ab') """ if check_dependency('scipy'): self.next = self._next_scipy self.repr_mode = repr_mode if 'm' in params and mean==None: mean = params['m'] if 's' in params and std==None: std = params['s'] # both mean and std not specified if (mean,std)==(None,None): if 'a' in params: a = params['a'] else: a = 1.0 if 'b' in params: b = params['b'] else: b = 1.0 else: if mean==None: mean = 0.0 if std==None: std=1.0 a = mean**2/std**2 b = mean/a ParameterDist.__init__(self,a=a,b=b) self.dist_name = 'GammaDist'
def init_pylab(): ############################ FIGURES ######################################## from NeuroTools import check_dependency HAVE_MATPLOTLIB = check_dependency('matplotlib') if HAVE_MATPLOTLIB: import matplotlib matplotlib.use( "Agg" ) # agg-backend, so we can create figures without x-server (no PDF, just PNG etc.) HAVE_PYLAB = check_dependency('pylab') if HAVE_PYLAB: import pylab # parameters for plots fig_width_pt = 500. # Get this from LaTeX using \showthe\columnwidth inches_per_pt = 1.0 / 72.27 # Convert pt to inches fig_width = fig_width_pt * inches_per_pt # width in inches fontsize = 8 # pe.edge_scale_chevrons, line_width = 64., .75 params = { 'backend': 'Agg', 'origin': 'upper', 'font.family': 'serif', 'font.serif': 'Times', 'font.sans-serif': 'Arial', 'text.usetex': True, # 'mathtext.fontset': 'stix', #http://matplotlib.sourceforge.net/users/mathtext.html 'interpolation': 'nearest', 'axes.labelsize': fontsize, 'text.fontsize': fontsize, 'legend.fontsize': fontsize, 'figure.subplot.bottom': 0.17, 'figure.subplot.left': 0.15, 'ytick.labelsize': fontsize, 'xtick.labelsize': fontsize, 'savefig.dpi': 100, } pylab.rcParams.update(params)
def gamma_hazard_scipy(x, a, b, dt=1e-4): """ Compute the hazard function for a gamma process with parameters a,b where a and b are the parameters of the gamma PDF: y(t) = x^(a-1) \exp(-x/b) / (\Gamma(a)*b^a) Inputs: x - in units of seconds a - dimensionless b - in units of seconds See also: inh_gamma_generator """ # This algorithm is presently not used by # inh_gamma_generator as it has numerical problems # Try: # plot(stgen.gamma_hazard(arange(0,1000.0,0.1),10.0,1.0/50.0)) # and look for the kinks. if check_dependency('scipy'): from scipy.special import gammaincc Hpre = -log(gammaincc(a,(x-dt)/b)) Hpost = -log(gammaincc(a,(x+dt)/b)) val = 0.5*(Hpost-Hpre)/dt if isinstance(val,numpy.ndarray): val[numpy.isnan(val)] = 1.0/b return val elif numpy.isnan(val): return 1.0/b else: return val else: raise ImportError("gamma_hazard_scipy requires SciPy")
from NeuroTools import check_dependency HAVE_INTERVAL = check_dependency('interval') if HAVE_INTERVAL: from interval import * import numpy class Interval(object): """ Interval(start_times, end_times). Inputs: start_times - A list of the start times for all the sub intervals considered, in ms stop_times - A list of the stop times for all the sub intervals considered, in ms Examples: >> itv = Interval([0,100,200,300],[50,150,250,350]) >> itv.time_parameters() 0, 350 """ def __init__(self, start_times, end_times): """ Constructor of the Interval object. """ if HAVE_INTERVAL: self.start_times = start_times self.end_times = end_times
IPython controller to be set up. Look at parameter_search_example.py and test_parameter_search.py for usage examples. Todo: - provide utility functions to start/stop controllers and engines. - control logging of engines instead of just letting them dump to the console. - extend to non-grid search algorithms (e.g. evo, swarm), possibly using scipy.optimize. """ from NeuroTools import check_dependency # Check availability and version of IPython if check_dependency('IPython'): import IPython v = IPython.__version__.split('.') if float(v[2]) < 1. or float(v[1]) < 9.: print(""" ----------------- Dependency Warning --------------------- Warning: IPython must be version 0.9.1 or higher. Parallel searching will most likely not work.""") try: import IPython.kernel.client except ImportError: print(""" ----------------- Dependency Warning --------------------- Warning: IPython seems to be installed without distributed parallelization support. Parallel searching will not work.""")
analog signals) NestFile - object used to manipulate raw NEST file that would not have been saved by pyNN (without headers) DataHandler - object to establish the interface between NeuroTools.signals and NeuroTools.io All those objects can be used with NeuroTools.signals >> data = StandardTextFile("my_data.dat") >> spikes = load(data,'s') """ from NeuroTools import check_dependency import os, logging, cPickle, numpy DEFAULT_BUFFER_SIZE = -1 HAVE_TABLEIO = check_dependency('TableIO') if HAVE_TABLEIO: import TableIO class FileHandler(object): """ Class to handle all the file read/write methods for the key objects of the signals class, i.e SpikeList and AnalogSignalList. Could be extented This is an abstract class that will be implemented for each format (txt, pickle, hdf5) The key methods of the class are: write(object) - Write an object to a file read_spikes(params) - Read a SpikeList file with some params read_analogs(type, params) - Read an AnalogSignalList of type `type` with some params
warning - plots data with level WARNING error - plots data with level ERROR critical - plots data with level CRITICAL exception - plots data with level ERROR log - plots data with a user-specified level """ import zipfile, atexit, os from NeuroTools import check_dependency from datetime import datetime from logging import CRITICAL, DEBUG, ERROR, FATAL, INFO, WARN, WARNING, NOTSET from time import sleep if check_dependency('matplotlib'): import matplotlib matplotlib.use('Agg') if check_dependency('pylab'): import pylab _filename = 'visual_log.zip' _zipfile = None _level = INFO _last_timestamp = '' def _remove_if_empty(): if len(_zipfile.namelist()) == 0 and os.path.exists(_filename): os.remove(_filename)
NestFile - object used to manipulate raw NEST file that would not have been saved by pyNN (without headers) DataHandler - object to establish the interface between NeuroTools.signals and NeuroTools.io All those objects can be used with NeuroTools.signals >> data = StandardTextFile("my_data.dat") >> spikes = load(data,'s') """ from NeuroTools import check_dependency import os, logging, cPickle, numpy DEFAULT_BUFFER_SIZE = -1 HAVE_TABLEIO = check_dependency('TableIO') if HAVE_TABLEIO: import TableIO class FileHandler(object): """ Class to handle all the file read/write methods for the key objects of the signals class, i.e SpikeList and AnalogSignalList. Could be extented This is an abstract class that will be implemented for each format (txt, pickle, hdf5) The key methods of the class are: write(object) - Write an object to a file read_spikes(params) - Read a SpikeList file with some params read_analogs(type, params) - Read an AnalogSignalList of type `type` with some params
import sys, os.path import numpy import tempfile, shutil import logging from NeuroTools import check_dependency from NeuroTools.plotting import progress_bar if check_dependency('matplotlib'): from matplotlib.figure import Figure from matplotlib.lines import Line2D from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.image import imread import matplotlib.cm class MultipanelMovie(object): def __init__(self, title='', frame_duration=40.0): self.fig = Figure() self.canvas = FigureCanvas(self.fig) self.panels = [] self.title = title self.frame_duration = frame_duration def add_panel(self, obj, bottomleft_corner, width, height): """ obj must be an object that has a string attribute `plot_function` and a method `next_frame()`. """ panel = self.fig.add_axes( [bottomleft_corner[0], bottomleft_corner[1], width, height]) self.panels.append((panel, obj))
from NeuroTools import check_dependency HAVE_INTERVAL = check_dependency('interval') if HAVE_INTERVAL: from interval import * import numpy class Interval(object): """ Interval(start_times, end_times). Inputs: start_times - A list of the start times for all the sub intervals considered, in ms stop_times - A list of the stop times for all the sub intervals considered, in ms Examples: >> itv = Interval([0,100,200,300],[50,150,250,350]) >> itv.time_parameters() 0, 350 """ def __init__(self, start_times, end_times) : """ Constructor of the Interval object. """ if HAVE_INTERVAL: self.start_times = start_times self.end_times = end_times
--------- .. autosummary:: :nosignatures: ccf crosscorrelate make_kernel simple_frequency_spectrum """ import numpy as np from NeuroTools import check_dependency HAVE_MATPLOTLIB = check_dependency('matplotlib') if HAVE_MATPLOTLIB: import matplotlib matplotlib.use('Agg') else: MATPLOTLIB_ERROR = "The matplotlib package was not detected" HAVE_PYLAB = check_dependency('pylab') if HAVE_PYLAB: import pylab else: PYLAB_ERROR = "The pylab package was not detected" def ccf(x, y, axis=None): """Fast cross correlation function based on fft.
get_display - returns a pylab object with a plot() function to draw the plots. progress_bar - prints a progress bar to stdout, filled to the given ratio. pylab_params - returns a dictionary with a set of parameters that help to nicely format figures by updating the pylab run command parameters dictionary 'pylab.rcParams'. set_axis_limits - defines the axis limits in a plot. set_labels - defines the axis labels of a plot. set_pylab_params - updates a set of parameters within the the pylab run command parameters dictionary 'pylab.rcParams' in order to achieve nicely formatted figures. save_2D_image - saves a 2D numpy array of gray shades between 0 and 1 to a PNG file. save_2D_movie - saves a list of 2D numpy arrays of gray shades between 0 and 1 to a zipped tree of PNG files. """ import sys, numpy from NeuroTools import check_dependency # Check availability of pylab (essential!) if check_dependency('matplotlib'): from matplotlib import use use('Agg') from matplotlib.figure import Figure from matplotlib.lines import Line2D from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas if check_dependency('pylab'): import pylab # Check availability of PIL PILIMAGEUSE = check_dependency('PIL') if PILIMAGEUSE: import PIL.Image as Image ######################################################## # UNIVERSAL FUNCTIONS AND CLASSES FOR NORMAL PYLAB USE #
get_display - returns a pylab object with a plot() function to draw the plots. progress_bar - prints a progress bar to stdout, filled to the given ratio. pylab_params - returns a dictionary with a set of parameters that help to nicely format figures by updating the pylab run command parameters dictionary 'pylab.rcParams'. set_axis_limits - defines the axis limits in a plot. set_labels - defines the axis labels of a plot. set_pylab_params - updates a set of parameters within the the pylab run command parameters dictionary 'pylab.rcParams' in order to achieve nicely formatted figures. save_2D_image - saves a 2D numpy array of gray shades between 0 and 1 to a PNG file. save_2D_movie - saves a list of 2D numpy arrays of gray shades between 0 and 1 to a zipped tree of PNG files. """ import sys, numpy from NeuroTools import check_dependency # Check availability of pylab (essential!) if check_dependency('matplotlib'): from matplotlib import use use('Agg') from matplotlib.figure import Figure from matplotlib.lines import Line2D from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas if check_dependency('pylab'): import pylab # Check availability of PIL PILIMAGEUSE = check_dependency('PIL') if PILIMAGEUSE: import PIL.Image as Image
load_currentlist - function to load a CurrentList object (inherits from AnalogSignalList) from a file. Same comments on format as previously. load_conductancelist - function to load a ConductanceList object (inherits from AnalogSignalList) from a file. Same comments on format as previously. load_conductancelist returns two ConductanceLists, one for the excitatory conductance and one for the inhibitory conductance load - a generic loader for all the previous load methods. See also NeuroTools.signals.spikes """ import os, re, numpy from NeuroTools import check_dependency, check_numpy_version from NeuroTools.io import * from NeuroTools.plotting import get_display, set_axis_limits, set_labels, SimpleMultiplot HAVE_MATPLOTLIB = check_dependency('matplotlib') if HAVE_MATPLOTLIB: import matplotlib matplotlib.use('Agg') HAVE_PYLAB = check_dependency('pylab') if HAVE_PYLAB: import pylab else: PYLAB_ERROR = "The pylab package was not detected" if not HAVE_MATPLOTLIB: MATPLOTLIB_ERROR = "The matplotlib package was not detected" newnum = check_numpy_version() from spikes import SpikeList, SpikeTrain from pairs import *
load_currentlist - function to load a CurrentList object (inherits from AnalogSignalList) from a file. Same comments on format as previously. load_conductancelist - function to load a ConductanceList object (inherits from AnalogSignalList) from a file. Same comments on format as previously. load_conductancelist returns two ConductanceLists, one for the excitatory conductance and one for the inhibitory conductance load - a generic loader for all the previous load methods. See also NeuroTools.signals.spikes """ import os, re, numpy from NeuroTools import check_dependency, check_numpy_version from NeuroTools.io import * from NeuroTools.plotting import get_display, set_axis_limits, set_labels, SimpleMultiplot HAVE_MATPLOTLIB = check_dependency("matplotlib") if HAVE_MATPLOTLIB: import matplotlib matplotlib.use("Agg") HAVE_PYLAB = check_dependency("pylab") if HAVE_PYLAB: import pylab else: PYLAB_ERROR = "The pylab package was not detected" if not HAVE_MATPLOTLIB: MATPLOTLIB_ERROR = "The matplotlib package was not detected" newnum = check_numpy_version()
info - plots data with level INFO warning - plots data with level WARNING error - plots data with level ERROR critical - plots data with level CRITICAL exception - plots data with level ERROR log - plots data with a user-specified level """ import zipfile, atexit, os from NeuroTools import check_dependency from datetime import datetime from logging import CRITICAL, DEBUG, ERROR, FATAL, INFO, WARN, WARNING, NOTSET from time import sleep if check_dependency('matplotlib'): import matplotlib matplotlib.use('Agg') if check_dependency('pylab'): import pylab _filename = 'visual_log.zip' _zipfile = None _level = INFO _last_timestamp = '' def _remove_if_empty(): if len(_zipfile.namelist()) == 0 and os.path.exists(_filename): os.remove(_filename)
import sys, os.path import numpy import tempfile, shutil import logging from NeuroTools import check_dependency from NeuroTools.plotting import progress_bar if check_dependency('matplotlib'): from matplotlib.figure import Figure from matplotlib.lines import Line2D from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.image import imread import matplotlib.cm class MultipanelMovie(object): def __init__(self, title='', frame_duration=40.0): self.fig = Figure() self.canvas = FigureCanvas(self.fig) self.panels = [] self.title = title self.frame_duration = frame_duration def add_panel(self, obj, bottomleft_corner, width, height): """ obj must be an object that has a string attribute `plot_function` and a method `next_frame()`. """ panel = self.fig.add_axes([bottomleft_corner[0], bottomleft_corner[1], width, height])