Skip to content

Commit 50a34fb

Browse files
committed
Implemented mask()
1 parent a8ce430 commit 50a34fb

7 files changed

Lines changed: 317 additions & 1 deletion

File tree

benchmarks/polars_performance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
third = datetime.datetime.now()
3131

3232
df3 = df.filter(pl.col("log_normal") > 8)
33-
print(f"Number of rows after select: {df3.select(pl.count()).item()}")
33+
print(f"Number of rows after select: {df3.select(pl.len()).item()}")
3434
fourth = datetime.datetime.now()
3535

3636
# df4 = df.sort(["log_normal", "exponential"]);

docs/HTML/DataFrame.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,10 @@ <H2 ID="2"><font color="blue">API Reference with code samples <font size="+4">&#
333333
<td title="Calculates K-Nearest Neighbors (KNN) algorithm"><a href="https://htmlpreview.github.io/?https://github.com/hosseinmoein/DataFrame/blob/master/docs/HTML/knn.html">knn</a>()</td>
334334
</tr>
335335

336+
<tr class="item" onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
337+
<td title="General masking function"><a href="https://htmlpreview.github.io/?https://github.com/hosseinmoein/DataFrame/blob/master/docs/HTML/mask.html">mask</a>()</td>
338+
</tr>
339+
336340
<tr class="item" onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
337341
<td title="Returns the Markov Chains stationary distributions"><a href="https://htmlpreview.github.io/?https://github.com/hosseinmoein/DataFrame/blob/master/docs/HTML/MC_station_dist.html">MC_station_dist</a>()</td>
338342
</tr>

docs/HTML/mask.html

Lines changed: 134 additions & 0 deletions
Large diffs are not rendered by default.

