2016-08-02 08:37:29 +02:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2015-08-19 22:14:34 +02:00
|
|
|
import Test.Hspec
|
|
|
|
|
2015-08-23 08:14:47 +02:00
|
|
|
import GEval.Core
|
|
|
|
import GEval.OptionsParser
|
2015-08-24 22:23:35 +02:00
|
|
|
import GEval.BLEU
|
2016-08-02 08:37:29 +02:00
|
|
|
import GEval.ClippEU
|
2016-08-01 22:47:43 +02:00
|
|
|
import GEval.PrecisionRecall
|
2017-03-24 18:26:12 +01:00
|
|
|
import GEval.ClusteringMetrics
|
2016-08-02 08:37:29 +02:00
|
|
|
import Data.Attoparsec.Text
|
2015-08-23 07:40:37 +02:00
|
|
|
import Options.Applicative
|
2016-08-02 08:37:29 +02:00
|
|
|
import Data.Text
|
2017-08-31 14:14:27 +02:00
|
|
|
import Text.EditDistance
|
|
|
|
|
2015-08-19 23:24:19 +02:00
|
|
|
import qualified Test.HUnit as HU
|
2015-08-19 22:14:34 +02:00
|
|
|
|
2017-03-25 22:11:23 +01:00
|
|
|
informationRetrievalBookExample :: [(String, Int)]
|
|
|
|
informationRetrievalBookExample = [("o", 2), ("o", 2), ("d", 2), ("x", 3), ("d", 3),
|
|
|
|
("x", 1), ("o", 1), ("x", 1), ( "x", 1), ("x", 1), ("x", 1),
|
|
|
|
("x", 2), ("o", 2), ("o", 2),
|
|
|
|
("x", 3), ("d", 3), ("d", 3)]
|
|
|
|
|
|
|
|
perfectClustering :: [(Int, Char)]
|
|
|
|
perfectClustering = [(0, 'a'), (2, 'b'), (3, 'c'), (2, 'b'), (2, 'b'), (1, 'd'), (0, 'a')]
|
|
|
|
|
|
|
|
stupidClusteringOneBigCluster :: [(Int, Int)]
|
|
|
|
stupidClusteringOneBigCluster = [(0, 2), (2, 2), (1, 2), (2, 2), (0, 2), (0, 2), (0, 2), (0, 2), (1, 2), (1, 2)]
|
|
|
|
|
|
|
|
stupidClusteringManySmallClusters :: [(Int, Int)]
|
|
|
|
stupidClusteringManySmallClusters = [(0, 0), (2, 1), (1, 2), (2, 3), (0, 4), (0, 5), (0, 6), (0, 7), (1, 8), (1, 9)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-08-17 23:32:00 +02:00
|
|
|
main :: IO ()
|
2015-08-19 22:14:34 +02:00
|
|
|
main = hspec $ do
|
2015-08-23 07:40:37 +02:00
|
|
|
describe "root mean square error" $ do
|
2015-08-19 22:14:34 +02:00
|
|
|
it "simple test" $ do
|
2015-08-23 07:40:37 +02:00
|
|
|
geval (defaultGEvalSpecification {gesExpectedDirectory=Just "test/rmse-simple/rmse-simple", gesOutDirectory="test/rmse-simple/rmse-simple-solution"}) `shouldReturnAlmost` 0.64549722436790
|
|
|
|
describe "mean square error" $ do
|
2015-11-06 22:02:01 +01:00
|
|
|
it "simple test with arguments" $
|
|
|
|
runGEvalTest "mse-simple" `shouldReturnAlmost` 0.4166666666666667
|
2015-08-24 23:40:40 +02:00
|
|
|
describe "BLEU" $ do
|
2015-11-06 22:02:01 +01:00
|
|
|
it "trivial example from Wikipedia" $
|
|
|
|
runGEvalTest "bleu-trivial" `shouldReturnAlmost` 0.0
|
|
|
|
it "complex example" $
|
|
|
|
runGEvalTest "bleu-complex" `shouldReturnAlmost` 0.6211
|
|
|
|
it "perfect translation" $
|
|
|
|
runGEvalTest "bleu-perfect" `shouldReturnAlmost` 1.0000
|
2017-10-04 21:56:17 +02:00
|
|
|
it "empty translation" $
|
|
|
|
runGEvalTest "bleu-empty" `shouldReturnAlmost` 0.0000
|
2015-10-31 19:05:23 +01:00
|
|
|
describe "Accuracy" $ do
|
2015-11-06 22:02:01 +01:00
|
|
|
it "simple example" $
|
|
|
|
runGEvalTest "accuracy-simple" `shouldReturnAlmost` 0.6
|
2016-12-03 09:18:04 +01:00
|
|
|
describe "F-measure" $ do
|
|
|
|
it "simple example" $
|
|
|
|
runGEvalTest "f-measure-simple" `shouldReturnAlmost` 0.57142857
|
|
|
|
it "perfect classifier" $
|
|
|
|
runGEvalTest "f-measure-perfect" `shouldReturnAlmost` 1.0
|
|
|
|
it "stupid classifier" $
|
|
|
|
runGEvalTest "f-measure-stupid" `shouldReturnAlmost` 0.0
|
|
|
|
it "all false" $
|
|
|
|
runGEvalTest "f-measure-all-false" `shouldReturnAlmost` 1.0
|
|
|
|
it "F2-measure" $
|
|
|
|
runGEvalTest "f2-simple" `shouldReturnAlmost` 0.714285714
|
2015-08-24 22:23:35 +02:00
|
|
|
describe "precision count" $ do
|
|
|
|
it "simple test" $ do
|
|
|
|
precisionCount [["Alice", "has", "a", "cat" ]] ["Ala", "has", "cat"] `shouldBe` 2
|
|
|
|
it "none found" $ do
|
|
|
|
precisionCount [["Alice", "has", "a", "cat" ]] ["for", "bar", "baz"] `shouldBe` 0
|
|
|
|
it "multiple values" $ do
|
|
|
|
precisionCount [["bar", "bar", "bar", "bar", "foo", "xyz", "foo"]] ["foo", "bar", "foo", "baz", "bar", "foo"] `shouldBe` 4
|
|
|
|
it "multiple refs" $ do
|
|
|
|
precisionCount [["foo", "baz"], ["bar"], ["baz", "xyz"]] ["foo", "bar", "foo"] `shouldBe` 2
|
2017-03-24 08:50:57 +01:00
|
|
|
describe "purity (in flat clustering)" $ do
|
|
|
|
it "the example from Information Retrieval Book" $ do
|
2017-03-25 22:11:23 +01:00
|
|
|
purity informationRetrievalBookExample `shouldBeAlmost` 0.70588
|
|
|
|
describe "NMI (in flat clustering)" $ do
|
|
|
|
it "the example from Information Retrieval Book" $ do
|
|
|
|
normalizedMutualInformation informationRetrievalBookExample `shouldBeAlmost` 0.36456
|
|
|
|
it "perfect clustering" $ do
|
|
|
|
normalizedMutualInformation perfectClustering `shouldBeAlmost` 1.0
|
|
|
|
it "stupid clustering with one big cluster" $ do
|
|
|
|
normalizedMutualInformation stupidClusteringOneBigCluster `shouldBeAlmost` 0.0
|
|
|
|
it "stupid clustering with many small clusters" $ do
|
|
|
|
normalizedMutualInformation stupidClusteringManySmallClusters `shouldBeAlmost` 0.61799
|
2017-03-26 08:01:19 +02:00
|
|
|
describe "NMI challenge" $ do
|
|
|
|
it "complex test" $ do
|
|
|
|
runGEvalTest "nmi-complex" `shouldReturnAlmost` 0.36456
|
2017-04-01 12:24:36 +02:00
|
|
|
describe "LogLossHashed challenge" $ do
|
|
|
|
it "simple example" $ do
|
|
|
|
runGEvalTest "log-loss-hashed-simple" `shouldReturnAlmost` 2.398479083333333
|
2017-04-03 10:07:58 +02:00
|
|
|
it "example with unnormalized values" $ do
|
|
|
|
runGEvalTest "log-loss-hashed-not-normalized" `shouldReturnAlmost` 1.0468455186722887
|
2015-12-20 16:49:17 +01:00
|
|
|
describe "reading options" $ do
|
|
|
|
it "can get the metric" $ do
|
|
|
|
extractMetric "bleu-complex" `shouldReturn` (Just BLEU)
|
2015-11-06 21:57:36 +01:00
|
|
|
describe "error handling" $ do
|
|
|
|
it "too few lines are handled" $ do
|
|
|
|
runGEvalTest "error-too-few-lines" `shouldThrow` (== TooFewLines)
|
|
|
|
it "too many lines are handled" $ do
|
|
|
|
runGEvalTest "error-too-many-lines" `shouldThrow` (== TooManyLines)
|
2015-11-06 22:42:08 +01:00
|
|
|
it "empty output is handled" $ do
|
|
|
|
runGEvalTest "empty-output" `shouldThrow` (== EmptyOutput)
|
2015-11-06 23:14:10 +01:00
|
|
|
it "unexpected data is handled" $
|
|
|
|
runGEvalTest "unexpected-data" `shouldThrow` (== UnexpectedData "input does not start with a digit")
|
2015-11-06 23:24:46 +01:00
|
|
|
it "unwanted data is handled" $
|
|
|
|
runGEvalTest "unwanted-data" `shouldThrow` (== UnexpectedData "number expected")
|
2016-08-01 22:47:43 +02:00
|
|
|
describe "precision and recall" $ do
|
|
|
|
it "null test" $ do
|
|
|
|
precision neverMatch ['a', 'b', 'c'] [0, 1, 2, 3, 4, 5] `shouldBeAlmost` 0.0
|
|
|
|
recall neverMatch ['a', 'b', 'c'] [0, 1, 2, 3, 4, 5] `shouldBeAlmost` 0.0
|
|
|
|
f1Measure neverMatch ['a', 'b', 'c'] [0, 1, 2, 3, 4, 5] `shouldBeAlmost` 0.0
|
|
|
|
it "basic test" $ do
|
|
|
|
precision testMatchFun ['a', 'b', 'c'] [0, 1, 2, 3, 4, 5] `shouldBeAlmost` 0.3333333333333333
|
|
|
|
recall testMatchFun ['a', 'b', 'c'] [0, 1, 2, 3, 4, 5] `shouldBeAlmost` 0.66666666666666666
|
|
|
|
f1Measure testMatchFun ['a', 'b', 'c'] [0, 1, 2, 3, 4, 5] `shouldBeAlmost` 0.444444444444444
|
2016-08-02 07:17:57 +02:00
|
|
|
it "perfect result" $ do
|
|
|
|
precision alwaysMatch ['a', 'b', 'c'] [0, 1, 2] `shouldBeAlmost` 1.0
|
|
|
|
recall alwaysMatch ['a', 'b', 'c'] [0, 1, 2] `shouldBeAlmost` 1.0
|
|
|
|
f1Measure alwaysMatch ['a', 'b', 'c'] [0, 1, 2] `shouldBeAlmost` 1.0
|
|
|
|
it "full match" $ do
|
|
|
|
precision alwaysMatch ['a', 'b', 'c'] [0, 1, 2, 3, 4, 5] `shouldBeAlmost` 0.5
|
|
|
|
recall alwaysMatch ['a', 'b', 'c'] [0, 1, 2, 3, 4, 5] `shouldBeAlmost` 1.0
|
|
|
|
f1Measure alwaysMatch ['a', 'b', 'c'] [0, 1, 2, 3 , 4, 5] `shouldBeAlmost` 0.66666666666666
|
2016-08-02 08:37:29 +02:00
|
|
|
describe "ClippEU" $ do
|
|
|
|
it "parsing rectangles" $ do
|
|
|
|
let (Right r) = parseOnly (lineClippingsParser <* endOfInput) "2/0,0,2,3 10/20,30,40,50 18/0,1,500,3 "
|
|
|
|
r `shouldBe` [Clipping (PageNumber 2) (Rectangle (Point 0 0) (Point 2 3)),
|
|
|
|
Clipping (PageNumber 10) (Rectangle (Point 20 30) (Point 40 50)),
|
|
|
|
Clipping (PageNumber 18) (Rectangle (Point 0 1) (Point 500 3))]
|
|
|
|
it "no rectangles" $ do
|
|
|
|
let (Right r) = parseOnly (lineClippingsParser <* endOfInput) ""
|
|
|
|
r `shouldBe` []
|
|
|
|
it "just spaces" $ do
|
|
|
|
let (Right r) = parseOnly lineClippingsParser " "
|
|
|
|
r `shouldBe` []
|
|
|
|
it "parsing specs" $ do
|
|
|
|
let (Right r) = parseOnly lineClippingSpecsParser " 2/0,0,2,3/5 10/20,30,40,50/10"
|
2016-08-02 09:48:58 +02:00
|
|
|
r `shouldBe` [ClippingSpec (PageNumber 2) (Rectangle (Point 5 5) (Point 0 0))
|
2016-08-02 08:37:29 +02:00
|
|
|
(Rectangle (Point 0 0) (Point 7 8)),
|
2016-08-02 09:48:58 +02:00
|
|
|
ClippingSpec (PageNumber 10) (Rectangle (Point 30 40) (Point 30 40))
|
2016-08-02 08:37:29 +02:00
|
|
|
(Rectangle (Point 10 20) (Point 50 60))]
|
2016-08-02 09:48:58 +02:00
|
|
|
it "full test" $ do
|
|
|
|
runGEvalTest "clippeu-simple" `shouldReturnAlmost` 0.399999999999
|
2016-12-03 09:18:04 +01:00
|
|
|
describe "evaluation metric specification is parsed" $ do
|
|
|
|
it "for simple names" $ do
|
|
|
|
let metrics = [RMSE, MSE, BLEU, Accuracy, ClippEU]
|
|
|
|
let parsedMetrics = Prelude.map (read . show) metrics
|
|
|
|
metrics `shouldBe` parsedMetrics
|
|
|
|
it "for F-Measure" $ do
|
|
|
|
read "F2" `shouldBe` (FMeasure 2.0)
|
|
|
|
read "F1" `shouldBe` (FMeasure 1.0)
|
|
|
|
read "F0.5" `shouldBe` (FMeasure 0.5)
|
2017-08-31 14:14:27 +02:00
|
|
|
describe "test edit-distance library" $ do
|
|
|
|
it "for handling UTF8" $ do
|
|
|
|
levenshteinDistance defaultEditCosts "źdźbło" "źd好bło" `shouldBe` 1
|
|
|
|
levenshteinDistance defaultEditCosts "źdźbło" "źdźcło" `shouldBe` 1
|
|
|
|
describe "CharMatch" $ do
|
|
|
|
it "simple test" $ do
|
2017-09-05 21:36:05 +02:00
|
|
|
runGEvalTest "charmatch-simple" `shouldReturnAlmost` 0.3571428571428571
|
2017-08-31 14:14:27 +02:00
|
|
|
it "perfect solution" $ do
|
|
|
|
runGEvalTest "charmatch-perfect" `shouldReturnAlmost` 1.0
|
|
|
|
it "more complex test" $ do
|
2017-09-05 21:36:05 +02:00
|
|
|
runGEvalTest "charmatch-complex" `shouldReturnAlmost` 0.1923076923076923
|
2017-08-31 14:14:27 +02:00
|
|
|
it "broken test without input" $ do
|
|
|
|
runGEvalTest "charmatch-no-input" `shouldThrow` (== NoInputFile "test/charmatch-no-input/charmatch-no-input/test-A/in.tsv")
|
2017-12-12 07:54:21 +01:00
|
|
|
describe "MAP" $ do
|
|
|
|
it "one result" $ do
|
|
|
|
(calculateMAPForOneResult ["Berlin", "London", "Warsaw"]
|
|
|
|
["Warsaw", "Moscow", "Berlin", "Prague"]) `shouldBeAlmost` 0.55555555
|
|
|
|
it "check whether you cannot cheat with duplicated results" $ do
|
|
|
|
(calculateMAPForOneResult ["one", "two"]
|
|
|
|
["one", "one"]) `shouldBeAlmost` 0.5
|
|
|
|
it "simple test" $ do
|
|
|
|
runGEvalTest "map-simple" `shouldReturnAlmost` 0.444444444
|
2015-11-06 23:24:46 +01:00
|
|
|
|
2016-08-01 22:47:43 +02:00
|
|
|
neverMatch :: Char -> Int -> Bool
|
|
|
|
neverMatch _ _ = False
|
|
|
|
|
2016-08-02 07:17:57 +02:00
|
|
|
alwaysMatch :: Char -> Int -> Bool
|
|
|
|
alwaysMatch _ _ = True
|
|
|
|
|
2016-08-01 22:47:43 +02:00
|
|
|
testMatchFun :: Char -> Int -> Bool
|
|
|
|
testMatchFun 'a' 1 = True
|
|
|
|
testMatchFun 'a' 2 = True
|
|
|
|
testMatchFun 'a' 3 = True
|
|
|
|
testMatchFun 'b' 1 = True
|
|
|
|
testMatchFun 'c' 1 = True
|
|
|
|
testMatchFun _ _ = False
|
2015-08-23 07:40:37 +02:00
|
|
|
|
|
|
|
extractVal :: (Either (ParserResult GEvalOptions) (Maybe MetricValue)) -> IO MetricValue
|
|
|
|
extractVal (Right (Just val)) = return val
|
2015-08-19 23:24:19 +02:00
|
|
|
|
2015-11-06 21:57:36 +01:00
|
|
|
runGEvalTest testName = (runGEval [
|
|
|
|
"--expected-directory",
|
|
|
|
"test/" ++ testName ++ "/" ++ testName,
|
|
|
|
"--out-directory",
|
|
|
|
"test/" ++ testName ++ "/" ++ testName ++ "-solution"]) >>= extractVal
|
|
|
|
|
2015-12-20 16:49:17 +01:00
|
|
|
extractMetric :: String -> IO (Maybe Metric)
|
|
|
|
extractMetric testName = do
|
|
|
|
result <- getOptions ["--expected-directory", "test/" ++ testName ++ "/" ++ testName]
|
|
|
|
return $ case result of
|
|
|
|
Left _ -> Nothing
|
|
|
|
Right opts -> Just $ gesMetric $ geoSpec opts
|
|
|
|
|
2015-08-19 23:24:19 +02:00
|
|
|
class AEq a where
|
|
|
|
(=~) :: a -> a -> Bool
|
|
|
|
|
|
|
|
instance AEq Double where
|
2015-08-25 16:10:20 +02:00
|
|
|
x =~ y = abs ( x - y ) < (1.0e-4 :: Double)
|
2015-08-19 23:24:19 +02:00
|
|
|
|
|
|
|
(@=~?) :: (Show a, AEq a) => a -> a -> HU.Assertion
|
2016-08-01 22:47:43 +02:00
|
|
|
(@=~?) actual expected = expected =~ actual HU.@? assertionMsg
|
2015-08-19 23:24:19 +02:00
|
|
|
where
|
|
|
|
assertionMsg = "Expected : " ++ show expected ++
|
|
|
|
"\nActual : " ++ show actual
|
|
|
|
|
2016-08-01 22:47:43 +02:00
|
|
|
shouldBeAlmost got expected = got @=~? expected
|
|
|
|
|
2015-08-19 23:24:19 +02:00
|
|
|
shouldReturnAlmost :: (AEq a, Show a, Eq a) => IO a -> a -> Expectation
|
|
|
|
shouldReturnAlmost action expected = action >>= (@=~? expected)
|