Utils.purs 2.77 KB
Newer Older
arturo's avatar
arturo committed
1 2 3
module Gargantext.Components.GraphExplorer.Utils
  ( stEdgeToGET, stNodeToGET
  , normalizeNodes
4
  , normalizeNodeSize
arturo's avatar
arturo committed
5 6
  , takeGreatestNodeByCluster, countNodeByCluster
  ) where
7 8 9

import Gargantext.Prelude

arturo's avatar
arturo committed
10
import Data.Array as A
11
import Data.Foldable (maximum, minimum)
12 13
import Data.Lens (Lens', lens, over, traversed)
import Data.Int (floor, toNumber)
arturo's avatar
arturo committed
14 15
import Data.Maybe (Maybe(..))
import Data.Newtype (wrap)
16
import Data.Sequence as Seq
17
import Gargantext.Components.GraphExplorer.GraphTypes as GEGT
18 19
import Gargantext.Components.GraphExplorer.Types as GET
import Gargantext.Hooks.Sigmax.Types as ST
arturo's avatar
arturo committed
20
import Gargantext.Utils (getter)
21
import Gargantext.Utils.Lens as GUL
22
import Gargantext.Utils.Seq as GUS
23

24
stEdgeToGET :: Record ST.Edge -> GEGT.Edge
25 26
stEdgeToGET { _original } = _original

27 28
stNodeToGET :: Record ST.Node -> GEGT.Node
stNodeToGET { id, label, x, y, _original: GEGT.Node { attributes, size, type_ } } = GEGT.Node {
29
    attributes
30
  , children: []
31 32 33 34 35 36 37
  , id_: id
  , label
  , size
  , type_
  , x
  , y
  }
38

arturo's avatar
arturo committed
39 40
-----------------------------------------------------------------------

41 42
-- | Normalize nodes, i.e. set their {x, y} values so that they are in
-- | range [0, 1].
43
normalizeNodes :: Seq.Seq GEGT.Node -> Seq.Seq GEGT.Node
44
normalizeNodes ns = GUL.normalizeLens xLens $ GUL.normalizeLens yLens ns
45
  where
46
    xLens :: Lens' GEGT.Node Number
47
    xLens = lens (\(GEGT.Node { x }) -> x) $ (\(GEGT.Node n) val -> GEGT.Node (n { x = val }))
48
    yLens :: Lens' GEGT.Node Number
49
    yLens = lens (\(GEGT.Node { y }) -> y) $ (\(GEGT.Node n) val -> GEGT.Node (n { y = val }))
50 51 52 53 54 55 56 57

normalizeNodeSize :: Int -> Int -> Seq.Seq GEGT.Node -> Seq.Seq GEGT.Node
normalizeNodeSize minSize maxSize ns = over traversed (over sizeLens (\s -> toNumber minSize + s * quotient)) $ GUL.normalizeLens sizeLens ns
  where
    quotient :: Number
    quotient = toNumber $ maxSize - minSize
    sizeLens :: Lens' GEGT.Node Number
    sizeLens = lens (\(GEGT.Node { size }) -> toNumber size) $ (\(GEGT.Node n) val -> GEGT.Node (n { size = floor val }))
arturo's avatar
arturo committed
58 59 60

------------------------------------------------------------------------

61
takeGreatestNodeByCluster :: GET.HyperdataGraph -> Int -> Int -> Array GEGT.Node
arturo's avatar
arturo committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
takeGreatestNodeByCluster graphData take clusterId
  =   graphData
  #   getter _.graph
  >>> getter _.nodes
  >>> A.filter
      (   getter _.attributes
      >>> getter _.clustDefault
      >>> eq clusterId
      )
  >>> A.sortWith
      ( getter _.size
      )
  >>> A.takeEnd take
  >>> A.reverse

77
countNodeByCluster :: GET.HyperdataGraph -> Int -> GEGT.ClusterCount
arturo's avatar
arturo committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91
countNodeByCluster graphData clusterId
  =   graphData
  #   getter _.graph
  >>> getter _.nodes
  >>> A.filter
      (   getter _.attributes
      >>> getter _.clustDefault
      >>> eq clusterId
      )
  >>> A.length
  >>> { id:  clusterId
      , count: _
      }
  >>> wrap