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

feat: add more command line inputs #109

Merged
merged 8 commits into from
Mar 1, 2021

Conversation

danielolsen
Copy link
Contributor

@danielolsen danielolsen commented Feb 10, 2021

Purpose

What is the code doing

  • In parser.py: adding two new command-line arguments.
  • In call.py: passing command-line arguments (including new ones) directly to the Launcher init.
  • In launchers.py:
    • All parameters passed for launching a simulation go to the Launcher init, to minimize duplication in the child launch_scenario methods.
    • A new init_julia method is added which will start a Julia session in the specified environment and import a given list of Julia packages. To be able to start in a given environment, we need to change the way we start the Julia sessions from from julia.api import Julia; Julia(compiled_modules=False) to from julia.api import LibJulia; api = LibJulia.load(); api.init_julia(["--compiled-modules=no"]); these are equivalent, see https://pyjulia.readthedocs.io/en/latest/troubleshooting.html#turn-off-compilation-cache. This lets us add another command-line argument to start in a given project folder (environment), see https://julialang.github.io/Pkg.jl/v1.5/environments/#Using-someone-else's-project
    • A new parse_and_return_runtime parse_runtime method is added to minimize duplication in the child launch_scenario methods.
    • The launcher lookup is fixed so that "GLPK" works, in addition to "glpk"

Usage Example

First, we'll set up a new Julia environment in an arbitrary folder, and install REISE and GLPK into this environment:

julia> import Pkg
julia> Pkg.activate(raw"C:\Users\DanielOlsen\repos\bes\scenario_3216\REISE_GLPK")
julia> Pkg.develop(path=raw"C:\Users\DanielOlsen\repos\bes\REISE.jl")
[...install truncated...]
julia> Pkg.add("GLPK")
[...install truncated...]
julia> exit()

Then, let's use all of our new command line parameters, executing them inside this new environment:

PS C:\Users\DanielOlsen\repos\bes\REISE.jl> python pyreisejl\utility\call.py -s '2016-01-01 00:00:00' -e '2016-01-01 05:00:00' -int 2 -i C:\Users\DanielOlsen\repos\bes\scenario_3216 -j C:\Users\DanielOlsen\repos\bes\scenario_3216\REISE_GLPK -l 3 --solver GLPK
Validation complete!
Launching scenario with parameters:
{'num_segments': 3, 'interval': 2, 'n_interval': 3, 'start_index': 1, 'input_dir': 'C:\\Users\\DanielOlsen\\repos\\bes\\scenario_3216', 'execute_dir': None, 'threads': None, 'julia_env': 'C:\\Users\\DanielOlsen\\repos\\bes\\scenario_3216\\REISE_GLPK', 'solver_kwargs': None}
INFO: threads not supported by GLPK, ignoring
Reading from folder: C:\Users\DanielOlsen\repos\bes\scenario_3216
...loading case.mat
...loading demand.csv
...loading hydro.csv
...loading wind.csv
...loading solar.csv
File case_storage.mat not found in C:\Users\DanielOlsen\repos\bes\scenario_3216
All scenario files loaded!
linearizing
All preparation complete!
Redirecting outputs, see stdout.log & stderr.err in outputfolder
Run time: 0:01:37

This works even if GLPK is not installed in your default environment, which you can check with a simple import in fresh julia session:

julia> import GLPK
ERROR: ArgumentError: Package GLPK not found in current path:
- Run `import Pkg; Pkg.add("GLPK")` to install the GLPK package.

Time to review

30 minutes.

@danielolsen danielolsen self-assigned this Feb 10, 2021
@danielolsen danielolsen added the new feature Feature that is currently in progress. label Feb 10, 2021
@danielolsen danielolsen force-pushed the daniel/more_command_line_inputs branch from 1178223 to 868ec69 Compare February 11, 2021 04:20
@danielolsen danielolsen force-pushed the daniel/more_command_line_inputs branch 6 times, most recently from 0edb38b to f4d2c07 Compare February 24, 2021 07:36
@danielolsen danielolsen marked this pull request as ready for review February 24, 2021 07:45
@danielolsen danielolsen requested a review from rouille February 24, 2021 07:47
@danielolsen danielolsen force-pushed the daniel/more_command_line_inputs branch from e14fe94 to dc5a3ba Compare February 25, 2021 22:47
@jenhagg
Copy link
Collaborator

