-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from davidbrochart/leftover
zarray is now separate from xtensor
- Loading branch information
Showing
10 changed files
with
295 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters