carma
Converters between Armadillo matrices (C++) and Numpy arrays using Pybind11
Science Score: 64.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
✓Academic publication links
Links to: zenodo.org -
✓Committers with academic emails
1 of 7 committers (14.3%) from academic institutions -
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (13.6%) to scientific vocabulary
Keywords
Repository
Converters between Armadillo matrices (C++) and Numpy arrays using Pybind11
Basic Info
Statistics
- Stars: 100
- Watchers: 1
- Forks: 25
- Open Issues: 5
- Releases: 18
Topics
Metadata Files
README.md
A C++ header only library providing conversions between Numpy arrays and Armadillo matrices.
| Documentation |
Introduction
CARMA provides fast bidirectional conversions between Numpy arrays and Armadillo matrices, vectors and cubes, much like RcppArmadillo does for R and Armadillo.
The library extends the impressive pybind11 library with support for Armadillo. For details on Pybind11 and Armadillo refer to their respective documentation 1, 2.
Installation
CARMA is a header only library that relies on two other header only libraries, Armadillo and Pybind11.
It can be integrated in a CMake build using ADD_SUBDIRECTORY(<path_to_carma>) or installation which provides an interface library target carma::carma that has been linked with Python, Numpy, Pybind11 and Armadillo. See build configuration for details.
It can be installed using: ```bash mkdir build cd build
optionally with -DCMAKEINSTALLPREFIX:PATH=
cmake -DCARMAINSTALLLIB=ON .. cmake --build . --config Release --target install ```
You can than include it in a project using:
cmake
FIND_PACKAGE(carma CONFIG REQUIRED)
TARGET_LINK_LIBRARIES(<your_target> PRIVATE carma::carma)
CMake subdirectory
Alternatively you can forgo installing CARMA and directly use it as CMake subdirectory. For Pybind11 and or Armadillo we create target(s) based on user settable version, see build configuration, when they are not defined.
To link with CARMA:
cmake
ADD_SUBDIRECTORY(extern/carma)
TARGET_LINK_LIBRARIES(<your_target> PRIVATE carma::carma)
CARMA and Armadillo can then be included using:
```C++
include
include
```
CARMA provides a number of configurations that can be set in the carma_config.cmake file at the root of the directory or passed to CMake, see Configuration and Build configuration documentation sections for details.
Requirements
Numpy v2.* is supported by CARMA >= v0.8.0 which requires a compiler with support for C++14 and supports:
- Python 3.8 -- 3.12
- Numpy >= 1.14
- Pybind11 >= v2.12.0
- Armadillo >= 10.5.2
CARMA <= v0.7 requires a compiler with support for C++14 and supports:
- Python 3.8 -- 3.12
- Numpy >= 1.14 < 2.0
- Pybind11 >= v2.6.0 < v2.12.0
- Armadillo >= 10.5.2
CARMA makes use of Armadillo's ARMA_ALIEN_MEM_ALLOC and ARMA_ALIEN_MEM_FREE functionality introduced in version 10.5.2 to use Numpy's (de)allocator.
Considerations
In order to achieve fast conversions the default behaviour is avoid copying both from and to Numpy whenever possible and reasonable. This allows very low overhead conversions but it impacts memory safety and requires user vigilance.
If you intend to return the memory of the input array back as another array, you must make sure to either copy or steal the memory on the conversion in or copy the memory out. If you don't the memory will be aliased by the two Numpy arrays and bad things will happen.
A second consideration is memory layout. Armadillo is optimised for column-major (Fortran order) memory whereas Numpy defaults to row-major (C order). The default behaviour is to automatically convert, read copy, C-order arrays to F-order arrays upon conversion to Armadillo. Users should note that the library will not convert back to C-order when returning.
For details see the documentation section Memory Management.
Example
On a high level CARMA provides two ways to work Numpy arrays in Armadillo: Automatic conversion saves a bit on code but provides less flexibility with regards to when to copy and when not. Manual conversion should be used when you need fine grained control.
Combining the two; we use automatic conversion on the conversion in and manual when creating the tuple for the way out.
```cpp
include
include
include
include
include
namespace py = pybind11;
py::tuple ols(arma::mat& X, arma::colvec& y) { // We borrow the data underlying the numpy arrays int n = X.nrows, k = X.ncols;
arma::colvec coeffs = arma::solve(X, y);
arma::colvec resid = y - X * coeffs;
double sig2 = arma::as_scalar(arma::trans(resid) * resid / (n-k));
arma::colvec std_errs = arma::sqrt(sig2 * arma::diagvec( arma::inv(arma::trans(X)*X)) );
// We take ownership of the memory from the armadillo objects and
// return to python as a tuple containing two Numpy arrays.
return py::make_tuple(
carma::col_to_arr(coeffs),
carma::col_to_arr(std_errs)
);
}
// adapted from https://gallery.rcpp.org/articles/fast-linear-model-with-armadillo/ ```
Which can be called using:
python
y = np.linspace(1, 100, num=100) + np.random.normal(0, 0.5, 100)
X = np.hstack((np.ones(100)[:, None], np.arange(1, 101)[:, None]))
coeff, std_err = carma.ols(X, y)
The repository contains tests, examples and CMake build instructions that can be used as an reference.
About
This project was created by Ralph Urlus. Significant improvements to the project have been contributed by Pascal H.
License
CARMA is provided under a Apache 2.0 license that can be found in the LICENSE file. By using, distributing, or contributing to this project, you agree to the terms and conditions of this license.
Owner
- Name: Ralph Urlus
- Login: RUrlus
- Kind: user
- Location: Amsterdam, Netherlands
- Company: ING
- Repositories: 5
- Profile: https://github.com/RUrlus
Data scientist working at ING Analytics WB in the Netherlands.
Citation (CITATION.cff)
cff-version: 1.2.0 message: "If you use this software, please cite it as below." authors: - family-names: "Urlus" given-names: "Ralph" title: "CARMA: bidirectional conversions between Numpy and Armadillo" version: 0.6.7 doi: 10.5281/zenodo.7791193 date-released: 2023-04-01 url: "https://github.com/RUrlus/carma"
GitHub Events
Total
- Watch event: 17
- Issue comment event: 2
- Push event: 2
- Pull request review event: 1
- Pull request event: 4
- Fork event: 2
Last Year
- Watch event: 17
- Issue comment event: 2
- Push event: 2
- Pull request review event: 1
- Pull request event: 4
- Fork event: 2
Committers
Last synced: 9 months ago
Top Committers
| Name | Commits | |
|---|---|---|
| Ralph Urlus | r****s@R****l | 344 |
| Pascal Havé | h****f@h****m | 29 |
| conradsnicta | c****a | 20 |
| Tamas Kenez | t****z | 1 |
| Jan Mrázek | e****l@h****z | 1 |
| Antonio Vilches | a****a@g****m | 1 |
| Dennis Kreber | k****d@u****e | 1 |
Committer Domains (Top 20 + Academic)
Issues and Pull Requests
Last synced: 6 months ago
All Time
- Total issues: 62
- Total pull requests: 72
- Average time to close issues: about 1 month
- Average time to close pull requests: 6 days
- Total issue authors: 23
- Total pull request authors: 9
- Average comments per issue: 4.61
- Average comments per pull request: 0.93
- Merged pull requests: 65
- Bot issues: 0
- Bot pull requests: 0
Past Year
- Issues: 0
- Pull requests: 3
- Average time to close issues: N/A
- Average time to close pull requests: 1 day
- Issue authors: 0
- Pull request authors: 2
- Average comments per issue: 0
- Average comments per pull request: 0.67
- Merged pull requests: 3
- Bot issues: 0
- Bot pull requests: 0
Top Authors
Issue Authors
- hpwxf (12)
- RUrlus (12)
- TNonet (5)
- kjohnsen (5)
- motiwari (5)
- conradsnicta (2)
- binoyp (2)
- peekxc (2)
- beckerrh (2)
- stormshawn (2)
- SanPen (1)
- avilchess (1)
- simon-hirsch (1)
- Adarsh321123 (1)
- dzuev2 (1)
Pull Request Authors
- RUrlus (34)
- hpwxf (22)
- conradsnicta (12)
- tamaskenez (2)
- avilchess (1)
- Dniskk (1)
- rolshoven (1)
- yaqwsx (1)
- mlouielu (1)
Top Labels
Issue Labels
Pull Request Labels
Dependencies
- docutils <0.18
- sphinx *
- sphinx-rtd-theme *
- sphinxcontrib-napoleon *
- actions/checkout v3 composite
- actions/setup-python v4 composite
- fkirc/skip-duplicate-actions master composite
- actions/checkout v3 composite
- actions/setup-node master composite
- actions/setup-python v4 composite
- coverallsapp/github-action master composite
- fkirc/skip-duplicate-actions master composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- fkirc/skip-duplicate-actions master composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- fkirc/skip-duplicate-actions master composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- fkirc/skip-duplicate-actions master composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- fkirc/skip-duplicate-actions master composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- egor-tensin/setup-clang v1 composite
- fkirc/skip-duplicate-actions master composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- fkirc/skip-duplicate-actions master composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- fkirc/skip-duplicate-actions master composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- fkirc/skip-duplicate-actions master composite
- actions/checkout v3 composite
- actions/setup-python v4 composite
- fkirc/skip-duplicate-actions master composite
- cppdebug 0.1 build
- ubuntu 18.04 build