@@ -50,9 +50,9 @@ def filtdict(d, *keys):
5050 return type (d )(i for i in d .items () if i [0 ] in keys )
5151
5252
53- def addall (* a ):
53+ def addall (* a , ** kw ):
5454 "Same as a + b + ...."
55- return sum (a )
55+ return sum (a ) + sum ( kw . values ())
5656
5757
5858def abspow (a , p ):
@@ -321,13 +321,48 @@ def test_network_merge_in_doctests():
321321 assert merged_graph .provides
322322
323323 assert (
324- repr (merged_graph ) ==
325- "NetworkOperation('merged_graph', "
324+ repr (merged_graph ) == "NetworkOperation('merged_graph', "
326325 "needs=['a', 'b', 'ab', 'a_minus_ab', 'c'], "
327326 "provides=['ab', 'a_minus_ab', 'abs_a_minus_ab_cubed', 'cab'], x4ops)"
328327 )
329328
330329
330+ @pytest .fixture
331+ def samplenet ():
332+ # Set up a network such that we don't need to provide a or b d if we only
333+ # request sum3 as output and if we provide sum2.
334+ sum_op1 = operation (name = "sum_op1" , needs = ["a" , "b" ], provides = "sum1" )(add )
335+ sum_op2 = operation (name = "sum_op2" , needs = ["c" , "d" ], provides = "sum2" )(add )
336+ sum_op3 = operation (name = "sum_op3" , needs = ["c" , "sum2" ], provides = "sum3" )(add )
337+ return compose ("test_net" , sum_op1 , sum_op2 , sum_op3 )
338+
339+
340+ def test_node_props_based_prune ():
341+ netop = compose (
342+ "N" ,
343+ operation (name = "A" , needs = ["a" ], provides = ["aa" ], node_props = {"color" : "red" })(
344+ identity
345+ ),
346+ operation (
347+ name = "B" , needs = ["b" ], provides = ["bb" ], node_props = {"color" : "green" }
348+ )(identity ),
349+ operation (name = "C" , needs = ["c" ], provides = ["cc" ])(identity ),
350+ operation (
351+ name = "SUM" ,
352+ needs = [optional (i ) for i in ("aa" , "bb" , "cc" )],
353+ provides = ["sum" ],
354+ )(addall ),
355+ )
356+ inp = {"a" : 1 , "b" : 2 , "c" : 3 }
357+ # assert netop(**inp)["sum"] == 6
358+
359+ pred = lambda n , d : d .get ("color" , None ) != "red"
360+ assert netop .narrow (predicate = pred )(** inp )["sum" ] == 5
361+
362+ pred = lambda n , d : "color" not in d
363+ assert netop .narrow (predicate = pred )(** inp )["sum" ] == 3
364+
365+
331366def test_input_based_pruning ():
332367 # Tests to make sure we don't need to pass graph inputs if we're provided
333368 # with data further downstream in the graph as an input.
@@ -349,72 +384,51 @@ def test_input_based_pruning():
349384 assert results ["sum3" ] == add (sum1 , sum2 )
350385
351386
352- def test_output_based_pruning ():
387+ def test_output_based_pruning (samplenet ):
353388 # Tests to make sure we don't need to pass graph inputs if they're not
354389 # needed to compute the requested outputs.
355390
356391 c = 2
357392 d = 3
358393
359- # Set up a network such that we don't need to provide a or b if we only
360- # request sum3 as output.
361- sum_op1 = operation (name = "sum_op1" , needs = ["a" , "b" ], provides = "sum1" )(add )
362- sum_op2 = operation (name = "sum_op2" , needs = ["c" , "d" ], provides = "sum2" )(add )
363- sum_op3 = operation (name = "sum_op3" , needs = ["c" , "sum2" ], provides = "sum3" )(add )
364- net = compose ("test_net" , sum_op1 , sum_op2 , sum_op3 )
365-
366- results = net .compute ({"a" : 0 , "b" : 0 , "c" : c , "d" : d }, ["sum3" ])
394+ results = samplenet .compute ({"a" : 0 , "b" : 0 , "c" : c , "d" : d }, ["sum3" ])
367395
368396 # Make sure we got expected result without having to pass a or b.
369397 assert "sum3" in results
370398 assert results ["sum3" ] == add (c , add (c , d ))
371399
372400
373- def test_deps_pruning_vs_narrowing ():
401+ def test_deps_pruning_vs_narrowing (samplenet ):
374402 # Tests to make sure we don't need to pass graph inputs if they're not
375403 # needed to compute the requested outputs or of we're provided with
376404 # inputs that are further downstream in the graph.
377405
378406 c = 2
379407 sum2 = 5
380408
381- # Set up a network such that we don't need to provide a or b d if we only
382- # request sum3 as output and if we provide sum2.
383- sum_op1 = operation (name = "sum_op1" , needs = ["a" , "b" ], provides = "sum1" )(add )
384- sum_op2 = operation (name = "sum_op2" , needs = ["c" , "d" ], provides = "sum2" )(add )
385- sum_op3 = operation (name = "sum_op3" , needs = ["c" , "sum2" ], provides = "sum3" )(add )
386- net = compose ("test_net" , sum_op1 , sum_op2 , sum_op3 )
387-
388- results = net .compute ({"c" : c , "sum2" : sum2 }, ["sum3" ])
409+ results = samplenet .compute ({"c" : c , "sum2" : sum2 }, ["sum3" ])
389410
390411 # Make sure we got expected result without having to pass a, b, or d.
391412 assert "sum3" in results
392413 assert results ["sum3" ] == add (c , sum2 )
393414
394415 # Compare with both `narrow()`.
395- net = net .narrow (inputs = ["c" , "sum2" ], outputs = ["sum3" ])
416+ net = samplenet .narrow (inputs = ["c" , "sum2" ], outputs = ["sum3" ])
396417 results = net (c = c , sum2 = sum2 )
397418
398419 # Make sure we got expected result without having to pass a, b, or d.
399420 assert "sum3" in results
400421 assert results ["sum3" ] == add (c , sum2 )
401422
402423
403- def test_pruning_raises_for_bad_output ():
424+ def test_pruning_raises_for_bad_output (samplenet ):
404425 # Make sure we get a ValueError during the pruning step if we request an
405426 # output that doesn't exist.
406427
407- # Set up a network that doesn't have the output sum4, which we'll request
408- # later.
409- sum_op1 = operation (name = "sum_op1" , needs = ["a" , "b" ], provides = "sum1" )(add )
410- sum_op2 = operation (name = "sum_op2" , needs = ["c" , "d" ], provides = "sum2" )(add )
411- sum_op3 = operation (name = "sum_op3" , needs = ["c" , "sum2" ], provides = "sum3" )(add )
412- net = compose ("test_net" , sum_op1 , sum_op2 , sum_op3 )
413-
414428 # Request two outputs we can compute and one we can't compute. Assert
415429 # that this raises a ValueError.
416430 with pytest .raises (ValueError ) as exinfo :
417- net .compute ({"a" : 1 , "b" : 2 , "c" : 3 , "d" : 4 }, ["sum1" , "sum3" , "sum4" ])
431+ samplenet .compute ({"a" : 1 , "b" : 2 , "c" : 3 , "d" : 4 }, ["sum1" , "sum3" , "sum4" ])
418432 assert exinfo .match ("sum4" )
419433
420434
0 commit comments