Skip to content

Commit

Permalink
Merge pull request #8 from davidbrochart/leftover
Browse files Browse the repository at this point in the history
zarray is now separate from xtensor
  • Loading branch information
davidbrochart committed Nov 12, 2020
2 parents 3049b3a + db2fdf2 commit ff0e06a
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ install:
# Install mamba
- conda install mamba -c conda-forge
# Install host dependencies
- mamba create -n test nlohmann_json google-cloud-cpp cpp-filesystem zlib xtensor-io python fsspec numpy numcodecs sh -c conda-forge
- mamba create -n test nlohmann_json google-cloud-cpp cpp-filesystem zlib xtensor-io zarray python fsspec numpy numcodecs sh -c conda-forge
# Install build dependencies
- mamba install -n test cmake -c conda-forge
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
Expand Down
11 changes: 9 additions & 2 deletions binder/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ name: xtensor-zarr
channels:
- conda-forge
dependencies:
- python=3.8
- notebook
- xeus-cling=0.9.0
- python
- xeus-cling=0.5.1
- xwidgets=0.17.0
- ffmpeg=4.3.1 # openimageio is being added as a runtime requirement to xtensor-io
- widgetsnbextension
- xtensor-io=0.10.1
- zarray
- bash_kernel
- nlohmann_json
- cpp-filesystem
Expand All @@ -16,3 +21,5 @@ dependencies:
- fsspec
- numcodecs
- numpy
- matplotlib
- zarr
Binary file added examples/marie_curie.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
273 changes: 273 additions & 0 deletions examples/zarr_v2.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Zarr (v2) store reading with xtensor-zarr"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this notebook we use [zarr-python](https://github.com/zarr-developers/zarr-python) to chunk an array and store it in a store (in Zarr v2 format). We then read it with [xtensor-zarr](https://github.com/xtensor-stack/xtensor-zarr), which supports reading Zarr v2 stores."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"from matplotlib.image import imread\n",
"import zarr"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = imread('marie_curie.jpg')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.imshow(a, cmap='Greys_r')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a.shape"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Store the array in Zarr v2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!rm -rf marie_curie.zarr"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"z = zarr.open('marie_curie.zarr', mode='w', shape=a.shape, chunks=(30, 20), dtype=a.dtype)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"z[:] = a"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!ls marie_curie.zarr"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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,
"metadata": {},
"outputs": [],
"source": [
">xcpp14\n",
"#pragma cling add_include_path(\"../include\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
">xcpp14\n",
"#pragma cling add_library_path(\"$CONDA_PREFIX/lib\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
">xcpp14\n",
"#pragma cling load(\"blosc\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
">xcpp14\n",
"#include \"xtensor/xio.hpp\"\n",
"#include \"xtensor/xview.hpp\"\n",
"#include \"xwidgets/ximage.hpp\"\n",
"#include \"xtensor/xchunked_array.hpp\"\n",
"#include \"xtensor-io/xio_blosc.hpp\"\n",
"#include \"xtensor-io/xchunk_store_manager.hpp\"\n",
"#include \"xtensor-io/ximage.hpp\"\n",
"#include \"xtensor-zarr/xzarr_hierarchy.hpp\"\n",
"#include \"xtensor-zarr/xzarr_file_system_store.hpp\"\n",
"#include \"xtl/xbase64.hpp\"\n",
"#include \"xcpp/xdisplay.hpp\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Read the Zarr store (v2) with xtensor-zarr"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"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 z = h.get_array(\"\");"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
">xcpp14\n",
"auto a = z.get_array<uint8_t>();\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
">xcpp14\n",
"std::vector<char> read_file(const char* filename)\n",
"{\n",
" std::basic_ifstream<char> file(filename, std::ios::binary);\n",
" return std::vector<char>((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
">xcpp14\n",
"template <class E>\n",
"std::vector<char> to_png_buffer(const xt::xexpression<E>& e)\n",
"{\n",
" const char* temp_filename = \"/tmp/xio_image.png\";\n",
" xt::dump_image(temp_filename, e);\n",
" return read_file(temp_filename);\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
">xcpp14\n",
"xw::image img;\n",
"img.value = to_png_buffer(a);\n",
"img"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The original image has been reconstructed."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "All the Kernels",
"language": "",
"name": "atk"
},
"language_info": {
"mimetype": "text/plain",
"name": "all-of-them"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
18 changes: 3 additions & 15 deletions examples/introduction.ipynb → examples/zarr_v3.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,12 @@
"outputs": [],
"source": [
">bash\n",
"rm -rf zarrita xtensor xtensor-io xtl *.tar.gz*\n",
"mkdir -p zarrita xtensor xtensor-io xtl\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 .\n",
"\n",
"wget https://github.com/xtensor-stack/xtensor/archive/master.tar.gz -O xtensor.tar.gz -q\n",
"tar zxf xtensor.tar.gz -C xtensor --strip-components 1\n",
"\n",
"wget https://github.com/xtensor-stack/xtensor-io/archive/master.tar.gz -O xtensor-io.tar.gz -q\n",
"tar zxf xtensor-io.tar.gz -C xtensor-io --strip-components 1\n",
"\n",
"wget https://github.com/xtensor-stack/xtl/archive/master.tar.gz -O xtl.tar.gz -q\n",
"tar zxf xtl.tar.gz -C xtl --strip-components 1"
"mv zarrita/zarrita.py ."
]
},
{
Expand All @@ -52,9 +43,6 @@
"outputs": [],
"source": [
">xcpp14\n",
"#pragma cling add_include_path(\"xtensor/include\")\n",
"#pragma cling add_include_path(\"xtensor-io/include\")\n",
"#pragma cling add_include_path(\"xtl/include\")\n",
"#pragma cling add_include_path(\"../include\")"
]
},
Expand Down
2 changes: 1 addition & 1 deletion include/xtensor-zarr/xzarr_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#define XTENSOR_ZARR_ARRAY_HPP

