-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRaportMaker.py
189 lines (146 loc) · 5.67 KB
/
RaportMaker.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
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 13 10:35:30 2015
@author: mdzikowski
"""
import functools
import types
from matplotlib.pyplot import *
#==============================================================================
# DECORATOR FOR HELPER FUNCTIONS
#==============================================================================
def finalize(func):
def fin(self, *args, **kwargs):
func(self, *args, **kwargs)
if not self.dont_show and not args[0] == '--internal--':
self.originals[args[0]](*(args[1:]), **kwargs)
return fin
def info(func):
def fin(self, *args, **kwargs):
print "INTERCEPTED: ", args[0]
func(self, *args, **kwargs)
return fin
def shift_plots(func):
def fin(self, *args, **kwargs):
if not self.currentPlot == {} and not self.currentPlot == 0:
self.plots.append(self.currentPlot)
self.currentPlot = {}
func(self, *args, **kwargs)
return fin
#==============================================================================
# MAIN INTERCEPTION CLASS
#==============================================================================
class RaportMaker(object):
def __init__(self, **kargs):
if kargs.has_key('dont_show') and kargs['dont_show']:
self.dont_show = True
else:
self.dont_show = False
self.originals = {}
self.currentPlot = 0
self.plots = []
self.figures = {}
self.path = '/dev/null'
self.fid = 0
@info
@shift_plots
@finalize
def record(self, name, *args, **kwargs):
self.currentPlot['fun_name'] = name
self.currentPlot['args'] = args
self.currentPlot['kwargs'] = kwargs
#==============================================================================
# @info
# @finalize
# def modify(self, name, *args, **kwargs):
# self.currentPlot['fun_name'] = name
# self.currentPlot['args'] = args
# self.currentPlot['kwargs'] = kwargs
#==============================================================================
@info
@shift_plots
@finalize
def store(self, name, *args, **kwargs):
if not self.plots == {}:
self.new_figure('--internal--')
np.savez(self.path, **self.figures)
@info
@shift_plots
@finalize
def new_figure(self, *args, **kwargs):
if len(self.plots) > 0:
if len(args) > 1 and isinstance(args[1], str):
self.figures[args[0]] = self.plots
else:
self.fid = self.fid + 1
self.figures[str(self.fid)] = self.plots
self.plots = []
def RecordingDecorator(self, func):
self.originals[func.func_name] = func
class _wrapper:
def __init__(self, parent, func):
self.parent = parent
self.func = func
@functools.wraps(func)
def __call__(self, *args, **kwargs):
self.parent.record(func.func_name, *args,**kwargs)
return _wrapper(self, func)
#==============================================================================
# def ModifingDecorator(self, func):
# self.originals[func.func_name] = func
# class _wrapper:
# def __init__(self, parent, func):
# self.parent = parent
# self.func = func
#
# @functools.wraps(func)
# def __call__(self, *args, **kwargs):
# self.parent.create(func.func_name, *args,**kwargs)
#
# return _wrapper(self, func)
#==============================================================================
def ShowDecorator(self, func):
self.originals[func.func_name] = func
class _wrapper:
def __init__(self, parent, func):
self.parent = parent
self.func = func
@functools.wraps(func)
def __call__(self, *args, **kwargs):
self.parent.store(func.func_name, *args,**kwargs)
return _wrapper(self, func)
def FigureDecorator(self, func):
self.originals[func.func_name] = func
class _wrapper:
def __init__(self, parent, func):
self.parent = parent
self.func = func
@functools.wraps(func)
def __call__(self, *args, **kwargs):
self.parent.new_figure(func.func_name, *args,**kwargs)
return _wrapper(self, func)
#==============================================================================
# def NotDecorated(func):
# def f(*args, **kwargs):
# print "NOT INTERCEPTED: ", func.func_name
# return func(*args, **kwargs)
# return f
#==============================================================================
raport = RaportMaker()
for k,v in vars(matplotlib.pyplot).items():
if isinstance(v, types.FunctionType):
globals()[k] = raport.RecordingDecorator(v)
#==============================================================================
# PLOTTING_FUNCTIONS = [
# 'plot',
# 'semilogx',
# 'semilogy',
# 'loglog'
# ]
#
#
# for name in PLOTTING_FUNCTIONS:
# globals()[name] = raport.PlottingDecorator(getattr(matplotlib.pyplot, name))
#==============================================================================
globals()['show'] = raport.ShowDecorator(getattr(matplotlib.pyplot, 'show'))
globals()['figure'] = raport.FigureDecorator(getattr(matplotlib.pyplot, 'figure'))