Skip to content
This repository was archived by the owner on Sep 28, 2024. It is now read-only.

Commit 4236b1b

Browse files
authored
Merge pull request #72 from SciML/format
Format to SciML Style
2 parents a8073de + 176ad65 commit 4236b1b

File tree

32 files changed

+515
-513
lines changed

32 files changed

+515
-513
lines changed

.JuliaFormatter.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
style = "sciml"

.github/workflows/FormatCheck.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: format-check
2+
3+
on:
4+
push:
5+
branches:
6+
- 'master'
7+
- 'release-'
8+
tags: '*'
9+
pull_request:
10+
11+
jobs:
12+
build:
13+
runs-on: ${{ matrix.os }}
14+
strategy:
15+
matrix:
16+
julia-version: [1]
17+
julia-arch: [x86]
18+
os: [ubuntu-latest]
19+
steps:
20+
- uses: julia-actions/setup-julia@latest
21+
with:
22+
version: ${{ matrix.julia-version }}
23+
24+
- uses: actions/checkout@v1
25+
- name: Install JuliaFormatter and format
26+
# This will use the latest version by default but you can set the version like so:
27+
#
28+
# julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter", version="0.13.0"))'
29+
run: |
30+
julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter"))'
31+
julia -e 'using JuliaFormatter; format(".", verbose=true)'
32+
- name: Format check
33+
run: |
34+
julia -e '
35+
out = Cmd(`git diff --name-only`) |> read |> String
36+
if out == ""
37+
exit(0)
38+
else
39+
@error "Some files have not been formatted !!!"
40+
write(stdout, out)
41+
exit(1)
42+
end'

docs/make.jl

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,23 @@ using NeuralOperators
22
using Documenter
33
using DocumenterCitations
44

5-
bib = CitationBibliography(joinpath(@__DIR__, "bibliography.bib"), sorting=:nyt)
5+
bib = CitationBibliography(joinpath(@__DIR__, "bibliography.bib"), sorting = :nyt)
66

7-
DocMeta.setdocmeta!(NeuralOperators, :DocTestSetup, :(using NeuralOperators); recursive=true)
7+
DocMeta.setdocmeta!(NeuralOperators, :DocTestSetup, :(using NeuralOperators);
8+
recursive = true)
89

910
include("pages.jl")
1011

11-
makedocs(
12-
bib,
13-
modules=[NeuralOperators],
14-
authors="JingYu Ning <foldfelis@gmail.com> and contributors",
15-
repo="https://github.com/SciML/NeuralOperators.jl/blob/{commit}{path}#{line}",
16-
sitename="NeuralOperators.jl",
17-
format=Documenter.HTML(;
18-
prettyurls=get(ENV, "CI", "false") == "true",
19-
canonical="http://neuraloperators.sciml.ai",
20-
assets=String[],
21-
),
22-
pages=pages,
23-
)
12+
makedocs(bib,
13+
modules = [NeuralOperators],
14+
authors = "JingYu Ning <foldfelis@gmail.com> and contributors",
15+
repo = "https://github.com/SciML/NeuralOperators.jl/blob/{commit}{path}#{line}",
16+
sitename = "NeuralOperators.jl",
17+
format = Documenter.HTML(;
18+
prettyurls = get(ENV, "CI", "false") == "true",
19+
canonical = "http://neuraloperators.sciml.ai",
20+
assets = String[]),
21+
pages = pages)
2422

2523
deploydocs(;
26-
repo="github.com/SciML/NeuralOperators.jl",
27-
)
24+
repo = "github.com/SciML/NeuralOperators.jl")

docs/pages.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Put in a separate page so it can be used by SciMLDocs.jl
22

33
pages = [
4-
"Home" => "index.md",
5-
"Introduction" => "introduction.md",
6-
"APIs" => "apis.md",
7-
"References" => "references.md",
8-
]
4+
"Home" => "index.md",
5+
"Introduction" => "introduction.md",
6+
"APIs" => "apis.md",
7+
"References" => "references.md",
8+
]

example/Burgers/src/Burgers.jl

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,17 @@ import Flux: params
88
include("Burgers_deeponet.jl")
99

1010
function register_burgers()
11-
register(DataDep(
12-
"Burgers",
13-
"""
14-
Burgers' equation dataset from
15-
[fourier_neural_operator](https://github.com/zongyi-li/fourier_neural_operator)
16-
""",
17-
"http://www.med.cgu.edu.tw/NeuralOperators/Burgers_R10.zip",
18-
"9cbbe5070556c777b1ba3bacd49da5c36ea8ed138ba51b6ee76a24b971066ecd",
19-
post_fetch_method=unpack
20-
))
11+
register(DataDep("Burgers",
12+
"""
13+
Burgers' equation dataset from
14+
[fourier_neural_operator](https://github.com/zongyi-li/fourier_neural_operator)
15+
""",
16+
"http://www.med.cgu.edu.tw/NeuralOperators/Burgers_R10.zip",
17+
"9cbbe5070556c777b1ba3bacd49da5c36ea8ed138ba51b6ee76a24b971066ecd",
18+
post_fetch_method = unpack))
2119
end
2220

