Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into 238-implement-s3-si…
Browse files Browse the repository at this point in the history
…nks-pt-3
  • Loading branch information
aliddell committed Jul 9, 2024
2 parents fbc1094 + 463fe6e commit a4c1442
Show file tree
Hide file tree
Showing 20 changed files with 115 additions and 7 deletions.
31 changes: 30 additions & 1 deletion src/zarr.v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "nlohmann/json.hpp"

#include <bit>

namespace zarr = acquire::sink::zarr;

namespace {
Expand All @@ -22,6 +24,33 @@ compressed_zarr_v2_init()
}
return nullptr;
}

std::string
sample_type_to_dtype(SampleType t)

{
const std::string dtype_prefix =
std::endian::native == std::endian::big ? ">" : "<";

switch (t) {
case SampleType_u8:
return dtype_prefix + "u1";
case SampleType_u10:
case SampleType_u12:
case SampleType_u14:
case SampleType_u16:
return dtype_prefix + "u2";
case SampleType_i8:
return dtype_prefix + "i1";
case SampleType_i16:
return dtype_prefix + "i2";
case SampleType_f32:
return dtype_prefix + "f4";
default:
throw std::runtime_error("Invalid SampleType: " +
std::to_string(static_cast<int>(t)));
}
}
} // end ::{anonymous} namespace

/// ZarrV2
Expand Down Expand Up @@ -249,7 +278,7 @@ zarr::ZarrV2::write_array_metadata_(size_t level) const
metadata["zarr_format"] = 2;
metadata["shape"] = array_shape;
metadata["chunks"] = chunk_shape;
metadata["dtype"] = common::sample_type_to_dtype(image_shape.type);
metadata["dtype"] = sample_type_to_dtype(image_shape.type);
metadata["fill_value"] = 0;
metadata["order"] = "C";
metadata["filters"] = nullptr;
Expand Down
26 changes: 25 additions & 1 deletion src/zarr.v3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@ compressed_zarr_v3_init()
}
return nullptr;
}

std::string
sample_type_to_dtype(SampleType t)

{
switch (t) {
case SampleType_u8:
return "uint8";
case SampleType_u10:
case SampleType_u12:
case SampleType_u14:
case SampleType_u16:
return "uint16";
case SampleType_i8:
return "int8";
case SampleType_i16:
return "int16";
case SampleType_f32:
return "float32";
default:
throw std::runtime_error("Invalid SampleType: " +
std::to_string(static_cast<int>(t)));
}
}
} // end ::{anonymous} namespace

zarr::ZarrV3::ZarrV3(BloscCompressionParams&& compression_params)
Expand Down Expand Up @@ -175,7 +199,7 @@ zarr::ZarrV3::write_array_metadata_(size_t level) const
});

metadata["chunk_memory_layout"] = "C";
metadata["data_type"] = common::sample_type_to_dtype(image_shape.type);
metadata["data_type"] = sample_type_to_dtype(image_shape.type);
metadata["extensions"] = json::array();
metadata["fill_value"] = 0;
metadata["shape"] = array_shape;
Expand Down
4 changes: 4 additions & 0 deletions tests/multiscales-metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ verify_layer(const LayerTestCase& test_case)
std::ifstream f(zarray_path);
json zarray = json::parse(f);

const std::string dtype =
std::endian::native == std::endian::little ? "<u1" : ">u1";
CHECK(dtype == zarray["dtype"].get<std::string>());

const auto shape = zarray["shape"];
ASSERT_EQ(int, "%d", frames_per_layer, shape[0]);
ASSERT_EQ(int, "%d", layer_frame_height, shape[1]);
Expand Down
4 changes: 3 additions & 1 deletion tests/repeat-start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ validate(AcquireRuntime* runtime)
ASSERT_EQ(int, "%d", 32, chunk_shape[3]);

CHECK("C" == metadata["chunk_memory_layout"]);
CHECK("u1" == metadata["data_type"]);

CHECK("uint8" == metadata["data_type"].get<std::string>());

CHECK(metadata["extensions"].empty());

const auto array_shape = metadata["shape"];
Expand Down
4 changes: 4 additions & 0 deletions tests/write-zarr-v2-compressed-multiscale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ verify_layer(const LayerTestCase& test_case)
std::ifstream f(zarray_path);
json zarray = json::parse(f);

