-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavg.py
65 lines (54 loc) · 1.92 KB
/
avg.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
63
64
65
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import os
import os.path
from scipy.optimize import curve_fit
import shutil
os.getcwd()
# Loading file, and obtaining long name components
path = "./avgtest"
data_file = os.listdir(path) # Create a list of all files in the folder
files_dict = {}
# Filling up files_dict dictionary to contain list of files names
for files in data_file:
if files[-4:] == '.csv':
key = files[:-5]
files_dict.setdefault(key, [])
files_dict[key].append(files)
tb_avg = [] # To be averaged -> file names
for key, value in files_dict.items():
if len(value) > 1:
tb_avg.append([key,len(value)])
# We know it starts A, B, C, D ,E. 5 maximum
end = ['A.csv', 'B.csv', 'C.csv', 'D.csv', 'E.csv']
print("Files that needs to be concatenated (file, count):")
for files in tb_avg:
print(f'- {files[0]}, {files[1]}')
# Making a new file within the folder that contains all the data
os.chdir(path)
dirName = 'averaged_data'
# Making new directory to put processed data
if not os.path.exists(dirName):
os.mkdir(dirName)
print("Directory " , dirName , " Created ")
else:
print("Directory " , dirName , " already exists")
print("Recreating directory...")
shutil.rmtree(dirName)
os.mkdir(dirName)
print('Transferring files with count of 1...')
for key, value in files_dict.items():
if len(value) == 1:
df = pd.read_csv(value[0])
df.to_csv(f'./averaged_data/{value[0][:-4]}')
for i in np.arange(0, len(tb_avg)):
print(f'Processing {tb_avg[i][0]}')
file_list = []
for j in np.arange(0, int(tb_avg[i][1])):
file_list.append(tb_avg[i][0] + end[j])
working = pd.concat([pd.read_csv(f) for f in file_list], ignore_index=False, axis = 0)
working = working.drop(index=0)
working = working.astype('float64')
working.to_csv(f'./averaged_data/{file_list[0][:-6]}')
print('Done!')