File size: 11,240 Bytes
3e62986
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Copyright 2024 DeepMind Technologies Limited
//
// AlphaFold 3 source code is licensed under CC BY-NC-SA 4.0. To view a copy of
// this license, visit https://creativecommons.org/licenses/by-nc-sa/4.0/
//
// To request access to the AlphaFold 3 model parameters, follow the process set
// out at https://github.com/google-deepmind/alphafold3. You may only use these
// if received directly from Google. Use is subject to terms of use available at
// https://github.com/google-deepmind/alphafold3/blob/main/WEIGHTS_TERMS_OF_USE.md

#include <Python.h>

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <iterator>
#include <limits>
#include <string>
#include <vector>

#include "numpy/arrayobject.h"
#include "numpy/ndarrayobject.h"
#include "numpy/ndarraytypes.h"
#include "numpy/npy_common.h"
#include "absl/algorithm/container.h"
#include "absl/container/flat_hash_set.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "pybind11/cast.h"
#include "pybind11/numpy.h"
#include "pybind11/pybind11.h"
#include "pybind11/pytypes.h"
#include "pybind11_abseil/absl_casters.h"

namespace {

namespace py = pybind11;

PyObject* RemapNumpyArrayObjects(PyObject* array, PyObject* mapping,
                                 bool inplace, PyObject* default_value) {
  import_array();
  if (!PyArray_Check(array)) {
    PyErr_SetString(PyExc_TypeError, "'array' must be a np.ndarray.");
    return nullptr;
  }
  if (!PyDict_Check(mapping)) {
    PyErr_SetString(PyExc_TypeError, "'mapping' must be a Python dict.");
    return nullptr;
  }

  PyArrayObject* array_obj = reinterpret_cast<PyArrayObject*>(array);
  if (PyArray_TYPE(array_obj) != NPY_OBJECT) {
    PyErr_SetString(PyExc_TypeError, "`array` must be an array of objects.");
    return nullptr;
  }

  if (inplace) {
    // We are returning original array so we need to increase the ref count.
    Py_INCREF(array);
  } else {
    // We are returning a fresh copy.
    array = PyArray_NewCopy(array_obj, NPY_CORDER);
    if (array == nullptr) {
      PyErr_SetString(PyExc_MemoryError, "Out of memory!");
      return nullptr;
    }
    array_obj = reinterpret_cast<PyArrayObject*>(array);
  }

  if (PyArray_SIZE(array_obj) == 0) {
    return array;
  }

  if (default_value == nullptr && PyDict_Size(mapping) == 0) {
    return array;
  }

  NpyIter* iter = NpyIter_New(
      array_obj, NPY_ITER_READWRITE | NPY_ITER_EXTERNAL_LOOP | NPY_ITER_REFS_OK,
      NPY_KEEPORDER, NPY_NO_CASTING, nullptr);
  if (iter == nullptr) {
    PyErr_SetString(PyExc_MemoryError, "Out of memory!");
    Py_XDECREF(array);
    return nullptr;
  }

  NpyIter_IterNextFunc* iter_next = NpyIter_GetIterNext(iter, nullptr);
  if (iter_next == nullptr) {
    NpyIter_Deallocate(iter);
    Py_XDECREF(array);
    PyErr_SetString(PyExc_MemoryError, "Out of memory!");
    return nullptr;
  }

  // Iterating arrays taken from:
  // https://numpy.org/doc/stable/reference/c-api/iterator.html
  char** data_pointer = NpyIter_GetDataPtrArray(iter);
  npy_intp* stride_pointer = NpyIter_GetInnerStrideArray(iter);
  npy_intp* inner_size_pointer = NpyIter_GetInnerLoopSizePtr(iter);
  do {
    char* data = *data_pointer;
    npy_intp stride = *stride_pointer;
    npy_intp count = *inner_size_pointer;
    for (size_t i = 0; i < count; ++i) {
      PyObject* entry;
      std::memcpy(&entry, data, sizeof(PyObject*));
      PyObject* result = PyDict_GetItem(mapping, entry);
      if (result != nullptr) {
        // Replace entry.
        Py_INCREF(result);
        Py_XDECREF(entry);
        std::memcpy(data, &result, sizeof(PyObject*));
      } else if (default_value != nullptr) {
        // Replace entry with a default value.
        Py_INCREF(default_value);
        Py_XDECREF(entry);
        std::memcpy(data, &default_value, sizeof(PyObject*));
      }
      data += stride;
    }
  } while (iter_next(iter));

  NpyIter_Deallocate(iter);
  return array;
}

// Convert 1D Numpy float array to a list of strings where each string has fixed
// number of decimal points. This is faster than Python list comprehension.
std::vector<std::string> FormatFloatArray(absl::Span<const float> values,
                                          int num_decimal_places) {
  std::vector<std::string> output;
  output.reserve(values.size());

  absl::c_transform(values, std::back_inserter(output),
                    [num_decimal_places](float value) {
                      return absl::StrFormat("%.*f", num_decimal_places, value);
                    });
  return output;
}

py::array_t<bool> IsIn(
    const py::array_t<PyObject*, py::array::c_style>& array,
    const absl::flat_hash_set<absl::string_view>& test_elements, bool invert) {
  const size_t num_elements = array.size();
  py::array_t<bool> output(num_elements);
  std::fill(output.mutable_data(), output.mutable_data() + output.size(),
            invert);

  // Shortcut: The output will be trivially always false if test_elements empty.
  if (test_elements.empty()) {
    return output;
  }

  for (size_t i = 0; i < num_elements; ++i) {
    // Compare the string values instead of comparing just object pointers.
    py::handle handle = array.data()[i];
    if (!PyUnicode_Check(handle.ptr()) && !PyBytes_Check(handle.ptr())) {
      continue;
    }
    if (test_elements.contains(py::cast<absl::string_view>(handle))) {
      output.mutable_data()[i] = !invert;
    }
  }
  if (array.ndim() > 1) {
    auto shape =
        std::vector<ptrdiff_t>(array.shape(), array.shape() + array.ndim());
    return output.reshape(shape);
  }
  return output;
}

py::array RemapMultipleArrays(
    const std::vector<py::array_t<PyObject*, py::array::c_style>>& arrays,
    const py::dict& mapping) {
  size_t array_size = arrays[0].size();
  for (const auto& array : arrays) {
    if (array.size() != array_size) {
      throw py::value_error("All arrays must have the same length.");
    }
  }

  // Create a result buffer.
  auto result = py::array_t<int64_t>(array_size);
  absl::Span<int64_t> result_buffer(result.mutable_data(), array_size);
  PyObject* entry = PyTuple_New(arrays.size());
  if (entry == nullptr) {
    throw py::error_already_set();
  }
  std::vector<absl::Span<PyObject* const>> array_spans;
  array_spans.reserve(arrays.size());
  for (const auto& array : arrays) {
    array_spans.emplace_back(array.data(), array.size());
  }

  // Iterate over arrays and look up elements in the `py_dict`.
  bool fail = false;
  for (size_t i = 0; i < array_size; ++i) {
    for (size_t j = 0; j < array_spans.size(); ++j) {
      PyTuple_SET_ITEM(entry, j, array_spans[j][i]);
    }
    PyObject* result = PyDict_GetItem(mapping.ptr(), entry);
    if (result != nullptr) {
      int64_t result_value = PyLong_AsLongLong(result);
      if (result_value == -1 && PyErr_Occurred()) {
        fail = true;
        break;
      }
      if (result_value > std::numeric_limits<int64_t>::max() ||
          result_value < std::numeric_limits<int64_t>::lowest()) {
        PyErr_SetString(PyExc_OverflowError, "Result value too large.");
        fail = true;
        break;
      }
      result_buffer[i] = result_value;
    } else {
      PyErr_Format(PyExc_KeyError, "%R", entry);
      fail = true;
      break;
    }
  }

  for (size_t j = 0; j < array_spans.size(); ++j) {
    PyTuple_SET_ITEM(entry, j, nullptr);
  }
  Py_XDECREF(entry);
  if (fail) {
    throw py::error_already_set();
  }
  return result;
}

constexpr char kRemapNumpyArrayObjects[] = R"(
Replace objects in NumPy array of objects using mapping.

Args:
  array: NumPy array with dtype=object.
  mapping: Dict mapping old values to new values.
  inplace: Bool (default False) whether to replace values inplace or to
    create a new array.
  default_value: If given, what value to map to if the mapping is missing
    for that particular item. If not given, such items are left unchanged.

Returns
  NumPy array of dtype object with values replaced according to mapping.
  If inplace is True the original array is modified inplace otherwise a
  new array is returned.
)";

constexpr char kFormatFloatArrayDoc[] = R"(
Converts float -> string array with given number of decimal places.
)";

constexpr char kIsInDoc[] = R"(
Computes whether each element is in test_elements.

Same use as np.isin, but much faster. If len(array) = n, len(test_elements) = m:
* This function has complexity O(n).
* np.isin with arrays of objects has complexity O(m*log(m) + n * log(m)).

Args:
  array: Input NumPy array with dtype=object.
  test_elements: The values against which to test each value of array.
  invert: If True, the values in the returned array are inverted, as if
    calculating `element not in test_elements`. Default is False.
    `isin(a, b, invert=True)` is equivalent to but faster than `~isin(a, b)`.

Returns
  A boolean array of the same shape as the input array. Each value `val` is:
  * `val in test_elements` if `invert=False`,
  * `val not in test_elements` if `invert=True`.
)";

constexpr char kRemapMultipleDoc[] = R"(
Maps keys from multiple aligned arrays to a single array.

Args:
  arrays: Numpy arrays of the same length. The tuple of aligned entries is used
    as key for the mapping.
  mapping: Dict mapping from tuples to integer values.

Returns
  NumPy array of dtype `int` with values looked up in mapping according to the
  tuple of aligned array entries as keys.
)";

}  // namespace

