-
Notifications
You must be signed in to change notification settings - Fork 9
/
csv_calculations.py
62 lines (50 loc) · 2.07 KB
/
csv_calculations.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import re
import pandas as pd
import datetime
import string_manipulation as sm
import filesystem_changes as fc
# RETURNS PLAYER NAMES FROM THE GIVEN DATA FRAME
def get_player_names_from_csv(file_path, file_name):
file_name = sm.get_file_name(file_name)
df = pd.read_csv(f'{file_path}{file_name}.csv')
return sm.list_to_string(df['name'].tolist())
# ADDS PLAYER NAMES, ACTIONS ETC TO CSV
def add_to_csv(player_name, type_of_action, action, timestamp):
timestamp = str(datetime.timedelta(seconds=int(timestamp)))
data = {
'Name': [player_name],
'Action Family': [type_of_action],
'Action': [action],
'Timestamp': timestamp
}
if fc.check_if_file_exists(f'{fc.find_last_created_folder()}actions.csv'):
df2 = pd.DataFrame(data)
df2.to_csv(f'{fc.find_last_created_folder()}actions.csv', mode='a', index=False, header=False)
else:
df = pd.DataFrame(data)
df.to_csv(f'{fc.find_last_created_folder()}actions.csv', index=False, header=True)
# CREATES A NEW CSV FILE IF IT DOESN'T ALREADY EXIST
def create_csv():
try:
with open(f'{fc.find_last_created_folder().actions.csv}', 'w') as f:
f.close()
pass
except AssertionError as ae:
print(ae)
# READ THE GIVEN CSV AND CLEAN THE USELESS DATA FROM IT FOR THIS SPECIFIC SCENARIO
def read_and_clean(file):
df = pd.read_csv(file, sep=' ', header=None)
df = df.drop([6, 7, 8, 9, 10], axis=1)
df = df.dropna(axis='columns')
df = df.rename(columns={0: 'Frame', 1: 'ID', 2: 'x', 3: 'y', 4: 'w', 5: 'h'})
return df
# CLEANS UP THE CSV FILE AND KEEPS ONLY THE IMPORTANT FIELDS
def cleanup_csv_lineup(df):
try:
df = df.drop(['photoFolderIndex', 'photo', 'shortName'], axis=1)
except KeyError as ke:
ke
df['positions'] = df['positions'].apply(lambda x: re.sub(r'[^a-zA-Z, ]+', '', x))
return df
# if __name__ == '__main__':
# read_and_clean('player_detection/runs/track/exp73/Tactical View- Pixellot C Coaching.mp4')