Skip to content

Commit

Permalink
Merge pull request #23 from davidbrochart/local_fs
Browse files Browse the repository at this point in the history
Overload hierarchy creation with local file system store
  • Loading branch information
JohanMabille committed Dec 7, 2020
2 parents 90d13d9 + 16aa4b1 commit 4524240
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 25 deletions.
3 changes: 1 addition & 2 deletions examples/explore_hierarchy.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@
"outputs": [],
"source": [
">xcpp14\n",
"xt::xzarr_file_system_store s1(\"test.zr3\");\n",
"auto h1 = xt::get_zarr_hierarchy(s1);"
"auto h1 = xt::get_zarr_hierarchy(\"test.zr3\");"
]
},
{
Expand Down
3 changes: 1 addition & 2 deletions examples/zarr_v2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@
"source": [
">xcpp14\n",
"xt::xzarr_register_compressor<xt::xzarr_file_system_store, xt::xio_blosc_config>();\n",
"xt::xzarr_file_system_store s(\"marie_curie.zarr\");\n",
"auto h = xt::get_zarr_hierarchy(s, \"2\");\n",
"auto h = xt::get_zarr_hierarchy(\"marie_curie.zarr\", \"2\");\n",
"auto z = h.get_array(\"\");"
]
},
Expand Down
10 changes: 4 additions & 6 deletions examples/zarr_v3.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@
"outputs": [],
"source": [
">xcpp14\n",
"xt::xzarr_file_system_store s1(\"h_zarrita.zr3\");\n",
"auto h1 = xt::get_zarr_hierarchy(s1);\n",
"auto h1 = xt::get_zarr_hierarchy(\"h_zarrita.zr3\");\n",
"xt::zarray z1 = h1.get_array(\"/arthur/dent\");"
]
},
Expand All @@ -170,7 +169,7 @@
"source": [
">xcpp14\n",
"auto a1 = z1.get_array<double>();\n",
"std::cout << a1 << std::endl;"
"a1"
]
},
{
Expand All @@ -192,8 +191,7 @@
"nlohmann::json attrs = {{\"question\", \"life\"}, {\"answer\", 42}};\n",
"std::size_t pool_size = 1;\n",
"double fill_value = 6.6;\n",
"xt::xzarr_file_system_store s2(\"h_xtensor.zr3\");\n",
"auto h2 = xt::create_zarr_hierarchy(s2);\n",
"auto h2 = xt::create_zarr_hierarchy(\"h_xtensor.zr3\");\n",
"xt::zarray z2 = h2.create_array(\"/arthur/dent\", shape, chunk_shape, \"<f8\", 'C', '/', xt::xio_binary_config(), attrs, pool_size, fill_value);"
]
},
Expand Down Expand Up @@ -262,7 +260,7 @@
"auto h3 = xt::get_zarr_hierarchy(s3);\n",
"xt::zarray z3 = h3.get_array(\"/arthur/dent\");\n",
"auto a3 = z3.get_array<int32_t>();\n",
"std::cout << a3 << std::endl;"
"a3"
]
}
],
Expand Down
26 changes: 25 additions & 1 deletion include/xtensor-zarr/xzarr_hierarchy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "xzarr_array.hpp"
#include "xzarr_group.hpp"
#include "xzarr_common.hpp"
#include "xzarr_file_system_store.hpp"

namespace xt
{
Expand Down Expand Up @@ -50,7 +51,7 @@ namespace xt
nlohmann::json get_nodes(const std::string& path="/");

private:
store_type& m_store;
store_type m_store;
std::string m_zarr_version;
};

Expand Down Expand Up @@ -140,6 +141,17 @@ namespace xt
return h;
}

xzarr_hierarchy<xzarr_file_system_store> create_zarr_hierarchy(const char* local_store_path, const std::string& zarr_version = "3")
{
xzarr_file_system_store store(local_store_path);
return create_zarr_hierarchy(store, zarr_version);
}

xzarr_hierarchy<xzarr_file_system_store> create_zarr_hierarchy(const std::string& local_store_path, const std::string& zarr_version = "3")
{
return create_zarr_hierarchy(local_store_path.c_str(), zarr_version);
}