include/DataFrame/DataFrame.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3744,6 +3744,29 @@ class DataFrame : public ThreadGranularity {
37443744
[[nodiscard]] DataFrame<I, HeteroVector<std::size_t(H::align_value)>>
37453745
duplication_mask(bool include_index, bool binary = false) const;
37463746

3747+
// This is the generalization of a masking function. Every element of the
3748+
// given column will be passed to the mfunc and the output of mfunc will
3749+
// be stored in the result vector. A typical example would be a Boolean
3750+
// result vector, although the default result is a vector of char.
3751+
// That’s because std::vector<bool> has an odd implementation that I don’t
3752+
// like.
3753+
//
3754+
// NOTE: Type MT must have default constructor
3755+
// NOTE: mfunc must be stateless, because it is used in multithreading
3756+
//
3757+
// T:
3758+
// Type of the "new index" column
3759+
// MT:
3760+
// Masking function return type
3761+
// col_name:
3762+
// Name of the given column
3763+
// mfunc:
3764+
// The masking function
3765+
//
3766+
template<typename T, typename MT = char>
3767+
[[nodiscard]] StlVecType<MT>
3768+
mask(const char *col_name, std::function<MT(const T &val)> &&mfunc) const;
3769+
37473770
// It behaves like get_data(), but it returns a View.
37483771
// A view is a DataFrame that is a reference to the original DataFrame.
37493772
// So if you modify anything in the view the original DataFrame will

include/DataFrame/DataFrameMLVisitors.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,6 +1479,71 @@ using fft_v = FastFourierTransVisitor<T, I, A>;
14791479

14801480
// ----------------------------------------------------------------------------
14811481

1482+
/*
1483+
template<std::signed_integral T, typename I = unsigned long, std::size_t A = 0>
1484+
struct NumTheoreticTransVisitor {
1485+
1486+
public:
1487+
1488+
DEFINE_VISIT_BASIC_TYPES_2
1489+
1490+
public:
1491+
1492+
// Inverse = false — Turns a vector of coefficients into its NTT form
1493+
// (evaluation at roots of unity).
1494+
// Inverse = true — Turns back from NTT domain to original coefficients.
1495+
//
1496+
//
1497+
// Vectors will be resized to the smallest power of 2 equal or larger than
1498+
// vector size. They will be appended with zeros.
1499+
//
1500+
template <typename K, typename H>
1501+
inline void
1502+
operator() (const K &idx_begin, const K &idx_end,
1503+
const H &column_begin, const H &column_end) {
1504+
1505+
1506+
}
1507+
1508+
// Multiply two polynomials
1509+
// {1, 2, 3} represents 1 + 2x + 3x^2
1510+
// {4, 5, 6} represents 4 + 5x + 6x^2
1511+
//
1512+
// Vectors will be resized to the smallest power of 2 equal or larger than
1513+
// vector size. They will be appended with zeros.
1514+
//
1515+
template <typename K, typename H>
1516+
inline void
1517+
operator() (const K &idx_begin, const K &idx_end,
1518+
const H &column1_begin, const H &column1_end,
1519+
const H &column2_begin, const H &column2_end) {
1520+
1521+
1522+
}
1523+
1524+
inline void pre () { }
1525+
inline void post () { }
1526+
1527+
DEFINE_RESULT
1528+
1529+
explicit
1530+
NumTheoreticTransVisitor(bool inverse = false)
1531+
: inverse_(inverse),
1532+
thread_level_(ThreadGranularity::get_thread_level()) { }
1533+
1534+
private:
1535+
1536+
const bool inverse_;
1537+
const long thread_level_;
1538+
result_type result_ { };
1539+
};
1540+
1541+
template<std::signed_integral T, typename I = unsigned long, std::size_t A = 0>
1542+
using ntt_v = NumTheoreticTransVisitor<T, I, A>;
1543+
*/
1544+
1545+
// ----------------------------------------------------------------------------
1546+
14821547
template<arithmetic T, typename I = unsigned long, std::size_t A = 0>
14831548
struct EntropyVisitor {
14841549

include/DataFrame/Internals/DataFrame_get.tcc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,40 @@ duplication_mask (bool include_index, bool binary) const {
382382

383383
// ----------------------------------------------------------------------------
384384

385+
template<typename I, typename H>
386+
template<typename T, typename MT>
387+
typename DataFrame<I, H>::StlVecType<MT> DataFrame<I, H>::
388+
mask(const char *col_name, std::function<MT(const T &val)> &&mfunc) const {
389+
390+
using res_t = StlVecType<MT>;
391+
392+
const auto &column = get_column<T>(col_name);
393+
const size_type col_s = column.size();
394+
res_t result(col_s);
395+
auto lbd =
396+
[&column = std::as_const(column), &result, &mfunc]
397+
(size_type begin, size_type end) -> void {
398+
for (auto i = begin; i < end; ++i)
399+
result[i] = mfunc(column[i]);
400+
};
401+
const auto thread_level =
402+
(col_s < ThreadPool::MUL_THR_THHOLD) ? 0L : get_thread_level();
403+
404+
if (thread_level > 2) {
405+
auto futuers = thr_pool_.parallel_loop(size_type(0), col_s,
406+
std::move(lbd));
407+
408+
for (auto &fut : futuers) fut.get();
409+
}
410+
else {
411+
lbd(size_type(0), col_s);
412+
}
413+
414+
return (result);
415+
}
416+
417+
// ----------------------------------------------------------------------------
418+
385419
template<typename I, typename H>
386420
template<typename T, typename ... Ts>
387421
DataFrame<I, HeteroVector<std::size_t(H::align_value)>> DataFrame<I, H>::

test/dataframe_tester_4.cc

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3531,6 +3531,61 @@ static void test_MannWhitneyUTestVisitor() {
35313531

35323532
// ----------------------------------------------------------------------------
35333533

3534+
static void test_mask() {
3535+
3536+
std::cout << "\nTesting mask( ) ..." << std::endl;
3537+
3538+
StrDataFrame ibm;
3539+
3540+
try {
3541+
ibm.read("IBM.csv", io_format::csv2);
3542+
}
3543+
catch (const DataFrameError &ex) {
3544+
std::cout << ex.what() << std::endl;
3545+
::exit(-1);
3546+
}
3547+
3548+
ibm.load_column("close_mask",
3549+
ibm.mask<double, char>("IBM_Close",
3550+
[](const double &close) -> char {
3551+
return (close > 180.0);
3552+
})
3553+
);
3554+
ibm.load_column("close_diff",
3555+
ibm.mask<double, double>("IBM_Close",
3556+
[](const double &close) -> double {
3557+
return (180.0 - close);
3558+
})
3559+
);
3560+
3561+
const auto &close_mask = ibm.get_column<char>("close_mask");
3562+
const auto &close_diff = ibm.get_column<double>("close_diff");
3563+
3564+
assert(close_mask[0] == 0);
3565+
assert(close_mask[10] == 0);
3566+
assert(close_mask[247] == 0);
3567+
assert(close_mask[1180] == 0);
3568+
assert(close_mask[2761] == 1);
3569+
assert(close_mask[2806] == 1);
3570+
assert(close_mask[3372] == 1);
3571+
assert(close_mask[4106] == 1);
3572+
assert(close_mask[4966] == 0);
3573+
assert(close_mask[5030] == 0);
3574+
3575+
assert((std::fabs(close_diff[0] - 81.4375) < 0.0001));
3576+
assert((std::fabs(close_diff[10] - 80.625) < 0.0001));
3577+
assert((std::fabs(close_diff[247] - 71.93) < 0.0001));
3578+
assert((std::fabs(close_diff[1180] - 98.19) < 0.0001));
3579+
assert((std::fabs(close_diff[2761] - -0.360001) < 0.000001));
3580+
assert((std::fabs(close_diff[2806] - -6.17999) < 0.0001));
3581+
assert((std::fabs(close_diff[3372] - -13.55) < 0.0001));
3582+
assert((std::fabs(close_diff[4106] - -0.529999) < 0.000001));
3583+
assert((std::fabs(close_diff[4966] - 57.06) < 0.0001));
3584+
assert((std::fabs(close_diff[5030] - 68.34) < 0.0001));
3585+
}
3586+
3587+
// ----------------------------------------------------------------------------
3588+
35343589
int main(int, char *[]) {
35353590

35363591
MyDataFrame::set_optimum_thread_level();
@@ -3592,6 +3647,7 @@ int main(int, char *[]) {
35923647
test_detect_and_change();
35933648
test_KolmoSmirnovTestVisitor();
35943649
test_MannWhitneyUTestVisitor();
3650+
test_mask();
35953651

35963652
return (0);
35973653
}

0 commit comments

Comments
 (0)