const std::string dtype =
std::endian::native == std::endian::little ? "<u1" : ">u1";
CHECK(dtype == zarray["dtype"].get<std::string>());

const auto shape = zarray["shape"];
ASSERT_EQ(int, "%d", frames_per_layer, shape[0]);
ASSERT_EQ(int, "%d", 1, shape[1]);
Expand Down
4 changes: 4 additions & 0 deletions tests/write-zarr-v2-compressed-with-chunking-and-rollover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ validate()
std::ifstream f(zarray_path);
json zarray = json::parse(f);

const std::string dtype =
std::endian::native == std::endian::little ? "<u1" : ">u1";
CHECK(dtype == zarray["dtype"].get<std::string>());

auto shape = zarray["shape"];
ASSERT_EQ(int, "%d", chunk_planes + 1, shape[0]);
ASSERT_EQ(int, "%d", 1, shape[1]);
Expand Down
4 changes: 4 additions & 0 deletions tests/write-zarr-v2-compressed-with-chunking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ validate()
std::ifstream f(zarray_path);
json zarray = json::parse(f);

const std::string dtype =
std::endian::native == std::endian::little ? "<u1" : ">u1";
CHECK(dtype == zarray["dtype"].get<std::string>());

auto shape = zarray["shape"];
ASSERT_EQ(int, "%d", chunk_planes, shape[0]);
ASSERT_EQ(int, "%d", 1, shape[1]);
Expand Down
4 changes: 4 additions & 0 deletions tests/write-zarr-v2-raw-chunk-size-larger-than-frame-size.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ validate()
std::ifstream f(zarray_path);
json zarray = json::parse(f);

const std::string dtype =
std::endian::native == std::endian::little ? "<u1" : ">u1";
CHECK(dtype == zarray["dtype"].get<std::string>());

auto shape = zarray["shape"];
ASSERT_EQ(int, "%d", frames_per_chunk, shape[0]);
ASSERT_EQ(int, "%d", 1, shape[1]);
Expand Down
4 changes: 4 additions & 0 deletions tests/write-zarr-v2-raw-multiscale-with-trivial-tile-size.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ verify_layer(const LayerTestCase& test_case)
std::ifstream f(zarray_path);
json zarray = json::parse(f);

const std::string dtype =
std::endian::native == std::endian::little ? "<u1" : ">u1";
CHECK(dtype == zarray["dtype"].get<std::string>());

const auto shape = zarray["shape"];
ASSERT_EQ(int, "%d", frames_per_layer, shape[0]);
ASSERT_EQ(int, "%d", 1, shape[1]);
Expand Down
4 changes: 4 additions & 0 deletions tests/write-zarr-v2-raw-multiscale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ verify_layer(const LayerTestCase& test_case)
std::ifstream f(zarray_path);
json zarray = json::parse(f);

const std::string dtype =
std::endian::native == std::endian::little ? "<u1" : ">u1";
CHECK(dtype == zarray["dtype"].get<std::string>());

const auto shape = zarray["shape"];
ASSERT_EQ(int, "%d", frames_per_layer, shape[0]);
ASSERT_EQ(int, "%d", 1, shape[1]);
Expand Down
4 changes: 4 additions & 0 deletions tests/write-zarr-v2-raw-with-even-chunking-and-rollover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ validate()
std::ifstream f(zarray_path);
json zarray = json::parse(f);

const std::string dtype =
std::endian::native == std::endian::little ? "<u1" : ">u1";
CHECK(dtype == zarray["dtype"].get<std::string>());

auto shape = zarray["shape"];
ASSERT_EQ(int, "%d", chunk_planes + 1, shape[0]);
ASSERT_EQ(int, "%d", 1, shape[1]);
Expand Down
4 changes: 4 additions & 0 deletions tests/write-zarr-v2-raw-with-even-chunking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ validate()
std::ifstream f(zarray_path);
json zarray = json::parse(f);

const std::string dtype =
std::endian::native == std::endian::little ? "<u1" : ">u1";
CHECK(dtype == zarray["dtype"].get<std::string>());

