Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
tests/cpp/expected_results_py.h5 filter=lfs diff=lfs merge=lfs -text
*.h5 diff=hdf5
4 changes: 2 additions & 2 deletions python/sdist/amici/jax/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,11 +713,11 @@ def simulate_condition(
m_obj = obs_trafo(my, iy_trafos)
if ret == ReturnValue.chi2:
sigma_obj = self._sigmays(ts, x, p, tcl, hs, iys, ops, nps)
chi2 = jnp.square((ys_obj - m_obj) / sigma_obj)
chi2 = jnp.square((m_obj - ys_obj) / sigma_obj)
chi2 = jnp.where(ts_mask, chi2, 0.0)
output = jnp.sum(chi2)
else:
output = ys_obj - m_obj
output = m_obj - ys_obj
output = jnp.where(ts_mask, output, 0.0)
else:
raise NotImplementedError(f"Return value {ret} not implemented.")
Expand Down
18 changes: 17 additions & 1 deletion python/tests/test_pregenerated_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,10 +494,26 @@ def verify_simulation_results(
rtol=1e-3,
)
else:
expected = expected_results[field][()]
if field in ("res", "sres"):
# FIXME: Some of the stored residuals are sign-flipped
# remove this once all expected results are updated
try:
_check_results(
rdata,
field,
expected * -1,
atol=atol,
rtol=rtol,
)
continue
except AssertionError:
pass

_check_results(
rdata,
field,
expected_results[field][()],
expected,
atol=atol,
rtol=rtol,
)
Expand Down
12 changes: 6 additions & 6 deletions src/rdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -889,11 +889,11 @@ static realtype
fres(realtype y, realtype my, realtype sigma_y, ObservableScaling scale) {
switch (scale) {
case ObservableScaling::lin:
return (y - my) / sigma_y;
return (my - y) / sigma_y;
case ObservableScaling::log:
return (std::log(y) - std::log(my)) / sigma_y;
return (std::log(my) - std::log(y)) / sigma_y;
case ObservableScaling::log10:
return (std::log10(y) - std::log10(my)) / sigma_y;
return (std::log10(my) - std::log10(y)) / sigma_y;
default:
throw std::invalid_argument("only lin, log, log10 allowed.");
}
Expand Down Expand Up @@ -951,11 +951,11 @@ static realtype fsres(
auto res = fres(y, my, sigma_y, scale);
switch (scale) {
case ObservableScaling::lin:
return (sy - ssigma_y * res) / sigma_y;
return (-sy - ssigma_y * res) / sigma_y;
case ObservableScaling::log:
return (sy / y - ssigma_y * res) / sigma_y;
return (-sy / y - ssigma_y * res) / sigma_y;
case ObservableScaling::log10:
return (sy / (y * std::log(10)) - ssigma_y * res) / sigma_y;
return (-sy / (y * std::log(10)) - ssigma_y * res) / sigma_y;
default:
throw std::invalid_argument("only lin, log, log10 allowed.");
}
Expand Down
2 changes: 1 addition & 1 deletion tests/cpp/expected_results_py.h5
Git LFS file not shown
28 changes: 17 additions & 11 deletions tests/cpp/testfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ extern std::unique_ptr<amici::Model> getModel();

} // namespace generic_model

std::vector<std::string> getVariableNames(const char* name, int length)
{
std::vector<std::string> getVariableNames(std::string const& name, int length) {
std::vector<std::string> names;
names.resize(length);
for (auto& it: names) {
auto index = &it - &names[0];
it += name + std::to_string(index);
names.reserve(length);
for (int i = 0; i < length; ++i) {
names.push_back(name + std::to_string(i));
}
return names;
}
Expand Down Expand Up @@ -99,7 +97,7 @@ std::unique_ptr<ExpData> getTestExpData(Model const& model) {
return std::unique_ptr<ExpData>(new ExpData(model));
}

bool withinTolerance(double expected, double actual, double atol, double rtol, int index, const char *name) {
bool withinTolerance(double expected, double actual, double atol, double rtol, int index, std::string_view name) {
bool withinTol = fabs(expected - actual) <= atol || fabs((expected - actual) / (rtol + expected)) <= rtol;

if(!withinTol && std::isnan(expected) && std::isnan(actual))
Expand All @@ -108,8 +106,16 @@ bool withinTolerance(double expected, double actual, double atol, double rtol, i
if(!withinTol && std::isinf(expected) && std::isinf(actual))
withinTol = true;

if(!withinTol && (name == "res" || name == "sres")) {
// FIXME: Residuals are sign-flipped in old test results.
// Until the remaining MATLAB tests models are implemented in Python
// and the correct expected results have been re-generated, we accept
// both signs.
withinTol = fabs(-expected - actual) <= atol || fabs((-expected - actual) / (rtol + -expected)) <= rtol;
}

if(!withinTol) {
fprintf(stderr, "ERROR: Expected value %e, but was %e in %s at index %d.\n",expected, actual, name, index);
fprintf(stderr, "ERROR: Expected value %e, but was %e in %s at index %d.\n",expected, actual, name.data(), index);
fprintf(stderr, " Relative error: %e (tolerance was %e)\n", fabs((expected - actual) / (rtol + expected)), rtol);
fprintf(stderr, " Absolute error: %e (tolerance was %e)\n", fabs(expected - actual), atol);
printBacktrace(12);
Expand All @@ -124,12 +130,12 @@ void checkEqualArray(std::vector<double> const& expected, std::vector<double> co

for(int i = 0; (unsigned) i < expected.size(); ++i)
{
bool withinTol = withinTolerance(expected[i], actual[i], atol, rtol, i, name.c_str());
bool withinTol = withinTolerance(expected[i], actual[i], atol, rtol, i, name);
ASSERT_TRUE(withinTol);
}
}

void checkEqualArray(const double *expected, const double *actual, const int length, double atol, double rtol, const char *name) {
void checkEqualArray(const double *expected, const double *actual, const int length, double atol, double rtol, std::string_view name) {
if(!expected && !actual)
return;
if(!length)
Expand All @@ -144,7 +150,7 @@ void checkEqualArray(const double *expected, const double *actual, const int len
}
}

void checkEqualArrayStrided(const double *expected, const double *actual, int length, int strideExpected, int strideActual, double atol, double rtol, const char *name) {
void checkEqualArrayStrided(const double *expected, const double *actual, int length, int strideExpected, int strideActual, double atol, double rtol, std::string_view name) {
if(!expected && !actual)
return;

Expand Down
34 changes: 10 additions & 24 deletions tests/cpp/testfunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@

#include <H5Cpp.h>

#ifndef __APPLE__
#include <iostream>
#endif

#include <sstream>
#include <string>
#include <string_view>

namespace amici {

Expand All @@ -31,8 +27,7 @@ class ExpData;
* @param length number of variables
* @return default names/ids
*/
std::vector<std::string>
getVariableNames(const char* name, int length);
std::vector<std::string> getVariableNames(std::string const& name, int length);

/**
* @brief The Model_Test class is a model-unspecific implementation
Expand Down Expand Up @@ -191,29 +186,20 @@ void simulateVerifyWrite(const std::string &hdffileOptions,

std::unique_ptr<ExpData> getTestExpData(const Model &model);

bool withinTolerance(double expected, double actual, double atol, double rtol,
int index, const char *name);
bool withinTolerance(
double expected, double actual, double atol, double rtol, int index,
std::string_view name
);

void checkEqualArray(const double *expected, const double *actual, int length,
double atol, double rtol, const char *name);
void checkEqualArray(
double const* expected, double const* actual, int length, double atol,
double rtol, std::string_view name
);

void checkEqualArray(std::vector<double> const &expected,
std::vector<double> const &actual, double atol,
double rtol, std::string const &name);

// TODO: delete after transitioning to C++-written test results
void verifyReturnDataMatlab(const std::string &hdffile,
const std::string &resultPath,
const ReturnData *rdata, const Model *model,
double atol, double rtol);

// TODO: delete after transitioning to C++-written test results
void verifyReturnDataSensitivitiesMatlab(const H5::H5File &file_id,
const std::string &resultPath,
const ReturnData *rdata,
const Model *model, double atol,
double rtol);

void verifyReturnData(const std::string &hdffile, const std::string &resultPath,
const ReturnData *rdata, const Model *model, double atol,
double rtol);
Expand Down
Loading