Skip to content

Commit

Permalink
Merge pull request #247 from iamveronika/saveproject-1
Browse files Browse the repository at this point in the history
Shell script json inteface
  • Loading branch information
cortner authored Sep 13, 2024
2 parents 6b2ca60 + d71a6d0 commit 29c6a9b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 15 deletions.
5 changes: 1 addition & 4 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ AtomsCalculatorsUtilities = "9855a07e-8816-4d1b-ac92-859c17475477"
Bumper = "8ce10254-0962-460f-a3d8-1f77fea1446e"
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
ChunkSplitters = "ae650224-84b6-46f8-82ea-d812ca08434e"
# EmpiricalPotentials = "38527215-9240-4c91-a638-d4250620c9e2"
EquivariantModels = "73ee3e68-46fd-466f-9c56-451dc0291ebc"
ExtXYZ = "352459e4-ddd7-4360-8937-99dcb397b478"
Folds = "41a02a25-b8f0-4f67-bc48-60067656b558"
Expand Down Expand Up @@ -41,7 +40,7 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
SpheriCart = "5caf2b29-02d9-47a3-9434-5931c85ba645"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
StrideArrays = "d1fa6d79-ef01-42a6-86c9-f7c551f8593b"
# UltraFastACE = "8bb720ee-daac-48fb-af73-8a282a9cbbd7"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
WithAlloc = "fb1aa66a-603c-4c1d-9bc4-66947c7b08dd"
YAML = "ddb6d928-2868-570f-bddf-ab3f9cf99eb6"
Expand All @@ -53,14 +52,12 @@ AtomsBase = "0.4.1"
AtomsBuilder = "0.2.0"
AtomsCalculators = "0.2"
AtomsCalculatorsUtilities = "0.1"
# EmpiricalPotentials = "0.2"
EquivariantModels = "0.0.5"
ExtXYZ = "0.2.0"
Interpolations = "0.15"
PrettyTables = "1.3, 2.0"
Reexport = "1"
StaticArrays = "1"
# UltraFastACE = "0.0.6"
YAML = "0.4"
julia = "~1.10.0"

Expand Down
3 changes: 2 additions & 1 deletion scripts/runfit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using ACEpotentials
using ACEpotentials: JSON, ExtXYZ
using TOML
using ACEpotentials.ArgParse: ArgParseSettings, @add_arg_table!, parse_args

parser = ArgParseSettings(description="Fit an ACE potential from parameters file")
Expand Down Expand Up @@ -169,4 +170,4 @@ if args_dict["output"]["make_plots"]
savefig(p, joinpath(res_path, "dimer_[$(ZZ[i]), $(ZZ[j])].png"))
end
end
end
end
75 changes: 65 additions & 10 deletions src/json_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import ArgParse
using NamedTupleTools
import .ACE1compat
using ACEfit
using TOML
using Pkg
using Optimisers: destructure

# === nt utilities ===
Expand Down Expand Up @@ -111,7 +113,10 @@ save model constructor, model parameters, and other information to a JSON file.
* `errors` : the fitting / test errors computed during the fitting
* `verbose` : print information about the saving process
"""
function save_model(model, filename;

# NOTE: save_model conflicts were not fully resolved!!!

function save_model(model, filename;
model_spec = nothing,
errors = nothing,
verbose = true,
Expand All @@ -128,17 +133,67 @@ function save_model(model, filename;
D["model_spec"] = model_spec
end

if !isnothing(errors)
D["errors"] = errors
end

open(filename, "w") do io
write(io, JSON.json(D, 3))
end
function find_manifest()
project_info = Pkg.project()
project_path = project_info.path
project_dir = joinpath(splitpath(project_path)[1:end-1]...)
manifest_path = joinpath(project_dir, "Manifest.toml")
if isfile(manifest_path)
return manifest_path
else
return nothing
end
end

if verbose
@info "Results saved to file: $filename"
end

function save_model(model, filename;
make_model_args = nothing,
errors = nothing,
verbose = true,
meta = Dict(),
save_project = true)

D = Dict("model_parameters" => model.ps,
"meta" => meta)

if isnothing(make_model_args)
if verbose
@warn("Only model parameters are saved but no information to reconstruct the model.")
end
else
D["make_model_args"] = make_model_args
end

if !isnothing(errors)
D["errors"] = errors
end

if save_project
manifest_path = find_manifest()
if manifest_path !== nothing
try
manifest_content = TOML.parsefile(manifest_path)
D["manifest"] = manifest_content
catch e
@warn("Failed to read Manifest.toml: $e")
end
else
@warn("Manifest.toml file not found in the project directory or any parent directories.")
end
else
if verbose
@info("Skipping saving of Manifest file.")
end
end

open(filename, "w") do io
write(io, JSON.json(D, 3))
end

if verbose
@info "Results saved to file: $filename"
end
end


Expand Down

0 comments on commit 29c6a9b

Please sign in to comment.