Skip to content

Commit 3ab375b

Browse files
committed
replace @Assert macro with throw(ErrorException(...))
1 parent eb0d23d commit 3ab375b

7 files changed

Lines changed: 67 additions & 19 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
- More explicit return types, drop `Dict` with `Dict{String, Any}` or `Dict{String, Vector}`
44
- Add `Julia v.1.10` to GitHub actions
5-
- Reduce memory allocations in `lts()` and `lms()`
6-
- Reduce memory allocations in `hadi92()` and `hadi94()`
7-
- Reduce memory allocations in `hs93()`
8-
- Reduce memory allocations in `robcov()`
5+
- Initial attempt to reduce memory allocations in `lts()`, `lms()`, `hadi92()`, `hadi94()`, `hs93()`, `robcov()`
6+
- Replace `@assert` macro with `throw(ErrorException())` in whole code
97

108
# v0.11.3
119

src/asm2000.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ function asm2000(X::AbstractMatrix{Float64}, y::AbstractVector{Float64})::Dict
7878
covmatrix = cov(pairs[hsubset, :])
7979
mahdist = mahalanobisSquaredBetweenPairs(pairs, covmatrix = covmatrix)
8080

81-
@assert !isnothing(mahdist)
81+
if isnothing(mahdist)
82+
throw(ErrorException("Mahalanobis distances are not calculated"))
83+
end
8284

8385
outlierset = Array{Int,1}(undef, 0)
8486

