Skip to content

Commit

Permalink
update, add DataDrames support
Browse files Browse the repository at this point in the history
  • Loading branch information
PharmCat committed Jul 23, 2022
1 parent 18311a1 commit aca22f9
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 15 deletions.
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name = "MetidaBase"
uuid = "075456b7-4006-432f-9324-2f8453996c49"
authors = ["PharmCat <[email protected]> and contributors"]
version = "0.6.0"
version = "0.7.0"

[deps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
StatsModels = "3eaba693-59b7-5ba5-a881-562e759f1c8d"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
Expand All @@ -12,6 +13,7 @@ TypedTables = "9d95f2ec-7b3d-5a63-8d20-e2491e220bb9"
CPUSummary = "2a0fbf3d-bb9c-48f3-b0a9-814d99fd7ab9"

[compat]
DataFrames = "0.22, 1"
StatsBase = "0.29, 0.30, 0.31, 0.32, 0.33"
StatsModels = "0.6"
Tables = "1"
Expand Down
4 changes: 2 additions & 2 deletions src/MetidaBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
__precompile__(true)
module MetidaBase

using Tables, PrettyTables, TypedTables, StatsBase, StatsModels#, Reexport
using Tables, PrettyTables, StatsBase, StatsModels#, Reexport

#@reexport using StatsModels

import DataFrames, TypedTables
import StatsModels: StatisticalModel
import Tables: istable, columnaccess, columns, getcolumn, columnnames, schema, rowaccess, rows
import CPUSummary: num_cores
Expand Down
37 changes: 37 additions & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
struct MetidaTable{T <: NamedTuple}
table::T
end


"""
metida_table(table::NamedTuple)
Expand Down Expand Up @@ -102,6 +104,22 @@ Base.names(t::MetidaTable) = collect(keys(t.table))
function Base.getindex(t::MetidaTable, col::Colon, ind::T) where T <: Union{Symbol, Int}
Tables.getcolumn(t, ind)
end
function Base.getindex(t::MetidaTable, col::Colon, inds::AbstractVector{T}) where T <: Union{Symbol, Int}
if T <: Int
names = columnnames(t)[inds]
else
names = inds
end
cols = map(c->Tables.getcolumn(t, c), inds)
MetidaTable(metida_table_(cols...; names = names))
end

function Base.getindex(t::MetidaTable, r::Int, ::Colon)
MetidaTableRow(r, t)
#NamedTuple{keys(t.table)}(tuple(Iterators.map(c -> getindex(t, r, c), keys(t.table))...))
end


function Base.getindex(t::MetidaTable, row::Int, ind::T) where T <: Union{Symbol, Int}
Tables.getcolumn(t, ind)[row]
end
Expand Down Expand Up @@ -151,6 +169,17 @@ end
function Base.show(io::IO, table::MetidaTable)
pretty_table(io, table; tf = PrettyTables.tf_compact)
end
function Base.show(io::IO, row::MetidaTableRow)
print(io, "Row: (")
names = keys(table(getfield(row, :source)))
print(io, names[1], " = ", row[names[1]])
if length(names) > 1
for i = 2:length(names)
print(io, ", ", names[i], " = ", row[names[i]])
end
end
print(io, ")")
end

# All
################################################################################
Expand Down Expand Up @@ -180,6 +209,7 @@ Tables.rowaccess(::AbstractDataSet) = false
function Base.getindex(d::DataSet, ind::Int)
d.ds[ind]
end

@inline function getresultindex_safe(rd::T, ind::Symbol) where T <: AbstractResultData
getindormiss(rd.result, ind)
end
Expand Down Expand Up @@ -374,7 +404,14 @@ function TypedTables.Table(obj::MetidaTable)
TypedTables.Table(obj.table)
end

# DataFrames.jl interface
function DataFrames.DataFrame(obj::AbstractDataSet; kwargs...)
DataFrames.DataFrame(metida_table_(obj; kwargs...))
end

function DataFrames.DataFrame(obj::MetidaTable)
DataFrames.DataFrame(obj.table)
end
# MetidaFreq.jl
struct Proportion <: AbstractData
x::Int
Expand Down
55 changes: 43 additions & 12 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
using MetidaBase
using Test, Tables, TypedTables, CSV
using Test, Tables, TypedTables, DataFrames, CSV

@testset "MetidaBase.jl" begin
io = IOBuffer();
# Metida table names - auto
mt = MetidaBase.metida_table([1,2,3], ["a", "b", "c"])

@test Tables.istable(mt) == true
@test Tables.rowaccess(mt) == true

# Get columns
mt = mt[:, [:x1, :x2]]
# Get row
mtr = mt[1, :]

@test names(mt) == [:x1, :x2]
# Metida table names - defined
mt = MetidaBase.metida_table([1,2,3], ["a", "b", "c"], names = (:a, :b))
Expand Down Expand Up @@ -32,6 +41,7 @@ using Test, Tables, TypedTables, CSV

# MetidaTable > TypedTables
df = Table(mt)
df = DataFrame(mt)

# Enumerate
for (i,j) in enumerate(mt)
Expand All @@ -50,6 +60,13 @@ using Test, Tables, TypedTables, CSV
# CSV compat test
@test_nowarn CSV.write(io, mt)

mtd = MetidaBase.indsdict!(Dict(), mt)
@test mtd[(3, "c")] == [5, 8]

mtd = MetidaBase.indsdict!(Dict(), mt[:, 1])
@test mtd[2] == [4, 7, 10]

############################################################################
# Structures
############################################################################
struct ExampleIDStruct <: MetidaBase.AbstractSubject
Expand Down Expand Up @@ -84,19 +101,18 @@ using Test, Tables, TypedTables, CSV
end
exrsds = MetidaBase.DataSet(exrsdsv)

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


@test MetidaBase.getid(exidds[3], :a) == 2
# SORT
sort!(exidds, :a)
@test MetidaBase.getid(exidds[3], :a) == 3
MetidaBase.getid(exrsds, :, :a)

@test_nowarn sort!(exrsds, :a)



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

MetidaBase.uniqueidlist(exidds, [:a])
Expand All @@ -114,37 +130,52 @@ using Test, Tables, TypedTables, CSV
@test length(filtexrsds) == length(exidds)
@test filtexrsds[1].id[:a] == exidds[1].id[:a] == 2

############################################################################
# Table
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])

# DataFrames export
@test_nowarn DataFrame(exrsds; results = :r1, ids = [:a, :b])

############################################################################
#Iterators data
v1 = [1,2,-6,missing,NaN]
v1 = [1, 2, -6, missing, NaN, 0]

#Iterators tests
itr1 = MetidaBase.skipnanormissing(v1)
for i in itr1
@test !MetidaBase.isnanormissing(i)
end
@test collect(eachindex(itr1)) == [1,2,3]
eltype(itr1)
@test collect(keys(itr1)) == [1,2,3]
@test length(itr1) == 3
@test collect(itr1) == [1.0, 2.0, -6.0, 0.0]
@test collect(eachindex(itr1)) == [1, 2, 3, 6]
@test eltype(itr1) <: Float64
@test collect(keys(itr1)) == [1, 2, 3, 6]
@test length(itr1) == 4

itr2 = MetidaBase.skipnonpositive(v1)
for i in itr2
@test MetidaBase.ispositive(i)
end
@test collect(eachindex(itr2)) == [1,2]
@test collect(eachindex(itr2)) == [1, 2]
eltype(itr2)
@test collect(keys(itr2)) == [1,2]
@test collect(keys(itr2)) == [1, 2]
@test length(itr2) == 2

############################################################################
# OTHER
@test MetidaBase.nonunique([1,2,3,3,4,5,6,6]) == [6,3]

@test MetidaBase.sortbyvec!([1,2,3,4,5,6,7,8], [2,5,3,1,8,4,6,7]) == [2,5,3,1,8,4,6,7]

#Ststutils
############################################################################
# Ststutils
MetidaBase.sdfromcv(0.4) 0.38525317015992666
MetidaBase.varfromcv(0.4) 0.1484200051182734
MetidaBase.cvfromvar(0.4) 0.7013021443295824
Expand Down

2 comments on commit aca22f9

@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/64836

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.7.0 -m "<description of version>" aca22f92f452163fcc4c733b2337fbc8ec85d94a
git push origin v0.7.0

Please sign in to comment.