/
helpers_state.py
627 lines (558 loc) · 21.5 KB
/
helpers_state.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
"""Additional functions used mostly by the State class."""
import attr
import itertools
from copy import deepcopy
import logging
from .helpers import ensure_list
logger = logging.getLogger("pydra")
class PydraStateError(Exception):
"""Custom error for Pydra State"""
def __init__(self, value):
self.value = value
def __str__(self):
return str(self.value)
def splitter2rpn(splitter, other_states=None, state_fields=True):
"""
Translate user-provided splitter into *reverse polish notation*.
The reverse polish notation is imposed by :class:`~pydra.engine.state.State`.
Parameters
----------
splitter :
splitter (standard form)
other_states :
other states that are connected to the state
state_fields : :obj:`bool`
if False the splitter from the previous states are unwrapped
"""
if not splitter:
return []
output_splitter = []
_ordering(
deepcopy(splitter),
i=0,
output_splitter=output_splitter,
other_states=deepcopy(other_states),
state_fields=state_fields,
)
return output_splitter
def _ordering(
el, i, output_splitter, current_sign=None, other_states=None, state_fields=True
):
"""Get a proper order of fields and signs (used by splitter2rpn)."""
if type(el) is tuple:
# checking if the splitter dont contain splitter from previous nodes
# i.e. has str "_NA", etc.
if len(el) == 1:
# treats .split(("x",)) like .split("x")
el = el[0]
_ordering(el, i, output_splitter, current_sign, other_states, state_fields)
else:
if type(el[0]) is str and el[0].startswith("_"):
node_nm = el[0][1:]
if node_nm not in other_states and state_fields:
raise PydraStateError(
"can't ask for splitter from {}, other nodes that are connected: {}".format(
node_nm, other_states.keys()
)
)
elif state_fields:
splitter_mod = add_name_splitter(
splitter=other_states[node_nm][0].splitter_final, name=node_nm
)
el = (splitter_mod, el[1])
if other_states[node_nm][0].other_states:
other_states.update(other_states[node_nm][0].other_states)
if type(el[1]) is str and el[1].startswith("_"):
node_nm = el[1][1:]
if node_nm not in other_states and state_fields:
raise PydraStateError(
"can't ask for splitter from {}, other nodes that are connected: {}".format(
node_nm, other_states.keys()
)
)
elif state_fields:
splitter_mod = add_name_splitter(
splitter=other_states[node_nm][0].splitter_final, name=node_nm
)
el = (el[0], splitter_mod)
if other_states[node_nm][0].other_states:
other_states.update(other_states[node_nm][0].other_states)
_iterate_list(
el,
".",
other_states,
output_splitter=output_splitter,
state_fields=state_fields,
)
elif type(el) is list:
if len(el) == 1:
# treats .split(["x"]) like .split("x")
el = el[0]
_ordering(el, i, output_splitter, current_sign, other_states, state_fields)
else:
if type(el[0]) is str and el[0].startswith("_"):
node_nm = el[0][1:]
if node_nm not in other_states and state_fields:
raise PydraStateError(
"can't ask for splitter from {}, other nodes that are connected: {}".format(
node_nm, other_states.keys()
)
)
elif state_fields:
splitter_mod = add_name_splitter(
splitter=other_states[node_nm][0].splitter_final, name=node_nm
)
el[0] = splitter_mod
if other_states[node_nm][0].other_states:
other_states.update(other_states[node_nm][0].other_states)
if type(el[1]) is str and el[1].startswith("_"):
node_nm = el[1][1:]
if node_nm not in other_states and state_fields:
raise PydraStateError(
"can't ask for splitter from {}, other nodes that are connected: {}".format(
node_nm, other_states.keys()
)
)
elif state_fields:
splitter_mod = add_name_splitter(
splitter=other_states[node_nm][0].splitter_final, name=node_nm
)
el[1] = splitter_mod
if other_states[node_nm][0].other_states:
other_states.update(other_states[node_nm][0].other_states)
_iterate_list(
el,
"*",
other_states,
output_splitter=output_splitter,
state_fields=state_fields,
)
elif type(el) is str:
if el.startswith("_"):
node_nm = el[1:]
if node_nm not in other_states and state_fields:
raise PydraStateError(
"can't ask for splitter from {}, other nodes that are connected: {}".format(
node_nm, other_states.keys()
)
)
elif state_fields:
splitter_mod = add_name_splitter(
splitter=other_states[node_nm][0].splitter_final, name=node_nm
)
el = splitter_mod
if other_states[node_nm][0].other_states:
other_states.update(other_states[node_nm][0].other_states)
if type(el) is str:
output_splitter.append(el)
elif type(el) is tuple:
_iterate_list(
el,
".",
other_states,
output_splitter=output_splitter,
state_fields=state_fields,
)
elif type(el) is list:
_iterate_list(
el,
"*",
other_states,
output_splitter=output_splitter,
state_fields=state_fields,
)
else:
raise PydraStateError("splitter has to be a string, a tuple or a list")
if i > 0:
output_splitter.append(current_sign)
def _iterate_list(element, sign, other_states, output_splitter, state_fields=True):
"""Iterate over list (used in the splitter2rpn to get recursion)."""
for i, el in enumerate(element):
_ordering(
deepcopy(el),
i,
current_sign=sign,
other_states=other_states,
output_splitter=output_splitter,
state_fields=state_fields,
)
def converter_groups_to_input(group_for_inputs):
"""
Return fields for each axis and number of all groups.
Requires having axes for all the input fields.
Parameters
----------
group_for_inputs :
specified axes (groups) for each input
"""
input_for_axis = {}
ngr = 0
for inp, grs in group_for_inputs.items():
for gr in ensure_list(grs):
if gr in input_for_axis.keys():
input_for_axis[gr].append(inp)
else:
ngr += 1
input_for_axis[gr] = [inp]
return input_for_axis, ngr
def remove_inp_from_splitter_rpn(splitter_rpn, inputs_to_remove):
"""
Remove inputs due to combining.
Mutates a splitter.
Parameters
----------
splitter_rpn :
The splitter in reverse polish notation
inputs_to_remove :
input names that should be removed from the splitter
"""
splitter_rpn_copy = splitter_rpn.copy()
# reverting order
splitter_rpn_copy.reverse()
stack_inp = []
stack_sgn = []
from_last_sign = []
for (ii, el) in enumerate(splitter_rpn_copy):
# element is a sign
if el == "." or el == "*":
stack_sgn.append((ii, el))
from_last_sign.append(0)
# it's an input but not to remove
elif el not in inputs_to_remove:
if from_last_sign:
from_last_sign[-1] += 1
stack_inp.append((ii, el))
# it'a an input that should be removed
else:
if not from_last_sign:
pass
elif from_last_sign[-1] <= 1:
stack_sgn.pop()
from_last_sign.pop()
else:
stack_sgn.pop(-1 * from_last_sign.pop())
# creating the final splitter_rpn after combining
remaining_elements = stack_sgn + stack_inp
remaining_elements.sort(reverse=True)
splitter_rpn_combined = [el for (i, el) in remaining_elements]
return splitter_rpn_combined
def rpn2splitter(splitter_rpn):
"""
Convert from splitter_rpn to splitter.
Recurrent algorithm to perform the conversion.
Every time combines pairs of input in one input,
ends when the length is one.
Parameters
----------
splitter_rpn :
splitter in reverse polish notation
Returns
-------
splitter :
splitter in the standard/original form
"""
if splitter_rpn == []:
return None
if len(splitter_rpn) == 1:
return splitter_rpn[0]
splitter_rpn_copy = splitter_rpn.copy()
signs = [".", "*"]
splitter_modified = []
while splitter_rpn_copy:
el = splitter_rpn_copy.pop()
# element is a sign
if el in signs:
if (
splitter_rpn_copy[-1] not in signs
and splitter_rpn_copy[-2] not in signs
):
right, left = splitter_rpn_copy.pop(), splitter_rpn_copy.pop()
if el == ".":
splitter_modified.append((left, right))
elif el == "*":
splitter_modified.append([left, right])
else:
splitter_modified.append(el)
else:
splitter_modified.append(el)
# reversing the list and combining more
splitter_modified.reverse()
return rpn2splitter(splitter_modified)
def add_name_combiner(combiner, name):
"""adding a node's name to each field from the combiner"""
combiner_changed = []
for comb in combiner:
if "." not in comb:
combiner_changed.append(f"{name}.{comb}")
else:
combiner_changed.append(comb)
return combiner_changed
def add_name_splitter(splitter, name):
"""adding a node's name to each field from the splitter"""
if isinstance(splitter, str):
return _add_name([splitter], name)[0]
elif isinstance(splitter, list):
return _add_name(splitter, name)
elif isinstance(splitter, tuple):
splitter_l = list(splitter)
return tuple(_add_name(splitter_l, name))
def _add_name(mlist, name):
"""adding anem to each element from the list"""
for i, elem in enumerate(mlist):
if isinstance(elem, str):
if "." in elem or elem.startswith("_"):
pass
else:
mlist[i] = "{}.{}".format(name, mlist[i])
elif isinstance(elem, list):
mlist[i] = _add_name(elem, name)
elif isinstance(elem, tuple):
mlist[i] = list(elem)
mlist[i] = _add_name(mlist[i], name)
mlist[i] = tuple(mlist[i])
return mlist
def flatten(vals, cur_depth=0, max_depth=None):
"""Flatten a list of values."""
if max_depth is None:
max_depth = len(list(input_shape(vals)))
values = []
if cur_depth >= max_depth:
values.append([vals])
else:
for val in vals:
if isinstance(val, (list, tuple)):
values.append(flatten(val, cur_depth + 1, max_depth))
else:
values.append([val])
return itertools.chain.from_iterable(values)
def iter_splits(iterable, keys):
"""Generate splits."""
for iter in list(iterable):
yield dict(zip(keys, list(flatten(iter, max_depth=1000))))
def input_shape(inp, cont_dim=1):
"""Get input shape, depends on the container dimension, if not specify it is assumed to be 1"""
# TODO: have to be changed for inner splitter (sometimes different length)
cont_dim -= 1
shape = [len(inp)]
last_shape = None
for value in inp:
if isinstance(value, list) and cont_dim > 0:
cur_shape = input_shape(value, cont_dim)
if last_shape is None:
last_shape = cur_shape
elif last_shape != cur_shape:
last_shape = None
break
else:
last_shape = None
break
if last_shape is not None:
shape.extend(last_shape)
return tuple(shape)
def splits_groups(splitter_rpn, combiner=None, inner_inputs=None):
"""splits inputs to groups (axes) and creates stacks for these groups
This is used to specify which input can be combined.
"""
if not splitter_rpn:
return [], {}, [], []
stack = []
keys = []
groups = {}
group_count = None
if not combiner:
combiner = []
if inner_inputs:
previous_states_ind = {
f"_{v.name}": v.keys_final for v in inner_inputs.values()
}
inner_inputs = {k: v for k, v in inner_inputs.items() if k in splitter_rpn}
else:
previous_states_ind = {}
inner_inputs = {}
# when splitter is a single element (no operators)
if len(splitter_rpn) == 1:
op_single = splitter_rpn[0]
return _single_op_splits_groups(op_single, combiner, inner_inputs, groups)
# len(splitter_rpn) > 1
# iterating splitter_rpn
for token in splitter_rpn:
if token in [".", "*"]:
terms = {}
terms["R"] = stack.pop()
terms["L"] = stack.pop()
# checking if opL/R are strings
trm_str = {"L": False, "R": False}
oldgroups = {}
for lr in ["L", "R"]:
if isinstance(terms[lr], str):
trm_str[lr] = True
else:
oldgroups[lr] = terms[lr]
if token == ".":
if all(trm_str.values()):
if group_count is None:
group_count = 0
else:
group_count += 1
oldgroup = groups[terms["L"]] = groups[terms["R"]] = group_count
elif trm_str["R"]:
groups[terms["R"]] = oldgroups["L"]
oldgroup = oldgroups["L"]
elif trm_str["L"]:
groups[terms["L"]] = oldgroups["R"]
oldgroup = oldgroups["R"]
else:
if len(ensure_list(oldgroups["L"])) != len(
ensure_list(oldgroups["R"])
):
raise ValueError(
"Operands do not have same shape "
"(left one is {}d and right one is {}d.".format(
len(ensure_list(oldgroups["L"])),
len(ensure_list(oldgroups["R"])),
)
)
oldgroup = oldgroups["L"]
# dj: changing axes for Right part of the scalar op.
for k, v in groups.items():
if v in ensure_list(oldgroups["R"]):
groups[k] = ensure_list(oldgroups["L"])[
ensure_list(oldgroups["R"]).index(v)
]
else: # if token == "*":
if all(trm_str.values()):
if group_count is None:
group_count = 0
else:
group_count += 1
groups[terms["L"]] = group_count
group_count += 1
groups[terms["R"]] = group_count
oldgroup = [groups[terms["L"]], groups[terms["R"]]]
elif trm_str["R"]:
group_count += 1
groups[terms["R"]] = group_count
oldgroup = ensure_list(oldgroups["L"]) + [groups[terms["R"]]]
elif trm_str["L"]:
group_count += 1
groups[terms["L"]] = group_count
oldgroup = [groups[terms["L"]]] + ensure_list(oldgroups["R"])
else:
oldgroup = ensure_list(oldgroups["L"]) + ensure_list(oldgroups["R"])
# creating list of keys
if trm_str["L"]:
if terms["L"].startswith("_"):
keys = previous_states_ind[terms["L"]] + keys
else:
keys.insert(0, terms["L"])
if trm_str["R"]:
if terms["R"].startswith("_"):
keys += previous_states_ind[terms["R"]]
else:
keys.append(terms["R"])
pushgroup = oldgroup
stack.append(pushgroup)
else: # name of one of the inputs
stack.append(token)
groups_stack = stack.pop()
if isinstance(groups_stack, int):
groups_stack = [groups_stack]
if inner_inputs:
groups_stack = [[], groups_stack]
else:
groups_stack = [groups_stack]
if combiner:
(
keys_final,
groups_final,
groups_stack_final,
combiner_all,
) = combine_final_groups(combiner, groups, groups_stack, keys)
return keys_final, groups_final, groups_stack_final, combiner_all
else:
return keys, groups, groups_stack, []
def _single_op_splits_groups(op_single, combiner, inner_inputs, groups):
"""splits_groups function if splitter is a singleton"""
if op_single in inner_inputs:
# TODO: have to be changed if differ length
# TODO: i think I don't want to add here from left part
# keys = inner_inputs[op_single].keys_final + [op_single]
keys = [op_single]
groups[op_single], groups_stack = 0, [[], [0]]
else:
keys = [op_single]
groups[op_single], groups_stack = 0, [[0]]
if combiner:
if combiner == [op_single]:
return [], {}, [], combiner
else:
# TODO: probably not needed, should be already check by st.combiner_validation
raise PydraStateError(
f"all fields from the combiner have to be in splitter_rpn: {[op_single]}, "
f"but combiner: {combiner} is set"
)
else:
return keys, groups, groups_stack, []
def combine_final_groups(combiner, groups, groups_stack, keys):
"""Combine the final groups."""
input_for_groups, _ = converter_groups_to_input(groups)
combiner_all = []
for comb in combiner:
for gr in ensure_list(groups[comb]):
combiner_all += input_for_groups[gr]
combiner_all = list(set(combiner_all))
combiner_all.sort()
# groups that were removed (so not trying to remove twice)
grs_removed = []
groups_stack_final = deepcopy(groups_stack)
for comb in combiner:
grs = groups[comb]
for gr in ensure_list(grs):
if gr in groups_stack_final[-1]:
grs_removed.append(gr)
groups_stack_final[-1].remove(gr)
elif gr in grs_removed:
pass
else:
raise PydraStateError(
"input {} not ready to combine, you have to combine {} "
"first".format(comb, groups_stack[-1])
)
groups_final = {inp: gr for (inp, gr) in groups.items() if inp not in combiner_all}
gr_final = set()
for el in groups_final.values():
gr_final.update(ensure_list(el))
gr_final = list(gr_final)
map_gr_nr = {nr: i for (i, nr) in enumerate(sorted(gr_final))}
groups_final_map = {}
for (inp, gr) in groups_final.items():
if isinstance(gr, int):
groups_final_map[inp] = map_gr_nr[gr]
elif isinstance(gr, list):
groups_final_map[inp] = [map_gr_nr[el] for el in gr]
else:
raise Exception("gr should be an int or a list, something wrong")
for i, groups_l in enumerate(groups_stack_final):
groups_stack_final[i] = [map_gr_nr[gr] for gr in groups_l]
keys_final = [key for key in keys if key not in combiner_all]
# TODO: not sure if I have to calculate and return keys, groups, groups_stack
return keys_final, groups_final_map, groups_stack_final, combiner_all
def map_splits(split_iter, inputs, cont_dim=None):
"""generate a dictionary of inputs prescribed by the splitter."""
if cont_dim is None:
cont_dim = {}
for split in split_iter:
yield {
k: list(flatten(ensure_list(inputs[k]), max_depth=cont_dim.get(k, None)))[v]
for k, v in split.items()
}
def inputs_types_to_dict(name, inputs):
"""Convert type.Inputs to dictionary."""
# dj: any better option?
input_names = [
field for field in attr.asdict(inputs, recurse=False) if field != "_func"
]
inputs_dict = {}
for field in input_names:
inputs_dict[f"{name}.{field}"] = getattr(inputs, field)
return inputs_dict