Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
PharmCat committed Sep 1, 2021
1 parent 5a3b344 commit 2edf49a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MetidaBase"
uuid = "075456b7-4006-432f-9324-2f8453996c49"
authors = ["PharmCat <[email protected]> and contributors"]
version = "0.4.2"
version = "0.4.3"

[deps]
#StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
Expand Down
17 changes: 7 additions & 10 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
struct MetidaTable{T <: NamedTuple}
table::T
end
function metida_table(table::NamedTuple)
MetidaTable(table)
end
function metida_table(args...; kwargs...)
MetidaTable(metida_table_(args...; kwargs...))
end
function metida_table_(args...; names = nothing)
if length(args) > 1
e1 = length(args[1])
Expand All @@ -13,7 +19,7 @@ function metida_table_(args...; names = nothing)
end
end
if isnothing(names)
names = Tuple(Symbol.("x" .* string.(collect(1:length(args)))))
names = Tuple(Symbol.(:x , Symbol.(collect(1:length(args)))))
else
if length(args) != length(names) error("Length args and names not equal") end
if !(typeof(names) <: Tuple)
Expand All @@ -22,12 +28,6 @@ function metida_table_(args...; names = nothing)
end
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 @@ -309,9 +309,6 @@ function metida_table_(obj::DataSet{RD}; order = nothing, results = nothing, ids
mt2 = metida_table_((obj[:, c] for c in ressetl)...; names = ressetl)
merge(mt1, mt2)
end
function metida_table(obj::DataSet{RD}; kwargs...) where RD <: AbstractIDResult
metida_table(metida_table_(obj; kwargs...))
end
################################################################################
# TypedTables.jl interface

Expand Down
56 changes: 36 additions & 20 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,55 @@ using Test, Tables, TypedTables, CSV

@testset "MetidaBase.jl" begin
io = IOBuffer();
# Metida table names - auto
mt = MetidaBase.metida_table([1,2,3], ["a", "b", "c"])
@test names(mt) == [:x1, :x2]
# Metida table names - defined
mt = MetidaBase.metida_table([1,2,3], ["a", "b", "c"], names = (:a, :b))
# Push row
pushfirst!(mt, [0, " "])
@test mt[1, :a] == 0
# Set element
mt[1, :b] = "_"
@test mt[1, :b] == "_"
# push Named tuple as row
ntr = NamedTuple{(:b, :a)}(["d", 10])
pushfirst!(mt, ntr)
@test mt[1, :b] == "d"
mt[:, :b]
Base.show(io, mt)
# Test
@test mt[:, :b] == ["d", "_", "a", "b", "c"]
# Test show
@test_nowarn Base.show(io, mt)

# Tst size
@test size(mt, 1) == 5
@test size(mt, 2) == 2

df = Table(mt)

# Tables rows method
rows = Tables.rows(mt)

# MetidaTable > TypedTables
df = Table(mt)

# Enumerate
for (i,j) in enumerate(mt)
@test mt[i, :a] == j[1]
@test mt[i, :b] == j[2]
end

# Appent one table to another
l1 = length(mt)
mt2 = MetidaBase.metida_table([1,2,3], ["a", "b", "c"], names = (:a, :b))
append!(mt, mt2)
@test l1 + length(mt2) == length(mt)
mt2 = MetidaBase.metida_table(["e", "f", "g"], [1,2,3], names = (:b, :a))
append!(mt, mt2)

CSV.write(io, mt)
# CSV compat test
@test_nowarn CSV.write(io, mt)

# Structures
############################################################################
struct ExampleIDStruct <: MetidaBase.AbstractSubject
#time
#obs
Expand All @@ -45,31 +62,28 @@ using Test, Tables, TypedTables, CSV
data::T
result::Dict
end

exiddsv = Vector{ExampleIDStruct}(undef, 3)
for i in 1:3
exiddsv[i] = ExampleIDStruct(Dict(:a => 1, :b => 1))
end
exidds = MetidaBase.DataSet(exiddsv)

############################################################################
# DATASET Tests
# Test length
@test length(MetidaBase.getdata(exidds)) == length(exidds)


# getid
MetidaBase.getid(exidds[2])[:a] = 3
MetidaBase.getid(exidds[3])[:a] = 2
MetidaBase.getid(exidds[2])[:b] = 2
MetidaBase.getid(exidds[3])[:b] = 3
MetidaBase.getid(exidds, :, :a)

exrsdsv = Vector{ExampleResultStruct}(undef, length(exidds))

for i in 1:length(exidds)
exrsdsv[i] = ExampleResultStruct(exidds[i], Dict(:r1 => 3, :r2 => 4))
end

exrsds = MetidaBase.DataSet(exrsdsv)


@test exrsds[:, :r1][1] == 3
@test exrsds[1, :r1] == 3

Expand All @@ -79,7 +93,7 @@ using Test, Tables, TypedTables, CSV
@test MetidaBase.getid(exidds[3], :a) == 3
MetidaBase.getid(exrsds, :, :a)

sort!(exrsds, :a)
@test_nowarn sort!(exrsds, :a)

@test first(exrsds) == exrsds[1]

Expand All @@ -90,13 +104,16 @@ using Test, Tables, TypedTables, CSV
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])
@test_nowarn map(identity, exidds)

mt = MetidaBase.metida_table(exrsds)
mt = MetidaBase.metida_table(exrsds; results = :r1, ids = :a)
smt = MetidaBase.Tables.schema(mt)
# TypedTables export
@test_nowarn Table(exrsds; results = :r1, ids = [:a, :b])
#Iterators data
v1 = [1,2,-6,missing,NaN]
#Iterators tests
itr1 = MetidaBase.skipnanormissing(v1)
for i in itr1
@test !MetidaBase.isnanormissing(i)
Expand All @@ -115,11 +132,10 @@ using Test, Tables, TypedTables, CSV
keys(itr2)
@test length(itr2) == 2

#Ststutils
MetidaBase.sdfromcv(0.4) 0.38525317015992666
MetidaBase.varfromcv(0.4) 0.1484200051182734
MetidaBase.cvfromvar(0.4) 0.7013021443295824
MetidaBase.cvfromsd(0.4) 0.41654636115540644

smt = MetidaBase.Tables.schema(mt)

end

2 comments on commit 2edf49a

@PharmCat
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/43982

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.3 -m "<description of version>" 2edf49a3afde89470ac3e014e7d413fb509b6829
git push origin v0.4.3

Please sign in to comment.