Helper function for padding boundaries with values in the array. Handles the cases where the padding is constructed from values in the array. Namely by reflecting them or tiling them to create periodic boundary constraints.
(array, pad_width, mode, **kwargs)
| 1088 | |
| 1089 | |
| 1090 | def pad_reuse(array, pad_width, mode, **kwargs): |
| 1091 | """ |
| 1092 | Helper function for padding boundaries with values in the array. |
| 1093 | |
| 1094 | Handles the cases where the padding is constructed from values in |
| 1095 | the array. Namely by reflecting them or tiling them to create periodic |
| 1096 | boundary constraints. |
| 1097 | """ |
| 1098 | |
| 1099 | if mode in {"reflect", "symmetric"}: |
| 1100 | reflect_type = kwargs.get("reflect", "even") |
| 1101 | if reflect_type == "odd": |
| 1102 | raise NotImplementedError("`pad` does not support `reflect_type` of `odd`.") |
| 1103 | if reflect_type != "even": |
| 1104 | raise ValueError( |
| 1105 | "unsupported value for reflect_type, must be one of (`even`, `odd`)" |
| 1106 | ) |
| 1107 | |
| 1108 | result = np.empty(array.ndim * (3,), dtype=object) |
| 1109 | for idx in np.ndindex(result.shape): |
| 1110 | select = [] |
| 1111 | orient = [] |
| 1112 | for i, s, pw in zip(idx, array.shape, pad_width): |
| 1113 | if mode == "wrap": |
| 1114 | pw = pw[::-1] |
| 1115 | |
| 1116 | if i < 1: |
| 1117 | if mode == "reflect": |
| 1118 | select.append(slice(1, pw[0] + 1, None)) |
| 1119 | else: |
| 1120 | select.append(slice(None, pw[0], None)) |
| 1121 | elif i > 1: |
| 1122 | if mode == "reflect": |
| 1123 | select.append(slice(s - pw[1] - 1, s - 1, None)) |
| 1124 | else: |
| 1125 | select.append(slice(s - pw[1], None, None)) |
| 1126 | else: |
| 1127 | select.append(slice(None)) |
| 1128 | |
| 1129 | if i != 1 and mode in ["reflect", "symmetric"]: |
| 1130 | orient.append(slice(None, None, -1)) |
| 1131 | else: |
| 1132 | orient.append(slice(None)) |
| 1133 | |
| 1134 | select = tuple(select) |
| 1135 | orient = tuple(orient) |
| 1136 | |
| 1137 | if mode == "wrap": |
| 1138 | idx = tuple(2 - i for i in idx) |
| 1139 | |
| 1140 | result[idx] = array[select][orient] |
| 1141 | |
| 1142 | result = block(result.tolist()) |
| 1143 | |
| 1144 | return result |
| 1145 | |
| 1146 | |
| 1147 | def pad_stats(array, pad_width, mode, stat_length): |