Skip to content

Commit

Permalink
Implement Base.allequal and Base.allunique for weight vectors (#894)
Browse files Browse the repository at this point in the history
* Implement `Base.allequal` and `Base.allunique` for weight vectors

* Combine definitions
  • Loading branch information
devmotion authored Sep 25, 2023
1 parent 7ca72c7 commit e794383
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "StatsBase"
uuid = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
authors = ["JuliaStats"]
version = "0.34.1"
version = "0.34.2"

[deps]
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
Expand Down
8 changes: 8 additions & 0 deletions src/weights.jl
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,14 @@ Base.:(==)(x::UnitWeights, y::UnitWeights) = (x.len == y.len)
Base.isequal(x::AbstractWeights, y::AbstractWeights) = false
Base.:(==)(x::AbstractWeights, y::AbstractWeights) = false

# https://github.com/JuliaLang/julia/pull/43354
if VERSION >= v"1.8.0-DEV.1494" # 98e60ffb11ee431e462b092b48a31a1204bd263d
Base.allequal(wv::AbstractWeights) = allequal(wv.values)
Base.allequal(::UnitWeights) = true
end
Base.allunique(wv::AbstractWeights) = allunique(wv.values)
Base.allunique(wv::UnitWeights) = length(wv) <= 1

##### Weighted sum #####

## weighted sum over vectors
Expand Down
36 changes: 36 additions & 0 deletions test/weights.jl
Original file line number Diff line number Diff line change
Expand Up @@ -574,4 +574,40 @@ end
end
end

@testset "allequal and allunique" begin
# General weights
for f in (weights, aweights, fweights, pweights)
@test allunique(f(Float64[]))
@test allunique(f([0.4]))
@test allunique(f([0.4, 0.3]))
@test !allunique(f([0.4, 0.4]))
@test allunique(f([0.4, 0.3, 0.5]))
@test !allunique(f([0.4, 0.4, 0.5]))
@test allunique(f([0.4, 0.3, 0.5, 0.35]))
@test !allunique(f([0.4, 0.3, 0.5, 0.4]))

if isdefined(Base, :allequal)
@test allequal(f(Float64[]))
@test allequal(f([0.4]))
@test allequal(f([0.4, 0.4]))
@test !allequal(f([0.4, 0.3]))
@test allequal(f([0.4, 0.4, 0.4, 0.4]))
@test !allunique(f([0.4, 0.4, 0.3, 0.4]))
end
end

# Uniform weights
@test allunique(uweights(0))
@test allunique(uweights(1))
@test !allunique(uweights(2))
@test !allunique(uweights(5))

if isdefined(Base, :allequal)
@test allequal(uweights(0))
@test allequal(uweights(1))
@test allequal(uweights(2))
@test allequal(uweights(5))
end
end

end # @testset StatsBase.Weights

4 comments on commit e794383

@devmotion
Copy link
Member Author

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/92208

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

Also, note the warning: Version 0.34.2 skips over 0.34.1
This can be safely ignored. However, if you want to fix this you can do so. Call register() again after making the fix. This will update the Pull request.

@devmotion
Copy link
Member Author

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 updated: JuliaRegistries/General/92208

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

Please sign in to comment.