Commit 759c6c7c authored by Kai Zhang's avatar Kai Zhang

change API for IGraph.Layout

parent c89f092d
module IGraph.Layout
( kamadaKawai
( getLayout
, LayoutOpt(..)
, LayoutMethod(..)
) where
import Foreign (nullPtr)
......@@ -15,21 +17,44 @@ import IGraph.Internal.Data
data LayoutOpt = LayoutOpt
{ _seed :: Maybe [(Double, Double)]
, _nIter :: Int
} deriving (Show)
, _method :: LayoutMethod
, _sigma :: (Int -> Double) -- ^ [KamadaKawai] the base standard deviation of position change proposals
, _startTemp :: Double -- ^ [KamadaKawai] the initial temperature for the annealing
, _coolFact :: Double -- ^ [KamadaKawai] the cooling factor for the simulated annealing
, _kkConst :: (Int -> Double) -- ^ [KamadaKawai] The Kamada-Kawai vertex attraction constant
}
instance Default LayoutOpt where
def = LayoutOpt
{ _seed = Nothing
, _nIter = 10000
, _method = KamadaKawai
, _sigma = \x -> fromIntegral x / 4
, _startTemp = 10
, _coolFact = 0.99
, _kkConst = \x -> fromIntegral $ x^2
}
kamadaKawai :: Graph d => LGraph d v e -> Double -> Double -> Double -> Double -> LayoutOpt -> [(Double, Double)]
kamadaKawai gr sigma initemp coolexp kkconst opt = unsafePerformIO $ do
data LayoutMethod = KamadaKawai
getLayout :: Graph d => LGraph d v e -> LayoutOpt -> [(Double, Double)]
getLayout gr opt = unsafePerformIO $ do
mptr <- mat
igraphLayoutKamadaKawai (_graph gr) mptr (_nIter opt) sigma initemp coolexp kkconst useSeed nullPtr nullPtr nullPtr nullPtr
case _method opt of
KamadaKawai -> igraphLayoutKamadaKawai gptr mptr iters s initemp coolexp
kkconst useSeed nullPtr nullPtr nullPtr nullPtr
[x, y] <- matrixPtrToColumnLists mptr
return $ zip x y
where
n = nNodes gr
gptr = _graph gr
iters = _nIter opt
s = _sigma opt n
initemp = _startTemp opt
coolexp = _coolFact opt
kkconst = _kkConst opt n
(useSeed, mat) = case _seed opt of
Just xs -> if length xs /= nNodes gr
then error "Seed error: incorrect size"
......
module IGraph.Read
( readAdjMatrix
, fromAdjMatrix
, readAdjMatrixWeighted
) where
......@@ -14,18 +15,21 @@ readDouble = fst . fromJust . readSigned readExponential
{-# INLINE readDouble #-}
readAdjMatrix :: Graph d => FilePath -> IO (LGraph d B.ByteString ())
readAdjMatrix fl = do
c <- B.readFile fl
let (header:xs) = B.lines c
readAdjMatrix = fmap fromAdjMatrix . B.readFile
fromAdjMatrix :: Graph d => B.ByteString -> LGraph d B.ByteString ()
fromAdjMatrix bs =
let (header:xs) = B.lines bs
mat = map (map readDouble . B.words) xs
es = fst $ unzip $ filter f $ zip [ (i,j) | i <- [0..nrow-1], j <- [0..nrow-1] ] $ concat mat
nrow = length mat
ncol = length $ head mat
if nrow /= ncol
then error "nrow != ncol"
else return $ mkGraph (nrow, Just $ B.words header) (es, Nothing)
in if nrow /= ncol
then error "fromAdjMatrix: nrow != ncol"
else mkGraph (nrow, Just $ B.words header) (es, Nothing)
where
f ((i,j),v) = i < j && v /= 0
{-# INLINE fromAdjMatrix #-}
readAdjMatrixWeighted :: Graph d => FilePath -> IO (LGraph d B.ByteString Double)
readAdjMatrixWeighted fl = do
......
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