correct_fausey_ages.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!usr/bin/env python
  2. # -*- coding: utf8 -*-
  3. import os
  4. import pandas as pd
  5. from ChildProject.projects import ChildProject
  6. def _correct_ages(path_dataset, path_metrics):
  7. project = ChildProject(path_dataset)
  8. project.read()
  9. project.recordings["age"] = project.compute_ages()
  10. updated_ages = project.recordings[["age", "session_id"]]
  11. updated_ages['age'] = updated_ages['age'].apply(int)
  12. metrics = pd.read_csv(path_metrics)
  13. for _, row in updated_ages.iterrows():
  14. old_age = metrics.loc[metrics['session_id']==row['session_id']]
  15. if not old_age.empty:
  16. metrics.loc[metrics['session_id']==row['session_id'], 'age'] = row['age']
  17. metrics.to_csv(path_metrics, index=False)
  18. def main(**kwargs):
  19. _correct_ages(**kwargs)
  20. def _parse_args(argv):
  21. import argparse
  22. parser = argparse.ArgumentParser(description='Correct ages in the metrics files.')
  23. parser.add_argument('--path-dataset', help='Path to the dataset.')
  24. parser.add_argument('--path-metrics', help='Path to the CSV metrics file.')
  25. args = parser.parse_args(argv)
  26. return vars(args)
  27. if __name__ == '__main__':
  28. import sys
  29. import logging
  30. pgrm_name, argv = sys.argv[0], sys.argv[1:]
  31. args = _parse_args(argv)
  32. try:
  33. main(**args)
  34. sys.exit(0)
  35. except Exception as e:
  36. logging.exception(e)
  37. sys.exit(1)