Skip to content

Commit dbec6aa

Browse files
committed
Add GArrowWinsorizeOptions
1 parent 1ddb440 commit dbec6aa

File tree

4 files changed

+211
-0
lines changed

4 files changed

+211
-0
lines changed

c_glib/arrow-glib/compute.cpp

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ G_BEGIN_DECLS
335335
*
336336
* #GArrowWeekOptions is a class to customize the `week` function.
337337
*
338+
* #GArrowWinsorizeOptions is a class to customize the `winsorize` function.
339+
*
338340
* There are many functions to compute data on an array.
339341
*/
340342

@@ -10451,6 +10453,124 @@ garrow_week_options_new(void)
1045110453
return GARROW_WEEK_OPTIONS(g_object_new(GARROW_TYPE_WEEK_OPTIONS, NULL));
1045210454
}
1045310455

10456+
enum {
10457+
PROP_WINSORIZE_OPTIONS_LOWER_LIMIT = 1,
10458+
PROP_WINSORIZE_OPTIONS_UPPER_LIMIT,
10459+
};
10460+
10461+
G_DEFINE_TYPE(GArrowWinsorizeOptions, garrow_winsorize_options, GARROW_TYPE_FUNCTION_OPTIONS)
10462+
10463+
static void
10464+
garrow_winsorize_options_set_property(GObject *object,
10465+
guint prop_id,
10466+
const GValue *value,
10467+
GParamSpec *pspec)
10468+
{
10469+
auto options = garrow_winsorize_options_get_raw(GARROW_WINSORIZE_OPTIONS(object));
10470+
10471+
switch (prop_id) {
10472+
case PROP_WINSORIZE_OPTIONS_LOWER_LIMIT:
10473+
options->lower_limit = g_value_get_double(value);
10474+
break;
10475+
case PROP_WINSORIZE_OPTIONS_UPPER_LIMIT:
10476+
options->upper_limit = g_value_get_double(value);
10477+
break;
10478+
default:
10479+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
10480+
break;
10481+
}
10482+
}
10483+
10484+
static void
10485+
garrow_winsorize_options_get_property(GObject *object,
10486+
guint prop_id,
10487+
GValue *value,
10488+
GParamSpec *pspec)
10489+
{
10490+
auto options = garrow_winsorize_options_get_raw(GARROW_WINSORIZE_OPTIONS(object));
10491+
10492+
switch (prop_id) {
10493+
case PROP_WINSORIZE_OPTIONS_LOWER_LIMIT:
10494+
g_value_set_double(value, options->lower_limit);
10495+
break;
10496+
case PROP_WINSORIZE_OPTIONS_UPPER_LIMIT:
10497+
g_value_set_double(value, options->upper_limit);
10498+
break;
10499+
default:
10500+
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
10501+
break;
10502+
}
10503+
}
10504+
10505+
static void
10506+
garrow_winsorize_options_init(GArrowWinsorizeOptions *object)
10507+
{
10508+
auto priv = GARROW_FUNCTION_OPTIONS_GET_PRIVATE(object);
10509+
priv->options =
10510+
static_cast<arrow::compute::FunctionOptions *>(new arrow::compute::WinsorizeOptions());
10511+
}
10512+
10513+
static void
10514+
garrow_winsorize_options_class_init(GArrowWinsorizeOptionsClass *klass)
10515+
{
10516+
auto gobject_class = G_OBJECT_CLASS(klass);
10517+
10518+
gobject_class->set_property = garrow_winsorize_options_set_property;
10519+
gobject_class->get_property = garrow_winsorize_options_get_property;
10520+
10521+
auto options = arrow::compute::WinsorizeOptions();
10522+
10523+
GParamSpec *spec;
10524+
/**
10525+
* GArrowWinsorizeOptions:lower-limit:
10526+
*
10527+
* The quantile below which all values are replaced with the quantile's value.
10528+
* For example, if lower_limit = 0.05, then all values in the lower 5% percentile
10529+
* will be replaced with the 5% percentile value.
10530+
*
10531+
* Since: 23.0.0
10532+
*/
10533+
spec = g_param_spec_double("lower-limit",
10534+
"Lower limit",
10535+
"The quantile below which all values are replaced with the quantile's value. For example, if lower_limit = 0.05, then all values in the lower 5% percentile will be replaced with the 5% percentile value",
10536+
0.0,
10537+
1.0,
10538+
options.lower_limit,
10539+
static_cast<GParamFlags>(G_PARAM_READWRITE));
10540+
g_object_class_install_property(gobject_class, PROP_WINSORIZE_OPTIONS_LOWER_LIMIT, spec);
10541+
10542+
/**
10543+
* GArrowWinsorizeOptions:upper-limit:
10544+
*
10545+
* The quantile above which all values are replaced with the quantile's value.
10546+
* For example, if upper_limit = 0.95, then all values in the upper 95% percentile
10547+
* will be replaced with the 95% percentile value.
10548+
*
10549+
* Since: 23.0.0
10550+
*/
10551+
spec = g_param_spec_double("upper-limit",
10552+
"Upper limit",
10553+
"The quantile above which all values are replaced with the quantile's value. For example, if upper_limit = 0.95, then all values in the upper 95% percentile will be replaced with the 95% percentile value",
10554+
0.0,
10555+
1.0,
10556+
options.upper_limit,
10557+
static_cast<GParamFlags>(G_PARAM_READWRITE));
10558+
g_object_class_install_property(gobject_class, PROP_WINSORIZE_OPTIONS_UPPER_LIMIT, spec);
10559+
}
10560+
10561+
/**
10562+
* garrow_winsorize_options_new:
10563+
*
10564+
* Returns: A newly created #GArrowWinsorizeOptions.
10565+
*
10566+
* Since: 23.0.0
10567+
*/
10568+
GArrowWinsorizeOptions *
10569+
garrow_winsorize_options_new(void)
10570+
{
10571+
return GARROW_WINSORIZE_OPTIONS(g_object_new(GARROW_TYPE_WINSORIZE_OPTIONS, NULL));
10572+
}
10573+
1045410574
G_END_DECLS
1045510575

