File: hkl.cpp

package info (click to toggle)
gemmi 0.6.5%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 5,836 kB
  • sloc: cpp: 54,719; python: 4,743; ansic: 3,972; sh: 384; makefile: 73; f90: 42; javascript: 12
file content (369 lines) | stat: -rw-r--r-- 18,024 bytes parent folder | download
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright 2019 Global Phasing Ltd.

#include "gemmi/unitcell.hpp"
#include "gemmi/refln.hpp"
#include "gemmi/fourier.hpp"  // for get_size_for_hkl, get_f_phi_on_grid, ...
#include "tostr.hpp"
#include "gemmi/fprime.hpp"   // for cromer_liberman
#include "gemmi/reciproc.hpp" // for count_reflections, make_miller_vector
#include "gemmi/cif2mtz.hpp"  // for CifToMtz
#include "gemmi/mtz2cif.hpp"  // for MtzToCif
#include "gemmi/intensit.hpp" // for Intensities
#include "gemmi/binner.hpp"   // for Binner
#include "gemmi/ecalc.hpp"    // for calculate_amplitude_normalizers

#include "common.h"
#include "arrvec.h"  // py_array_from_vector
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
#include <pybind11/numpy.h>

namespace py = pybind11;
using namespace gemmi;

PYBIND11_MAKE_OPAQUE(std::vector<ReflnBlock>)

namespace gemmi {
  // operator<< is used by stl_bind for vector's __repr__
  inline std::ostream& operator<< (std::ostream& os, const ReflnBlock& rb) {
    os << "<gemmi.ReflnBlock " << rb.block.name << " with ";
    if (rb.default_loop)
      os << rb.default_loop->width() << " x " << rb.default_loop->length();
    else
      os << " no ";
    os << " loop>";
    return os;
  }
}

