Commit 35e3d393 authored by Grégoire Locqueville's avatar Grégoire Locqueville

A bit of cleanup. Also make all imports explicit or qualified

parent 06ca7c6f
Pipeline #7496 passed with stages
in 47 minutes and 39 seconds
...@@ -12,16 +12,17 @@ import Data.Array.Accelerate.Interpreter qualified as Naive ...@@ -12,16 +12,17 @@ import Data.Array.Accelerate.Interpreter qualified as Naive
import Data.Array.Accelerate qualified as A import Data.Array.Accelerate qualified as A
import Data.Functor ((<&>)) import Data.Functor ((<&>))
import Data.Massiv.Array qualified as Massiv import Data.Massiv.Array qualified as Massiv
import Data.Proxy import Data.Proxy (Proxy(Proxy))
import Data.Scientific import Data.Scientific (Scientific, fromFloatDigits)
import Gargantext.Core.LinearAlgebra qualified as LA import Gargantext.Core.LinearAlgebra qualified as LA
import Gargantext.Core.Methods.Matrix.Accelerate.Utils qualified as A import Gargantext.Core.Methods.Matrix.Accelerate.Utils qualified as A
import Gargantext.Core.Methods.Matrix.Accelerate.Utils qualified as Legacy import Gargantext.Core.Methods.Matrix.Accelerate.Utils qualified as Legacy
import Gargantext.Core.Methods.Similarities.Accelerate.Distributional qualified as Legacy import Gargantext.Core.Methods.Similarities.Accelerate.Distributional qualified as Legacy
import Gargantext.Orphans.Accelerate (sliceArray) import Gargantext.Orphans.Accelerate (sliceArray)
import Prelude hiding ((^)) import Prelude hiding ((^))
import Test.Tasty import Test.Tasty (TestTree, testGroup)
import Test.Tasty.QuickCheck import qualified Test.Tasty.QuickCheck as QC
import Test.Tasty.QuickCheck ((===), (.&&.))
-- --
...@@ -31,11 +32,12 @@ import Test.Tasty.QuickCheck ...@@ -31,11 +32,12 @@ import Test.Tasty.QuickCheck
newtype SquareMatrix a = SquareMatrix { _SquareMatrix :: Matrix a } newtype SquareMatrix a = SquareMatrix { _SquareMatrix :: Matrix a }
deriving newtype (Show, Eq) deriving newtype (Show, Eq)
instance (Elt a, Show a, Prelude.Num a, Ord a, Arbitrary a) => Arbitrary (SquareMatrix a) where instance (Elt a, Show a, Prelude.Num a, Ord a, QC.Arbitrary a) =>
QC.Arbitrary (SquareMatrix a) where
arbitrary = do arbitrary = do
x <- choose (1,30) x <- QC.choose (1,30)
let sh = Z :. x :. x let sh = Z :. x :. x
SquareMatrix . A.fromList sh <$> vectorOf (x*x) arbitrary SquareMatrix . A.fromList sh <$> QC.vectorOf (x*x) QC.arbitrary
shrink = map SquareMatrix . sliceArray . _SquareMatrix shrink = map SquareMatrix . sliceArray . _SquareMatrix
...@@ -45,16 +47,16 @@ instance (Elt a, Show a, Prelude.Num a, Ord a, Arbitrary a) => Arbitrary (Square ...@@ -45,16 +47,16 @@ instance (Elt a, Show a, Prelude.Num a, Ord a, Arbitrary a) => Arbitrary (Square
newtype CoocMatrix = CoocMatrix { _CoocMatrix :: Matrix Int } newtype CoocMatrix = CoocMatrix { _CoocMatrix :: Matrix Int }
deriving newtype (Show, Eq) deriving newtype (Show, Eq)
instance Arbitrary CoocMatrix where instance QC.Arbitrary CoocMatrix where
-- | We simulate the creation of a cooccurence matrix: there are `numContexts` -- | We simulate the creation of a cooccurence matrix: there are `numContexts`
-- "virtual contexts" and `numTerms` "virtual terms"; for each context and -- "virtual contexts" and `numTerms` "virtual terms"; for each context and
-- each term, the term has probability `probAppearance` to appear in the -- each term, the term has probability `probAppearance` to appear in the
-- context. The generated matrix is the cooccurence matrix of the resulting -- context. The generated matrix is the cooccurence matrix of the resulting
-- "virtual corpus" -- "virtual corpus"
arbitrary = do arbitrary = do
numContexts <- choose (1, 30) numContexts <- QC.choose (1, 30)
numTerms <- choose (1, 30) numTerms <- QC.choose (1, 30)
probAppearance <- (choose (0, 1) :: Gen Double) probAppearance <- (QC.choose (0, 1) :: QC.Gen Double)
-- `appearances` is a list of lists of integers encoding which virtual terms -- `appearances` is a list of lists of integers encoding which virtual terms
-- appear in which virtual contexts. More specifically, the integer j -- appear in which virtual contexts. More specifically, the integer j
-- appears in the i-th list iff the virtual term j appears in the i-th -- appears in the i-th list iff the virtual term j appears in the i-th
...@@ -63,7 +65,7 @@ instance Arbitrary CoocMatrix where ...@@ -63,7 +65,7 @@ instance Arbitrary CoocMatrix where
-- In a given virtual context, iterate over all terms and pick whether -- In a given virtual context, iterate over all terms and pick whether
-- they appear in the context with probability `probAppearance` -- they appear in the context with probability `probAppearance`
foldM (\termsSoFar candidateTerm -> do foldM (\termsSoFar candidateTerm -> do
randomVar <- choose (0, 1) randomVar <- QC.choose (0, 1)
return $ if randomVar < probAppearance then candidateTerm : termsSoFar return $ if randomVar < probAppearance then candidateTerm : termsSoFar
else termsSoFar else termsSoFar
) [] [1..numTerms] ) [] [1..numTerms]
...@@ -71,7 +73,7 @@ instance Arbitrary CoocMatrix where ...@@ -71,7 +73,7 @@ instance Arbitrary CoocMatrix where
[1..numTerms] <&> (\j -> [1..numTerms] <&> (\j ->
(i, j) )) (i, j) ))
-- For each pair of virtual terms, iterate over all virtual contexts and add -- For each pair of virtual terms, iterate over all virtual contexts and add
-- 1 each time both term appear in the context: -- 1 each time both terms appear in the context:
let coocMatrix = (fmap . fmap) (\(i, j) -> let coocMatrix = (fmap . fmap) (\(i, j) ->
foldr (\contextTerms currentCoocCount -> foldr (\contextTerms currentCoocCount ->
if i `elem` contextTerms && j `elem` contextTerms if i `elem` contextTerms && j `elem` contextTerms
...@@ -90,7 +92,7 @@ twoByTwo :: SquareMatrix Int ...@@ -90,7 +92,7 @@ twoByTwo :: SquareMatrix Int
twoByTwo = SquareMatrix $ fromList (Z :. 2 :. 2) (Prelude.replicate 4 5) twoByTwo = SquareMatrix $ fromList (Z :. 2 :. 2) (Prelude.replicate 4 5)
testMatrix_01 :: SquareMatrix Int testMatrix_01 :: SquareMatrix Int
testMatrix_01 = SquareMatrix $ fromList (Z :. 14 :. 14) $ testMatrix_01 = SquareMatrix $ fromList (Z :. 14 :. 14)
[ 30, 36, -36, -16, 0, 7, 34, -7, 5, -4, 0, 21, 6, -35, [ 30, 36, -36, -16, 0, 7, 34, -7, 5, -4, 0, 21, 6, -35,
0, -31, 20, -15, -22, -7, -22, -37, -29, -29, 23, -31, -29, -23, 0, -31, 20, -15, -22, -7, -22, -37, -29, -29, 23, -31, -29, -23,
-24, -29, 19, -6, 16, 7, 15, -27, -27, -30, -9, -33, 18, -23, -24, -29, 19, -6, 16, 7, 15, -27, -27, -30, -9, -33, 18, -23,
...@@ -107,7 +109,7 @@ testMatrix_01 = SquareMatrix $ fromList (Z :. 14 :. 14) $ ...@@ -107,7 +109,7 @@ testMatrix_01 = SquareMatrix $ fromList (Z :. 14 :. 14) $
13, -37, -16, 2, 7, -13, 21, -10, -33, -33, -26, -19, -1, 29] 13, -37, -16, 2, 7, -13, 21, -10, -33, -33, -26, -19, -1, 29]
testMatrix_02 :: SquareMatrix Int testMatrix_02 :: SquareMatrix Int
testMatrix_02 = SquareMatrix $ fromList (Z :. 7 :. 7) $ testMatrix_02 = SquareMatrix $ fromList (Z :. 7 :. 7)
[ 30, 36, -36, -16, 0, 7, 34, [ 30, 36, -36, -16, 0, 7, 34,
0, -31, 20, -15, -22, -7, -22, 0, -31, 20, -15, -22, -7, -22,
-24, -29, 19, -6, 16, 7, 15, -24, -29, 19, -6, 16, 7, 15,
...@@ -117,7 +119,7 @@ testMatrix_02 = SquareMatrix $ fromList (Z :. 7 :. 7) $ ...@@ -117,7 +119,7 @@ testMatrix_02 = SquareMatrix $ fromList (Z :. 7 :. 7) $
13, -37, -16, 2, 7, -13, 21] 13, -37, -16, 2, 7, -13, 21]
testMatrix_03 :: SquareMatrix Int testMatrix_03 :: SquareMatrix Int
testMatrix_03 = SquareMatrix $ fromList (Z :. 11 :. 11) $ testMatrix_03 = SquareMatrix $ fromList (Z :. 11 :. 11)
[ 1, -1, 1, 0, 1, -1, 0, 1, 1, 0, 0, [ 1, -1, 1, 0, 1, -1, 0, 1, 1, 0, 0,
1, 1, 1, 1, 1, 0, 1, -1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, -1, 1, 0, 0,
-1, 1, 0, -1, 0, -1, 0, 1, 0, -1, 0, -1, 1, 0, -1, 0, -1, 0, 1, 0, -1, 0,
...@@ -131,7 +133,7 @@ testMatrix_03 = SquareMatrix $ fromList (Z :. 11 :. 11) $ ...@@ -131,7 +133,7 @@ testMatrix_03 = SquareMatrix $ fromList (Z :. 11 :. 11) $
1, 1, -1, 0, -1, -1, 1, 0, 1, 0, -1] 1, 1, -1, 0, -1, -1, 1, 0, 1, 0, -1]
testMatrix_04 :: SquareMatrix Int testMatrix_04 :: SquareMatrix Int
testMatrix_04 = SquareMatrix $ fromList (Z :. 8 :. 8) $ testMatrix_04 = SquareMatrix $ fromList (Z :. 8 :. 8)
[ 3, -1, 0, 1, -1, 1, 1, -3, [ 3, -1, 0, 1, -1, 1, 1, -3,
-2, -2, 2, 1, 1, -2, 1, -1, -2, -2, 2, 1, 1, -2, 1, -1,
-2, -3, -1, 1, 1, -3, -2, -1, -2, -3, -1, 1, 1, -3, -2, -1,
...@@ -146,28 +148,28 @@ testMatrix_04 = SquareMatrix $ fromList (Z :. 8 :. 8) $ ...@@ -146,28 +148,28 @@ testMatrix_04 = SquareMatrix $ fromList (Z :. 8 :. 8) $
tests :: TestTree tests :: TestTree
tests = testGroup "LinearAlgebra" [ tests = testGroup "LinearAlgebra" [
testProperty "termDivNan" compareTermDivNan QC.testProperty "termDivNan" compareTermDivNan
, testProperty "diag" compareDiag , QC.testProperty "diag" compareDiag
, testProperty "sumRows" compareSumRows , QC.testProperty "sumRows" compareSumRows
, testProperty "matMaxMini" compareMatMaxMini , QC.testProperty "matMaxMini" compareMatMaxMini
, testProperty "sumM_go" compareSumM_go , QC.testProperty "sumM_go" compareSumM_go
, testProperty "sumMin_go" compareSumMin_go , QC.testProperty "sumMin_go" compareSumMin_go
, testProperty "matrixEye" compareMatrixEye , QC.testProperty "matrixEye" compareMatrixEye
, testProperty "diagNull" compareDiagNull , QC.testProperty "diagNull" compareDiagNull
, testGroup "distributional" [ , testGroup "distributional" [
testProperty "reference implementation roundtrips" compareDistributionalImplementations QC.testProperty "reference implementation roundtrips" compareDistributionalImplementations
, testProperty "2x2" (compareDistributional (Proxy @Double) twoByTwo) , QC.testProperty "2x2" (compareDistributional (Proxy @Double) twoByTwo)
, testProperty "7x7" (compareDistributional (Proxy @Double) testMatrix_02) , QC.testProperty "7x7" (compareDistributional (Proxy @Double) testMatrix_02)
, testProperty "14x14" (compareDistributional (Proxy @Double) testMatrix_01) , QC.testProperty "14x14" (compareDistributional (Proxy @Double) testMatrix_01)
, testProperty "roundtrips" (compareDistributional (Proxy @Double)) , QC.testProperty "roundtrips" (compareDistributional (Proxy @Double))
] ]
, testGroup "logDistributional2" [ , testGroup "logDistributional2" [
testProperty "2x2" (compareLogDistributional2 (Proxy @Double) twoByTwo) QC.testProperty "2x2" (compareLogDistributional2 (Proxy @Double) twoByTwo)
, testProperty "7x7" (compareLogDistributional2 (Proxy @Double) testMatrix_02) , QC.testProperty "7x7" (compareLogDistributional2 (Proxy @Double) testMatrix_02)
, testProperty "8x8" (compareLogDistributional2 (Proxy @Double) testMatrix_04) , QC.testProperty "8x8" (compareLogDistributional2 (Proxy @Double) testMatrix_04)
, testProperty "11x11" (compareLogDistributional2 (Proxy @Double) testMatrix_03) , QC.testProperty "11x11" (compareLogDistributional2 (Proxy @Double) testMatrix_03)
, testProperty "14x14" (compareLogDistributional2 (Proxy @Double) testMatrix_01) , QC.testProperty "14x14" (compareLogDistributional2 (Proxy @Double) testMatrix_01)
,testProperty "roundtrips" (compareLogDistributional2 (Proxy @Double)) ,QC.testProperty "roundtrips" (compareLogDistributional2 (Proxy @Double))
] ]
] ]
...@@ -177,27 +179,27 @@ tests = testGroup "LinearAlgebra" [ ...@@ -177,27 +179,27 @@ tests = testGroup "LinearAlgebra" [
compareTermDivNan :: (Array TermDivNanShape Double) compareTermDivNan :: (Array TermDivNanShape Double)
-> (Array TermDivNanShape Double) -> (Array TermDivNanShape Double)
-> Property -> QC.Property
compareTermDivNan i1 i2 compareTermDivNan i1 i2
= let massiv = LA.termDivNan @Massiv.U (LA.accelerate2MassivMatrix i1) (LA.accelerate2MassivMatrix i2) = let massiv = LA.termDivNan @Massiv.U (LA.accelerate2MassivMatrix i1) (LA.accelerate2MassivMatrix i2)
accelerate = Naive.run (Legacy.termDivNan (use i1) (use i2)) accelerate = Naive.run (Legacy.termDivNan (use i1) (use i2))
in accelerate === LA.massiv2AccelerateMatrix massiv in accelerate === LA.massiv2AccelerateMatrix massiv
compareDiag :: SquareMatrix Int -> Property compareDiag :: SquareMatrix Int -> QC.Property
compareDiag (SquareMatrix i1) compareDiag (SquareMatrix i1)
= let massiv = LA.diag (LA.accelerate2MassivMatrix i1) = let massiv = LA.diag (LA.accelerate2MassivMatrix i1)
accelerate = Naive.run (Legacy.diag (use i1)) accelerate = Naive.run (Legacy.diag (use i1))
in accelerate === LA.massiv2AccelerateVector massiv in accelerate === LA.massiv2AccelerateVector massiv
compareSumRows :: Array (Z :. Int :. Int :. Int) Int -> Property compareSumRows :: Array (Z :. Int :. Int :. Int) Int -> QC.Property
compareSumRows i1 compareSumRows i1
= let massiv = LA.sumRows @_ @Massiv.U (LA.accelerate2Massiv3DMatrix i1) = let massiv = LA.sumRows @_ @Massiv.U (LA.accelerate2Massiv3DMatrix i1)
massiv' = LA.sumRowsReferenceImplementation @Massiv.U (LA.accelerate2Massiv3DMatrix i1) massiv' = LA.sumRowsReferenceImplementation @Massiv.U (LA.accelerate2Massiv3DMatrix i1)
accelerate = Naive.run (A.sum (use i1)) accelerate = Naive.run (A.sum (use i1))
in counterexample "sumRows and reference implementation do not agree" (massiv === massiv') .&&. in QC.counterexample "sumRows and reference implementation do not agree" (massiv === massiv') .&&.
accelerate === LA.massiv2AccelerateMatrix massiv accelerate === LA.massiv2AccelerateMatrix massiv
compareDistributionalImplementations :: SquareMatrix Int -> Property compareDistributionalImplementations :: SquareMatrix Int -> QC.Property
compareDistributionalImplementations (SquareMatrix i1) = compareDistributionalImplementations (SquareMatrix i1) =
let ma = LA.accelerate2MassivMatrix i1 let ma = LA.accelerate2MassivMatrix i1
in LA.distributional @Massiv.U @Double ma === LA.distributionalReferenceImplementation ma in LA.distributional @Massiv.U @Double ma === LA.distributionalReferenceImplementation ma
...@@ -215,13 +217,13 @@ compareDistributional :: forall e. ...@@ -215,13 +217,13 @@ compareDistributional :: forall e.
, Monoid e , Monoid e
) => Proxy e ) => Proxy e
-> SquareMatrix Int -> SquareMatrix Int
-> Property -> QC.Property
compareDistributional Proxy (SquareMatrix i1) compareDistributional Proxy (SquareMatrix i1)
= let massiv = Massiv.computeAs Massiv.B $ LA.distributional @_ @e (LA.accelerate2MassivMatrix i1) = let massiv = Massiv.computeAs Massiv.B $ LA.distributional @_ @e (LA.accelerate2MassivMatrix i1)
accelerate = Legacy.distributionalWith Naive.run i1 accelerate = Legacy.distributionalWith Naive.run i1
expected = map conv (A.toList accelerate) expected = map conv (A.toList accelerate)
actual = map conv (mconcat (Massiv.toLists2 massiv)) actual = map conv (mconcat (Massiv.toLists2 massiv))
in counterexample "size not equal" (Prelude.length expected === Prelude.length actual) .&&. expected === actual in QC.counterexample "size not equal" (Prelude.length expected === Prelude.length actual) .&&. expected === actual
where where
conv :: e -> Scientific conv :: e -> Scientific
conv = fromFloatDigits conv = fromFloatDigits
...@@ -241,42 +243,42 @@ compareLogDistributional2 :: forall e. ...@@ -241,42 +243,42 @@ compareLogDistributional2 :: forall e.
, Monoid e , Monoid e
) => Proxy e ) => Proxy e
-> SquareMatrix Int -> SquareMatrix Int
-> Property -> QC.Property
compareLogDistributional2 Proxy (SquareMatrix i1) compareLogDistributional2 Proxy (SquareMatrix i1)
= let massiv = Massiv.computeAs Massiv.B $ LA.logDistributional2 @_ @e (LA.accelerate2MassivMatrix i1) = let massiv = Massiv.computeAs Massiv.B $ LA.logDistributional2 @_ @e (LA.accelerate2MassivMatrix i1)
accelerate = Legacy.logDistributional2With Naive.run i1 accelerate = Legacy.logDistributional2With Naive.run i1
expected = map conv (A.toList accelerate) expected = map conv (A.toList accelerate)
actual = map conv (mconcat (Massiv.toLists2 massiv)) actual = map conv (mconcat (Massiv.toLists2 massiv))
in counterexample "size not equal" (Prelude.length expected === Prelude.length actual) .&&. expected === actual in QC.counterexample "size not equal" (Prelude.length expected === Prelude.length actual) .&&. expected === actual
where where
conv :: e -> Scientific conv :: e -> Scientific
conv = fromFloatDigits conv = fromFloatDigits
compareMatMaxMini :: SquareMatrix Int -> Property compareMatMaxMini :: SquareMatrix Int -> QC.Property
compareMatMaxMini (SquareMatrix i1) compareMatMaxMini (SquareMatrix i1)
= let massiv = LA.matMaxMini (LA.accelerate2MassivMatrix i1) = let massiv = LA.matMaxMini (LA.accelerate2MassivMatrix i1)
accelerate = Naive.run (A.matMaxMini (use i1)) accelerate = Naive.run (A.matMaxMini (use i1))
in accelerate === LA.massiv2AccelerateMatrix massiv in accelerate === LA.massiv2AccelerateMatrix massiv
compareSumMin_go :: SquareMatrix Int -> Property compareSumMin_go :: SquareMatrix Int -> QC.Property
compareSumMin_go (SquareMatrix i1) compareSumMin_go (SquareMatrix i1)
= let massiv = LA.sumMin_go (A.dim i1) (LA.accelerate2MassivMatrix i1) = let massiv = LA.sumMin_go (A.dim i1) (LA.accelerate2MassivMatrix i1)
accelerate = Naive.run (Legacy.sumMin_go (A.dim i1) (use i1)) accelerate = Naive.run (Legacy.sumMin_go (A.dim i1) (use i1))
in accelerate === LA.massiv2AccelerateMatrix massiv in accelerate === LA.massiv2AccelerateMatrix massiv
compareSumM_go :: SquareMatrix Int -> Property compareSumM_go :: SquareMatrix Int -> QC.Property
compareSumM_go (SquareMatrix i1) compareSumM_go (SquareMatrix i1)
= let massiv = LA.sumM_go (A.dim i1) (LA.accelerate2MassivMatrix i1) = let massiv = LA.sumM_go (A.dim i1) (LA.accelerate2MassivMatrix i1)
accelerate = Naive.run (Legacy.sumM_go (A.dim i1) (use i1)) accelerate = Naive.run (Legacy.sumM_go (A.dim i1) (use i1))
in accelerate === LA.massiv2AccelerateMatrix massiv in accelerate === LA.massiv2AccelerateMatrix massiv
compareMatrixEye :: Positive Int -> Property compareMatrixEye :: QC.Positive Int -> QC.Property
compareMatrixEye (getPositive -> n) compareMatrixEye (QC.getPositive -> n)
= let massiv = Massiv.compute @Massiv.U $ LA.matrixEye @Int n = let massiv = Massiv.compute @Massiv.U $ LA.matrixEye @Int n
accelerate = Naive.run (Legacy.matrixEye n) accelerate = Naive.run (Legacy.matrixEye n)
in accelerate === LA.massiv2AccelerateMatrix massiv in accelerate === LA.massiv2AccelerateMatrix massiv
compareDiagNull :: SquareMatrix Int -> Property compareDiagNull :: SquareMatrix Int -> QC.Property
compareDiagNull (SquareMatrix i1) compareDiagNull (SquareMatrix i1)
= let massiv = Massiv.compute @Massiv.U $ LA.diagNull (A.dim i1) (LA.accelerate2MassivMatrix i1) = let massiv = Massiv.compute @Massiv.U $ LA.diagNull (A.dim i1) (LA.accelerate2MassivMatrix i1)
accelerate = Naive.run (Legacy.diagNull (A.dim i1) (use i1)) accelerate = Naive.run (Legacy.diagNull (A.dim i1) (use i1))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment