Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call jags with full paths to files to avoid changing working directory #39

Merged
merged 5 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions src/jagscode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ function jags(
updateinitfiles::Bool=true
)

old = pwd()

try
cd(ProjDir)

if updatedatafile
if length(keys(data)) > 0
print("\nCreating data file $(model.name)-data.R - ")
Expand All @@ -34,16 +30,12 @@ function jags(
end

println()
curdir = pwd()
cd(model.tmpdir)
println("Executing $(model.ncommands) command(s), each with $(model.nchains) chain(s) took:")
@time run(pipeline(par(model.command), stdout="$(model.name)-run.log"))
sim = mchain(model)
cd(old)
return(sim)
catch e
println(e)
cd(old)
end
end

Expand Down
28 changes: 13 additions & 15 deletions src/jagsmodel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ function Jagsmodel(;
updatejagsfile::Bool=true,
pdir::String=pwd())

cd(pdir)

tmpdir = joinpath(pdir, "tmp")
@show tmpdir
if !isdir(tmpdir)
Expand All @@ -54,7 +52,7 @@ function Jagsmodel(;
else
println("No proper model defined.")
end
model_file = "$(name).bugs"
model_file = joinpath(pdir, "$(name).bugs")

# Remove old files created by previous runs
for i in 1:ncommands
Expand All @@ -71,7 +69,7 @@ function Jagsmodel(;
# Create the command array which will be executed in parallel
cmdarray = fill(``, ncommands)
for i in 1:ncommands
jfile = "$(name)-cmd$(i).jags"
jfile = joinpath(tmpdir, "$(name)-cmd$(i).jags")
cmdarray[i] = @static Sys.iswindows() ? `cmd /c jags $(jfile)` : `jags $(jfile)`
end

Expand All @@ -87,7 +85,7 @@ function Jagsmodel(;
println("No monitors defined!")
end

data_file = "$(name)-data.R"
data_file = joinpath(tmpdir, "$(name)-data.R")

jm = Jagsmodel(name,
ncommands, nchains,
Expand All @@ -109,7 +107,6 @@ function Jagsmodel(;
updatejagsfile && update_jags_file(jm, i)
end
end

jm
end

Expand Down Expand Up @@ -139,12 +136,13 @@ function update_jags_file(model::Jagsmodel)
if model.deviance || model.dic || model.popt
jagsstr = jagsstr*"load dic\n"
end
jagsstr = jagsstr*"model in $(model.model_file)\n"
jagsstr = jagsstr*"data in $(model.data_file)\n"
modelfile = joinpath(model.tmpdir, basename(model.model_file))
jagsstr = jagsstr* "model in \"$(modelfile)\"\n"
jagsstr = jagsstr*"data in \"$(joinpath(model.tmpdir, model.data_file))\"\n"
jagsstr = jagsstr*"compile, nchains($(model.nchains))\n"
for i in 1:model.nchains
fname = "$(model.name)-inits$(i).R"
jagsstr = jagsstr*"parameters in $(fname), chain($(i))\n"
jagsstr = jagsstr*"parameters in \"$(joinpath(model.tmpdir, fname))\", chain($(i))\n"
end
jagsstr = jagsstr*"initialize\n"
jagsstr = jagsstr*"update $(model.adapt)\n"
Expand All @@ -164,7 +162,7 @@ function update_jags_file(model::Jagsmodel)
end
end
jagsstr = jagsstr*"update $(model.nsamples)\n"
jagsstr = jagsstr*"coda *, stem($(model.name)-cmd1-)\n"
jagsstr = jagsstr*"coda *, stem(\"$(joinpath(model.tmpdir, model.name))-cmd1-\")\n"
jagsstr = jagsstr*"exit\n"
check_jags_file(joinpath(model.tmpdir, "$(model.name)-cmd1.jags"), jagsstr)
end
Expand All @@ -179,12 +177,12 @@ function update_jags_file(model::Jagsmodel, cmd::Int)
if model.deviance || model.dic || model.popt
jagsstr = jagsstr*"load dic\n"
end
jagsstr = jagsstr*"model in $(model.model_file)\n"
jagsstr = jagsstr*"data in $(model.data_file)\n"
jagsstr = jagsstr* "model in \"" * joinpath(model.tmpdir, basename(model.model_file)) * "\"\n"
jagsstr = jagsstr*"data in \"$(joinpath(model.tmpdir, model.data_file))\"\n"
jagsstr = jagsstr*"compile, nchains($(model.nchains))\n"
for i in 1:model.nchains
fname = "$(model.name)-inits$(indx[i]).R"
jagsstr = jagsstr*"parameters in $(fname), chain($(i))\n"
fname = joinpath(model.tmpdir, "$(model.name)-inits$(indx[i]).R")
jagsstr = jagsstr*"parameters in \"$(fname)\", chain($(i))\n"
end
jagsstr = jagsstr*"initialize\n"
jagsstr = jagsstr*"update $(model.adapt)\n"
Expand All @@ -204,7 +202,7 @@ function update_jags_file(model::Jagsmodel, cmd::Int)
end
end
jagsstr = jagsstr*"update $(model.nsamples)\n"
jagsstr = jagsstr*"coda *, stem($(model.name)-cmd$(cmd)-)\n"
jagsstr = jagsstr*"coda *, stem(\"$(joinpath(model.tmpdir, model.name))-cmd$(cmd)-\")\n"
jagsstr = jagsstr*"exit\n"
check_jags_file(joinpath(model.tmpdir, "$(model.name)-cmd$(cmd).jags"), jagsstr)
end
Expand Down
15 changes: 5 additions & 10 deletions test/test_bones1.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
ProjDir = joinpath(dirname(@__FILE__), "..", "Examples", "Bones1")
cd(ProjDir) do

println("Moving to directory: $(ProjDir)")

isdir("tmp") &&
rm("tmp", recursive=true);
tmpdir = joinpath(ProjDir, "tmp")
isdir(tmpdir) &&
rm(tmpdir, recursive=true);

include(joinpath(ProjDir, "jbones1.jl"))

isdir("tmp") &&
rm("tmp", recursive=true);

end #cd
isdir(tmpdir) &&
rm(tmpdir, recursive=true);
13 changes: 4 additions & 9 deletions test/test_bones2.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
ProjDir = joinpath(dirname(@__FILE__), "..", "Examples", "Bones2")
cd(ProjDir) do

println("Moving to directory: $(ProjDir)")

isfile("tmp") &&
rm("tmp");
isfile(tmpdir) &&
rm(tmpdir);

include(joinpath(ProjDir, "jbones2.jl"))

isdir("tmp") &&
rm("tmp", recursive=true);

end
isdir(tmpdir) &&
rm(tmpdir, recursive=true);

11 changes: 5 additions & 6 deletions test/test_cmd.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ProjDir = dirname(@__FILE__)
cd(ProjDir) do
tmpdir = joinpath(ProjDir, "tmp")


inits1 = [
Dict("alpha" => 0,"beta" => 0,"tau" => 1),
Expand Down Expand Up @@ -63,8 +64,6 @@ cd(ProjDir) do
println("\nInput initial values dictionary:")
inits |> display

cd(ProjDir)
isdir("tmp") &&
rm("tmp", recursive=true);

end

isdir(tmpdir) &&
rm(tmpdir, recursive=true);
15 changes: 5 additions & 10 deletions test/test_dyes.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
ProjDir = joinpath(dirname(@__FILE__), "..", "Examples", "Dyes")
cd(ProjDir) do

println("Moving to directory: $(ProjDir)")

isdir("tmp") &&
rm("tmp", recursive=true);
tmpdir = joinpath(ProjDir, "tmp")
isdir(tmpdir) &&
rm(tmpdir, recursive=true);

include(joinpath(ProjDir, "jdyes.jl"))

isdir("tmp") &&
rm("tmp", recursive=true);

end
isdir(tmpdir) &&
rm(tmpdir, recursive=true);
13 changes: 5 additions & 8 deletions test/test_line1.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
ProjDir = joinpath(dirname(@__FILE__), "..", "Examples", "Line1")
cd(ProjDir) do
tmpdir = joinpath(ProjDir, "tmp")

println("Moving to directory: $(ProjDir)")

isdir("tmp") &&
rm("tmp", recursive=true);
isdir(tmpdir) &&
rm(tmpdir, recursive=true);

include(joinpath(ProjDir, "jline1.jl"))

isdir("tmp") &&
rm("tmp", recursive=true);
isdir(tmpdir) &&
rm(tmpdir, recursive=true);

end
15 changes: 6 additions & 9 deletions test/test_line2.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
ProjDir = joinpath(dirname(@__FILE__), "..", "Examples", "Line2")
cd(ProjDir) do

println("Moving to directory: $(ProjDir)")

isdir("tmp") &&
rm("tmp", recursive=true);
tmpdir = joinpath(ProjDir, "tmp")

isdir(tmpdir) &&
rm(tmpdir, recursive=true);

include(joinpath(ProjDir, "jline2.jl"))

isdir("tmp") &&
rm("tmp", recursive=true);
isdir(tmpdir) &&
rm(tmpdir, recursive=true);

end
14 changes: 5 additions & 9 deletions test/test_line3.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
ProjDir = joinpath(dirname(@__FILE__), "..", "Examples", "Line3")
cd(ProjDir) do
tmpdir = joinpath(ProjDir, "tmp")

println("Moving to directory: $(ProjDir)")

isdir("tmp") &&
rm("tmp", recursive=true);
isdir(tmpdir) &&
rm(tmpdir, recursive=true);

include(joinpath(ProjDir, "jline3.jl"))

isdir("tmp") &&
rm("tmp", recursive=true);

end
isdir(tmpdir) &&
rm(tmpdir, recursive=true);
13 changes: 5 additions & 8 deletions test/test_line4.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
ProjDir = joinpath(dirname(@__FILE__), "..", "Examples", "Line4")
cd(ProjDir) do
tmpdir = joinpath(ProjDir, "tmp")

println("Moving to directory: $(ProjDir)")

isdir("tmp") &&
rm("tmp", recursive=true);
isdir(tmpdir) &&
rm(tmpdir, recursive=true);

include(joinpath(ProjDir, "jline4.jl"))

isdir("tmp") &&
rm("tmp", recursive=true);
isdir(tmpdir) &&
rm(tmpdir, recursive=true);

end
13 changes: 5 additions & 8 deletions test/test_rats.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
ProjDir = joinpath(dirname(@__FILE__), "..", "Examples", "Rats")
cd(ProjDir) do
tmpdir = joinpath(ProjDir, "tmp")

println("Moving to directory: $(ProjDir)")

isdir("tmp") &&
rm("tmp", recursive=true);
isdir(tmpdir) &&
rm(tmpdir, recursive=true);

include(joinpath(ProjDir, "jrats.jl"))

isdir("tmp") &&
rm("tmp", recursive=true);
isdir(tmpdir) &&
rm(tmpdir, recursive=true);

end