Skip to content

Commit

Permalink
TypedTables support
Browse files Browse the repository at this point in the history
  • Loading branch information
PharmCat committed Aug 30, 2021
1 parent b28c49c commit 3cdb493
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
7 changes: 4 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
name = "MetidaBase"
uuid = "075456b7-4006-432f-9324-2f8453996c49"
authors = ["PharmCat <[email protected]> and contributors"]
version = "0.4.1"
version = "0.4.2"

[deps]
#StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
StatsModels = "3eaba693-59b7-5ba5-a881-562e759f1c8d"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
TypedTables = "9d95f2ec-7b3d-5a63-8d20-e2491e220bb9"

[compat]
#StatsBase = "0.29, 0.30, 0.31, 0.32, 0.33"
StatsModels = "0.6"
Tables = "1"
PrettyTables = "1"
TypedTables = "1"
julia = "1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"

[targets]
test = ["Test", "DataFrames", "CSV"]
test = ["Test", "CSV"]
2 changes: 1 addition & 1 deletion src/MetidaBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__precompile__(true)
module MetidaBase

using Tables, PrettyTables#, Reexport
using Tables, PrettyTables, TypedTables#, Reexport

#@reexport using StatsModels

Expand Down
42 changes: 27 additions & 15 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
struct MetidaTable{T <: NamedTuple}
table::T
end
function metida_table(table::NamedTuple)
MetidaTable(table)
end
function metida_table(args...; names = nothing)
function metida_table_(args...; names = nothing)
if length(args) > 1
e1 = length(args[1])
i = 2
Expand All @@ -23,7 +20,13 @@ function metida_table(args...; names = nothing)
names = Tuple(names)
end
end
MetidaBase.MetidaTable(NamedTuple{names}(args))
NamedTuple{names}(args)
end
function metida_table(table::NamedTuple)
MetidaTable(table)
end
function metida_table(args...; names = nothing)
metida_table(metida_table_(args...; names = names))
end

table(t::MetidaTable) = getfield(t, :table)
Expand Down Expand Up @@ -155,10 +158,6 @@ end
rd.result[ind]
end

#@inline function getresultindex(subj, ind::Symbol)
# getindormiss(subj.result, ind)
#end

function Base.getindex(d::DataSet{T}, col::Int, ind) where T <: AbstractResultData
getresultindex_safe(d[col], ind)
end
Expand All @@ -167,7 +166,6 @@ function Base.getindex(d::DataSet{T}, col::Colon, ind) where T <: AbstractResult
if Base.ht_keyindex(d.ds[i].result, ind) < 1 return getresultindex_safe.(d.ds, ind) end
end
getresultindex_unsafe.(d.ds, ind)
#getresultindex.(ds.data, ind)
end

Base.first(d::DataSet) = first(d.ds)
Expand All @@ -179,6 +177,7 @@ end
function Base.iterate(d::DataSet)
return Base.iterate(d.ds)
end

function Base.iterate(d::DataSet, i::Int)
return Base.iterate(d.ds, i)
end
Expand Down Expand Up @@ -256,6 +255,7 @@ function uniqueidlist(d::DataSet{T}, list::AbstractVector{Symbol}) where T <: Ab
end
dl
end

function uniqueidlist(d::DataSet{T}, list::Symbol) where T <: AbstractIdData
dl = Vector{Dict}(undef, 0)
for i in d
Expand All @@ -277,10 +277,13 @@ function subset(d::DataSet{T}, sort::Dict) where T <: AbstractIDResult
if length(inds) > 0 return DataSet(d.ds[inds]) end
DataSet(Vector{T}(undef, 0))
end
function subset(d::DataSet{T}, inds) where T
DataSet(getdata(d)[inds])
end
################################################################################
# metida_table from DataSet{AbstractIDResult}
################################################################################
function metida_table(obj::DataSet{RD}; order = nothing, results = nothing, ids = nothing) where RD <: AbstractIDResult
function metida_table_(obj::DataSet{RD}; order = nothing, results = nothing, ids = nothing) where RD <: AbstractIDResult
idset = Set(keys(first(obj).data.id))
resset = Set(keys(first(obj).result))
if length(obj) > 1
Expand All @@ -302,13 +305,22 @@ function metida_table(obj::DataSet{RD}; order = nothing, results = nothing, ids
ids idset || error("Some id not in dataset!")
idset = intersect(idset, ids)
end
mt1 = MetidaBase.metida_table((getid(obj, :, c) for c in idset)...; names = idset)
mt2 = MetidaBase.metida_table((obj[:, c] for c in ressetl)...; names = ressetl)
MetidaTable(merge(mt1.table, mt2.table))
mt1 = metida_table_((getid(obj, :, c) for c in idset)...; names = idset)
mt2 = metida_table_((obj[:, c] for c in ressetl)...; names = ressetl)
merge(mt1, mt2)
end
function metida_table(obj::DataSet{RD}; order = nothing, results = nothing, ids = nothing) where RD <: AbstractIDResult
metida_table(metida_table_(obj; order = order, results = results, ids = ids))
end
################################################################################
# MetidaFreq.jl
# TypedTables.jl interface

function TypedTables.Table(obj::DataSet{RD}; order = nothing, results = nothing, ids = nothing) where RD <: AbstractIDResult
TypedTables.Table(metida_table_(obj; order = order, results = results, ids = ids))
end


# MetidaFreq.jl
struct Proportion <: AbstractData
x::Int
n::Int
Expand Down
7 changes: 5 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using MetidaBase
using Test, DataFrames, CSV
using Test, Tables, TypedTables, CSV

@testset "MetidaBase.jl" begin
io = IOBuffer();
Expand All @@ -17,7 +17,7 @@ using Test, DataFrames, CSV
@test size(mt, 1) == 5
@test size(mt, 2) == 2

df = DataFrame(mt)
df = Table(mt)

rows = Tables.rows(mt)

Expand Down Expand Up @@ -88,11 +88,14 @@ using Test, DataFrames, CSV

MetidaBase.subset(exidds, Dict(:a => 1))
MetidaBase.subset(exrsds, Dict(:a => 1))
MetidaBase.subset(exrsds, 1:2)

map(identity, exidds)

mt = MetidaBase.metida_table(exrsds)
mt = MetidaBase.metida_table(exrsds; results = :r1, ids = :a)
Table(exrsds; results = :r1, ids = [:a, :b])
#Table(exrsds)

v1 = [1,2,-6,missing,NaN]
itr1 = MetidaBase.skipnanormissing(v1)
Expand Down

0 comments on commit 3cdb493

Please sign in to comment.