def test_intermediate_inputs(self): """ restrict blocks with inputs which are intermediates """ block = Block('c = a + b\n'\ 'd = c * 3') sub_block = block.restrict(inputs=('c')) self.assertEqual(sub_block.inputs, set(['c'])) self.assertEqual(sub_block.outputs, set(['d'])) context = {'a':1, 'b':2} block.execute(context) self.assertEqual(context['c'], 3) self.assertEqual(context['d'], 9) context = {'c':10} sub_block.execute(context) self.assertEqual(context['c'], 10) self.assertEqual(context['d'], 30) context = {'d':15} sub_block = block.restrict(inputs=('d')) self.assertEqual(sub_block.inputs, set([])) self.assertEqual(sub_block.outputs, set([])) sub_block.execute(context) self.assertEqual(context['d'], 15)
def test_intermediate_inputs(self): """ restrict blocks with inputs which are intermediates """ block = Block('c = a + b\n'\ 'd = c * 3') sub_block = block.restrict(inputs=('c')) self.assertEqual(sub_block.inputs, set(['c'])) self.assertEqual(sub_block.outputs, set(['d'])) context = {'a': 1, 'b': 2} block.execute(context) self.assertEqual(context['c'], 3) self.assertEqual(context['d'], 9) context = {'c': 10} sub_block.execute(context) self.assertEqual(context['c'], 10) self.assertEqual(context['d'], 30) context = {'d': 15} sub_block = block.restrict(inputs=('d')) self.assertEqual(sub_block.inputs, set([])) self.assertEqual(sub_block.outputs, set([])) sub_block.execute(context) self.assertEqual(context['d'], 15)
def test_heirarchical_inputs(self): """ restrict blocks with inputs which are nested """ code = "from numpy import arange\n" \ "x = arange(0, 10, 0.1)\n" \ "c1 = inner.a * inner.a\n" \ "x1 = x * x\n" \ "t1 = c1 *x1\n" \ "t2 = inner.b * x\n" \ "t3 = t1 + t2\n" \ "y = t3 + inner.c\n" block = Block(code) sub_block = block.restrict(inputs=['inner.a']) self.assertEqual(sub_block.inputs, set(['inner.a', 'inner.c', 'x1', 't2'])) self.assertEqual(sub_block.outputs, set(['y', 'c1', 't3', 't1'])) self.assertEqual(len(sub_block.sub_blocks), 5) self.assertSimilar(sub_block.sub_blocks[0], Block('from numpy import arange')) self.assertSimilar(sub_block.sub_blocks[1], Block('c1 = inner.a * inner.a')) self.assertSimilar(sub_block.sub_blocks[2], Block('t1 = c1 *x1')) self.assertSimilar(sub_block.sub_blocks[3], Block('t3 = t1 + t2')) self.assertSimilar(sub_block.sub_blocks[4], Block('y = t3 + inner.c')) code = "from numpy import arange\n" \ "x = arange(0, 10, 0.1)\n" \ "c1 = outter.a * outter.a\n" \ "x1 = x * x\n" \ "t1 = c1 *x1\n" \ "t2 = outter.inner.b * x\n" \ "t3 = t1 + t2\n" \ "y = t3 + outter.inner.c\n" block = Block(code) sub_block = block.restrict(inputs=['outter.a']) self.assertEqual(sub_block.inputs, set(['outter.a', 'outter.inner.c', 'x1', 't2'])) self.assertEqual(sub_block.outputs, set(['y', 'c1', 't3', 't1'])) self.assertEqual(len(sub_block.sub_blocks), 5) self.assertSimilar(sub_block.sub_blocks[0], Block('from numpy import arange')) self.assertSimilar(sub_block.sub_blocks[1], Block('c1 = outter.a * outter.a')) self.assertSimilar(sub_block.sub_blocks[2], Block('t1 = c1 *x1')) self.assertSimilar(sub_block.sub_blocks[3], Block('t3 = t1 + t2')) self.assertSimilar(sub_block.sub_blocks[4], Block('y = t3 + outter.inner.c'))
def test_intermediate_inputs_and_outputs(self): """ restrict blocks with inputs and outputs which are intermediates """ code = "c = a + b\n" \ "d = c * 3\n" \ "e = a * c\n" \ "f = d + e\n" \ "g = e + c\n" \ "h = a * 3" block = Block(code) sub_block = block.restrict(inputs=('c'), outputs=('e', 'g')) self.assertEqual(sub_block.inputs, set(['a', 'c'])) self.assertEqual(sub_block.outputs, set(['e', 'g'])) context = {'a': 1, 'b': 2} block.execute(context) self.assertEqual(context['c'], 3) self.assertEqual(context['e'], 3) self.assertEqual(context['g'], 6) context = {'a': 1, 'c': 10} sub_block.execute(context) self.assertEqual(context['c'], 10) self.assertEqual(context['e'], 10) self.assertEqual(context['g'], 20)
def test_intermediate_inputs_with_highly_connected_graph(self): """ restrict blocks with inputs which are intermediates on a highly connected graph""" code = "c = a + b\n" \ "d = c * 3\n" \ "e = a * c\n" \ "f = d + e\n" \ "g = e + c\n" \ "h = a * 3" block = Block(code) sub_block = block.restrict(inputs=('c')) self.assertEqual(sub_block.inputs, set(['a', 'c'])) self.assertEqual(sub_block.outputs, set(['d', 'e', 'f', 'g'])) context = {'a': 1, 'b': 2} block.execute(context) self.assertEqual(context['c'], 3) self.assertEqual(context['d'], 9) self.assertEqual(context['e'], 3) self.assertEqual(context['f'], 12) self.assertEqual(context['g'], 6) context = {'a': 1, 'c': 10} sub_block.execute(context) self.assertEqual(context['c'], 10) self.assertEqual(context['d'], 30) self.assertEqual(context['e'], 10) self.assertEqual(context['f'], 40) self.assertEqual(context['g'], 20)
def test_intermediate_inputs_and_outputs(self): """ restrict blocks with inputs and outputs which are intermediates """ code = "c = a + b\n" \ "d = c * 3\n" \ "e = a * c\n" \ "f = d + e\n" \ "g = e + c\n" \ "h = a * 3" block = Block(code) sub_block = block.restrict(inputs=('c'), outputs=('e', 'g')) self.assertEqual(sub_block.inputs, set(['a', 'c'])) self.assertEqual(sub_block.outputs, set(['e', 'g'])) context = {'a':1, 'b':2} block.execute(context) self.assertEqual(context['c'], 3) self.assertEqual(context['e'], 3) self.assertEqual(context['g'], 6) context = {'a':1, 'c':10} sub_block.execute(context) self.assertEqual(context['c'], 10) self.assertEqual(context['e'], 10) self.assertEqual(context['g'], 20)
def test_intermediate_inputs_with_highly_connected_graph(self): """ restrict blocks with inputs which are intermediates on a highly connected graph""" code = "c = a + b\n" \ "d = c * 3\n" \ "e = a * c\n" \ "f = d + e\n" \ "g = e + c\n" \ "h = a * 3" block = Block(code) sub_block = block.restrict(inputs=('c')) self.assertEqual(sub_block.inputs, set(['a', 'c'])) self.assertEqual(sub_block.outputs, set(['d', 'e', 'f', 'g'])) context = {'a':1, 'b':2} block.execute(context) self.assertEqual(context['c'], 3) self.assertEqual(context['d'], 9) self.assertEqual(context['e'], 3) self.assertEqual(context['f'], 12) self.assertEqual(context['g'], 6) context = {'a':1, 'c':10} sub_block.execute(context) self.assertEqual(context['c'], 10) self.assertEqual(context['d'], 30) self.assertEqual(context['e'], 10) self.assertEqual(context['f'], 40) self.assertEqual(context['g'], 20)
def test_inputs_are_dependent_outputs(self): """ restrict blocks with inputs which are intermediates and outputs""" code = "t2 = b * 2\n" \ "t3 = t2 + 3\n" block = Block(code) sub_block = block.restrict(inputs=['t2', 't3'])
def test_inputs_are_imports(self): """ restrict blocks with inputs which are imported """ code = "from numpy import arange\n" \ "x = arange(0, 10, 0.1)\n" \ "c1 = a * a\n" \ "x1 = x * x\n" \ "t1 = c1 *x1\n" \ "t2 = b * x\n" \ "t3 = t1 + t2\n" \ "y = t3 + c\n" block = Block(code) sub_block = block.restrict(inputs=['arange']) self.assertEqual(sub_block.inputs, set(['c1', 'b', 'c'])) self.assertEqual(sub_block.outputs, set(['x', 'x1', 't1', 't2', 't3', 'y']))