behaviorSummary.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon May 11 16:55:37 2020
  4. @author: Cecilia
  5. """
  6. """
  7. Summerize the behavioral data (the table in TWF manuscript) and
  8. """
  9. import os
  10. import pandas as pd
  11. import numpy as np
  12. #%%
  13. ## Get the path of the folder where the data csv file sits in.
  14. ## The results will save to this folder.
  15. #csv_path = r"C:\Users\Cecilia\OneDrive\Python\TWF_behaviour\behaviouralRecalculated\TWF_ITD_dataset_withoutCorrectionTrials.csv"
  16. csv_path = r"C:\Users\USER\OneDrive\Python\TWF_behaviour\behaviouralRecalculated\TWF_ITD_dataset_withoutCorrectionTrials.csv"
  17. folder_path, csv_filename = os.path.split(csv_path)
  18. ## Load data file (without correction trials)
  19. dataset = pd.read_csv(csv_path,encoding='utf-8-sig', sep='\s*,\s*', engine='python').copy()
  20. ## Get rid of the white spaces in front or at the end of collumns. If not doing so, will raise error when calling get_group()
  21. dataset['animal'] = dataset['animal'].astype(str).str.strip()
  22. dataset.columns = dataset.columns.str.strip()
  23. ## Get the unique condition and animal ID
  24. conditions = list(dataset['condition'].unique())
  25. animals = list(dataset['animal'].unique())
  26. #%% How many probe trials in each condition for each rat
  27. ## Extract the offset (ms) == 0 (probe) trials
  28. probe_data = dataset.loc[dataset['offset (ms)'] == 0].copy()
  29. ## Group data by condition and animal
  30. grouped_probe_data = probe_data.groupby(['condition','animal'])
  31. ## Print out the number of trials in each conditon
  32. for (probe_condition, probe_animal), probe_group in grouped_probe_data:
  33. print("The total number of probe trials in group {} {} is {}".format(probe_condition, probe_animal, probe_group.shape[0] ))
  34. #%% How is the performance in honesty trial in each condition for each rat
  35. ## calculate the honesty trial correct rate in each condition
  36. honesty_data = dataset.loc[np.abs(dataset['offset (ms)'])>dataset['jitter (ms)']].copy()
  37. ## Group data by condition and animal
  38. grouped_honesty_data = honesty_data.groupby(['condition','animal'])
  39. for (honesty_condition, honesty_animal), honesty_group in grouped_honesty_data:
  40. # The number of correct trials in each group
  41. honesty_correct=sum(honesty_group['correct'])
  42. print('Honesty trials in group {} {} :- Total:{} Correct: {} ({}%)'.format(honesty_condition, honesty_animal, len(honesty_group),honesty_correct,honesty_correct*100/len(honesty_group)))