static PyObject* logical_or_popcount_neon_512_wrap(PyObject* self, PyObject* args) { // get numpy.array([], numpy.uint8) PyArrayObject *image_pyobj, *mask_pyobj; char *image, *mask; PyArray_Descr *descr1, *descr2; npy_intp dims1[3], dims2[3]; int pixels; if (!PyArg_ParseTuple(args, "O!O!i", &PyArray_Type, &image_pyobj, &PyArray_Type, &mask_pyobj, &pixels)){ return NULL; } descr1 = PyArray_DescrFromType(NPY_UINT8); descr2 = PyArray_DescrFromType(NPY_UINT8); if (PyArray_AsCArray((PyObject **) &image_pyobj, (void *)&image, dims1, 1, descr1) < 0 || PyArray_AsCArray((PyObject **) &mask_pyobj, (void *)&mask, dims2, 1, descr2) < 0) { PyErr_SetString(PyExc_TypeError, "error converting to c array"); return NULL; } uint32_t popcnt = logical_or_popcount_neon_512(image, mask, pixels); PyArray_Free((PyObject *) image_pyobj, (void *) image); PyArray_Free((PyObject *) mask_pyobj, (void *) mask); return Py_BuildValue("i", popcnt); }
static PyObject* dtw_extension(PyObject *dummy, PyObject *args) { PyObject *arg1 = NULL; PyObject *arr1 = NULL; PyObject *arg2 = NULL; PyObject *arr2 = NULL; if (!PyArg_ParseTuple(args, "OO", &arg1, &arg2)) { return NULL; } arr1 = PyArray_FROM_OTF(arg1, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY); if (arr1 == NULL) { return NULL; } arr2 = PyArray_FROM_OTF(arg2, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY); if (arr2 == NULL) { return NULL; } // Number of dimensions int nd1 = PyArray_NDIM((PyArrayObject*)arr1); int arr_type1 = PyArray_TYPE((PyArrayObject*)arr1); npy_intp *dims1 = PyArray_DIMS((PyArrayObject*)arr1); int nd2 = PyArray_NDIM((PyArrayObject*)arr2); int arr_type2 = PyArray_TYPE((PyArrayObject*)arr2); npy_intp *dims2 = PyArray_DIMS((PyArrayObject*)arr2); int r = 0; npy_double **input1 = NULL; r = PyArray_AsCArray((PyObject**)&arr1, (void**)&input1, dims1, nd1, PyArray_DescrFromType(arr_type1)); if (r < 0) { PyErr_SetString(PyExc_RuntimeError, "Could not convert input to C array"); return NULL; } npy_double **input2 = NULL; r = PyArray_AsCArray((PyObject**)&arr2, (void**)&input2, dims2, nd2, PyArray_DescrFromType(arr_type2)); if (r < 0) { PyErr_SetString(PyExc_RuntimeError, "Could not convert input to C array"); return NULL; } // DTW matrix_t mat_a = { input1, dims1[0], dims1[1] }; matrix_t mat_b = { input2, dims2[0], dims2[1] }; double dist = dtw_distance(&mat_a, &mat_b); PyObject *value = PyFloat_FromDouble(dist); if (value == NULL) { PyErr_SetString(PyExc_RuntimeError, "Could not convert double to object"); return NULL; } Py_DECREF(arr1); Py_DECREF(arr2); PyArray_Free(arr1, (void*)input1); PyArray_Free(arr2, (void*)input2); return value; }
PyObject * fftpack_rfftb(PyObject *NPY_UNUSED(self), PyObject *args) { PyObject *op1, *op2; PyArrayObject *data, *ret; PyArray_Descr *descr; double *wsave, *dptr, *rptr; npy_intp nsave; int npts, nrepeats, i; if(!PyArg_ParseTuple(args, "OO", &op1, &op2)) { return NULL; } data = (PyArrayObject *)PyArray_ContiguousFromObject(op1, NPY_CDOUBLE, 1, 0); if (data == NULL) { return NULL; } npts = PyArray_DIM(data, PyArray_NDIM(data) - 1); ret = (PyArrayObject *)PyArray_Zeros(PyArray_NDIM(data), PyArray_DIMS(data), PyArray_DescrFromType(NPY_DOUBLE), 0); descr = PyArray_DescrFromType(NPY_DOUBLE); if (PyArray_AsCArray(&op2, (void *)&wsave, &nsave, 1, descr) == -1) { goto fail; } if (data == NULL || ret == NULL) { goto fail; } if (nsave != npts*2 + 15) { PyErr_SetString(ErrorObject, "invalid work array for fft size"); goto fail; } nrepeats = PyArray_SIZE(ret)/npts; rptr = (double *)PyArray_DATA(ret); dptr = (double *)PyArray_DATA(data); NPY_SIGINT_ON; for (i = 0; i < nrepeats; i++) { memcpy((char *)(rptr + 1), (dptr + 2), (npts - 1)*sizeof(double)); rptr[0] = dptr[0]; rfftb(npts, rptr, wsave); rptr += npts; dptr += npts*2; } NPY_SIGINT_OFF; PyArray_Free(op2, (char *)wsave); Py_DECREF(data); return (PyObject *)ret; fail: PyArray_Free(op2, (char *)wsave); Py_XDECREF(data); Py_XDECREF(ret); return NULL; }
PyObject * fftpack_cfftb(PyObject *NPY_UNUSED(self), PyObject *args) { PyObject *op1, *op2; PyArrayObject *data; PyArray_Descr *descr; double *wsave, *dptr; npy_intp nsave; int npts, nrepeats, i; if(!PyArg_ParseTuple(args, "OO", &op1, &op2)) { return NULL; } data = (PyArrayObject *)PyArray_CopyFromObject(op1, NPY_CDOUBLE, 1, 0); if (data == NULL) { return NULL; } descr = PyArray_DescrFromType(NPY_DOUBLE); if (PyArray_AsCArray(&op2, (void *)&wsave, &nsave, 1, descr) == -1) { goto fail; } if (data == NULL) { goto fail; } npts = PyArray_DIM(data, PyArray_NDIM(data) - 1); if (nsave != npts*4 + 15) { PyErr_SetString(ErrorObject, "invalid work array for fft size"); goto fail; } nrepeats = PyArray_SIZE(data)/npts; dptr = (double *)PyArray_DATA(data); NPY_SIGINT_ON; for (i = 0; i < nrepeats; i++) { cfftb(npts, dptr, wsave); dptr += npts*2; } NPY_SIGINT_OFF; PyArray_Free(op2, (char *)wsave); return (PyObject *)data; fail: PyArray_Free(op2, (char *)wsave); Py_DECREF(data); return NULL; }
/** * * calculate_energy lgscore C implementation * **/ static PyObject * clgscore_calculate_energy(PyObject *self, PyObject *args) { PyObject *receptor_coordinates, *ligand_coordinates = NULL; PyObject *tmp0, *tmp1 = NULL; PyArrayObject *rec_charges, *lig_charges, *rec_vdw, *lig_vdw, *rec_vdw_radii, *lig_vdw_radii = NULL; PyArrayObject *rec_hydrogens, *lig_hydrogens, *rec_asa, *lig_asa, *rec_des_energy, *lig_des_energy = NULL; double atom_elec, total_elec, total_vdw, total_solvation, vdw_energy, vdw_radius, p6, k; unsigned int rec_len, lig_len, i, j, interface_len, intf_array_size, *interface_receptor, *interface_ligand; double **rec_array, **lig_array, x, y, z, distance2, interface_cutoff, interface_cutoff2; npy_intp dims[2]; double *rec_c_charges, *lig_c_charges, *rec_c_vdw, *lig_c_vdw, *rec_c_vdw_radii, *lig_c_vdw_radii = NULL; double *rec_c_asa, *lig_c_asa, *rec_c_des_energy, *lig_c_des_energy = NULL; unsigned int *rec_c_hydrogens, *lig_c_hydrogens = NULL; double *min_rec_distance, *min_lig_distance = NULL; PyObject *result = PyTuple_New(5); interface_receptor = interface_ligand = NULL; total_elec = 0.0; atom_elec = 0.0; total_vdw = 0.0; total_solvation = 0.0; interface_cutoff = 3.9; interface_len = 0; intf_array_size = 1; if (PyArg_ParseTuple(args, "OOOOOOOOOOOOOO|d", &receptor_coordinates, &ligand_coordinates, &rec_charges, &lig_charges, &rec_vdw, &lig_vdw, &rec_vdw_radii, &lig_vdw_radii, &rec_hydrogens, &lig_hydrogens, &rec_asa, &lig_asa, &rec_des_energy, &lig_des_energy, &interface_cutoff)) { interface_cutoff2 = interface_cutoff*interface_cutoff; tmp0 = PyObject_GetAttrString(receptor_coordinates, "coordinates"); tmp1 = PyObject_GetAttrString(ligand_coordinates, "coordinates"); rec_len = PySequence_Size(tmp0); lig_len = PySequence_Size(tmp1); dims[1] = 3; dims[0] = rec_len; PyArray_AsCArray((PyObject **)&tmp0, (void **)&rec_array, dims, 2, PyArray_DescrFromType(NPY_DOUBLE)); dims[0] = lig_len; PyArray_AsCArray((PyObject **)&tmp1, (void **)&lig_array, dims, 2, PyArray_DescrFromType(NPY_DOUBLE)); // Get pointers to the Python array structures rec_c_charges = PyArray_GETPTR1(rec_charges, 0); lig_c_charges = PyArray_GETPTR1(lig_charges, 0); rec_c_vdw = PyArray_GETPTR1(rec_vdw, 0); lig_c_vdw = PyArray_GETPTR1(lig_vdw, 0); rec_c_vdw_radii = PyArray_GETPTR1(rec_vdw_radii, 0); lig_c_vdw_radii = PyArray_GETPTR1(lig_vdw_radii, 0); rec_c_hydrogens = PyArray_GETPTR1(rec_hydrogens, 0); lig_c_hydrogens = PyArray_GETPTR1(lig_hydrogens, 0); rec_c_asa = PyArray_GETPTR1(rec_asa, 0); lig_c_asa = PyArray_GETPTR1(lig_asa, 0); rec_c_des_energy = PyArray_GETPTR1(rec_des_energy, 0); lig_c_des_energy = PyArray_GETPTR1(lig_des_energy, 0); // Structures to store the atom at minimal distance of a given atom min_rec_distance = malloc(rec_len*sizeof(double)); min_lig_distance = malloc(lig_len*sizeof(double)); interface_receptor = malloc(lig_len*sizeof(unsigned int)); interface_ligand = malloc(lig_len*sizeof(unsigned int)); for (i = 0; i < rec_len; i++) min_rec_distance[i] = HUGE_DISTANCE; for (j = 0; j < lig_len; j++) min_lig_distance[j] = HUGE_DISTANCE; // For all atoms in receptor for (i = 0; i < rec_len; i++) { // For all atoms in ligand for (j = 0; j < lig_len; j++) { // Euclidean^2 distance x = rec_array[i][0] - lig_array[j][0]; y = rec_array[i][1] - lig_array[j][1]; z = rec_array[i][2] - lig_array[j][2]; distance2 = x*x + y*y + z*z; // Electrostatics energy if (distance2 <= ELEC_DIST_CUTOFF2) { atom_elec = (rec_c_charges[i] * lig_c_charges[j]) / distance2; if (atom_elec >= (MAX_ES_CUTOFF*EPSILON/FACTOR)) atom_elec = MAX_ES_CUTOFF*EPSILON/FACTOR; if (atom_elec <= (MIN_ES_CUTOFF*EPSILON/FACTOR)) atom_elec = MIN_ES_CUTOFF*EPSILON/FACTOR; total_elec += atom_elec; } // Van der Waals energy if (distance2 <= VDW_DIST_CUTOFF2) { vdw_energy = sqrt(rec_c_vdw[i] * lig_c_vdw[j]); vdw_radius = rec_c_vdw_radii[i] + lig_c_vdw_radii[j]; p6 = pow(vdw_radius, 6) / pow(distance2, 3); k = vdw_energy * (p6*p6 - 2.0 * p6); if (k > VDW_CUTOFF) k = VDW_CUTOFF; total_vdw += k; } // Desolvation energy if (distance2 <= SOLVATION_DISTANCE2) { total_solvation += rec_c_des_energy[i] + lig_c_des_energy[j]; } if (distance2 <= interface_cutoff2) { interface_receptor[interface_len] = i; interface_ligand[interface_len++] = j; } } if (((interface_len + lig_len - 1)/lig_len + 1) > intf_array_size) { intf_array_size++; interface_receptor = realloc(interface_receptor, intf_array_size*lig_len*sizeof(unsigned int)); interface_ligand = realloc(interface_ligand, intf_array_size*lig_len*sizeof(unsigned int)); } } // Convert total electrostatics to Kcal/mol: // - coordinates are in Ang // - charges are in e (elementary charge units) total_elec = total_elec * FACTOR / EPSILON; // Free structures PyArray_Free(tmp0, rec_array); PyArray_Free(tmp1, lig_array); Py_DECREF(tmp0); Py_DECREF(tmp1); free(min_rec_distance); free(min_lig_distance); } if (interface_receptor != NULL) interface_receptor = realloc(interface_receptor, interface_len*sizeof(unsigned int)); if (interface_ligand != NULL) interface_ligand = realloc(interface_ligand, interface_len*sizeof(unsigned int)); dims[0] = interface_len; // Return a tuple with the following values for calculated energies: PyTuple_SetItem(result, 0, PyFloat_FromDouble(total_elec)); PyTuple_SetItem(result, 1, PyFloat_FromDouble(total_vdw)); PyTuple_SetItem(result, 2, PyFloat_FromDouble(total_solvation)); PyTuple_SetItem(result, 3, PyArray_SimpleNewFromData(1, dims, NPY_UINT, interface_receptor)); PyTuple_SetItem(result, 4, PyArray_SimpleNewFromData(1, dims, NPY_UINT, interface_ligand)); return result; }
PyObject * fftpack_rfftf(PyObject *NPY_UNUSED(self), PyObject *args) { PyObject *op1, *op2; PyArrayObject *data, *ret; PyArray_Descr *descr; double *wsave, *dptr, *rptr; npy_intp nsave; int npts, nrepeats, i, rstep; if(!PyArg_ParseTuple(args, "OO", &op1, &op2)) { return NULL; } data = (PyArrayObject *)PyArray_ContiguousFromObject(op1, PyArray_DOUBLE, 1, 0); if (data == NULL) { return NULL; } npts = data->dimensions[data->nd-1]; data->dimensions[data->nd - 1] = npts/2 + 1; ret = (PyArrayObject *)PyArray_Zeros(data->nd, data->dimensions, PyArray_DescrFromType(PyArray_CDOUBLE), 0); data->dimensions[data->nd - 1] = npts; rstep = (ret->dimensions[ret->nd - 1])*2; descr = PyArray_DescrFromType(PyArray_DOUBLE); if (PyArray_AsCArray(&op2, (void *)&wsave, &nsave, 1, descr) == -1) { goto fail; } if (data == NULL || ret == NULL) { goto fail; } if (nsave != npts*2+15) { PyErr_SetString(ErrorObject, "invalid work array for fft size"); goto fail; } nrepeats = PyArray_SIZE(data)/npts; rptr = (double *)ret->data; dptr = (double *)data->data; NPY_SIGINT_ON; for (i = 0; i < nrepeats; i++) { memcpy((char *)(rptr+1), dptr, npts*sizeof(double)); rfftf(npts, rptr+1, wsave); rptr[0] = rptr[1]; rptr[1] = 0.0; rptr += rstep; dptr += npts; } NPY_SIGINT_OFF; PyArray_Free(op2, (char *)wsave); Py_DECREF(data); return (PyObject *)ret; fail: PyArray_Free(op2, (char *)wsave); Py_XDECREF(data); Py_XDECREF(ret); return NULL; }