From fa61a776cfa69a94c2770423154db0c203bb80c6 Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Sat, 23 Nov 2024 18:28:00 +0100 Subject: [PATCH] Add missing fields in ExpData HDF5 I/O Closes #2589 --- include/amici/hdf5.h | 3 +- src/hdf5.cpp | 94 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 2 deletions(-) diff --git a/include/amici/hdf5.h b/include/amici/hdf5.h index 731c4ddecc..d8acf8881e 100644 --- a/include/amici/hdf5.h +++ b/include/amici/hdf5.h @@ -182,8 +182,7 @@ void writeSimulationExpData( void writeSimulationExpData( ExpData const& edata, std::string const& hdf5Filename, std::string const& hdf5Location - ); - +); /** * @brief Check whether an attribute with the given name exists diff --git a/src/hdf5.cpp b/src/hdf5.cpp index a22ec65a2b..8ee20c0dbe 100644 --- a/src/hdf5.cpp +++ b/src/hdf5.cpp @@ -195,6 +195,47 @@ std::unique_ptr readSimulationExpData( )); } + if (locationExists(file, hdf5Root + "/parameters")) { + edata->parameters = getDoubleDataset1D(file, hdf5Root + "/parameters"); + } + + if (locationExists(file, hdf5Root + "/x0")) { + edata->x0 = getDoubleDataset1D(file, hdf5Root + "/x0"); + } + + if (locationExists(file, hdf5Root + "/sx0")) { + edata->sx0 = getDoubleDataset1D(file, hdf5Root + "/sx0"); + } + + if (locationExists(file, hdf5Root + "/pscale")) { + auto pscaleInt = getIntDataset1D(file, hdf5Root + "/pscale"); + edata->pscale.resize(pscaleInt.size()); + for (int i = 0; (unsigned)i < pscaleInt.size(); ++i) + edata->pscale[i] = static_cast(pscaleInt[i]); + } + + if (locationExists(file, hdf5Root + "/plist")) { + edata->plist = getIntDataset1D(file, hdf5Root + "/plist"); + } + + if (locationExists( + file, hdf5Root + "/reinitialization_state_idxs_presim" + )) { + edata->reinitialization_state_idxs_presim = getIntDataset1D( + file, hdf5Root + "/reinitialization_state_idxs_presim" + ); + } + + if (locationExists(file, hdf5Root + "/reinitialization_state_idxs_sim")) { + edata->reinitialization_state_idxs_sim = getIntDataset1D( + file, hdf5Root + "/reinitialization_state_idxs_sim" + ); + } + + if (attributeExists(file, hdf5Root, "tstart")) { + edata->tstart_ = getDoubleScalarAttribute(file, hdf5Root, "tstart"); + } + return edata; } @@ -262,6 +303,59 @@ void writeSimulationExpData( file.getId(), hdf5Location.c_str(), "reinitializeFixedParameterInitialStates", &int_attr, 1 ); + + if (!edata.parameters.empty()) + createAndWriteDouble1DDataset( + file, hdf5Location + "/parameters", edata.parameters + ); + + if (!edata.x0.empty()) + createAndWriteDouble1DDataset(file, hdf5Location + "/x0", edata.x0); + if (!edata.sx0.empty()) + createAndWriteDouble1DDataset(file, hdf5Location + "/sx0", edata.sx0); + + std::vector int_buffer; + + if (!edata.pscale.empty()) { + int_buffer.resize(edata.pscale.size()); + for (int i = 0; (unsigned)i < edata.pscale.size(); i++) + int_buffer[i] = static_cast(edata.pscale[i]); + createAndWriteInt1DDataset(file, hdf5Location + "/pscale", int_buffer); + } + + if (!edata.plist.empty()) { + int_buffer.resize(edata.plist.size()); + for (int i = 0; (unsigned)i < edata.plist.size(); i++) + int_buffer[i] = static_cast(edata.plist[i]); + createAndWriteInt1DDataset(file, hdf5Location + "/plist", int_buffer); + } + + if (!edata.reinitialization_state_idxs_presim.empty()) { + int_buffer.resize(edata.reinitialization_state_idxs_presim.size()); + for (int i = 0; + (unsigned)i < edata.reinitialization_state_idxs_presim.size(); i++) + int_buffer[i] + = static_cast(edata.reinitialization_state_idxs_presim[i]); + createAndWriteInt1DDataset( + file, hdf5Location + "/reinitialization_state_idxs_presim", + int_buffer + ); + } + + if (!edata.reinitialization_state_idxs_sim.empty()) { + int_buffer.resize(edata.reinitialization_state_idxs_sim.size()); + for (int i = 0; + (unsigned)i < edata.reinitialization_state_idxs_sim.size(); i++) + int_buffer[i] + = static_cast(edata.reinitialization_state_idxs_sim[i]); + createAndWriteInt1DDataset( + file, hdf5Location + "/reinitialization_state_idxs_sim", int_buffer + ); + } + + H5LTset_attribute_double( + file.getId(), hdf5Location.c_str(), "tstart", &edata.tstart_, 1 + ); } void writeReturnData(