Skip to content

Commit

Permalink
potential solution for the expression name issue
Browse files Browse the repository at this point in the history
  • Loading branch information
nevrome committed Nov 25, 2023
1 parent dea01c5 commit 316eb41
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
17 changes: 10 additions & 7 deletions src/Currycarbon/CLI/RunCalibrate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -170,25 +170,28 @@ runCalibrate (
-- | Helper function to replace empty input names with a sequence of numbers,
-- to get each input date an unique identifier
replaceEmptyNames :: [NamedCalExpr] -> [NamedCalExpr]
replaceEmptyNames = zipWith (modifyExpr . show) ([1..] :: [Integer])
replaceEmptyNames = zipWith (modifyNamedExpr . show) ([1..] :: [Integer])
where
modifyExpr :: String -> NamedCalExpr -> NamedCalExpr
modifyExpr i nexpr = nexpr { _expr = replaceName i (_expr nexpr) }
modifyNamedExpr :: String -> NamedCalExpr -> NamedCalExpr
modifyNamedExpr i nexpr =
if _exprID nexpr == ""
then nexpr { _exprID = i, _expr = replaceName i (_expr nexpr) }
else nexpr { _expr = replaceName i (_expr nexpr) }
replaceName :: String -> CalExpr -> CalExpr
replaceName i (UnCalDate (UncalC14 name x y)) =
if name == "unknownSampleName"
if name == ""
then UnCalDate $ UncalC14 i x y
else UnCalDate $ UncalC14 name x y
replaceName i (WindowBP (TimeWindowBP name start stop)) =
if name == "unknownSampleName"
if name == ""
then WindowBP $ TimeWindowBP i start stop
else WindowBP $ TimeWindowBP name start stop
replaceName i (WindowBCAD (TimeWindowBCAD name start stop)) =
if name == "unknownSampleName"
if name == ""
then WindowBCAD $ TimeWindowBCAD i start stop
else WindowBCAD $ TimeWindowBCAD name start stop
replaceName i (CalDate (CalPDF name x y)) =
if name == "unknownSampleName"
if name == ""
then CalDate $ CalPDF i x y
else CalDate $ CalPDF name x y
replaceName i (SumCal a b) = SumCal (replaceName (i ++ "s") a) (replaceName (i ++ "S") b)
Expand Down
13 changes: 6 additions & 7 deletions src/Currycarbon/Parsers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,8 @@ renderCalDatePretty ascii (calExpr, calPDF, calC14) =
renderNamedCalExpr :: NamedCalExpr -> String
renderNamedCalExpr (NamedCalExpr exprID calExpr) = renderExprID exprID ++ " " ++ renderCalExpr calExpr

renderExprID :: Maybe String -> String
renderExprID Nothing = ""
renderExprID (Just s) = "{" ++ s ++ "}"
renderExprID :: String -> String
renderExprID s = "[" ++ s ++ "]"

renderCalExpr :: CalExpr -> String
renderCalExpr (UnCalDate a) = renderUncalC14 a
Expand Down Expand Up @@ -160,16 +159,16 @@ expr :: P.Parser CalExpr
expr = P.try addOperator P.<|> term -- <* P.eof

namedExpr :: P.Parser NamedCalExpr
namedExpr = P.try record P.<|> (NamedCalExpr Nothing <$> expr)
namedExpr = P.try record P.<|> (NamedCalExpr "" <$> expr)
where
record = parseRecordType "calExpr" $ P.try long P.<|> short
long = do
name <- parseArgument "name" parseAnyString
ex <- parseArgument "expr" expr
return (NamedCalExpr (Just name) ex)
return (NamedCalExpr name ex)
short = do
ex <- parseArgument "expr" expr
return (NamedCalExpr Nothing ex)
return (NamedCalExpr "" ex)

readNamedCalExprs :: String -> Either String [NamedCalExpr]
readNamedCalExprs s =
Expand Down Expand Up @@ -243,7 +242,7 @@ parseUncalC14 = parseRecordType "uncalC14" $ P.try long P.<|> short
short = do
age <- parseArgument "age" parseWord
sigma <- parseArgument "sigma" parseWord
return (UncalC14 "unknownSampleName" age sigma)
return (UncalC14 "" age sigma)

-- CalC14
-- | Write 'CalC14's to the file system. The output file is a long .csv file with the following structure:
Expand Down
4 changes: 1 addition & 3 deletions src/Currycarbon/SumCalibration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ evalNamedCalExpr :: CalibrateDatesConf -> CalCurveBP -> NamedCalExpr -> Either C
evalNamedCalExpr conf curve (NamedCalExpr exprID expr) =
case evalCalExpr conf curve expr of
Left err -> Left err
Right calPDF -> case exprID of
Nothing -> Right calPDF
Just x -> Right $ calPDF { _calPDFid = x }
Right calPDF -> Right calPDF { _calPDFid = exprID }

-- | Evaluate a dating expression by calibrating the individual dates and forming the respective
-- sums and products of post-calibration density distributions
Expand Down
2 changes: 1 addition & 1 deletion src/Currycarbon/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ data CalPDF = CalPDF {
-- | A data type for named calibration expressions
data NamedCalExpr = NamedCalExpr {
-- | Expression identifier
_exprID :: Maybe String
_exprID :: String
-- | Expression
, _expr :: CalExpr
}
Expand Down

0 comments on commit 316eb41

Please sign in to comment.