def test_atypical_nesting(self): results = FakeAtypicalNestingResults( unique_atypical_nesting=0, atypical_inner_results=FakeResults(1)) self.assertTrue(unnest.has_nested(results, 'unique_atypical_nesting')) self.assertTrue(unnest.has_nested(results, 'unique_core')) self.assertFalse(hasattr(results, 'unique_core')) self.assertFalse(unnest.has_nested(results, 'foo')) self.assertEqual( results.unique_atypical_nesting, unnest.get_innermost(results, 'unique_atypical_nesting')) self.assertEqual( results.unique_atypical_nesting, unnest.get_outermost(results, 'unique_atypical_nesting')) self.assertEqual(results.atypical_inner_results.unique_core, unnest.get_innermost(results, 'unique_core')) self.assertEqual(results.atypical_inner_results.unique_core, unnest.get_outermost(results, 'unique_core')) self.assertRaises(AttributeError, lambda: unnest.get_innermost(results, 'foo')) self.assertRaises(AttributeError, lambda: unnest.get_outermost(results, 'foo')) self.assertIs(unnest.get_innermost(results, 'foo', SINGLETON), SINGLETON) self.assertIs(unnest.get_outermost(results, 'foo', SINGLETON), SINGLETON)
def _process_results(self): """Process outputs to extract useful data.""" if unnest.has_nested(self.kernel, 'reducer'): reducers = unnest.get_outermost(self.kernel, 'reducer') # Finalize streaming calculations. self.reductions = nest.map_structure_up_to( reducers, lambda r, s: r.finalize(s), reducers, unnest.get_outermost(self.results, 'reduction_results'), check_types=False) # Grab useful reductions. def process_reductions(reducer, reduction): if isinstance(reducer, tracing_reducer.TracingReducer): self.all_states, self.trace = reduction nest.map_structure_up_to(reducers, process_reductions, reducers, self.reductions, check_types=False) if unnest.has_nested(self.results, 'new_step_size'): self.new_step_size = unnest.get_outermost(self.results, 'new_step_size')
def test_flat(self): results = FakeResults(0) self.assertTrue(unnest.has_nested(results, 'unique_core')) self.assertFalse(unnest.has_nested(results, 'foo')) self.assertEqual(results.unique_core, unnest.get_innermost(results, 'unique_core')) self.assertEqual(results.unique_core, unnest.get_outermost(results, 'unique_core')) self.assertRaises( AttributeError, lambda: unnest.get_innermost(results, 'foo')) self.assertRaises( AttributeError, lambda: unnest.get_outermost(results, 'foo')) self.assertIs(unnest.get_innermost(results, 'foo', SINGLETON), SINGLETON) self.assertIs(unnest.get_outermost(results, 'foo', SINGLETON), SINGLETON)
def test_deeply_nested(self): results = _build_deeply_nested(0, 1, 2, 3, 4) self.assertTrue(unnest.has_nested(results, 'unique_nesting')) self.assertTrue(unnest.has_nested(results, 'unique_atypical_nesting')) self.assertTrue(unnest.has_nested(results, 'unique_core')) self.assertFalse(hasattr(self, 'unique_core')) self.assertFalse(unnest.has_nested(results, 'foo')) self.assertEqual(unnest.get_innermost(results, 'unique_nesting'), 3) self.assertEqual(unnest.get_outermost(results, 'unique_nesting'), 0) self.assertEqual( unnest.get_innermost(results, 'unique_atypical_nesting'), 2) self.assertEqual( unnest.get_outermost(results, 'unique_atypical_nesting'), 1) self.assertEqual(unnest.get_innermost(results, 'unique_core'), 4) self.assertEqual(unnest.get_outermost(results, 'unique_core'), 4) self.assertRaises( AttributeError, lambda: unnest.get_innermost(results, 'foo')) self.assertRaises( AttributeError, lambda: unnest.get_outermost(results, 'foo')) self.assertIs(unnest.get_innermost(results, 'foo', SINGLETON), SINGLETON) self.assertIs(unnest.get_outermost(results, 'foo', SINGLETON), SINGLETON)