def test_logger_initialisation_deletion(): basepath = '.' subfolder = 'tmplog' log = exp.Logger(basepath, subfolder) # Check if intialisation works properly assert log.basepath == '.' assert log.path == './tmplog' assert log.procedure_calls == 0 assert log.create_samples_header is True # Check that folder exists assert os.path.exists(basepath + os.sep + subfolder) is True # Check that handles for 3 files are opened assert os.path.exists(basepath + os.sep + subfolder + os.sep + 'samples.csv') is True assert os.path.exists(basepath + os.sep + subfolder + os.sep + 'functioncalls.csv') is True assert os.path.exists(basepath + os.sep + subfolder + os.sep + 'procedurecalls.csv') is True # Check that duplicate names are handled properly log2 = exp.Logger(basepath, subfolder) assert log.path != log2.path # Remove created folders shutil.rmtree(log.path) shutil.rmtree(log2.path) # Close handles log.handle_samples.close() del (log.handle_samples) del (log)
def test_logger_functioncalls(): basepath = '.' subfolder = 'tmplog' log = exp.Logger(basepath, subfolder) # Fake function calls log.procedure_calls = 10 function = func.GaussianShells() function.counter = [[10, 3, 1], [9, 2, 0]] log.log_function_calls(function) # Read function call log call_log = pd.read_csv(log.path + os.sep + 'functioncalls.csv') reference = np.array([[10, 10, 3, 1], [10, 9, 2, 0]]) assert np.array_equal(call_log.values[:, :-1], reference[:, :-1]) assert np.array_equal(call_log['asked_for_derivative'] * 1, reference[:, -1]) shutil.rmtree(log.path)
def test_logger_procedurecalls(): basepath = '.' subfolder = 'tmplog' log = exp.Logger(basepath, subfolder) # Fake procedure calls log.procedure_calls = 10 # Log procedure calls log.log_procedure_calls(5, 9, 3) log.log_procedure_calls(1, 2, 6) # Read procedure call log call_log = np.genfromtxt(log.path + os.sep + 'procedurecalls.csv', delimiter=',', skip_header=1).astype(np.float) reference = np.array([[10, 5, 9, 3], [10, 1, 2, 6]]) assert np.array_equal(call_log, reference) # Read procedure calls and assert that they are what i think they should be shutil.rmtree(basepath + os.sep + subfolder)
def test_logger_benchmarks(): basepath = '.' subfolder = 'tmplog' log = exp.Logger(basepath, subfolder) # Create benchmarks log.log_benchmarks() # Read benchmarks and validate they are > 0 benchmarks = {} with open(basepath + "/benchmarks.yaml", 'r') as stream: benchmarks = yaml.safe_load(stream) assert 'benchmarks' in benchmarks assert 'matrix_inversion' in benchmarks['benchmarks'] assert 'sha_hashing' in benchmarks['benchmarks'] assert benchmarks['benchmarks']['matrix_inversion'] > 0 assert benchmarks['benchmarks']['sha_hashing'] > 0 # Test that benchmark file is not made a second time t_start = time.time() log.log_benchmarks() assert time.time() - t_start < 1 # Remove logs os.remove(basepath + os.sep + 'benchmarks.yaml') shutil.rmtree(log.path)
def test_logger_logsamples(): basepath = '.' subfolder = 'tmplog' log = exp.Logger(basepath, subfolder) # Log samples x = np.random.rand(1000, 3) y = np.random.rand(1000, 1).reshape(-1, 1) total = np.hstack((x, y)) log.log_samples(x, y) # Read log data = np.genfromtxt(basepath + os.sep + subfolder + os.sep + 'samples.csv', skip_header=1, delimiter=',') assert np.array_equal(total, data[:, 1:]) is True # Add new data log.log_samples(x, y) # Read log data = np.genfromtxt(basepath + os.sep + subfolder + os.sep + 'samples.csv', delimiter=',', skip_header=1).astype(np.float) assert np.array_equal(np.vstack((total, total)), data[:, 1:]) is True shutil.rmtree(basepath + os.sep + subfolder)