Comments (2)
I think the PYBIND11_MAKE_OPAQUE(std::vector<double>);
and https://pybind11.readthedocs.io/en/stable/advanced/pycpp/numpy.html approach is going to work, but in case it doesn't here are some code snippets:
From pybind/pybind11#1042:
pybind11::array WrapVector(std::vector<double> *v) {
auto capsule = py::capsule(
&v, [](void *v) { delete reinterpret_cast<std::vector<double> *>(v); });
return py::array(static_cast<pybind11::ssize_t>(v->size()), v->data(),
capsule);
}
which led to double-free problems.
This silliness also worked:
class Vector : public std::vector<double> {
public:
Vector(size_t size) : std::vector<double>(size){};
};
from bito.
Here's the code I used to time the various versions:
import numpy as np
import sbn
import timeit
v1_c = sbn.make_vector()
v2_c = sbn.make_vector()
v3_c = sbn.make_vector()
a1_c = np.array(v1_c, copy=False)
a2_c = np.array(v2_c, copy=False)
a3_c = np.array(v3_c, copy=False)
print("pybind11 version: ", timeit.timeit("a3_c = a1_c + a2_c", globals=globals()))
a1 = np.random.uniform(size=len(a1_c))
a2 = np.random.uniform(size=len(a1_c))
a3 = np.random.uniform(size=len(a1_c))
print("numpy version: ", timeit.timeit("a3 = a1 + a2", globals=globals()))
class Vector {
public:
Vector(size_t size) : m_size(size) { m_data = new double[size]; }
double *data() { return m_data; }
size_t size() const { return m_size; }
private:
size_t m_size;
double *m_data;
};
Vector make_vector() {
std::random_device
rd; // Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd()
std::uniform_real_distribution<> dis(0., 1.);
Vector x(1000);
for (size_t i = 0; i < x.size(); i++) {
x.data()[i] = dis(gen);
}
return x;
}
...
py::class_<Vector>(m, "Vector", py::buffer_protocol())
.def_buffer([](Vector &v) -> py::buffer_info {
return py::buffer_info(
v.data(), /* Pointer to buffer */
sizeof(double), /* Size of one scalar */
py::format_descriptor<double>::format(), /* Python
struct-style format
descriptor */
1, /* Number of dimensions */
{v.size()}, /* Buffer dimensions */
{sizeof(double)});
});
m.def("make_vector", &make_vector, "test");
from bito.
Related Issues (20)
- Top-Pruning: PLVHandler Update
- Top-Pruning: ChoiceMap -- TreeMask Update and Build Trees HOT 3
- parsimony 1: perform Sankoff on trees HOT 2
- parsimony 2 Sankoff on DAG
- Top-Pruning: Top-Pruning Engine & Init on Tree HOT 2
- yet more iteration over unordered maps leading to inconsistent behavior
- TPEngine: Init DAG for Likelihood
- TPEngine: Init DAG for Parsimony
- TPEngine: Update after Add NNI
- TPEngine: Add Rescaling to Likelihood Computations
- TPEngine: Move PVs from Node-based to Edge-based HOT 2
- Interfaces for Evaluation Engines
- Optimization fails after Brent templating
- TPEngine: Assign an edge's neighbors based on "edge heuristic" instead of "subsplit heuristic". HOT 2
- Errors compiling bito with GitHub action and locally
- Rooted trees ignore original taxa labels
- SubsplitDAG: Add map for quick lookup of parent and child nodes.
- DAGStorage throws error where line references a line outside of range.
- Bug: GP Branch lengths are not being updated from default branch length during NNI search.
- Add Dockerfile
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bito.