avg_react_data.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python3
  2. """
  3. This is free and unencumbered software released into the public domain.
  4. Anyone is free to copy, modify, publish, use, compile, sell, or
  5. distribute this software, either in source code form or as a compiled
  6. binary, for any purpose, commercial or non-commercial, and by any
  7. means.
  8. In jurisdictions that recognize copyright laws, the author or authors
  9. of this software dedicate any and all copyright interest in the
  10. software to the public domain. We make this dedication for the benefit
  11. of the public at large and to the detriment of our heirs and
  12. successors. We intend this dedication to be an overt act of
  13. relinquishment in perpetuity of all present and future rights to this
  14. software under copyright law.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. For more information, please refer to [http://unlicense.org]
  23. """
  24. import os
  25. import sys
  26. def get_last_line_count(file):
  27. last_line = ''
  28. with open(file, 'r') as f:
  29. for line in f:
  30. if line:
  31. last_line = line
  32. return float(last_line.split()[1])
  33. def add_last_line_observables_counts(dir, counts):
  34. seed_dir = os.path.join(os.getcwd(), dir)
  35. file_list = os.listdir(seed_dir)
  36. for file in file_list:
  37. file_path = os.path.join(seed_dir, file)
  38. if os.path.isfile(file_path) and file.endswith('.dat'):
  39. cnt = get_last_line_count(file_path)
  40. observable = os.path.splitext(file)[0]
  41. if observable.endswith('_MDLString'):
  42. observable = observable[:-len('_MDLString')]
  43. if observable in counts:
  44. counts[observable] += cnt
  45. else:
  46. counts[observable] = cnt
  47. # just one variant for now, but this could grow int oa more versatile tool
  48. def main():
  49. # go through all seed_xxx directories in the current directory
  50. dir_list = os.listdir(os.getcwd())
  51. num_dirs = 0
  52. counts = {}
  53. for dir in dir_list:
  54. if os.path.isdir(dir) and dir.startswith('seed_'):
  55. add_last_line_observables_counts(dir, counts)
  56. num_dirs += 1
  57. for name, count in sorted(counts.items()):
  58. print(name.ljust(20) + ': ' + format(float(count)/num_dirs, '.4f'))
  59. if __name__ == "__main__":
  60. main()