Collect input and/or object indexes for alignment. Return new dictionaries of xarray Index objects and coordinate variables, whose keys are used to later retrieve all the indexes to compare with each other (based on the name and dimensions of their associated coordin
(
self, indexes: Indexes
)
| 231 | self.results = tuple() |
| 232 | |
| 233 | def _collect_indexes( |
| 234 | self, indexes: Indexes |
| 235 | ) -> tuple[IndexesToAlign, IndexVarsToAlign]: |
| 236 | """Collect input and/or object indexes for alignment. |
| 237 | |
| 238 | Return new dictionaries of xarray Index objects and coordinate |
| 239 | variables, whose keys are used to later retrieve all the indexes to |
| 240 | compare with each other (based on the name and dimensions of their |
| 241 | associated coordinate variables as well as the Index type). |
| 242 | |
| 243 | """ |
| 244 | collected_indexes = {} |
| 245 | collected_index_vars = {} |
| 246 | |
| 247 | for idx, idx_vars in indexes.group_by_index(): |
| 248 | idx_coord_names_and_dims = [] |
| 249 | idx_all_dims: set[Hashable] = set() |
| 250 | |
| 251 | for name, var in idx_vars.items(): |
| 252 | dims = var.dims |
| 253 | idx_coord_names_and_dims.append((name, dims)) |
| 254 | idx_all_dims.update(dims) |
| 255 | |
| 256 | key: MatchingIndexKey = (tuple(idx_coord_names_and_dims), type(idx)) |
| 257 | |
| 258 | if idx_all_dims: |
| 259 | exclude_dims = idx_all_dims & self.exclude_dims |
| 260 | if exclude_dims == idx_all_dims: |
| 261 | # Do not collect an index if all the dimensions it uses are |
| 262 | # also excluded from the alignment |
| 263 | continue |
| 264 | elif exclude_dims: |
| 265 | # If the dimensions used by index partially overlap with the dimensions |
| 266 | # excluded from alignment, it is possible to check index equality along |
| 267 | # non-excluded dimensions only. However, in this case each of the aligned |
| 268 | # objects must retain (a copy of) their original index. Re-indexing and |
| 269 | # overriding the index are not supported. |
| 270 | if self.join == "override": |
| 271 | excl_dims_str = ", ".join(str(d) for d in exclude_dims) |
| 272 | incl_dims_str = ", ".join( |
| 273 | str(d) for d in idx_all_dims - exclude_dims |
| 274 | ) |
| 275 | raise AlignmentError( |
| 276 | f"cannot exclude dimension(s) {excl_dims_str} from alignment " |
| 277 | "with `join='override` because these are used by an index " |
| 278 | f"together with non-excluded dimensions {incl_dims_str}" |
| 279 | "(cannot safely override the index)." |
| 280 | ) |
| 281 | else: |
| 282 | self.keep_original_indexes.add(key) |
| 283 | |
| 284 | collected_indexes[key] = idx |
| 285 | collected_index_vars[key] = idx_vars |
| 286 | |
| 287 | return collected_indexes, collected_index_vars |
| 288 | |
| 289 | def find_matching_indexes(self) -> None: |
| 290 | all_indexes: dict[MatchingIndexKey, list[Index]] |
no test coverage detected