rp-tree 0.3.2 → 0.3.3
raw patch · 5 files changed
+40/−65 lines, 5 filesdep −mnist-idx-conduit
Dependencies removed: mnist-idx-conduit
Files
- app/Main.hs +3/−3
- bench/time/Main.hs +13/−36
- rp-tree.cabal +3/−3
- src/Data/RPTree.hs +2/−2
- src/Data/RPTree/Internal.hs +19/−21
app/Main.hs view
@@ -16,8 +16,8 @@ import qualified Data.IntMap as IM (IntMap, fromList, insert, lookup, map, mapWithKey, traverseWithKey, foldlWithKey, foldrWithKey) -- exceptions import Control.Monad.Catch (MonadThrow(..))--- mnist-idx-conduit-import Data.IDX.Conduit (sourceIdxSparse, sBufSize, sNzComponents)+-- -- mnist-idx-conduit+-- import Data.IDX.Conduit (sourceIdxSparse, sBufSize, sNzComponents) -- splitmix-distributions import System.Random.SplitMix.Distributions (Gen, GenT, sample, sampleT, bernoulli, normal) -- transformers@@ -27,7 +27,7 @@ import qualified Data.Vector as V (Vector, toList, fromList, replicate, zip) import Control.Monad (replicateM)-import Data.RPTree (knn, candidates, Embed(..), Inner(..), RPTree, RPForest, leaves, SVector, fromListSv, DVector, fromListDv, dense, writeCsv, forest, dataSource, sparse, normal2, normalSparse2, datS, datD)+import Data.RPTree (knn, candidates, Embed(..), Inner(..), RPTree, RPForest, SVector, fromListSv, DVector, fromListDv, dense, writeCsv, forest, dataSource, sparse, normal2, normalSparse2, datS, datD) -- import Data.RPTree.Internal.Testing (datS, datD) main :: IO ()
bench/time/Main.hs view
@@ -19,8 +19,8 @@ import Control.DeepSeq (NFData(..), force) -- exceptions import Control.Monad.Catch (MonadThrow(..))--- mnist-idx-conduit-import Data.IDX.Conduit (sourceIdxSparse, sBufSize, sNzComponents)+-- -- mnist-idx-conduit+-- import Data.IDX.Conduit (sourceIdxSparse, sBufSize, sNzComponents) -- splitmix-distributions import System.Random.SplitMix.Distributions (GenT, sampleT, sample, samples) @@ -31,8 +31,7 @@ import qualified Data.Vector as V (Vector, replicateM, fromList) import qualified Data.Vector.Unboxed as VU (Unbox, Vector, map) -import Data.RPTree (tree, forest, recallWith, knn, fromVectorSv, fromListSv, RPForest, RPTree, SVector, Inner(..), normalSparse2, liftC, Embed(..))-import Data.RPTree.Internal.Testing (BenchConfig(..), randSeed, datD, datS)+import Data.RPTree (forest, recallWith, knn, fromVectorSv, fromListSv, RPForest, RPTree, SVector, Inner(..), normalSparse2, liftC, Embed(..), BenchConfig(..), randSeed, datD, datS) main :: IO () main = do -- putStrLn "hello!"@@ -113,16 +112,16 @@ -- let q = sample 1234 $ normalSparse2 nzData d -- pure $! recallWith metricL2 tt k q -mnist :: MonadResource m =>- FilePath -- path to uncompressed MNIST IDX data file- -> Int -- number of data items- -> C.ConduitT a (Embed SVector Double ()) m ()-mnist fp n = C.takeExactly n src- where- src = sourceIdxSparse fp .|- C.map (\r -> fromVectorSv (sBufSize r) (VU.map f $ sNzComponents r)) .|- C.map (\r -> Embed r ())- f (i, x) = (i, toUnitRange x)+-- mnist :: MonadResource m =>+-- FilePath -- path to uncompressed MNIST IDX data file+-- -> Int -- number of data items+-- -> C.ConduitT a (Embed SVector Double ()) m ()+-- mnist fp n = C.takeExactly n src+-- where+-- src = sourceIdxSparse fp .|+-- C.map (\r -> fromVectorSv (sBufSize r) (VU.map f $ sNzComponents r)) .|+-- C.map (\r -> Embed r ())+-- f (i, x) = (i, toUnitRange x) toUnitRange :: Word8 -> Double toUnitRange w8 = fromIntegral w8 / 255@@ -161,28 +160,6 @@ x <- sampleT seed $ growForest s cfg src pure $ force x ----- treeBench :: (Monad m, Inner SVector v) =>--- C.ConduitT () (v Double) m ()--- -> (m (RPTree Double (V.Vector (v Double))) -> IO c)--- -> Int--- -> BenchConfig--- -> IO (c, Double)-treeBench src go n cfg = benchmark n setup (const $ pure ()) go- where- setup = do- s <- randSeed- -- let src' = C.transPipe lift src- pure $ growTree s cfg src---- growTree :: (Monad m, Inner SVector v) =>--- Word64--- -> BenchConfig--- -> C.ConduitT () (v Double) m ()--- -> m (RPTree Double (V.Vector (v Double)))-growTree seed (BenchConfig _ maxd minl _ chunksize pnz pdim _ _) =- tree seed maxd minl chunksize pnz pdim -- growForest :: (Monad m, Inner SVector v) => -- Word64
rp-tree.cabal view
@@ -1,5 +1,5 @@ name: rp-tree-version: 0.3.2+version: 0.3.3 synopsis: Random projection trees description: Random projection trees for approximate nearest neighbor search in high-dimensional vector spaces .@@ -77,7 +77,7 @@ , conduit , deepseq >= 1.4.4.0 , exceptions- , mnist-idx-conduit+ -- , mnist-idx-conduit , mtl , rp-tree , splitmix >= 0.1.0.3@@ -94,7 +94,7 @@ , conduit , containers , exceptions- , mnist-idx-conduit+ -- , mnist-idx-conduit , rp-tree , splitmix , splitmix-distributions
src/Data/RPTree.hs view
@@ -59,7 +59,7 @@ -- * Validation , recallWith -- * Access- , levels, points, leaves, candidates+ , levels, points, candidates -- * Types , Embed(..) -- ** RPTree@@ -128,7 +128,7 @@ import Data.RPTree.Conduit (tree, forest, dataSource, liftC) import Data.RPTree.Gen (sparse, dense, normal2, normalSparse2)-import Data.RPTree.Internal (RPTree(..), RPForest, RPT(..), Embed(..), levels, points, leaves, RT(..), Inner(..), Scale(..), scaleS, scaleD, (/.), innerDD, innerSD, innerSS, metricSSL2, metricSDL2, SVector(..), fromListSv, fromVectorSv, DVector(..), fromListDv, fromVectorDv, partitionAtMedian, Margin, getMargin, sortByVG, serialiseRPForest, deserialiseRPForest)+import Data.RPTree.Internal (RPTree(..), RPForest, RPT(..), Embed(..), levels, points, Inner(..), Scale(..), scaleS, scaleD, (/.), innerDD, innerSD, innerSS, metricSSL2, metricSDL2, SVector(..), fromListSv, fromVectorSv, DVector(..), fromListDv, fromVectorDv, partitionAtMedian, Margin, getMargin, sortByVG, serialiseRPForest, deserialiseRPForest) import Data.RPTree.Internal.Testing (BenchConfig(..), randSeed, datS, datD) import Data.RPTree.Draw (draw, writeCsv)
src/Data/RPTree/Internal.hs view
@@ -9,7 +9,7 @@ {-# language LambdaCase #-} {-# language MultiParamTypeClasses #-} {-# language GeneralizedNewtypeDeriving #-}-{-# language TemplateHaskell #-}+-- {-# language TemplateHaskell #-} {-# LANGUAGE BangPatterns #-} {-# options_ghc -Wno-unused-imports #-} module Data.RPTree.Internal where@@ -33,9 +33,9 @@ import qualified Data.IntMap.Strict as IM (IntMap, fromList) -- deepseq import Control.DeepSeq (NFData(..))--- microlens-import Lens.Micro (Traversal', (.~), (^..), folded)-import Lens.Micro.TH (makeLensesFor, makeLensesWith, lensRules, generateSignatures)+-- -- microlens+-- import Lens.Micro (Traversal', (.~), (^..), folded)+-- import Lens.Micro.TH (makeLensesFor, makeLensesWith, lensRules, generateSignatures) -- mtl import Control.Monad.State (MonadState(..), modify) -- serialise@@ -91,8 +91,16 @@ show (SV n vv) = unwords ["SV", show n, show (VU.toList vv)] instance NFData (SVector a) +-- | (Unsafe) Pack a 'SVector' from its vector dimension and components+--+-- Note : the relevant invariants are not checked :+--+-- * vector components are _assumed_ to be in increasing order+--+-- * vector dimension is larger than any component index fromListSv :: VU.Unbox a => Int -> [(Int, a)] -> SVector a fromListSv n ll = SV n $ VU.fromList ll+ -- | (Unsafe) Pack a 'SVector' from its vector dimension and components -- -- Note : the relevant invariants are not checked :@@ -119,17 +127,7 @@ toListDv :: (VU.Unbox a) => DVector a -> [a] toListDv (DV v) = VU.toList v --- | Internal------ one projection vector per node (like @annoy@)-data RT v d a =- RBin !d !(v d) !(RT v d a) !(RT v d a)- | RTip { _rData :: !a } deriving (Eq, Show, Generic, Functor, Foldable, Traversable)-makeLensesFor [("_rData", "rData")] ''RT-instance (NFData (v d), NFData d, NFData a) => NFData (RT v d a) -- -- | Internal -- -- one projection vector per tree level (as suggested in https://www.cs.helsinki.fi/u/ttonteri/pub/bigdata2016.pdf )@@ -142,7 +140,7 @@ | Tip { _rpData :: !a } deriving (Eq, Show, Generic, Functor, Foldable, Traversable) instance (Serialise a, Serialise d) => Serialise (RPT d a)-makeLensesFor [("_rpData", "rpData")] ''RPT+-- makeLensesFor [("_rpData", "rpData")] ''RPT instance (NFData v, NFData a) => NFData (RPT v a) -- | Random projection trees@@ -157,7 +155,7 @@ , _rpTree :: !(RPT d a) } deriving (Eq, Show, Functor, Foldable, Traversable, Generic) instance (Serialise d, Serialise a, VU.Unbox d) => Serialise (RPTree d a)-makeLensesFor [("_rpTree", "rpTree")] ''RPTree+-- makeLensesFor [("_rpTree", "rpTree")] ''RPTree instance (NFData a, NFData d) => NFData (RPTree d a) -- | A random projection forest is an ordered set of 'RPTree's@@ -179,12 +177,12 @@ Left e -> Left (show e) Right xs -> Right $ IM.fromList $ zip [0 ..] xs -rpTreeData :: Traversal' (RPTree d a) a-rpTreeData = rpTree . rpData+-- rpTreeData :: Traversal' (RPTree d a) a+-- rpTreeData = rpTree . rpData --- | All data buckets stored at the leaves of the tree-leaves :: RPTree d a -> [a]-leaves = (^.. rpTreeData)+-- -- | All data buckets stored at the leaves of the tree+-- leaves :: RPTree d a -> [a]+-- leaves = (^.. rpTreeData) -- | Number of tree levels levels :: RPTree d a -> Int