Commit 67a730e4 authored by Alexandre Delanoë's avatar Alexandre Delanoë

[TEST] hspec

parent 6050f41e
...@@ -4,7 +4,7 @@ cabal-version: 1.12 ...@@ -4,7 +4,7 @@ cabal-version: 1.12
-- --
-- see: https://github.com/sol/hpack -- see: https://github.com/sol/hpack
-- --
-- hash: c963b0693d31715c24d7005b13eb4c408cd00c23009b3e68b6dbf329a47db200 -- hash: f8da0e8f0d95addc40b203c0fc52a4e61e8af8847f134c289ba4acc5bfefd8d7
name: gargantext-graph name: gargantext-graph
version: 0.1.0.0 version: 0.1.0.0
...@@ -102,7 +102,7 @@ executable gargantext-graph-exe ...@@ -102,7 +102,7 @@ executable gargantext-graph-exe
test-suite gargantext-graph-test test-suite gargantext-graph-test
type: exitcode-stdio-1.0 type: exitcode-stdio-1.0
main-is: Spec.hs main-is: Main.hs
other-modules: other-modules:
Paths_gargantext_graph Paths_gargantext_graph
hs-source-dirs: hs-source-dirs:
...@@ -110,7 +110,8 @@ test-suite gargantext-graph-test ...@@ -110,7 +110,8 @@ test-suite gargantext-graph-test
default-extensions: DataKinds FlexibleInstances OverloadedStrings default-extensions: DataKinds FlexibleInstances OverloadedStrings
ghc-options: -threaded -rtsopts -with-rtsopts=-N ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends: build-depends:
accelerate QuickCheck
, accelerate
, accelerate-arithmetic , accelerate-arithmetic
, accelerate-utility , accelerate-utility
, base >=4.7 && <5 , base >=4.7 && <5
...@@ -122,6 +123,7 @@ test-suite gargantext-graph-test ...@@ -122,6 +123,7 @@ test-suite gargantext-graph-test
, fgl , fgl
, gargantext-graph , gargantext-graph
, haskell-igraph >=0.6.0 , haskell-igraph >=0.6.0
, hspec
, lens , lens
, pretty-simple , pretty-simple
, protolude , protolude
......
#!/bin/bash #!/bin/bash
stack install --profile # --test # --haddock stack install --profile --test # --haddock
...@@ -62,10 +62,11 @@ executables: ...@@ -62,10 +62,11 @@ executables:
- -Wcompat - -Wcompat
dependencies: dependencies:
- gargantext-graph - gargantext-graph
- criterion
tests: tests:
gargantext-graph-test: gargantext-graph-test:
main: Spec.hs main: Main.hs
source-dirs: test source-dirs: test
ghc-options: ghc-options:
- -threaded - -threaded
...@@ -73,3 +74,5 @@ tests: ...@@ -73,3 +74,5 @@ tests:
- -with-rtsopts=-N - -with-rtsopts=-N
dependencies: dependencies:
- gargantext-graph - gargantext-graph
- hspec
- QuickCheck
...@@ -152,9 +152,9 @@ adjacent (FiniteGraph g) isReflexive = ...@@ -152,9 +152,9 @@ adjacent (FiniteGraph g) isReflexive =
transition :: KnownNat n transition :: KnownNat n
=> AdjacencyMatrix n => AdjacencyMatrix n
-> TransitionMatrix n -> TransitionMatrix n
transition m = SMatrix.imap (\i j v -> v * (List.!!) s i) m transition m = SMatrix.imap (\i j v -> v * (VS.!) s i) m
where where
s = sumWith' Colonne (\s -> 1 / s) m s = sumWith Colonne (\s -> 1 / s) m
proxemie :: KnownNat n proxemie :: KnownNat n
...@@ -175,13 +175,13 @@ matconf :: forall n. KnownNat n ...@@ -175,13 +175,13 @@ matconf :: forall n. KnownNat n
matconf False a p = symmetry confmat matconf False a p = symmetry confmat
where where
-- vcount = natToInt @n -- vcount = natToInt @n
degs = sumWith' Colonne identity a degs = sumWith Colonne identity a
sumdeg = List.sum degs sumdeg = VS.sum degs
confmat = DMatrix.imap (\ x y v -> confmat = DMatrix.imap (\ x y v ->
if x < y if x < y
then let then let
prox_y_x_length = v prox_y_x_length = v
prox_y_x_infini = ((List.!!) degs x) / sumdeg prox_y_x_infini = ((VS.!) degs x) / sumdeg
in in
(prox_y_x_length - prox_y_x_infini) (prox_y_x_length - prox_y_x_infini)
/ (prox_y_x_length + prox_y_x_infini) / (prox_y_x_length + prox_y_x_infini)
...@@ -219,15 +219,15 @@ edges_confluence l am tm = -- traceShow ("degs", degs) $ ...@@ -219,15 +219,15 @@ edges_confluence l am tm = -- traceShow ("degs", degs) $
SMatrix.toList matconf' SMatrix.toList matconf'
where where
vcount = natToInt @n vcount = natToInt @n
degs = sumWith' Colonne identity am degs = sumWith Colonne identity am
sumdeg = List.sum degs sumdeg = VS.sum degs
matconf' = SMatrix.imap matconf' = SMatrix.imap
(\x y _ -> (\x y _ ->
if x < y if x < y
then then
let let
deg_x = (List.!!) degs x - 1 deg_x = (VS.!) degs x - 1
deg_y = (List.!!) degs y - 1 deg_y = (VS.!) degs y - 1
tm' = SMatrix.imap (\i j v -> if (i == x && j == y) || ( i == y && j == x) tm' = SMatrix.imap (\i j v -> if (i == x && j == y) || ( i == y && j == x)
then 0 then 0
...@@ -404,7 +404,6 @@ symmetryS :: KnownNat n => MatrixS n -> MatrixS n ...@@ -404,7 +404,6 @@ symmetryS :: KnownNat n => MatrixS n -> MatrixS n
symmetryS m = SMatrix.imap (\x y v -> if x < y then v else coeffSM y x m) m symmetryS m = SMatrix.imap (\x y v -> if x < y then v else coeffSM y x m) m
--------------------------- ---------------------------
class KnownNat n => Vector n a where class KnownNat n => Vector n a where
fromList :: KnownNat n => Proxy n -> [(Integer,Double)] -> a fromList :: KnownNat n => Proxy n -> [(Integer,Double)] -> a
...@@ -425,7 +424,6 @@ vectorFromListD :: KnownNat n => Proxy n -> [(Integer, Double)] -> VectorD n ...@@ -425,7 +424,6 @@ vectorFromListD :: KnownNat n => Proxy n -> [(Integer, Double)] -> VectorD n
vectorFromListD pn ns = toMatrix $ vectorFromListS pn ns vectorFromListD pn ns = toMatrix $ vectorFromListS pn ns
------------------------------ ------------------------------
data Direction = Ligne | Colonne data Direction = Ligne | Colonne
sumWith :: ( Elem a sumWith :: ( Elem a
...@@ -440,17 +438,4 @@ sumWith dir f m = VS.fromList ...@@ -440,17 +438,4 @@ sumWith dir f m = VS.fromList
where where
somme m' = map (sum . SMatrix.toMatrix) m' somme m' = map (sum . SMatrix.toMatrix) m'
sumWith' :: ( Elem a
, Elem t
, KnownNat n
) => Direction -> (t -> a) -> SparseMatrix n n t -> [a]
sumWith' dir f m = map (\v -> f v)
$ case dir of
Colonne -> somme $ SMatrix.getCols m
Ligne -> somme $ SMatrix.getRows m
where
somme m' = map (sum . SMatrix.toMatrix) m'
-------------------------------------------- --------------------------------------------
{-# LANGUAGE NoImplicitPrelude #-}
module Main where
import Data.Set as Set
import Data.String.Conversions (cs)
import Data.Text (Text)
import qualified Eigen.Matrix as Matrix
import Graph.BAC.ProxemyOptim
import Graph.FGL
import Graph.Types
import Graph.Tools.Random
import Prelude (String)
import Protolude
import Graph.BAC.ProxemyOptim
import Test.Hspec
import qualified Data.IntMap as Dict
import qualified Eigen.SparseMatrix as SMatrix
import qualified Prelude as Prelude
import qualified Data.List as List
main :: IO ()
main = hspec $ do
describe "Graph Toy first test" $ do
let
edges_test :: [(Int,Int)]
edges_test=[(0,1),(0,2),(0,4),(0,5),(0,3),(0,6)
,(1,2),(1,3),(2,3),(4,5),(4,6),(5,6)
,(7,8),(7,3),(7,4),(8,2),(8,5)
]
clustering_result =
ClusteringIs { parts = Dict.fromList [(0, Set.fromList [0,4,5,6])
,(1, Set.fromList [1,2,3])
,(7, Set.fromList [7,8])]
, index = Dict.fromList [(0,0),(1,1),(2,1)
,(3,1),(4,0),(5,0)
,(6,0),(7,7),(8,7)
]
, score = 3.0558391780792453
, mode = Part
}
g :: Graph () ()
g = mkGraphUfromEdges edges_test
result = withG g (identity . (clusteringOptim 3 Conf))
it "Graph Toy test exact result" $ do
shouldBe True (result == clustering_result)
{-
m <- randomAdjacency
describe "Random Matrix of fixed size (TODO dynamic size)" $ do
let
g :: Graph () ()
g = mkGraphUfromEdges
$ List.map (\(x,y,_) -> (x,y))
$ SMatrix.toList m
result = withG g (parts . (clusteringOptim 3 Conf))
it "Random test, checking clustering size" $ do
shouldBe True (let n = (Dict.size result) in n > 1 && n < 10)
-}
{-# LANGUAGE NoImplicitPrelude #-}
module Spec where
import Data.Set as Set
import Data.String.Conversions (cs)
import Data.Text (Text)
import qualified Eigen.Matrix as Matrix
import Graph.BAC.ProxemyOptim
import Graph.FGL
import Graph.Types
import Graph.Tools.Random
import Prelude (String)
import Protolude
import Graph.BAC.ProxemyOptim
import qualified Data.IntMap as Dict
import qualified Eigen.SparseMatrix as SMatrix
import qualified Prelude as Prelude
import qualified Data.List as List
main :: IO ()
main = do
let
edges_test :: [(Int,Int)]
edges_test=[(0,1),(0,2),(0,4),(0,5),(0,3),(0,6)
,(1,2),(1,3),(2,3),(4,5),(4,6),(5,6)
,(7,8),(7,3),(7,4),(8,2),(8,5)
]
clustering_result =
ClusteringIs { parts = Dict.fromList [(0, Set.fromList [0,4,5,6])
,(1, Set.fromList [1,2,3])
,(7, Set.fromList [7,8])]
, index = Dict.fromList [(0,0),(1,1),(2,1)
,(3,1),(4,0),(5,0)
,(6,0),(7,7),(8,7)
]
, score = 3.0558391780792453
, mode = Part
}
g :: Graph () ()
g = mkGraphUfromEdges edges_test
result = withG g (identity . (clusteringOptim 3 Conf))
print $ result == clustering_result
test_random :: IO ()
test_random = do
m <- randomAdjacency
let
g :: Graph () ()
g = mkGraphUfromEdges
$ List.map (\(x,y,_) -> (x,y))
$ SMatrix.toList m
result = withG g (identity . (clusteringOptim 3 Conf))
print (result)
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