add --precision option

This commit is contained in:
Filip Gralinski 2015-09-12 15:36:50 +02:00 committed by Filip Gralinski
parent e96f16b829
commit 085ef0ec9e
4 changed files with 37 additions and 9 deletions

View File

@ -6,11 +6,20 @@ import GEval.OptionsParser
import System.Environment
import Options.Applicative
import Text.Printf
main :: IO ()
main = do
args <- getArgs
result <- runGEval args
result <- runGEvalGetOptions args
case result of
Left parseResult -> handleParseResult parseResult >> return ()
Right (Just result) -> print $ result
Right Nothing -> return ()
Right (opts, Just result) -> showTheResult opts result
Right (_, Nothing) -> return ()
showTheResult :: GEvalOptions -> MetricValue -> IO ()
showTheResult opts val = putStrLn $ formatTheResult (geoPrecision opts) val
formatTheResult :: Maybe Int -> MetricValue -> String
formatTheResult Nothing = show
formatTheResult (Just prec) = printf "%0.*f" prec

View File

@ -1,5 +1,5 @@
name: geval
version: 0.1.0.0
version: 0.1.1.0
synopsis: Machine learning evaluation tools
description: Please see README.md
homepage: http://github.com/name/project

View File

@ -66,6 +66,7 @@ getExpectedDirectory spec = fromMaybe outDirectory $ gesExpectedDirectory spec
data GEvalOptions = GEvalOptions
{ geoInit :: Bool,
geoPrecision :: Maybe Int,
geoSpec :: GEvalSpecification }

View File

@ -2,7 +2,8 @@
module GEval.OptionsParser
(fullOptionsParser,
runGEval) where
runGEval,
runGEvalGetOptions) where
import Options.Applicative
import qualified System.Directory as D
@ -25,8 +26,15 @@ optionsParser = GEvalOptions
<$> switch
( long "init"
<> help "Init a sample Gonito challenge rather than run an evaluation" )
<*> optional precisionArgParser
<*> specParser
precisionArgParser :: Parser Int
precisionArgParser = option auto
( long "precision"
<> metavar "PRECISION"
<> help "Precision with which the evaluation results should be shown" )
specParser :: Parser GEvalSpecification
specParser = GEvalSpecification
<$> strOption
@ -68,18 +76,28 @@ metricReader = option auto
<> help "Metric to be used - RMSE, MSE or BLEU" )
runGEval :: [String] -> IO (Either (ParserResult GEvalOptions) (Maybe MetricValue))
runGEval = runGEval' True
runGEval args = do
ret <- runGEvalGetOptions args
case ret of
Left e -> return $ Left e
Right (_, mmv) -> return $ Right mmv
runGEval' :: Bool -> [String] -> IO (Either (ParserResult GEvalOptions) (Maybe MetricValue))
runGEvalGetOptions :: [String] -> IO (Either (ParserResult GEvalOptions) (GEvalOptions, Maybe MetricValue))
runGEvalGetOptions = runGEval' True
-- the first argument: whether to try to read from the config file
runGEval' :: Bool -> [String] -> IO (Either (ParserResult GEvalOptions) (GEvalOptions, Maybe MetricValue))
runGEval' readOptsFromConfigFile args =
case parserResult of
Success opts -> if readOptsFromConfigFile then
attemptToReadOptsFromConfigFile args opts else
Right <$> runGEval'' opts
do
mmv <- runGEval'' opts
return $ Right $ (opts, mmv)
otherwise -> return $ Left parserResult
where parserResult = execParserPure (prefs idm) fullOptionsParser args
attemptToReadOptsFromConfigFile :: [String] -> GEvalOptions -> IO (Either (ParserResult GEvalOptions) (Maybe MetricValue))
attemptToReadOptsFromConfigFile :: [String] -> GEvalOptions -> IO (Either (ParserResult GEvalOptions) (GEvalOptions, Maybe MetricValue))
attemptToReadOptsFromConfigFile args opts = do
configExists <- D.doesFileExist configFilePath
if configExists then do