Skip to content

Commit

Permalink
StDev Sample
Browse files Browse the repository at this point in the history
Use first/second
Expanding tests
  • Loading branch information
jdunkerley committed Mar 1, 2022
1 parent bf9bae8 commit 2f74a42
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
32 changes: 25 additions & 7 deletions test/Table_Tests/src/Aggregate_Column_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 2f74a42

Please sign in to comment.