Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
e1aa7ee
PoC: PEtab math parser and sympy converter
dweindl May 22, 2024
ebce955
use
dweindl May 22, 2024
d4bfd82
noqa
dweindl May 29, 2024
1622b6d
refactor
dweindl May 29, 2024
cfbacbe
update and/or precedence
dweindl May 29, 2024
47d0b1e
Change piecewise arg order
dweindl May 29, 2024
03ff238
multiple piecewise conditions
dweindl May 29, 2024
d441d88
check for boolean conditions
dweindl May 29, 2024
44ad34c
refactor
dweindl May 29, 2024
195cebe
reserved
dweindl May 29, 2024
41acb4d
case-insensitive reserved keywords
dweindl May 30, 2024
7d1c941
Merge branch 'develop' into parse_math
dweindl Jun 25, 2024
143cb2e
Merge branch 'develop' into parse_math
dweindl Jun 25, 2024
5d7552f
Merge branch 'develop' into parse_math
dweindl Jun 25, 2024
1c95d56
Handle bool<->float
dweindl Jun 25, 2024
81c14e6
Merge branch 'develop' into parse_math
dweindl Jun 25, 2024
dd16b09
always float
dweindl Jun 26, 2024
5680b4d
Handle symbolic expressions
dweindl Jun 26, 2024
ba8990f
boolean not
dweindl Jun 26, 2024
563fac5
Test cases from test suite repo
dweindl Jun 26, 2024
6555130
Lexer listener
dweindl Jun 26, 2024
2040ce9
error on complex
dweindl Jun 26, 2024
c19103f
log(0)
dweindl Jun 26, 2024
b7ac852
Fix real=True, fix test tables
dweindl Jun 26, 2024
8d2b778
Update petab/math/sympify.py
dweindl Jun 26, 2024
c0d5e57
rename, fix pow
dweindl Jun 26, 2024
2485c05
Merge branch 'develop' into parse_math
dweindl Jun 26, 2024
5e0b7ab
simplify
dweindl Jun 26, 2024
d7779b5
cleanup
dweindl Jun 26, 2024
87de272
Merge branch 'develop' into parse_math
dweindl Jun 26, 2024
27f3de0
doc, prettify
dweindl Jun 26, 2024
18c59e4
Update petab/math/SympyVisitor.py
dweindl Jun 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions petab/calculate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import numpy as np
import pandas as pd
import sympy
import sympy as sp

import petab

Expand Down Expand Up @@ -144,7 +144,7 @@ def calculate_residuals_for_table(
return residual_df


def get_symbolic_noise_formulas(observable_df) -> dict[str, sympy.Expr]:
def get_symbolic_noise_formulas(observable_df) -> dict[str, sp.Expr]:
"""Sympify noise formulas.

Arguments:
Expand All @@ -167,7 +167,7 @@ def get_symbolic_noise_formulas(observable_df) -> dict[str, sympy.Expr]:

def evaluate_noise_formula(
measurement: pd.Series,
noise_formulas: dict[str, sympy.Expr],
noise_formulas: dict[str, sp.Expr],
parameter_df: pd.DataFrame,
simulation: numbers.Number,
) -> float:
Expand All @@ -192,16 +192,18 @@ def evaluate_noise_formula(
)
# fill in measurement specific parameters
overrides = {
f"noiseParameter{i_obs_par + 1}_{observable_id}": obs_par
sp.Symbol(
f"noiseParameter{i_obs_par + 1}_{observable_id}", real=True
): obs_par
for i_obs_par, obs_par in enumerate(observable_parameter_overrides)
}

# fill in observables
overrides[observable_id] = simulation
overrides[sp.Symbol(observable_id, real=True)] = simulation

# fill in general parameters
for row in parameter_df.itertuples():
overrides[row.Index] = row.nominalValue
overrides[sp.Symbol(row.Index, real=True)] = row.nominalValue

# replace parametric measurement specific parameters
for key, value in overrides.items():
Expand Down
35 changes: 35 additions & 0 deletions petab/math/PetabMathExprLexer.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Lexer grammar for PEtab math expressions
// run `regenerate.sh` to regenerate the lexer
lexer grammar PetabMathExprLexer;


NUMBER : EXPONENT_FLOAT | INTEGER | POINT_FLOAT | INF;
INTEGER : DIGITS ;
EXPONENT_FLOAT : (INTEGER | POINT_FLOAT) EXPONENT ;
POINT_FLOAT : DIGITS '.' DIGITS ;
fragment EXPONENT: ('e' | 'E') ('+' | '-')? DIGITS ;
FLOAT_NUMBER: POINT_FLOAT | EXPONENT_FLOAT;
fragment DIGITS : [0-9]+ ;

WS : [ \t\r\n]+ -> skip ;
TRUE : 'true' ;
FALSE : 'false' ;
INF : 'inf' ;
NAME : [a-zA-Z_][a-zA-Z0-9_]* ;
OPEN_PAREN : '(' ;
CLOSE_PAREN : ')' ;
BOOLEAN_OR : '||' ;
BOOLEAN_AND : '&&' ;
GT : '>' ;
LT : '<' ;
GTE : '>=' ;
LTE : '<=' ;
EQ : '==' ;
NEQ : '!=' ;
PLUS : '+' ;
MINUS : '-' ;
ASTERISK : '*' ;
SLASH : '/' ;
CARET: '^';
EXCLAMATION_MARK: '!';
COMMA: ',';
42 changes: 42 additions & 0 deletions petab/math/PetabMathExprParser.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Parser grammar for PEtab math expressions
// run `regenerate.sh` to regenerate the parser
parser grammar PetabMathExprParser;

options { tokenVocab=PetabMathExprLexer; }

petabExpression:
expr EOF ;

expr:
<assoc=right> expr '^' expr # PowerExpr
| ('+'|'-') expr # UnaryExpr
| '!' expr # BooleanNotExpr
| expr ('*'|'/') expr # MultExpr
| expr ('+'|'-') expr # AddExpr
| '(' expr ')' # ParenExpr
| expr comp_op expr # ComparisonExpr
| expr (BOOLEAN_AND | BOOLEAN_OR) expr # BooleanAndOrExpr
| number # Number_
| booleanLiteral # BooleanLiteral_
| functionCall # functionCall_
| var # VarExpr_
;

comp_op:
GT
| LT
| GTE
| LTE
| EQ
| NEQ
;

argumentList: expr (',' expr)* ;
functionCall: NAME OPEN_PAREN argumentList CLOSE_PAREN ;

booleanLiteral:
TRUE
| FALSE
;
number: NUMBER ;
var: NAME ;
Loading