Example #1
0
def initialize_configs(config_path):
    config_modules = []

    all_configs = config_names(config_path)

    if (len(all_configs) == 0):
        print("Error: no config files found in config path '{0}'".format(
            config_path),
              file=sys.stderr)
        sys.exit(1)

    config_helper = Configs(all_configs)
    config_helper.load_modules(config_modules)

    # Give at least one module the config helper
    config_modules[0].config_helper = config_helper

    # Step Four: Load jenni

    try:
        from __init__ import run
    except ImportError:
        try:
            from jenni import run
        except ImportError:
            print("Error: Couldn't find jenni to import", file=sys.stderr)
            sys.exit(1)

    # Step Five: Initialise And Run The jennies

    # @@ ignore SIGHUP
    for config_module in config_modules:
        run(config_module)  # @@ thread this
Example #2
0
def initialize_configs(config_path):
    config_modules = []

    all_configs = config_names(config_path)

    if(len(all_configs) == 0):
        print("Error: no config files found in config path '{0}'".format(config_path), file=sys.stderr)
        sys.exit(1)

    config_helper = Configs(all_configs)
    config_helper.load_modules(config_modules)

    # Give at least one module the config helper
    config_modules[0].config_helper = config_helper

    # Step Four: Load jenni

    try: from __init__ import run
    except ImportError:
        try: from jenni import run
        except ImportError:
            print("Error: Couldn't find jenni to import", file=sys.stderr)
            sys.exit(1)

    # Step Five: Initialise And Run The jennies

    # @@ ignore SIGHUP
    for config_module in config_modules:
        run(config_module) # @@ thread this
Example #3
0
def main(argv=None): 
   # Step One: Parse The Command Line

   parser = optparse.OptionParser('%prog [options]')
   parser.add_option('-c', '--config', metavar='fn', 
      help='use this configuration file or directory')
   opts, args = parser.parse_args(argv)
   if args: print >> sys.stderr, 'Warning: ignoring spurious arguments'

   # Step Two: Check Dependencies

   check_python_version() # require python2.4 or later
   if not opts.config:
      check_dotdir() # require ~/.caesar, or make it and exit

   # Step Three: Load The Configurations

   config_modules = []
   for config_name in config_names(opts.config): 
      name = os.path.basename(config_name).split('.')[0] + '_config'
      module = imp.load_source(name, config_name)
      module.filename = config_name

      if not hasattr(module, 'prefix'): 
         module.prefix = r'\!'

      if not hasattr(module, 'name'): 
         module.name = 'Caesar, a fork of Phenny inamidst.com/phenny'

      if not hasattr(module, 'port'): 
         module.port = 6667

      if not hasattr(module, 'password'): 
         module.password = None

      if module.host == 'irc.example.net': 
         error = ('Error: you must edit the config file first!\n' + 
                  "You're currently using %s" % module.filename)
         print >> sys.stderr, error
         sys.exit(1)

      config_modules.append(module)

   # Step Four: Load caesar

   try: from __init__ import run
   except ImportError: 
      try: from caesar import run
      except ImportError: 
         print >> sys.stderr, "Error: Couldn't find caesar to import"
         sys.exit(1)

   # Step Five: Initialise And Run The Phennies

   # @@ ignore SIGHUP
   for config_module in config_modules: 
      run(config_module) # @@ thread this
Example #4
0
def main():
    pNode = AppCore.NodeGraph.createNode('Clip')
    pNode['xpos'].setValue(0)
    pNode['ypos'].setValue(0)
    pNode['file'].setValue('*NWSTORAGE/')
    
    AppCore.ViewerWidget.setInput(0, pNode)
    
    MyApp.run()
Example #5
0
def install_hdf():
    meta = get_meta('hdf5')
    fab.run('mkdir -p '+meta.src)
    with fab.cd(meta.src):
        fab.run('wget '+meta.url)
        fab.run('tar xzvf '+meta.tar_name)
        with fab.cd(meta.base):
            fab.run('./configure --prefix={0} --enable-shared --enable-hl'.format(meta.install))
Example #6
0
 def run_ssasc(self, theta, N, O):
     # Initialise the library for computing pattern probabilities
     transforms.initialise(N, O)
     # Compute probability from theta values
     p = numpy.zeros((self.T, 2**N))
     for i in xrange(self.T):
         p[i,:] = transforms.compute_p(theta[i,:])
     # Generate spikes according to those probabilities
     spikes = synthesis.generate_spikes(p, self.R, seed=self.spike_seed)
     # Run the algorithm!
     emd = __init__.run(spikes, O)
     # Compute the KL divergence between real and estimated parameters
     kld = klic(theta, emd.theta_s, emd.N)
     # Check that KL divergence is OK
     if numpy.any(kld[50:-50] > .01):
         self.plot(theta, emd.theta_s, emd.sigma_s, emd.y, kld, emd.N, emd.T,
             emd.D)
     self.assertFalse(numpy.any(kld[50:-50] > .01))
Example #7
0
def install_netCDF4():
    meta = get_meta('netCDF4')
    meta_hdf5 = get_meta('hdf5')
    fab.sudo('apt-get install libcurl4-openssl-dev')
    fab.run('mkdir -p '+meta.src)
    with fab.cd(meta.src):
        fab.run('wget '+meta.url)
        fab.run('tar xzvf '+meta.tar_name)
        with fab.cd(meta.base):
            fab.run('export LDFLAGS=-L{0}/lib'.format(meta_hdf5.install))
            fab.run('export CPPFLAGS=-I{0}R/include'.format(meta_hdf5.install))
            fab.run('export LD_LIBRARY_PATH={0}/lib'.format(meta_hdf5.install))
            fab.run('./configure --enable-netcdf-4 --enable-dap --enable-shared --prefix='+meta.install)
Example #8
0
#===============================================================================
# @License: 
#    This example file is public domain. See ADDENDUM section in LICENSE.
#    You may do the following things with this file without restrictions or conditions:
#        1. Modify it.
#        2. Remove or modify this section to your liking.
#        3. Redistribute it under any licensing terms that you wish.
#        4. Make copyright claims to derivative works of this file.
#
#    This library is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Lesser General Public License for more details.
#===============================================================================

import os
if __name__ == '__main__':
    if os.path.abspath(__file__).split(os.sep)[-2] == 'MediaApp':
        import __init__ as MediaApp
        MediaApp.run()
    else:
        ##### Rename MyApp to your App's name here #####
        import __init__ as MyApp
        MyApp.run()
