Skip to content

Commit

Permalink
find methods, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
PharmCat committed Jan 8, 2025
1 parent e0fdee8 commit b281761
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 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.12.0"
version = "0.12.1"

[deps]
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
Expand Down
4 changes: 3 additions & 1 deletion src/MetidaBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ module MetidaBase
import Tables: istable, columnaccess, columns, getcolumn, columnnames, schema, rowaccess, rows
import CPUSummary: num_cores

import Base: getindex, length, ht_keyindex, show, pushfirst!, iterate, size, findfirst, push!, append!
import Base: getindex, length, ht_keyindex, show, pushfirst!, iterate, size, findfirst, findlast, findall, findnext, findprev, push!, append!

export DataSet, MetidaTable, getdata, getid

include("abstracttype.jl")
include("m_tables.jl")
Expand Down
56 changes: 53 additions & 3 deletions src/dataset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,30 @@ end
# filter!
################################################################################
function Base.filter(f::Function, d::DataSet)
ds = getdata(d)
inds = findall(f, ds)
DataSet(ds[inds])
DataSet(filter(f, getdata(d)))
end
function Base.filter!(f::Function, d::DataSet)
filter!(f, getdata(d))
d
end

function Base.filter(f::Dict{:Symbol, Function}, d::DataSet)
k = keys(f)
a = filter(x -> f[first(k)](getid(x, first(k))), getdata(d))
if length(k) > 1
for kn = 2:length(k)
filter!(x -> f[k[kn]](getid(x, k[kn])), a)
end
end
DataSet(a)
end
function Base.filter!(f::Dict{:Symbol, Function}, d::DataSet)
for k in keys(f)
filter!(x -> f[k](getid(x, k)), getdata(d))
end
d
end

################################################################################
# Base.findfirst
################################################################################
Expand All @@ -123,6 +138,41 @@ function Base.findfirst(d::DataSet{<: AbstractIdData}, sort::Dict)
findfirst(x-> sort getid(x), getdata(d))
end

################################################################################
# Base.findlast
################################################################################

function Base.findlast(d::DataSet{<: AbstractIdData}, sort::Dict)
findlast(x-> sort getid(x), getdata(d))
end

################################################################################
# Base.findnext
################################################################################

function Base.findnext(d::DataSet{<: AbstractIdData}, sort::Dict, i::Int)
findnext(x-> sort getid(x), getdata(d), i)
end


################################################################################
# Base.findprev
################################################################################

function Base.findprev(d::DataSet{<: AbstractIdData}, sort::Dict, i::Int)
findprev(x-> sort getid(x), getdata(d), i)
end

################################################################################
# Base.findall
################################################################################

function Base.findall(d::DataSet{<: AbstractIdData}, sort::Dict)
findall(x-> sort getid(x), getdata(d))
end



################################################################################
# SELF
################################################################################
Expand Down
2 changes: 1 addition & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ end
"""
cvfromsd(σ::Real)::AbstractFloat
CV from variance.
CV from LnSD.
"""
function cvfromsd(σ)
return sqrt(exp^2) - 1)
Expand Down
5 changes: 4 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ using Test, Tables, TypedTables, DataFrames, CSV
########################################################################

@test MetidaBase.findfirst(exidds, Dict(:a => 1, :b => 1)) == 1

@test MetidaBase.findlast(exidds, Dict(:a => 1, :b => 1)) == 1
@test MetidaBase.findall(exidds, Dict(:a => 1, :b => 1)) == [1]
@test MetidaBase.findnext(exidds, Dict(:a => 2, :b => 3), 1) ==2
@test MetidaBase.findprev(exidds, Dict(:a => 2, :b => 3), 3) == 2
#######################################################################


Expand Down

0 comments on commit b281761

Please sign in to comment.