Permalink
Browse files

Upgrade zoom-cache dependency from 0.8.* to 0.9.*.

This also affords us many simplifications in the code.
  • Loading branch information...
1 parent d22e282 commit 6ec04eb27fe367ae0aed4e55804dcedef30416b7 @alang9 committed Dec 13, 2011
Showing with 45 additions and 87 deletions.
  1. +37 −71 Data/ZoomCache/Gnuplot.hs
  2. +4 −14 tools/zoom-cache-gnuplot.hs
  3. +4 −2 zoom-cache-gnuplot.cabal
View
@@ -24,32 +24,22 @@ module Data.ZoomCache.Gnuplot
, linePlot
) where
-import Control.Arrow ((***))
-import Control.Monad
-import Data.Functor ((<$>))
import Data.Maybe
-import Data.ByteString (ByteString)
import Data.Monoid
-import Debug.Trace
import qualified Data.Iteratee as I
import Data.ZoomCache as Z
import Data.ZoomCache.Numeric.Types as Z
-import Graphics.Gnuplot.Advanced
import qualified Graphics.Gnuplot.Graph.TwoDimensional as Graph
import qualified Graphics.Gnuplot.Plot.TwoDimensional as Plot
import qualified Graphics.Gnuplot.Value.Atom as Atom
import qualified Graphics.Gnuplot.Value.Tuple as Tuple
-import Unsafe.Coerce
-
----------------------------------------------------------------------
-singleton :: a -> [a]
-singleton = (:[])
-
instance Tuple.C TimeStamp where
- text = singleton . shows . Z.unTS
+ text (TS x) = [shows x]
+
instance Atom.C TimeStamp where
candlePlotData :: [Summary Double]
@@ -74,24 +64,18 @@ linePlot = Plot.list Graph.lines
----------------------------------------------------------------------
instance Num Z.TimeStamp where
- a + b = Z.TS $ Z.unTS a + Z.unTS b
- a - b = Z.TS $ Z.unTS a - Z.unTS b
- a * b = Z.TS $ Z.unTS a * Z.unTS b
- negate a = Z.TS . negate $ Z.unTS a
- abs a = Z.TS . abs $ Z.unTS a
- signum a = Z.TS . signum $ Z.unTS a
- fromInteger i = Z.TS $ fromInteger i
-
-instance Real Z.TimeStamp where
- toRational a = toRational $ Z.unTS a
-
-instance Enum Z.TimeStamp where
- toEnum i = Z.TS $ toEnum i
- fromEnum a = fromEnum $ Z.unTS a
-
-instance Integral Z.TimeStamp where
- quotRem a b = Z.TS *** Z.TS $ Z.unTS a `quotRem` Z.unTS b
- toInteger a = toInteger $ Z.unTS a
+ (TS a) + (TS b) = TS $ a + b
+ (TS a) - (TS b) = TS $ a - b
+ (TS a) * (TS b) = TS $ a * b
+ negate (TS a) = TS $ negate a
+ abs (TS a) = TS $ abs a
+ signum (TS a) = TS $ signum a
+ fromInteger i = TS $ fromInteger i
+
+instance Fractional Z.TimeStamp where
+ fromRational r = TS $ fromRational r
+ recip (TS a) = TS $ recip a
+ (TS a) / (TS b) = TS $ a / b
----------------------------------------------------------------------
@@ -108,7 +92,7 @@ push a (AvgQueue m [] ys l)
= push a (AvgQueue m (reverse ys) [] l)
queueAvg :: AvgQueue -> Double
-queueAvg (AvgQueue m xs ys l) = realToFrac (sum (map snd xs)
+queueAvg (AvgQueue _ xs ys l) = realToFrac (sum (map snd xs)
+ sum (map snd ys))
/ realToFrac l
@@ -119,15 +103,15 @@ totalTime :: [Summary Double] -> Maybe Z.TimeStamp
totalTime [] = Nothing
totalTime l = Just $ globalClose - globalOpen
where
- globalClose = Z.summaryExitTime $ last l
- globalOpen = Z.summaryEntryTime $ head l
+ globalClose = Z.summaryExit $ last l
+ globalOpen = Z.summaryEntry $ head l
-mavgPlot :: forall a. [Summary Double] -> Plot.T Z.TimeStamp Double
+mavgPlot :: [Summary Double] -> Plot.T Z.TimeStamp Double
mavgPlot zsums = Plot.list Graph.lines . snd $
foldl mavg (avgEmptyQueue window, []) zsums
where
window = (fromMaybe (error "Trying to draw an empty plot") $
- totalTime zsums) `div` Z.TS 10
+ totalTime zsums) / (TS 10)
mavg :: (AvgQueue, [(Z.TimeStamp, Double)]) -> Summary Double
-> (AvgQueue, [(Z.TimeStamp, Double)])
mavg (queue, l) dsum =
@@ -145,7 +129,7 @@ data BoundedQueue a = BoundedQueue Z.TimeStamp
bqPush :: ((Z.TimeStamp, Z.TimeStamp), a) -> BoundedQueue a -> BoundedQueue a
bqPush a (BoundedQueue m [] [])
= BoundedQueue m [a] []
-bqPush a@((o, c), v) (BoundedQueue m xs'@(((oldO, oldC), oldV):xs) ys)
+bqPush a@((_, c), _) (BoundedQueue m xs'@(((oldO, oldC), oldV):xs) ys)
| c - m > oldC = bqPush a $ BoundedQueue m xs ys
| (oldC >= c-m) && (c-m > oldO) = BoundedQueue m
(((c - m, oldC), oldV):xs) (a:ys)
@@ -157,28 +141,28 @@ queueAvgVar :: BoundedQueue (Double, Double) -> (Double, Double)
queueAvgVar (BoundedQueue _ xs ys) =
divWeight $ foldl avgFolder (Z.TS 0, 0, 0) (xs ++ ys)
where
- divWeight (w, accM, accV) = (accM/(realToFrac w), accV/(realToFrac w))
+ divWeight (TS w, accM, accV) = (accM / w, accV / w)
avgFolder :: (Z.TimeStamp, Double, Double)
-> ((Z.TimeStamp, Z.TimeStamp), (Double, Double))
-> (Z.TimeStamp, Double, Double)
- avgFolder (weight, accMean, accVar) ((o, c), (m, s2)) =
- ( (weight + c - o)
- , (accMean + (m * realToFrac (c - o)))
- , (accVar + (s2 * realToFrac (c - o)))
+ avgFolder (TS weight, accMean, accVar) ((TS o, TS c), (m, s2)) =
+ ( TS $ weight + c - o
+ , accMean + (m * (c - o))
+ , accVar + (s2 * (c - o))
)
emptyAvgVarQueue :: Z.TimeStamp -> BoundedQueue (Double, Double)
emptyAvgVarQueue m = BoundedQueue m [] []
-bollingerPlot :: forall a. [Summary Double] -> Plot.T Z.TimeStamp Double
+bollingerPlot :: [Summary Double] -> Plot.T Z.TimeStamp Double
bollingerPlot dsums = mavg `mappend` upperBB `mappend` lowerBB
where
window = (fromMaybe (error "Trying to draw an empty plot") $
- totalTime dsums) `div` Z.TS 10
+ totalTime dsums) / (TS 10)
avgsVars = map getSummaryAvgVar dsums
movingAvgsVars :: [(Z.TimeStamp, (Double, Double))]
movingAvgsVars = snd $ foldl folder (emptyAvgVarQueue window, []) avgsVars
- mavg = Plot.list Graph.lines $ map (\(t, (m, s)) -> (t, m)) movingAvgsVars
+ mavg = Plot.list Graph.lines $ map (\(t, (m, _)) -> (t, m)) movingAvgsVars
upperBB = Plot.list Graph.lines $
map (\(t, (m, s)) -> (t, m + (2 * sqrt s))) movingAvgsVars
lowerBB = Plot.list Graph.lines $
@@ -192,37 +176,19 @@ bollingerPlot dsums = mavg `mappend` upperBB `mappend` lowerBB
(newQueue, (timeStamp, queueAvgVar newQueue):l)
where
newQueue = bqPush ((o, c), (avg, var)) queue
- timeStamp = (c + o) `div` 2
+ timeStamp = (c + o) / TS 2
----------------------------------------------------------------------
--- plotSummaries :: Atom.C a => Int -> [Z.Stream a] -> [Attribute] -> IO ()
--- plotSummaries lvl streams attrs = plotListStyle attrs
--- (defaultStyle{plotType = CandleSticks})
--- candles
--- where
--- candles = map getSummaryCandleVals $
--- mapMaybe (maybeSummaryLevel lvl) streams
-
getStreams :: FilePath -> Z.TrackNo -> IO [Z.Stream]
getStreams fp tn =
flip I.fileDriverRandom fp $
(I.joinI $ (enumCacheFile standardIdentifiers) I.stream2stream)
--- As things stand, we are doing too much processing after running the
--- iteratee. Most of it can be moved before.
-
-maybeSummaryLevel :: Int -> Z.Stream -> Maybe ZoomSummary
-maybeSummaryLevel _ (StreamPacket _ _ _) = Nothing
-maybeSummaryLevel lvl (StreamSummary file tn zs@(ZoomSummary s)) =
- case Z.summaryLevel s == lvl of
- True -> Just zs
- False -> Nothing
-
getSummaryCandleVals :: Summary Double
-> (Z.TimeStamp, (Double, Double, Double, Double))
getSummaryCandleVals dsum =
- ( (openT + closeT) `div` 2
+ ( (openT + closeT) / TS 2
, ( Z.numEntry sData
, Z.numMin sData
, Z.numMax sData
@@ -231,17 +197,17 @@ getSummaryCandleVals dsum =
)
where
sData = summaryData dsum
- openT = Z.summaryEntryTime dsum
- closeT = Z.summaryExitTime dsum
+ openT = Z.summaryEntry dsum
+ closeT = Z.summaryExit dsum
getSummaryAvg :: Summary Double -> (Z.TimeStamp, Double)
getSummaryAvg dsum =
- ( (openT + closeT) `div` 2
+ ( (openT + closeT) / TS 2
, Z.numAvg $ Z.summaryData dsum
)
where
- openT = summaryEntryTime dsum
- closeT = summaryExitTime dsum
+ openT = summaryEntry dsum
+ closeT = summaryExit dsum
getSummaryAvgVar :: Summary Double
-> (Z.TimeStamp, Z.TimeStamp, Double, Double)
@@ -255,8 +221,8 @@ getSummaryAvgVar dsum =
sData = summaryData dsum
avg = Z.numAvg sData
rms = Z.numRMS sData
- openTime = Z.summaryEntryTime dsum
- closeTime = Z.summaryExitTime dsum
+ openTime = Z.summaryEntry dsum
+ closeTime = Z.summaryExit dsum
var = (rms * rms) - (avg * avg)
-- data ZoomNumDict a where
@@ -8,7 +8,7 @@ import Control.Applicative
import Control.Exception
import Data.ByteString (ByteString)
import Data.Char (toLower)
-import Data.Maybe (fromMaybe, catMaybes, mapMaybe)
+import Data.Maybe (fromMaybe)
import Data.Monoid
import Data.Typeable
import System.Console.GetOpt
@@ -17,7 +17,6 @@ import System.Exit (exitWith)
import System.FilePath.Posix
import qualified Data.Iteratee as I
import Data.Iteratee (Iteratee(..), stream2stream, fileDriverRandom, (><>))
-import Data.Iteratee.ZoomCache (Stream)
import Data.ZoomCache.Numeric
import Data.ZoomCache.Gnuplot
@@ -27,7 +26,6 @@ import qualified Graphics.Gnuplot.Terminal.PNG as PNG
import qualified Graphics.Gnuplot.Terminal.PostScript as PostScript
import qualified Graphics.Gnuplot.Terminal.SVG as SVG
import qualified Graphics.Gnuplot.Terminal.X11 as X11
-import Graphics.Gnuplot.Value.Tuple (C(..))
import qualified Graphics.Gnuplot.Plot.TwoDimensional as Plot
data ParseError = ParseError
@@ -42,9 +40,9 @@ parseTrack arg =
words :: String -> [String]
words s = case dropWhile (==':') s of
"" -> []
- s' -> w : words s''
+ s' -> w' : words s''
where
- (w, s'') = break (==':') s'
+ (w', s'') = break (==':') s'
parseTrack2 :: String -> Either ParseError (FilePath, TrackNo)
parseTrack2 arg =
@@ -71,6 +69,7 @@ data Options = Options
, ls :: [(FilePath, TrackNo)]
}
+defaultOptions :: Options
defaultOptions = Options
{ gnuplotOpts = []
, candleSticks = []
@@ -85,8 +84,6 @@ parseCustom s =
Custom s1 [tail s2]
where (s1, s2) = break (==':') s
-
-
-- Code needs to be abstracted and factored here
options :: [OptDescr (Options -> Options)]
options =
@@ -180,13 +177,6 @@ lineProcess (fp, tn) = fileDriverRandom iter fp
(I.joinI . filterTracks [tn] .
I.joinI . enumDouble $ (linePlot <$> stream2stream))
-isSumLvl :: TrackNo -> Int -> Stream -> Maybe ZoomSummary
-isSumLvl tn lvl str =
- case str of
- StreamPacket{} -> Nothing
- StreamSummary _ tn' zsum@(ZoomSummary sum) ->
- if tn == tn' && summaryLevel sum == lvl then Just zsum else Nothing
-
data UnrecognisedFormatException = UnrecognisedFormatException
deriving (Show, Typeable)
View
@@ -47,19 +47,21 @@ Cabal-version: >=1.2
Library
Exposed-modules: Data.ZoomCache.Gnuplot
+ GHC-Options: -Wall
Build-Depends: base,
bytestring,
- zoom-cache >= 0.8,
+ zoom-cache >= 0.9, zoom-cache < 1.0,
gnuplot,
iteratee
Executable zoom-cache-gnuplot
-- .hs or .lhs file containing the Main module.
Main-is: zoom-cache-gnuplot.hs
+ GHC-Options: -Wall
Hs-Source-Dirs: ., tools
Build-Depends: base,
filepath,
- zoom-cache >= 0.8,
+ zoom-cache >= 0.9, zoom-cache < 1.0,
gnuplot,
iteratee

0 comments on commit 6ec04eb

Please sign in to comment.