Example #9
0
# Generate spikes by Gibbs sampling!
# Sample steps: How many samples are dropped to make them independent
# Pre_n: Determines how many samples are used for the burn in phase
spikes = synthesis.generate_spikes_gibbs(theta, N, O, R, pre_n=100,
                                         sample_steps=1)
# Generate spikes in parallel
# num_proc: How many processes are started in parallel
# spikes = synthesis.generate_spikes_gibbs_parallel(theta, N, O, R, pre_n=100,
#                                          sample_steps=1, num_proc=4)

# ----- ALGORITHM EXECUTION -----
# Local module
import __init__ # From outside this folder, this would be 'import ssll'

# Run the algorithm with pseudo likelihood, bethe approximation!
emd = __init__.run(spikes, O, map_function='cg', lmbda1=200, lmbda2=200,
                   param_est='pseudo', param_est_eta='bethe_hybrid')

# Run the algorithm with pseudo likelihood, TAP approximation!
# emd = __init__.run(spikes, O, map_function='cg', lmbda1=200, lmbda2=200,
#                    param_est='pseudo', param_est_eta='mf')


# ----- PLOTTING -----
# Global module
import pylab

# Set up an output figure
fig, ax = pylab.subplots(2, 1, sharex=True)
# Plot underlying theta traces
ax[0].plot(theta[:,0], c='b', linestyle='--')
ax[0].plot(theta[:,1], c='r', linestyle='--')
Example #10
0
def main():
    global parser

    # use ArgumentParser to interpret commandline options
    parser = argparse.ArgumentParser(_("usage: sbot [options] inputfile.bot [args]"))
    parser.add_argument("script")
    parser.add_argument("-o",
                    "--outputfile",
                    dest="outputfile",
                    help=_("run script and output to FILE (accepts .svg, .ps, .pdf and .png extensions)"),
                    metavar="FILE")
    parser.add_argument("-w",
                    "--window",
                    action="store_true",
                    dest="window",
                    default=False,
                    help=_("run script in a GTK window")
                    )
    parser.add_argument("-f",
                    "--fullscreen",
                    action="store_true",
                    dest="fullscreen",
                    default=False,
                    help=_("run in fullscreen mode")
                    )
    parser.add_argument("-t",
                    "--title",
                    action="store",
                    dest="title",
                    default=None,
                    help=_("Set window title")
                    )
    parser.add_argument("-s",
                    "--socketserver",
                    action="store_true",
                    dest="socketserver",
                    default=False,
                    help=_("run a socket server for external control (will run the script in windowed mode)"))
    parser.add_argument("-dv",
                    "--disable-vars",
                    action="store_true",
                    dest="disable_vars",
                    default=False,
                    help=_("disable the variables pane when in windowed mode."))
    parser.add_argument("-dt",
                    "--disable-background-thread",
                    action="store_true",
                    dest="disable_background_thread",
                    default=False,
                    help=_("disable running bot code in background thread."))
    parser.add_argument("-p",
                    "--serverport",
                    type=int,
                    dest="serverport",
                    default=DEFAULT_SERVERPORT,
                    help=_("set socketserver port to listen for connections (default is 7777)"))
    parser.add_argument("-r",
                    "--repeat",
                    type=int,
                    dest="repeat",
                    default=False,
                    help=_("set number of iteration, multiple images will be produced"))
    parser.add_argument("-g",
                    "--grammar",
                    dest="grammar",
                    default=NODEBOX,
                    help=_("Select the bot grammar 'nodebox' (default) or 'drawbot' languages"),
                    )
    parser.add_argument("-c",
                    "--close",
                    action="store_true",
                    dest="close",
                    default=False,
                    help=_("Close window after running bot (use with -r for benchmarking)"),
                    )
    parser.add_argument("-v",
                    "--vars",
                    dest="vars",
                    default=False,
                    help=_("Initial variables, in JSON (Note: Single quotes OUTSIDE, double INSIDE) --vars='{\"variable1\": 1}'"),
                    )
    parser.add_argument("-ns",
                    "--namespace",
                    dest="namespace",
                    default=None,
                    help=_("Initial namespace, in JSON (Note: Single quotes OUTSIDE, double INSIDE) --namespace='{\"variable1\": 1}'"),
                    )
    parser.add_argument("-l",
                    "--l",
                    dest="shell",
                    action="store_true",
                    default=False,
                    help=_("Simple shell - for IDE interaction"),
                    )
    parser.add_argument("-a",
                    "--args",
                    dest="script_args",
                    help=_("Pass to the bot"),
                    )
    parser.add_argument("-V",
                    "--verbose",
                    action="store_true",
                    dest="verbose",
                    default=False,
                    help=_("Show internal shoebot error information in traceback"),
                    )
    parser.add_argument('script_args', nargs='?')

    # get argparse arguments and check for sanity
    args, extra = parser.parse_known_args()

    if not args.script and not args.window:
        error(_('Please specify an input script!\n (check /usr/share/shoebot/examples/ for example scripts)'))

    vars = None
    if args.vars:
        vars = json_arg(args.vars)
        # try:
        #     import json
        #     vars = json.loads(args.vars)
        # except Exception as e:
        #     error(_('Error parsing JSON, remember single quotes OUTSIDE, double QUOTES inside.'))
        #     raise e

    namespace = None
    if args.namespace:
        namespace = json_arg(args.namespace)
        # try:
        #     import json
        #     namespace = json.loads(args.namespace)
        # except Exception as e:
        #     error(_('Error parsing JSON, remember single quotes OUTSIDE, double QUOTES inside.'))
        #     raise e

    run(src = args.script,
        grammar = args.grammar,
        outputfile = args.outputfile,
        iterations = args.repeat or None,
        window = args.window or args.socketserver,
        fullscreen = args.fullscreen,
        title = args.title,
        close_window = args.close,
        server=args.socketserver,
        port=args.serverport,
        show_vars = args.window and args.disable_vars == False,
        vars = vars or None,
        namespace = namespace,
        run_shell = args.shell,
        args = shlex.split(args.script_args or ""),
        verbose = args.verbose,
        background_thread=not args.disable_background_thread,
        )