namespace alphafold3 {

void RegisterModuleStringArray(pybind11::module m) {
  m.def(
      "remap",
      [](py::object array, py::object mapping, bool inplace,
         py::object default_value) -> py::object {
        PyObject* result = RemapNumpyArrayObjects(array.ptr(), mapping.ptr(),
                                                  inplace, default_value.ptr());
        if (result == nullptr) {
          throw py::error_already_set();
        }
        return py::reinterpret_steal<py::object>(result);
      },
      py::return_value_policy::take_ownership, py::arg("array"),
      py::arg("mapping"), py::arg("inplace") = false, py::arg("default_value"),
      py::doc(kRemapNumpyArrayObjects + 1));
  m.def(
      "remap",
      [](py::object array, py::object mapping, bool inplace) -> py::object {
        PyObject* result = RemapNumpyArrayObjects(array.ptr(), mapping.ptr(),
                                                  inplace, nullptr);
        if (result == nullptr) {
          throw py::error_already_set();
        }
        return py::reinterpret_steal<py::object>(result);
      },
      py::return_value_policy::take_ownership, py::arg("array"),
      py::arg("mapping"), py::arg("inplace") = false,
      py::doc(kRemapNumpyArrayObjects + 1));
  m.def("format_float_array", &FormatFloatArray, py::arg("values"),
        py::arg("num_decimal_places"), py::doc(kFormatFloatArrayDoc + 1),
        py::call_guard<py::gil_scoped_release>());
  m.def("isin", &IsIn, py::arg("array"), py::arg("test_elements"),
        py::kw_only(), py::arg("invert") = false, py::doc(kIsInDoc + 1));
  m.def("remap_multiple", &RemapMultipleArrays, py::arg("arrays"),
        py::arg("mapping"), py::doc(kRemapMultipleDoc + 1));
}

}  // namespace alphafold3