Skip to content

Commit

Permalink
Merge pull request #122 from TGSAI/120_slice_converter
Browse files Browse the repository at this point in the history
Implemented conversion from SliceDescriptor to RangeDescriptor<>
  • Loading branch information
markspec authored Sep 27, 2024
2 parents f04616e + 8054a6f commit 7ea1434
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
13 changes: 13 additions & 0 deletions mdio/dataset_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,19 @@ TEST(Dataset, selRange) {
ASSERT_TRUE(sliceRes.ok()) << sliceRes.status();
}

TEST(Dataset, legacySliceDescriptor) {
std::string path = "zarrs/selTester.mdio";
auto dsRes = makePopulated(path);
ASSERT_TRUE(dsRes.ok()) << dsRes.status();
auto ds = dsRes.value();

mdio::SliceDescriptor ilRange = {"inline", 2, 5, 1};

// ds.sel does not, and will not, support the legacy slice descriptor
auto sliceRes = ds.isel(ilRange);
ASSERT_TRUE(sliceRes.ok()) << sliceRes.status();
}

TEST(Dataset, selRangeFlippedStartStop) {
std::string path = "zarrs/selTester.mdio";
auto dsRes = makePopulated(path);
Expand Down
37 changes: 20 additions & 17 deletions mdio/variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ struct extract_descriptor_Ttype;
template <typename T>
struct outer_type;

/**
* @brief A descriptor for slicing a Variable or Dataset.
* @tparam T The type of the range. Default is `Index` for `isel` based slicing.
* @param label The label of the dimension to slice. The recommended is to use a
* label instead of an index.
* @param start The start index or value of the slice.
* @param stop The stop index or value of the slice.
* @param step The step index or value of the slice. Default is 1.
*/
template <typename T = Index>
struct RangeDescriptor {
using type = T;
DimensionIdentifier label;
T start;
T stop;
Index step = 1;
};

/**
* @brief A descriptor for slicing a Variable.
* A struct representing how to slice a Variable or Dataset.
Expand All @@ -88,24 +106,9 @@ struct SliceDescriptor {
Index start;
Index stop;
Index step;
};

/**
* @brief A descriptor for slicing a Variable or Dataset.
* @tparam T The type of the range. Default is `Index` for `isel` based slicing.
* @param label The label of the dimension to slice. The recommended is to use a
* label instead of an index.
* @param start The start index or value of the slice.
* @param stop The stop index or value of the slice.
* @param step The step index or value of the slice. Default is 1.
*/
template <typename T = Index>
struct RangeDescriptor {
using type = T;
DimensionIdentifier label;
T start;
T stop;
Index step = 1;
// Implicit conversion to RangeDescriptor
operator RangeDescriptor<Index>() const { return {label, start, stop, step}; }
};

/**
Expand Down
22 changes: 22 additions & 0 deletions mdio/variable_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,28 @@ TEST(Variable, slice) {
std::filesystem::remove_all("name");
}

TEST(Variable, legacySliceDescriptor) {
auto variable =
mdio::Variable<>::Open(json_good, mdio::constants::kCreateClean).value();

// compile time example
mdio::SliceDescriptor desc1 = {"x", 0, 5, 1};
mdio::SliceDescriptor desc2 = {"y", 5, 11, 1};

auto result = variable.slice(desc2, desc1);
ASSERT_TRUE(result.ok());

auto domain = result->dimensions();

EXPECT_THAT(domain.labels(), ::testing::ElementsAre("x", "y"));

EXPECT_THAT(domain.origin(), ::testing::ElementsAre(0, 5));

EXPECT_THAT(domain.shape(), ::testing::ElementsAre(5, 6));

std::filesystem::remove_all("name");
}

TEST(Variable, sliceTyped) {
auto variable = mdio::Variable<mdio::dtypes::int16_t>::Open(
json_good, mdio::constants::kCreateClean)
Expand Down

0 comments on commit 7ea1434

Please sign in to comment.