Skip to content

Commit 44f2a9e

Browse files
authored
Merge pull request #83 from ReactiveBayes/67-missing-formatter
#67 Add formatter. make format.
2 parents c63e995 + 5a805a3 commit 44f2a9e

42 files changed

Lines changed: 860 additions & 514 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ alloc-profile.pb.gz
1010
profile.pb.gz
1111

1212
*.ipynb
13-
.ipynb_checkpoints
13+
.ipynb_checkpoints
14+
*Manifest.toml

Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
DOCSRC = docs
55
DOCTARGET = $(DOCSRC)/build
66

7+
SCRIPTSRC = scripts
8+
FORMATTER = $(SCRIPTSRC)/formatter.jl
9+
710
JULIA ?= julia
811
JULIAFLAGS ?= --project=.
912
JULIAFLAGSDOCS ?= --project=$(DOCSRC)
13+
JULIAFLAGSSCRIPTS ?= --project=$(SCRIPTSRC)
1014

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

78+
deps-scripts: ## Install script dependencies
79+
$(JULIA) $(JULIAFLAGSSCRIPTS) -e 'using Pkg; Pkg.instantiate()'
80+
7181
test: deps ## Run project tests
7282
$(JULIA) $(JULIAFLAGS) -e 'using Pkg; Pkg.test(test_args = split("$(test_args)") .|> string)'
7383

84+
format: deps-scripts ## Format Julia code
85+
$(JULIA) $(JULIAFLAGSSCRIPTS) $(FORMATTER) --overwrite
86+
87+
check-format: deps-scripts ## Check Julia code formatting (does not modify files)
88+
$(JULIA) $(JULIAFLAGSSCRIPTS) $(FORMATTER)
89+
7490
clean: docs-clean ## Clean all generated files
7591
rm -rf .julia/compiled

docs/make.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ makedocs(;
1616
canonical = "https://reactivebayes.github.io/ExponentialFamilyProjection.jl",
1717
edit_link = "main",
1818
assets = String[],
19-
repolink="https://github.com/ReactiveBayes/ExponentialFamilyProjection.jl"
19+
repolink = "https://github.com/ReactiveBayes/ExponentialFamilyProjection.jl",
2020
),
2121
pages = ["Home" => "index.md"],
2222
)

scripts/Project.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[deps]
2+
ArgParse = "c7e460c6-2fb9-53a9-8c5b-16f535851c63"
3+
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
4+
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
5+
PkgBenchmark = "32113eaa-f34f-5b0d-bd6c-c81e245fc73d"

scripts/formatter.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using JuliaFormatter
2+
using ArgParse
3+
4+
s = ArgParseSettings()
5+
6+
@add_arg_table s begin
7+
"--overwrite"
8+
help = "Overwrite the files with the formatted code"
9+
action = :store_true
10+
default = false
11+
end
12+
13+
args = parse_args(ARGS, s)
14+
overwrite = args["overwrite"]
15+
projectroot = joinpath(@__DIR__, "..")
16+
17+
passed = format(projectroot; verbose = true, overwrite = overwrite)
18+
19+
if !passed && !overwrite
20+
@error "JuliaFormatter check has failed. Run `make format` from the main directory and commit your changes to fix code style."
21+
exit(1)
22+
elseif !passed && overwrite
23+
@info "JuliaFormatter has overwritten files according to style guidelines"
24+
elseif passed
25+
@info "Codestyle from JuliaFormatted checks have passed"
26+
end

src/jacobians.jl

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,65 +3,121 @@ function jacobian_nat_to_manifold!(::AbstractManifold, X_p, X_nat)
33
return X_p
44
end
55

6-
function jacobian_nat_to_manifold!(::ExponentialFamilyManifolds.NaturalParametersManifold{F, ExponentialFamily.NormalMeanVariance}, X_p, X_nat) where {F}
6+
function jacobian_nat_to_manifold!(
7+
::ExponentialFamilyManifolds.NaturalParametersManifold{
8+
F,
9+
ExponentialFamily.NormalMeanVariance,
10+
},
11+
X_p,
12+
X_nat,
13+
) where {F}
714
X_p[1:1] .= X_nat[1]
815
X_p[2:2] .= -X_nat[2]
916
return X_p
1017
end
1118

12-
function jacobian_nat_to_manifold!(::ExponentialFamilyManifolds.NaturalParametersManifold{F, ExponentialFamily.Gamma}, X_p, X_nat) where {F}
19+
function jacobian_nat_to_manifold!(
20+
::ExponentialFamilyManifolds.NaturalParametersManifold{F,ExponentialFamily.Gamma},
21+
X_p,
22+
X_nat,
23+
) where {F}
1324
X_p[1:1] .= X_nat[1]
1425
X_p[2:2] .= -X_nat[2]
1526
return X_p
1627
end
1728

18-
function jacobian_nat_to_manifold!(::ExponentialFamilyManifolds.NaturalParametersManifold{F, ExponentialFamily.Rayleigh}, X_p, X_nat) where {F}
29+
function jacobian_nat_to_manifold!(
30+
::ExponentialFamilyManifolds.NaturalParametersManifold{F,ExponentialFamily.Rayleigh},
31+
X_p,
32+
X_nat,
33+
) where {F}
1934
X_p[1:1] .= -X_nat[1]
2035
return X_p
2136
end
2237

23-
function jacobian_nat_to_manifold!(::ExponentialFamilyManifolds.NaturalParametersManifold{F, ExponentialFamily.Geometric}, X_p, X_nat) where {F}
38+
function jacobian_nat_to_manifold!(
39+
::ExponentialFamilyManifolds.NaturalParametersManifold{F,ExponentialFamily.Geometric},
40+
X_p,
41+
X_nat,
42+
) where {F}
2443
X_p[1:1] .= -X_nat[1]
2544
return X_p
2645
end
2746

28-
function jacobian_nat_to_manifold!(::ExponentialFamilyManifolds.NaturalParametersManifold{F, ExponentialFamily.GammaInverse}, X_p, X_nat) where {F}
47+
function jacobian_nat_to_manifold!(
48+
::ExponentialFamilyManifolds.NaturalParametersManifold{
49+
F,
50+
ExponentialFamily.GammaInverse,
51+
},
52+
X_p,
53+
X_nat,
54+
) where {F}
2955
X_p[1:1] .= -X_nat[1]
3056
X_p[2:2] .= -X_nat[2]
3157
return X_p
3258
end
3359

34-
function jacobian_nat_to_manifold!(::ExponentialFamilyManifolds.NaturalParametersManifold{F, ExponentialFamily.Exponential}, X_p, X_nat) where {F}
60+
function jacobian_nat_to_manifold!(
61+
::ExponentialFamilyManifolds.NaturalParametersManifold{F,ExponentialFamily.Exponential},
62+
X_p,
63+
X_nat,
64+
) where {F}
3565
X_p[1:1] .= -X_nat[1]
3666
return X_p
3767
end
3868

39-
function jacobian_nat_to_manifold!(::ExponentialFamilyManifolds.NaturalParametersManifold{F, ExponentialFamily.Weibull}, X_p, X_nat) where {F}
69+
function jacobian_nat_to_manifold!(
70+
::ExponentialFamilyManifolds.NaturalParametersManifold{F,ExponentialFamily.Weibull},
71+
X_p,
72+
X_nat,
73+
) where {F}
4074
X_p[1:1] .= -X_nat[1]
4175
return X_p
4276
end
4377

44-
function jacobian_nat_to_manifold!(::ExponentialFamilyManifolds.NaturalParametersManifold{F, ExponentialFamily.Laplace}, X_p, X_nat) where {F}
78+
function jacobian_nat_to_manifold!(
79+
::ExponentialFamilyManifolds.NaturalParametersManifold{F,ExponentialFamily.Laplace},
80+
X_p,
81+
X_nat,
82+
) where {F}
4583
X_p[1:1] .= -X_nat[1]
4684
return X_p
4785
end
4886

49-
function jacobian_nat_to_manifold!(::ExponentialFamilyManifolds.NaturalParametersManifold{F, ExponentialFamily.LogNormal}, X_p, X_nat) where {F}
87+
function jacobian_nat_to_manifold!(
88+
::ExponentialFamilyManifolds.NaturalParametersManifold{F,ExponentialFamily.LogNormal},
89+
X_p,
90+
X_nat,
91+
) where {F}
5092
X_p[1:1] .= X_nat[1]
5193
X_p[2:2] .= -X_nat[2]
5294
return X_p
5395
end
5496

55-
function jacobian_nat_to_manifold!(M::ExponentialFamilyManifolds.NaturalParametersManifold{F, ExponentialFamily.MvNormalMeanScalePrecision}, X_p, X_nat) where {F}
97+
function jacobian_nat_to_manifold!(
98+
M::ExponentialFamilyManifolds.NaturalParametersManifold{
99+
F,
100+
ExponentialFamily.MvNormalMeanScalePrecision,
101+
},
102+
X_p,
103+
X_nat,
104+
) where {F}
56105
k = first(ExponentialFamilyManifolds.getdims(M))
57106
X_p[1:k] .= X_nat[1:k]
58-
X_p[k+1:k+1] .= -X_nat[k+1:k+1]
107+
X_p[(k+1):(k+1)] .= -X_nat[(k+1):(k+1)]
59108
return X_p
60109
end
61110

62-
function jacobian_nat_to_manifold!(M::ExponentialFamilyManifolds.NaturalParametersManifold{F, ExponentialFamily.MvNormalMeanCovariance}, X_p, X_nat) where {F}
111+
function jacobian_nat_to_manifold!(
112+
M::ExponentialFamilyManifolds.NaturalParametersManifold{
113+
F,
114+
ExponentialFamily.MvNormalMeanCovariance,
115+
},
116+
X_p,
117+
X_nat,
118+
) where {F}
63119
k = first(ExponentialFamilyManifolds.getdims(M))
64120
X_p[1:k] .= X_nat[1:k]
65-
X_p[(k + 1):(k + k^2)] .= -X_nat[(k + 1):(k + k^2)]
121+
X_p[(k+1):(k+k^2)] .= -X_nat[(k+1):(k+k^2)]
66122
return X_p
67-
end
123+
end

src/manopt/bounded_norm_update_rule.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ function init_direction_rule(d::BoundedNormUpdateRule, ::Any)
2929
return d
3030
end
3131

32-
function init_direction_rule(bounded_direction::BoundedNormUpdateRule{L,D}, M) where {L, D <: Manopt.ManifoldDefaultsFactory}
32+
function init_direction_rule(
33+
bounded_direction::BoundedNormUpdateRule{L,D},
34+
M,
35+
) where {L,D<:Manopt.ManifoldDefaultsFactory}
3336
inner_direction = bounded_direction.direction(M)
3437
return BoundedNormUpdateRule(bounded_direction.limit, inner_direction)
3538
end
@@ -54,4 +57,3 @@ function (b::BoundedNormUpdateRule)(
5457
end
5558
return step, d
5659
end
57-

src/manopt/projection_objective.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ get_supplementary_η(obj::ProjectionCostGradientObjective) = obj.supplementary_
3131
get_strategy(obj::ProjectionCostGradientObjective) = obj.strategy
3232
get_strategy_state(obj::ProjectionCostGradientObjective) = obj.strategy_state
3333

34-
function call_objective(objective::ProjectionCostGradientObjective, M::AbstractManifold, X, p)
34+
function call_objective(
35+
objective::ProjectionCostGradientObjective,
36+
M::AbstractManifold,
37+
X,
38+
p,
39+
)
3540
current_ef = convert(ExponentialFamilyDistribution, M, p)
3641
current_η = copyto!(get_current_η(objective), getnaturalparameters(current_ef))
3742

@@ -89,5 +94,3 @@ end
8994
function (objective::ProjectionCostGradientObjective)(M::AbstractManifold, X, p)
9095
return call_objective(objective, M, X, p)
9196
end
92-
93-

src/projected_to.jl

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,13 @@ end
205205

206206
using Manopt
207207

208-
function check_inputs(prj::ProjectedTo, projection_argument::F, supplementary...; initialpoint = nothing, kwargs...) where {F}
208+
function check_inputs(
209+
prj::ProjectedTo,
210+
projection_argument::F,
211+
supplementary...;
212+
initialpoint = nothing,
213+
kwargs...,
214+
) where {F}
209215
if isnothing(initialpoint)
210216
return
211217
end
@@ -214,7 +220,7 @@ function check_inputs(prj::ProjectedTo, projection_argument::F, supplementary...
214220
lazy"The initial point must be on the manifold `$(get_projected_to_manifold(prj))`, got `$(typeof(initialpoint))`",
215221
)
216222
end
217-
end
223+
end
218224
"""
219225
project_to(to::ProjectedTo, argument::F, supplementary..., initialpoint, kwargs...)
220226
@@ -294,8 +300,15 @@ function project_to(
294300
getstrategy(projection_parameters),
295301
projection_argument,
296302
)
297-
current_iteration_point = preprocess_initialpoint(initialpoint, strategy, M, projection_parameters)
298-
check_inputs(prj, projection_argument, supplementary...; initialpoint = current_iteration_point, kwargs...)
303+
current_iteration_point =
304+
preprocess_initialpoint(initialpoint, strategy, M, projection_parameters)
305+
check_inputs(
306+
prj,
307+
projection_argument,
308+
supplementary...;
309+
initialpoint = current_iteration_point,
310+
kwargs...,
311+
)
299312
current_ef = convert(ExponentialFamilyDistribution, M, current_iteration_point)
300313
state = create_state!(
301314
strategy,
@@ -424,4 +437,4 @@ end
424437
# Otherwise we just copy the initial point, since we use it for the optimization in place
425438
function preprocess_initialpoint(_, initialpoint::AbstractArray, strategy, M, parameters)
426439
return copy(initialpoint)
427-
end
440+
end

0 commit comments

Comments
 (0)