quasardb 3.14.2.dev1__cp311-cp311-macosx_11_0_arm64.whl → 3.14.2.dev4__cp311-cp311-macosx_11_0_arm64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of quasardb might be problematic. Click here for more details.
- quasardb/CMakeFiles/CMakeDirectoryInformation.cmake +1 -1
- quasardb/Makefile +20 -20
- quasardb/__init__.py +33 -4
- quasardb/cmake_install.cmake +7 -1
- quasardb/date/CMakeFiles/CMakeDirectoryInformation.cmake +1 -1
- quasardb/date/CMakeFiles/Export/a52b05f964b070ee926bcad51d3288af/dateTargets.cmake +13 -13
- quasardb/date/Makefile +20 -20
- quasardb/date/cmake_install.cmake +7 -1
- quasardb/date/dateConfigVersion.cmake +9 -2
- quasardb/date/dateTargets.cmake +4 -8
- quasardb/libqdb_api.dylib +0 -0
- quasardb/numpy/__init__.py +58 -10
- quasardb/pandas/__init__.py +58 -102
- quasardb/pybind11/CMakeFiles/CMakeDirectoryInformation.cmake +1 -1
- quasardb/pybind11/Makefile +20 -20
- quasardb/pybind11/cmake_install.cmake +7 -1
- quasardb/quasardb.cpython-311-darwin.so +0 -0
- quasardb/range-v3/CMakeFiles/CMakeDirectoryInformation.cmake +1 -1
- quasardb/range-v3/CMakeFiles/Export/d94ef200eca10a819b5858b33e808f5b/range-v3-targets.cmake +13 -13
- quasardb/range-v3/CMakeFiles/range.v3.headers.dir/DependInfo.cmake +6 -2
- quasardb/range-v3/CMakeFiles/range.v3.headers.dir/build.make +7 -4
- quasardb/range-v3/Makefile +20 -20
- quasardb/range-v3/cmake_install.cmake +19 -1
- quasardb/range-v3/range-v3-config-version.cmake +9 -2
- quasardb/range-v3/range-v3-config.cmake +4 -8
- {quasardb-3.14.2.dev1.dist-info → quasardb-3.14.2.dev4.dist-info}/METADATA +15 -10
- quasardb-3.14.2.dev4.dist-info/RECORD +45 -0
- {quasardb-3.14.2.dev1.dist-info → quasardb-3.14.2.dev4.dist-info}/WHEEL +1 -1
- quasardb/CMakeLists.txt +0 -510
- quasardb/batch_column.hpp +0 -80
- quasardb/batch_inserter.hpp +0 -248
- quasardb/blob.hpp +0 -150
- quasardb/cluster.cpp +0 -89
- quasardb/cluster.hpp +0 -551
- quasardb/concepts.hpp +0 -278
- quasardb/continuous.cpp +0 -149
- quasardb/continuous.hpp +0 -106
- quasardb/convert/array.hpp +0 -282
- quasardb/convert/point.hpp +0 -330
- quasardb/convert/range.hpp +0 -282
- quasardb/convert/unicode.hpp +0 -598
- quasardb/convert/util.hpp +0 -22
- quasardb/convert/value.hpp +0 -711
- quasardb/convert.hpp +0 -38
- quasardb/detail/qdb_resource.hpp +0 -129
- quasardb/detail/ts_column.hpp +0 -224
- quasardb/direct_blob.hpp +0 -108
- quasardb/direct_handle.hpp +0 -83
- quasardb/direct_integer.hpp +0 -94
- quasardb/dispatch.hpp +0 -157
- quasardb/double.hpp +0 -87
- quasardb/entry.hpp +0 -273
- quasardb/error.hpp +0 -318
- quasardb/handle.cpp +0 -29
- quasardb/handle.hpp +0 -98
- quasardb/integer.hpp +0 -88
- quasardb/logger.cpp +0 -106
- quasardb/logger.hpp +0 -228
- quasardb/masked_array.hpp +0 -651
- quasardb/metrics.cpp +0 -103
- quasardb/metrics.hpp +0 -112
- quasardb/module.cpp +0 -76
- quasardb/module.hpp +0 -24
- quasardb/node.hpp +0 -123
- quasardb/numpy.cpp +0 -6
- quasardb/numpy.hpp +0 -489
- quasardb/object_tracker.hpp +0 -283
- quasardb/options.hpp +0 -244
- quasardb/perf.hpp +0 -336
- quasardb/pytypes.hpp +0 -221
- quasardb/query.cpp +0 -420
- quasardb/query.hpp +0 -92
- quasardb/reader/ts_row.hpp +0 -281
- quasardb/reader/ts_value.hpp +0 -245
- quasardb/remove_cvref.hpp +0 -31
- quasardb/string.hpp +0 -160
- quasardb/table.cpp +0 -289
- quasardb/table.hpp +0 -325
- quasardb/table_reader.hpp +0 -220
- quasardb/tag.hpp +0 -77
- quasardb/timestamp.hpp +0 -97
- quasardb/traits.hpp +0 -619
- quasardb/ts_iterator.hpp +0 -193
- quasardb/utils/blob_deque.hpp +0 -96
- quasardb/utils/ostream.hpp +0 -17
- quasardb/utils/permutation.hpp +0 -50
- quasardb/utils/stable_sort.hpp +0 -25
- quasardb/utils/unzip_view.hpp +0 -89
- quasardb/utils.cpp +0 -28
- quasardb/utils.hpp +0 -174
- quasardb/writer.cpp +0 -534
- quasardb/writer.hpp +0 -396
- quasardb-3.14.2.dev1.dist-info/RECORD +0 -109
- {quasardb-3.14.2.dev1.dist-info → quasardb-3.14.2.dev4.dist-info}/LICENSE.md +0 -0
- {quasardb-3.14.2.dev1.dist-info → quasardb-3.14.2.dev4.dist-info}/top_level.txt +0 -0
quasardb/masked_array.hpp
DELETED
|
@@ -1,651 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
*
|
|
3
|
-
* Official Python API
|
|
4
|
-
*
|
|
5
|
-
* Copyright (c) 2009-2024, quasardb SAS. All rights reserved.
|
|
6
|
-
* All rights reserved.
|
|
7
|
-
*
|
|
8
|
-
* Redistribution and use in source and binary forms, with or without
|
|
9
|
-
* modification, are permitted provided that the following conditions are met:
|
|
10
|
-
*
|
|
11
|
-
* * Redistributions of source code must retain the above copyright
|
|
12
|
-
* notice, this list of conditions and the following disclaimer.
|
|
13
|
-
* * Redistributions in binary form must reproduce the above copyright
|
|
14
|
-
* notice, this list of conditions and the following disclaimer in the
|
|
15
|
-
* documentation and/or other materials provided with the distribution.
|
|
16
|
-
* * Neither the name of quasardb nor the names of its contributors may
|
|
17
|
-
* be used to endorse or promote products derived from this software
|
|
18
|
-
* without specific prior written permission.
|
|
19
|
-
*
|
|
20
|
-
* THIS SOFTWARE IS PROVIDED BY QUASARDB AND CONTRIBUTORS ``AS IS'' AND ANY
|
|
21
|
-
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
22
|
-
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
-
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
|
|
24
|
-
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
25
|
-
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
26
|
-
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
27
|
-
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
28
|
-
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
29
|
-
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
30
|
-
*/
|
|
31
|
-
#pragma once
|
|
32
|
-
|
|
33
|
-
#include "concepts.hpp"
|
|
34
|
-
#include "numpy.hpp"
|
|
35
|
-
#include "traits.hpp"
|
|
36
|
-
#include <pybind11/numpy.h>
|
|
37
|
-
#include <pybind11/stl_bind.h>
|
|
38
|
-
#include <range/v3/algorithm/for_each.hpp>
|
|
39
|
-
#include <range/v3/view/chunk.hpp>
|
|
40
|
-
#include <range/v3/view/counted.hpp>
|
|
41
|
-
|
|
42
|
-
namespace qdb::detail
|
|
43
|
-
{
|
|
44
|
-
|
|
45
|
-
enum mask_probe_t
|
|
46
|
-
{
|
|
47
|
-
mask_unknown = 0,
|
|
48
|
-
mask_all_true = 1 << 0,
|
|
49
|
-
mask_all_false = 1 << 1,
|
|
50
|
-
mask_mixed = mask_all_true | mask_all_false
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Efficiently probes one chunk, without returning early.
|
|
55
|
-
*/
|
|
56
|
-
template <typename Rng>
|
|
57
|
-
inline std::uint8_t probe_chunk(Rng const & xs) noexcept
|
|
58
|
-
{
|
|
59
|
-
std::uint8_t state{static_cast<std::uint8_t>(mask_unknown)};
|
|
60
|
-
|
|
61
|
-
// XXX(leon): Super hot code path, but it's auto-vectorized which makes it
|
|
62
|
-
// faster than any alternative (including reinterpreting them
|
|
63
|
-
// as 64-bit integers).
|
|
64
|
-
ranges::for_each(xs, [&state](bool x) -> void {
|
|
65
|
-
state |=
|
|
66
|
-
(x ? static_cast<std::uint8_t>(mask_all_true) : static_cast<std::uint8_t>(mask_all_false));
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
return state;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
template <typename Rng>
|
|
73
|
-
inline enum qdb::detail::mask_probe_t probe_mask(Rng const & xs) noexcept
|
|
74
|
-
{
|
|
75
|
-
// We don't accept empty arrays
|
|
76
|
-
assert(ranges::size(xs) > 0);
|
|
77
|
-
|
|
78
|
-
// In order for auto-vectorization to work, we use an outer loop (this function)
|
|
79
|
-
// which divides work into chunks of 256 booleans; these are then processed as
|
|
80
|
-
// one work unit.
|
|
81
|
-
//
|
|
82
|
-
// The outer loop checks whether we already have a mixed mask, and shortcuts when
|
|
83
|
-
// that's the case.
|
|
84
|
-
//
|
|
85
|
-
// This ensures that, if we're dealing with large, mixed masked, we scan only a
|
|
86
|
-
// fraction of it.
|
|
87
|
-
constexpr std::size_t chunk_size = 256; // not chosen scientifically
|
|
88
|
-
std::uint8_t state = mask_unknown;
|
|
89
|
-
|
|
90
|
-
// Interpret the booleans as a range with `chunk_size` chunks of data.
|
|
91
|
-
auto rng = xs | ranges::views::chunk(chunk_size);
|
|
92
|
-
|
|
93
|
-
for (auto chunk : rng)
|
|
94
|
-
{
|
|
95
|
-
state |= probe_chunk(chunk);
|
|
96
|
-
|
|
97
|
-
if (state == mask_mixed)
|
|
98
|
-
{
|
|
99
|
-
// Exit early if we have found mixed data.
|
|
100
|
-
break;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
assert(0 < state && state <= 3);
|
|
105
|
-
return static_cast<mask_probe_t>(state);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
inline enum qdb::detail::mask_probe_t probe_mask(bool const * xs, std::size_t n) noexcept
|
|
109
|
-
{
|
|
110
|
-
return probe_mask(ranges::views::counted(xs, n));
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
inline enum mask_probe_t probe_mask(py::array const & xs) noexcept
|
|
114
|
-
{
|
|
115
|
-
assert(xs.dtype().kind() == 'b');
|
|
116
|
-
bool const * xs_ = xs.unchecked<bool>().data();
|
|
117
|
-
return probe_mask(xs_, xs.size());
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
template <bool v>
|
|
121
|
-
constexpr mask_probe_t probe_of_bool();
|
|
122
|
-
|
|
123
|
-
template <>
|
|
124
|
-
constexpr inline mask_probe_t probe_of_bool<true>()
|
|
125
|
-
{
|
|
126
|
-
return mask_all_true;
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
template <>
|
|
130
|
-
constexpr inline mask_probe_t probe_of_bool<false>()
|
|
131
|
-
{
|
|
132
|
-
return mask_all_false;
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
constexpr inline mask_probe_t probe_of_bool(bool b)
|
|
136
|
-
{
|
|
137
|
-
return (b == true ? probe_of_bool<true>() : probe_of_bool<false>());
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
template <mask_probe_t v>
|
|
141
|
-
constexpr bool bool_of_probe();
|
|
142
|
-
|
|
143
|
-
template <>
|
|
144
|
-
constexpr inline bool bool_of_probe<mask_all_true>()
|
|
145
|
-
{
|
|
146
|
-
return true;
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
template <>
|
|
150
|
-
constexpr inline bool bool_of_probe<mask_all_false>()
|
|
151
|
-
{
|
|
152
|
-
return false;
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
constexpr inline bool bool_of_probe(mask_probe_t p)
|
|
156
|
-
{
|
|
157
|
-
switch (p)
|
|
158
|
-
{
|
|
159
|
-
case mask_all_true:
|
|
160
|
-
return true;
|
|
161
|
-
case mask_all_false:
|
|
162
|
-
return false;
|
|
163
|
-
default:
|
|
164
|
-
throw qdb::internal_local_exception(
|
|
165
|
-
"Mask probe not convertible to boolean: " + std::to_string(p));
|
|
166
|
-
};
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
}; // namespace qdb::detail
|
|
170
|
-
|
|
171
|
-
namespace qdb
|
|
172
|
-
{
|
|
173
|
-
namespace py = pybind11;
|
|
174
|
-
|
|
175
|
-
class mask
|
|
176
|
-
{
|
|
177
|
-
public:
|
|
178
|
-
/**
|
|
179
|
-
* Default constructor required because objects of this class are wrapped
|
|
180
|
-
* inside masked_array, and py::cast needs default constructors to work.
|
|
181
|
-
*/
|
|
182
|
-
inline mask() noexcept {};
|
|
183
|
-
|
|
184
|
-
inline mask(mask const & o) noexcept
|
|
185
|
-
: xs_{o.xs_}
|
|
186
|
-
, probe_{o.probe_} {};
|
|
187
|
-
|
|
188
|
-
inline mask(mask && o) noexcept
|
|
189
|
-
: xs_{std::move(o.xs_)}
|
|
190
|
-
, probe_{std::move(o.probe_)} {};
|
|
191
|
-
|
|
192
|
-
inline mask(py::array const & xs, detail::mask_probe_t probe) noexcept
|
|
193
|
-
: xs_{xs}
|
|
194
|
-
, probe_{probe} {};
|
|
195
|
-
|
|
196
|
-
inline mask & operator=(mask const & o) noexcept
|
|
197
|
-
{
|
|
198
|
-
xs_ = o.xs_;
|
|
199
|
-
probe_ = o.probe_;
|
|
200
|
-
return *this;
|
|
201
|
-
};
|
|
202
|
-
|
|
203
|
-
static inline mask of_array(py::array const & xs) noexcept
|
|
204
|
-
{
|
|
205
|
-
return mask{xs, detail::probe_mask(xs)};
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
inline bool load(py::array const & xs) noexcept
|
|
209
|
-
{
|
|
210
|
-
return load(xs, detail::probe_mask(xs));
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
inline bool load(py::array const & xs, detail::mask_probe_t probe) noexcept
|
|
214
|
-
{
|
|
215
|
-
if (xs.size() == 0) [[unlikely]]
|
|
216
|
-
{
|
|
217
|
-
return false;
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
xs_ = xs;
|
|
221
|
-
probe_ = probe;
|
|
222
|
-
return true;
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* Initialize a mask of size `n` with all values set to `true` (everything
|
|
226
|
-
* masked, i.e. everything hidden).
|
|
227
|
-
*/
|
|
228
|
-
template <bool v>
|
|
229
|
-
static inline mask of_all(py::ssize_t n) noexcept
|
|
230
|
-
{
|
|
231
|
-
assert(n > 0);
|
|
232
|
-
return mask{numpy::array::initialize(n, v), detail::probe_of_bool(v)};
|
|
233
|
-
};
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* Initialize a mask of size `n` with all values set to `true`.
|
|
237
|
-
*/
|
|
238
|
-
template <detail::mask_probe_t p>
|
|
239
|
-
static inline mask of_probe(py::ssize_t n) noexcept
|
|
240
|
-
{
|
|
241
|
-
assert(n > 0);
|
|
242
|
-
return mask{numpy::array::initialize(n, detail::bool_of_probe<p>()), p};
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* Returns amount of elemenbts in the mask.
|
|
247
|
-
*/
|
|
248
|
-
py::ssize_t size() const noexcept
|
|
249
|
-
{
|
|
250
|
-
return xs_.size();
|
|
251
|
-
};
|
|
252
|
-
|
|
253
|
-
py::array const & array() const noexcept
|
|
254
|
-
{
|
|
255
|
-
return xs_;
|
|
256
|
-
};
|
|
257
|
-
|
|
258
|
-
bool const * data() const noexcept
|
|
259
|
-
{
|
|
260
|
-
return xs_.unchecked<bool>().data();
|
|
261
|
-
};
|
|
262
|
-
|
|
263
|
-
bool * mutable_data() noexcept
|
|
264
|
-
{
|
|
265
|
-
return xs_.mutable_unchecked<bool>().mutable_data();
|
|
266
|
-
};
|
|
267
|
-
|
|
268
|
-
detail::mask_probe_t probe() const noexcept
|
|
269
|
-
{
|
|
270
|
-
return probe_;
|
|
271
|
-
};
|
|
272
|
-
|
|
273
|
-
private:
|
|
274
|
-
py::array xs_;
|
|
275
|
-
detail::mask_probe_t probe_;
|
|
276
|
-
};
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
* Masked array that can hold any type of data. Types are templated in the member
|
|
280
|
-
* functions that require them, and no type checking is done upon initialization.
|
|
281
|
-
*/
|
|
282
|
-
class masked_array
|
|
283
|
-
{
|
|
284
|
-
public:
|
|
285
|
-
using ShapeContainer = typename py::array::ShapeContainer;
|
|
286
|
-
|
|
287
|
-
public:
|
|
288
|
-
// Default constructor 'deleted' but necessary for py::cast
|
|
289
|
-
masked_array() = default;
|
|
290
|
-
|
|
291
|
-
// Initialize a masked array with everything open
|
|
292
|
-
explicit masked_array(py::array arr)
|
|
293
|
-
: masked_array(arr, qdb::mask::of_probe<detail::mask_all_false>(arr.size()))
|
|
294
|
-
{}
|
|
295
|
-
|
|
296
|
-
// Initialized from an array and a mask array. Mask array should be with dtype bool.
|
|
297
|
-
// Automatically 'probes' the mask to find all-true / all-false patterns.
|
|
298
|
-
masked_array(py::array arr, py::array mask)
|
|
299
|
-
: masked_array{arr, mask::of_array(mask)}
|
|
300
|
-
{}
|
|
301
|
-
|
|
302
|
-
// Initialized from an array and a mask array. Mask
|
|
303
|
-
// array should be with dtype bool. True indicates masked (invisible).
|
|
304
|
-
masked_array(py::array arr, qdb::mask mask)
|
|
305
|
-
: logger_("quasardb.masked_array")
|
|
306
|
-
, arr_{arr}
|
|
307
|
-
, mask_{mask}
|
|
308
|
-
{
|
|
309
|
-
assert(arr.size() == mask.size());
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
// Copy constructor
|
|
313
|
-
masked_array(const masked_array & ma)
|
|
314
|
-
: arr_{ma.arr_}
|
|
315
|
-
, mask_{ma.mask_}
|
|
316
|
-
{}
|
|
317
|
-
|
|
318
|
-
~masked_array()
|
|
319
|
-
{}
|
|
320
|
-
|
|
321
|
-
py::handle cast(py::return_value_policy /* policy */) const
|
|
322
|
-
{
|
|
323
|
-
py::module numpy_ma = py::module::import("numpy.ma");
|
|
324
|
-
py::object init = numpy_ma.attr("masked_array");
|
|
325
|
-
|
|
326
|
-
return init(arr_, mask_.array()).inc_ref();
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
py::array data() const
|
|
330
|
-
{
|
|
331
|
-
return arr_;
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
qdb::mask const & mask() const
|
|
335
|
-
{
|
|
336
|
-
return mask_;
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
inline bool load(py::object src)
|
|
340
|
-
{
|
|
341
|
-
|
|
342
|
-
if (masked_array::check(src)) [[likely]]
|
|
343
|
-
{
|
|
344
|
-
// This is an actual numpy.ma.array
|
|
345
|
-
logger_.debug("loading masked array from numpy.ma.MaskedArray object");
|
|
346
|
-
return load(src.attr("data"), src.attr("mask"));
|
|
347
|
-
}
|
|
348
|
-
else if (py::isinstance<py::array>(src))
|
|
349
|
-
{
|
|
350
|
-
logger_.debug(
|
|
351
|
-
"initializing quasardb.masked_array from numpy.ndarray with size %d", arr_.size());
|
|
352
|
-
py::array src_ = py::cast<py::array>(src);
|
|
353
|
-
return load(src_, mask::of_all<false>(src_.size()));
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
return false;
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
inline bool load(py::array arr, py::array mask)
|
|
360
|
-
{
|
|
361
|
-
if (arr.size() != mask.size())
|
|
362
|
-
{
|
|
363
|
-
logger_.warn("array[%d] and mask[%d] not of identical size", arr.size(), mask.size());
|
|
364
|
-
};
|
|
365
|
-
assert(arr.size() == mask.size());
|
|
366
|
-
|
|
367
|
-
arr_ = py::array::ensure(arr);
|
|
368
|
-
if (mask_.load(mask) == false)
|
|
369
|
-
{
|
|
370
|
-
logger_.warn("unable to load mask");
|
|
371
|
-
return false;
|
|
372
|
-
};
|
|
373
|
-
|
|
374
|
-
return true;
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
inline bool load(py::array arr, qdb::mask mask)
|
|
378
|
-
{
|
|
379
|
-
assert(arr.size() == mask.size());
|
|
380
|
-
|
|
381
|
-
arr_ = py::array::ensure(arr);
|
|
382
|
-
mask_ = mask;
|
|
383
|
-
|
|
384
|
-
return true;
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
bool load(std::pair<py::array, py::array> const & src)
|
|
388
|
-
{
|
|
389
|
-
return load(std::get<0>(src), std::get<1>(src));
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
/**
|
|
393
|
-
* Returns `true` if handle is of a masked array type.
|
|
394
|
-
*/
|
|
395
|
-
static bool check(py::handle x)
|
|
396
|
-
{
|
|
397
|
-
// XXX(leon): perhaps there's a higher performance way to check the
|
|
398
|
-
// type, such as acquiring a reference to the type of
|
|
399
|
-
// `numpy.MaskedArray` and invoking py::isinstance().
|
|
400
|
-
//
|
|
401
|
-
// but this path is not very performance critical.
|
|
402
|
-
py::module numpy_ma = py::module::import("numpy.ma");
|
|
403
|
-
py::object isMA = numpy_ma.attr("isMaskedArray");
|
|
404
|
-
|
|
405
|
-
return py::cast<bool>(isMA(x));
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
inline py::dtype dtype() const
|
|
409
|
-
{
|
|
410
|
-
return arr_.dtype();
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
inline std::size_t size() const noexcept
|
|
414
|
-
{
|
|
415
|
-
assert(arr_.size() == mask_.size());
|
|
416
|
-
return arr_.size();
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
inline ShapeContainer shape() const noexcept
|
|
420
|
-
{
|
|
421
|
-
return ShapeContainer{{size()}};
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
/**
|
|
425
|
-
* Return a regular numpy array with the masked values "filled" with
|
|
426
|
-
* the provided value.
|
|
427
|
-
*
|
|
428
|
-
* The behavior mirrors the numpy.ma.filled function, and aligns fairly
|
|
429
|
-
* well with how QuasarDB wants data to be shaped.
|
|
430
|
-
*
|
|
431
|
-
* This specific implementation is for dtypes with fixed length, which
|
|
432
|
-
* allows us to use "fast" point-based copies.
|
|
433
|
-
*/
|
|
434
|
-
template <concepts::dtype T>
|
|
435
|
-
inline py::array filled(typename T::value_type const & fill_value) const
|
|
436
|
-
{
|
|
437
|
-
assert(arr_.size() == mask_.size());
|
|
438
|
-
|
|
439
|
-
py::array ret;
|
|
440
|
-
|
|
441
|
-
switch (mask_.probe())
|
|
442
|
-
{
|
|
443
|
-
case detail::mask_all_true:
|
|
444
|
-
// Everything masked: which can just initialize a whole array with fill_value
|
|
445
|
-
// and call it a day.
|
|
446
|
-
return qdb::numpy::array::initialize<T>(arr_.size(), fill_value);
|
|
447
|
-
break;
|
|
448
|
-
|
|
449
|
-
case detail::mask_all_false:
|
|
450
|
-
// Fast(est) path: nothing masked, which implies it's identical to arr_
|
|
451
|
-
return arr_;
|
|
452
|
-
|
|
453
|
-
case detail::mask_mixed:
|
|
454
|
-
return qdb::numpy::array::fill_with_mask<T>(arr_, mask_.array(), fill_value);
|
|
455
|
-
case detail::mask_unknown:
|
|
456
|
-
// This is an internal error: mask should always be probed (as it's probed
|
|
457
|
-
// in the constructor).
|
|
458
|
-
//
|
|
459
|
-
// Only condition would be when masked_array is default-constructed and
|
|
460
|
-
// not initialized.
|
|
461
|
-
throw qdb::internal_local_exception{"Mask probe is unknown, masked array not initialized?"};
|
|
462
|
-
|
|
463
|
-
default:
|
|
464
|
-
throw qdb::internal_local_exception{
|
|
465
|
-
"Mask probe is corrupted: not a known value: " + std::to_string(mask_.probe())};
|
|
466
|
-
};
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
template <concepts::dtype T>
|
|
470
|
-
inline py::array filled() const
|
|
471
|
-
{
|
|
472
|
-
return filled<T>(T::null_value());
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
static inline masked_array masked_all(py::array xs)
|
|
476
|
-
{
|
|
477
|
-
return masked_array{xs, mask::of_all<true>(xs.size())};
|
|
478
|
-
};
|
|
479
|
-
|
|
480
|
-
static inline masked_array masked_none(py::array xs)
|
|
481
|
-
{
|
|
482
|
-
return masked_array{xs, mask::of_all<false>(xs.size())};
|
|
483
|
-
};
|
|
484
|
-
|
|
485
|
-
// Initialize an array mask from a regular array and a "null" value.
|
|
486
|
-
//
|
|
487
|
-
// Intended to be used as follows:
|
|
488
|
-
//
|
|
489
|
-
// py::array xs{"float64", 100};
|
|
490
|
-
// // .. fill data, some of them NaN .. //
|
|
491
|
-
// auto ma = masked_array{xs, masked_array::masked_null(xs)};
|
|
492
|
-
|
|
493
|
-
template <concepts::dtype Dtype>
|
|
494
|
-
static qdb::mask masked_null(py::array const & xs)
|
|
495
|
-
{
|
|
496
|
-
using value_type = typename Dtype::value_type;
|
|
497
|
-
|
|
498
|
-
py::array_t<bool> ret{ShapeContainer{xs.size()}};
|
|
499
|
-
bool * p_ret = static_cast<bool *>(ret.mutable_data());
|
|
500
|
-
|
|
501
|
-
// The step_size is `1` for all fixed-width dtypes, but in case
|
|
502
|
-
// of variable width dtypes, is, well, variable.
|
|
503
|
-
py::ssize_t step_size = Dtype::stride_size(xs.itemsize());
|
|
504
|
-
|
|
505
|
-
value_type const * begin = static_cast<value_type const *>(xs.data());
|
|
506
|
-
value_type const * end = begin + (xs.size() * step_size);
|
|
507
|
-
|
|
508
|
-
// TODO(leon): [perf] we should be able to determine the mask_probe_t while
|
|
509
|
-
// we iterate over this array.
|
|
510
|
-
for (value_type const * cur = begin; cur != end; cur += step_size, ++p_ret)
|
|
511
|
-
{
|
|
512
|
-
*p_ret = Dtype::is_null(*cur);
|
|
513
|
-
};
|
|
514
|
-
|
|
515
|
-
return mask::of_array(py::cast<py::array>(ret));
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
protected:
|
|
519
|
-
qdb::logger logger_;
|
|
520
|
-
py::array arr_;
|
|
521
|
-
qdb::mask mask_;
|
|
522
|
-
};
|
|
523
|
-
|
|
524
|
-
/**
|
|
525
|
-
* "Typed" masked array, like py::array / py::array_t, except in our case our 'type'
|
|
526
|
-
* is our dtype dispatch tag.
|
|
527
|
-
*
|
|
528
|
-
* IMPORTANT: we do *not* have a virtual destructor, this is fine because we do not
|
|
529
|
-
* have any member objects.
|
|
530
|
-
*/
|
|
531
|
-
template <concepts::dtype T>
|
|
532
|
-
class masked_array_t : public masked_array
|
|
533
|
-
{
|
|
534
|
-
public:
|
|
535
|
-
bool load(py::object src)
|
|
536
|
-
{
|
|
537
|
-
if (masked_array::load(src) == true) [[likely]]
|
|
538
|
-
{
|
|
539
|
-
return true;
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
if (py::isinstance<py::list>(src))
|
|
543
|
-
{
|
|
544
|
-
logger_.warn("Converting list to masked array: this is a very expensive operation. If you "
|
|
545
|
-
"are having performance issues, "
|
|
546
|
-
"consider using numpy.ndarray instead..");
|
|
547
|
-
|
|
548
|
-
// Convert list to numpy array and try again.
|
|
549
|
-
//
|
|
550
|
-
// The reason we can do this, is that because we have the "additional" context
|
|
551
|
-
// in masked_array_t of knowing what dtype we're looking for, we can also
|
|
552
|
-
// reasonably cast a list to that type; otherwise you'll end up with a numpy
|
|
553
|
-
// array of objects, which is very much meh.
|
|
554
|
-
return masked_array::load(numpy::array::of_list_with_mask<T>(py::cast<py::list>(src)));
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
return false;
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
static py::array masked_null(py::array const & xs)
|
|
561
|
-
{
|
|
562
|
-
return masked_array::masked_null<T>(xs);
|
|
563
|
-
};
|
|
564
|
-
|
|
565
|
-
inline py::array filled(typename T::value_type const & fill_value) const
|
|
566
|
-
{
|
|
567
|
-
return masked_array::filled<T>(fill_value);
|
|
568
|
-
};
|
|
569
|
-
|
|
570
|
-
inline py::array filled() const
|
|
571
|
-
{
|
|
572
|
-
return masked_array::filled<T>(T::null_value());
|
|
573
|
-
};
|
|
574
|
-
};
|
|
575
|
-
|
|
576
|
-
template <typename Module>
|
|
577
|
-
static inline void register_masked_array(Module & m)
|
|
578
|
-
{
|
|
579
|
-
namespace py = pybind11;
|
|
580
|
-
|
|
581
|
-
py::class_<masked_array>{m, "MaskedArray"};
|
|
582
|
-
}
|
|
583
|
-
|
|
584
|
-
} // namespace qdb
|
|
585
|
-
|
|
586
|
-
namespace PYBIND11_NAMESPACE
|
|
587
|
-
{
|
|
588
|
-
namespace detail
|
|
589
|
-
{
|
|
590
|
-
|
|
591
|
-
/**
|
|
592
|
-
* Implements custom type caster for our ts_value class, so that conversion
|
|
593
|
-
* to and from native python types is completely transparent.
|
|
594
|
-
*/
|
|
595
|
-
template <>
|
|
596
|
-
struct type_caster<qdb::masked_array>
|
|
597
|
-
{
|
|
598
|
-
public:
|
|
599
|
-
/**
|
|
600
|
-
* Note that this macro magically sets a member variable called 'value'.
|
|
601
|
-
*/
|
|
602
|
-
PYBIND11_TYPE_CASTER(qdb::masked_array, const_name("numpy.ma.MaskedArray"));
|
|
603
|
-
|
|
604
|
-
/**
|
|
605
|
-
* Python->C++.
|
|
606
|
-
*/
|
|
607
|
-
bool load(py::handle src, bool)
|
|
608
|
-
{
|
|
609
|
-
py::object obj = reinterpret_borrow<py::object>(src);
|
|
610
|
-
bool result = value.load(obj);
|
|
611
|
-
return result;
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
/**
|
|
615
|
-
* C++->Python
|
|
616
|
-
*/
|
|
617
|
-
static py::handle cast(qdb::masked_array && src, return_value_policy policy, handle /* parent */)
|
|
618
|
-
{
|
|
619
|
-
return src.cast(policy);
|
|
620
|
-
}
|
|
621
|
-
};
|
|
622
|
-
|
|
623
|
-
template <qdb::concepts::dtype T>
|
|
624
|
-
struct type_caster<qdb::masked_array_t<T>>
|
|
625
|
-
{
|
|
626
|
-
public:
|
|
627
|
-
using type = qdb::masked_array_t<T>;
|
|
628
|
-
PYBIND11_TYPE_CASTER(type, const_name("numpy.ma.MaskedArray<T>"));
|
|
629
|
-
|
|
630
|
-
/**
|
|
631
|
-
* Python->C++.
|
|
632
|
-
*/
|
|
633
|
-
bool load(py::handle src, bool)
|
|
634
|
-
{
|
|
635
|
-
py::object obj = reinterpret_borrow<py::object>(src);
|
|
636
|
-
bool result = value.load(obj);
|
|
637
|
-
return result;
|
|
638
|
-
};
|
|
639
|
-
|
|
640
|
-
/**
|
|
641
|
-
* C++->Python
|
|
642
|
-
*/
|
|
643
|
-
static py::handle cast(type && src, return_value_policy policy, handle /* parent */)
|
|
644
|
-
{
|
|
645
|
-
return src.cast(policy);
|
|
646
|
-
}
|
|
647
|
-
};
|
|
648
|
-
|
|
649
|
-
}; // namespace detail
|
|
650
|
-
|
|
651
|
-
}; // namespace PYBIND11_NAMESPACE
|