-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more functionalities for data convenience
- Loading branch information
Showing
10 changed files
with
185 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
using LinearAlgebra | ||
|
||
function CCA(x::AbstractMatrix, y::AbstractMatrix) | ||
export canonicalcor | ||
function canonicalcor(x::AbstractMatrix, y::AbstractMatrix) | ||
ma = inv(cov(x))*cov(x, y)*inv(cov(y))*cov(y,x) | ||
mb = inv(cov(y))*cov(y, x)*inv(cov(x))*cov(x,y) | ||
cor(x*eigvecs(ma)[5], y*eigvecs(mb)[5]) | ||
evx = eigvecs(ma) | ||
evy = eigvecs(mb) | ||
abs(cor(x*evx[:, end], y*evy[:, end])) | ||
#[-cor(x*evx, y*evy) for (evx, evy) in zip(eachcol(evx), eachcol(evy))] | ||
end | ||
|
||
using RCall |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
function parseDateTimeN(str) | ||
date, mmn = split(str, '.') | ||
date1, time1 = split(date,'T') | ||
|
||
time2 = parse.(Int64, split(time1, ':')) | ||
|
||
mmn1 = mmn * reduce(*, ["0" for i in 1:(9-length(mmn))]) | ||
|
||
rd = reverse(digits(parse(Int, mmn1), pad = 9)) | ||
|
||
t = reduce(vcat, [ | ||
time2, | ||
parse(Int, reduce(*, string.(rd[1:3]))), | ||
parse(Int, reduce(*, string.(rd[4:6]))), | ||
parse(Int, reduce(*, string.(rd[7:9])))] | ||
) | ||
|
||
DateTimeN(Date(date1), Time(t...)) | ||
end | ||
|
||
|
||
import Base:show | ||
|
||
show(io::IO, dd::DateTimeN) = begin | ||
print(io, dd.d) | ||
print(io, dd.t) | ||
end | ||
|
||
DateTimeN(str::String) = parseDateTimeN(str) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
################################################################################ | ||
# convenient function for CategoricalArrays | ||
################################################################################ | ||
import SortingLab:sorttwo! | ||
using SortingLab | ||
import StatsBase: rle | ||
using CategoricalArrays | ||
|
||
SortingLab.sorttwo!(x::CategoricalVector, y) = begin | ||
SortingLab.sorttwo!(x.refs, y) | ||
x, y | ||
end | ||
|
||
pooltype(::CategoricalPool{T,S}) where {T, S} = T,S | ||
|
||
rle(x::CategoricalVector) = begin | ||
refrle = rle(x.refs) | ||
T,S = pooltype(x.pool) | ||
(CategoricalArray{T, 1}(S.(refrle[1]), x.pool), refrle[2]) | ||
end | ||
|
||
""" | ||
StringVector(v::CategoricalVector{String}) | ||
Convert `v::CategoricalVector` efficiently to WeakRefStrings.StringVector | ||
## Example | ||
```julia | ||
using DataFrames | ||
a = categorical(["a","c", "a"]) | ||
a.refs | ||
a.pool.index | ||
# efficiently convert | ||
sa = StringVector(a) | ||
sa.buffer | ||
sa.lengths | ||
sa.offsets | ||
``` | ||
""" | ||
StringVector(v::CategoricalVector{S}) where S<:AbstractString = begin | ||
sa = StringVector(v.pool.index) | ||
StringVector{S}(sa.buffer, sa.offsets[v.refs], sa.lengths[v.refs]) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import DataFrames: AbstractDataFrame | ||
|
||
using DataFrames: rename! | ||
|
||
export cleannames!, cleanname, renamedups! | ||
|
||
""" | ||
cleannames!(df::DataFrame) | ||
Uses R's `janitor::clean_names` to clean the names | ||
""" | ||
const ALLOWED_CHARS = vcat(vcat(vcat(Char.(-32+97:-32+97+25), Char.(97:97+25)), '_'), Char.(48:57)) | ||
|
||
renamedups!(n::AbstractVector{Symbol}) = begin | ||
# are the uniques? | ||
d = Dict{Symbol, Bool}() | ||
for (i, n1) in enumerate(n) | ||
if haskey(d, n1) | ||
n[i] = Symbol(string(n[i])*"_1") | ||
d[n[i]] = true | ||
else | ||
d[n1] = true | ||
end | ||
end | ||
n | ||
end | ||
|
||
cleanname(s) = begin | ||
ss = string(s) | ||
res = join([c in ALLOWED_CHARS ? c : '_' for c in ss]) | ||
|
||
if res[1] in vcat(Char.(48:57)) | ||
res = "x" * res | ||
end | ||
Symbol(res) | ||
end | ||
|
||
function cleannames!(df::AbstractDataFrame) | ||
n = names(df) | ||
cn = cleanname.(n) | ||
cn = renamedups!(cn) | ||
|
||
for p in Pair.(n, cn) | ||
rename!(df, p) | ||
end | ||
df | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using RCall | ||
|
||
@testset "DataConvenience.jl" begin | ||
for i in 1:100 | ||
# Write your own tests here. | ||
x = rand(100, 5) | ||
y = rand(100, 5) | ||
|
||
@rput x | ||
@rput y | ||
R""" | ||
res = cancor(x,y)$cor[1] | ||
""" | ||
@rget res | ||
@test res ≈ canonicalcor(x,y) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# support for nanoseconds in dates | ||
using Dates | ||
|
||
struct DateTimeN | ||
d::Date | ||
t::Time | ||
end | ||
|
||
str = "2019-10-23T12:01:15.123456789" | ||
|
||
parseDateTimeN(str) | ||
parseDateTimeN( "2019-10-23T12:01:15.230") | ||
|
||
parseDateTimeN(str) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using DataFrames | ||
using Test | ||
|
||
@testset "clean names " begin | ||
df = DataFrame(ok = 2:3, ok2 = 2:3, ok3=2:3) | ||
rename!(df, :ok => Symbol("ok-2")) | ||
|
||
@test names(cleannames!(df)) == [:ok_2, :ok2, :ok3] | ||
|
||
@test renamedups!([:ok, :ok_1, :ok_1]) == [:ok, :ok_1, :ok_1_1] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
using DataConvenience | ||
using Test | ||
|
||
include("canonicalcor.jl") | ||
include("janitor.jl") | ||
|
||
@testset "DataConvenience.jl" begin | ||
# Write your own tests here. | ||
end |