How to make standard Julia package with input/options parameters in non compiled Julia files

I wrote a large scientific package SoilWater-ToolBox https://github.com/joseph-pollacco/SoilWaterToolbox and now it is time to compile the software into a Julia Package

The question is that I have different options of the model in a File Option.jl and would like users to modify the options before running the SoilWater-ToolBox.

The question would it be possible to compile SoilWater-ToolBox and at the same time give the users the possibilities to modify the **Option.jl** ?

EXAMPLE OF OPTION FILES?

module option
const HydroTranslateModel = false # <true>; <false>
      const Hypix       = false # <true>; <false>
      const Smap        = true # <true> ; <false>
      const BulkDensity = false # <true> <false>
      const θΨ          = :Opt # <:Opt>* Optimize hydraulic parameters from θ(Ψ); <:File> from save file; <:No> not available
      const Psd         = false	# <true> Derive θ(Ψ) AND/OR hydraulic parameters from Psd; <false>
      const Infilt      = false # <true> Derive θ(Ψ) AND/OR hydraulic parameters from Infiltration data; <false>
      const Temporary   = false # <true>; <false>
     const Jules = false # <true>; <false>
     		# PLOTTING
      const Plot      = false # <true>* plot; <false> no plotting
      const Plot_Show = false # <true>* plot shown in VScode; <false>```

Usually those options would be out in a structure, which may be set by the user and passed to the functions.

The idea of the user modifying a file that actually belongs to the package is not very nice. The alternative I think is to provide an example file that the user should follow and where those variables are defined.

(Note that defining the model with global constants won’t allow the user to run two different models in the same script. I don’t know if that is ok).

6 Likes

Thanks for conforming that when a function is compiled into a Package
it cannot read Julia files as input. As recommended, I will put the Options into a structure and read the parameters from a separate input file which will be object of another topic Discourse

Store your options in a JSON or TOML file and read that.

(You’re at Landcare? Hi from New Zealand. cc @mortenpi)

2 Likes

I use something like this:

julia> using Parameters, JSON3, StructTypes
       @with_kw struct Options
          opt1::Bool = 1
          opt2::Float64 = 1.0
       end
       StructTypes.StructType(::Type{Options}) = StructTypes.Struct() #needed for JSON


julia> opt = Options(opt2=5.0)
Options
  opt1: Bool true
  opt2: Float64 5.0


julia> JSON3.write("test.dat",opt)
"test.dat"

julia> my_saved_options = JSON3.read(open("test.dat","r"),Options)
Options
  opt1: Bool true
  opt2: Float64 5.0

The user defines the opt variable, which sets some options he/she wants to change, and passes that variable to the solvers, inner functions, etc.

Would Preferences.jl work?

3 Likes

Thanks for your help. I understand that in JULIA we have 4 formats to write input parametersand then read them automatically into mutable struct:

  • CSV (I know how to read csv automatically into struct but not very good for giving explanations of the different options available);
  • JSON (You must love {{{}}} );
  • YAML;
  • TOML;

My question which format is more readable for humans and which one will more easily read them automatically into mutable struct?

Thanks for any suggestions :grinning: ?

Thanks for the example. Would this be the same code for TOML ?

TOML

2 Likes