19 #ifndef mitkModelTestFixture_h
20 #define mitkModelTestFixture_h
26 #include <itksys/SystemTools.hxx>
30 #include <nlohmann/json.hpp>
67 std::ifstream file(pathToFile);
71 MITK_ERROR <<
"Could not open \"" << pathToFile <<
"\"!";
75 json_obj = nlohmann::json::parse(file);
77 catch (
const json::exception& e)
96 testparameters.SetSize(modelValues_json_obj[
"modelParameterValues"].size());
97 for (
unsigned long i = 0; i < modelValues_json_obj[
"modelParameterValues"].size(); ++i)
99 testparameters[i] = modelValues_json_obj[
"modelParameterValues"][i];
101 return testparameters;
114 for (
unsigned long i = 0; i < modelValues_json_obj[
"staticParameterValues"].size(); ++i)
118 staticParameterName = profile_json_obj[
"staticParameterNames"][i];
119 for (
unsigned int j = 0; j < modelValues_json_obj[
"staticParameterValues"][i].size(); ++j)
121 staticParameterValues.push_back(modelValues_json_obj[
"staticParameterValues"][i][j]);
123 staticParameterMap.insert(std::make_pair(staticParameterName, staticParameterValues));
125 return staticParameterMap;
139 testmodel->SetStaticParameters(staticParameterMap);
154 CPPUNIT_ASSERT_MESSAGE(
"Checking number of parameters in model.", testmodel->GetNumberOfParameters() == profile_json_obj[
"numberOfParameters"].get<
unsigned int>());
155 for (
unsigned long i = 0; i < profile_json_obj[
"numberOfParameters"]; i++)
157 CPPUNIT_ASSERT_MESSAGE(
"Checking parameter names.", testmodel->GetParameterNames()[i] == profile_json_obj[
"parameterNames"][i].get<std::string>());
158 CPPUNIT_ASSERT_MESSAGE(
"Checking parameter scales.", testmodel->GetParameterScales()[testmodel->GetParameterNames()[i]] == profile_json_obj[
"parameterScales"][i].get<
double>());
159 CPPUNIT_ASSERT_MESSAGE(
"Checking parameter units.", testmodel->GetParameterUnits()[testmodel->GetParameterNames()[i]] == profile_json_obj[
"parameterUnits"][i].get<std::string>());
161 CPPUNIT_ASSERT_MESSAGE(
"Checking number of derived parameters in model.", testmodel->GetNumberOfDerivedParameters() == profile_json_obj[
"numberOfDerivedParameters"].get<
unsigned int>());
162 for (
unsigned long i = 0; i < profile_json_obj[
"numberOfDerivedParameters"]; i++)
164 CPPUNIT_ASSERT_MESSAGE(
"Checking derived parameter names.", testmodel->GetDerivedParameterNames()[i] == profile_json_obj[
"derivedParameterNames"][i].get<std::string>());
165 CPPUNIT_ASSERT_MESSAGE(
"Checking derived parameter scales.", testmodel->GetDerivedParameterScales()[testmodel->GetDerivedParameterNames()[i]] == profile_json_obj[
"derivedParameterScales"][i].get<
double>());
166 CPPUNIT_ASSERT_MESSAGE(
"Checking derived parameter units.", testmodel->GetDerivedParameterUnits()[testmodel->GetDerivedParameterNames()[i]] == profile_json_obj[
"derivedParameterUnits"][i].get<std::string>());
168 CPPUNIT_ASSERT_MESSAGE(
"Checking number of static parameters in model.", testmodel->GetNumberOfStaticParameters() == profile_json_obj[
"numberOfStaticParameters"].get<
unsigned int>());
169 for (
unsigned long i = 0; i < profile_json_obj[
"numberOfStaticParameters"]; i++)
171 CPPUNIT_ASSERT_MESSAGE(
"Checking static parameter names.", testmodel->GetStaticParameterNames()[i] == profile_json_obj[
"staticParameterNames"][i].get<std::string>());
172 CPPUNIT_ASSERT_MESSAGE(
"Checking static parameter units.", testmodel->GetStaticParameterUnits()[testmodel->GetStaticParameterNames()[i]] == profile_json_obj[
"staticParameterUnits"][i].get<std::string>());
174 CPPUNIT_ASSERT_MESSAGE(
"Checking function string.", testmodel->GetFunctionString() == profile_json_obj[
"functionString"].get<std::string>());
175 CPPUNIT_ASSERT_MESSAGE(
"Checking class ID.", testmodel->GetClassID() == profile_json_obj[
"classID"].get<std::string>());
176 CPPUNIT_ASSERT_MESSAGE(
"Checking model display name.", testmodel->GetModelDisplayName() == profile_json_obj[
"modelDisplayName"].get<std::string>());
177 CPPUNIT_ASSERT_MESSAGE(
"Checking model type.", testmodel->GetModelType() == profile_json_obj[
"modelType"].get<std::string>());
178 CPPUNIT_ASSERT_MESSAGE(
"Checking x name.", testmodel->GetXName() == profile_json_obj[
"xName"].get<std::string>());
179 CPPUNIT_ASSERT_MESSAGE(
"Checking x axis name.", testmodel->GetXAxisName() == profile_json_obj[
"xAxisName"].get<std::string>());
180 CPPUNIT_ASSERT_MESSAGE(
"Checking x axis unit.", testmodel->GetXAxisUnit() == profile_json_obj[
"xAxisUnit"].get<std::string>());
181 CPPUNIT_ASSERT_MESSAGE(
"Checking y axis name.", testmodel->GetYAxisName() == profile_json_obj[
"yAxisName"].get<std::string>());
182 CPPUNIT_ASSERT_MESSAGE(
"Checking y axis unit.", testmodel->GetYAxisUnit() == profile_json_obj[
"yAxisUnit"].get<std::string>());
199 for (
unsigned int j = 0; j < modelValues_json_obj[
"modelValues"].size(); j++)
201 json modelValues_json_obj_current = modelValues_json_obj[
"modelValues"][j];
207 timeGrid.SetSize(modelValues_json_obj_current[
"timeGrid"].size());
208 for (
unsigned long i = 0; i < modelValues_json_obj_current[
"timeGrid"].size(); ++i)
210 timeGrid[i] = modelValues_json_obj_current[
"timeGrid"][i];
212 testmodel->SetTimeGrid(timeGrid);
220 signal = testmodel->GetSignal(testparameters);
225 std::stringstream ss;
226 ss <<
"Checking signal for model parameter set " << j <<
".";
227 std::string message = ss.str();
228 for (
unsigned long i = 0; i < signal.size(); i++)
230 CPPUNIT_ASSERT_MESSAGE(message,
mitk::Equal(signal[i], modelValues_json_obj_current[
"signal"][i], 1e-6,
true) ==
true);
247 for (
unsigned int j = 0; j < modelValues_json_obj[
"modelValues"].size(); j++)
249 json modelValues_json_obj_current = modelValues_json_obj[
"modelValues"][j];
253 timeGrid.SetSize(modelValues_json_obj_current[
"timeGrid"].size());
254 for (
unsigned long i = 0; i < modelValues_json_obj_current[
"timeGrid"].size(); ++i)
256 timeGrid[i] = modelValues_json_obj_current[
"timeGrid"][i];
258 testmodel->SetTimeGrid(timeGrid);
262 derivedParameterMap = testmodel->GetDerivedParameters(testparameters);
263 std::stringstream ss;
264 ss <<
"Checking derived parameters for model parameter set " << j <<
".";
265 std::string message = ss.str();
266 for (
unsigned long i = 0; i < modelValues_json_obj_current[
"derivedParameterValues"].size(); i++)
268 CPPUNIT_ASSERT_MESSAGE(message,
mitk::Equal(derivedParameterMap[testmodel->GetDerivedParameterNames()[i]], modelValues_json_obj_current[
"derivedParameterValues"][i], 1e-6,
true) ==
true);
std::map< ParameterNameType, DerivedParameterValueType > DerivedParameterMapType
std::map< ParameterNameType, StaticParameterValuesType > StaticParameterMapType
std::vector< StaticParameterValueType > StaticParameterValuesType
ModelTraitsInterface::ParameterNameType ParameterNameType
ModelTraitsInterface::ModelResultType ModelResultType
itk::Array< double > TimeGridType
Type defining the time grid used by models.
ModelTraitsInterface::ParametersType ParametersType
Test fixture for parameterized tests.
static std::string GetTestDataFilePath(const std::string &testData)
Get the absolute path for test data.
Test fixture base class for model fit unit tests.
static ModelBase::StaticParameterMapType ParseStaticParameters(const json profile_json_obj, const json modelValues_json_obj)
Parse static parameter names and values from JSON into a StaticParameterMapType.
static ModelBase::ParametersType ParseTestParameters(const json modelValues_json_obj)
Extract model parameter values from a JSON object into a ParametersType array.
static void CompareModelAndReferenceDerivedParameters(const mitk::ModelBase::Pointer testmodel, json modelValues_json_obj)
Assert that a model's derived parameters match a JSON reference for all parameter sets.
static void CompareModelAndReferenceSignal(mitk::ModelBase::Pointer testmodel, const json modelValues_json_obj, const json profile_json_obj)
Assert that a model's computed signal matches a JSON reference for all parameter sets.
static void CompareModelAndReferenceProfile(const mitk::ModelBase::Pointer testmodel, const json profile_json_obj)
Assert that a model's profile (parameter names, scales, units, etc.) matches a JSON reference.
static json ParseJSONFile(const std::string path)
Parse a JSON file from the test data directory.
static void SetStaticParametersForTest(mitk::ModelBase::Pointer testmodel, const json profile_json_obj, const json modelValues_json_obj)
Convenience method that parses static parameters from JSON and applies them to a model.
#define MITK_ERROR
Log an error message.
Find image slices visible on a given plane.
MITKNEWMODULE_EXPORT bool Equal(mitk::ExampleDataStructure *leftHandSide, mitk::ExampleDataStructure *rightHandSide, mitk::ScalarType eps, bool verbose)
Returns true if the example data structures are considered equal.