Example #1
0
    def _potential_paths(self):
        """
        Allowed paths between the endpoints starting upwards or downwards.

        Generates lists of index paths from the starting layer to the ending
        layer incorporating up to the maximum number of reflections. Provides
        sets of the paths that start by going upwards and by going downwards,
        respectively.

        """
        if not self.valid_ice_model:
            raise TypeError("Ice model must consist of layers of ice")
        start = self.ice.layers.index(self.ice.layer_at_depth(self.z0))
        end = self.ice.layers.index(self.ice.layer_at_depth(self.z1))
        upward_paths = []
        upward_tree = self._build_path(path=(start,), direction=-1,
                                       reflections=self.max_reflections,
                                       max_level=len(self.ice.layers)-1)
        for path in flatten(upward_tree, dont_flatten=(tuple,)):
            endpoints = [i for i, level in enumerate(path) if level==end]
            subpaths = [path[:i+1] for i in endpoints]
            upward_paths.extend(subpaths)
        downward_paths = []
        downward_tree = self._build_path(path=(start,), direction=1,
                                         reflections=self.max_reflections,
                                         max_level=len(self.ice.layers)-1)
        for path in flatten(downward_tree, dont_flatten=(tuple,)):
            endpoints = [i for i, level in enumerate(path) if level==end]
            subpaths = [path[:i+1] for i in endpoints]
            downward_paths.extend(subpaths)
        return set(upward_paths), set(downward_paths)
Example #2
0
 def test_dont_flatten(self):
     """Test that dont_flatten argument is obeyed"""
     l1 = [1, 2, (3, 4)]
     f1 = flatten(l1, dont_flatten=[tuple])
     assert list(f1) == l1
     l2 = [1, 2, (3, [4, 5]), 6]
     f2 = flatten(l2, dont_flatten=[tuple])
     assert list(f2) == l2
     l3 = [1, 2, (3, [4, 5]), 6, [7, 8]]
     f3 = flatten(l3, dont_flatten=[tuple])
     assert list(f3) == [1, 2, (3, [4, 5]), 6, 7, 8]
Example #3
0
 def test_strings_nonrecursive(self):
     """Test that strings don't cause a recursive explosion
     (by just not flattening them)"""
     l = [1, "two", 3]
     assert np.array_equal(list(flatten(l)), [1, "two", 3])
Example #4
0
 def test_varied_iterables(self):
     """Test that multiple iterable types can be flattened at once"""
     l1 = [1, 2, (3, 4), [5, 6]]
     assert np.array_equal(list(flatten(l1)), [1, 2, 3, 4, 5, 6])
     l2 = [1, [2, (3, 4), 5, [6, 7], 8]]
     assert np.array_equal(list(flatten(l2)), [1, 2, 3, 4, 5, 6, 7, 8])
Example #5
0
 def test_flatten_flattened(self):
     """Test that list containing flattened list (i.e. generator objects)
     can be flattened"""
     l = [1, 2, [3, flatten([4, [5, 6]])]]
     assert np.array_equal(list(flatten(l)), [1, 2, 3, 4, 5, 6])
Example #6
0
 def test_empty_list(self):
     """Test that flattening an empty list doesn't cause trouble"""
     assert np.array_equal(list(flatten([])), [])
Example #7
0
 def test_double_nest(self):
     """Test that a double nested layer can be flattened"""
     l1 = [1, 2, [3, [4, 5]]]
     assert np.array_equal(list(flatten(l1)), [1, 2, 3, 4, 5])
     l2 = [1, [2, 3], 4, [5, [6, 7], 8]]
     assert np.array_equal(list(flatten(l2)), [1, 2, 3, 4, 5, 6, 7, 8])
Example #8
0
 def test_flat_list(self):
     """Test that a flat list isn't changed"""
     l = [1, 2, 3]
     assert np.array_equal(list(flatten(l)), l)
Example #9
0
 def test_generator(self):
     """Test that the returned object is a generator object"""
     l = [1, 2, 3]
     assert isinstance(flatten(l), types.GeneratorType)
Example #10
0
 def __getitem__(self, key):
     return list(flatten(self.subsets))[key]
Example #11
0
 def __len__(self):
     return len(list(flatten(self.subsets)))
Example #12
0
 def __iter__(self):
     yield from flatten(self.subsets)