Skip to content

Commit b3c4481

Browse files
authored
Code formatting: JuliaFormatter (#2255)
* Add .JuliaFormatter.toml * Run JuliaFormatter * Remove some overly cautious formatter ignores * Run JuliaFormatter * Add Format GA workflow
1 parent f135821 commit b3c4481

Some content is hidden

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

65 files changed

+1527
-1332
lines changed

.JuliaFormatter.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
style="blue"
2+
format_markdown = true
3+
# TODO
4+
# We ignore these files because when formatting was first put in place they were being worked on.
5+
# These ignores should be removed once the relevant PRs are merged/closed.
6+
ignore = [
7+
# https://github.com/TuringLang/Turing.jl/pull/2231/files
8+
"src/experimental/gibbs.jl",
9+
"src/mcmc/abstractmcmc.jl",
10+
"test/experimental/gibbs.jl",
11+
"test/test_utils/numerical_tests.jl",
12+
# https://github.com/TuringLang/Turing.jl/pull/2218/files
13+
"src/mcmc/Inference.jl",
14+
"test/mcmc/Inference.jl",
15+
# https://github.com/TuringLang/Turing.jl/pull/2068
16+
"src/variational/VariationalInference.jl",
17+
"src/variational/advi.jl",
18+
"test/variational/advi.jl",
19+
"test/variational/optimisers.jl",
20+
# https://github.com/TuringLang/Turing.jl/pull/1887
21+
"test/mcmc/Inference.jl",
22+
"test/mcmc/hmc.jl",
23+
"test/mcmc/sghmc.jl",
24+
"test/runtests.jl",
25+
]

.github/workflows/Format.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Format
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
merge_group:
11+
types: [checks_requested]
12+
13+
concurrency:
14+
# Skip intermediate builds: always.
15+
# Cancel intermediate builds: only if it is a pull request build.
16+
group: ${{ github.workflow }}-${{ github.ref }}
17+
cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }}
18+
19+
jobs:
20+
format:
21+
runs-on: ubuntu-latest
22+
steps:
23+
- uses: actions/checkout@v4
24+
- uses: julia-actions/setup-julia@latest
25+
with:
26+
version: 1
27+
- name: Format code
28+
run: |
29+
using Pkg
30+
Pkg.add(; name="JuliaFormatter", uuid="98e50ef6-434e-11e9-1051-2b60c6c9e899")
31+
using JuliaFormatter
32+
format("."; verbose=true)
33+
shell: julia --color=yes {0}
34+
- uses: reviewdog/action-suggester@v1
35+
if: github.event_name == 'pull_request'
36+
with:
37+
tool_name: JuliaFormatter
38+
fail_on_error: true

HISTORY.md