jenhagg commented Feb 26, 2021

I got a warning during test run, just fyi. Each line is from stderr in the api response, and I removed a duplicate from line 74. The scenario still ran successfully though.

/app/pyreisejl/utility/helpers.py:72: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.",
  'data = np.array([f[r][()] for r in data.flat])',


return tuple([importlib.import_module(f"julia.{i}") for i in imports])

def parse_and_return_runtime(self, start, 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 might call this just parse_runtime since the return is kinda implied

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Julia(compiled_modules=False)
from julia import Gurobi # noqa: F401
from julia import REISE
Gurobi, REISE = self.init_julia(imports=["Gurobi", "REISE"])
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since we don't use the gurobi import directly, you could do _, REISE = ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@danielolsen
Copy link
Contributor Author

I got a warning during test run, just fyi. Each line is from stderr in the api response, and I removed a duplicate from line 74. The scenario still ran successfully though.

/app/pyreisejl/utility/helpers.py:72: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.",
  'data = np.array([f[r][()] for r in data.flat])',

Yeah, this is from our custom parse-hdf5-matfile-to-nested-dict-of-arrays script, and it's not new. Everything still works fine, but we'll probably want to update the call to avoid this warning in the future.

Copy link
Collaborator

@jenhagg jenhagg left a comment

Choose a reason for hiding this comment

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

LGTM. I was able to run simulations with both glpk and gurobi successfully, and your test of the new arguments looks sufficient.

@jenhagg
Copy link
Collaborator

jenhagg commented Feb 26, 2021

One more question: is the default of 1 for num_segments consistent with the current behavior?

@danielolsen
Copy link
Contributor Author

danielolsen commented Feb 26, 2021

One more question: is the default of 1 for num_segments consistent with the current behavior?

Yes, see

num_segments::Int=1, interval::Int, n_interval::Int, start_index::Int,

Copy link
Collaborator

@BainanXia BainanXia left a comment

Choose a reason for hiding this comment

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

I set up my local environment for running REISE.jl on my mac and run similar tests as @danielolsen posted. It works!

bxia REISE.jl (daniel/more_command_line_inputs) $ python pyreisejl/utility/call.py -s '2016-01-01 00:00:00' -e '2016-01-01 05:00:00' -int 2 -i "/Users/bainanxia/OneDrive - Gates Ventures/Documents/local_scenario_runs/scenario_3217" -l 3 --solver GLPK
Validation complete!
Launching scenario with parameters:
{'num_segments': 3, 'interval': 2, 'n_interval': 3, 'start_index': 1, 'input_dir': '/Users/bainanxia/OneDrive - Gates Ventures/Documents/local_scenario_runs/scenario_3217', 'execute_dir': None, 'threads': None, 'julia_env': None, 'solver_kwargs': None}
INFO: threads not supported by GLPK, ignoring
Reading from folder: /Users/bainanxia/OneDrive - Gates Ventures/Documents/local_scenario_runs/scenario_3217
...loading case.mat
...loading demand.csv
...loading hydro.csv
...loading wind.csv
...loading solar.csv
File case_storage.mat not found in /Users/bainanxia/OneDrive - Gates Ventures/Documents/local_scenario_runs/scenario_3217
All scenario files loaded!
linearizing
All preparation complete!
Redirecting outputs, see stdout.log & stderr.err in outputfolder
Run time: 0:01:28

@danielolsen danielolsen force-pushed the daniel/more_command_line_inputs branch from dc5a3ba to 1700b77 Compare March 1, 2021 18:18
@danielolsen danielolsen merged commit 4640554 into develop Mar 1, 2021
@danielolsen danielolsen deleted the daniel/more_command_line_inputs branch March 1, 2021 18:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature Feature that is currently in progress.
Projects
None yet
4 participants