Skip to content

Commit

Permalink
Merge pull request #16 from siddharthlal25/mode
Browse files Browse the repository at this point in the history
Added mode statistic and tests
  • Loading branch information
mileslucas committed Mar 2, 2020
2 parents c1588e4 + 13da8e4 commit 941cf14
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"

[compat]
DataFrames = "0.17,0.18,0.19,0.20"
RecipesBase = "0.4,0.5,0.6,0.7,0.8"
Reexport = "0.2"
julia = "1"
StatsBase = "0.32,0.31,0.30,0.29,0.28"

[extras]
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
Expand Down
2 changes: 2 additions & 0 deletions docs/src/background/estimators.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ All of these estimators are subtypes of [`Background.BackgroundEstimator`](@ref)

```@docs
Mean
Median
Mode
```

## API/Reference
Expand Down
5 changes: 3 additions & 2 deletions src/Background/Background.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module Background

export estimate_background,
Mean,
Median
Median,
Mode


# Abstract types
Expand All @@ -17,7 +18,7 @@ abstract type BackgroundEstimator end
"""
estimate_background(::BackgroundEstimator, data; dims=:)
Perform 2D background estimation using the given estimator.
Perform 2D background estimation using the given estimator.
The value returned will be an two arrays corresponding to the estimated background, whose dimensionality will depend on the `dims` keyword and the estimator used.
Expand Down
23 changes: 21 additions & 2 deletions src/Background/stat_estimators.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Statistics
using Statistics, StatsBase

"""
Mean <: BackgroundEstimator
Expand Down Expand Up @@ -28,7 +28,7 @@ This estimator returns the median of the input.
# Example
```jldoctest
julia> data = ones(5 ,5)
julia> data = ones(5 ,5);
julia> estimate_background(Median, data)
1.0
Expand All @@ -41,3 +41,22 @@ julia> estimate_background(Median, data, dims=1)
struct Median <: BackgroundEstimator end

estimate_background(::Median, data; dims = :) = median(data, dims = dims)

"""
Mode <: BackgroundEstimator
This estimator returns the mode of the input.
!!! note
`Mode` does not supports the dims keyword.
# Example
```jldoctest
julia> data = ones(5, 5);
julia> estimate_background(Mode, data)
1.0
```
"""
struct Mode <: BackgroundEstimator end

estimate_background(::Mode, data; dims = :) = mode(data)
6 changes: 6 additions & 0 deletions test/background/simple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ end
test_ones(E)
test_zeros(E)
end

@testset "mode" begin
x = [1,2,3,4,5,6,5,4,3,4,34,3,43,43,3,3,3,3,1]
@test estimate_background(Mode, x) == 3
@test estimate_background(Mode, x) == estimate_background(Mode(), x)
end

0 comments on commit 941cf14

Please sign in to comment.