-
Notifications
You must be signed in to change notification settings - Fork 1
/
csv-to-pandas.py
46 lines (37 loc) · 1.42 KB
/
csv-to-pandas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import os
import glob
import pandas as pd
# "Movement" types.. change as needed
MOVEMENT_TYPES = ['baseline', 'bite', 'blink']
'''
Function looks for the path to the data directory
'''
def dynamic_filepath():
for root, dirs, files in os.walk(os.getcwd(), topdown=False):
for name in dirs:
if name == "Bite and Blink Data Analysis v1":
return os.path.join(root, name)
def csv_to_dataframe(path_to_files = None):
if path_to_files is not None:
os.chdir(path_to_files)
else:
os.chdir(dynamic_filepath())
file_extension = '.csv'
# These are the column headers. Action type, time logs then all 4 channels (c1->c4) in order
tmp = [['Type', 'Time', 'c1', 'c2', 'c3', 'c4']]
for i in glob.glob(f"*{file_extension}"):
type = check_movement_type(i)
data = pd.read_csv(i, usecols=[1, 6, 7, 8, 9]) # based on current csv formatting
tmp.append([type, data['time'], data['c1'], data['c2'], data['c3'], data['c4']])
headings = tmp.pop(0)
temp = pd.DataFrame(tmp, columns=headings)
return temp
'''
Determines action type from CSV name.
@:returns movement type name if found in file name
'''
def check_movement_type(file_name):
for a in range(len(MOVEMENT_TYPES)):
if MOVEMENT_TYPES[a] in file_name.lower():
return MOVEMENT_TYPES[a]
raise ValueError("Input file: " + file_name + " has no recognizable movement type")