Skip to content

Commit

Permalink
Markdown editing of README
Browse files Browse the repository at this point in the history
  • Loading branch information
chraibi authored Jan 3, 2018
1 parent f19917f commit 4ecb0a5
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions README
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
Purpose:
# Purpose:

Numpy offers the save method for easy saving of arrays into .npy and savez for zipping multiple .npy arrays together into a .npz file. cnpy lets you read and write to these formats in C++. The motivation comes from scientific programming where large amounts of data are generated in C++ and analyzed in Python. Writing to .npy has the advantage of using low-level C++ I/O (fread and fwrite) for speed and binary format for size. The .npy file header takes care of specifying the size, shape, and data type of the array, so specifying the format of the data is unnecessary. Loading data written in numpy formats into C++ is equally simple, but requires you to type-cast the loaded data to the type of your choice.
NumPy offers the `save` method for easy saving of arrays into .npy and `savez` for zipping multiple .npy arrays together into a .npz file.

Installation:
`cnpy` lets you read and write to these formats in C++.

Default installation directory is /usr/local. To specify a different directory, add -DCMAKE_INSTALL_PREFIX=/path/to/install/dir to the cmake invocation in step 4.
The motivation comes from scientific programming where large amounts of data are generated in C++ and analyzed in Python.
Writing to .npy has the advantage of using low-level C++ I/O (fread and fwrite) for speed and binary format for size.
The .npy file header takes care of specifying the size, shape, and data type of the array, so specifying the format of the data is unnecessary.
Loading data written in numpy formats into C++ is equally simple, but requires you to type-cast the loaded data to the type of your choice.

# Installation:

Default installation directory is /usr/local.
To specify a different directory, add `-DCMAKE_INSTALL_PREFIX=/path/to/install/dir` to the cmake invocation in step 4.

1. get cmake at www.cmake.org
2. create a build directory, say $HOME/build
Expand All @@ -13,24 +21,33 @@ Default installation directory is /usr/local. To specify a different directory,
5. make
6. make install

Using:
# Using:

To use, #include"cnpy.h" in your source code. Compile the source code mycode.cpp as

```bash
g++ -o mycode mycode.cpp -L/path/to/install/dir -lcnpy -lz --std=c++11
```

Description:
# Description:

There are two functions for writing data: npy_save, npz_save.
There are two functions for writing data: `npy_save` and `npz_save`.

There are 3 functions for reading. npy_load will load a .npy file. npz_load(fname) will load a .npz and return a dictionary of NpyArray structues. npz_load(fname,varname) will load and return the NpyArray for data varname from the specified .npz file.
There are 3 functions for reading:
- `npy_load` will load a .npy file.
- `npz_load(fname)` will load a .npz and return a dictionary of NpyArray structues.
- `npz_load(fname,varname)` will load and return the NpyArray for data varname from the specified .npz file.

The data structure for loaded data is below. Data is accessed via the data<T>() method, which returns a pointer of the specified type (which must match the underlying datatype of the data). The array shape and word size are read from the npy header.
The data structure for loaded data is below.
Data is accessed via the `data<T>()`-method, which returns a pointer of the specified type (which must match the underlying datatype of the data).
The array shape and word size are read from the npy header.

```c++
struct NpyArray {
std::vector<size_t> shape;
size_t word_size;
template<typename T> T* data();
};
```

See example1.cpp for examples of how to use the library. example1 will also be build during cmake installation.
See [example1.cpp](example1.cpp) for examples of how to use the library. example1 will also be build during cmake installation.

0 comments on commit 4ecb0a5

Please sign in to comment.