1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
.. Copyright (c) 2016, Johan Mabille, Sylvain Corlay and Wolf Vollprecht
Distributed under the terms of the BSD 3-Clause License.
The full license is in the file LICENSE, distributed with this software.
File input and output
=====================
*xtensor* has some built-in mechanisms to make loading and saving data easy.
The base *xtensor* package allows to save and load data in the ``.csv``, ``.json`` and ``.npy``
format.
Please note that many more input and output formats are available in the `xtensor-io
<https://github.com/xtensor-stack/xtensor-io>`_ package.
`xtensor-io` offers functions to load and store from image files (``jpg``, ``gif``, ``png``...),
sound files (``wav``, ``ogg``...), HDF5 files (``h5``, ``hdf5``, ...), and compressed NumPy format (``npz``).
Loading CSV data into xtensor
-----------------------------
The following example code demonstrates how to use :cpp:func:`xt::load_csv` and :cpp:func:`xt::dump_csv` to load and
save data in the Comma-separated value format. The reference documentation is :doc:`api/xcsv`.
.. code::
#include <istream>
#include <fstream>
#include <iostream>
#include <xtensor/xarray.hpp>
#include <xtensor/xcsv.hpp>
int main()
{
std::ifstream in_file;
in_file.open("in.csv");
auto data = xt::load_csv<double>(in_file);
std::ofstream out_file;
out_file("out.csv");
xt::xarray<double> a = {{1,2,3,4}, {5,6,7,8}};
xt::dump_csv(out_file, a);
return 0;
}
Loading NPY data into xtensor
-----------------------------
The following example demonstrates how to load and store xtensor data in the ``npy`` "NumPy" format,
using the :cpp:func:`xt::load_npy` and :cpp:func:`xt::dump_npy` functions.
Reference documentation for the functions used is found here :doc:`api/xnpy`.
.. code::
#include <istream>
#include <iostream>
#include <fstream>
#include <xtensor/xarray.hpp>
#include <xtensor/xnpy.hpp>
int main()
{
// Note: you need to supply the data type you are loading
// in this case "double".
auto data = xt::load_npy<double>("in.npy");
xt::xarray<double> a = {{1,2,3,4}, {5,6,7,8}};
xt::dump_npy("out.npy", a);
return 0;
}
Loading JSON data into xtensor
------------------------------
It's possible to load and dump data to json, using the json library written by
`nlohmann` (https://nlohmann.github.io/json/) which offers a convenient way
to handle json data in C++. Note that the library needs to be separately installed.
The reference documentation is found :doc:`api/xjson`.
.. code::
#include <xtensor/xjson.hpp>
#include <xtensor/xarray.hpp>
int main()
{
xt::xarray<double> t = {{{1, 2},
{3, 4}},
{{1, 2},
{3, 4}}};
nlohmann::json jl = t;
// To obtain the json serialized string
std::string s = jl.dump();
xt::xarray<double> res;
auto j = "[[10.0,10.0],[10.0,10.0]]"_json;
xt::from_json(j, res);
}
|