Lines changed: 76 additions & 67 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@
33
[![Build Status](https://github.com/TuringLang/Turing.jl/workflows/Turing-CI/badge.svg)](https://github.com/TuringLang/Turing.jl/actions?query=workflow%3ATuring-CI+branch%3Amaster)
44
[![Coverage Status](https://coveralls.io/repos/github/TuringLang/Turing.jl/badge.svg?branch=master)](https://coveralls.io/github/TuringLang/Turing.jl?branch=master)
55
[![codecov](https://codecov.io/gh/TuringLang/Turing.jl/branch/master/graph/badge.svg?token=OiUBsnDQqf)](https://codecov.io/gh/TuringLang/Turing.jl)
6-
[![ColPrac: Contributor's Guide on Collaborative Practices for Community Packages](https://img.shields.io/badge/ColPrac-Contributor's%20Guide-blueviolet)](https://github.com/SciML/ColPrac)
7-
6+
[![ColPrac: Contributor's Guide on Collaborative Practices for Community Packages](https://img.shields.io/badge/ColPrac-Contributor%27s%20Guide-blueviolet)](https://github.com/SciML/ColPrac)
87

98
## Getting Started
109

1110
Turing's home page, with links to everything you'll need to use Turing, is available at:
1211

1312
https://turinglang.org/docs/
1413

15-
1614
## What's changed recently?
1715

1816
See [releases](https://github.com/TuringLang/Turing.jl/releases).
@@ -25,6 +23,5 @@ You can see the complete list on Github: https://github.com/TuringLang/Turing.jl
2523
Turing is an open source project so if you feel you have some relevant skills and are interested in contributing, please get in touch. See the [Contributing](https://turinglang.org/dev/docs/contributing/guide) page for details on the process. You can contribute by opening issues on Github, implementing things yourself, and making a pull request. We would also appreciate example models written using Turing.
2624

2725
## Issues and Discussions
28-
Issues related to bugs and feature requests are welcome on the [issues page](https://github.com/TuringLang/Turing.jl/issues), while discussions and questions about statistical applications and theory should place on the [Discussions page](https://github.com/TuringLang/Turing.jl/discussions) or [our channel](https://julialang.slack.com/messages/turing/) (`#turing`) in the Julia Slack chat. If you do not have an invitation to Julia's Slack, you can get one by going [here](https://julialang.org/slack/).
29-
3026

27+
Issues related to bugs and feature requests are welcome on the [issues page](https://github.com/TuringLang/Turing.jl/issues), while discussions and questions about statistical applications and theory should place on the [Discussions page](https://github.com/TuringLang/Turing.jl/discussions) or [our channel](https://julialang.slack.com/messages/turing/) (`#turing`) in the Julia Slack chat. If you do not have an invitation to Julia's Slack, you can get one by going [here](https://julialang.org/slack/).

benchmarks/benchmarks_suite.jl

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@ BenchmarkSuite["constrained"] = BenchmarkGroup(["constrained"])
1616

1717
data = [0, 1, 0, 1, 1, 1, 1, 1, 1, 1]
1818

19-
2019
@model function constrained_test(obs)
21-
p ~ Beta(2,2)
22-
for i = 1:length(obs)
20+
p ~ Beta(2, 2)
21+
for i in 1:length(obs)
2322
obs[i] ~ Bernoulli(p)
2423
end
25-
p
24+
return p
2625
end
2726

28-
29-
BenchmarkSuite["constrained"]["constrained"] = @benchmarkable sample($(constrained_test(data)), $(HMC(0.01, 2)), 2000)
30-
27+
BenchmarkSuite["constrained"]["constrained"] = @benchmarkable sample(
28+
$(constrained_test(data)), $(HMC(0.01, 2)), 2000
29+
)
3130

3231
## gdemo
3332

@@ -41,9 +40,9 @@ BenchmarkSuite["gdemo"] = BenchmarkGroup(["gdemo"])
4140
return s², m
4241
end
4342

44-
BenchmarkSuite["gdemo"]["hmc"] = @benchmarkable sample($(gdemo(1.5, 2.0)), $(HMC(0.01, 2)), 2000)
45-
46-
43+
BenchmarkSuite["gdemo"]["hmc"] = @benchmarkable sample(
44+
$(gdemo(1.5, 2.0)), $(HMC(0.01, 2)), 2000
45+
)
4746

4847
## MvNormal
4948

@@ -52,33 +51,38 @@ BenchmarkSuite["mnormal"] = BenchmarkGroup(["mnormal"])
5251
# Define the target distribution and its gradient
5352

5453
@model function target(dim)
55-
Θ = Vector{Real}(undef, dim)
56-
θ ~ MvNormal(zeros(dim), I)
54+
Θ = Vector{Real}(undef, dim)
55+
return θ ~ MvNormal(zeros(dim), I)
5756
end
5857

5958
# Sampling parameter settings
6059
dim = 10
6160
n_samples = 100_000
6261
n_adapts = 2_000
6362

64-
BenchmarkSuite["mnormal"]["hmc"] = @benchmarkable sample($(target(dim)), $(HMC(0.1, 5)), $n_samples)
63+
BenchmarkSuite["mnormal"]["hmc"] = @benchmarkable sample(
64+
$(target(dim)), $(HMC(0.1, 5)), $n_samples
65+
)
6566

6667
## MvNormal: ForwardDiff vs ReverseDiff
6768

6869
@model function mdemo(d, N)
6970
Θ = Vector(undef, N)
70-
for n=1:N
71-
Θ[n] ~ d
72-
end
71+
for n in 1:N
72+
Θ[n] ~ d
73+
end
7374
end
7475

7576
dim2 = 250
76-
A = rand(Wishart(dim2, Matrix{Float64}(I, dim2, dim2)));
77-
d = MvNormal(zeros(dim2), A)
77+
A = rand(Wishart(dim2, Matrix{Float64}(I, dim2, dim2)));
78+
d = MvNormal(zeros(dim2), A)
7879

7980
# ForwardDiff
80-
BenchmarkSuite["mnormal"]["forwarddiff"] = @benchmarkable sample($(mdemo(d, 1)), $(HMC(0.1, 5; adtype=AutoForwardDiff(; chunksize=0))), 5000)
81-
81+
BenchmarkSuite["mnormal"]["forwarddiff"] = @benchmarkable sample(
82+
$(mdemo(d, 1)), $(HMC(0.1, 5; adtype=AutoForwardDiff(; chunksize=0))), 5000
83+
)
8284

8385
# ReverseDiff
84-
BenchmarkSuite["mnormal"]["reversediff"] = @benchmarkable sample($(mdemo(d, 1)), $(HMC(0.1, 5; adtype=AutoReverseDiff(false))), 5000)
86+
BenchmarkSuite["mnormal"]["reversediff"] = @benchmarkable sample(
87+
$(mdemo(d, 1)), $(HMC(0.1, 5; adtype=AutoReverseDiff(false))), 5000
88+
)

benchmarks/models/hlr.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,21 @@ end
1010
x, y = readlrdata()
1111

1212
@model function hlr_nuts(x, y, θ)
13+
N, D = size(x)
1314

14-
N,D = size(x)
15-
16-
σ² ~ Exponential(θ)
15+
σ² ~ Exponential(θ)
1716
α ~ Normal(0, sqrt(σ²))
1817
β ~ MvNormal(zeros(D), σ² * I)
1918

20-
for n = 1:N
21-
y[n] ~ BinomialLogit(1, dot(x[n,:], β) + α)
19+
for n in 1:N
20+
y[n] ~ BinomialLogit(1, dot(x[n, :], β) + α)
2221
end
2322
end
2423

2524
# Sampling parameter settings
2625
n_samples = 10_000
2726

2827
# Sampling
29-
BenchmarkSuite["nuts"]["hrl"] = @benchmarkable sample(hlr_nuts(x, y, 1/0.1), NUTS(0.65), n_samples)
28+
BenchmarkSuite["nuts"]["hrl"] = @benchmarkable sample(
29+
hlr_nuts(x, y, 1 / 0.1), NUTS(0.65), n_samples
30+
)

benchmarks/models/lr.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ end
1010
X, Y = readlrdata()
1111

1212
@model function lr_nuts(x, y, σ)
13-
14-
N,D = size(x)
13+
N, D = size(x)
1514

1615
α ~ Normal(0, σ)
1716
β ~ MvNormal(zeros(D), σ^2 * I)
1817

19-
for n = 1:N
20-
y[n] ~ BinomialLogit(1, dot(x[n,:], β) + α)
18+
for n in 1:N
19+
y[n] ~ BinomialLogit(1, dot(x[n, :], β) + α)
2120
end
2221
end
2322

@@ -26,5 +25,6 @@ n_samples = 1_000
2625
n_adapts = 1_000
2726

2827
# Sampling
29-
BenchmarkSuite["nuts"]["lr"] = @benchmarkable sample(lr_nuts(X, Y, 100),
30-
NUTS(0.65), n_samples)
28+
BenchmarkSuite["nuts"]["lr"] = @benchmarkable sample(
29+
lr_nuts(X, Y, 100), NUTS(0.65), n_samples
30+
)

benchmarks/models/lr_helper.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
using DelimitedFiles
22

33
function readlrdata()
4-
54
fname = joinpath(dirname(@__FILE__), "lr_nuts.data")
65
z = readdlm(fname)
7-
x = z[:,1:end-1]
8-
y = z[:,end] .- 1
6+
x = z[:, 1:(end - 1)]
7+
y = z[:, end] .- 1
98
return x, y
109
end

benchmarks/models/sv_nuts.jl

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,25 @@ if !haskey(BenchmarkSuite, "nuts")
66
end
77

88
fname = joinpath(dirname(@__FILE__), "sv_nuts.data")
9-
y, header = readdlm(fname, ',', header=true)
9+
y, header = readdlm(fname, ','; header=true)
1010

1111
# Stochastic volatility (SV)
1212
@model function sv_nuts(y, dy, ::Type{T}=Vector{Float64}) where {T}
13-
N = size(y,1)
13+
N = size(y, 1)
1414

15-
τ ~ Exponential(1/100)
16-
ν ~ Exponential(1/100)
15+
τ ~ Exponential(1 / 100)
16+
ν ~ Exponential(1 / 100)
1717
s = T(undef, N)
1818

19-
s[1] ~ Exponential(1/100)
19+
s[1] ~ Exponential(1 / 100)
2020
for n in 2:N
21-
s[n] ~ Normal(log(s[n-1]), τ)
21+
s[n] ~ Normal(log(s[n - 1]), τ)
2222
s[n] = exp(s[n])
23-
dy = log(y[n] / y[n-1]) / s[n]
24-
dy ~ TDist(ν)
23+
dy = log(y[n] / y[n - 1]) / s[n]
24+
dy ~ TDist(ν)
2525
end
2626
end
2727

28-
2928
# Sampling parameter settings
3029
n_samples = 10_000
3130

docs/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Turing's documentation in this directory is in markdown format.
1+
Turing's documentation in this directory is in markdown format.
22

33
If you want to build the doc locally, please refer to the [README](https://github.com/TuringLang/turinglang.github.io) file in [turinglang.github.io](https://github.com/TuringLang/turinglang.github.io).
44

5-
Please also visit [this repo](https://github.com/TuringLang/TuringTutorials/tree/master/tutorials) for the docs.
5+
Please also visit [this repo](https://github.com/TuringLang/TuringTutorials/tree/master/tutorials) for the docs.

0 commit comments

Comments
 (0)