<commit_before>#include "include/ami_hdf5.h"
#include "include/amici_interface_cpp.h"

#include <assert.h>
#ifdef AMI_HDF5_H_DEBUG
#include <execinfo.h>
#endif
#include <unistd.h>

#include "wrapfunctions.h"
#include "include/symbolic_functions.h"

#include "include/rdata_accessors.h"
#include "include/edata_accessors.h"
#include "include/udata_accessors.h"


UserData *AMI_HDF5_readSimulationUserDataFromFileName(const char* fileName, const char* datasetPath) {

    hid_t file_id = H5Fopen(fileName, H5F_ACC_RDONLY, H5P_DEFAULT);

    UserData *udata = AMI_HDF5_readSimulationUserDataFromFileObject(file_id, datasetPath);

    H5Fclose(file_id);

    return(udata);
}

UserData *AMI_HDF5_readSimulationUserDataFromFileObject(hid_t fileId, const char *datasetPath)
{
    UserData *udata = getDefaultUserData();
    if (udata == NULL)
        return(NULL);

    // Set hard-coded model dimensions
    init_modeldims(udata);

    atol       = AMI_HDF5_getDoubleScalarAttribute(fileId, datasetPath, "atol");
    rtol       = AMI_HDF5_getDoubleScalarAttribute(fileId, datasetPath, "rtol");
    maxsteps   = AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "maxsteps");
    tstart     = AMI_HDF5_getDoubleScalarAttribute(fileId, datasetPath, "tstart");
    lmm        = AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "lmm");
    iter       = AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "iter");
    linsol     = AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "linsol");
    stldet     = AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "stldet");
    interpType = AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "interpType");
    ism        = AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "ism");
    sensi_meth = (AMI_sensi_meth) AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "sensi_meth");
    sensi      = (AMI_sensi_order) AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "sensi");
    nmaxevent  = AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "nmaxevent");
    ordering   = AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "ordering");

    hsize_t length;
    int status = 0;

    status += AMI_HDF5_getDoubleArrayAttribute(fileId, datasetPath, "qpositivex", &qpositivex, &length);
    assert(length == nx);

    if(AMI_HDF5_attributeExists(fileId, datasetPath, "theta")) {
        status += AMI_HDF5_getDoubleArrayAttribute(fileId, datasetPath, "theta", &p, &length);
        if(np != length)
            return NULL;
    }

    if(AMI_HDF5_attributeExists(fileId, datasetPath, "kappa")) {
        status += AMI_HDF5_getDoubleArrayAttribute(fileId, datasetPath, "kappa", &k, &length);
        if(length != nk)
            return NULL;
    }

    if(AMI_HDF5_attributeExists(fileId, datasetPath, "ts")) {
        status += AMI_HDF5_getDoubleArrayAttribute(fileId, datasetPath, "ts", &ts, &length);
        nt = AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "nt");
        if(length != nt || status > 0)
            return NULL;
    }

    // parameter selection and reordering for sensitivities (matlab: fifth argument)
    // For now, use all parameters
    nplist = np;
    plist = new int[np];
    for (int i = 0; i < np; i++)
        plist[i] = i;

    /* Options ; matlab: fourth argument   */
    z2event = new realtype[ne];
    for(int i = 0; i < ne; ++i)
        z2event[i] = i;

    idlist = new realtype[nplist]();
    for(int i = 0; i < nplist; ++i)
        idlist[i] = 0;

    //user-provided sensitivity initialisation. this should be a matrix of dimension [#states x #parameters] default is sensitivity initialisation based on the derivative of the state initialisation
    x0data = NULL;
    sx0data = NULL;

    /* pbarm parameterscales ; matlab: sixth argument*/
    pbar = NULL;

    // xscale, matlab: seventh argument
    xbar = NULL;

    h = 0;

    udata->am_dxdotdp = NULL;
    udata->am_dwdx = NULL;
    udata->am_dwdp = NULL;
    udata->am_M = NULL;
    udata->am_dfdx = NULL;
    udata->am_stau = NULL;

    processUserData(udata);

    return udata;
}

<commit_msg>HDF IO fixes<commit_after>#include "include/ami_hdf5.h"
#include "include/amici_interface_cpp.h"

#include <assert.h>
#ifdef AMI_HDF5_H_DEBUG
#include <execinfo.h>
#endif
#include <unistd.h>