Example #11
0
def generate_data_figure1(data_path = '../Data/'):
    N, O, R, T = 15, 2, 200, 500
    mu = numpy.zeros(T)
    x = numpy.arange(1, 401)
    mu[100:] = 1. * (3. / (2. * numpy.pi * (x/400.*3.) ** 3)) ** .5 * \
               numpy.exp(-3. * ((x/400.*3.) - 1.) ** 2 / (2. * (x/400.*3.)))
    theta1 = synthesis.generate_thetas(N, O, T, mu1=-2.)
    theta2 = synthesis.generate_thetas(N, O, T, mu1=-2.)
    theta1[:, :N] += mu[:, numpy.newaxis]
    theta2[:, :N] += mu[:, numpy.newaxis]
    D = transforms.compute_D(N * 2, O)
    theta_all = numpy.empty([T, D])
    theta_all[:, :N] = theta1[:, :N]
    theta_all[:, N:2 * N] = theta2[:, :N]
    triu_idx = numpy.triu_indices(N, k=1)
    triu_idx_all = numpy.triu_indices(2 * N, k=1)
    for t in range(T):
        theta_ij = numpy.zeros([2 * N, 2 * N])
        theta_ij[triu_idx] = theta1[t, N:]
        theta_ij[triu_idx[0] + N, triu_idx[1] + N] = theta2[t, N:]
        theta_all[t, 2 * N:] = theta_ij[triu_idx_all]

    psi1 = numpy.empty([T, 3])
    psi2 = numpy.empty([T, 3])
    eta1 = numpy.empty(theta1.shape)
    eta2 = numpy.empty(theta2.shape)
    alpha = [.999,1.,1.001]
    transforms.initialise(N, O)
    for i in range(T):
        for j, a in enumerate(alpha):
            psi1[i, j] = transforms.compute_psi(a * theta1[i])
        p = transforms.compute_p(theta1[i])
        eta1[i] = transforms.compute_eta(p)
        for j, a in enumerate(alpha):
            psi2[i, j] = transforms.compute_psi(a * theta2[i])
        p = transforms.compute_p(theta2[i])
        eta2[i] = transforms.compute_eta(p)

    psi_all = psi1 + psi2
    S1 = -numpy.sum(eta1 * theta1, axis=1) + psi1[:, 1]
    S1 /= numpy.log(2)
    S2 = -numpy.sum(eta2 * theta2, axis=1) + psi2[:, 1]
    S2 /= numpy.log(2)
    S_all = S1 + S2

    C1 = (psi1[:, 0] - 2. * psi1[:, 1] + psi1[:, 2]) / .001 ** 2
    C1 /= numpy.log(2)
    C2 = (psi2[:, 0] - 2. * psi2[:, 1] + psi2[:, 2]) / .001 ** 2
    C2 /= numpy.log(2)

    C_all = C1 + C2

    spikes = synthesis.generate_spikes_gibbs_parallel(theta_all, 2 * N, O, R,
                                                      sample_steps=10,
                                                      num_proc=4)

    print 'Model and Data generated'

    emd = __init__.run(spikes, O, map_function='cg', param_est='pseudo',
                       param_est_eta='bethe_hybrid', lmbda1=100, lmbda2=200)

    f = h5py.File(data_path + 'figure1data.h5', 'w')
    g_data = f.create_group('data')
    g_data.create_dataset('theta_all', data=theta_all)
    g_data.create_dataset('psi_all', data=psi_all)
    g_data.create_dataset('S_all', data=S_all)
    g_data.create_dataset('C_all', data=C_all)
    g_data.create_dataset('spikes', data=spikes)
    g_data.create_dataset('theta1', data=theta1)
    g_data.create_dataset('theta2', data=theta2)
    g_data.create_dataset('psi1', data=psi1)
    g_data.create_dataset('S1', data=S1)
    g_data.create_dataset('C1', data=C1)
    g_data.create_dataset('psi2', data=psi2)
    g_data.create_dataset('S2', data=S2)
    g_data.create_dataset('C2', data=C2)
    g_fit = f.create_group('fit')
    g_fit.create_dataset('theta_s', data=emd.theta_s)
    g_fit.create_dataset('sigma_s', data=emd.sigma_s)
    g_fit.create_dataset('Q', data=emd.Q)
    f.close()

    print 'Fit and saved'

    f = h5py.File(data_path + 'figure1data.h5', 'r+')
    g_fit = f['fit']
    theta = g_fit['theta_s'].value
    sigma = g_fit['sigma_s'].value

    X = numpy.random.randn(theta.shape[0], theta.shape[1], 100)
    theta_sampled = \
        theta[:, :, numpy.newaxis] + X * numpy.sqrt(sigma)[:, :, numpy.newaxis]

    T = range(theta.shape[0])
    eta_sampled = numpy.empty([theta.shape[0], theta.shape[1], 100])
    psi_sampled = numpy.empty([theta.shape[0], 100, 3])

    func = partial(get_sampled_eta_psi, theta_sampled=theta_sampled, N=2*N)
    pool = multiprocessing.Pool(10)
    results = pool.map(func, T)

    for eta, psi, i in results:
        eta_sampled[i] = eta
        psi_sampled[i] = psi
    S_sampled = \
        -(numpy.sum(eta_sampled*theta_sampled, axis=1) - psi_sampled[:, :, 1])
    S_sampled /= numpy.log(2)
    C_sampled = \
        (psi_sampled[:, :, 0] - 2.*psi_sampled[:, :, 1] +
         psi_sampled[:, :, 2])/.001**2
    C_sampled /= numpy.log(2)
    g_sampled = f.create_group('sampled_results')
    g_sampled.create_dataset('theta_sampled', data=theta_sampled)
    g_sampled.create_dataset('eta_sampled', data=eta_sampled)
    g_sampled.create_dataset('psi_sampled', data=psi_sampled)
    g_sampled.create_dataset('S_sampled', data=S_sampled)
    g_sampled.create_dataset('C_sampled', data=C_sampled)
    f.close()

    print 'Done'
Example #12
0
import __init__