23-
function get_data(; n=2048, Δsamples=2^3, grid_size=div(2^13, Δsamples), T=Float32)
21+
function get_data(; n = 2048, Δsamples = 2^3, grid_size = div(2^13, Δsamples), T = Float32)
2422
file = matopen(joinpath(datadep"Burgers", "burgers_data_R10.mat"))
2523
x_data = T.(collect(read(file, "a")[1:n, 1:Δsamples:end]'))
2624
y_data = T.(collect(read(file, "u")[1:n, 1:Δsamples:end]'))
@@ -33,19 +31,19 @@ function get_data(; n=2048, Δsamples=2^3, grid_size=div(2^13, Δsamples), T=Flo
3331
return x_loc_data, reshape(y_data, 1, :, n)
3432
end
3533

36-
function get_dataloader(; ratio::Float64=0.9, batchsize=100)
37-
𝐱, 𝐲 = get_data(n=2048)
38-
data_train, data_test = splitobs((𝐱, 𝐲), at=ratio)
34+
function get_dataloader(; ratio::Float64 = 0.9, batchsize = 100)
35+
𝐱, 𝐲 = get_data(n = 2048)
36+
data_train, data_test = splitobs((𝐱, 𝐲), at = ratio)
3937

40-
loader_train = DataLoader(data_train, batchsize=batchsize, shuffle=true)
41-
loader_test = DataLoader(data_test, batchsize=batchsize, shuffle=false)
38+
loader_train = DataLoader(data_train, batchsize = batchsize, shuffle = true)
39+
loader_test = DataLoader(data_test, batchsize = batchsize, shuffle = false)
4240

4341
return loader_train, loader_test
4442
end
4543

4644
__init__() = register_burgers()
4745

48-
function train(; cuda=true, η₀=1f-3, λ=1f-4, epochs=500)
46+
function train(; cuda = true, η₀ = 1.0f-3, λ = 1.0f-4, epochs = 500)
4947
if cuda && CUDA.has_cuda()
5048
device = gpu
5149
CUDA.allowscalar(false)
@@ -55,22 +53,21 @@ function train(; cuda=true, η₀=1f-3, λ=1f-4, epochs=500)
5553
@info "Training on CPU"
5654
end
5755

58-
model = FourierNeuralOperator(ch=(2, 64, 64, 64, 64, 64, 128, 1), modes=(16, ), σ=gelu)
56+
model = FourierNeuralOperator(ch = (2, 64, 64, 64, 64, 64, 128, 1), modes = (16,),
57+
σ = gelu)
5958
data = get_dataloader()
6059
optimiser = Flux.Optimiser(WeightDecay(λ), Flux.ADAM(η₀))
6160
loss_func = l₂loss
6261

63-
learner = Learner(
64-
model, data, optimiser, loss_func,
65-
ToDevice(device, device),
66-
)
62+
learner = Learner(model, data, optimiser, loss_func,
63+
ToDevice(device, device))
6764

6865
fit!(learner, epochs)
6966

7067
return learner
7168
end
7269

73-
function train_nomad(; n=300, cuda=true, learning_rate=0.001, epochs=400)
70+
function train_nomad(; n = 300, cuda = true, learning_rate = 0.001, epochs = 400)
7471
if cuda && has_cuda()
7572
@info "Training on GPU"
7673
device = gpu
@@ -79,32 +76,31 @@ function train_nomad(; n=300, cuda=true, learning_rate=0.001, epochs=400)
7976
device = cpu
8077
end
8178

82-
x, y = get_data_don(n=n)
79+
x, y = get_data_don(n = n)
8380

8481
xtrain = x[1:280, :]'
8582
ytrain = y[1:280, :]
8683

87-
xval = x[end-19:end, :]' |> device
88-
yval = y[end-19:end, :] |> device
84+
xval = x[(end - 19):end, :]' |> device
85+
yval = y[(end - 19):end, :] |> device
8986

9087
# grid = collect(range(0, 1, length=1024)') |> device
9188
grid = rand(collect(0:0.001:1), (280, 1024)) |> device
9289
gridval = rand(collect(0:0.001:1), (20, 1024)) |> device
9390

94-
9591
opt = ADAM(learning_rate)
9692

97-
m = NOMAD((1024,1024), (2048,1024), gelu, gelu) |> device
93+
m = NOMAD((1024, 1024), (2048, 1024), gelu, gelu) |> device
9894

9995
loss(X, y, sensor) = Flux.Losses.mse(m(X, sensor), y)
10096
evalcb() = @show(loss(xval, yval, gridval))
10197

10298
data = [(xtrain, ytrain, grid)] |> device
103-
Flux.@epochs epochs Flux.train!(loss, params(m), data, opt, cb=evalcb)
99+
Flux.@epochs epochs Flux.train!(loss, params(m), data, opt, cb = evalcb)
104100
= m(xval |> device, gridval |> device)
105101

106102
diffvec = vec(abs.(cpu(yval) .- cpu(ỹ)))
107-
mean_diff = sum(diffvec)/length(diffvec)
103+
mean_diff = sum(diffvec) / length(diffvec)
108104
return mean_diff
109105
end
110106

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function get_data_don(; n=2048, Δsamples=2^3, grid_size=div(2^13, Δsamples))
1+
function get_data_don(; n = 2048, Δsamples = 2^3, grid_size = div(2^13, Δsamples))
22
file = matopen(joinpath(datadep"Burgers", "burgers_data_R10.mat"))
33
x_data = collect(read(file, "a")[1:n, 1:Δsamples:end])
44
y_data = collect(read(file, "u")[1:n, 1:Δsamples:end])
@@ -7,7 +7,7 @@ function get_data_don(; n=2048, Δsamples=2^3, grid_size=div(2^13, Δsamples))
77
return x_data, y_data
88
end
99

10-
function train_don(; n=300, cuda=true, learning_rate=0.001, epochs=400)
10+
function train_don(; n = 300, cuda = true, learning_rate = 0.001, epochs = 400)
1111
if cuda && has_cuda()
1212
@info "Training on GPU"
1313
device = gpu
@@ -16,28 +16,28 @@ function train_don(; n=300, cuda=true, learning_rate=0.001, epochs=400)
1616
device = cpu
1717
end
1818

19-
x, y = get_data_don(n=n)
19+
x, y = get_data_don(n = n)
2020

2121
xtrain = x[1:280, :]'
2222
ytrain = y[1:280, :]
2323

24-
xval = x[end-19:end, :]' |> device
25-
yval = y[end-19:end, :] |> device
24+
xval = x[(end - 19):end, :]' |> device
25+
yval = y[(end - 19):end, :] |> device
2626

27-
grid = collect(range(0, 1, length=1024)') |> device
27+
grid = collect(range(0, 1, length = 1024)') |> device
2828

2929
opt = ADAM(learning_rate)
3030

31-
m = DeepONet((1024,1024,1024), (1,1024,1024), gelu, gelu) |> device
31+
m = DeepONet((1024, 1024, 1024), (1, 1024, 1024), gelu, gelu) |> device
3232

3333
loss(X, y, sensor) = Flux.Losses.mse(m(X, sensor), y)
3434
evalcb() = @show(loss(xval, yval, grid))
3535

3636
data = [(xtrain, ytrain, grid)] |> device
37-
Flux.@epochs epochs Flux.train!(loss, params(m), data, opt, cb=evalcb)
37+
Flux.@epochs epochs Flux.train!(loss, params(m), data, opt, cb = evalcb)
3838
= m(xval |> device, grid |> device)
3939

4040
diffvec = vec(abs.(cpu(yval) .- cpu(ỹ)))
41-
mean_diff = sum(diffvec)/length(diffvec)
41+
mean_diff = sum(diffvec) / length(diffvec)
4242
return mean_diff
4343
end

example/Burgers/test/runtests.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ using Burgers
22
using Test
33

44
@testset "Burgers" begin
5-
xs, ys = Burgers.get_data(n=1000)
5+
xs, ys = Burgers.get_data(n = 1000)
66

77
@test size(xs) == (2, 1024, 1000)
88
@test size(ys) == (1, 1024, 1000)
99

10-
learner = Burgers.train(epochs=10)
10+
learner = Burgers.train(epochs = 10)
1111
loss = learner.cbstate.metricsepoch[ValidationPhase()][:Loss].values[end]
1212
@test loss < 0.1
1313

1414
# include("deeponet.jl")
1515
end
1616

1717
@testset "Burger: NOMAD Training Accuracy" begin
18-
ϵ = Burgers.train_nomad(; cuda=true, epochs=100)
18+
ϵ = Burgers.train_nomad(; cuda = true, epochs = 100)
1919
@test ϵ < 0.4 # epoch=100 returns 0.233
20-
end
20+
end

0 commit comments

Comments
 (0)