Commit 808c69a1 authored by Alexandre Delanoë's avatar Alexandre Delanoë

[GRAPH] Old graph format of gargantext utils to convert.

parent a10e3b30
......@@ -130,18 +130,18 @@ data2graph :: [(Label, Int)] -> Map (Int, Int) Int
data2graph labels coocs distance partitions = Graph nodes edges
where
community_id_by_node_id = M.fromList [ (n, c) | LouvainNode n c <- partitions ]
nodes = [ Node { n_size = maybe 0 identity (M.lookup (n,n) coocs)
, n_type = Terms -- or Unknown
, n_id = cs (show n)
, n_label = T.unwords l
, n_attributes =
nodes = [ Node { node_size = maybe 0 identity (M.lookup (n,n) coocs)
, node_type = Terms -- or Unknown
, node_id = cs (show n)
, node_label = T.unwords l
, node_attributes =
Attributes { clust_default = maybe 0 identity
(M.lookup n community_id_by_node_id) } }
| (l, n) <- labels ]
edges = [ Edge { e_source = s
, e_target = t
, e_weight = w
, e_id = i }
edges = [ Edge { edge_source = cs (show s)
, edge_target = cs (show t)
, edge_weight = w
, edge_id = cs (show i) }
| (i, ((s,t), w)) <- zip [0..] (M.toList distance) ]
-----------------------------------------------------------
......
......@@ -16,9 +16,17 @@ Portability : POSIX
module Gargantext.Viz.Graph
where
import GHC.IO (FilePath)
import GHC.Generics (Generic)
import Data.Aeson.TH (deriveJSON)
import qualified Data.Aeson as DA
import Data.ByteString.Lazy as DBL (readFile, writeFile)
import Data.Text (Text)
import qualified Text.Read as T
import qualified Data.Text as T
import Data.Map (Map)
import Gargantext.Prelude
......@@ -36,28 +44,78 @@ data Attributes = Attributes { clust_default :: Int }
deriving (Show, Generic)
$(deriveJSON (unPrefix "") ''Attributes)
data Node = Node { n_size :: Int
, n_type :: TypeNode
, n_id :: Text
, n_label :: Text
, n_attributes :: Attributes
data Node = Node { node_size :: Int
, node_type :: TypeNode
, node_id :: Text
, node_label :: Text
, node_attributes :: Attributes
}
deriving (Show, Generic)
$(deriveJSON (unPrefix "n_") ''Node)
$(deriveJSON (unPrefix "node_") ''Node)
data Edge = Edge { e_source :: Int
, e_target :: Int
, e_weight :: Double
, e_id :: Int
data Edge = Edge { edge_source :: Text
, edge_target :: Text
, edge_weight :: Double
, edge_id :: Text
}
deriving (Show, Generic)
$(deriveJSON (unPrefix "e_") ''Edge)
$(deriveJSON (unPrefix "edge_") ''Edge)
data Graph = Graph { g_nodes :: [Node]
, g_edges :: [Edge]
data Graph = Graph { graph_nodes :: [Node]
, graph_edges :: [Edge]
}
deriving (Show, Generic)
$(deriveJSON (unPrefix "g_") ''Graph)
$(deriveJSON (unPrefix "graph_") ''Graph)
-----------------------------------------------------------
-- Old Gargantext Version
data AttributesOld = AttributesOld { cl :: Int }
deriving (Show, Generic)
$(deriveJSON (unPrefix "") ''AttributesOld)
data NodeOld = NodeOld { no_id :: Int
, no_at :: AttributesOld
, no_s :: Int
, no_lb :: Text
}
deriving (Show, Generic)
$(deriveJSON (unPrefix "no_") ''NodeOld)
data EdgeOld = EdgeOld { eo_s :: Int
, eo_t :: Int
, eo_w :: Text
}
deriving (Show, Generic)
$(deriveJSON (unPrefix "eo_") ''EdgeOld)
data GraphOld = GraphOld {
go_links :: [EdgeOld]
, go_nodes :: [NodeOld]
}
deriving (Show, Generic)
$(deriveJSON (unPrefix "go_") ''GraphOld)
----------------------------------------------------------
graphOld2graph :: GraphOld -> Graph
graphOld2graph (GraphOld links nodes) = Graph (map nodeOld2node nodes) (zipWith linkOld2edge [1..] links)
where
nodeOld2node :: NodeOld -> Node
nodeOld2node (NodeOld no_id' (AttributesOld cl') no_s' no_lb')
= Node no_s' Terms (cs $ show no_id') no_lb' (Attributes cl')
linkOld2edge :: Int -> EdgeOld -> Edge
linkOld2edge n (EdgeOld eo_s' eo_t' eo_w') = Edge (cs $ show eo_s') (cs $ show eo_t') ((T.read $ T.unpack eo_w') :: Double) (cs $ show n)
graphOld2graphWithFiles :: FilePath -> FilePath -> IO ()
graphOld2graphWithFiles g1 g2 = do
graph <- DBL.readFile g1
let newGraph = case DA.decode graph :: Maybe GraphOld of
Nothing -> panic "no graph"
Just new -> new
DBL.writeFile g2 (DA.encode $ graphOld2graph newGraph)
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