Apply a function, and enforce the output.ndim to match expected_ndim Ensures the output has the expected dimensionality.
(*args, **kwargs)
| 1026 | |
| 1027 | |
| 1028 | def apply_and_enforce(*args, **kwargs): |
| 1029 | """Apply a function, and enforce the output.ndim to match expected_ndim |
| 1030 | |
| 1031 | Ensures the output has the expected dimensionality.""" |
| 1032 | func = kwargs.pop("_func") |
| 1033 | expected_ndim = kwargs.pop("expected_ndim") |
| 1034 | out = func(*args, **kwargs) |
| 1035 | if getattr(out, "ndim", 0) != expected_ndim: |
| 1036 | out_ndim = getattr(out, "ndim", 0) |
| 1037 | raise ValueError( |
| 1038 | f"Dimension mismatch: expected output of {func} " |
| 1039 | f"to have dims = {expected_ndim}. Got {out_ndim} instead." |
| 1040 | ) |
| 1041 | return out |
| 1042 | |
| 1043 | |
| 1044 | def broadcast_chunks(*chunkss): |