#!/usr/bin/env python3

import glob
import os

import pandas as pd

import dfs

SRC_DIR = f"{dfs.get_data_dir()}/adhd_sin_orig"
OUT_DIR = f"{dfs.get_data_dir()}/adhd_sin"

if __name__ == '__main__':
  files = glob.glob(f"{SRC_DIR}/*.csv")
  file_names = list(map(os.path.basename, files))
  for file_name in file_names:
    df: pd.DataFrame = pd.read_csv(f'{SRC_DIR}/{file_name}').set_index('EyeTrackerTimestamp').sort_index()[
      ['GazePointX (ADCSpx)', 'GazePointY (ADCSpx)', 'PupilLeft', 'PupilRight']].reset_index()
    df.columns = ['t', 'x', 'y', 'dl', 'dr']
    # fill blanks (order=interpolate(inter)->bfill+ffill(edges))->zerofill
    df = df.apply(lambda x: x.interpolate().fillna(method="bfill").fillna(method="ffill")).fillna(0)
    df['x'] = df['x'] / 1920
    df['y'] = df['y'] / 1080
    df['d'] = (df['dl'] + df['dr']) / 2
    # start with t=0, and set unit to ms
    df['t'] = (df['t'] - df['t'].min()) / 1000
    df = df[['t', 'x', 'y', 'd']].round(6).set_index('t')
    df.to_csv(f'{OUT_DIR}/{file_name}')
    print(f'Processed: {file_name}')

[(14, 'glob.glob', 'glob.glob', False, 'import glob\n'), (10, 'dfs.get_data_dir', 'dfs.get_data_dir', False, 'import dfs\n'), (11, 'dfs.get_data_dir', 'dfs.get_data_dir', False, 'import dfs\n'), (17, 'pandas.read_csv', 'pd.read_csv', True, 'import pandas as pd\n')]