Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ alloc-profile.pb.gz
profile.pb.gz

*.ipynb
.ipynb_checkpoints
.ipynb_checkpoints
*Manifest.toml
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
DOCSRC = docs
DOCTARGET = $(DOCSRC)/build

SCRIPTSRC = scripts
FORMATTER = $(SCRIPTSRC)/formatter.jl

JULIA ?= julia
JULIAFLAGS ?= --project=.
JULIAFLAGSDOCS ?= --project=$(DOCSRC)
JULIAFLAGSSCRIPTS ?= --project=$(SCRIPTSRC)

# Colors for terminal output
ifdef NO_COLOR
Expand Down Expand Up @@ -38,7 +42,10 @@ help:
@echo '${GREEN}Development commands:${RESET}'
@echo ' ${YELLOW}deps${RESET} Install project dependencies'
@echo ' ${YELLOW}deps-docs${RESET} Install documentation dependencies'
@echo ' ${YELLOW}deps-scripts${RESET} Install script dependencies'
@echo ' ${YELLOW}test${RESET} Run project tests'
@echo ' ${YELLOW}format${RESET} Format Julia code'
@echo ' ${YELLOW}check-format${RESET} Check Julia code formatting (does not modify files)'
@echo ' ${YELLOW}clean${RESET} Clean all generated files'
@echo ''
@echo '${GREEN}Help:${RESET}'
Expand Down Expand Up @@ -68,8 +75,17 @@ deps: ## Install project dependencies
deps-docs: ## Install documentation dependencies
$(JULIA) $(JULIAFLAGSDOCS) -e 'using Pkg; Pkg.develop(path="."); Pkg.instantiate()'

deps-scripts: ## Install script dependencies
$(JULIA) $(JULIAFLAGSSCRIPTS) -e 'using Pkg; Pkg.instantiate()'

test: deps ## Run project tests
$(JULIA) $(JULIAFLAGS) -e 'using Pkg; Pkg.test(test_args = split("$(test_args)") .|> string)'

format: deps-scripts ## Format Julia code
$(JULIA) $(JULIAFLAGSSCRIPTS) $(FORMATTER) --overwrite

check-format: deps-scripts ## Check Julia code formatting (does not modify files)
$(JULIA) $(JULIAFLAGSSCRIPTS) $(FORMATTER)

clean: docs-clean ## Clean all generated files
rm -rf .julia/compiled
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ makedocs(;
canonical = "https://reactivebayes.github.io/ExponentialFamilyProjection.jl",
edit_link = "main",
assets = String[],
repolink="https://github.com/ReactiveBayes/ExponentialFamilyProjection.jl"
repolink = "https://github.com/ReactiveBayes/ExponentialFamilyProjection.jl",
),
pages = ["Home" => "index.md"],
)
Expand Down
5 changes: 5 additions & 0 deletions scripts/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[deps]
ArgParse = "c7e460c6-2fb9-53a9-8c5b-16f535851c63"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
PkgBenchmark = "32113eaa-f34f-5b0d-bd6c-c81e245fc73d"
26 changes: 26 additions & 0 deletions scripts/formatter.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using JuliaFormatter
using ArgParse

s = ArgParseSettings()

@add_arg_table s begin
"--overwrite"
help = "Overwrite the files with the formatted code"
action = :store_true
default = false
end

args = parse_args(ARGS, s)
overwrite = args["overwrite"]
projectroot = joinpath(@__DIR__, "..")

passed = format(projectroot; verbose = true, overwrite = overwrite)

if !passed && !overwrite
@error "JuliaFormatter check has failed. Run `make format` from the main directory and commit your changes to fix code style."
exit(1)
elseif !passed && overwrite
@info "JuliaFormatter has overwritten files according to style guidelines"
elseif passed
@info "Codestyle from JuliaFormatted checks have passed"
end
84 changes: 70 additions & 14 deletions src/jacobians.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,121 @@ function jacobian_nat_to_manifold!(::AbstractManifold, X_p, X_nat)
return X_p
end

function jacobian_nat_to_manifold!(::ExponentialFamilyManifolds.NaturalParametersManifold{F, ExponentialFamily.NormalMeanVariance}, X_p, X_nat) where {F}
function jacobian_nat_to_manifold!(
::ExponentialFamilyManifolds.NaturalParametersManifold{
F,
ExponentialFamily.NormalMeanVariance,
},
X_p,
X_nat,
) where {F}
X_p[1:1] .= X_nat[1]
X_p[2:2] .= -X_nat[2]
return X_p
end

function jacobian_nat_to_manifold!(::ExponentialFamilyManifolds.NaturalParametersManifold{F, ExponentialFamily.Gamma}, X_p, X_nat) where {F}
function jacobian_nat_to_manifold!(
::ExponentialFamilyManifolds.NaturalParametersManifold{F,ExponentialFamily.Gamma},
X_p,
X_nat,
) where {F}
X_p[1:1] .= X_nat[1]
X_p[2:2] .= -X_nat[2]
return X_p
end

function jacobian_nat_to_manifold!(::ExponentialFamilyManifolds.NaturalParametersManifold{F, ExponentialFamily.Rayleigh}, X_p, X_nat) where {F}
function jacobian_nat_to_manifold!(
::ExponentialFamilyManifolds.NaturalParametersManifold{F,ExponentialFamily.Rayleigh},
X_p,
X_nat,
) where {F}
X_p[1:1] .= -X_nat[1]
return X_p
end

function jacobian_nat_to_manifold!(::ExponentialFamilyManifolds.NaturalParametersManifold{F, ExponentialFamily.Geometric}, X_p, X_nat) where {F}
function jacobian_nat_to_manifold!(
::ExponentialFamilyManifolds.NaturalParametersManifold{F,ExponentialFamily.Geometric},
X_p,
X_nat,
) where {F}
X_p[1:1] .= -X_nat[1]
return X_p
end

function jacobian_nat_to_manifold!(::ExponentialFamilyManifolds.NaturalParametersManifold{F, ExponentialFamily.GammaInverse}, X_p, X_nat) where {F}
function jacobian_nat_to_manifold!(
::ExponentialFamilyManifolds.NaturalParametersManifold{
F,
ExponentialFamily.GammaInverse,
},
X_p,
X_nat,
) where {F}
X_p[1:1] .= -X_nat[1]
X_p[2:2] .= -X_nat[2]
return X_p
end

function jacobian_nat_to_manifold!(::ExponentialFamilyManifolds.NaturalParametersManifold{F, ExponentialFamily.Exponential}, X_p, X_nat) where {F}
function jacobian_nat_to_manifold!(
::ExponentialFamilyManifolds.NaturalParametersManifold{F,ExponentialFamily.Exponential},
X_p,
X_nat,
) where {F}
X_p[1:1] .= -X_nat[1]
return X_p
end

function jacobian_nat_to_manifold!(::ExponentialFamilyManifolds.NaturalParametersManifold{F, ExponentialFamily.Weibull}, X_p, X_nat) where {F}
function jacobian_nat_to_manifold!(
::ExponentialFamilyManifolds.NaturalParametersManifold{F,ExponentialFamily.Weibull},
X_p,
X_nat,
) where {F}
X_p[1:1] .= -X_nat[1]
return X_p
end

function jacobian_nat_to_manifold!(::ExponentialFamilyManifolds.NaturalParametersManifold{F, ExponentialFamily.Laplace}, X_p, X_nat) where {F}
function jacobian_nat_to_manifold!(
::ExponentialFamilyManifolds.NaturalParametersManifold{F,ExponentialFamily.Laplace},
X_p,
X_nat,
) where {F}
X_p[1:1] .= -X_nat[1]
return X_p
end

function jacobian_nat_to_manifold!(::ExponentialFamilyManifolds.NaturalParametersManifold{F, ExponentialFamily.LogNormal}, X_p, X_nat) where {F}
function jacobian_nat_to_manifold!(
::ExponentialFamilyManifolds.NaturalParametersManifold{F,ExponentialFamily.LogNormal},
X_p,
X_nat,
) where {F}
X_p[1:1] .= X_nat[1]
X_p[2:2] .= -X_nat[2]
return X_p
end

function jacobian_nat_to_manifold!(M::ExponentialFamilyManifolds.NaturalParametersManifold{F, ExponentialFamily.MvNormalMeanScalePrecision}, X_p, X_nat) where {F}
function jacobian_nat_to_manifold!(
M::ExponentialFamilyManifolds.NaturalParametersManifold{
F,
ExponentialFamily.MvNormalMeanScalePrecision,
},
X_p,
X_nat,
) where {F}
k = first(ExponentialFamilyManifolds.getdims(M))
X_p[1:k] .= X_nat[1:k]
X_p[k+1:k+1] .= -X_nat[k+1:k+1]
X_p[(k+1):(k+1)] .= -X_nat[(k+1):(k+1)]
return X_p
end

function jacobian_nat_to_manifold!(M::ExponentialFamilyManifolds.NaturalParametersManifold{F, ExponentialFamily.MvNormalMeanCovariance}, X_p, X_nat) where {F}
function jacobian_nat_to_manifold!(
M::ExponentialFamilyManifolds.NaturalParametersManifold{
F,
ExponentialFamily.MvNormalMeanCovariance,
},
X_p,
X_nat,
) where {F}
k = first(ExponentialFamilyManifolds.getdims(M))
X_p[1:k] .= X_nat[1:k]
X_p[(k + 1):(k + k^2)] .= -X_nat[(k + 1):(k + k^2)]
X_p[(k+1):(k+k^2)] .= -X_nat[(k+1):(k+k^2)]
return X_p
end
end
6 changes: 4 additions & 2 deletions src/manopt/bounded_norm_update_rule.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ function init_direction_rule(d::BoundedNormUpdateRule, ::Any)
return d
end

function init_direction_rule(bounded_direction::BoundedNormUpdateRule{L,D}, M) where {L, D <: Manopt.ManifoldDefaultsFactory}
function init_direction_rule(
bounded_direction::BoundedNormUpdateRule{L,D},
M,
) where {L,D<:Manopt.ManifoldDefaultsFactory}
inner_direction = bounded_direction.direction(M)
return BoundedNormUpdateRule(bounded_direction.limit, inner_direction)
end
Expand All @@ -54,4 +57,3 @@ function (b::BoundedNormUpdateRule)(
end
return step, d
end

9 changes: 6 additions & 3 deletions src/manopt/projection_objective.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ get_supplementary_η(obj::ProjectionCostGradientObjective) = obj.supplementary_
get_strategy(obj::ProjectionCostGradientObjective) = obj.strategy
get_strategy_state(obj::ProjectionCostGradientObjective) = obj.strategy_state

function call_objective(objective::ProjectionCostGradientObjective, M::AbstractManifold, X, p)
function call_objective(
objective::ProjectionCostGradientObjective,
M::AbstractManifold,
X,
p,
)
current_ef = convert(ExponentialFamilyDistribution, M, p)
current_η = copyto!(get_current_η(objective), getnaturalparameters(current_ef))

Expand Down Expand Up @@ -89,5 +94,3 @@ end
function (objective::ProjectionCostGradientObjective)(M::AbstractManifold, X, p)
return call_objective(objective, M, X, p)
end


23 changes: 18 additions & 5 deletions src/projected_to.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,13 @@ end

using Manopt

function check_inputs(prj::ProjectedTo, projection_argument::F, supplementary...; initialpoint = nothing, kwargs...) where {F}
function check_inputs(
prj::ProjectedTo,
projection_argument::F,
supplementary...;
initialpoint = nothing,
kwargs...,
) where {F}
if isnothing(initialpoint)
return
end
Expand All @@ -214,7 +220,7 @@ function check_inputs(prj::ProjectedTo, projection_argument::F, supplementary...
lazy"The initial point must be on the manifold `$(get_projected_to_manifold(prj))`, got `$(typeof(initialpoint))`",
)
end
end
end
"""
project_to(to::ProjectedTo, argument::F, supplementary..., initialpoint, kwargs...)

Expand Down Expand Up @@ -294,8 +300,15 @@ function project_to(
getstrategy(projection_parameters),
projection_argument,
)
current_iteration_point = preprocess_initialpoint(initialpoint, strategy, M, projection_parameters)
check_inputs(prj, projection_argument, supplementary...; initialpoint = current_iteration_point, kwargs...)
current_iteration_point =
preprocess_initialpoint(initialpoint, strategy, M, projection_parameters)
check_inputs(
prj,
projection_argument,
supplementary...;
initialpoint = current_iteration_point,
kwargs...,
)
current_ef = convert(ExponentialFamilyDistribution, M, current_iteration_point)
state = create_state!(
strategy,
Expand Down Expand Up @@ -424,4 +437,4 @@ end
# Otherwise we just copy the initial point, since we use it for the optimization in place
function preprocess_initialpoint(_, initialpoint::AbstractArray, strategy, M, parameters)
return copy(initialpoint)
end
end
Loading
Loading