Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use JuliaFormatter #28

Merged
merged 2 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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.
yebai marked this conversation as resolved.
Show resolved Hide resolved
- 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 @@ -3,6 +3,7 @@
[![Build Status](https://github.com/TuringLang/AdvancedPS.jl/workflows/CI/badge.svg?branch=master)](https://github.com/TuringLang/AdvancedPS.jl/actions?query=workflow%3ACI%20branch%3Amaster)
[![Coverage](https://codecov.io/gh/TuringLang/AdvancedPS.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/TuringLang/AdvancedPS.jl)
[![Coverage](https://coveralls.io/repos/github/TuringLang/AdvancedPS.jl/badge.svg?branch=master)](https://coveralls.io/github/TuringLang/AdvancedPS.jl?branch=master)
[![Code Style: Blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/invenia/BlueStyle)

### Reference

Expand Down
10 changes: 5 additions & 5 deletions src/AdvancedPS.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module AdvancedPS

import AbstractMCMC
import Distributions
import Libtask
import Random
import StatsFuns
using AbstractMCMC: AbstractMCMC
using Distributions: Distributions
using Libtask: Libtask
using Random: Random
using StatsFuns: StatsFuns

include("resampling.jl")
include("container.jl")
Expand Down
36 changes: 20 additions & 16 deletions src/container.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ end
const Particle = Trace

function Trace(f)
ctask = let f=f
ctask = let f = f
Libtask.CTask() do
res = f()
Libtask.produce(nothing)
Expand Down Expand Up @@ -34,7 +34,7 @@ reset_model(f) = deepcopy(f)
delete_retained!(f) = nothing

# Task copying version of fork for Trace.
function fork(trace::Trace, isref::Bool = false)
function fork(trace::Trace, isref::Bool=false)
newtrace = copy(trace)
isref && delete_retained!(newtrace.f)

Expand All @@ -48,7 +48,7 @@ end
# Create new task and copy randomness
function forkr(trace::Trace)
newf = reset_model(trace.f)
ctask = let f=trace.ctask.task.code
ctask = let f = trace.ctask.task.code
Libtask.CTask() do
res = f()
Libtask.produce(nothing)
Expand Down Expand Up @@ -105,7 +105,7 @@ end
function Base.push!(pc::ParticleContainer, p::Particle)
push!(pc.vals, p)
push!(pc.logWs, 0.0)
pc
return pc
end

# clones a theta-particle
Expand All @@ -116,7 +116,7 @@ function Base.copy(pc::ParticleContainer)
# copy weights
logWs = copy(pc.logWs)

ParticleContainer(vals, logWs)
return ParticleContainer(vals, logWs)
end

"""
Expand Down Expand Up @@ -183,9 +183,9 @@ of the particle `weights`. For Particle Gibbs sampling, one can provide a refere
function resample_propagate!(
rng::Random.AbstractRNG,
pc::ParticleContainer,
randcat = resample_systematic,
ref::Union{Particle, Nothing} = nothing;
weights = getweights(pc)
randcat=resample_systematic,
ref::Union{Particle,Nothing}=nothing;
weights=getweights(pc),
)
# check that weights are not NaN
@assert !any(isnan, weights)
Expand Down Expand Up @@ -232,24 +232,24 @@ function resample_propagate!(
pc.vals = children
reset_logweights!(pc)

pc
return pc
end

function resample_propagate!(
rng::Random.AbstractRNG,
pc::ParticleContainer,
resampler::ResampleWithESSThreshold,
ref::Union{Particle,Nothing} = nothing;
weights = getweights(pc)
ref::Union{Particle,Nothing}=nothing;
weights=getweights(pc),
)
# Compute the effective sample size ``1 / ∑ wᵢ²`` with normalized weights ``wᵢ``
ess = inv(sum(abs2, weights))

if ess ≤ resampler.threshold * length(pc)
resample_propagate!(rng, pc, resampler.resampler, ref; weights = weights)
resample_propagate!(rng, pc, resampler.resampler, ref; weights=weights)
end

pc
return pc
end

"""
Expand Down Expand Up @@ -290,9 +290,13 @@ function reweight!(pc::ParticleContainer)

# The posterior for models with random number of observations is not well-defined.
if numdone != 0
error("mis-aligned execution traces: # particles = ", n,
" # completed trajectories = ", numdone,
". Please make sure the number of observations is NOT random.")
error(
"mis-aligned execution traces: # particles = ",
n,
" # completed trajectories = ",
numdone,
". Please make sure the number of observations is NOT random.",
)
end

return false
Expand Down
Loading