Skip to content

Commit

Permalink
🤖 Format .jl files (#70)
Browse files Browse the repository at this point in the history
Co-authored-by: juliohm <[email protected]>
  • Loading branch information
github-actions[bot] and juliohm committed Jul 17, 2024
1 parent b4bd09c commit f2e3269
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 25 deletions.
6 changes: 4 additions & 2 deletions src/api/estimators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ available_optlib(dre::DensityRatioEstimator) = available_optlib(typeof(dre))
## functions to be implemented by new estimators ##
###################################################

_densratio(x_nu, x_de, dre::DensityRatioEstimator, optlib::Type{<:OptimizationLibrary}) = _throw_not_implemented_error("densratio", dre, optlib)
_densratio(x_nu, x_de, dre::DensityRatioEstimator, optlib::Type{<:OptimizationLibrary}) =
_throw_not_implemented_error("densratio", dre, optlib)

_densratiofunc(x_nu, x_de, dre::DensityRatioEstimator, optlib::Type{<:OptimizationLibrary}) = _throw_not_implemented_error("densratiofunc", dre, optlib)
_densratiofunc(x_nu, x_de, dre::DensityRatioEstimator, optlib::Type{<:OptimizationLibrary}) =
_throw_not_implemented_error("densratiofunc", dre, optlib)

default_optlib(dre::Type{<:DensityRatioEstimator}) = _throw_not_implemented_error("default_optlib", dre)

Expand Down
3 changes: 2 additions & 1 deletion src/api/fitters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ fit(dre::Type{<:DensityRatioEstimator}, x_nu, x_de, fitter::EstimatorFitter; opt
## functions to be implemented by new fitters ##
################################################

_fit(dre::Type{<:DensityRatioEstimator}, x_nu, x_de, fitter::EstimatorFitter, optlib::Type{<:OptimizationLibrary}) = _throw_not_fit_error(dre, fitter, optlib)
_fit(dre::Type{<:DensityRatioEstimator}, x_nu, x_de, fitter::EstimatorFitter, optlib::Type{<:OptimizationLibrary}) =
_throw_not_fit_error(dre, fitter, optlib)
22 changes: 11 additions & 11 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,29 @@ It is compatible with
"""
safe_diagm(mat, a) = a * I


###################################################
## Functions and objects for throwing errors ##
###################################################

OPTLIB_DICT = Dict(
"JuliaLib" => "Julia",
"OptimLib" => "Optim",
"ConvexLib" => "Convex",
"JuMPLib" => "JuMP"
)
OPTLIB_DICT = Dict("JuliaLib" => "Julia", "OptimLib" => "Optim", "ConvexLib" => "Convex", "JuMPLib" => "JuMP")

function _throw_opt_error(dre::DensityRatioEstimator, optlib::Type{<:OptimizationLibrary})
dre_name = nameof(typeof(dre))
lib_name = OPTLIB_DICT[string(optlib)]
optlib_options = join(available_optlib(dre), ", ")
error("Attempted to compute $(dre_name) density ratios using (possibly default) optimization library $(optlib), but this library has either not been loaded or is not implemented for use with $(dre_name). Available options for `optlib`: $(optlib_options). If $(optlib) is contained within the available options, be sure to call `using $(lib_name)` before calling `densratio`, `densratiofunc`, or other functions fitting the $(dre_name) estimator.")
error(
"Attempted to compute $(dre_name) density ratios using (possibly default) optimization library $(optlib), but this library has either not been loaded or is not implemented for use with $(dre_name). Available options for `optlib`: $(optlib_options). If $(optlib) is contained within the available options, be sure to call `using $(lib_name)` before calling `densratio`, `densratiofunc`, or other functions fitting the $(dre_name) estimator."
)
end

function _throw_not_implemented_error(func::String, dre::Type{<:DensityRatioEstimator})
dre_name = nameof(dre)
error("Attempted to call `$(func)($(dre_name))` but this function has not been implemented for density ratio estimator of type $(dre_name).")
error(
"Attempted to call `$(func)($(dre_name))` but this function has not been implemented for density ratio estimator of type $(dre_name)."
)
end

_throw_not_fit_error(dre::Type{<:DensityRatioEstimator}, fitter::EstimatorFitter, optlib::Type{<:OptimizationLibrary}) =
error("Attempted to `fit` estimator `$(nameof(dre))` using fitter `$(typeof(fitter))` with optimization library `optlib=$(optlib)`, but no `fit` function has been implemented for this combination of estimator, fitter, and optimization library.")
_throw_not_fit_error(dre::Type{<:DensityRatioEstimator}, fitter::EstimatorFitter, optlib::Type{<:OptimizationLibrary}) =
error(
"Attempted to `fit` estimator `$(nameof(dre))` using fitter `$(typeof(fitter))` with optimization library `optlib=$(optlib)`, but no `fit` function has been implemented for this combination of estimator, fitter, and optimization library."
)
16 changes: 5 additions & 11 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,32 @@ using ReferenceTests, ImageIO
import CairoMakie as Mke

@testset "Throw errors: optlib not loaded" begin

@test_throws ErrorException densratio([0], [0], KLIEP())
@test_throws ErrorException densratiofunc([0], [0], KLIEP())
@test_throws ErrorException densratio([0], [0], LSIF())
@test_throws ErrorException densratiofunc([0], [0], LSIF())
@test_throws ErrorException densratio([0], [0], KMM(); optlib = JuMPLib)

@test_throws ErrorException densratio([0], [0], KMM(); optlib=JuMPLib)
end

using Optim
using JuMP, Ipopt
using Convex, ECOS

@testset "Throw errors: optlib not available" begin

@test_throws ErrorException densratio([0], [0], KLIEP(); optlib = JuMPLib)
@test_throws ErrorException densratio([0], [0], LSIF(); optlib = ConvexLib)
@test_throws ErrorException densratio([0], [0], KMM(); optlib = ConvexLib)

@test_throws ErrorException densratio([0], [0], KLIEP(); optlib=JuMPLib)
@test_throws ErrorException densratio([0], [0], LSIF(); optlib=ConvexLib)
@test_throws ErrorException densratio([0], [0], KMM(); optlib=ConvexLib)
end

@testset "Throw errors: undefined functions" begin

@test_throws ErrorException fit(KMM, [0], [0], LCV((σ=[1.],b=[10])))
@test_throws ErrorException fit(KMM, [0], [0], LCV((σ=[1.0], b=[10])))

struct NewDRE <: DensityRatioEstimator end

@test_throws ErrorException densratio([0], [0], NewDRE())
@test_throws ErrorException densratiofunc([0], [0], NewDRE())
@test_throws ErrorException default_optlib(NewDRE)
@test_throws ErrorException available_optlib(NewDRE)

end

# environment settings
Expand Down

0 comments on commit f2e3269

Please sign in to comment.