1+ module HealthBaseOMOPExt
2+
3+ using HealthBase
4+ using DataFrames
5+ using OMOPCommonDataModel
6+
7+ __init__ () = @info " OMOP CDM extension for HealthBase has been loaded!"
8+
9+ """
10+ HealthTable(df::DataFrame, omop_cdm_version="5.4"; disable_type_enforcement=false)
11+
12+ Validate a DataFrame against the OMOP CDM specification for the given version.
13+
14+ Checks column names/types, attaches OMOP metadata to columns, and returns the DataFrame.
15+
16+ If `disable_type_enforcement` is true, type mismatches emit warnings instead of errors.
17+ """
18+ function HealthBase. HealthTable (df:: DataFrame , omop_cdm_version= " 5.4" ; disable_type_enforcement= false )
19+ # TODO : have to add logic for version specific fields types
20+ omop_fields = Dict {String, Dict{Symbol, Any}} ()
21+
22+ for t in subtypes (OMOPCommonDataModel. CDMType)
23+ for f in fieldnames (t)
24+ actual_field_type = fieldtype (t, f)
25+ omop_fields[string (f)] = Dict (:type => actual_field_type)
26+ end
27+ end
28+
29+ for col in names (df)
30+ if haskey (omop_fields, col)
31+ fieldinfo = omop_fields[col]
32+ expected_type = get (fieldinfo, :type , Any)
33+ actual_type = eltype (df[! , col])
34+
35+ if ! (actual_type <: expected_type )
36+ msg = " Column '$(col) ' has type $(actual_type) , expected $(expected_type) "
37+ if disable_type_enforcement
38+ @warn msg
39+ else
40+ throw (ArgumentError (msg))
41+ end
42+ end
43+
44+ for (key, val) in fieldinfo
45+ colmetadata! (df, col, string (key), string (val), style= :note )
46+ end
47+ end
48+ end
49+
50+ return df
51+ end
0 commit comments