Stacks the given `Field`s along `dim`. See Also: `concat()`. Args: fields: List of matching `Field` instances. dim: Stack dimension as `Shape`. Size is ignored. dim_bounds: `Box` defining the physical size for `dim`. Returns: `Field` matchi
(fields: Sequence[Field], dim: Shape, dim_bounds: Box = None)
| 944 | |
| 945 | |
| 946 | def stack(fields: Sequence[Field], dim: Shape, dim_bounds: Box = None): |
| 947 | """ |
| 948 | Stacks the given `Field`s along `dim`. |
| 949 | |
| 950 | See Also: |
| 951 | `concat()`. |
| 952 | |
| 953 | Args: |
| 954 | fields: List of matching `Field` instances. |
| 955 | dim: Stack dimension as `Shape`. Size is ignored. |
| 956 | dim_bounds: `Box` defining the physical size for `dim`. |
| 957 | |
| 958 | Returns: |
| 959 | `Field` matching stacked fields. |
| 960 | """ |
| 961 | assert all(isinstance(f, Field) for f in fields), f"All fields must be Fields of the same type but got {fields}" |
| 962 | assert all(isinstance(f, type(fields[0])) for f in fields), f"All fields must be Fields of the same type but got {fields}" |
| 963 | if any([f.sampled_at != fields[0].sampled_at for f in fields]): |
| 964 | return math.layout(fields, dim) |
| 965 | if any(f.boundary != fields[0].boundary for f in fields): |
| 966 | boundary = math.stack([f.boundary for f in fields], dim) |
| 967 | else: |
| 968 | boundary = fields[0].boundary |
| 969 | if fields[0].is_grid: |
| 970 | values = math.stack([f.values for f in fields], dim) |
| 971 | if spatial(dim): |
| 972 | if dim_bounds is None: |
| 973 | dim_bounds = Box(**{dim.name: len(fields)}) |
| 974 | return grid(values, boundary, fields[0].bounds * dim_bounds) |
| 975 | else: |
| 976 | return fields[0].with_values(values).with_boundary(boundary) |
| 977 | else: |
| 978 | values = math.stack([f.values for f in fields], dim) |
| 979 | geometry = fields[0].geometry if all(f.geometry == fields[0].geometry for f in fields) else math.stack([f.geometry for f in fields], dim, layout_non_matching=True) |
| 980 | if isinstance(geometry, Tensor): |
| 981 | from phi.geom._geom_ops import GeometryStack |
| 982 | geometry = GeometryStack(geometry) |
| 983 | return Field(geometry, values, boundary) |
| 984 | |
| 985 | |
| 986 | def assert_close(*fields: Field or Tensor or Number, |