Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_iter(self):
parameter_list = [["gbar_Na", 120, cp.Uniform(110, 130)],
["gbar_K", 36, cp.Normal(36, 1)],
["gbar_L", 0.3, cp.Chi(1, 1, 0.3)]]
parameters = Parameters(parameter_list)
result = [parameter for parameter in parameters]
self.assertEqual(len(result), 3)
self.assertIsInstance(result[0], Parameter)
self.assertIsInstance(result[1], Parameter)
self.assertIsInstance(result[2], Parameter)
def test_setitem(self):
parameter = Parameter("gbar_Na", 120, cp.Uniform(110, 130))
parameters = Parameters()
parameters["gbar_Na"] = parameter
self.assertTrue("gbar_Na" in parameters.parameters)
self.assertIsInstance(parameters["gbar_Na"], Parameter)
# Set up samplers
sweep1 = {
"angle": [0.1, 0.2, 0.3],
"height": [2.0, 10.0],
"velocity": [10.0, 10.1, 10.2]
}
sampler1 = uq.sampling.BasicSweep(sweep=sweep1)
sweep2 = {
"air_resistance": [0.2, 0.3, 0.4]
}
sampler2 = uq.sampling.BasicSweep(sweep=sweep2)
vary = {
"gravity": cp.Uniform(9.8, 1.0),
"mass": cp.Uniform(2.0, 10.0),
}
sampler3 = uq.sampling.RandomSampler(vary=vary, max_num=5)
# Make a multisampler
multisampler = uq.sampling.MultiSampler(sampler1, sampler2, sampler3)
# Set the campaign to use this sampler
my_campaign.set_sampler(multisampler)
# Test reloading
my_campaign.save_state(tmpdir + "test_multisampler.json")
reloaded_campaign = uq.Campaign(state_file=tmpdir + "test_multisampler.json", work_dir=tmpdir)
# Draw all samples
my_campaign.draw_samples()
"type": "float",
"min": 0.0,
"max": 1000.0,
"default": 10.0}}
encoder = uq.encoders.GenericEncoder(
template_fname='tests/cannonsim/test_input/cannonsim.template',
delimiter='#',
target_filename='in.cannon')
decoder = uq.decoders.SimpleCSV(
target_filename='output.csv', output_columns=[
'Dist', 'lastvx', 'lastvy'], header=0)
collater = uq.collate.AggregateSamples(average=False)
vary = {
"gravity": cp.Uniform(9.8, 1.0),
"mass": cp.Uniform(2.0, 10.0),
}
cannon_sampler = uq.sampling.RandomSampler(vary=vary, max_num=5)
cannon_action = uq.actions.ExecuteLocal("tests/cannonsim/bin/cannonsim in.cannon output.csv")
cannon_stats = uq.analysis.BasicStats(qoi_cols=['Dist', 'lastvx', 'lastvy'])
return params, encoder, decoder, collater, cannon_sampler, cannon_action, cannon_stats
def create_PCE_custom(self, uncertain_parameters=None, custom_argument=None):
uncertain_parameters = self.convert_uncertain_parameters(uncertain_parameters)
data = Data()
q0, q1 = cp.variable(2)
parameter_space = [cp.Uniform(), cp.Uniform()]
distribution = cp.J(*parameter_space)
data.uncertain_parameters = ["a", "b"]
data.test_value = custom_argument
data.add_features(["TestingModel1d", "feature0d", "feature1d", "feature2d"])
U_hat = {}
U_hat["TestingModel1d"] = cp.Poly([q0, q1*q0, q1])
U_hat["feature0d"] = cp.Poly([q0, q1*q0, q1])
U_hat["feature1d"] = cp.Poly([q0, q1*q0, q1])
U_hat["feature2d"] = cp.Poly([q0, q1*q0, q1])
return U_hat, distribution, data
"out_file": {
"type": "string",
"default": "output.csv"}}
output_filename = params["out_file"]["default"]
output_columns = ["u"]
encoder = uq.encoders.GenericEncoder(
template_fname=f'tests/sc/sc.template',
delimiter='$',
target_filename='sc_in.json')
decoder = uq.decoders.SimpleCSV(target_filename=output_filename,
output_columns=output_columns,
header=0)
collater = uq.collate.AggregateSamples(average=False)
vary = {
"Pe": cp.Uniform(100.0, 200.0),
"f": cp.Normal(1.0, 0.1)
}
sampler = uq.sampling.SCSampler(vary=vary, polynomial_order=1)
actions = uq.actions.ExecuteLocal(f"tests/sc/sc_model.py sc_in.json")
stats = uq.analysis.SCAnalysis(sampler=sampler, qoi_cols=output_columns)
campaign(tmpdir, 'sc', 'sc', params, encoder, decoder, sampler,
collater, actions, stats, vary, 0, 1)
decoder=decoder)
# Set the active app to be cannonsim (this is redundant when only one app
# has been added)
my_campaign.set_app("cannonsim")
# Create a collation element for this campaign
collater = uq.collate.AggregateSamples(average=False)
my_campaign.set_collater(collater)
print("Serialized collation:", collater.serialize())
# Make a random sampler
vary = {
"angle": cp.Uniform(0.0, 1.0),
"height": cp.Uniform(2.0, 10.0),
"velocity": cp.Normal(10.0, 1.0),
"mass": cp.Uniform(5.0, 1.0)
}
sampler1 = uq.sampling.RandomSampler(vary=vary)
print("Serialized sampler:", sampler1.serialize())
# Set the campaign to use this sampler
my_campaign.set_sampler(sampler1)
# Draw 5 samples
my_campaign.draw_samples(num_samples=5)
# Print the list of runs now in the campaign db
print("List of runs added:")
pprint(my_campaign.list_runs())
print("---")
def test_get_error(self):
parameter_list = [["gbar_Na", 120, cp.Uniform(110, 130)],
["gbar_K", 36, cp.Normal(36, 1)],
["gbar_L", 0.3, cp.Chi(1, 1, 0.3)]]
self.parameters = Parameters(parameter_list)
with self.assertRaises(AttributeError):
self.parameters.get("not_a_parameter")
target_filename='output.csv', output_columns=[
'Dist', 'lastvx', 'lastvy'], header=0)
# Create a collation element for this campaign
collater = uq.collate.AggregateSamples(average=False)
actions = uq.actions.ExecuteLocal("/home/hpc/pn69ju/di73kuj2/cannonsim/bin/cannonsim in.cannon output.csv")
campaign.add_app(name='cannonsim',
params=params,
encoder=encoder,
decoder=decoder,
collater=collater)
stats = uq.analysis.BasicStats(qoi_cols=['Dist', 'lastvx', 'lastvy'])
# Make a random sampler
vary = {
"angle": cp.Uniform(0.0, 1.0),
"height": cp.Uniform(2.0, 10.0),
"velocity": cp.Normal(10.0, 1.0),
"mass": cp.Uniform(5.0, 1.0)
}
sampler = uq.sampling.RandomSampler(vary=vary)
campaign.set_sampler(sampler)
campaign.draw_samples(num_samples=56, replicas=1)
cluster = SLURMCluster(job_extra=['--cluster=mpp2'], queue='mpp2_batch', cores=56, processes=56, memory='64 GB')
print(cluster.job_script())
cluster.scale(4)
client = Client(cluster)
campaign.populate_runs_dir()
campaign.apply_for_each_run_dir(actions, client)
campaign.collate()
campaign.apply_analysis(stats)
print(campaign.get_last_analysis())
def test_iter(self):
parameter_list = [["gbar_Na", 120, cp.Uniform(110, 130)],
["gbar_K", 36, cp.Normal(36, 1)],
["gbar_L", 0.3, cp.Chi(1, 1, 0.3)]]
parameters = Parameters(parameter_list)
result = [parameter for parameter in parameters]
self.assertEqual(len(result), 3)
self.assertIsInstance(result[0], Parameter)
self.assertIsInstance(result[1], Parameter)
self.assertIsInstance(result[2], Parameter)