Skip to content

Commit

Permalink
Julia formatter (#38)
Browse files Browse the repository at this point in the history
* juliaformater

* oops, no need to do BioJulia CI prep stuff
  • Loading branch information
phaverty authored Aug 29, 2020
1 parent 4787f8e commit f142a5e
Show file tree
Hide file tree
Showing 32 changed files with 1,383 additions and 1,229 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
indent = 4
margin = 100
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ matrix:

script:
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
- julia ci_prep.jl
- julia --color=yes -e 'using Pkg; Pkg.build()'
- julia --check-bounds=yes --color=yes -e 'using Pkg; Pkg.add("Test"); Pkg.test("RLEVectors"; coverage=true)'

Expand Down
46 changes: 46 additions & 0 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,35 @@
[[Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[CSTParser]]
deps = ["Tokenize"]
git-tree-sha1 = "60e9121d9ea044c30a04397e59b00c5d9eb826ee"
uuid = "00ebfdb7-1f24-5e51-bd34-a7502290713f"
version = "2.5.0"

[[CategoricalArrays]]
deps = ["Compat", "DataAPI", "Future", "JSON", "Missings", "Printf", "Reexport", "Statistics", "Unicode"]
git-tree-sha1 = "23d7324164c89638c18f6d7f90d972fa9c4fa9fb"
uuid = "324d7699-5711-5eae-9e2f-1d82baa6b597"
version = "0.7.7"

[[CommonMark]]
deps = ["Crayons", "JSON", "URIParser"]
git-tree-sha1 = "df663743a8812677a74c9f3505a29002e017e59b"
uuid = "a80b9123-70ca-4bc0-993e-6e3bcb318db6"
version = "0.6.2"

[[Compat]]
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]
git-tree-sha1 = "0198d18b28c093bef39872a22f1a897218a925f5"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "3.8.0"

[[Crayons]]
git-tree-sha1 = "c437a9c2114c7ba19322712e58942b383ffbd6c0"
uuid = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
version = "4.0.3"

[[DataAPI]]
git-tree-sha1 = "674b67f344687a88310213ddfa8a2b3c76cc4252"
uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
Expand Down Expand Up @@ -49,6 +66,18 @@ uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab"
deps = ["Random", "Serialization", "Sockets"]
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"

[[DocStringExtensions]]
deps = ["LibGit2", "Markdown", "Pkg", "Test"]
git-tree-sha1 = "50ddf44c53698f5e784bbebb3f4b21c5807401b1"
uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
version = "0.8.3"

[[Documenter]]
deps = ["Base64", "Dates", "DocStringExtensions", "InteractiveUtils", "JSON", "LibGit2", "Logging", "Markdown", "REPL", "Test", "Unicode"]
git-tree-sha1 = "fb1ff838470573adc15c71ba79f8d31328f035da"
uuid = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
version = "0.25.2"

[[Future]]
deps = ["Random"]
uuid = "9fa8497b-333b-5362-9e8d-4d0656e87820"
Expand All @@ -74,6 +103,12 @@ git-tree-sha1 = "b34d7cef7b337321e97d22242c3c2b91f476748e"
uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
version = "0.21.0"

[[JuliaFormatter]]
deps = ["CSTParser", "CommonMark", "DataStructures", "Documenter", "Pkg", "Test", "Tokenize"]
git-tree-sha1 = "0db70dbc17dfd8c655cd385e0cb134f6fe1e06b1"
uuid = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
version = "0.7.3"

[[LibGit2]]
deps = ["Printf"]
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"
Expand Down Expand Up @@ -196,6 +231,17 @@ version = "1.0.3"
deps = ["Distributed", "InteractiveUtils", "Logging", "Random"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[[Tokenize]]
git-tree-sha1 = "73c00ad506d88a7e8e4f90f48a70943101728227"
uuid = "0796e94c-ce3b-5d07-9a54-7f471281c624"
version = "0.5.8"

[[URIParser]]
deps = ["Unicode"]
git-tree-sha1 = "53a9f49546b8d2dd2e688d216421d050c9a31d0d"
uuid = "30578b45-9adc-5946-b283-645ec420af67"
version = "0.4.1"

[[UUIDs]]
deps = ["Random", "SHA"]
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
Expand Down
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.9.4"

[deps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
Missings = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28"
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Expand Down
61 changes: 32 additions & 29 deletions src/RLEDataFrame-type.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ z = RLEDataFrame( a=RLEVector([5,2,2]), b=RLEVector([4,4,4])
mutable struct RLEDataFrame{T1,T2<:Integer}
columns::Vector{RLEVector{T1,T2}}
colindex::NamedTuple
function RLEDataFrame{T1,T2}(columns::Vector{RLEVector{T1,T2}}, colnames::Vector{Symbol}) where {T1,T2}
function RLEDataFrame{T1,T2}(
columns::Vector{RLEVector{T1,T2}},
colnames::Vector{Symbol},
) where {T1,T2}
ncol = length(columns)
nval = length(colnames)
if ncol != nval
Expand All @@ -29,7 +32,7 @@ mutable struct RLEDataFrame{T1,T2<:Integer}
throw(ArgumentError("All incoming columns must be of equal length."))
end
end
c = Tuple( Symbol(x) for x in colnames )
c = Tuple(Symbol(x) for x in colnames)
colindex = NamedTuple{c}(1:ncol)
new(columns, colindex)
end
Expand All @@ -49,9 +52,9 @@ function Base.show(io::IO, x::RLEDataFrame)
t = typeof(x)
show(io, t)
println()
for (c,v) in zip(names(x),columns(x))
println(io,"Column: $c")
println(io,v)
for (c, v) in zip(names(x), columns(x))
println(io, "Column: $c")
println(io, v)
end
end

Expand All @@ -61,21 +64,21 @@ function RLEDataFrame(cols, colnames)
end

function RLEDataFrame(; kwargs...)
cnames = [k for (k,v) in kwargs]
cvalues = [v for (k,v) in kwargs]
RLEDataFrame(cvalues,cnames)
cnames = [k for (k, v) in kwargs]
cvalues = [v for (k, v) in kwargs]
RLEDataFrame(cvalues, cnames)
end

Base.copy(x::RLEDataFrame) = RLEDataFrame(copy(x.columns), names(x))

### Get/set
## Just columns
Base.getindex(x::RLEDataFrame,j::Colon) = copy(x)
Base.getindex(x::RLEDataFrame,j::ColumnIndex) = columns(x)[index(x)[j]]
function Base.getindex(x::RLEDataFrame,j::AbstractArray)
Base.getindex(x::RLEDataFrame, j::Colon) = copy(x)
Base.getindex(x::RLEDataFrame, j::ColumnIndex) = columns(x)[index(x)[j]]
function Base.getindex(x::RLEDataFrame, j::AbstractArray)
ind = index(x)
inds = [ ind[x] for x in j ]
RLEDataFrame( columns(x)[inds], names(x)[inds] )
inds = [ind[x] for x in j]
RLEDataFrame(columns(x)[inds], names(x)[inds])
end

function Base.setindex!(x::RLEDataFrame, value::AbstractVector, j::Integer)
Expand All @@ -97,26 +100,26 @@ function Base.setindex!(x::RLEDataFrame, value::AbstractVector, j::Symbol)
if j in names(x)
columns(x)[index(x)[j]] = value
else
x.colindex = merge(index(x), NamedTuple{(j,)}( Tuple(length(x) + 1) ))
x.columns = push!(x.columns,value)
x.colindex = merge(index(x), NamedTuple{(j,)}(Tuple(length(x) + 1)))
x.columns = push!(x.columns, value)
end
x
end

## with rows
function Base.getindex(x::RLEDataFrame, i, j)
ind = index(x)
j_inds = [ ind[x] for x in j ]
cols = [ x.columns[j_ind][i] for j_ind in j_inds ]
RLEDataFrame( cols, names(x)[j_inds] )
j_inds = [ind[x] for x in j]
cols = [x.columns[j_ind][i] for j_ind in j_inds]
RLEDataFrame(cols, names(x)[j_inds])
end
Base.getindex(x::RLEDataFrame, i::Integer, j::ColumnIndex) = x[j][i]
Base.getindex(x::RLEDataFrame, i::Integer, j) = x[ [i], j ]
Base.getindex(x::RLEDataFrame, i::Integer, j) = x[[i], j]
Base.getindex(x::RLEDataFrame, i, j::ColumnIndex) = x[j][i]

function Base.setindex!(x::RLEDataFrame, value, i, j)
ind = index(x)
j_inds = [ ind[x] for x in j ]
j_inds = [ind[x] for x in j]
for j_ind in j_inds
x.columns[j_ind][i] = value
end
Expand All @@ -128,19 +131,19 @@ end


## Conversion
Base.convert(Matrix, x::RLEDataFrame) = hcat(map(collect,x.columns)...)
Base.convert(Matrix, x::RLEDataFrame) = hcat(map(collect, x.columns)...)

### Familiar operations over rows or columns from R

# Probably these are all a job for mapslice or slicedim. I need to RTM.
rowmap(x::Matrix,f::Function) = [ f( @view x[i,:] ) for i in 1:size(x)[1] ]
colmap(x::Matrix,f::Function) = [ f( @view x[:,j] ) for j in 1:size(x)[2] ]
rowMeans(x) = rowmap(x,mean)
rowMedians(x) = rowmap(x,median)
rowSums(x) = rowmap(x,sum)
colMeans(x) = colmap(x,mean)
colMedians(x) = colmap(x,median)
colSums(x) = colmap(x,sum)
rowmap(x::Matrix, f::Function) = [f(@view x[i, :]) for i = 1:size(x)[1]]
colmap(x::Matrix, f::Function) = [f(@view x[:, j]) for j = 1:size(x)[2]]
rowMeans(x) = rowmap(x, mean)
rowMedians(x) = rowmap(x, median)
rowSums(x) = rowmap(x, sum)
colMeans(x) = colmap(x, mean)
colMedians(x) = colmap(x, median)
colSums(x) = colmap(x, sum)

#rowMeans(x::RLEDataFrame) = rowSum(x) ./ ncol(x)
colSums(x::RLEDataFrame) = map(sum, columns(x))
Expand Down
63 changes: 32 additions & 31 deletions src/RLEVector-type.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,41 +47,41 @@ specific to the type.
* `endtype(x)` Returns the element type of the run ends
"""
struct RLEVector{T1,T2 <: Integer} <: AbstractArray{T1,1}
runvalues::Vector{T1}
runends::Vector{T2}
RLEVector{T1,T2}(runvalues,runends) where {T1,T2<:Integer} = new(runvalues,runends)
struct RLEVector{T1,T2<:Integer} <: AbstractArray{T1,1}
runvalues::Vector{T1}
runends::Vector{T2}
RLEVector{T1,T2}(runvalues, runends) where {T1,T2<:Integer} = new(runvalues, runends)
end

function RLEVector(runvalues::Vector{T1}, runends::Vector{T2}) where {T1, T2 <: Integer}
runvalues, runends = ree!(runvalues,runends)
function RLEVector(runvalues::Vector{T1}, runends::Vector{T2}) where {T1,T2<:Integer}
runvalues, runends = ree!(runvalues, runends)
RLEVector{T1,T2}(runvalues, runends)
end

function RLEVector(runvalues::BitVector, runends::Vector{T2}) where T2 <: Integer
runvalues, runends = ree!(runvalues,runends)
function RLEVector(runvalues::BitVector, runends::Vector{T2}) where {T2<:Integer}
runvalues, runends = ree!(runvalues, runends)
RLEVector{Bool,T2}(runvalues, runends)
end

function RLEVector(x::Vector)
runvalues, runends = ree(x)
RLEVector(runvalues, runends)
runvalues, runends = ree(x)
RLEVector(runvalues, runends)
end

function RLEVector(x::UnitRange)
RLEVector{eltype(x),Int64}(collect(x),collect(1:length(x)))
RLEVector{eltype(x),Int64}(collect(x), collect(1:length(x)))
end

function RLEVector(x::BitVector)
runvalues, runends = ree(x)
RLEVector(runvalues, runends)
runvalues, runends = ree(x)
RLEVector(runvalues, runends)
end

function RLEVector(x)
RLEVector([x])
end

function RLEVector(runvalues::T1, runends::T2) where {T1,T2 <: Integer}
function RLEVector(runvalues::T1, runends::T2) where {T1,T2<:Integer}
RLEVector{T1,T2}([runvalues], [runends])
end

Expand All @@ -91,53 +91,54 @@ const FloatRle = RLEVector{Float64,UInt32}
const IntegerRle = RLEVector{Int64,UInt32}
const BoolRle = RLEVector{Bool,UInt32}
const StringRle = RLEVector{String,UInt32}
const RLEVectorList{T1,T2} = Vector{ RLEVector{T1,T2} }
@doc (@doc RLEVector) FloatRle, IntegerRle, BoolRle, StringRle, RLEVectorList
const RLEVectorList{T1,T2} = Vector{RLEVector{T1,T2}}
@doc (@doc RLEVector) FloatRle, IntegerRle, BoolRle, StringRle, RLEVectorList

# copy
Base.copy(x::RLEVector) = RLEVector(copy(x.runvalues), copy(x.runends))

# similar
function Base.similar(a::RLEVector{T1,T2}, ::Type{T}, dims::Tuple{Int}) where {T1,T2,T}
RLEVector{T,T2}(Vector{T}(undef,1), [dims[1]])
RLEVector{T,T2}(Vector{T}(undef, 1), [dims[1]])
end

# show
function Base.show(io::IO, ::MIME"text/plain", x::RLEVector)
t = typeof(x)::DataType
show(io, t)
n = nrun(x)
write(io,"\n Run values: ")
Base.show_vector(io,x.runvalues,"[", "]")
write(io,"\n Run ends: ")
Base.show_vector(io,x.runends,"[", "]")
write(io, "\n Run values: ")
Base.show_vector(io, x.runvalues, "[", "]")
write(io, "\n Run ends: ")
Base.show_vector(io, x.runends, "[", "]")
end

function Base.show(io::IO, x::RLEVector)
write(io,"Values: ")
Base.show_vector(io,values(x),"[", "]")
write(io," Ends: ")
Base.show_vector(io,ends(x),"[", "]")
write(io, "Values: ")
Base.show_vector(io, values(x), "[", "]")
write(io, " Ends: ")
Base.show_vector(io, ends(x), "[", "]")
end

function ree!(x::RLEVector)
ree!(x.runvalues,x.runends)
ree!(x.runvalues, x.runends)
end

# conversions
convert(::Type{Vector}, x::RLEVector) = collect(x)
convert(::Type{Set}, x::RLEVector) = Set(values(x))
convert(::Type{RLEVector}, x::Vector) = RLEVector(x)
promote_rule(::Type{Set}, ::Type{RLEVector}) = Set
convert(::Type{RLEVector{T1,T2}}, x::RLEVector) where {T1,T2} = RLEVector(convert(Vector{T1},values(x)),convert(Vector{T2},ends(x)))
convert(::Type{RLEVector{T1,T2}}, x::RLEVector) where {T1,T2} =
RLEVector(convert(Vector{T1}, values(x)), convert(Vector{T2}, ends(x)))

# the basics
function collect(x::RLEVector)
inverse_ree(x.runvalues,x.runends)
inverse_ree(x.runvalues, x.runends)
end

function isequal(x::RLEVector, y::RLEVector)
isequal(x.runends,y.runends) && isequal(x.runvalues, y.runvalues)
isequal(x.runends, y.runends) && isequal(x.runvalues, y.runvalues)
end

Base.hash(a::RLEVector) = hash(a.runvalues, hash(a.runlengths, hash(:RLEVector)))
Expand All @@ -153,8 +154,8 @@ function growat!(x::AbstractVector, i, insert_length)
len = length(x)
resize!(x, len + insert_length)
ind = len
@inbounds for ind in len:-1:i
x[ind + insert_length] = x[ind]
@inbounds for ind = len:-1:i
x[ind+insert_length] = x[ind]
end
x
end
Expand Down
Loading

0 comments on commit f142a5e

Please sign in to comment.