eth-mds / ricu

🏥 ICU data with R 🏥
https://eth-mds.github.io/ricu/
GNU General Public License v3.0
34 stars 10 forks source link

load_dictionary not working on json from documentation #72

Open Addison-Weatherhead opened 1 month ago

Addison-Weatherhead commented 1 month ago

Calling load_dictionary on the json file provided in the documentation (https://cran.r-project.org/web/packages/ricu/ricu.pdf) (page 34/35), yields this error:

! Cannot merge concept 'glu' due to malformed 'sources' entry Backtrace: ricu::load_dictionary(cfg_dirs = "../ricu-extensions/configs/chemistry/") ricu:::parse_dictionary(...) ricu:::read_dictionary(name, cfg_dirs) ricu::get_config(...) base::Reduce(combine_fun, res, NULL) ricu (local) f(init, x[[i]]) ricu:::map(combine_sources, x[dups], y[dups], dups) base::Map(f, ..., USE.NAMES = FALSE) base::mapply(FUN = f, ..., SIMPLIFY = FALSE) ricu (local) ''(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) ricu:::stop_ricu(...) rlang::abort(...) Execution halted

What exactly is wrong with the concept-dict.json:

{ "glu": { "unit": "mg/dL", "min": 0, "max": 1000, "description": "glucose", "category": "chemistry", "sources": { "mimic_demo": [ { "ids": [50809, 50931], "table": "labevents", "sub_var": "itemid" } ] } } }

I'm using R 4.2.2, and ricu 0.6.0 (below is the entry for ricu from my renv.lock):

"ricu": { "Package": "ricu", "Version": "0.6.0", "Source": "GitHub", "RemoteType": "github", "RemoteHost": "api.github.com", "RemoteRepo": "ricu-package", "RemoteUsername": "prockenschaub", "RemoteRef": "monkeypatch0.6.0", "RemoteSha": "0969a52747c71ae9f47051d640caa5df2d5169a0", "Requirements": [ "R", "assertthat", "backports", "cli", "curl", "data.table", "fansi", "fst", "jsonlite", "methods", "openssl", "prt", "readr", "rlang", "stats", "tibble", "utils", "vctrs" ], "Hash": "2681cdea2f2f3446691071ea30f745b5" },

prockenschaub commented 3 weeks ago

Under the hood, load_dictionary always also loads the base dictionary that comes with ricu. This happens at the end of the subroutine read_dictionary by calling config_path, which itself calls user_config_path and default_config_path. The former can be set via the environment variable RICU_CONFIG_PATH.

The error comes from the fact that the .json in the documentation is copied out of the base dictionary. When you supply your own .json, ricu loads both your .json and the base dictionary and tries to combine the concepts within them. Importantly, only the "sources" part of the concept can be updated, all other entries (e.g., unit, min, max) are expected to stay the same.

So, nothing is wrong with your .json but you tried to perform an update that isn't currently allowed. You can try two fixes that should work:

Create your own concept with it's own name

{
  "glu2": {
    "unit": "mg/dL",
    "min": 0,
    "max": 1000,
    "description": "glucose",
    "category": "chemistry",
    "sources": {
      "other_database": [
        {
          "ids": [50809, 50931],
          "table": "labevents",
          "sub_var": "itemid"
        }
      ]
    }
  }
}

Just update the sources part

{
  "glu": {
    "sources": {
      "mimic_demo": [
        {
          "ids": [999],
          "table": "labevents",
          "sub_var": "itemid"
        }
      ]
    }
  }
}