-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DummyModel for loading unknown models (#150)
* add DummyModel * start implementing tests * reorganize for DummyModel * fixes * fix required methods * update dummy model save * fix names * cleanup model compat * autoconvert to Float64 * fix test errors
- Loading branch information
Showing
9 changed files
with
233 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
""" | ||
DummyModel() | ||
If an unknown or undefined model is loaded a `DummyModel` will be created to | ||
allow the load to succeed and keep the data in check. | ||
""" | ||
struct DummyModel <: Model | ||
data::Dict{String, Any} | ||
end | ||
|
||
Base.show(io::IO, model::DummyModel) = print(io, "DummyModel()") | ||
|
||
function Base.getproperty(obj::DummyModel, field::Symbol) | ||
if hasfield(DummyModel, field) | ||
return getfield(obj, field) | ||
else | ||
return getfield(obj, :data)[string(field)] | ||
end | ||
end | ||
|
||
|
||
choose_field(::DummyModel) = DensityHirschField | ||
nflavors(::DummyModel) = 1 | ||
lattice(m::DummyModel) = m.l | ||
|
||
|
||
function save_model(file::JLDFile, m::DummyModel, entryname::String="Model") | ||
close(file) | ||
error("DummyModel cannot be saved.") | ||
end | ||
|
||
function _load_model(data, ::Val) | ||
tag = to_tag(data) | ||
@warn "Failed to load $tag, creating DummyModel" | ||
dict = _load_to_dict(data) | ||
if haskey(dict, "data") | ||
x = pop!(dict, "data") | ||
push!(dict, x...) | ||
end | ||
DummyModel(dict) | ||
end | ||
|
||
_load_to_dict(file::FileWrapper) = _load_to_dict(file.file) | ||
function _load_to_dict(data) | ||
if parentmodule(typeof(data)) == JLD2.ReconstructedTypes | ||
Dict(map(fieldnames(typeof(data))) do f | ||
string(f) => _load_to_dict(getfield(data, f)) | ||
end) | ||
else | ||
data | ||
end | ||
end | ||
function _load_to_dict(data::Union{JLD.JldFile, JLD2.JLDFile, JLD2.Group}) | ||
output = Dict{String, Any}() | ||
for key in keys(data) | ||
push!(output, key => _load_to_dict(data[key])) | ||
end | ||
output | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.