auto shape = zarray["shape"];
ASSERT_EQ(int, "%d", chunk_planes, shape[0]);
ASSERT_EQ(int, "%d", 1, shape[1]);
Expand Down
4 changes: 4 additions & 0 deletions tests/write-zarr-v2-raw-with-ragged-chunking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ validate()
std::ifstream f(zarray_path);
json zarray = json::parse(f);

const std::string dtype =
std::endian::native == std::endian::little ? "<u1" : ">u1";
CHECK(dtype == zarray["dtype"].get<std::string>());

auto shape = zarray["shape"];
ASSERT_EQ(int, "%d", max_frame_count, shape[0]);
ASSERT_EQ(int, "%d", 1, shape[1]);
Expand Down
5 changes: 5 additions & 0 deletions tests/write-zarr-v2-raw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "platform.h" // clock
#include "logger.h"

#include <bit>
#include <filesystem>
#include <fstream>
#include <stdexcept>
Expand Down Expand Up @@ -233,6 +234,10 @@ validate()
std::ifstream f(zarray_path);
json zarray = json::parse(f);

const std::string dtype =
std::endian::native == std::endian::little ? "<u1" : ">u1";
CHECK(dtype == zarray["dtype"].get<std::string>());

auto shape = zarray["shape"];
ASSERT_EQ(int, "%d", frames_per_chunk, shape[0]);
ASSERT_EQ(int, "%d", 1, shape[1]);
Expand Down
4 changes: 4 additions & 0 deletions tests/write-zarr-v2-with-lz4-compression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ validate()
std::ifstream f(zarray_path);
json zarray = json::parse(f);

const std::string dtype =
std::endian::native == std::endian::little ? "<u1" : ">u1";
CHECK(dtype == zarray["dtype"].get<std::string>());

auto shape = zarray["shape"];
ASSERT_EQ(int, "%d", frames_per_chunk, shape[0]);
ASSERT_EQ(int, "%d", 1, shape[1]);
Expand Down
4 changes: 4 additions & 0 deletions tests/write-zarr-v2-with-zstd-compression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ validate()
std::ifstream f(zarray_path);
json zarray = json::parse(f);

const std::string dtype =
std::endian::native == std::endian::little ? "<u1" : ">u1";
CHECK(dtype == zarray["dtype"].get<std::string>());

auto shape = zarray["shape"];
ASSERT_EQ(int, "%d", frames_per_chunk, shape[0]);
ASSERT_EQ(int, "%d", 1, shape[1]);
Expand Down
2 changes: 1 addition & 1 deletion tests/write-zarr-v3-compressed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ validate()
ASSERT_EQ(int, "%d", chunk_width, chunk_shape[3]);

CHECK("C" == metadata["chunk_memory_layout"]);
CHECK("u1" == metadata["data_type"]);
CHECK("uint8" == metadata["data_type"]);
CHECK(metadata["extensions"].empty());

const auto array_shape = metadata["shape"];
Expand Down
2 changes: 1 addition & 1 deletion tests/write-zarr-v3-raw-chunk-exceeds-array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ validate()
ASSERT_EQ(int, "%d", chunk_width, chunk_shape[2]);

CHECK("C" == metadata["chunk_memory_layout"]);
CHECK("u1" == metadata["data_type"]);
CHECK("uint8" == metadata["data_type"]);
CHECK(metadata["extensions"].empty());

const auto array_shape = metadata["shape"];
Expand Down
2 changes: 1 addition & 1 deletion tests/write-zarr-v3-raw-with-ragged-sharding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ validate()
ASSERT_EQ(int, "%d", chunk_width, chunk_shape[2]);

CHECK("C" == metadata["chunk_memory_layout"]);
CHECK("u1" == metadata["data_type"]);
CHECK("uint8" == metadata["data_type"]);
CHECK(metadata["extensions"].empty());

const auto array_shape = metadata["shape"];
Expand Down
2 changes: 1 addition & 1 deletion tests/write-zarr-v3-raw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ validate()
ASSERT_EQ(int, "%d", chunk_width, chunk_shape[3]);

CHECK("C" == metadata["chunk_memory_layout"]);
CHECK("u1" == metadata["data_type"]);
CHECK("uint8" == metadata["data_type"]);
CHECK(metadata["extensions"].empty());

const auto array_shape = metadata["shape"];
Expand Down

0 comments on commit a4c1442

Please sign in to comment.