__init__.run()
Example #13
0
def generate_data_figure2(data_path='../Data/', max_network_size=60):
    N, O, R, T = 10, 2, 200, 500
    num_of_networks = max_network_size/N
    mu = numpy.zeros(T)
    x = numpy.arange(1, 401)
    mu[100:] = 1. * (3. / (2. * numpy.pi * (x / 400. * 3.) ** 3)) ** .5 * \
               numpy.exp(-3. * ((x / 400. * 3.) - 1.) ** 2 /
                         (2. * (x / 400. * 3.)))

    D = transforms.compute_D(N, O)
    thetas = numpy.empty([num_of_networks, T, D])
    etas = numpy.empty([num_of_networks, T, D])
    psi = numpy.empty([num_of_networks, T])
    S = numpy.empty([num_of_networks, T])
    C = numpy.empty([num_of_networks, T])
    transforms.initialise(N, O)
    for i in range(num_of_networks):
        thetas[i] = synthesis.generate_thetas(N, O, T, mu1=-2.)
        thetas[i, :, :N] += mu[:, numpy.newaxis]
        for t in range(T):
            p = transforms.compute_p(thetas[i, t])
            etas[i, t] = transforms.compute_eta(p)
            psi[i, t] = transforms.compute_psi(thetas[i, t])
            psi1 = transforms.compute_psi(.999 * thetas[i, t])
            psi2 = transforms.compute_psi(1.001 * thetas[i, t])
            C[i, t] = (psi1 - 2. * psi[i, t] + psi2) / .001 ** 2
            S[i, t] = -(numpy.sum(etas[i, t] * thetas[i, t]) - psi[i, t])
    C /= numpy.log(2)
    S /= numpy.log(2)
    f = h5py.File(data_path + 'figure2data.h5', 'w')
    g1 = f.create_group('data')
    g1.create_dataset('thetas', data=thetas)
    g1.create_dataset('etas', data=etas)
    g1.create_dataset('psi', data=psi)
    g1.create_dataset('S', data=S)
    g1.create_dataset('C', data=C)
    g2 = f.create_group('error')
    g2.create_dataset('MISE_thetas', shape=[num_of_networks])
    g2.create_dataset('MISE_population_rate', shape=[num_of_networks])
    g2.create_dataset('MISE_psi', shape=[num_of_networks])
    g2.create_dataset('MISE_S', shape=[num_of_networks])
    g2.create_dataset('MISE_C', shape=[num_of_networks])
    g2.create_dataset('population_rate', shape=[num_of_networks, T])
    g2.create_dataset('psi', shape=[num_of_networks, T])
    g2.create_dataset('S', shape=[num_of_networks, T])
    g2.create_dataset('C', shape=[num_of_networks, T])
    f.close()
    for i in range(num_of_networks):
        print 'N=%d' % ((i + 1) * N)
        D = transforms.compute_D((i + 1) * N, O)
        theta_all = numpy.empty([T, D])
        triu_idx = numpy.triu_indices(N, k=1)
        triu_idx_all = numpy.triu_indices((i + 1) * N, k=1)
        for j in range(i + 1):
            theta_all[:, N * j:(j + 1) * N] = thetas[j, :, :N]
            for t in range(T):
                theta_ij = numpy.zeros([(i + 1) * N, (i + 1) * N])
                for j in range(i + 1):
                    theta_ij[triu_idx[0] + j * N, triu_idx[1] + j * N] = \
                        thetas[j, t, N:]

            theta_all[t, (i + 1) * N:] = theta_ij[triu_idx_all]

        spikes = synthesis.generate_spikes_gibbs_parallel(theta_all
                                                          , (i + 1) * N, O, R,
                                                          sample_steps=10,
                                                          num_proc=4)
        emd = __init__.run(spikes, O, map_function='cg', param_est='pseudo',
                           param_est_eta='bethe_hybrid', lmbda1=100,
                           lmbda2=200)

        eta_est = numpy.empty(emd.theta_s.shape)
        psi_est = numpy.empty(T)
        S_est = numpy.empty(T)
        C_est = numpy.empty(T)
        for t in range(T):
            eta_est[t], psi_est[t] = bethe_approximation.compute_eta_hybrid(
                emd.theta_s[t], (i + 1) * N, return_psi=1)
            psi1 = bethe_approximation.compute_eta_hybrid(
                .999 * emd.theta_s[t], (i + 1) * N, return_psi=1)[1]
            psi2 = bethe_approximation.compute_eta_hybrid(
                1.001 * emd.theta_s[t], (i + 1) * N, return_psi=1)[1]
            S_est[t] = -(numpy.sum(eta_est[t] * emd.theta_s[t]) - psi_est[t])
            C_est[t] = (psi1 - 2. * psi_est[t] + psi2) / .001 ** 2
        S_est /= numpy.log(2)
        C_est /= numpy.log(2)
        population_rate = numpy.mean(numpy.mean(etas[:i + 1, :, :N], axis=0),
                                     axis=1)
        population_rate_est = numpy.mean(eta_est[:, :(i + 1) * N], axis=1)
        psi_true = numpy.sum(psi[:(i + 1), :], axis=0)
        S_true = numpy.sum(S[:(i + 1), :], axis=0)
        C_true = numpy.sum(C[:(i + 1), :], axis=0)

        f = h5py.File(data_path + 'figure2data.h5', 'r+')
        f['error']['MISE_thetas'][i] = numpy.mean(
            (theta_all - emd.theta_s) ** 2)
        f['error']['MISE_population_rate'][i] = numpy.mean(
            (population_rate - population_rate_est) ** 2)
        f['error']['MISE_psi'][i] = numpy.mean((psi_est - psi_true) ** 2)
        f['error']['MISE_S'][i] = numpy.mean((S_est - S_true) ** 2)
        f['error']['MISE_C'][i] = numpy.mean((C_est - C_true) ** 2)
        f['error']['population_rate'][i] = population_rate_est
        f['error']['psi'][i] = psi_est
        f['error']['S'][i] = S_est
        f['error']['C'][i] = C_est
        f.close()

    f = h5py.File(data_path + 'figure2data.h5', 'r+')
    thetas = f['data']['thetas'].value
    etas = f['data']['etas'].value
    psi = f['data']['psi'].value
    S = f['data']['S'].value
    C = f['data']['C'].value

    g2 = f.create_group('error500')
    g2.create_dataset('population_rate', shape=[num_of_networks, T])
    g2.create_dataset('psi', shape=[num_of_networks, T])
    g2.create_dataset('S', shape=[num_of_networks, T])
    g2.create_dataset('C', shape=[num_of_networks, T])
    g2.create_dataset('MISE_thetas', shape=[num_of_networks])
    g2.create_dataset('MISE_population_rate', shape=[num_of_networks])
    g2.create_dataset('MISE_psi', shape=[num_of_networks])
    g2.create_dataset('MISE_S', shape=[num_of_networks])
    g2.create_dataset('MISE_C', shape=[num_of_networks])
    f.close()

    R = 500

    for i in range(num_of_networks):
        print 'N=%d' % ((i + 1) * N)
        D = transforms.compute_D((i + 1) * N, O)
        theta_all = numpy.empty([T, D])
        triu_idx = numpy.triu_indices(N, k=1)
        triu_idx_all = numpy.triu_indices((i + 1) * N, k=1)

        for j in range(i + 1):
            theta_all[:, N * j:(j + 1) * N] = thetas[j, :, :N]

        for t in range(T):
            theta_ij = numpy.zeros([(i + 1) * N, (i + 1) * N])
            for j in range(i + 1):
                theta_ij[triu_idx[0] + j * N, triu_idx[1] + j * N] = \
                    thetas[j, t, N:]

            theta_all[t, (i + 1) * N:] = theta_ij[triu_idx_all]

        spikes = synthesis.generate_spikes_gibbs_parallel(theta_all,
                                                          (i + 1) * N, O, R,
                                                          sample_steps=10,
                                                          num_proc=4)
        emd = __init__.run(spikes, O, map_function='cg', param_est='pseudo',
                           param_est_eta='bethe_hybrid', lmbda1=100,
                           lmbda2=200)

        eta_est = numpy.empty(emd.theta_s.shape)
        psi_est = numpy.empty(T)
        S_est = numpy.empty(T)
        C_est = numpy.empty(T)

        for t in range(T):
            eta_est[t], psi_est[t] = \
                bethe_approximation.compute_eta_hybrid(emd.theta_s[t],
                                                       (i + 1) * N,
                                                       return_psi=1)
            psi1 = bethe_approximation.compute_eta_hybrid(.999 * emd.theta_s[t],
                                                          (i + 1) * N,
                                                          return_psi=1)[1]
            psi2 = bethe_approximation.compute_eta_hybrid(
                1.001 * emd.theta_s[t], (i + 1) * N, return_psi=1)[1]
            S_est[t] = -(numpy.sum(eta_est[t] * emd.theta_s[t]) - psi_est[t])
            C_est[t] = (psi1 - 2. * psi_est[t] + psi2) / .001 ** 2
        S_est /= numpy.log(2)
        C_est /= numpy.log(2)
        population_rate = numpy.mean(numpy.mean(etas[:i + 1, :, :N], axis=0),
                                     axis=1)
        population_rate_est = numpy.mean(eta_est[:, :(i + 1) * N], axis=1)
        psi_true = numpy.sum(psi[:(i + 1), :], axis=0)
        S_true = numpy.sum(S[:(i + 1), :], axis=0)
        C_true = numpy.sum(C[:(i + 1), :], axis=0)

        f = h5py.File(data_path + 'figure2data.h5', 'r+')
        f['error500']['MISE_thetas'][i] = numpy.mean(
            (theta_all - emd.theta_s) ** 2)
        f['error500']['MISE_population_rate'][i] = numpy.mean(
            (population_rate - population_rate_est) ** 2)
        f['error500']['MISE_psi'][i] = numpy.mean((psi_est - psi_true) ** 2)
        f['error500']['MISE_S'][i] = numpy.mean((S_est - S_true) ** 2)
        f['error500']['MISE_C'][i] = numpy.mean((C_est - C_true) ** 2)
        f['error500']['population_rate'][i] = population_rate_est
        f['error500']['psi'][i] = psi_est
        f['error500']['S'][i] = S_est
        f['error500']['C'][i] = C_est
        f.close()