/**
* Accesses a Zarr hierarchy.
* This function returns a ``xzarr_hierarchy`` handler to a hierarchy in a given store.
Expand Down Expand Up @@ -176,6 +188,18 @@ namespace xt
xzarr_hierarchy<store_type> h(store, zarr_ver);
return h;
}

xzarr_hierarchy<xzarr_file_system_store> get_zarr_hierarchy(const char* local_store_path, const std::string& zarr_version = "0")
{
xzarr_file_system_store store(local_store_path);
return get_zarr_hierarchy(store, zarr_version);
}

xzarr_hierarchy<xzarr_file_system_store> get_zarr_hierarchy(const std::string& local_store_path, const std::string& zarr_version = "0")
{
return get_zarr_hierarchy(local_store_path.c_str(), zarr_version);
}

}

#endif
21 changes: 7 additions & 14 deletions test/test_zarr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ namespace xt
TEST(xzarr_hierarchy, read_v2)
{
xzarr_register_compressor<xzarr_file_system_store, xio_gzip_config>();
xzarr_file_system_store s("h_zarr.zr2");
auto h = get_zarr_hierarchy(s);
auto h = get_zarr_hierarchy("h_zarr.zr2");
zarray z = h.get_array("/arthur/dent");
auto ref = arange(2 * 5).reshape({2, 5});
auto a = z.get_array<double>();
Expand All @@ -37,8 +36,7 @@ namespace xt

TEST(xzarr_hierarchy, read_array)
{
xzarr_file_system_store s("h_zarrita.zr3");
auto h = get_zarr_hierarchy(s);
auto h = get_zarr_hierarchy("h_zarrita.zr3");
zarray z = h.get_array("/arthur/dent");
auto ref = arange(2 * 5).reshape({2, 5});
auto a = z.get_array<double>();
Expand All @@ -53,8 +51,7 @@ namespace xt
nlohmann::json attrs = {{"question", "life"}, {"answer", 42}};
std::size_t pool_size = 1;
double fill_value = 6.6;
xzarr_file_system_store s("h_xtensor.zr3");
auto h = create_zarr_hierarchy(s);
auto h = create_zarr_hierarchy("h_xtensor.zr3");
zarray z1 = h.create_array("/arthur/dent", shape, chunk_shape, "<f8", 'C', '/', xio_gzip_config(), attrs, pool_size, fill_value);
//xchunked_array_factory<xzarr_file_system_store>::add_dtype<half_float::half>("f2");
//auto a1 = z1.get_array<double>();
Expand All @@ -80,8 +77,7 @@ namespace xt

TEST(xzarr_hierarchy, create_group)
{
xzarr_file_system_store store1("h_xtensor.zr3");
auto h1 = get_zarr_hierarchy(store1);
auto h1 = get_zarr_hierarchy("h_xtensor.zr3");
nlohmann::json attrs = {{"heart", "gold"}, {"improbability", "infinite"}};
auto g1 = h1.create_group("/tricia/mcmillan", attrs);
// since "/tricia/mcmillan" is a group, it should not be possible to get it as an array
Expand All @@ -90,8 +86,7 @@ namespace xt

TEST(xzarr_hierarchy, create_node)
{
xzarr_file_system_store store1("h_xtensor.zr3");
auto h1 = get_zarr_hierarchy(store1);
auto h1 = get_zarr_hierarchy("h_xtensor.zr3");
h1.create_group("/marvin");
h1["/marvin"].create_group("paranoid");
std::vector<size_t> shape = {5, 5};
Expand All @@ -103,8 +98,7 @@ namespace xt

TEST(xzarr_hierarchy, explore)
{
xzarr_file_system_store store1("h_xtensor.zr3");
auto h1 = get_zarr_hierarchy(store1);
auto h1 = get_zarr_hierarchy("h_xtensor.zr3");
std::string children = h1.get_children("/").dump();
std::string ref1 = "{\"arthur\":\"implicit_group\",\"marvin\":\"explicit_group\",\"tricia\":\"implicit_group\"}";
EXPECT_EQ(children, ref1);
Expand Down Expand Up @@ -187,8 +181,7 @@ namespace xt
std::size_t pool_size = 1;
double fill_value = 6.6;
std::string zarr_version = "2";
xzarr_file_system_store s("h_xtensor.zr2");
auto h = create_zarr_hierarchy(s, zarr_version);
auto h = create_zarr_hierarchy("h_xtensor.zr2", zarr_version);
zarray z1 = h.create_array("/arthur/dent", shape, chunk_shape, "<f8", 'C', '.', xio_gzip_config(), attrs, pool_size, fill_value);
}
}

0 comments on commit 4524240

Please sign in to comment.