Skip to content

Commit

Permalink
Code improvements in peers
Browse files Browse the repository at this point in the history
  • Loading branch information
javierbarbero committed Jul 9, 2020
1 parent bdf1d8d commit 3c5ee5a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
44 changes: 21 additions & 23 deletions src/peers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
AbstractDEAPeersDMU
An abstract type representing the DEA Peers of a DMU.
"""
abstract type AbstractDEAPeersDMU <: AbstractArray{Tuple{Tuple{Int64,String},Float64}, 1} end
abstract type AbstractDEAPeersDMU end

"""
DEAPeersDMU
Expand All @@ -17,37 +17,31 @@ struct DEAPeersDMU <: AbstractDEAPeersDMU
dmunamesref::Vector{String}
end

Base.size(P::DEAPeersDMU) = (length(P.J),)

Base.IndexStyle(::Type{<:DEAPeersDMU}) = IndexLinear()
Base.size(P::DEAPeersDMU) = (length(P.J),)

Base.getindex(P::DEAPeersDMU, i::Int) = return (P.J[i], P.dmunamesref[i]), P.V[i]
Base.length(P::DEAPeersDMU) = length(P.J)

function Base.show(io::IO, x::DEAPeersDMU)
compact = get(io, :compact, false)
Base.eltype(P::DEAPeersDMU) = Tuple{Tuple{Int64,String},Float64}

J = x.J
V = x.V
dmuname = x.dmuname
dmunamesref = x.dmunamesref
Base.firstindex(P::DEAPeersDMU) = 1

print(io, dmuname, ": ")
Base.lastindex(P::DEAPeersDMU) = length(P)

for p in x
print(io, p[1][2], " ( ", p[2], " ) ")
end
Base.getindex(P::DEAPeersDMU, i::Int) = return (P.J[i], P.dmunamesref[i]), P.V[i]

end
Base.iterate(P::DEAPeersDMU, state=1) = state > length(P) ? nothing : (P[state], state + 1)

function Base.show(io::IO, ::MIME"text/plain", x::DEAPeersDMU)
function Base.show(io::IO, x::DEAPeersDMU)
compact = get(io, :compact, false)

J = x.J
V = x.V
dmuname = x.dmuname
dmunamesref = x.dmunamesref

print(io, "Peers of ", dmuname, ": ")
print(io, dmuname, ": ")

for p in x
print(io, p[1][2], " ( ", p[2], " ) ")
end
Expand All @@ -59,7 +53,7 @@ end
AbstractDEAPeers
An abstract type representing a DEA Peers.
"""
abstract type AbstractDEAPeers <: AbstractArray{DEAPeersDMU, 1}end
abstract type AbstractDEAPeers end

"""
DEAPeers
Expand Down Expand Up @@ -124,7 +118,13 @@ end

Base.size(P::DEAPeers) = (P.n,)

Base.IndexStyle(::Type{<:DEAPeers}) = IndexLinear()
Base.length(P::DEAPeers) = P.n

Base.eltype(P::DEAPeers) = DEAPeersDMU

Base.firstindex(P::DEAPeers) = 1

Base.lastindex(P::DEAPeers) = length(P)

function Base.getindex(P::DEAPeers, i::Int)::DEAPeersDMU

Expand All @@ -138,6 +138,8 @@ function Base.getindex(P::DEAPeers, i::Int)::DEAPeersDMU

end

Base.iterate(P::DEAPeers, state = 1) = state > length(P) ? nothing : (P[state], state+1)

function Base.show(io::IO, x::DEAPeers)
compact = get(io, :compact, false)

Expand All @@ -151,10 +153,6 @@ function Base.show(io::IO, x::DEAPeers)

end

function Base.show(io::IO, ::MIME"text/plain", x::DEAPeers)
show(io, x)
end

"""
peers(model::AbstractDEAModel)
Return peers of a DEA model.
Expand Down
18 changes: 16 additions & 2 deletions test/peers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

@test typeof(P) == DEAPeers
@test eltype(P) == DEAPeersDMU
@test IndexStyle(P) == IndexLinear()

@test size(P) == (11,)
@test ispeer(P, 1, 1) == true
Expand All @@ -35,6 +34,14 @@

Pn = peers(dea(X, Y, orient = :Input, rts = :CRS, names = firms))

@test typeof(P) == DEAPeers
@test eltype(P) == DEAPeersDMU

@test size(P) == (11,)
@test length(P) == 11
@test firstindex(P) == 1
@test lastindex(P) == 11

@test ispeer(Pn, "A", "A") == true
@test ispeer(Pn, "A", "B") == false
@test ispeer(Pn, "B", "A") == false
Expand All @@ -45,9 +52,12 @@

@test typeof(P2) == DEAPeersDMU
@test eltype(P2) == Tuple{Tuple{Int64,String},Float64}
@test IndexStyle(P) == IndexLinear()

@test size(P2) == (2,)
@test length(P2) == 2
@test firstindex(P2) == 1
@test lastindex(P2) == 2

@test ispeer(P2, 1) == false
@test ispeer(P2, 2) == false
@test ispeer(P2, 3) == false
Expand All @@ -63,6 +73,10 @@
@test ispeer(P2n, "D") == true
@test ispeer(P2n, "G") == true

# Test elements of DEAPeersDMU
@test P2n[1] == ((4, "D"), 0.424978317432784)
@test P2n[2] == ((7, "G"), 0.10928013876843023)

# Test conversions
@test typeof(convert(Matrix, P)) == Array{Float64,2}
#@test typeof(convert(SparseMatrixCSC, P)) == SparseMatrixCSC{Float64,Int64}
Expand Down

0 comments on commit 3c5ee5a

Please sign in to comment.