-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiles.py
159 lines (113 loc) · 4.23 KB
/
files.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""
Functions to interact with REDD's .dat files and their structure,
as well as our custom .npy files
"""
import numpy as np
import os
import shutil
PATH = "C:/users/arneb/P&O3/Data/REDD/data/low_freq" # Path to the low_freq folder of the REDD dataset
POWER_DICT = {
"oven": 100,
"refrigerator": 800,
"dishwaser": 100,
"kitchen_outlets": 100,
"lighting": 100,
"washer_dryer": 100,
"microwave": 1000,
"bathroom_gfi": 100,
"electric_heat": 100,
"stove": 100
}
WIDTH_DICT = {
"oven": 100,
"refrigerator": 100,
"dishwaser": 100,
"kitchen_outlets": 100,
"lighting": 100,
"washer_dryer": 100,
"microwave": 100,
"bathroom_gfi": 100,
"electric_heat": 100,
"stove": 100
}
def get_labels(house, path=PATH):
d = {}
with open(path + f"/house_{house}/labels.dat", 'r') as file:
for line in file.readlines():
if not line: continue
d.update({line.split()[0]: line.split()[1]})
return d
def get_max_power(house, channel, path=PATH):
with open(path + f"/house_{house}/labels.dat", 'r') as file:
line = file.readlines()[channel - 1]
appliance = line.split()[1]
return POWER_DICT[appliance]
def get_rectangle_width(house, channel, path = PATH):
return 100
with open(path + f"/house_{house}/labels.dat", 'r') as file:
line = file.readlines()[channel - 1]
appliance = line.split()[1]
return WIDTH_DICT[appliance]
def read_dat_file(path):
"""
Reads the data from a channel's .dat file into two numpy arrays: one
holding the Unix timestamps, the other holding the corresponding data
"""
with open(path, 'r') as file:
# Put the times and data into numpy arrays
times, data = np.array(list(map(lambda line: line.split(), file.readlines()))[:-1]).transpose()
# Convert to the appropriate types
times = times.astype(np.int32)
data = data.astype(np.float32)
return times, data
def write_dat_file(path, times, data):
"""
Writes the times and data entries into the .dat file specified by path
"""
with open(path, "w+") as file:
for i in range(len(data)):
file.write(str(times[i]) + ' ' + str(round(data[i], 2)) + '\n')
def read_npy_file(path):
"""
Reads the data from a .npy file, returning numpy arrays of the Unix
timestamps and the corresponding data
"""
# First load the array we stored into the file
data = np.load(path)
# Split it into the actual data and the timestamps
raw_times = np.copy(data[-2:])
data.resize((len(data) - 2,))
begin_time, end_time = np.frombuffer(raw_times.tobytes(), dtype=np.int32)
times = np.arange(begin_time, end_time + 1)
return times, data
def write_npy_file(path, times, data):
"""
Writes the times and data arrays into the .npy file specified by path
"""
# First put the begin and end timestamps into their own array
array_times_int = np.array([times[0], times[-1]])
# Then make them pretend to be floats to fit into the data array
array_times_float = np.frombuffer(array_times_int.tobytes(), dtype=np.float32)
data = np.append(data, array_times_float)
np.save(path, data)
def read_file(path, ftype):
if ftype == "npy":
return read_npy_file(path)
elif ftype == "dat":
return read_dat_file(path)
else:
raise ValueError
def write_file(path, times, data, ftype):
if ftype == "npy":
return write_npy_file(path, times, data)
elif ftype == "dat":
return write_dat_file(path, times, data)
else:
raise ValueError
def delete_preprocessed(path=PATH):
"""
Deletes all preprocessed data anywhere in the given path
"""
for p, _, _ in os.walk(path):
if os.path.exists(p) and os.path.isdir(p) and ("_preprocessed" in p):
shutil.rmtree(p)