Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
savefile = MCMC_test.npz''')
p = tmp_path / 'quadratic.py'
p.write_text(u'''
def quad(p, x):
y = p[0] + p[1]*x + p[2]*x**2.0
return y''')
# Create synthetic dataset:
x = np.linspace(0, 10, 1000) # Independent model variable
p0 = [3, -2.4, 0.5] # True-underlying model parameters
y = quad(p0, x) # Noiseless model
uncert = np.sqrt(np.abs(y)) # Data points uncertainty
error = np.random.normal(0, uncert) # Noise for the data
data = y + error # Noisy data set
# Store data set and other inputs:
mc3.utils.savebin([data, uncert], 'data.npz')
mc3.utils.savebin([x], 'indp.npz')
subprocess.call('mc3 -c MCMC.cfg'.split())
assert "MCMC_test.npz" in os.listdir(".")
assert "MCMC_test_trace.png" in os.listdir(".")
assert "MCMC_test_pairwise.png" in os.listdir(".")
assert "MCMC_test_posterior.png" in os.listdir(".")
assert "MCMC_test_model.png" in os.listdir(".")
savefile = MCMC_test.npz''')
p = tmp_path / 'quadratic.py'
p.write_text(u'''
def quad(p, x):
y = p[0] + p[1]*x + p[2]*x**2.0
return y''')
# Create synthetic dataset:
x = np.linspace(0, 10, 1000) # Independent model variable
p0 = [3, -2.4, 0.5] # True-underlying model parameters
y = quad(p0, x) # Noiseless model
uncert = np.sqrt(np.abs(y)) # Data points uncertainty
error = np.random.normal(0, uncert) # Noise for the data
data = y + error # Noisy data set
# Store data set and other inputs:
mc3.utils.savebin([data, uncert], 'data.npz')
mc3.utils.savebin([x], 'indp.npz')
subprocess.call('mc3 -c MCMC.cfg'.split())
assert "MCMC_test.npz" in os.listdir(".")
assert "MCMC_test_trace.png" in os.listdir(".")
assert "MCMC_test_pairwise.png" in os.listdir(".")
assert "MCMC_test_posterior.png" in os.listdir(".")
assert "MCMC_test_model.png" in os.listdir(".")
def test_load_savebin_array(tmp_path):
binfile = str(tmp_path / 'saved_bin.npz')
data = np.arange(4)
indata = [data]
mu.savebin(indata, binfile)
outdata = mu.loadbin(binfile)
assert type(outdata[0]) == np.ndarray
np.testing.assert_equal(outdata[0], data)
def test_loadsavebin_all(tmp_path):
# This could be replaced with pickle files
binfile = str(tmp_path / "saved_bin.npz")
indata = [np.arange(4), "one", np.ones((2,2)), True, [42], (42, 42)]
mu.savebin(indata, binfile)
outdata = mu.loadbin(binfile)
# Check types:
assert type(outdata[0]) == np.ndarray
assert type(outdata[1]) == str
assert type(outdata[2]) == np.ndarray
assert type(outdata[3]) == bool
assert type(outdata[4]) == list
assert type(outdata[5]) == tuple
# Check values:
np.testing.assert_equal(outdata[0], np.arange(4))
assert outdata[1] == 'one'
np.testing.assert_equal(outdata[2], np.ones((2,2)))
assert outdata[3] == True
assert outdata[4] == [42]
assert outdata[5] == (42,42)
def test_load_savebin(tmp_path, data):
binfile = str(tmp_path / 'saved_bin.npz')
dtype = type(data)
indata = [data]
mu.savebin(indata, binfile)
outdata = mu.loadbin(binfile)
assert type(outdata[0]) == dtype
np.testing.assert_equal(outdata[0], data)