Example #14
0
def run_em(orientation,
           O,
           monkey,
           spikes,
           spike_reverse=False,
           spike_shuffle=False,
           lmbda1=100,
           lmbda2=100,
           max_iter=100,
           param_est='exact',
           param_est_eta='exact',
           stationary='None',
           theta_o=0,
           sigma_o=0.1,
           mstep=True,
           trials='all',
           pop='',
           save=False):
    """
    Performs the EM algorithm to fit an Ising model on a given spike train
    coming from a given population of neurons from a given monkey exposed to
    gratings at a given orientation. Then, is save is True, creates a pickle
    file with a name containing the index of the monkey, the orientation of
    the stimulus and the type of spike train analyzed (i.e. reversed in time
    and/or trial shuffled or not). The file is saved in the 'Data' folder
    in the working directory. This folder is created if inexistant.

    :param int orientation:
        Orientation of the stimulus to consider (0, 30, 60, 90,..., 330)
    :param int O:
        Order of interactions to consider in the Ising model (1 or 2)
    :param int monkey:
        Index of the monkey to consider (0, 1, 2)
    :param numpy.ndarray spikes
        Spike train to which the Ising model should be fitted (T,R,N)
    :param boolean spike_reverse:
        If True, the spike train to analyze will be reversed in time
    :param boolean spike_shuffle:
        If True, the spike train to analyze will be trial-shuffled to remove
        correlations
    :param float lmbda1:
        Inverse coefficient on the identity matrix of the initial
        state-transition covariance matrix for the first order theta parameters
    :param float lmbda2:
        Inverse coefficient on the identity matrix of the initial
        state-transition covariance matrix for the second order theta parameters
    :param int max_iter:
        Maximum number of iterations for which to run the EM algorithm.
    :param str param_est:
        Parameter whether exact likelihood ('exact') or pseudo likelihood
        ('pseudo') should be used
    :param str param_est_eta:
        Eta parameters are either calculated exactly ('exact'), by mean
        field TAP approximation ('TAP'), or Bethe approximation (belief
        propagation-'bethe_BP', CCCP-'bethe_CCCP', hybrid-'bethe_hybrid')
    :param stationary:
        To fit stationary model. Set 'all' to have stationary thetas
        (Default='None')
    :param numpy.ndarray theta_o:
        Prior mean at the first time bin (one-step predictor)
    :param numpy.ndarray sigma_o:
        Prior covariance at the first time bin (one-step predictor)
    :param boolean mstep:
        The m-step of the EM algorithm is performed only if this parameter
        is true
    :param str trials
        If 'all', all trials are considered.
        If 'odd', only odd-numbered trials are considered
        If 'even', only even-numbered trials are considered
    :param str pop
        Index of the population to analyze. Give different index numbers to
        populations of the same monkey, otherwise the results of different
        populations will overwrite each other.
    :param boolean save
        If True, the emd container will be saved in a pickle file in the
        "Data" folder.

    :returns:
        EMD container. Contains the results of the EM algorithm
    """

    # Marker for the name of the created file
    b = pop
    if spike_reverse == True:
        b += '_i'
    if spike_shuffle == True:
        b += '_shuffle'
    if trials != 'all':
        b += '_' + trials
    # Run the algorithm
    emd = __init__.run(spikes, O, map_function='cg', lmbda1=lmbda1, \
                       lmbda2=lmbda2, max_iter=max_iter, param_est=param_est, \
                       param_est_eta=param_est_eta, stationary=stationary, \
                       theta_o=theta_o, sigma_o=sigma_o, mstep=mstep)
    if save == True:
        # Export emd container to a pickle file
        directory = os.getcwd()
        if not os.path.exists(directory + '/Data/'):
            os.makedirs(directory + '/Data/')
        f = open(
            directory + '/Data/m' + str(monkey + 1) + 'd' + str(orientation) +
            b, 'wb')
        pickle.dump(emd, f)
        f.close()
    print('Lambda = ' + str(1 / emd.Q[0, 0]) + ' for orientation ' +
          str(orientation))

    return emd