1045610576
arrow::Result<arrow::FieldRef>
@@ -10727,6 +10847,11 @@ garrow_function_options_new_raw(const arrow::compute::FunctionOptions *arrow_opt
1072710847
static_cast<const arrow::compute::WeekOptions *>(arrow_options);
1072810848
auto options = garrow_week_options_new_raw(arrow_week_options);
1072910849
return GARROW_FUNCTION_OPTIONS(options);
10850+
} else if (arrow_type_name == "WinsorizeOptions") {
10851+
const auto arrow_winsorize_options =
10852+
static_cast<const arrow::compute::WinsorizeOptions *>(arrow_options);
10853+
auto options = garrow_winsorize_options_new_raw(arrow_winsorize_options);
10854+
return GARROW_FUNCTION_OPTIONS(options);
1073010855
} else {
1073110856
auto options = g_object_new(GARROW_TYPE_FUNCTION_OPTIONS, NULL);
1073210857
return GARROW_FUNCTION_OPTIONS(options);
@@ -11853,3 +11978,22 @@ garrow_week_options_get_raw(GArrowWeekOptions *options)
1185311978
return static_cast<arrow::compute::WeekOptions *>(
1185411979
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
1185511980
}
11981+
11982+
GArrowWinsorizeOptions *
11983+
garrow_winsorize_options_new_raw(const arrow::compute::WinsorizeOptions *arrow_options)
11984+
{
11985+
auto options = g_object_new(GARROW_TYPE_WINSORIZE_OPTIONS,
11986+
"lower-limit",
11987+
arrow_options->lower_limit,
11988+
"upper-limit",
11989+
arrow_options->upper_limit,
11990+
NULL);
11991+
return GARROW_WINSORIZE_OPTIONS(options);
11992+
}
11993+
11994+
arrow::compute::WinsorizeOptions *
11995+
garrow_winsorize_options_get_raw(GArrowWinsorizeOptions *options)
11996+
{
11997+
return static_cast<arrow::compute::WinsorizeOptions *>(
11998+
garrow_function_options_get_raw(GARROW_FUNCTION_OPTIONS(options)));
11999+
}

c_glib/arrow-glib/compute.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,4 +1759,17 @@ GARROW_AVAILABLE_IN_23_0
17591759
GArrowWeekOptions *
17601760
garrow_week_options_new(void);
17611761

1762+
#define GARROW_TYPE_WINSORIZE_OPTIONS (garrow_winsorize_options_get_type())
1763+
GARROW_AVAILABLE_IN_23_0
1764+
G_DECLARE_DERIVABLE_TYPE(
1765+
GArrowWinsorizeOptions, garrow_winsorize_options, GARROW, WINSORIZE_OPTIONS, GArrowFunctionOptions)
1766+
struct _GArrowWinsorizeOptionsClass
1767+
{
1768+
GArrowFunctionOptionsClass parent_class;
1769+
};
1770+
1771+
GARROW_AVAILABLE_IN_23_0
1772+
GArrowWinsorizeOptions *
1773+
garrow_winsorize_options_new(void);
1774+
17621775
G_END_DECLS

c_glib/arrow-glib/compute.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,8 @@ GArrowWeekOptions *
332332
garrow_week_options_new_raw(const arrow::compute::WeekOptions *arrow_options);
333333
arrow::compute::WeekOptions *
334334
garrow_week_options_get_raw(GArrowWeekOptions *options);
335+
336+
GArrowWinsorizeOptions *
337+
garrow_winsorize_options_new_raw(const arrow::compute::WinsorizeOptions *arrow_options);
338+
arrow::compute::WinsorizeOptions *
339+
garrow_winsorize_options_get_raw(GArrowWinsorizeOptions *options);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
class TestWinsorizeOptions < Test::Unit::TestCase
19+
include Helper::Buildable
20+
21+
def setup
22+
@options = Arrow::WinsorizeOptions.new
23+
end
24+
25+
def test_lower_limit_property
26+
assert_equal(0.0, @options.lower_limit)
27+
@options.lower_limit = 0.05
28+
assert_equal(0.05, @options.lower_limit)
29+
end
30+
31+
def test_upper_limit_property
32+
assert_equal(1.0, @options.upper_limit)
33+
@options.upper_limit = 0.95
34+
assert_equal(0.95, @options.upper_limit)
35+
end
36+
37+
def test_winsorize_function
38+
args = [
39+
Arrow::ArrayDatum.new(build_double_array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0])),
40+
]
41+
@options.lower_limit = 0.1
42+
@options.upper_limit = 0.9
43+
winsorize_function = Arrow::Function.find("winsorize")
44+
result = winsorize_function.execute(args, @options).value
45+
expected = build_double_array([2.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 9.0])
46+
assert_equal(expected, result)
47+
end
48+
end
49+

0 commit comments

Comments
 (0)