src/bacon.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ function initial_basic_subset_multivariate_data(
3131
n, _ = size(X)
3232
if method == "mahalanobis"
3333
msm = mahalanobisSquaredMatrix(X)
34-
@assert !isnothing(msm)
34+
35+
if isnothing(msm)
36+
throw(ErrorException("Mahalanobis distances are not calculated"))
37+
end
38+
3539
distances = sqrt.(diag(msm))
3640
elseif method == "median"
3741
median_vector = applyColumns(median, X)
@@ -113,7 +117,9 @@ function bacon_multivariate_outlier_detection(
113117

114118
msm = mahalanobisSquaredMatrix(X, meanvector = mean_basic_subset, covmatrix = cov_basic_subset)
115119

116-
@assert !isnothing(msm)
120+
if isnothing(msm)
121+
throw(ErrorException("Mahalanobis distances are not calculated"))
122+
end
117123

118124
distances = sqrt.(diag(msm))
119125
c_hr = (h - r) / (h + r)

src/bch.jl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ function bch(
105105
A = ((X .- coordmeds')' * (X .- coordmeds')) / (n - 1.0)
106106

107107
msm = mahalanobisSquaredMatrix(X, meanvector = coordmeds, covmatrix = A)
108-
@assert !isnothing(msm)
108+
109+
if isnothing(msm)
110+
throw(ErrorException("Mahalanobis distances are not calculated"))
111+
end
109112

110113
dsquared = diag(msm)
111114
d = sqrt.(dsquared)
@@ -116,7 +119,11 @@ function bch(
116119
covmatofh = cov(X[bestindicesofd, :])
117120

118121
msm2 = mahalanobisSquaredMatrix(X, meanvector = colmeansofh, covmatrix = covmatofh)
119-
@assert !isnothing(msm2)
122+
123+
124+
if isnothing(msm2)
125+
throw(ErrorException("Mahalanobis distances are not calculated"))
126+
end
120127

121128
newdsquared = diag(msm2)
122129

@@ -150,7 +157,11 @@ function bch(
150157
covmatofbasicsubset = cov(X[basicsubsetindices, :])
151158

152159
msm4 = mahalanobisSquaredMatrix(X, meanvector = colmeanofbasicsubset, covmatrix = covmatofbasicsubset)
153-
@assert !isnothing(msm4)
160+
161+
if isnothing(msm4)
162+
throw(ErrorException("Mahalanobis distances are not calculated"))
163+
end
164+
154165
newdsquared = diag(msm4)
155166
newd = sqrt.(newdsquared)
156167
sortednewdsquared = sort(newdsquared)

src/dataimage.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,24 @@ function dataimage(
4949
)::Matrix{RGBX{Float64}}
5050
d = nothing
5151
if distance == :mahalanobis
52+
5253
d = mahalanobisSquaredBetweenPairs(dataMatrix)
53-
@assert !isnothing(d)
54+
55+
if isnothing(d)
56+
throw(ErrorException("Mahalanobis distances are not calculated"))
57+
end
58+
5459
elseif distance == :euclidean
5560
d = euclideanDistances(dataMatrix)
5661
else
5762
@error "Distance function unknown: " distance
5863
@error "Using mahalanobis instead"
5964
d = mahalanobisSquaredBetweenPairs(dataMatrix)
60-
@assert !isnothing(d)
65+
66+
if isnothing(d)
67+
throw(ErrorException("Mahalanobis distances are not calculated"))
68+
end
69+
6170
end
6271
colours = 1.0 .- d / maximum(d)
6372
n, _ = size(d)

src/hadi1992.jl

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ function hadi1992(multivariateData::AbstractMatrix{Float64}; alpha = 0.05)
8787
Sm = (1.0 / (n - 1.0)) * (multivariateData .- meds')' * (multivariateData .- meds')
8888

8989
msm1 = mahalanobisSquaredMatrix(multivariateData, meanvector = meds, covmatrix = Sm)
90-
@assert !isnothing(msm1)
90+
91+
if isnothing(msm1)
92+
throw(ErrorException("Mahalanobis distances are not calculated"))
93+
end
9194

9295
mah0 = diag(msm1)
9396

@@ -99,7 +102,10 @@ function hadi1992(multivariateData::AbstractMatrix{Float64}; alpha = 0.05)
99102
Sv = (1.0 / (h - 1.0)) * (starting_data .- Cv')' * (starting_data .- Cv')
100103

101104
msm2 = mahalanobisSquaredMatrix(multivariateData, meanvector = Cv, covmatrix = Sv)
102-
@assert !isnothing(msm2)
105+
106+
if isnothing(msm2)
107+
throw(ErrorException("Mahalanobis distances are not calculated"))
108+
end
103109

104110
mah1 = diag(msm2)
105111
ordering_indices_mah1 = sortperm(mah1)
@@ -130,15 +136,22 @@ function hadi1992(multivariateData::AbstractMatrix{Float64}; alpha = 0.05)
130136
newSb .= hadi1992_handle_singularity(cfactor * Sb)
131137

132138
msm3 .= mahalanobisSquaredMatrix(multivariateData, meanvector = Cb, covmatrix = newSb,)
133-
@assert !isnothing(msm3)
139+
140+
if isnothing(msm3)
141+
throw(ErrorException("Mahalanobis distances are not calculated"))
142+
end
134143

135144
mah1 .= diag(msm3)
136145

137146
ordering_indices_mah1 = sortperm(mah1)
138147
basic_subset_indices = ordering_indices_mah1[1:r]
139148
else
140149
msm4 .= mahalanobisSquaredMatrix(multivariateData, meanvector = Cb, covmatrix = (cfactor * Sb))
141-
@assert !isnothing(msm4)
150+
151+
if isnothing(msm4)
152+
throw(ErrorException("Mahalanobis distances are not calculated"))
153+
end
154+
142155
mah1 .= diag(msm4)
143156
ordering_indices_mah1 = sortperm(mah1)
144157
basic_subset_indices = ordering_indices_mah1[1:r]

src/hadi1994.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ function hadi1994(multivariateData::AbstractMatrix{Float64}; alpha = 0.05)
6060
Sm = (1.0 / (n - 1.0)) * (multivariateData .- meds')' * (multivariateData .- meds')
6161

6262
msm1 = mahalanobisSquaredMatrix(multivariateData, meanvector = meds, covmatrix = Sm)
63-
@assert !isnothing(msm1)
63+
64+
if isnothing(msm1)
65+
throw(ErrorException("Mahalanobis distances are not calculated"))
66+
end
6467

6568
mah0 = diag(msm1)
6669

@@ -71,7 +74,11 @@ function hadi1994(multivariateData::AbstractMatrix{Float64}; alpha = 0.05)
7174
Cv = coordinatwisemedians(starting_data)
7275
Sv = (1.0 / (h - 1.0)) * (starting_data .- Cv')' * (starting_data .- Cv')
7376
msm2 = mahalanobisSquaredMatrix(multivariateData, meanvector = Cv, covmatrix = Sv)
74-
@assert !isnothing(msm2)
77+
78+
if isnothing(msm2)
79+
throw(ErrorException("Mahalanobis distances are not calculated"))
80+
end
81+
7582
mah1 = diag(msm2)
7683
ordering_indices_mah1 = sortperm(mah1)
7784

@@ -116,7 +123,9 @@ function hadi1994(multivariateData::AbstractMatrix{Float64}; alpha = 0.05)
116123
covmatrix = (cfactor * Sb),
117124
)
118125

119-
@assert !isnothing(msm3)
126+
if isnothing(msm3)
127+
throw(ErrorException("Mahalanobis distances are not calculated"))
128+
end
120129

121130
mah1 = diag(msm3)
122131

0 commit comments

Comments
 (0)