Skip to content

Commit

Permalink
Merge pull request #5 from TARGENE/fix_type_issues
Browse files Browse the repository at this point in the history
add downcasting of binary arrayed fields to Int
  • Loading branch information
olivierlabayle authored Apr 16, 2023
2 parents 71e468f + b3747e2 commit d32d5ac
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/fields_processing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
"""
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions test/fields_processing.jl
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Test

@test include("fields_list.jl")
@test include("datasets_extraction.jl")
@test include("datasets_extraction.jl")
@test include("fields_processing.jl")

0 comments on commit d32d5ac

Please sign in to comment.