-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogs.py
More file actions
58 lines (46 loc) · 1.4 KB
/
logs.py
File metadata and controls
58 lines (46 loc) · 1.4 KB
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
import numpy as np
import glob
class DataLog(object):
def __init__(self, path, shape=None, dtype=np.float, resume=False):
self.path = path
self.file_counter = 0
if shape is not None:
self.pointer = 0
self.array = np.zeros(shape, dtype=dtype)
self.max_len = shape[0]
if resume:
files = self._available_files()
self.file_counter = max(files) + 1
def __iter__(self):
files = glob.glob(self.path + "_*.npy")
for f in files:
yield np.load(f)
def _available_files(self):
files = glob.glob(self.path + "_*.npy")
files = [int(f[len(self.path) + 1:-4]) for f in files]
return files
def next_file_name(self):
path = self.path + "_%i" % self.file_counter
self.file_counter += 1
return path
def add(self, *values):
self.array[self.pointer] = values
self.pointer += 1
if self.pointer >= self.max_len:
self.save()
def save(self):
if self.pointer:
path = self.next_file_name()
np.save(path, self.array[:self.pointer])
self.pointer = 0
if __name__ == "__main__":
d = DataLog("temp", (5, 2), resume=True)
d.add(1, 2)
d.add(1, 2)
d.add(1, 2)
d.add(1, 2)
d.add(1, 2)
d.add(1, 2)
d.add(1, 2)
for a in d:
print(a)