(self, func, variant, unit, dtype)
| 4569 | ids=repr, |
| 4570 | ) |
| 4571 | def test_comparisons(self, func, variant, unit, dtype): |
| 4572 | array1 = np.linspace(0, 5, 10).astype(dtype) |
| 4573 | array2 = np.linspace(-5, 0, 10).astype(dtype) |
| 4574 | |
| 4575 | coord = np.arange(len(array1)).astype(dtype) |
| 4576 | |
| 4577 | variants = { |
| 4578 | "data": (unit_registry.m, 1, 1), |
| 4579 | "dims": (1, unit_registry.m, 1), |
| 4580 | "coords": (1, 1, unit_registry.m), |
| 4581 | } |
| 4582 | data_unit, dim_unit, coord_unit = variants[variant] |
| 4583 | a = array1 * data_unit |
| 4584 | b = array2 * data_unit |
| 4585 | x = coord * dim_unit |
| 4586 | y = coord * coord_unit |
| 4587 | |
| 4588 | ds = xr.Dataset( |
| 4589 | data_vars={"a": ("x", a), "b": ("x", b)}, |
| 4590 | coords={"x": x, "y": ("x", y)}, |
| 4591 | ) |
| 4592 | units = extract_units(ds) |
| 4593 | |
| 4594 | other_variants = { |
| 4595 | "data": (unit, 1, 1), |
| 4596 | "dims": (1, unit, 1), |
| 4597 | "coords": (1, 1, unit), |
| 4598 | } |
| 4599 | other_data_unit, other_dim_unit, other_coord_unit = other_variants[variant] |
| 4600 | |
| 4601 | other_units = { |
| 4602 | "a": other_data_unit, |
| 4603 | "b": other_data_unit, |
| 4604 | "x": other_dim_unit, |
| 4605 | "y": other_coord_unit, |
| 4606 | } |
| 4607 | |
| 4608 | to_convert = { |
| 4609 | key: unit if is_compatible(unit, reference) else None |
| 4610 | for key, (unit, reference) in zip_mappings(units, other_units) |
| 4611 | } |
| 4612 | # convert units where possible, then attach all units to the converted dataset |
| 4613 | other = attach_units(strip_units(convert_units(ds, to_convert)), other_units) |
| 4614 | other_units = extract_units(other) |
| 4615 | |
| 4616 | # make sure all units are compatible and only then try to |
| 4617 | # convert and compare values |
| 4618 | equal_ds = all( |
| 4619 | is_compatible(unit, other_unit) |
| 4620 | for _, (unit, other_unit) in zip_mappings(units, other_units) |
| 4621 | ) and (strip_units(ds).equals(strip_units(convert_units(other, units)))) |
| 4622 | equal_units = units == other_units |
| 4623 | expected = equal_ds and (func.name != "identical" or equal_units) |
| 4624 | |
| 4625 | actual = func(ds, other) |
| 4626 | |
| 4627 | assert expected == actual |
| 4628 |
nothing calls this directly
no test coverage detected