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

Add run_reps.jl helper to run RME #26

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Add run_reps.jl helper to run RME #26

wants to merge 3 commits into from

Conversation

Zapiano
Copy link
Contributor

@Zapiano Zapiano commented Nov 13, 2024

Adds the ability to run many RME repetitions in batches. Note that the rnd_seed, that can be set by the user, is used to generate a vector of rnd_seeds where each one is used for running a distinct batch.

@Zapiano Zapiano added the enhancement New feature or request label Nov 13, 2024
@Zapiano Zapiano self-assigned this Nov 13, 2024
@BG-AIMS
Copy link

BG-AIMS commented Nov 18, 2024

I think this looks good to me. I can add the function to remove the duplicate counterfactual runs before saving in another branch when this is merged

@Zapiano
Copy link
Contributor Author

Zapiano commented Nov 18, 2024

I think this looks good to me. I can add the function to remove the duplicate counterfactual runs before saving in another branch when this is merged

You can create a branch from this one and create you PR already even before this one is merged, if you prefer.

@BG-AIMS
Copy link

BG-AIMS commented Nov 18, 2024

When I try to use the latest code with this PR I get this error adding the package. Is it a fault on my end in packages?
image

@ConnectedSystems
Copy link
Collaborator

When I try to use the latest code with this PR I get this error adding the package. Is it a fault on my end in packages? image

Minor point: dev . from within the sandbox is saying to install the sandbox as a dev'ed package - which doesn't make sense...

Unless you're not in the sandbox directory at all?

But the error seems to be pointing to ReefModEngine.jl requiring Julia v1.11 but you seem to be running Julia v1.10.4

Copy link
Collaborator

@ConnectedSystems ConnectedSystems left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments for consideration. Happy to be convinced otherwise.

"""
run_rme(rme_path::String, n_threads::Int64, reps::Int64, result_path::String; start_year::Int64=2022, end_year::Int64=2099, batch_size::Int64=10, start_batch::Int64=1, RCP_scen::String="SSP 2.45", gcm::String="CNRM_ESM2_1", rnd_seed::Int64=1234)::Nothing

Run counterfactual scenarios with ReefModEngine.jl and save result set to desired dir.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Run counterfactual scenarios with ReefModEngine.jl and save result set to desired dir.
Run counterfactual scenarios with ReefModEngine.jl and save result set to desired directory.

- `batch_size` : Number of repetitions to be run in each batch.
- `RCP_scen` : RCP scenario to be used for RME runs.
- `gcm` : GCM to be used for RME runs.
- `rnd_seed` : Random seed.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer to accept an RNG instead:

Suggested change
- `rnd_seed` : Random seed.
- `rng` : Random number generator (default: Globally set RNG).

rng::AbstractRNG=Random.GLOBAL_RNG

Comment on lines +33 to +35
init_rme(rme_path)
set_option("thread_count", n_threads) # Set number of threads
set_option("use_fixed_seed", 1) # Turn on use of a fixed seed value
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel these should be set outside this function by the user and the settings reused across runs if possible.

Currently, RME is re-initialized every time run_rme() is called. In recent versions it takes milliseconds, but in the past (and potentially in the future) it may take 10s of seconds to minutes.

Comment on lines +125 to +129
function _rnd_seeds(rnd_seed::Int64, batch_size::Int64, reps::Int64)::Vector{Int64}
Random.seed!(rnd_seed)
n_seeds = Int(ceil(reps / batch_size))
return Int.(floor.(rand(n_seeds) .* 1e6))
end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think use StatsBase and sample without replacement.
Right now there is a very (very) small possibility of a clash (multiple scenarios with same seed).

function _rnd_seeds(rnd_seed::Int64, batch_size::Int64, reps::Int64; rng::AbstractRNG=Random.GLOBAL_RNG, max_seed::Int64=1_000_000)::Vector{Int64}    
    n_seeds = Int(ceil(reps / batch_size))
    if n_seeds > max_seed
        # Protect against possibly number of seed values exceed seed range
        max_seed = max_seed + (n_seeds * 2)
    end

    return sample(rng, 1:max_seed, n_seeds; replace=false)
end

end

function _resultset_dir_name()::String
timestamp = Dates.format(now(), "yyyymmdd_HHMMSS")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer YYYY-mm-dd format

Run counterfactual scenarios with ReefModEngine.jl and save result set to desired dir.

# Arguments
- `rme_path` : Path to REM folder.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we move the RME option setting step outside this function as suggested below, we can remove this (and other) argument lines, but if we decide to keep it:

Suggested change
- `rme_path` : Path to REM folder.
- `rme_path` : Path to RME folder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants