stim_param_test.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from view.python_core.measurement_list import MeasurementList
  2. import pathlib as pl
  3. import tempfile
  4. def valid_stim_params_test():
  5. """
  6. Testing whether valid stim paramter settings do not cause an error
  7. :return:
  8. """
  9. valid_stim_list = 'tests/test_files/measurement_test_files/190112_locust_ip_VALID_stims.lst.xls'
  10. ml = MeasurementList.create_from_lst_file(valid_stim_list, LE_loadExp=4)
  11. for measu_index, measu in enumerate(ml.get_measus()):
  12. p1_metadata, extra_metadata = ml.get_p1_metadata_by_index(measu_index)
  13. def invalid_stim_params_test():
  14. """
  15. Testing whether invalid stim parameter settings do cause an error
  16. :return:
  17. """
  18. invalid_stim_list = 'tests/test_files/measurement_test_files/190112_locust_ip_INVALID_stims.lst.xls'
  19. ml = MeasurementList.create_from_lst_file(invalid_stim_list, LE_loadExp=4)
  20. for measu_index, measu in enumerate(ml.get_measus()):
  21. try:
  22. p1_metadata, extra_metadata = ml.get_p1_metadata_by_measu(measu)
  23. except ValueError as e:
  24. pass
  25. except AssertionError as e:
  26. pass
  27. def stim_spec_test_generator():
  28. test_root = "tests/test_files/measurement_test_files/valid_files"
  29. test_root_path = pl.Path(test_root)
  30. dirs = [child for child in test_root_path.iterdir() if child.is_dir()]
  31. for direc in dirs:
  32. for child in direc.iterdir():
  33. if child.suffix == ".xls":
  34. try_importing_measurement_list.description = f"Testing with the stimulus specification in " \
  35. f"{child.relative_to(test_root_path)}"
  36. yield try_importing_measurement_list, str(child)
  37. def try_importing_measurement_list(xls):
  38. expected_csv = f"{xls.split('.')[0]}.csv"
  39. ml = MeasurementList.create_from_lst_file(xls, LE_loadExp=666)
  40. p1_metadata, extra_metadata = ml.get_p1_metadata_by_measu(1)
  41. temp_out = str(pl.Path(tempfile.gettempdir()) / "view_temp.csv")
  42. temp = p1_metadata["pulsed_stimuli_handler"].stimulus_frame.copy()
  43. del temp["Sampling Period"]
  44. temp.to_csv(str(temp_out))
  45. # this works for expected files saved from linux when tested on linux and windows, filecmp.cmp does not.
  46. # might have something to do line terminators
  47. with open(temp_out) as fho:
  48. with open(expected_csv) as fhe:
  49. assert fho.read() == fhe.read()
  50. if __name__ == "__main__":
  51. # valid_stim_params_test()
  52. try_importing_measurement_list("tests/test_files/measurement_test_files/valid_files/"
  53. "one_stim/stimON_stimOFF.lst.xls")
  54. # try_importing_measurement_list("tests/test_files/measurement_test_files/valid_files/"
  55. # "two_stim_new_mixed/stimON_stimOFF_stimLen.lst.xls")