#include "nlohmann/json.hpp"
#include "xtensor/zarray.hpp"
#include "zarray/zarray.hpp"
#include "xtensor-io/xio_binary.hpp"
#include "xzarr_chunked_array.hpp"

Expand Down
8 changes: 6 additions & 2 deletions include/xtensor-zarr/xzarr_chunked_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#define XTENSOR_ZARR_CHUNKED_ARRAY_HPP

#include "xtensor/xchunked_array.hpp"
#include "xtensor/zarray.hpp"
#include "zarray/zarray.hpp"
#include "xzarr_common.hpp"
#include "xzarr_compressor.hpp"

Expand Down Expand Up @@ -43,7 +43,11 @@ namespace xt
{
std::string dtype_noendian = dtype;
char endianness = dtype[0];
if ((dtype[0] == '<') || ((dtype[0] == '>')))
if ((dtype[0] == '<') || (dtype[0] == '>'))
{
dtype_noendian = dtype.substr(1);
}
else if ((zarr_version == 2) && (dtype[0] == '|'))
{
dtype_noendian = dtype.substr(1);
}
Expand Down
2 changes: 1 addition & 1 deletion include/xtensor-zarr/xzarr_compressor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "xzarr_common.hpp"
#include "xtensor-io/xchunk_store_manager.hpp"
#include "xtensor-io/xio_binary.hpp"
#include "xtensor/zarray.hpp"
#include "zarray/zarray.hpp"
#include "xtl/xhalf_float.hpp"

namespace xt
Expand Down
1 change: 0 additions & 1 deletion include/xtensor-zarr/xzarr_gcs_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ namespace xt

void xzarr_gcs_store::erase(const std::string& key)
{
fs::remove(m_root + '/' + key);
google::cloud::Status status = m_client.DeleteObject(m_bucket, m_root + '/' + key);
if (!status.ok())
{
Expand Down
2 changes: 1 addition & 1 deletion include/xtensor-zarr/xzarr_hierarchy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#define XTENSOR_ZARR_HIERARCHY_HPP

#include "nlohmann/json.hpp"
#include "xtensor/zarray.hpp"
#include "zarray/zarray.hpp"
#include "xzarr_node.hpp"
#include "xzarr_array.hpp"
#include "xzarr_group.hpp"
Expand Down

0 comments on commit ff0e06a

Please sign in to comment.