Example #1
0
def test_get_simulation_error_end_point():
  force_file = os.path.join(TEST_DATA_ROOT, "forces.txt")
  fit_data = np.loadtxt(force_file, skiprows=1)[:, -1]
  target_truth = np.mean(fit_data[-10:])
  this_sr_dict = copy.copy(SIM_RUNNER_DICT)
  this_sr_dict["fit_mode"] = "end_point"
  this_sr_dict["output_dir"] = TEST_DATA_ROOT
  this_sr_dict["target_data"] = target_truth
  sr = SimulationRunner(**this_sr_dict)
  sr.read_simulation_results()

  assert (np.isclose(sr.get_simulation_error(), 0)), ("SimulationRunner not calculating error "
    "correctly!")

# def test_run_simulation():
#   """This uses the `echo` windows command to test that the simulation runs correctly"""
#   this_sr_dict = copy.copy(SIM_RUNNER_DICT)
#   this_sr_dict["fibersim_file"] = "echo"
#   this_sr_dict["options_file"] = "Hello,"
#   this_sr_dict["model_file"] = "World!"
#   # this_sr_dict["protocol_file"] = ">"
#   # this_sr_dict["output_dir"] = os.path.realpath(os.path.join(TEST_RESULT_DIR, "sim_run_test.txt"))
#   sr = SimulationRunner(**this_sr_dict)

#   sr.run_simulation()

#   # Read the newly created file.
#   with open(this_sr_dict["output_dir"], 'r') as f:
#     text = f.read()

#   assert (text == "Hello, World!"), "SimulationRunner did not run simulation correctly!"
Example #2
0
def test_get_simulation_error_time():
  force_file = os.path.join(TEST_DATA_ROOT, "forces.txt")
  results_truth = np.loadtxt(force_file, skiprows=1)[:, -1]
  sr = SimulationRunner(**SIM_RUNNER_DICT)
  sr.fit_data = results_truth

  assert (np.isclose(sr.get_simulation_error(), 0)), ("SimulationRunner not calculating error "
    "correctly!")
Example #3
0
def test_read_simulation_results_force():
  force_file = os.path.join(TEST_DATA_ROOT, "forces.txt")
  results_truth = np.loadtxt(force_file, skiprows=1)[:, -1]
  this_sr_dict = copy.copy(SIM_RUNNER_DICT)
  this_sr_dict["output_dir"] = TEST_DATA_ROOT
  sr = SimulationRunner(**this_sr_dict)

  sr.read_simulation_results()

  assert (np.array_equal(sr.fit_data, results_truth)), ("SimulationRunner did not read in "
    "simulation results correctly!")
Example #4
0
def test_simulation_runner_initialization():
  sr = SimulationRunner(**SIM_RUNNER_DICT)

  msg = "SimulationRunner did not initialize correctly!"
  assert (sr), msg
  assert (sr.iteration_number == 0), msg
  assert (sr.best_error == np.inf), msg
  assert (sr.error_values == []), msg
Example #5
0
def test_read_target_data_maintains_shape_of_target_data():
  target_data_orig = np.loadtxt(SIM_RUNNER_DICT["target_data"])
  this_sr_dict = copy.copy(SIM_RUNNER_DICT)
  this_sr_dict["target_data"] = target_data_orig

  sr = SimulationRunner(**this_sr_dict)

  assert (sr.target_data.shape[0] == target_data_orig.shape[0]), ("SimulationRunner reading of "
    "target data does NOT preserve shape of target data! THIS IS CURRENTLY IGNORED")
Example #6
0
def test_read_target_data_muscle_force_time_string():
  sr = SimulationRunner(**SIM_RUNNER_DICT)

  target_data_truth = np.loadtxt(SIM_RUNNER_DICT["target_data"])
  sim_times = protocol.get_sim_time(SIM_RUNNER_DICT["protocol_file"])

  # Linearly interpolate the target data according to the simulation time steps.
  times_to_interpolate = (np.asarray(sim_times[SIM_RUNNER_DICT["time_steps_to_steady_state"]:])
    - sim_times[SIM_RUNNER_DICT["time_steps_to_steady_state"]])
  interpolated_values = np.interp(times_to_interpolate, target_data_truth[:, 0], 
    target_data_truth[:, 1])
  
  # Concatenate these back together to form the newly interpolated target data and store it.
  target_data_truth = np.stack((times_to_interpolate, interpolated_values), axis=-1)

  assert (np.array_equal(sr.target_data, target_data_truth)), ("SimulationRunner did not read "
    "target data correctly!")
Example #7
0
def test_get_simulation_times():
  sr = SimulationRunner(**SIM_RUNNER_DICT)

  truth_times = protocol.get_sim_time(SIM_RUNNER_DICT["protocol_file"])

  assert (sr.sim_times == truth_times), "SimulationRunner did not read protocol times correctly!"