Example #15
0
from __init__ import run

run()
Example #16
0
def main(argv=None):
    # Step One: Parse The Command Line

    parser = optparse.OptionParser('%prog [options]')
    parser.add_option('-c',
                      '--config',
                      metavar='fn',
                      help='use this configuration file or directory')
    opts, args = parser.parse_args(argv)
    if args: print >> sys.stderr, 'Warning: ignoring spurious arguments'

    # Step Two: Check Dependencies

    check_python_version()  # require python2.4 or later
    if not opts.config:
        check_dotdir()  # require ~/.phenny, or make it and exit

    # Step Three: Load The Configurations

    config_modules = []
    for config_name in config_names(opts.config):
        name = os.path.basename(config_name).split('.')[0] + '_config'
        module = imp.load_source(name, config_name)
        module.filename = config_name

        if not hasattr(module, 'prefix'):
            module.prefix = r'\.'

        if not hasattr(module, 'name'):
            module.name = 'Phenny Palmersbot, http://inamidst.com/phenny/'

        if not hasattr(module, 'port'):
            module.port = 6667

        if not hasattr(module, 'password'):
            module.password = None

        if module.host == 'irc.example.net':
            error = ('Error: you must edit the config file first!\n' +
                     "You're currently using %s" % module.filename)
            print >> sys.stderr, error
            sys.exit(1)

        config_modules.append(module)

    # Step Four: Load Phenny

    try:
        from __init__ import run
    except ImportError:
        try:
            from phenny import run
        except ImportError:
            print >> sys.stderr, "Error: Couldn't find phenny to import"
            sys.exit(1)

    # Step Five: Initialise And Run The Phennies

    # @@ ignore SIGHUP
    for config_module in config_modules:
        run(config_module)  # @@ thread this
Example #17
0
from __init__ import run

def myview(request):
    return "<h1>Hello world!</h1>"

urls = { 
    "/hello" : {"view": myview}
}

if __name__ == "__main__":
    run(urls)
Example #18
0
# Compute P for each timestep
p = numpy.zeros((T, 2**N))
for i in xrange(T):
    p[i,:] = transforms.compute_p(theta[i,:])
# Generate spikes!
spikes = synthesis.generate_spikes(p, R, seed=1)


# ----- ALGORITHM EXECUTION -----
# Global module
import numpy
# Local module
import __init__ # From outside this folder, this would be 'import ssasc'

# Run the algorithm!
emd = __init__.run(spikes, O, lmbda=.005)


# ----- PLOTTING -----
# Global module
import pylab

# Set up an output figure
fig, ax = pylab.subplots(2, 1, sharex=1)
# Plot theta traces
ax[0].plot(emd.theta_s[:,0], c='b')
ax[0].plot(emd.theta_s[:,1], c='r')
ax[1].plot(emd.theta_s[:,2], c='g')
# Set axis limits
ax[0].set_ylim(-4.5, -1.5)
ax[1].set_ylim(1.5, 4.5)
Example #19
0
def generate_data_figure3and4(data_path = '../Data/', num_of_iterations=10):
    R, T, N, O = 200, 500, 15, 2
    f = h5py.File(data_path + 'figure1data.h5', 'r')
    theta = f['data']['theta1'].value
    f.close()

    transforms.initialise(N, O)
    psi_true = numpy.empty(T)
    for i in range(T):
        psi_true[i] = transforms.compute_psi(theta[i])
    p = numpy.zeros((T, 2 ** N))
    for i in range(T):
        p[i, :] = transforms.compute_p(theta[i, :])
    fitting_methods = ['exact', 'bethe_hybrid', 'mf']

    f = h5py.File(data_path + 'figure2and3data.h5', 'w')
    f.create_dataset('psi_true', data=psi_true)
    f.create_dataset('theta_true', data=theta)
    for fit in fitting_methods:
        g = f.create_group(fit)
        g.create_dataset('MISE_theta', shape=[num_of_iterations])
        g.create_dataset('MISE_psi', shape=[num_of_iterations])
        g.create_dataset('psi', shape=[num_of_iterations, T])
    f.close()

    for iteration in range(num_of_iterations):
        print 'Iteration %d' % iteration
        spikes = synthesis.generate_spikes(p, R, seed=None)

        for fit in fitting_methods:
            if fit == 'exact':
                emd = __init__.run(spikes, O, map_function='cg',
                                   param_est='exact', param_est_eta='exact')
            else:
                emd = __init__.run(spikes, O, map_function='cg',
                                   param_est='pseudo', param_est_eta=fit)

            psi = numpy.empty(T)

            if fit == 'exact':
                for i in range(T):
                    psi[i] = transforms.compute_psi(emd.theta_s[i])
            elif fit == 'bethe_hybrid':
                for i in range(T):
                    psi[i] = bethe_approximation.compute_eta_hybrid(
                        emd.theta_s[i], N, return_psi=1)[1]
            elif fit == 'mf':
                for i in range(T):
                    eta_mf = mean_field.forward_problem(emd.theta_s[i], N,
                                                        'TAP')
                    psi[i] = mean_field.compute_psi(emd.theta_s[i], eta_mf, N)

            mise_theta = numpy.mean((theta - emd.theta_s) ** 2)
            mise_psi = numpy.mean((psi_true - psi) ** 2)
            f = h5py.File(data_path + 'figure2and3data.h5', 'r+')
            g = f[fit]
            g['MISE_theta'][iteration] = mise_theta
            g['MISE_psi'][iteration] = mise_psi
            if iteration == 0:
                g.create_dataset('theta', data=emd.theta_s)
                g.create_dataset('sigma', data=emd.sigma_s)
            g['psi'][iteration] = psi
            f.close()
            print 'Fitted with %s' % fit
