From 2f74a4216be3ef1ee31262ef9f7996abe9d08a74 Mon Sep 17 00:00:00 2001 From: James Dunkerley Date: Tue, 1 Mar 2022 11:16:43 +0000 Subject: [PATCH] StDev Sample Use first/second Expanding tests --- .../0.0.0-dev/src/Data/Aggregate_Column.enso | 13 ++++---- .../src/Aggregate_Column_Spec.enso | 32 +++++++++++++++---- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/distribution/lib/Standard/Table/0.0.0-dev/src/Data/Aggregate_Column.enso b/distribution/lib/Standard/Table/0.0.0-dev/src/Data/Aggregate_Column.enso index 4fab96f5a00d3..cbb1e0526846f 100644 --- a/distribution/lib/Standard/Table/0.0.0-dev/src/Data/Aggregate_Column.enso +++ b/distribution/lib/Standard/Table/0.0.0-dev/src/Data/Aggregate_Column.enso @@ -87,9 +87,9 @@ type Aggregate_Column case this of Count _ -> "Count" Count_Distinct c _ -> "Count Distinct " + (get_name c) - Count_Not_Nothing c _ -> "Count NotNull " + (get_name c) - Count_Nothing c _ -> "Count Null " + (get_name c) - Count_Not_Empty c _ -> "Count NotEmpty " + (get_name c) + Count_Not_Nothing c _ -> "Count Not Nothing " + (get_name c) + Count_Nothing c _ -> "Count Nothing " + (get_name c) + Count_Not_Empty c _ -> "Count Not Empty " + (get_name c) Count_Empty c _ -> "Count Empty " + (get_name c) Sum c _ -> "Sum " + (get_name c) Average c _ -> "Average " + (get_name c) @@ -164,9 +164,10 @@ type Aggregate_Column evaluate value = case this of Count_Distinct _ _ -> value.size Median _ _ -> (Error.throw "Not in prototype") - Average _ _ -> if (value.at 0) == 0 then Nothing else (value.at 1 / value.at 0) - Standard_Deviation _ _ p -> if (value.at 0) == 0 then Nothing else - (value.at 2/value.at 0 - (value.at 1/value.at 0)^2).sqrt * (if p then 1 else 1) + Average _ _ -> if value.first == 0 then Nothing else (value.second / value.first) + Standard_Deviation _ _ p -> if value.first == 0 then Nothing else + f = if p then 1 else (value.first / (value.first - 1)).sqrt + (value.at 2/value.at 0 - (value.at 1/value.at 0)^2).sqrt * f Concatenate _ _ _ s p _ -> if value.is_nothing then value else (s + value + p) _ -> value diff --git a/test/Table_Tests/src/Aggregate_Column_Spec.enso b/test/Table_Tests/src/Aggregate_Column_Spec.enso index 6048c106619cf..327926922bb8e 100644 --- a/test/Table_Tests/src/Aggregate_Column_Spec.enso +++ b/test/Table_Tests/src/Aggregate_Column_Spec.enso @@ -6,33 +6,37 @@ from Standard.Table.Data.Aggregate_Column import all import Standard.Test spec = Test.group "Aggregate Columns" <| - simple_table = Table.new [["count", [1, 2, Nothing, 3]], ["is_valid", [True, False, True, False]], ["text", ["A", "", Nothing, "B"]]] + simple_table = Table.new [["count", [1, 2, Nothing, 3]], ["is_valid", [True, False, True, False]], ["float", [1, 2.1, 3.4, 5.6]], ["text", ["A", "", Nothing, "B"]]] text_col = simple_table.at "text" + float_col = simple_table.at "float" empty_table = Table.new [["count", []], ["is_valid", []], ["text", []]] test_name = "Test Column" - test_aggregator table col expected_name expected_result = + test_aggregator table col expected_name expected_result epsilon=False = col.column_name table . should_equal expected_name acc = col.make_aggregator table folded_value = 0.up_to table.row_count . fold col.initial_value acc - col.evaluate folded_value . should_equal expected_result + result = col.evaluate folded_value + + if epsilon != False then (result.should_equal expected_result epsilon=epsilon) else + result.should_equal expected_result Test.specify "should be able to count a set" <| test_aggregator simple_table (Count Nothing) "Count" simple_table.row_count test_aggregator simple_table (Count test_name) test_name simple_table.row_count test_aggregator empty_table (Count test_name) test_name empty_table.row_count - Test.specify "should be able to count NULLs in a set" <| - test_aggregator simple_table (Count_Nothing 0) "Count Null count" 1 + Test.specify "should be able to count missing values in a set" <| + test_aggregator simple_table (Count_Nothing 0) "Count Nothing count" 1 test_aggregator simple_table (Count_Nothing 0 test_name) test_name 1 test_aggregator simple_table (Count_Nothing "text" test_name) test_name 1 test_aggregator simple_table (Count_Nothing text_col test_name) test_name 1 test_aggregator empty_table (Count_Nothing 0 test_name) test_name empty_table.row_count - Test.specify "should be able to count not NULLs in a set" <| - test_aggregator simple_table (Count_Not_Nothing 0) "Count NotNull count" 3 + Test.specify "should be able to count non missing values in a set" <| + test_aggregator simple_table (Count_Not_Nothing 0) "Count Not Nothing count" 3 test_aggregator simple_table (Count_Not_Nothing 0 test_name) test_name 3 test_aggregator simple_table (Count_Not_Nothing "text" test_name) test_name 3 test_aggregator simple_table (Count_Not_Nothing text_col test_name) test_name 3 @@ -52,4 +56,18 @@ spec = Test.group "Aggregate Columns" <| test_aggregator simple_table (Count_Not_Empty text_col test_name) test_name 2 test_aggregator empty_table (Count_Not_Empty 0 test_name) test_name empty_table.row_count + Test.specify "should be able to total a set of values" <| + test_aggregator simple_table (Sum -2) "Sum float" 12.1 + test_aggregator simple_table (Sum -2 test_name) test_name 12.1 + test_aggregator simple_table (Sum "float" test_name) test_name 12.1 + test_aggregator simple_table (Sum float_col test_name) test_name 12.1 + test_aggregator empty_table (Sum 0 test_name) test_name 0 + + Test.specify "should be able to total a set of values" <| + test_aggregator simple_table (Sum -2) "Sum float" 12.1 0.000001 + test_aggregator simple_table (Sum -2 test_name) test_name 12.1 0.000001 + test_aggregator simple_table (Sum "float" test_name) test_name 12.1 0.000001 + test_aggregator simple_table (Sum float_col test_name) test_name 12.1 0.000001 + test_aggregator empty_table (Sum 0 test_name) test_name 0 0.000001 + main = Test.Suite.run_main here.spec