diff --git a/src/fields_processing.jl b/src/fields_processing.jl index e8c2338..37db178 100644 --- a/src/fields_processing.jl +++ b/src/fields_processing.jl @@ -23,6 +23,16 @@ end update_with_coding!(output, coding::Missing, index, coding_to_column_indices) = nothing +maybe_convert_to_int(v, ::Type{<:AbstractFloat}) = + convert(Vector{Int}, v) + +maybe_convert_to_int(v, ::Type{<:Union{Missing, AbstractFloat}}) = + convert(Vector{Union{Missing, Int}}, v) + +maybe_convert_to_int(v, ::Any) = v + +maybe_convert_to_int(v) = maybe_convert_to_int(v, eltype(v)) + """ process_binary_arrayed(dataset, fields_entry) """ @@ -55,7 +65,7 @@ function process_binary_arrayed(dataset, fields_entry) # This means that a trait is declared present # if it is diagnosed at any of the assessment visits for colname in field_columns - column = dataset[!, colname] + column = maybe_convert_to_int(dataset[!, colname]) for index in eachindex(column) coding = getindex(column, index) update_with_coding!(output, coding, index, coding_to_column_indices) diff --git a/test/fields_processing.jl b/test/fields_processing.jl new file mode 100644 index 0000000..5d3c711 --- /dev/null +++ b/test/fields_processing.jl @@ -0,0 +1,25 @@ +module TestFieldsProcessing + +using DataFrames +using Test +using UKBMain + +@testset "Test misc functions" begin + data = DataFrame( + A = [1.0, missing, 2.0], + B = [1, missing, 2], + C = [1., 2., 3.] + ) + intA = UKBMain.maybe_convert_to_int(data.A) + @test eltype(intA) === Union{Missing, Int} + intB = UKBMain.maybe_convert_to_int(data.B) + @test intB === data.B + intC = UKBMain.maybe_convert_to_int(data.C) + @test eltype(intC) === Int + + +end + +end + +true \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index 9a3dea6..1f3581c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,5 @@ using Test @test include("fields_list.jl") -@test include("datasets_extraction.jl") \ No newline at end of file +@test include("datasets_extraction.jl") +@test include("fields_processing.jl") \ No newline at end of file