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 else: raise ImportError( "gamma_hazard requires RPy or RPy2 (http://rpy.sourceforge.net/)")
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 else: raise ImportError("gamma_hazard requires RPy or RPy2 (http://rpy.sourceforge.net/)")
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 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
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
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.rpy_checked: self.have_rpy = check_dependency('rpy') or check_dependency('rpy2') self.rpy_checked = True if self.have_rpy: return self._inh_gamma_generator_python(a, b, t, t_stop, array) else: raise Exception( "inh_gamma_generator is disabled as dependency RPy|RPy2 was not found." )
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.rpy_checked: self.have_rpy = check_dependency('rpy') or check_dependency('rpy2') self.rpy_checked = True if self.have_rpy: return self._inh_gamma_generator_python(a, b, t, t_stop, array) else: raise Exception("inh_gamma_generator is disabled as dependency RPy|RPy2 was not found.")
""" Assorted tools relating to the FACETS Knowledge Base (FKB) $Id: fkbtools.py 131 2007-03-15 08:08:13Z apdavison $ """ from neurotools import check_dependency if check_dependency("PIL"): import PIL if check_dependency("tables"): import tables use_hdf5 = check_dependency("neurotools.facets.hdf5") if use_hdf5: import neurotools.facets.hdf5.FileExtension as file_extension import neurotools.facets.hdf5.Movie as movie if check_dependency("srblib"): import srblib else: import urllib as srblib import os, sys, zipfile, tempfile, shutil, subprocess, logging, time, numpy def getFromURL(url): """ Get a remote file and save in a temporary directory. Return the path to the local file. """
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 from neurotools import check_dependency 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
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.""")
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)
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
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.0 or float(v[1]) < 9.0: 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 ( """
fig = pylab.figure() pylab.imshow(arr) if arr.min() != arr.max(): pylab.colorbar() fig.savefig(filename) """ Convert between FACETS movie (image sequence) formats At the moment, these are: * zipped directory containing png files, a frames text file and a parameters text file * HDF5 """ if check_dependency('neurotools.facets.hdf5'): from neurotools.facets.hdf5 import FileExtension as file_extension from neurotools.facets.fkbtools import png_to_hdf5, hdf5_to_png import os, sys def show(url): """Display an HDF5 movie.""" file = file_extension.openHDF5File(url, "r") file.showMovie("/Movie") file.close() #=============================================================================== if __name__ == "__main__": if len(sys.argv) > 1:
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 #
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])
import pylab fig = pylab.figure() pylab.imshow(arr) if arr.min() != arr.max(): pylab.colorbar() fig.savefig(filename) """ Convert between FACETS movie (image sequence) formats At the moment, these are: * zipped directory containing png files, a frames text file and a parameters text file * HDF5 """ if check_dependency('neurotools.facets.hdf5'): from neurotools.facets.hdf5 import FileExtension as file_extension from neurotools.facets.fkbtools import png_to_hdf5, hdf5_to_png import os, sys def show(url): """Display an HDF5 movie.""" file = file_extension.openHDF5File(url, "r") file.showMovie("/Movie") file.close() #=============================================================================== if __name__ == "__main__":
""" Assorted tools relating to the FACETS Knowledge Base (FKB) $Id: fkbtools.py 131 2007-03-15 08:08:13Z apdavison $ """ from neurotools import check_dependency if check_dependency('PIL'): import PIL if check_dependency('tables'): import tables use_hdf5 = check_dependency('neurotools.facets.hdf5') if use_hdf5: import neurotools.facets.hdf5.FileExtension as file_extension import neurotools.facets.hdf5.Movie as movie if check_dependency('srblib'): import srblib else: import urllib as srblib import os, sys, zipfile, tempfile, shutil, subprocess, logging, time, numpy def getFromURL(url): """ Get a remote file and save in a temporary directory. Return the path to the local file. """