Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def initialize_test_harness():
""" Initialize a model with N neurons. Use the data if specified on the
command line, otherwise sample new data from the model.
Return a population object, the data, and a set of true parameters
which is expected for synthetic tests
"""
# Parse command line args
(options, args) = parse_cmd_line_args()
# Load data from file or create synthetic test dataset
data = load_data(options)
print "Creating master population object"
model = make_model(options.model, N=data['N'], dt=0.001)
stabilize_sparsity(model)
popn = KayakPopulation(model)
popn.add_data(data)
# Initialize the GLM with the data
popn_true = None
x_true = None
if 'vars' in data:
x_true = data['vars']
# Load the true model
data_dir = os.path.dirname(options.dataFile)
model_file = os.path.join(data_dir, 'model.pkl')
print "Loading true model from %s" % model_file
with open(model_file) as f:
popn_true = cPickle.load(f)
popn_true.set_parameters(x_true)
with open(results_file) as f:
x_smpls = cPickle.load(f)
N_samples = len(x_smpls)
# TODO: Check that the results are from the same model?
else:
print "Results not found. Running MCMC inference."
# If x0 specified, load x0 from file
x0 = None
if options.x0_file is not None:
with open(options.x0_file, 'r') as f:
print "Initializing with state from: %s" % options.x0_file
mle_x0 = cPickle.load(f)
# HACK: We're assuming x0 came from a standard GLM
mle_model = make_model('standard_glm', N=data['N'])
mle_popn = KayakPopulation(mle_model)
mle_popn.set_data(data)
x0 = popn.sample()
x0 = convert_model(mle_popn, mle_model, mle_x0, popn, popn.model, x0)
# DEBUG: Initialize with true params
# x0 = x_true
import pdb; pdb.set_trace()
# Perform inference
# raw_input('Press any key to begin inference...\n')
x_smpls = gibbs_sample(popn, x0=x0, N_samples=N_samples,
init_from_mle=False,
callback=None)
def run_gen_synth_data():
""" Run a test with synthetic data and MCMC inference
"""
options, args = parse_cmd_line_args()
# Create the model
dt = 0.001
model = make_model(options.model, N=options.N, dt=dt)
# Set the sparsity level to minimize the risk of unstable networks
stabilize_sparsity(model)
print "Creating master population object"
popn = KayakPopulation(model)
# Sample random parameters from the model
x_true = popn.sample()
# Check stability of matrix
assert check_stability(model, x_true, options.N), "ERROR: Sampled network is unstable!"
# Save the model so it can be loaded alongside the data
fname_model = os.path.join(options.resultsDir, 'model.pkl')
print "Saving population to %s" % fname_model
with open(fname_model,'w') as f:
cPickle.dump(popn, f, protocol=-1)
print "Generating synthetic data with %d neurons and %.2f seconds." % \
(options.N, options.T_stop)