Skip to content

Commit e966d71

Browse files
authored
Merge pull request #98 from ENCCS/npz_example
Added npz example and credited Aalto for the table
2 parents 7dcf4fb + 1899ec4 commit e966d71

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

content/scientific-data.rst

+2
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ An overview of common data formats
227227
- 🟨 : Ok / depends on a case
228228
- ❌ : Bad
229229

230+
Adapted from Aalto university's `Python for scientific computing <https://aaltoscicomp.github.io/python-for-scicomp/work-with-data/#what-is-a-data-format>`__.
231+
230232
Some of these formats (e.g. JSON and CSV) are saved as text files (ASCII), thus they are
231233
human-readable. This makes them easier to visually check them (e.g. for format errors) and
232234
are supported out of the box by many tools. However, they tend to be slower during I/O and

content/stack.rst

+38-8
Original file line numberDiff line numberDiff line change
@@ -362,14 +362,44 @@ Views and copies of arrays
362362
I/O with NumPy
363363
^^^^^^^^^^^^^^
364364

365-
- Numpy provides functions for reading data from file and for writing data
366-
into the files
367-
- Simple text files
368-
369-
- :meth:`numpy.loadtxt`
370-
- :meth:`numpy.savetxt`
371-
- Data in regular column layout
372-
- Can deal with comments and different column delimiters
365+
Numpy provides functions for reading from/writing to files. Both ASCII and binary
366+
formats are supported with the CSV and npy/npz formats:
367+
368+
.. tabs::
369+
370+
.. tab:: CSV
371+
372+
The ``numpy.loadtxt()`` and ``numpy.savetxt()`` functions can be used. They
373+
save in a regular column layout and can deal with different delimiters,
374+
column titles and numerical representations.
375+
376+
.. code-block:: python
377+
378+
a = np.array([1, 2, 3, 4])
379+
np.savetxt("my_array.csv", a)
380+
b = np.loadtxt("my_array.csv")
381+
a == b
382+
# True
383+
384+
.. tab:: Binary
385+
386+
The npy format is a binary format used to dump arrays of any
387+
shape. Several arrays can be saved into a single npz file, which is
388+
simply a zipped collection of different npy files. All the arrays to
389+
be saved into a npz file can be passed as kwargs to the ``numpy.savez()``
390+
function. The data can then be recovered using the ``numpy.load()`` method,
391+
which returns a dictionary-like object in which each key points to one of the arrays:
392+
393+
.. code-block:: python
394+
395+
a = np.array([1, 2, 3, 4])
396+
b = np.array([5, 6, 7, 8])
397+
398+
np.savez("my_arrays.npz", array_1=a, array_2=b)
399+
data = np.load("my_arrays.npz")
400+
data['array_1'] == a
401+
data['array_2'] == b
402+
# Both are true
373403
374404
375405
Random numbers

0 commit comments

Comments
 (0)