Skip to content
Snippets Groups Projects
Commit 8ffc8a5e authored by Maximilian Kurthen's avatar Maximilian Kurthen
Browse files

fixed bug in benchmark script

parent a2ef4923
No related branches found
No related tags found
No related merge requests found
...@@ -16,11 +16,8 @@ NAME = args.name ...@@ -16,11 +16,8 @@ NAME = args.name
FIRST_ID = args.first_id FIRST_ID = args.first_id
LAST_ID = args.last_id LAST_ID = args.last_id
MODEL = args.model
N_BINS = args.nbins N_BINS = args.nbins
NOISE_VAR = args.noise_var NOISE_VAR = args.noise_var
ITERATION_LIMIT = args.iteration_limit
TOL_REL_GRADNORM = args.tol_rel_gradnorm
BENCHMARK = args.benchmark BENCHMARK = args.benchmark
VERBOSITY = args.verbosity VERBOSITY = args.verbosity
...@@ -35,15 +32,12 @@ if CONFIG is not None: ...@@ -35,15 +32,12 @@ if CONFIG is not None:
with open('./model_configurations.txt') as f: with open('./model_configurations.txt') as f:
configs = eval(f.read()) configs = eval(f.read())
parameters = configs[CONFIG] parameters = configs[CONFIG]
MODEL = parameters.get('model', MODEL)
N_BINS = parameters.get('nbins', N_BINS) N_BINS = parameters.get('nbins', N_BINS)
NOISE_VAR = parameters.get('noise_var', NOISE_VAR) NOISE_VAR = parameters.get('noise_var', NOISE_VAR)
POWER_SPECTRUM_BETA_STR = parameters.get( POWER_SPECTRUM_BETA_STR = parameters.get(
'power_spectrum_beta', POWER_SPECTRUM_BETA_STR) 'power_spectrum_beta', POWER_SPECTRUM_BETA_STR)
POWER_SPECTRUM_F_STR = parameters.get( POWER_SPECTRUM_F_STR = parameters.get(
'power_spectrum_f', POWER_SPECTRUM_F_STR) 'power_spectrum_f', POWER_SPECTRUM_F_STR)
ITERATION_LIMIT = parameters.get('iteration_limit', ITERATION_LIMIT)
TOL_REL_GRADNORM = parameters.get('tol_rel_gradnorm', TOL_REL_GRADNORM)
if LAST_ID is None: if LAST_ID is None:
LAST_ID = get_benchmark_default_length(BENCHMARK) LAST_ID = get_benchmark_default_length(BENCHMARK)
...@@ -55,14 +49,17 @@ print( ...@@ -55,14 +49,17 @@ print(
'power spectrum beta: {}\n' 'power spectrum beta: {}\n'
'power spectrum f: {}\n' 'power spectrum f: {}\n'
'rho: {}\n' 'rho: {}\n'
'scale_max: {}\n'
'storing results with suffix {}'.format( 'storing results with suffix {}'.format(
BENCHMARK, FIRST_ID, LAST_ID, N_BINS, BENCHMARK, FIRST_ID, LAST_ID, N_BINS,
NOISE_VAR, NOISE_VAR,
POWER_SPECTRUM_BETA_STR, POWER_SPECTRUM_BETA_STR,
POWER_SPECTRUM_F_STR, POWER_SPECTRUM_F_STR,
RHO, RHO,
SCALE_MAX,
NAME)) NAME))
np.random.seed(1)
POWER_SPECTRUM_BETA = lambda q: eval(POWER_SPECTRUM_BETA_STR) POWER_SPECTRUM_BETA = lambda q: eval(POWER_SPECTRUM_BETA_STR)
POWER_SPECTRUM_F = lambda q: eval(POWER_SPECTRUM_F_STR) POWER_SPECTRUM_F = lambda q: eval(POWER_SPECTRUM_F_STR)
scale = (0, SCALE_MAX) scale = (0, SCALE_MAX)
...@@ -75,7 +72,8 @@ if os.path.isfile(prediction_file): ...@@ -75,7 +72,8 @@ if os.path.isfile(prediction_file):
prediction_file = './benchmark_predictions/{}_{}_{}.txt'.format( prediction_file = './benchmark_predictions/{}_{}_{}.txt'.format(
BENCHMARK, NAME, c) BENCHMARK, NAME, c)
accuracy = 0 np.random.seed(1)
sum_of_weights = 0 sum_of_weights = 0
weighted_correct = 0 weighted_correct = 0
...@@ -88,13 +86,6 @@ for i in range(FIRST_ID-1, LAST_ID): ...@@ -88,13 +86,6 @@ for i in range(FIRST_ID-1, LAST_ID):
scaler = MinMaxScaler(scale) scaler = MinMaxScaler(scale)
x, y = scaler.fit_transform(np.array((x, y)).T).T x, y = scaler.fit_transform(np.array((x, y)).T).T
minimizer = nifty5.RelaxedNewton(controller=nifty5.GradientNormController(
tol_rel_gradnorm=TOL_REL_GRADNORM,
iteration_limit=ITERATION_LIMIT,
convergence_level=5,
))
if MODEL == 1:
bcm = bayesian_causal_model.cause_model_shallow.CausalModelShallow( bcm = bayesian_causal_model.cause_model_shallow.CausalModelShallow(
N_bins=N_BINS, N_bins=N_BINS,
noise_var=NOISE_VAR, noise_var=NOISE_VAR,
...@@ -102,21 +93,12 @@ for i in range(FIRST_ID-1, LAST_ID): ...@@ -102,21 +93,12 @@ for i in range(FIRST_ID-1, LAST_ID):
power_spectrum_beta=POWER_SPECTRUM_BETA, power_spectrum_beta=POWER_SPECTRUM_BETA,
power_spectrum_f=POWER_SPECTRUM_F, power_spectrum_f=POWER_SPECTRUM_F,
) )
elif MODEL == 2:
bcm = bayesian_causal_model_nifty.cause_model_shallow.CausalModelShallow(
N_bins=N_BINS,
noise_var=NOISE_VAR,
rho=RHO,
power_spectrum_beta=POWER_SPECTRUM_BETA,
power_spectrum_f=POWER_SPECTRUM_F,
minimizer=minimizer,
)
bcm.set_data(x, y) bcm.set_data(x, y)
H1 = bcm.get_evidence(direction=1, verbosity=1) H1 = bcm.get_evidence(direction=1, verbosity=VERBOSITY - 1)
H2 = bcm.get_evidence(direction=-1, verbosity=1) H2 = bcm.get_evidence(direction=-1, verbosity=VERBOSITY - 1)
predicted_direction = 1 if int(H1 < H2) else 0 predicted_direction = 1 if int(H1 < H2) else -1
if predicted_direction == true_direction: if predicted_direction == true_direction:
fore = colorama.Fore.GREEN fore = colorama.Fore.GREEN
...@@ -126,6 +108,7 @@ for i in range(FIRST_ID-1, LAST_ID): ...@@ -126,6 +108,7 @@ for i in range(FIRST_ID-1, LAST_ID):
sum_of_weights += weight sum_of_weights += weight
accuracy = weighted_correct / sum_of_weights accuracy = weighted_correct / sum_of_weights
if VERBOSITY > 0:
print( print(
'dataset {}, {} true direction: {}, predicted direction {}\n' 'dataset {}, {} true direction: {}, predicted direction {}\n'
'H1: {:.2e},\n H2: {:.2e},\n{}' 'H1: {:.2e},\n H2: {:.2e},\n{}'
...@@ -147,7 +130,6 @@ print('accuracy: {:.2f}'.format(accuracy)) ...@@ -147,7 +130,6 @@ print('accuracy: {:.2f}'.format(accuracy))
benchmark_information = { benchmark_information = {
'benchmark': BENCHMARK, 'benchmark': BENCHMARK,
'model': MODEL,
'n_bins': N_BINS, 'n_bins': N_BINS,
'noise_var': NOISE_VAR, 'noise_var': NOISE_VAR,
'rho': RHO, 'rho': RHO,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment