Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions package/MDAnalysis/lib/trans.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import numpy as np
cimport cython
cimport numpy as np


@cython.boundscheck(False)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can turn this off globally in setup.py file. check my pytraj's one

https://github.com/Amber-MD/pytraj/blob/master/setup.py#L327-L347

@cython.wraparound(False)
def residue_positions(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a doc string would be nice.

float[:, :] coords not None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can tell the compiler how the array is stored (Row or Column Major). That can be done by giving it the strides directly float[:, ::1 is row major for example the C/Python standard. That should give a small boost but requires that the given arrays follow that memory layout.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep your current version as this is less pain for someone who uses the function.

long[:] indices not None):
cdef Py_ssize_t i, j, k
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually use just int here as type.


cdef int n_items = indices.shape[0]
cdef int n_res = np.unique(indices).shape[0]

cdef long[:] counter = np.zeros(n_res, dtype=np.int64)
cdef float[:, :] output = np.zeros((n_res, 3), dtype=np.float32)

for i in range(n_items):
j = indices[i]
for k in range(3):
output[j, k] += coords[i, k]
counter[j] += 1

for i in range(n_res):
for k in range(3):
output[i, k] /= counter[i]

return np.asarray(output)
5 changes: 4 additions & 1 deletion package/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,12 @@ def extensions(config):
util = MDAExtension('lib.formats.cython_util',
sources=['MDAnalysis/lib/formats/cython_util.pyx'],
include_dirs=include_dirs)
trans = MDAExtension('lib.trans',
sources=['MDAnalysis/lib/trans.pyx'],
include_dirs=include_dirs)

extensions = [dcd, dcd_time, distances, distances_omp, qcprot,
transformation, xdrlib, util]
transformation, xdrlib, util, trans]
if use_cython:
extensions = cythonize(extensions)
return extensions
Expand Down