-
Notifications
You must be signed in to change notification settings - Fork 1
/
sharedlib.py
672 lines (555 loc) · 23.5 KB
/
sharedlib.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
#!/usr/bin/env python
""" A collection of shared classes/functions for the intutive agent.
Dependencies:
Pandas (pip install pandas)
PyTorch (see https://pytorch.org)
"""
__author__ = "Dustin Fast"
__email__ = "dustin.fast@outlook.com"
__license__ = "GPLv3"
# Std lib
import os
import logging
import logging.handlers
# Third-party
import torch
from torch.utils.data import Dataset
from torch.autograd import Variable as V
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
# User configurable
OUT_PATH = 'var' # Log and Model file output directories
LOGFILE_EXT = '.log' # Log file extension
LOG_LEVEL = logging.DEBUG # Log file level
LOG_SIZE = 1 * 1048576 # Log file size (x * bytes in a mb)
LOG_FILES = 2 # Concurrent locating log file count
# Global logger
g_logger = None
#################
# Class Lib #
#################
class ModelHandler(object):
""" The ModelHandler, used by many of the inutive agent's classes for
logging, saving, and loading methods. A "child" is defined here as
any module using this class, not necessarily inheriting from it.
Console Output:
Each child may output via self.log() with console_out enabled.
Model Persistance:
If persist mode enabled, any string passed to self.log() will also
be logged to the model log file OUT_PATH/child_type/child_ID.log.
Additionally, saving/loading of the model is enabled to/from file
OUT_PATH/child_type/child_ID.model_ext w/ self.save() & self.load()
"""
def __init__(self, child, console_out, persist, **kwargs):
""" Accepts the following parameters:
child (obj) : A ref to the child object
console_out (bool) : Console output flag
persist (bool) : Persist mode flag
If persist, kwargs must include:
model_ext (str) : Model filename extension. Ex: '.pt'
save_func (str) : Save function and args (see example)
load_func (str) : Load function and args (see example)
Load/Save function example:
save_func = "torch.save(self.state_dict(), 'MODEL_FILE')"
load_func = "load_state_dict(torch.load('MODEL_FILE'))"
The func string will be used to save/load with eval()
Note: The 'MODEL_FILE' placeholder is required iff func !=None
Note: Ensure depencies of save/load funcs are imported
"""
# Model properties
self._child = child
self._console_out = console_out
self._persist = persist
self._model_file = None
self._save_func = None
self._load_func = None
# Init persist mode, if specified
if self._persist:
model_ext = kwargs.get('model_ext')
save_func = kwargs.get('save_func')
load_func = kwargs.get('load_func')
# Ensure good accompanying kwargs
if not (model_ext and save_func and load_func):
raise Exception(
'ERROR: Persistant mode set but missing attributes.')
# Form filenames from kwargs
child_type = child.__class__.__name__.lower()
output_path = OUT_PATH + '/' + child_type
file_prefix = output_path + '/' + child.ID
log_file = file_prefix + LOGFILE_EXT
self._model_file = file_prefix + model_ext
# Replace the 'MODEL_FILE' placeholder in load/save_func
self._save_func = save_func.replace('MODEL_FILE', self._model_file)
self._load_func = load_func.replace('MODEL_FILE', self._model_file)
if self._save_func == save_func or self._load_func == load_func:
raise Exception("ERROR: Missing 'MODEL_FILE' placeholder.")
# Replace refs to self with refs to child obj
self._save_func = self._save_func.replace('self.', 'self._child.')
self._load_func = self._load_func.replace('self.', 'self._child.')
# Create output path if not exists
if not os.path.exists(OUT_PATH):
os.mkdir(OUT_PATH)
if not os.path.exists(output_path):
os.mkdir(output_path)
# Init global logger (if not already) - it gets first callers ID
global g_logger
if not g_logger:
g_logger = Logger(child.ID, log_file, False)
self.log('*** Initialized ' + child_type + ': ' + str(child))
# Denote model filename and, if it exists, load the model from it
if os.path.isfile(self._model_file):
self.load()
def log(self, log_str, level=logging.info):
""" Logs the given string to the log file, iff persist enabled.
Also outputs the string to the console, iff console_out enabled.
"""
if self._persist:
g_logger.info(log_str)
if self._console_out:
print(log_str)
def save(self):
""" Saves the model to the file given by self._model_file.
"""
try:
eval(self._save_func)
self.log('Saved model to: ' + self._model_file)
except Exception as e:
err_str = 'Error saving model: ' + str(e)
self.log(err_str, level=logging.error)
raise Exception(err_str)
def load(self):
""" Loads the model from the file given by self._model_file.
"""
# try:
eval(self._load_func)
self.log('Loaded model from: ' + self._model_file)
# except Exception as e:
# err_str = 'Error loading model: ' + str(e)
# self.log(err_str, level=logging.error)
# raise Exception(err_str)
class DataFrom(Dataset):
""" A set of inputs & targets (i.e. labels & features) as torch.tensors,
populated from the given CSV file and normalized if specified.
self.inputs = torch.FloatTensor, with a gradient for torch.optim.
self.targets = torch.FloatTensors, converted from str->float if needed.
Assumes CSV file w/no header with format: label, feat_1, ... , feat_n
"""
def __init__(self, csvfile, normalize=True):
""" Accepts the following parameters:
csvfile (str) : CSV filename
normalize : If True, inputs data is normalized
"""
self.inputs = None # 3D Inputs tensor
self.targets = None # 3D Targets tensor
self.raw_inputs = None # Original inputs form
self.raw_targets = None # Original targets form
self.class_labels = None # Unique instance labels
self.class_count = None # Num unique labels
self.feature_count = None # Num input features
self.row_count = None # Num data rows
self.normalized = normalize # Denotes normalized data
self.fname = csvfile # CVS file name
# Load data
data = pd.read_csv(csvfile, header=None) # csvfile -> pd.DataFrame
inputs = data.loc[:, 1:] # All cols but leftmost
targets = data.loc[:, :0] # Only leftmost col
# Populate non-tensor member info
self.raw_inputs = inputs
self.raw_targets = targets
self.class_labels = sorted(list(data[0].unique()), key=lambda x: x)
self.class_count = len(self.class_labels)
self.row_count = len(inputs)
# Load inputs and normalize if specified
if normalize:
self.norm_max = max(inputs.max())
self.norm_min = min(inputs.min())
inputs.apply(self._normalize)
# Store inputs as a torch.tensor with gradients
self.inputs = V(torch.FloatTensor(inputs.values), requires_grad=True)
self.feature_count = self.inputs.size()[1]
# Init targets
targets = targets.apply(lambda t: self._map_outnode(t.iloc[0]), axis=1)
self.targets = targets
def __str__(self):
str_out = 'Classes: ' + str(self.class_labels) + '\n'
str_out += 'Row 1 Target: ' + str(self.targets[0]) + '\n'
str_out += 'Row 1 Inputs: ' + str(self.inputs[0])
return str_out
def __len__(self):
return len(self.targets)
def __getitem__(self, idx):
return self.inputs[idx], self.targets[idx]
def _map_outnode(self, label):
""" Given a class label, returns zeroed tensor with tensor[label] = 1.
Facilitates mapping each class to its corresponding output node
"""
tgt_width = len(self.class_labels)
target = torch.tensor([0 for i in range(tgt_width)], dtype=torch.float)
target[self.class_labels.index(label)] = 1
return target
def _normalize(self, t):
""" Returns a normalized representation of the given tensor
"""
return (t - self.norm_min) / (self.norm_max - self.norm_min)
class Logger(logging.Logger):
""" An extension of Python's logging.Logger. Implements log file rotation
and optional console output.
"""
def __init__(self,
name,
fname,
console_output=False,
level=LOG_LEVEL,
num_files=LOG_FILES,
max_filesize=LOG_SIZE):
""""""
logging.Logger.__init__(self, name, level)
# Define output formats
log_fmt = '%(asctime)s - %(levelname)s: %(message)s'
log_fmt = logging.Formatter(log_fmt + '')
# Init log file rotation
rotate_handler = logging.handlers.RotatingFileHandler(
fname, max_filesize, num_files)
rotate_handler.setLevel(level)
rotate_handler.setFormatter(log_fmt)
self.addHandler(rotate_handler)
if console_output:
console_fmt = '%(asctime)s - %(levelname)s:'
console_fmt += '\n%(message)s'
console_fmt = logging.Formatter(console_fmt)
console_handler = logging.StreamHandler()
console_handler.setLevel(level)
console_handler.setFormatter(console_fmt)
self.addHandler(console_handler)
class AttrIter(object):
""" An iterable set of parrallel attribute stacks (one set per label).
The iterator returns the next set of all attributes for every label
as a dict: { ID_1: {attr_1: val, attr_n: val}, ... }
"""
def __init__(self):
self._results = {} # {ID: {attr_1: [val_x], attr_n: [val_x]}, ..}
def __str__(self):
return str(self._results)
def push(self, label, attr, value):
""" Push the given results attribute to its stack for the given label.
Note: To keep stacks balanced, each push() for a labels's attribute
should be followed by the rest of the attributes for that label.
"""
try:
node = self._results[label]
except KeyError:
self._results[label] = {}
node = self._results[label]
try:
val_list = node[attr]
except KeyError:
node[attr] = []
val_list = node[attr]
val_list.append(value)
def rm_empties(self, attr):
""" Removes all sets/labels having no attributes of the given name.
"""
self._results = {k: v for k, v in self._results.items() if v[attr]}
def is_empty(self, attr):
""" Returns True if the given attr exists for any label. Else False.
"""
if {k: v for k, v in self._results.items() if v[attr]}:
return False
return True
def keys(self):
""" Returns a list of this object's keys/labels.
"""
return [k for k in self._results.keys()]
def __iter__(self):
return self
def __next__(self):
""" Dequeues and returns a single set of attributes for each label.
"""
attributes = {}
good_results = False
for label, attrs in self._results.items():
attributes[label] = {}
for k, v in attrs.items():
try:
attributes[label][k] = v.pop()
good_results = True
except IndexError:
attributes[label][k] = None
if not good_results:
raise StopIteration
return attributes
class Queue:
""" A Queue data structure.
Exposes reset, push, pop, shove, cut, peek, top, is_empty, item_count,
and get_items.
TODO: contains. use list.pop()
"""
def __init__(self, maxsize=None):
self.items = [] # Container
self.maxsize = maxsize # Max size of self.items
# Validate maxsize and populate with defaultdata
if maxsize and maxsize < 0:
raise Exception('Invalid maxsize parameter.')
def __len__(self):
return len(self.items)
def reset(self):
""" Clear/reset queue items.
"""
self.items = []
def push(self, item):
""" Adds an item to the back of queue.
"""
if self.is_full():
raise Exception('Attempted to push item to a full queue.')
self.items.append(item)
def shove(self, item):
""" Adds an item to the back of queue. If queue already full, makes
room for it by removing the item at front. If an item is removed
in this way, is returned.
"""
removed = None
if self.is_full():
removed = self.pop()
self.items.append(item)
return removed
def cut(self, n, item):
""" Inserts an item at the nth position from queue front. Existing
items are moved back to accomodate it.
"""
length = len(self)
if length < n + 1:
raise Exception('Attempted to cut at an out of bounds position.')
if length >= self.maxsize:
raise Exception('Attempted to cut into a full queue.')
self.items = self.items[:n] + item + self.items[n:] # TODO: Test cut
def pop(self):
""" Removes front item from queue and returns it.
"""
if self.is_empty():
raise Exception('Attempted to pop from an empty queue.')
d = self.items[0]
self.items = self.items[1:]
return d
def peek(self, n=0): # TODO: Test safety of peek
""" Returns the nth item from queue front. Leaves queue unchanged.
"""
if len(self) < n + 1:
raise Exception('Attempted to peek at an out of bounds position.')
if self.is_empty():
raise Exception('Attempted to peek at an empty.')
return self.items[n]
def is_empty(self):
""" Returns true iff queue empty.
"""
return len(self) == 0
def is_full(self):
""" Returns true iff queue at max capacity.
"""
return self.maxsize and len(self) >= self.maxsize
def get_items(self):
""" Returns queue contents as a new list.
"""
return [item for item in self.items]
class MultiPlotAnimated():
""" A multi-axes matplotlib graph - displays one or more line graphs
whith each graph using the same x axis data. The graph auto-draws at
at the given interval viaFuncAnimation w/data from user-defined func.
"""
_colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k'] # Line color codes
def __init__(self, line_count, lines_func, field_count=0, field_func=None,
interval=60, lim_x=50, lim_y=50, lock_y_lim=False,
title_txt='', legend=()):
""" Accepts:
line_count (int) : Num seperate graphs figure will house
lines_func (func) : Func returning line_count variables
field_count (int) : Num dynamic txt boxes figure contains
field_func (func) : Func returning field_count strings
interval (int) : Graph refresh interval (ms)
legend (tuple) : String labels for each line
lim_x (int) : Initial x axis limit (grows as needed)
lim_y (int) : Initial y axis limit (grows as needed)
lock_y_lim (bool) : Denotes y axis does not grow as needed
title_txt (str) : Figure title text
"""
self.figure = None # The matplot lib figure
self._lines_num = line_count # Count of lines contained in figure
self._lines_func = lines_func # Func for getting updated line data
self._fields_num = field_count # Count of dynamic txt fields in fig
self._fields_func = field_func # Func for getting updated field data
self._animation_sp = interval # Graph refresh speed (ms)
self._locked_y = lock_y_lim # Denotes fixed y axis boundry
self._animation = None # Ref to animation instance
self._paused = False # Denotes graph update is paused
# Containers
self._datasets = [] # Axis data sets
self._lines_txt = [] # Holds refs to "current value" txtboxes
self._fields_txt = [] # Holds refs to Figure descriotion
self._lines = [] # Holds refs to each line on the figure
self._axes = [] # Holds refs to each axes on the figure
# Set up the plot figure, dictating windows size (figsize)
if line_count > 1:
self.figure, self._axes = plt.subplots(
line_count, 1, figsize=(16, 10))
else:
self.figure, *self._axes = plt.subplots(
line_count, 1, figsize=(16, 10))
# Adjust the subplot l/r margins
plt.subplots_adjust(left=0.04, right=.95)
# Add plot header
self.figure.text(0.5, 0.91, title_txt, fontsize=18, fontweight='bold',
horizontalalignment='center')
# Init sets for each line plus 1 more (the last) for time data
self._datasets = [[] for i in range(line_count + 1)]
# Set up the figure's lines, axes, etc
color = self._next_color_gen()
for i in range(line_count):
# Lines...
axes = self._axes[i]
ln, = axes.plot([], [], lw=2, color=next(color))
self._lines.append(ln)
# Text value placeholders...
txt = axes.text(1.01, 0, '', transform=axes.transAxes)
self._lines_txt.append(txt)
# Legend...
try:
axes.legend(list(legend[i]))
except IndexError:
pass
# Set up dynamic text field placeholders
y = .01
for i in range(field_count):
txt = self.figure.text(0.01, y, '',
fontsize=15, fontname='monospace')
self._fields_txt.append(txt)
y += .025
# Set initial axes bounds
for ax in self._axes:
ax.set_xlim(0, lim_x)
ax.set_ylim(0, lim_y)
ax.grid()
def _get_x_dataset(self):
""" An internal helper function for getting the shared/x axis dataset.
"""
return self._datasets[self._lines_num]
def _update_graph(self, frame):
""" Function to refresh graph data.
"""
# Refresh data if not paused, else just return the current lines
if not self._paused:
# Refresh line data...
line_data = self._lines_func() # User specified function
# Get the last line data element (it's the shared x axis data)
x_dataset = self._get_x_dataset()
x_dataset.append(line_data[self._lines_num])
new_xval = line_data[len(line_data) - 1]
# Iterate all but last data set to update line properties
for i in range(self._lines_num):
d = line_data[i]
axes = self._axes[i]
self._datasets[i].append(d)
self._lines[i].set_data(x_dataset, self._datasets[i])
self._lines_txt[i].set_text('%.2f' % d)
# Grow this y axis if needed
if not self._locked_y:
ymin, ymax = axes.get_ylim()
if d >= ymax:
axes.set_ylim(ymin, 1.5 * ymax)
axes.figure.canvas.draw()
# Grow all x axis' if needed (if needed by one, needed by all)
xmin, xmax = self._axes[0].get_xlim()
if new_xval >= xmax:
for ax in self._axes:
ax.set_xlim(xmin, 1.25 * xmax)
ax.figure.canvas.draw()
# Refresh field data...
if self._fields_func:
field_data = self._fields_func() # User specified function
for i in range(self._fields_num):
self._fields_txt[i].set_text(str(field_data[i]))
return self._lines
def _next_color_gen(self):
""" A generator returning the next matplotlib color code (ex: 'b').
"""
idx = -1
ubound = len(self._colors)
while True:
idx += 1
if idx >= ubound:
idx = -1
yield self._colors[idx]
def annotate(self, s):
""" Adds an annotation across all plots at the current x location.
"""
x_data = self._get_x_dataset()
# Ensure populated
x = 0
if x_data:
x = x_data[len(x_data) - 1]
# Annotate every axis at (x, 0.01)
for ax in self._axes:
ax.text(x, 0, s)
# TODO: Verticle line on each graph
def play(self):
""" Plays/Resumes graph animation, starting the animation if needed.
"""
self._paused = False
# Init animation if needed
if not self._animation:
self._animation = FuncAnimation(self.figure,
self._update_graph,
interval=self._animation_sp)
def pause(self):
""" Pauses the graph animation if it is running.
"""
# Avoid sticky-pause confusion
if self._animation:
self._paused = True
self.annotate('<-paused')
def stop(self):
""" Closes the graph windows.
"""
plt.close()
def show(self):
""" Shows the animated graph window. Should be called after a play().
"""
plt.show()
################
# Function Lib #
################
def negate(x):
""" Returns a negated version of the given string or digit.
"""
# String negate
if x.islower():
return x.upper()
elif x.isupper():
return x.lower()
# Numerical negate
return (x * -1)
###############
# Graph Debug #
###############
# t = 1
# dt = 2
# a = 10
# b = 20
# c = 30
# def simline():
# global t, a, b, c
# t += dt
# c += 1
# return a, b, c, t
# def simtxt():
# global t
# return 'a' + str(t), 'b', 'c'
# legend = ('x', 'y', 'z')
# # plot = MultiPlotAnimated(3, simline, interval=10,
# # legend=legend, title_txt='testg')
# plot = MultiPlotAnimated(3, simline, 3, simtxt, interval=10,
# legend=legend, title_txt='testg')
# plot.play()
# print('Running... ', sep=' ')
# plot.annotate('t')
# plot.show()
# print('Done.')