Example #20
0
def main():
    global parser

    # use ArgumentParser to interpret commandline options
    parser = argparse.ArgumentParser(_("usage: sbot [options] inputfile.bot [args]"))
    parser.add_argument("script", help="Shoebot / Nodebox script to run (filename or code)", nargs='?')

    group = parser.add_argument_group('Input / Output')
    # IO - Output to file
    group.add_argument("-o",
                       "--outputfile",
                       dest="outputfile",
                       help=_("run script and output to image file (accepts .png .svg .pdf and .ps extensions)"),
                       metavar="FILE")

    # Shoebot IO - Sockets
    group.add_argument("-s",
                       "--socketserver",
                       action="store_true",
                       dest="socketserver",
                       default=False,
                       help=_("run a socket server for external control (will run the script in windowed mode)"))
    group.add_argument("-p",
                       "--serverport",
                       type=int,
                       dest="serverport",
                       default=DEFAULT_SERVERPORT,
                       help=_("set socketserver port to listen for connections (default is 7777)"))

    # IO - Variables
    group.add_argument("-v",
                       "--vars",
                       dest="vars",
                       default=False,
                       help=_("Initial variables, in JSON (Note: Single quotes OUTSIDE, double INSIDE) --vars='{\"variable1\": 1}'"),
                       )
    # IO - Namespace
    group.add_argument("-ns",
                    "--namespace",
                    dest="namespace",
                    default=None,
                    help=_("Initial namespace, in JSON (Note: Single quotes OUTSIDE, double INSIDE) --namespace='{\"variable1\": 1}'"),
                    )
    # IO - IDE integration Shell
    group.add_argument("-l",
                    "--l",
                    dest="shell",
                    action="store_true",
                    default=False,
                    help=_("Simple shell - for IDE interaction"),
                    )

    # IO - Passing args to the bot
    group.add_argument("-a",
                    "--args",
                    dest="script_args",
                    help=_("Pass to the bot"),
                    )
    group.add_argument('script_args', nargs='?')

    group = parser.add_argument_group('Bot Lifecycle')
    # Bot Lifecycle
    group.add_argument("-r",
                       "--repeat",
                       type=int,
                       dest="repeat",
                       default=False,
                       help=_("set number of iteration, multiple images will be produced"))
    group.add_argument("-g",
                    "--grammar",
                    dest="grammar",
                    default=NODEBOX,
                    help=_("Select the bot grammar 'nodebox' (default) or 'drawbot' languages"),
                    )

    group = parser.add_argument_group('Window Management')
    group.add_argument("-w",
                       "--window",
                       action="store_true",
                       dest="window",
                       default=True,
                       help=_("run script in a GTK window")
                       )
    group.add_argument("-f",
                       "--fullscreen",
                       action="store_true",
                       dest="fullscreen",
                       default=False,
                       help=_("run in fullscreen mode")
                       )
    group.add_argument("-t",
                       "--title",
                       action="store",
                       dest="title",
                       default=None,
                       help=_("Set window title")
                       )
    group.add_argument("-c",
                       "--close",
                       action="store_true",
                       dest="close",
                       default=False,
                       help=_("Close window after running bot (use with -r for benchmarking)"),
                       )
    group.add_argument("-dv",
                       "--disable-vars",
                       action="store_true",
                       dest="disable_vars",
                       default=False,
                       help=_("disable the variables pane when in windowed mode."))

    group = parser.add_argument_group('Debugging / Dev flags')
    group.add_argument("-dn",
                       "--diagnose",
                       action="store_true",
                       default=False,
                       help=_("Output information for debugging installation / graphics issues."))
    group.add_argument("-dt",
                       "--disable-background-thread",
                       action="store_true",
                       dest="disable_background_thread",
                       default=sys.platform=='darwin',
                       help=_("disable running bot code in background thread (default on OSX)."))
    group.add_argument("-V",
                       "--verbose",
                       action="store_true",
                       dest="verbose",
                       default=False,
                       help=_("Show internal shoebot error information in traceback"),
                       )

    # get argparse arguments and check for sanity
    args, extra = parser.parse_known_args()

    if args.diagnose:
        from diagnose import diagnose
        diagnose()
        sys.exit()

    if not args.script:
        error(_('Please specify an input script!\n (check /usr/share/shoebot/examples/ for example scripts)'))

    if args.vars:
        vars = json_arg(args.vars)
    else:
        vars = None

    if args.namespace:
        namespace = json_arg(args.namespace)
    else:
        namespace = None

    from __init__ import run  # https://github.com/shoebot/shoebot/issues/206
    run(src=args.script,
        grammar=args.grammar,
        outputfile=args.outputfile,
        iterations=args.repeat or None,
        window=args.window or args.socketserver,
        fullscreen=args.fullscreen,
        title=args.title,
        close_window=args.close,
        server=args.socketserver,
        port=args.serverport,
        show_vars=args.window and args.disable_vars is False,
        vars=vars or None,
        namespace=namespace,
        run_shell=args.shell,
        args=shlex.split(args.script_args or ""),
        verbose=args.verbose,
        background_thread=not args.disable_background_thread,
        )
Example #21
0
# Initial one-step predictor (hyperparameters mu and sigma)
theta_o = numpy.zeros((1, D_fit))
sigma_o = 0.1 * numpy.eye(D_fit)

# Initialise theta, sigma and marginal log likelihood
theta_fit = numpy.zeros((T, D_fit))
lm = numpy.zeros(T)

# Initialise a list of J estimates
J_list = list()
J_list.append(numpy.copy(J))

