interpolate_general.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. from scipy import signal
  4. import numpy as np
  5. import math
  6. import easygui
  7. scorer=['DLC_resnet50_Cylinder_camera-1Jun15shuffle1_250000', 'DLC_resnet50_Cylinder_camera-2Jun15shuffle1_250000', 'DLC_resnet50_GridWalk_camera-1Jun15shuffle1_250000', 'DLC_resnet50_GridWalk_camera-2Jun15shuffle1_250000']
  8. path=easygui.fileopenbox(multiple=True, default='/home/user/owncloud/3D_videos/3D/GW', filetypes="*.csv")
  9. def likelihood(data, threshold):
  10. coln=data.columns
  11. for i in range(1, len(coln), 3):
  12. data.loc[data[coln[i][0], 'likelihood']<threshold, (coln[i][0], 'x')]=math.nan
  13. data.loc[data[coln[i][0], 'likelihood']<threshold, (coln[i][0], 'y')]=math.nan
  14. return data
  15. def prep_dlc(pathway, threshold, height):
  16. data=pd.read_csv(pathway, index_col=0, delimiter=',', skiprows=0, header=[1,2])
  17. data=likelihood(data, threshold)
  18. #data=y_invert(data, height)
  19. return data
  20. for i in path:
  21. header = list(range(3))
  22. data=prep_dlc(i, 0.9, 1080)
  23. names=[i[0] for i in data.columns[::3]]
  24. for j in names:
  25. if np.count_nonzero(data['snout', 'x'].value_counts())<2:
  26. data[j, 'x']=data[j, 'x'].interpolate(method='polynomial', order=1)
  27. data[j, 'y']=data[j, 'y'].interpolate(method='polynomial', order=1)
  28. data[j, 'likelihood']=0.95
  29. data.loc[pd.isna(data[j, 'x']), (j, 'likelihood')]=0.1
  30. data.loc[pd.isna(data[j, 'y']), (j, 'likelihood')]=0.1
  31. if (('CY') and ('camera-1')) in i:
  32. data=pd.concat([data], keys=[scorer[0]], axis=1, names=['scorer'])
  33. elif (('CY') and ('camera-2')) in i:
  34. data=pd.concat([data], keys=[scorer[1]], axis=1, names=['scorer'])
  35. elif (('GW') and ('camera-1')) in i:
  36. data=pd.concat([data], keys=[scorer[2]], axis=1, names=['scorer'])
  37. elif (('GW') and ('camera-2')) in i:
  38. data=pd.concat([data], keys=[scorer[3]], axis=1, names=['scorer'])
  39. if 'filtered' in i:
  40. data.to_hdf(i.replace('_filtered.csv', '.h5'), key='df_with_missing', mode='w')
  41. data.to_hdf(i.replace('_filtered.csv', '_filtered.h5'), key='df_with_missing', mode='w')
  42. data.to_hdf(i, key='df_with_missing', mode='w')
  43. data.to_hdf(i.replace('_filtered.csv', '.csv'), key='df_with_missing', mode='w')
  44. else:
  45. data.to_hdf(i.replace('.csv', '.h5'), key='df_with_missing', mode='w')