void add_hkl(py::module& m) {
  py::class_<ReflnBlock> pyReflnBlock(m, "ReflnBlock");
  py::bind_vector<std::vector<ReflnBlock>>(m, "ReflnBlocks");

  pyReflnBlock
    .def_readonly("block", &ReflnBlock::block)
    .def_readonly("entry_id", &ReflnBlock::entry_id)
    .def_readonly("cell", &ReflnBlock::cell)
    .def_readonly("spacegroup", &ReflnBlock::spacegroup)
    .def_readonly("wavelength", &ReflnBlock::wavelength)
    .def_readonly("default_loop", &ReflnBlock::default_loop)
    .def("column_labels", &ReflnBlock::column_labels)
    .def("make_int_array",
         [](ReflnBlock& self, const std::string& tag, int null) {
           return py_array_from_vector(self.make_vector(tag, null));
    }, py::arg("tag"), py::arg("null"))
    .def("make_float_array",
         [](ReflnBlock& self, const std::string& tag, double null) {
           return py_array_from_vector(self.make_vector(tag, null));
    }, py::arg("tag"), py::arg("null")=NAN)
    .def("make_miller_array", [](ReflnBlock& self) {
        return py::array_t<int>(py::cast(self.make_miller_vector()));
    })
    .def("make_1_d2_array", [](ReflnBlock& self) {
        return py_array_from_vector(self.make_1_d2_vector());
    })
    .def("make_d_array", [](ReflnBlock& self) {
        return py_array_from_vector(self.make_d_vector());
    })
    .def("get_size_for_hkl",
         [](const ReflnBlock& self,
            std::array<int,3> min_size, double sample_rate) {
          return get_size_for_hkl(ReflnDataProxy(self), min_size, sample_rate);
    }, py::arg("min_size")=std::array<int,3>{{0,0,0}},
       py::arg("sample_rate")=0.)
    .def("data_fits_into", [](const ReflnBlock& self, std::array<int,3> size) {
        return data_fits_into(ReflnDataProxy(self), size);
    }, py::arg("size"))
    .def("get_f_phi_on_grid", [](const ReflnBlock& self,
                                 const std::string& f_col,
                                 const std::string& phi_col,
                                 std::array<int, 3> size,
                                 bool half_l, AxisOrder order) {
        size_t f_idx = self.get_column_index(f_col);
        size_t phi_idx = self.get_column_index(phi_col);
        FPhiProxy<ReflnDataProxy> fphi(ReflnDataProxy{self}, f_idx, phi_idx);
        return get_f_phi_on_grid<float>(fphi, size, half_l, order);
    }, py::arg("f"), py::arg("phi"), py::arg("size"),
       py::arg("half_l")=false, py::arg("order")=AxisOrder::XYZ)
    .def("get_value_on_grid", [](const ReflnBlock& self,
                                 const std::string& column,
                                 std::array<int, 3> size,
                                 bool half_l, AxisOrder order) {
        size_t col_idx = self.get_column_index(column);
        return get_value_on_grid<float>(ReflnDataProxy(self), col_idx,
                                        size, half_l, order);
    }, py::arg("column"), py::arg("size"), py::arg("half_l")=false,
       py::arg("order")=AxisOrder::XYZ)
    .def("transform_f_phi_to_map", [](const ReflnBlock& self,
                                      const std::string& f_col,
                                      const std::string& phi_col,
                                      std::array<int, 3> min_size,
                                      std::array<int, 3> exact_size,
                                      double sample_rate,
                                      AxisOrder order) {
        size_t f_idx = self.get_column_index(f_col);
        size_t phi_idx = self.get_column_index(phi_col);
        FPhiProxy<ReflnDataProxy> fphi(ReflnDataProxy{self}, f_idx, phi_idx);
        return transform_f_phi_to_map2<float>(fphi, min_size, sample_rate,
                                              exact_size, order);
    }, py::arg("f"), py::arg("phi"),
       py::arg("min_size")=std::array<int,3>{{0,0,0}},
       py::arg("exact_size")=std::array<int,3>{{0,0,0}},
       py::arg("sample_rate")=0.,
       py::arg("order")=AxisOrder::XYZ)
    .def("get_float", &make_asu_data<float, ReflnBlock>,
         py::arg("col"), py::arg("as_is")=false)
    .def("get_int", &make_asu_data<int, ReflnBlock>,
         py::arg("col"), py::arg("as_is")=false)
    .def("get_f_phi", [](const ReflnBlock& self, const std::string& f_col,
                                                 const std::string& phi_col,
                                                 bool as_is) {
        return make_asu_data<std::complex<float>, 2>(self, {f_col, phi_col}, as_is);
    }, py::arg("f"), py::arg("phi"), py::arg("as_is")=false)
    .def("get_value_sigma", [](const ReflnBlock& self, const std::string& f_col,
                                                       const std::string& sigma_col,
                                                       bool as_is) {
        return make_asu_data<ValueSigma<float>, 2>(self, {f_col, sigma_col}, as_is);
    }, py::arg("f"), py::arg("sigma"), py::arg("as_is")=false)
    .def("is_unmerged", &ReflnBlock::is_unmerged)
    .def("use_unmerged", &ReflnBlock::use_unmerged)
    .def("__bool__", [](const ReflnBlock& self) { return self.ok(); })
    .def("__repr__", [](const ReflnBlock& self) { return tostr(self); })
    ;
  m.def("as_refln_blocks",
        [](cif::Document& d) { return as_refln_blocks(std::move(d.blocks)); });
  m.def("hkl_cif_as_refln_block", &hkl_cif_as_refln_block, py::arg("block"));
  m.def("transform_f_phi_grid_to_map", [](FPhiGrid<float> grid) {
          return transform_f_phi_grid_to_map<float>(std::move(grid));
        }, py::arg("grid"));
  m.def("transform_map_to_f_phi", &transform_map_to_f_phi<float>,
        py::arg("map"), py::arg("half_l")=false, py::arg("use_scale")=true);
  m.def("cromer_liberman", [](int z, double energy) {
          std::pair<double, double> r;
          r.first = cromer_liberman(z, energy, &r.second);
          return r;
        }, py::arg("z"), py::arg("energy"));
  m.def("count_reflections", &count_reflections,
        py::arg("cell"), py::arg("spacegroup"), py::arg("dmin"),
        py::arg("dmax")=0., py::arg("unique")=true);
  m.def("make_miller_array", [](const UnitCell& cell, const SpaceGroup* sg,
                                double dmin, double dmax, bool unique) {
          return py::array_t<int>(py::cast(
                      gemmi::make_miller_vector(cell, sg, dmin, dmax, unique)));
        }, py::arg("cell"), py::arg("spacegroup"), py::arg("dmin"),
           py::arg("dmax")=0., py::arg("unique")=true);

  py::class_<CifToMtz>(m, "CifToMtz")
    .def(py::init<>())
    .def_readwrite("title", &CifToMtz::title)
    .def_readwrite("history", &CifToMtz::history)
    .def_readwrite("spec_lines", &CifToMtz::spec_lines)
    .def("convert_block_to_mtz", [](const CifToMtz& self, const ReflnBlock& rb) {
        std::ostringstream out;
        return new Mtz(self.convert_block_to_mtz(rb, out));
    })
    ;

  py::class_<MtzToCif>(m, "MtzToCif")
    .def(py::init<>())
    .def_readwrite("spec_lines", &MtzToCif::spec_lines)
    .def_readwrite("with_comments", &MtzToCif::with_comments)
    .def_readwrite("with_history", &MtzToCif::with_history)
    .def_readwrite("skip_empty", &MtzToCif::skip_empty)
    .def_readwrite("skip_negative_sigi", &MtzToCif::skip_negative_sigi)
    .def_readwrite("wavelength", &MtzToCif::wavelength)
    .def_readwrite("free_flag_value", &MtzToCif::free_flag_value)
    .def("write_cif_to_string", [](MtzToCif& self, const Mtz& mtz, const Mtz* mtz2) {
        std::ostringstream out;
        self.write_cif(mtz, mtz2, nullptr, out);
        return out.str();
    }, py::arg("mtz"), py::arg("mtz2")=nullptr)
    ;

  py::enum_<DataType>(m, "DataType")
    .value("Unknown", DataType::Unknown)
    .value("Unmerged", DataType::Unmerged)
    .value("Mean", DataType::Mean)
    .value("Anomalous", DataType::Anomalous)
    ;
  m.def("check_data_type_under_symmetry", [](const ReflnBlock& data) {
      return check_data_type_under_symmetry(ReflnDataProxy(data));
  });
  m.def("check_data_type_under_symmetry", [](const Mtz& data) {
      return check_data_type_under_symmetry(MtzDataProxy{data});
  });

  py::class_<Intensities>(m, "Intensities")
    .def(py::init<>())
    .def_readwrite("spacegroup", &Intensities::spacegroup)
    .def_readwrite("unit_cell", &Intensities::unit_cell)
    .def_readwrite("type", &Intensities::type)
    .def("resolution_range", &Intensities::resolution_range)
    .def("remove_systematic_absences", &Intensities::remove_systematic_absences)
    .def("merge_in_place", &Intensities::merge_in_place, py::arg("itype"))
    .def("read_mtz", &Intensities::read_mtz, py::arg("mtz"), py::arg("type"))
    .def("prepare_merged_mtz", &Intensities::prepare_merged_mtz,
         py::arg("with_nobs"))
    .def_property_readonly("miller_array", [](const Intensities& self) {
      const Intensities::Refl* data = self.data.data();
      py::array::ShapeContainer shape({(py::ssize_t)self.data.size(), 3});
      py::array::StridesContainer strides({(const char*)(data+1) - (const char*)data,
                                           sizeof(int)});
      return py::array_t<int>(shape, strides, &data->hkl[0], py::cast(self));
    }, py::return_value_policy::reference_internal)
    .def_property_readonly("value_array", [](const Intensities& self) {
      const Intensities::Refl* data = self.data.data();
      py::ssize_t stride = (const char*)(data+1) - (const char*)data;
      return py::array_t<double>({(py::ssize_t)self.data.size()}, {stride},
                                 &data->value, py::cast(self));
    }, py::return_value_policy::reference_internal)
    .def_property_readonly("sigma_array", [](const Intensities& self) {
      const Intensities::Refl* data = self.data.data();
      py::ssize_t stride = (const char*)(data+1) - (const char*)data;
      return py::array_t<double>({(py::ssize_t)self.data.size()}, {stride},
                                 &data->sigma, py::cast(self));
    }, py::return_value_policy::reference_internal)
    .def_property_readonly("nobs_array", [](const Intensities& self) {
      const Intensities::Refl* data = self.data.data();
      py::ssize_t stride = (const char*)(data+1) - (const char*)data;
      return py::array_t<short>({(py::ssize_t)self.data.size()}, {stride},
                                &data->nobs, py::cast(self));
    }, py::return_value_policy::reference_internal)
    .def_property_readonly("isign_array", [](const Intensities& self) {
      const Intensities::Refl* data = self.data.data();
      py::ssize_t stride = (const char*)(data+1) - (const char*)data;
      return py::array_t<short>({(py::ssize_t)self.data.size()}, {stride},
                                &data->isign, py::cast(self));
    }, py::return_value_policy::reference_internal)
    .def("set_data", [](Intensities& self, const UnitCell& unit_cell,
                        const SpaceGroup* sg, py::array_t<int> hkl,
                        py::array_t<double> values, py::array_t<double> sigmas) {
      auto h = hkl.unchecked<2>();
      if (h.shape(1) != 3)
        throw std::domain_error("the hkl array must have size N x 3");
      auto v = values.template unchecked<1>();
      auto s = sigmas.template unchecked<1>();
      if (h.shape(0) != v.shape(0) || h.shape(0) != s.shape(0))
        throw std::domain_error("arrays have different lengths");

      self.unit_cell = unit_cell;
      self.spacegroup = sg;
      self.data.reserve(h.shape(0));
      for (py::ssize_t i = 0; i < h.shape(0); ++i)
        if (!std::isnan(v(i)) && s(i) > 0)
          self.data.push_back({{{h(i, 0), h(i, 1), h(i, 2)}}, 1, 0, v(i), s(i)});

      self.switch_to_asu_indices();
      self.type = DataType::Unmerged;
    }, py::arg("cell"), py::arg("sg").none(false),
       py::arg("miller_array"), py::arg("value_array"), py::arg("sigma_array"))
    ;

  py::class_<Binner> binner(m, "Binner");
  py::enum_<Binner::Method>(binner, "Method")
      .value("EqualCount", Binner::Method::EqualCount)
      .value("Dstar", Binner::Method::Dstar)
      .value("Dstar2", Binner::Method::Dstar2)
      .value("Dstar3", Binner::Method::Dstar3)
      ;
  binner
    .def(py::init<>())
    .def("setup", [](Binner& self, int nbins, Binner::Method method,
                     const Mtz& mtz, const UnitCell* cell) {
        self.setup(nbins, method, MtzDataProxy{mtz}, cell);
    }, py::arg("nbins"), py::arg("method"), py::arg("mtz"), py::arg("cell")=nullptr)
    .def("setup", [](Binner& self, int nbins, Binner::Method method,
                     const ReflnBlock& r, const UnitCell* cell) {
        self.setup(nbins, method, ReflnDataProxy(r), cell);
    }, py::arg("nbins"), py::arg("method"), py::arg("r"), py::arg("cell")=nullptr)
    .def("setup", [](Binner& self, int nbins, Binner::Method method,
                     py::array_t<int> hkl, const UnitCell* cell) {
        auto h = hkl.unchecked<2>();
        if (h.shape(1) != 3)
          throw std::domain_error("the hkl array must have size N x 3");
        std::vector<double> inv_d2(h.shape(0));
        if (cell)
          for (size_t i = 0; i < inv_d2.size(); ++i)
            inv_d2[i] = cell->calculate_1_d2_double(h(i, 0), h(i, 1), h(i, 2));
        self.setup_from_1_d2(nbins, method, std::move(inv_d2), cell);
    }, py::arg("nbins"), py::arg("method"), py::arg("hkl"), py::arg("cell"))
    .def("setup_from_1_d2", [](Binner& self, int nbins, Binner::Method method,
                               py::array_t<double> inv_d2, const UnitCell* cell) {
        double* ptr = (double*) inv_d2.request().ptr;
        auto len = inv_d2.shape(0);
        self.setup_from_1_d2(nbins, method, std::vector<double>(ptr, ptr+len), cell);
    }, py::arg("nbins"), py::arg("method"), py::arg("inv_d2"), py::arg("cell"))
    .def("get_bin", &Binner::get_bin)
    .def("get_bins", [](Binner& self, const Mtz& mtz) {
        return py_array_from_vector(self.get_bins(MtzDataProxy{mtz}));
    })
    .def("get_bins", [](Binner& self, const ReflnBlock& r) {
        return py_array_from_vector(self.get_bins(ReflnDataProxy(r)));
    })
    .def("get_bins", [](Binner& self, py::array_t<int> hkl) {
        self.ensure_limits_are_set();
        auto h = hkl.unchecked<2>();
        if (h.shape(1) != 3)
          throw std::domain_error("the hkl array must have size N x 3");
        auto len = h.shape(0);
        int hint = 0;
        py::array_t<int> arr(len);
        int* ptr = (int*) arr.request().ptr;
        for (int i = 0; i < len; ++i)
          ptr[i] = self.get_bin_hinted({{h(i, 0), h(i, 1), h(i, 2)}}, hint);
        return arr;
    })
    .def("get_bins_from_1_d2", [](Binner& self, py::array_t<double> inv_d2) {
        self.ensure_limits_are_set();
        auto v = inv_d2.unchecked<1>();
        auto len = v.shape(0);
        int hint = 0;
        py::array_t<int> arr(len);
        int* ptr = (int*) arr.request().ptr;
        for (int i = 0; i < len; ++i)
          ptr[i] = self.get_bin_from_1_d2_hinted(v(i), hint);
        return arr;
    })
    .def("dmin_of_bin", &Binner::dmin_of_bin)
    .def("dmax_of_bin", &Binner::dmax_of_bin)
    .def_property_readonly("size", &Binner::size)
    .def_readonly("limits", &Binner::limits)
    .def_readwrite("cell", &Binner::cell)
    .def_readonly("min_1_d2", &Binner::min_1_d2)
    .def_readonly("max_1_d2", &Binner::max_1_d2)
    ;

  m.def("combine_correlations", &combine_correlations);

  m.def("calculate_amplitude_normalizers",
        [](const Mtz& mtz, const std::string& f_col, const Binner& binner) {
      const Mtz::Column& f = mtz.get_column_with_label(f_col);
      return py_array_from_vector(
          calculate_amplitude_normalizers(MtzDataProxy{mtz}, f.idx, binner));
  });

  py::class_<HklMatch>(m, "HklMatch")
    .def(py::init([](py::array_t<int, py::array::c_style> hkl,
                     py::array_t<int, py::array::c_style> ref) {
        auto hkl_ = hkl.unchecked<2>();
        if (hkl_.shape(1) != 3)
          throw std::domain_error("the hkl array must have size N x 3");
        auto ref_ = ref.unchecked<2>();
        if (ref_.shape(1) != 3)
          throw std::domain_error("the ref array must have size N x 3");
        return new HklMatch((Miller*)hkl.request().ptr, hkl.shape(0),
                            (Miller*)ref.request().ptr, ref.shape(0));
    }), py::arg("hkl"), py::arg("ref"))
    .def("aligned", [](HklMatch& self, py::array_t<double> vec) {
        auto v = vec.unchecked<1>();
        if (v.shape(0) != (py::ssize_t) self.hkl_size)
          fail("HklMatch.aligned(): wrong data, size differs");
        py::array_t<double> result(self.pos.size());
        double* ptr = (double*) result.request().ptr;
        for (size_t i = 0; i != self.pos.size(); ++i)
          ptr[i] = self.pos[i] >= 0 ? v(self.pos[i]) : NAN;
        return result;
    })
    .def_readonly("pos", &HklMatch::pos)
    ;
}