-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessing.py
93 lines (89 loc) · 3.02 KB
/
preprocessing.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from glob import glob
import pandas as pd
from datetime import datetime as dt
from tqdm import tqdm
import matplotlib.pyplot as plt
def loadIntoOneFile(logging=False):
if logging:
p = lambda _: print(_)
else:
p = lambda _: None
logging = not logging
# Get all the file names
file_names = glob('data/*.csv')
# Loop through the file names
data = []
# load and clean data
for file_name in tqdm(file_names, disable=logging):
name = file_name.split(f"\\")[1].split(".")[0]
# Read the file
df = pd.read_csv(file_name)
df.drop('machineId', axis=1, inplace=True)
if name == 'pressure':
df = df.reindex(columns=['_time', 'sensor', 'pressure'])
data += df.values.tolist()
# parse time data
for index, value in tqdm(enumerate(data), total=3947991, disable=logging):
date_time: str = value[0]
try:
if len(date_time) > 24:
date_time = date_time[:24]
if len(date_time) == 20:
date_time = date_time.replace("Z", ".000Z")
while len(date_time) < 24:
date_time = date_time.replace("Z", "0Z")
data[index][0] = dt.fromisoformat(date_time[:-1])
except ValueError as e:
p(data[index-2])
p(f"{index=}, {value=}, {len(value[0])=}")
p(f"{date_time=} {len(date_time)=}\n")
raise ValueError(e)
df = pd.DataFrame(data, columns=['time', 'device', 'value'])
df['device'] = df['device'].fillna('pressure0')
devices = df['device'].unique()
data = df.values.tolist()
# Create empty dataframe with column names
cols = ['time'] + devices.tolist()
df = []
indexLookup = {}
for i, device in enumerate(cols):
indexLookup[device] = i
# loop through the lists
for time, device, value in tqdm(data, disable=logging):
temp = [0]*len(cols)
temp[0] = time
try:
temp[indexLookup[device]] = value
except Exception as e:
print(time, device, value)
raise e
df.append(temp)
df = pd.DataFrame(df, columns=cols)
df.sort_values(by="time", ascending=True, inplace=True)
df = df.drop_duplicates()
p(df.head())
df.to_csv('combined.csv', index=False)
return df
def interpolate(df: pd.DataFrame|str):
if isinstance(df, str):
df = pd.read_csv(df)
print(len(df))
# to_fill = df.columns.tolist()[1:]
df['time'] = pd.DatetimeIndex(df['time'])
df = df.replace(to_replace=0, method='bfill')
df = df.replace(to_replace=0, method='ffill')
df = df.groupby(
pd.Grouper(key='time', freq='10S')
).mean()
df.dropna(inplace=True)
df = (df - df.mean()) / df.std()
df.dropna(axis=1, inplace=True, how='all')
df.fillna(0, inplace=True)
df.to_csv('pre_processed.csv', index=True)
return df
if __name__ == '__main__':
# df = loadIntoOneFile(logging=True)
df = interpolate('combined.csv')
print(len(df))
print(df.head())
print(df.tail())