Skip to content

Commit

Permalink
Use BlueStyle and JuliaFormatter (#246)
Browse files Browse the repository at this point in the history
We have been using JuliaFormatter quite successfully for some while in the JuliaGaussianProcesses organization, and I think it would be good to adopt some style guide also in DynamicPPL and other TuringLang packages. This helps to keep the code base consistent and, in my opinion, increases readability and reduces mental overhead.

I just chose [BlueStyle](https://github.com/invenia/BlueStyle) here since it is used in JuliaGaussianProcesses. I would be fine with YAS as well, I think a main criterion should just be that is supported by [JuliaFormatter](https://github.com/domluna/JuliaFormatter.jl) since this makes it much easier to format code correctly.

Speaking about convenience, I also added a configuration for JuliaFormatter such that one can just run `format(path_to_repo)` to re-format the codebase correctly (actually, that's what I did when putting together the PR). Moreover, I added a Github action that will automatically check and report if PRs are formatted correctly. It will even post suggestions with format fixes (if required) which is even more convenient.
  • Loading branch information
devmotion committed May 19, 2021
1 parent 4c659e3 commit 5ffbbd2
Show file tree
Hide file tree
Showing 38 changed files with 828 additions and 788 deletions.
1 change: 1 addition & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
style="blue"
33 changes: 33 additions & 0 deletions .github/workflows/Format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Format

on:
push:
branches:
# This is where pull requests from "bors r+" are built.
- staging
# This is where pull requests from "bors try" are built.
- trying
# Build the master branch.
- master
pull_request:

jobs:
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@latest
with:
version: 1
- name: Format code
run: |
using Pkg
Pkg.add(; name="JuliaFormatter", uuid="98e50ef6-434e-11e9-1051-2b60c6c9e899")
using JuliaFormatter
format("."; verbose=true)
shell: julia --color=yes {0}
- uses: reviewdog/action-suggester@v1
if: github.event_name == 'pull_request'
with:
tool_name: JuliaFormatter
fail_on_error: true
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[![IntegrationTest](https://github.com/TuringLang/DynamicPPL.jl/workflows/IntegrationTest/badge.svg?branch=master)](https://github.com/TuringLang/DynamicPPL.jl/actions?query=workflow%3AIntegrationTest+branch%3Amaster)
[![Coverage Status](https://coveralls.io/repos/github/TuringLang/DynamicPPL.jl/badge.svg?branch=master)](https://coveralls.io/github/TuringLang/DynamicPPL.jl?branch=master)
[![Codecov](https://codecov.io/gh/TuringLang/DynamicPPL.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/TuringLang/DynamicPPL.jl)
[![Code Style: Blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/invenia/BlueStyle)
[![ColPrac: Contributor's Guide on Collaborative Practices for Community Packages](https://img.shields.io/badge/ColPrac-Contributor's%20Guide-blueviolet)](https://colprac.sciml.ai/)
[![Bors enabled](https://bors.tech/images/badge_small.svg)](https://app.bors.tech/repositories/24589)

Expand Down
3 changes: 2 additions & 1 deletion bors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ status = [
"test (1.%, windows-latest, x64, 2)",
"test (1, windows-latest, x64, 1)",
"test (1, windows-latest, x64, 2)",
"Turing.jl"
"Turing.jl",
"format"
]
delete_merged_branches = true
# Require at least on approval of a project member.
Expand Down
183 changes: 91 additions & 92 deletions src/DynamicPPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,101 +5,101 @@ using AbstractPPL
using Distributions
using Bijectors

import AbstractMCMC
import ChainRulesCore
import NaturalSort
import MacroTools
import ZygoteRules
using AbstractMCMC: AbstractMCMC
using ChainRulesCore: ChainRulesCore
using NaturalSort: NaturalSort
using MacroTools: MacroTools
using ZygoteRules: ZygoteRules

import Random
using Random: Random

import Base: Symbol,
==,
hash,
getindex,
setindex!,
push!,
show,
isempty,
empty!,
getproperty,
setproperty!,
keys,
haskey
import Base:
Symbol,
==,
hash,
getindex,
setindex!,
push!,
show,
isempty,
empty!,
getproperty,
setproperty!,
keys,
haskey

# VarInfo
export AbstractVarInfo,
VarInfo,
UntypedVarInfo,
TypedVarInfo,
getlogp,
setlogp!,
acclogp!,
resetlogp!,
get_num_produce,
set_num_produce!,
reset_num_produce!,
increment_num_produce!,
set_retained_vns_del_by_spl!,
is_flagged,
set_flag!,
unset_flag!,
setgid!,
updategid!,
setorder!,
istrans,
link!,
invlink!,
tonamedtuple,
# VarName (reexport from AbstractPPL)
VarName,
inspace,
subsumes,
@varname,
# Compiler
@model,
# Utilities
vectorize,
reconstruct,
reconstruct!,
Sample,
init,
vectorize,
# Model
Model,
getmissings,
getargnames,
generated_quantities,
# Samplers
Sampler,
SampleFromPrior,
SampleFromUniform,
# Contexts
DefaultContext,
LikelihoodContext,
PriorContext,
MiniBatchContext,
PrefixContext,
assume,
dot_assume,
observer,
dot_observe,
tilde,
dot_tilde,
# Pseudo distributions
NamedDist,
NoDist,
# Prob macros
@prob_str,
@logprob_str,
# Convenience functions
logprior,
logjoint,
pointwise_loglikelihoods,
# Convenience macros
@addlogprob!,
@submodel

export AbstractVarInfo,
VarInfo,
UntypedVarInfo,
TypedVarInfo,
getlogp,
setlogp!,
acclogp!,
resetlogp!,
get_num_produce,
set_num_produce!,
reset_num_produce!,
increment_num_produce!,
set_retained_vns_del_by_spl!,
is_flagged,
set_flag!,
unset_flag!,
setgid!,
updategid!,
setorder!,
istrans,
link!,
invlink!,
tonamedtuple,
# VarName (reexport from AbstractPPL)
VarName,
inspace,
subsumes,
@varname,
# Compiler
@model,
# Utilities
vectorize,
reconstruct,
reconstruct!,
Sample,
init,
vectorize,
# Model
Model,
getmissings,
getargnames,
generated_quantities,
# Samplers
Sampler,
SampleFromPrior,
SampleFromUniform,
# Contexts
DefaultContext,
LikelihoodContext,
PriorContext,
MiniBatchContext,
PrefixContext,
assume,
dot_assume,
observer,
dot_observe,
tilde,
dot_tilde,
# Pseudo distributions
NamedDist,
NoDist,
# Prob macros
@prob_str,
@logprob_str,
# Convenience functions
logprior,
logjoint,
pointwise_loglikelihoods,
# Convenience macros
@addlogprob!,
@submodel

# Reexport
using Distributions: loglikelihood
Expand All @@ -112,7 +112,6 @@ function getspace end
abstract type AbstractVarInfo <: AbstractModelTrace end
abstract type AbstractContext end


include("utils.jl")
include("selector.jl")
include("model.jl")
Expand Down
12 changes: 3 additions & 9 deletions src/compat/ad.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
# See https://github.com/TuringLang/Turing.jl/issues/1199
ChainRulesCore.@non_differentiable push!(
vi::VarInfo,
vn::VarName,
r,
dist::Distribution,
gidset::Set{Selector}
vi::VarInfo, vn::VarName, r, dist::Distribution, gidset::Set{Selector}
)

ChainRulesCore.@non_differentiable updategid!(
vi::AbstractVarInfo,
vn::VarName,
spl::Sampler,
vi::AbstractVarInfo, vn::VarName, spl::Sampler
)

# https://github.com/TuringLang/Turing.jl/issues/1595
ZygoteRules.@adjoint function dot_observe(
spl::Union{SampleFromPrior, SampleFromUniform},
spl::Union{SampleFromPrior,SampleFromUniform},
dists::AbstractArray{<:Distribution},
value::AbstractArray,
vi,
Expand Down
Loading

0 comments on commit 5ffbbd2

Please sign in to comment.