Skip to content

Interoperating with Numpy

Grossfield Lab edited this page Dec 20, 2019 · 5 revisions

Many scientific programming tools, including scikitlearn and scipy, use NumPy arrays as their fundamental data types. For this reason, pyLOOS provides simple functions to get coordinates in and out of NumPy objects:

import loos
import numpy

molecule = loos.createSystem("filename")
vec = molecule.getCoords()

vec will be a NumPy array of floats, and vec.shape will be (numatoms, 3)

If we wish to translate the molecule, we could do something like

translation = numpy.array([1.0, 0.0, 0.0])
vec += translation
molecule.setCoords(vec)

Of course, you would actually do this using native LOOS:

molecule.translate(loos.GCoord(1., 0., 0.))

However, the intervening code could do something far more interesting (PCA, t-SNE, clustering, whatever). The point is just that you can get to and from Numpy easily.

When called from the C++ layer, getCoords() and setCoords() return pointers to allocated memory for floats, which are similarly useful for interoperating with external libraries.