validate_config.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import glob
  2. import yaml
  3. from cerberus import Validator
  4. def validate_schema(params):
  5. # config_files = glob.glob('**/config.yaml', recursive=True)
  6. # fname = config_files[0]
  7. # with open(fname) as stream:
  8. # params = yaml.load(stream, Loader=yaml.FullLoader)
  9. # print(params)
  10. schema = {
  11. 'daq': {
  12. 'type': 'dict',
  13. 'schema': {
  14. 'n_channels_max': {'required': True, 'type': 'integer', 'min': 1, 'max': 128},
  15. 'daq_sleep': {'required': True, 'type': 'float', 'min': 0, 'max': 1.},
  16. 'fs': {'required': True, 'type': 'float', 'min': 1., 'max': 30000.},
  17. 'smpl_fct': {'required': True, 'type': 'integer', 'min': 1, 'max': 30000},
  18. }
  19. },
  20. 'speller': {
  21. 'type': 'dict',
  22. 'schema': {
  23. 'type': {'required': True, 'type': 'string', 'allowed': ['question', 'exploration', 'training_color', 'color', 'feedback']}
  24. }
  25. },
  26. 'recording': {
  27. 'type': 'dict',
  28. 'schema': {
  29. 'timing': {
  30. 'type': 'dict',
  31. 'schema': {
  32. 't_baseline_1': {'required': True, 'type': 'float', 'min': 0., 'max': 600.},
  33. 't_baseline_all': {'required': True, 'type': 'float', 'min': 0., 'max': 10.},
  34. 't_baseline_rand': {'required': True, 'type': 'float', 'min': 0., 'max': 10.},
  35. 't_after_stimulus': {'required': True, 'type': 'float', 'min': 0., 'max': 10.},
  36. 't_response': {'required': True, 'type': 'float', 'min': 0., 'max': 60.},
  37. 'decoder_refresh_interval': {'required': True, 'type': 'float', 'min': 0., 'max': 10.},
  38. 'bci_loop_interval': {'required': True, 'type': 'float', 'min': 0., 'max': 10.},
  39. 'recording_loop_interval': {'required': True, 'type': 'float', 'min': 0., 'max': 10.},
  40. 'recording_loop_interval_data': {'required': True, 'type': 'float', 'min': 0., 'max': 10.},
  41. }
  42. }
  43. }
  44. }
  45. }
  46. # print(schema)
  47. v = Validator()
  48. v.schema = schema
  49. v.allow_unknown = True
  50. validation_passed = v.validate(params, schema)
  51. errors = v.errors
  52. # print("Validation validation_passed: {}".format(validation_passed))
  53. # print("Errors: {}".format(errors))
  54. return validation_passed, errors
  55. if __name__ == '__main__':
  56. config_files = glob.glob('**/config.yaml', recursive=True)
  57. fname = config_files[0]
  58. validation_passed, errors = validate_schema(fname)
  59. # print(validation_passed)
  60. if validation_passed is True:
  61. print("Config file validation success! All values are as expected.")
  62. else:
  63. print("Config file validation failed with the following errors:")
  64. for key, value in errors.items():
  65. print("\nerror in {}: {}".format(key, errors[key]))