-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
286 lines (245 loc) · 9.34 KB
/
utils.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
"""
Various utlity functions to help with Data Pipeline development
"""
import datetime
import importlib
import inspect
import logging
import time
from contextlib import contextmanager
from functools import wraps
from pprint import pprint
import luigi
import networkx as nx
from luigi.parameter import _no_value
from luigi.task_register import Register
logger = logging.getLogger('luigi-interface')
def timeit(method):
@wraps(method)
def timed(*args, **kw):
ts = time.time()
result = method(*args, **kw)
te = time.time()
if 'log_time' in kw:
name = kw.get('log_name', method.__name__.upper())
kw['log_time'][name] = int((te - ts) * 1000)
else:
print('%r %2.2f ms' % (method.__name__, (te - ts) * 1000))
return result
return timed
@contextmanager
def temporary_config():
"""
Create a temporary Luigi Config
"""
config = luigi.configuration.get_config()
original_config = config.defaults()
# Yield the completed config
yield config
# Remove any config we set up
config.reload()
for k, v in original_config.items():
config[k] = v
@contextmanager
def complete_config():
"""
Create a temporary Luigi Config that has defaults for all variables
"""
def get_default(param_obj):
if isinstance(param_obj, luigi.IntParameter):
return '1'
elif isinstance(param_obj, luigi.DateParameter):
return datetime.date.today().isoformat()
elif isinstance(param_obj, luigi.Parameter):
return ''
config = luigi.configuration.get_config()
# Make sure every parameter has a default value
sections_to_remove = []
options_to_remove = []
for task_name, is_without_section, param_name, param_obj in Register.get_all_params():
if param_obj._default == _no_value:
if is_without_section:
sections_to_remove.append(task_name)
options_to_remove.append((task_name, param_name))
config.set(task_name, param_name, get_default(param_obj))
# Yield the completed config
yield
# Remove any config we set up
for section, option in options_to_remove:
config.remove_option(section, option)
for section in sections_to_remove:
config.remove_section(section)
def print_dag(task, level=0):
"""
Print the DAG for a Task
"""
def print_task(task, level):
padding = ' ' * 8 * level
print(padding + str(task))
params = task.get_params()
values = task.get_param_values(params, [], {})
if params:
print(padding + ' Parameters:')
for name, value in values:
print(padding + ' ' + name + ': ' + (str(value) or "''"))
deps = task.requires()
if deps:
print(padding + ' Requires:')
if isinstance(deps, luigi.Task):
print_dag(deps, level + 1)
elif isinstance(deps, dict):
for task in deps.values():
print_dag(task, level + 1)
elif isinstance(deps, (list, tuple)):
for task in deps:
print_dag(task, level + 1)
else:
# Remaining case: assume struct is iterable...
try:
for task in deps:
print_dag(task, level + 1)
except TypeError:
raise Exception('Cannot determine dependencies for %s' % str(deps))
if level == 0:
with complete_config():
if inspect.isclass(task):
task = task()
print_task(task, level)
else:
print_task(task, level)
def remove_outputs(task, cascade=False):
"""Remove the outputs from a Task, and possibly its parents."""
# Don't remove outputs from External Tasks
if not isinstance(task, luigi.ExternalTask):
output = task.output()
if isinstance(output, luigi.Target):
if output.exists():
logger.info('Removing Target %s from Task %s' % (str(output), str(task)))
output.remove()
elif isinstance(output, dict):
for target in output.values():
if target.exists():
logger.info('Removing Target %s from Task %s' % (str(target), str(task)))
target.remove()
elif isinstance(output, (list, tuple)):
for target in output:
if target.exists():
logger.info('Removing Target %s from Task %s' % (str(target), str(task)))
target.remove()
else:
# Remaining case: assume struct is iterable...
try:
for target in output:
if target.exists():
target.remove()
except TypeError:
raise Exception('Cannot determine target for %s' % str(output))
# Remove any upstream dependencies too
if cascade:
deps = task.requires()
if isinstance(deps, luigi.Task):
remove_outputs(deps, cascade)
elif isinstance(deps, dict):
for task in deps.values():
remove_outputs(task, cascade)
elif isinstance(deps, (list, tuple)):
for task in deps:
remove_outputs(task, cascade)
else:
# Remaining case: assume struct is iterable...
try:
for task in deps:
remove_outputs(task, cascade)
except TypeError:
raise Exception('Cannot determine dependencies for %s' % str(deps))
def run(task, **kwargs):
"""Run a Task using a flexible interface"""
with temporary_config() as config:
config.update(kwargs)
# If Task is a sting then replace it with the Class definition
if isinstance(task, str):
module, cls = task.rsplit('.', 1)
task = getattr(importlib.import_module(module), cls)
# If Task is a class then instantiate it
if inspect.isclass(task):
task = task()
assert isinstance(task, luigi.Task)
luigi.build([task])
return task.output()
def generate_dag(task, done=None):
def edge_attribs(task):
return {'href': 'http://data-lab.pages.kimetrica.com/rm/chris_pipeline.html#%s.%s' %
(task.__class__.__module__, task.__class__.__name__),
'target': "_blank"}
def merge_prior_dag(dag, done, src, task):
src = generate_dag(src, done)
done.add(str(task))
if isinstance(src, list):
dag += src
dag.append((src[-1][1], str(task), edge_attribs(task)))
else:
dag.append((src, str(task), edge_attribs(task)))
return dag
if done is None:
done = set()
deps = task.requires()
if deps and str(task) not in done:
dag=[]
if isinstance(deps, luigi.Task):
dag = merge_prior_dag(dag, done, deps, task)
else:
try:
for dep in deps:
dag = merge_prior_dag(dag, done, dep, task)
except TypeError:
for dep in deps.values():
dag = merge_prior_dag(dag, done, dep, task)
return dag
else:
return str(task)
def get_dag(task):
"""
Create a temporary Luigi Config that has defaults for all variables
Instantiates the Task DAG into a list of edge tuples containing source task, destination task, and node attributes.
Passes this to ``networkx`` ``MultiDiGraph.add_edges_from``.
Returns a :class:`nx.MultiDiGraph` containing source task, destination task, and node attributes
"""
def get_default(param_obj):
if isinstance(param_obj, luigi.IntParameter):
return '1'
elif isinstance(param_obj, luigi.DateParameter):
return datetime.date.today().isoformat()
elif isinstance(param_obj, luigi.Parameter):
return ''
config = luigi.configuration.get_config()
# Make sure every parameter has a default value
sections_to_remove = []
options_to_remove = []
for task_name, is_without_section, param_name, param_obj in Register.get_all_params():
if param_obj._default == luigi.parameter._no_value:
if is_without_section:
sections_to_remove.append(task_name)
options_to_remove.append((task_name, param_name))
config.set(task_name, param_name, get_default(param_obj))
dag = generate_dag(task)
# Remove any config we set up
for section, option in options_to_remove:
config.remove_option(section, option)
for section in sections_to_remove:
config.remove_section(section)
G = nx.MultiDiGraph()
G.add_edges_from(dag)
return G
def process_docstring(app, what, name, obj, options, lines):
if inspect.isclass(obj) and issubclass(obj, luigi.Task):
dag_G = get_dag(obj())
assert isinstance(dag_G, nx.MultiDiGraph)
AGraph = nx.nx_agraph.to_agraph(dag_G)
# AGraph uses pygraphviz, which seems to be the most flexible in terms of formatting
# and richer dot expressions, to be confirmed.
dot = AGraph.string()
logging.info(pprint(dot))
lines.extend(['',
'.. graphviz::',
'',
' ' + dot])