Skip to content

Commit

Permalink
Merge pull request #29 from davidbrochart/named_parameters
Browse files Browse the repository at this point in the history
Implement optional named parameters for create_array()
  • Loading branch information
davidbrochart committed Dec 17, 2020
2 parents 7518411 + d0ec08f commit 49a7552
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 38 deletions.
15 changes: 0 additions & 15 deletions examples/zarr_v2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,6 @@
"The array consists of about 100 chunks."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
">bash\n",
"rm -rf zarrita *.tar.gz*\n",
"mkdir -p zarrita\n",
"\n",
"wget https://github.com/alimanfoo/zarrita/archive/master.tar.gz -O zarrita.tar.gz -q\n",
"tar zxf zarrita.tar.gz -C zarrita --strip-components 1\n",
"mv zarrita/zarrita.py ."
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
8 changes: 7 additions & 1 deletion examples/zarr_v3.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,13 @@
"std::size_t pool_size = 1;\n",
"double fill_value = 6.6;\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);"
"xt::xzarr_create_array_options<> o;\n",
"o.chunk_memory_layout = 'C';\n",
"o.chunk_separator = '/';\n",
"o.attrs = attrs;\n",
"o.chunk_pool_size = pool_size;\n",
"o.fill_value = fill_value;\n",
"xt::zarray z2 = h2.create_array(\"/arthur/dent\", shape, chunk_shape, \"<f8\", o);"
]
},
{
Expand Down
21 changes: 21 additions & 0 deletions include/xtensor-zarr/xzarr_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@

namespace xt
{
template <class C = xio_binary_config>
struct xzarr_create_array_options
{
char chunk_memory_layout;
char chunk_separator;
C compressor;
nlohmann::json attrs;
std::size_t chunk_pool_size;
nlohmann::json fill_value;

xzarr_create_array_options()
: chunk_memory_layout('C')
, chunk_separator('/')
, compressor(C())
, attrs(nlohmann::json::object())
, chunk_pool_size(1)
, fill_value(nlohmann::json())
{
}
};

inline std::size_t get_zarr_major(const std::string& zarr_version)
{
std::size_t i = zarr_version.find('.');
Expand Down
19 changes: 5 additions & 14 deletions include/xtensor-zarr/xzarr_hierarchy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,8 @@ namespace xt

void create_hierarchy();

template <class shape_type, class C>
zarray create_array(const std::string& path, shape_type shape, shape_type chunk_shape, const std::string& dtype, char chunk_memory_layout='C', char chunk_separator='/', const C& compressor=xio_binary_config(), const nlohmann::json& attrs=nlohmann::json::object(), std::size_t chunk_pool_size=1, const nlohmann::json& fill_value=nlohmann::json());

template <class shape_type>
zarray create_array(const std::string& path, shape_type shape, shape_type chunk_shape, const std::string& dtype, char chunk_memory_layout='C', char chunk_separator='/', const xio_binary_config& compressor=xio_binary_config(), const nlohmann::json& attrs=nlohmann::json::object(), std::size_t chunk_pool_size=1, const nlohmann::json& fill_value=nlohmann::json());
template <class shape_type, class O = xzarr_create_array_options<xio_binary_config>>
zarray create_array(const std::string& path, shape_type shape, shape_type chunk_shape, const std::string& dtype, O o=O());

zarray get_array(const std::string& path, std::size_t chunk_pool_size=1);

Expand Down Expand Up @@ -85,18 +82,12 @@ namespace xt
}

template <class store_type>
template <class shape_type, class C>
zarray xzarr_hierarchy<store_type>::create_array(const std::string& path, shape_type shape, shape_type chunk_shape, const std::string& dtype, char chunk_memory_layout, char chunk_separator, const C& compressor, const nlohmann::json& attrs, std::size_t chunk_pool_size, const nlohmann::json& fill_value)
template <class shape_type, class O>
zarray xzarr_hierarchy<store_type>::create_array(const std::string& path, shape_type shape, shape_type chunk_shape, const std::string& dtype, O o)
{
return create_zarr_array(m_store, path, shape, chunk_shape, dtype, chunk_memory_layout, chunk_separator, compressor, attrs, chunk_pool_size, fill_value, m_zarr_version);
return create_zarr_array(m_store, path, shape, chunk_shape, dtype, o.chunk_memory_layout, o.chunk_separator, o.compressor, o.attrs, o.chunk_pool_size, o.fill_value, m_zarr_version);
}

template <class store_type>
template <class shape_type>
zarray xzarr_hierarchy<store_type>::create_array(const std::string& path, shape_type shape, shape_type chunk_shape, const std::string& dtype, char chunk_memory_layout, char chunk_separator, const xio_binary_config& compressor, const nlohmann::json& attrs, std::size_t chunk_pool_size, const nlohmann::json& fill_value)
{
return create_zarr_array(m_store, path, shape, chunk_shape, dtype, chunk_memory_layout, chunk_separator, compressor, attrs, chunk_pool_size, fill_value, m_zarr_version);
}

template <class store_type>
zarray xzarr_hierarchy<store_type>::get_array(const std::string& path, std::size_t chunk_pool_size)
Expand Down
11 changes: 6 additions & 5 deletions include/xtensor-zarr/xzarr_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "nlohmann/json.hpp"
#include "xzarr_array.hpp"
#include "xzarr_group.hpp"
#include "xzarr_common.hpp"

namespace xt
{
Expand All @@ -38,8 +39,8 @@ namespace xt

xzarr_group<store_type> create_group(const std::string& name, const nlohmann::json& attrs=nlohmann::json::object(), const nlohmann::json& extensions=nlohmann::json::array());

template <class shape_type, class C>
zarray create_array(const std::string& name, shape_type shape, shape_type chunk_shape, const std::string& dtype, char chunk_memory_layout='C', char chunk_separator='/', const C& compressor=xio_binary_config(), const nlohmann::json& attrs=nlohmann::json::object(), std::size_t chunk_pool_size=1, const nlohmann::json& fill_value=nlohmann::json());
template <class shape_type, class O = xzarr_create_array_options<xio_binary_config>>
zarray create_array(const std::string& name, shape_type shape, shape_type chunk_shape, const std::string& dtype, O o=O());

zarray get_array(std::size_t chunk_pool_size=1);
xzarr_group<store_type> get_group();
Expand Down Expand Up @@ -115,11 +116,11 @@ namespace xt
}

template <class store_type>
template <class shape_type, class C>
zarray xzarr_node<store_type>::create_array(const std::string& name, shape_type shape, shape_type chunk_shape, const std::string& dtype, char chunk_memory_layout, char chunk_separator, const C& compressor, const nlohmann::json& attrs, std::size_t chunk_pool_size, const nlohmann::json& fill_value)
template <class shape_type, class O>
zarray xzarr_node<store_type>::create_array(const std::string& name, shape_type shape, shape_type chunk_shape, const std::string& dtype, O o)
{
m_node_type = xzarr_node_type::array;
return create_zarr_array(m_store, m_path + '/' + name, shape, chunk_shape, dtype, chunk_memory_layout, chunk_separator, compressor, attrs, chunk_pool_size, fill_value);
return create_zarr_array(m_store, m_path + '/' + name, shape, chunk_shape, dtype, o.chunk_memory_layout, o.chunk_separator, o.compressor, o.attrs, o.chunk_pool_size, o.fill_value);
}

template <class store_type>
Expand Down
21 changes: 18 additions & 3 deletions test/test_zarr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ namespace xt
std::size_t pool_size = 1;
double fill_value = 6.6;
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);
xzarr_create_array_options<xio_gzip_config> o;
o.chunk_memory_layout = 'C';
o.chunk_separator = '/';
o.attrs = attrs;
o.chunk_pool_size = pool_size;
o.fill_value = fill_value;
zarray z1 = h.create_array("/arthur/dent", shape, chunk_shape, "<f8", o);
//xchunked_array_factory<xzarr_file_system_store>::add_dtype<half_float::half>("f2");
//auto a1 = z1.get_array<double>();
//double v = 3.;
Expand Down Expand Up @@ -91,7 +97,10 @@ namespace xt
h1["/marvin"].create_group("paranoid");
std::vector<size_t> shape = {5, 5};
std::vector<size_t> chunk_shape = {3, 3};
h1["/marvin"].create_array("android", shape, chunk_shape, "<f8", 'C', '/', xio_binary_config());
xzarr_create_array_options<> o;
o.chunk_memory_layout = 'C';
o.chunk_separator = '/';
h1["/marvin"].create_array("android", shape, chunk_shape, "<f8", o);
// since "/marvin/android" is an array, it should not be possible to get it as a group
EXPECT_THROW(h1["/marvin/android"].get_group(), std::runtime_error);
}
Expand Down Expand Up @@ -182,7 +191,13 @@ namespace xt
double fill_value = 6.6;
std::string zarr_version = "2";
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);
xzarr_create_array_options<xio_gzip_config> o;
o.chunk_memory_layout = 'C';
o.chunk_separator = '.';
o.attrs = attrs;
o.chunk_pool_size = pool_size;
o.fill_value = fill_value;
zarray z1 = h.create_array("/arthur/dent", shape, chunk_shape, "<f8", o);
}

TEST(xzarr_hierarchy, array_default_params)
Expand Down

0 comments on commit 49a7552

Please sign in to comment.