#include "wrapfunctions.h"
#include "include/symbolic_functions.h"

#include "include/rdata_accessors.h"
#include "include/edata_accessors.h"
#include "include/udata_accessors.h"


UserData *AMI_HDF5_readSimulationUserDataFromFileName(const char* fileName, const char* datasetPath) {

    hid_t file_id = H5Fopen(fileName, H5F_ACC_RDONLY, H5P_DEFAULT);

    UserData *udata = AMI_HDF5_readSimulationUserDataFromFileObject(file_id, datasetPath);

    H5Fclose(file_id);

    return(udata);
}

UserData *AMI_HDF5_readSimulationUserDataFromFileObject(hid_t fileId, const char *datasetPath)
{
    UserData *udata = getDefaultUserData();
    if (udata == NULL)
        return(NULL);

    // Set hard-coded model dimensions
    init_modeldims(udata);

    atol       = AMI_HDF5_getDoubleScalarAttribute(fileId, datasetPath, "atol");
    rtol       = AMI_HDF5_getDoubleScalarAttribute(fileId, datasetPath, "rtol");
    maxsteps   = AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "maxsteps");
    tstart     = AMI_HDF5_getDoubleScalarAttribute(fileId, datasetPath, "tstart");
    lmm        = AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "lmm");
    iter       = AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "iter");
    linsol     = AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "linsol");
    stldet     = AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "stldet");
    interpType = AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "interpType");
    ism        = AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "ism");
    sensi_meth = (AMI_sensi_meth) AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "sensi_meth");
    sensi      = (AMI_sensi_order) AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "sensi");
    nmaxevent  = AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "nmaxevent");
    ordering   = AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "ordering");

    hsize_t length;
    int status = 0;

    status += AMI_HDF5_getDoubleArrayAttribute(fileId, datasetPath, "qpositivex", &qpositivex, &length);
    assert(length == nx);

    if(AMI_HDF5_attributeExists(fileId, datasetPath, "theta")) {
        status += AMI_HDF5_getDoubleArrayAttribute(fileId, datasetPath, "theta", &p, &length);
        if(np != length)
            return NULL;
    }

    if(AMI_HDF5_attributeExists(fileId, datasetPath, "kappa")) {
        status += AMI_HDF5_getDoubleArrayAttribute(fileId, datasetPath, "kappa", &k, &length);
        if(length != nk)
            return NULL;
    }

    if(AMI_HDF5_attributeExists(fileId, datasetPath, "ts")) {
        status += AMI_HDF5_getDoubleArrayAttribute(fileId, datasetPath, "ts", &ts, &length);
        nt = AMI_HDF5_getIntScalarAttribute(fileId, datasetPath, "nt");
        if(length != nt || status > 0)
            return NULL;
    }

    // parameter selection and reordering for sensitivities (matlab: fifth argument)
    AMI_HDF5_getIntArrayAttribute(fileId, datasetPath, "sens_ind", &plist, &length);
    nplist = length;
    assert(nplist <= np);
    // TODO: currently base 1 indices are written
    for(int i = 0; i < nplist; ++i) plist[i] -= 1;

    /* Options ; matlab: fourth argument   */
    z2event = new realtype[ne];
    for(int i = 0; i < ne; ++i)
        z2event[i] = i;

    idlist = new realtype[nx]();

    //user-provided sensitivity initialisation. this should be a matrix of dimension [#states x #parameters] default is sensitivity initialisation based on the derivative of the state initialisation
    x0data = NULL;
    sx0data = NULL;

    /* pbarm parameterscales ; matlab: sixth argument*/
    pbar = NULL;

    // xscale, matlab: seventh argument
    xbar = NULL;

    h = 0;

    udata->am_dxdotdp = NULL;
    udata->am_dwdx = NULL;
    udata->am_dwdp = NULL;
    udata->am_M = NULL;
    udata->am_dfdx = NULL;
    udata->am_stau = NULL;

    processUserData(udata);

    return udata;
}


{'fraction_non_alphanumeric': 0.11201079622132254, 'fraction_numerical': 0.010428168322905164, 'mean_word_length': 3.9049338146811072, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2832616', 'n_tokens_mistral': 2908, 'n_tokens_neox': 2834, 'n_words': 575}