print('D_fit = ' + str(D_fit))
for t in range(T):
    # Fit to update J
    emd = __init__.run(spikes[t,numpy.newaxis], D_fit, lmbda=lmbda, J=J, \
                       theta_o=theta_o, sigma_o=sigma_o, exact=exact)
    # Retrieve and normalize J (unit variance)
    J = emd.J / (numpy.var(emd.J, axis=0)**0.5)
    # Save J
    J_list.append(numpy.copy(J))
    # Update one-step predictors
    theta_o = numpy.dot(emd.F, emd.theta_f[0, :])
    tmp = numpy.dot(emd.F, emd.sigma_f[0, :, :])
    sigma_o = numpy.dot(tmp, emd.F.T) + emd.Q
    # Retrieve fitted weights and their variance
    theta_fit[t, :] = emd.theta_s[0, :]
    # Compute the current marginal log-likelihood
    lm[t] = probability.log_marginal(emd)
    print('step ' + str(t) + '/ ' + str(T))

# Save results
Example #22
0
# Spikes list
spikes_list = list()

for r in range(rep):
    print(str(r + 1) + '/' + str(rep) + ' epochs')
    for t in range(T1):
        # Get the spike array
        if exact:
            spikes = synthesis.generate_spikes(p[t, numpy.newaxis, :],
                                               1,
                                               seed=seed)
        else:
            spikes = synthesis.generate_spikes_gibbs(mixed_theta[t,:].reshape(1,dim), N, \
                                                     2, 1)
        # Fit to update J
        emd = __init__.run(spikes, D_fit, lmbda=lmbda, J=J, \
                       theta_o=theta_o, sigma_o=sigma_o, exact=exact)
        # Retrieve updated J
        J = emd.J
        # Scale columns of J
        J = J / (numpy.var(J, axis=0)**0.5)
        # Save J
        J_list.append(J)
        # Update one-step predictors
        theta_o = numpy.dot(emd.F, emd.theta_f[0, :])
        tmp = numpy.dot(emd.F, emd.sigma_f[0, :, :])
        sigma_o = numpy.dot(tmp, emd.F.T) + emd.Q
        # Retrieve fitted weights and their variance
        theta_fit[t, :] = emd.theta_s[0, :]
        sigma[t, :] = numpy.diag(emd.sigma_s[0])
        # Compute the current marginal log-likelihood
        lm[t] = probability.log_marginal(emd)
Example #23
0
# Compute P for each time step
p = numpy.zeros((T, 2**N))
for i in range(T):
    p[i,:] = transforms.compute_p(theta[i,:])
# Generate spikes!
spikes = synthesis.generate_spikes(p, R, seed=1)


# ----- ALGORITHM EXECUTION -----
# Global module
import numpy
# Local module
import __init__ # From outside this folder, this would be 'import ssll'

# Run the algorithm!
emd = __init__.run(spikes, O, map_function='nr', lmbda1=200, lmbda2=200,)


# ----- PLOTTING -----
# Global module
import pylab

# Set up an output figure
fig, ax = pylab.subplots(2, 1, sharex=True)
# Plot underlying theta traces
ax[0].plot(theta[:,0], c='b', linestyle='--')
ax[0].plot(theta[:,1], c='r', linestyle='--')
ax[1].plot(theta[:,2], c='g', linestyle='--')

# Plot estimated theta traces
ax[0].plot(emd.theta_s[:,0], c='b')
Example #24
0
def generate_data_ctime(data_path='../Data/', max_network_size=60,
                              num_procs=4):
    N, O, R, T = 10, 2, 200, 500
    num_of_networks = max_network_size/N
    mu = numpy.zeros(T)
    x = numpy.arange(1, 401)
    mu[100:] = 1. * (3. / (2. * numpy.pi * (x / 400. * 3.) ** 3)) ** .5 * \
               numpy.exp(-3. * ((x / 400. * 3.) - 1.) ** 2 /
                         (2. * (x / 400. * 3.)))

    D = transforms.compute_D(N, O)
    thetas = numpy.empty([num_of_networks, T, D])
    transforms.initialise(N, O)
    for i in range(num_of_networks):
        thetas[i] = synthesis.generate_thetas(N, O, T, mu1=-2.)
        thetas[i, :, :N] += mu[:, numpy.newaxis]

    R = 500
    f = h5py.File(data_path + 'comp_time_data.h5', 'w')
    f.create_dataset('N', data=numpy.arange(N, max_network_size+N, N))
    f.create_dataset('ctime', shape=[2,num_of_networks])
    f.close()
    for i in range(num_of_networks):
        print 'N=%d' % ((i + 1) * N)
        D = transforms.compute_D((i + 1) * N, O)
        theta_all = numpy.empty([T, D])
        triu_idx = numpy.triu_indices(N, k=1)
        triu_idx_all = numpy.triu_indices((i + 1) * N, k=1)

        for j in range(i + 1):
            theta_all[:, N * j:(j + 1) * N] = thetas[j, :, :N]

        for t in range(T):
            theta_ij = numpy.zeros([(i + 1) * N, (i + 1) * N])
            for j in range(i + 1):
                theta_ij[triu_idx[0] + j * N, triu_idx[1] + j * N] = \
                    thetas[j, t, N:]

            theta_all[t, (i + 1) * N:] = theta_ij[triu_idx_all]

        spikes = synthesis.generate_spikes_gibbs_parallel(theta_all,
                                                          (i + 1) * N, O, R,
                                                          sample_steps=10,
                                                          num_proc=num_procs)
        t1 = time.time()
        result = __init__.run(spikes, O, map_function='cg',
                                    param_est='pseudo',
                           param_est_eta='bethe_hybrid',
                            lmbda1=100,
                           lmbda2=200)
        t2 = time.time()
        ctime_bethe = t2 - t1

        f = h5py.File(data_path + 'comp_time_data.h5', 'r+')
        f['ctime'][0, i] = ctime_bethe
        f.close()

        try:
            t1 = time.time()
            result = __init__.run(spikes, O, map_function='cg',
                                                   param_est='pseudo',
                                                   param_est_eta='mf',
                                                   lmbda1=100,
                                                   lmbda2=200)
            t2 = time.time()
            ctime_TAP = t2 - t1
        except Exception:
            ctime_TAP = numpy.nan

        f = h5py.File(data_path + 'comp_time_data.h5', 'r+')
        f['ctime'][1, i] = ctime_TAP
        f.close()
Example #25
0
import __init__

if __name__ == "__main__":
    __init__.welcome()
    __init__.init()
    __init__.run()