Skip to content

Commit

Permalink
cleanup; runfit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Ortner committed Sep 9, 2024
1 parent 4dc91e2 commit f7c3cd1
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 12 deletions.
2 changes: 1 addition & 1 deletion scripts/example_params.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"virial_key": "dft_virial"
},
"output": {
"model": "ACE.json"
"model": "results.json"
}
}

2 changes: 1 addition & 1 deletion scripts/runfit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ end

# saving results
result_file = args_dict["output"]["model"]
ACEpotentials.save_model(model, @__DIR__() * "/results.json";
ACEpotentials.save_model(model, joinpath(@__DIR__(), args_dict["output"]["model"]);
make_model_args = args_dict,
errors = err, )
30 changes: 29 additions & 1 deletion src/json_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ end
##

# === make fits ===

"""
make_model(model_dict::Dict)
User-facing script to generate a model from a dictionary. See documentation
for details.
"""
function make_model(model_dict::Dict)
if model_dict["model_name"] == "ACE1"
model_nt = _sanitize_dict(model_dict)
Expand Down Expand Up @@ -59,7 +66,17 @@ end
# end
# end


"""
copy_runfit(dest)
Copies the `runfit.jl` script and an example model parameter file to `dest`.
If called from the destination directory, use
```julia
ACEpotentials.copy_runfit(@__DIR__())
```
This is intended to setup a local project directory with the necessary
scripts to run a fitting job.
"""
function copy_runfit(dest)
script_path = joinpath(@__DIR__(), "..", "scripts")
runfit_orig = joinpath(script_path, "runfit.jl")
Expand All @@ -72,7 +89,18 @@ function copy_runfit(dest)
end


"""
save_model(model, filename; kwargs...)
save model constructor, model parameters, and other information to a JSON file.
* `model` : the model to be saved
* `filename` : the name of the file to which the model will be saved
* `make_model_args` : the arguments used to construct the model; without this
the model cannot be reconstructed unless the original script is available
* `errors` : the fitting / test errors computed during the fitting
* `verbose` : print information about the saving process
"""
function save_model(model, filename;
make_model_args = nothing,
errors = nothing,
Expand Down
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
ExtXYZ = "352459e4-ddd7-4360-8937-99dcb397b478"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
LazyArtifacts = "4af54fe1-eca0-43a8-85a7-787d91b784e3"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Lux = "b2108857-7c20-44ae-9111-449ecde12c47"
LuxCore = "bb33d45b-7691-41d6-9220-0943567d0623"
Optimisers = "3bd65402-5787-11e9-1adc-39752487f4e2"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Polynomials4ML = "03c4bcba-a943-47e9-bfa1-b1661fc2974f"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Expand Down
40 changes: 35 additions & 5 deletions test/json/json_test.jl → test/json_test.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@

tmpproj = tempname()
ap_dir = joinpath(@__DIR__(), "..", "..")
ap_dir = joinpath(@__DIR__(), "..")
using LazyArtifacts
datafile = artifact"Si_tiny_dataset" * "/Si_tiny.xyz"
julia_cmd = Base.julia_cmd()

# prepare the project folder
run(`mkdir $tmpproj`)
run(`cp $datafile $(tmpproj*"/")`);
prep_proj = "using Pkg; Pkg.activate(tmpproj); Pkg.develop(; path = ap_dir); using ACEpotentials; ACEpotentials.copy_runfit(tmpproj);"
run(`$( String(julia_cmd) * "-e '" * prep_proj * "'" )`)
# run(`$(Base.julia_cmd()) --project=$tmpproj $tmpproj/runfit.jl $tmpproj/example_params.json`)
run(`cp $datafile $(tmpproj*"/")`);
prep_proj = """
begin
using Pkg;
Pkg.activate(\"$tmpproj\");
Pkg.develop(; path = \"$ap_dir\");
using ACEpotentials;
ACEpotentials.copy_runfit(\"$tmpproj\");
end
"""
run(`$julia_cmd -e $prep_proj`)

# run the fit
cd(tmpproj) do
run(`pwd`)
run(`$julia_cmd --project=. runfit.jl -p example_params.json`)
end

# load the results in the current process!
@info("Load the results")
using JSON
example_params = JSON.parsefile(joinpath(tmpproj, "example_params.json"))
results = JSON.parsefile(joinpath(tmpproj, "results.json"))

@info("Clean up temporary project")
run(`rm -rf $tmpproj`)

##

using ACEpotentials

@info("Check that runfit gives the same results as a julia script")
@info("TODO: ")

##

Expand Down
12 changes: 8 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@ using ACEpotentials, Test, LazyArtifacts

@testset "ACEpotentials.jl" begin

@testset "Models" begin include("models/test_models.jl") end
# @testset "Models" begin include("models/test_models.jl") end

# fitting tests
@testset "Test silicon" begin include("test_silicon.jl") end
@testset "Test recomputation of weights" begin include("test_recompw.jl") end

# json interface tests
@testset "Test JSON interface" begin include("json_test.jl") end

# misc
@testset "Weird bugs" begin include("test_bugs.jl") end


# TODO: bring FIO back cf Issue #217
# @testset "Test IO" begin include("test_io.jl") end

# experimental
# TODO move UF_ACE into ACEpotential properly
# @testset "UF_ACE" begin include("test_uface.jl") end

# weird stuff
@testset "Weird bugs" begin include("test_bugs.jl") end

# ACE1 compatibility tests
# TODO: these tests need to be revived either by creating a JSON
# of test data, or by updating ACE1/ACE1x/JuLIP to be compatible.
Expand Down

0 comments on commit f7c3cd1

Please sign in to comment.