|
2 | 2 | $(TYPEDSIGNATURES) |
3 | 3 |
|
4 | 4 | Parse the problem instance found in `model_filepath` defined in the UAI model |
5 | | -format. |
| 5 | +format. If the provided file path is empty, return `nothing`. |
6 | 6 |
|
7 | 7 | The UAI file formats are defined in: |
8 | 8 | https://uaicompetition.github.io/uci-2022/file-formats/ |
9 | 9 | """ |
10 | | -function read_model_file(model_filepath; factor_eltype = Float64) |
| 10 | +function read_model_file(model_filepath::AbstractString; factor_eltype = Float64) |
11 | 11 | # Read the uai file into an array of lines |
12 | 12 | str = open(model_filepath) do file |
13 | 13 | read(file, String) |
@@ -244,10 +244,79 @@ function read_instance( |
244 | 244 | return UAIInstance(nvars, ncliques, cards, factors, obsvars, obsvals, queryvars, reference_solution) |
245 | 245 | end |
246 | 246 |
|
247 | | -function read_instance_from_string(uai::AbstractString; eltype = Float64)::UAIInstance |
248 | | - nvars, cards, ncliques, factors = read_model_string(uai; factor_eltype = eltype) |
| 247 | +function read_instance_from_string(model::AbstractString; eltype = Float64)::UAIInstance |
| 248 | + nvars, cards, ncliques, factors = read_model_string(model; factor_eltype = eltype) |
249 | 249 | return UAIInstance(nvars, ncliques, cards, factors, Int[], Int[], Int[], nothing) |
250 | 250 | end |
251 | 251 |
|
252 | 252 | # patch to get content by broadcasting into array, while keep array size unchanged. |
253 | 253 | broadcasted_content(x) = asarray(content.(x), x) |
| 254 | + |
| 255 | +""" |
| 256 | +$(TYPEDSIGNATURES) |
| 257 | +
|
| 258 | +Load a specific instance, identified by `task/name`, from the provided |
| 259 | +artifact. |
| 260 | +""" |
| 261 | +function read_instance_from_artifact( |
| 262 | + artifact_name::AbstractString, |
| 263 | + problem_name::AbstractString, |
| 264 | + task::AbstractString; |
| 265 | + eltype = Float64 |
| 266 | +) |
| 267 | + model_filepath, evidence_filepath, query_filepath, solution_filepath = get_instance_filepaths( |
| 268 | + artifact_name, |
| 269 | + problem_name, |
| 270 | + task |
| 271 | + ) |
| 272 | + read_instance(model_filepath; evidence_filepath, query_filepath, solution_filepath, eltype) |
| 273 | +end |
| 274 | + |
| 275 | +""" |
| 276 | +Helper function to obtain the filepaths for an instance's model, evidence, and |
| 277 | +solution files within the provided artifact, corresponding to the provided |
| 278 | +task. If a given file path does not exist in the instance's directory, an |
| 279 | +empty file path is returned. |
| 280 | +""" |
| 281 | +function get_instance_filepaths( |
| 282 | + artifact_name::AbstractString, |
| 283 | + problem_name::AbstractString, |
| 284 | + task::AbstractString |
| 285 | +) |
| 286 | + artifact_toml = pkgdir(TensorInference, "Artifacts.toml") |
| 287 | + Pkg.ensure_artifact_installed(artifact_name, artifact_toml) |
| 288 | + artifact_hash = Pkg.Artifacts.artifact_hash(artifact_name, artifact_toml) |
| 289 | + artifact_path = Pkg.Artifacts.artifact_path(artifact_hash) |
| 290 | + model_filepath = joinpath(artifact_path, task, problem_name * ".uai") |> topath |
| 291 | + evidence_filepath = joinpath(artifact_path, task, problem_name * ".uai.evid") |> topath |
| 292 | + query_filepath = joinpath(artifact_path, task, problem_name * ".uai.query") |> topath |
| 293 | + solution_filepath = joinpath(artifact_path, task, problem_name * ".uai." * task) |> topath |
| 294 | + return model_filepath, evidence_filepath, query_filepath, solution_filepath |
| 295 | +end |
| 296 | + |
| 297 | +""" |
| 298 | +This function returns the provided file path if it exists on disk, otherwise it |
| 299 | + returns an empty string. |
| 300 | +""" |
| 301 | +topath(filepath::AbstractString) = isfile(filepath) ? filepath : "" |
| 302 | + |
| 303 | +""" |
| 304 | +Helper function that captures the problem names that belong to `problem_set` |
| 305 | +for the given task. |
| 306 | +""" |
| 307 | +function get_problem_names( |
| 308 | + artifact_name::AbstractString, |
| 309 | + problem_set::AbstractString, |
| 310 | + task::AbstractString |
| 311 | +) |
| 312 | + artifact_toml = pkgdir(TensorInference, "Artifacts.toml") |
| 313 | + Pkg.ensure_artifact_installed(artifact_name, artifact_toml) |
| 314 | + artifact_hash = Pkg.Artifacts.artifact_hash(artifact_name, artifact_toml) |
| 315 | + artifact_path = Pkg.Artifacts.artifact_path(artifact_hash) |
| 316 | + |
| 317 | + regex = Regex("($(problem_set)_\\d*)(\\.uai)\$") |
| 318 | + return readdir(joinpath(artifact_path, task); sort = false) |> |
| 319 | + x -> map(y -> match(regex, y), x) |> # apply regex |
| 320 | + x -> filter(!isnothing, x) |> # filter out `nothing` values |
| 321 | + x -> map(first, x) # get the first capture of each element |
| 322 | +end |
0 commit comments