hmatrix-csv (empty) → 0.1.0.0
raw patch · 4 files changed
+134/−0 lines, 4 filesdep +basedep +bytestringdep +cassavasetup-changed
Dependencies added: base, bytestring, cassava, hmatrix, vector
Files
- Data/Csv/HMatrix.hs +69/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- hmatrix-csv.cabal +33/−0
+ Data/Csv/HMatrix.hs view
@@ -0,0 +1,69 @@+module Data.Csv.HMatrix+ (+ -- * Usage example+ -- $example++ -- * Information+ -- $info++ decodeMatrix+ , decodeMatrixWith+ , encodeMatrix+ , encodeMatrixWith+ ) where++import Data.Csv+import Numeric.LinearAlgebra.HMatrix+import qualified Data.Vector as V+import Data.ByteString.Lazy (ByteString)+import qualified Data.ByteString.Char8 as C+import Data.Char (ord)++-- $example+--+-- For decoding we have to specify, if there is a header. This can be done with the HasHeader/NoHeader type:+--+-- > >>> decodeMatrix NoHeader "1.0,2.0,3.0\r\n4.0,5.0,6.0\r\n7.0,8.0,9.0\r\n"+-- > (3><3)+-- > [ 1.0, 2.0, 3.0+-- > , 4.0, 5.0, 6.0+-- > , 7.0, 8.0, 9.0 ]+--+-- Cassava, which is used for parsing the .csv files uses overloaded string literals. If you try this in ghci, make sure to start it with the right flag:+--+-- > >>> ghci -XOverloadedStrings+--+-- Encoding a file works pretty much the same, except that we do not need to specify a header.+--+-- > >>> encodeMatrix $ matrix 3 [1,2,3,4,5,6,7,8,9]+-- > "1.0,2.0,3.0\r\n4.0,5.0,6.0\r\n7.0,8.0,9.0\r\n"++-- $info+-- I'm quite new to haskell programming. If you have any advice or want to help improve this library, feel free to file an issue or send a pull-request on github. Every feedback is appreciated.+-- As of now only matrices of type Double are supported.++-- | Decodes a matrix.+decodeMatrix :: HasHeader -> ByteString -> Matrix Double+decodeMatrix header s = decodeMatrixWith header ',' s++-- | Decodes a matrix from ByteString and additionally allow+-- to specify the delimter which was used.+decodeMatrixWith :: HasHeader -> Char -> ByteString -> Matrix Double+decodeMatrixWith header del s =+ case decodeWith opt header s of+ Left err -> error err+ Right v -> fromLists . V.toList . V.map V.toList $ v+ where opt = defaultDecodeOptions { decDelimiter = fromIntegral (ord del) }++rowToRecord :: [Double] -> Record+rowToRecord x = record $ map (C.pack . show) x++-- | Encodes a matrix with comma as delimiter.+encodeMatrix :: Matrix Double -> ByteString+encodeMatrix m = encodeMatrixWith ',' m++-- | Encodes a matrix but allows to specify a delimiter.+encodeMatrixWith :: Char -> Matrix Double -> ByteString+encodeMatrixWith del m = encodeWith opt s+ where opt = defaultEncodeOptions { encDelimiter = fromIntegral (ord del) }+ s = map rowToRecord $ toLists m
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2014, Jochen Görtler++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Jochen Görtler nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ hmatrix-csv.cabal view
@@ -0,0 +1,33 @@+name: hmatrix-csv+version: 0.1.0.0+synopsis: CSV encoding and decoding for hmatrix.+description:+ Aims to provide easy CSV encoding and decoding of matrices.+ Right now only matrices of type 'Double' are supported.+ Please feel free to report issues or possible improvements.+Homepage: https://github.com/grtlr/hmatrix-csv+license: BSD3+license-file: LICENSE+bug-reports: https://github.com/grtlr/hmatrix-csv/issues+author: Jochen Goertler+maintainer: lyzrd17@gmail.com+copyright: (c) 2014 Jochen Goertler+category: Data+build-type: Simple+-- extra-source-files: +cabal-version: >=1.10++library+ exposed-modules: Data.Csv.HMatrix+ build-depends:+ base >=4.7 && <4.8,+ cassava >=0.4 && <0.5,+ hmatrix >=0.16 && <0.17,+ vector >=0.10 && <0.11,+ bytestring >=0.10 && <0.11+ -- hs-source-dirs: + default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/grtlr/hmatrix-csv.git