API documentation
- class iwutil.OptionSpec(default_value, other_allowed_values: list | None = None)
Specification for an option
Parameters
- default_valueany
The default value for the option
- other_allowed_valueslist, optional
A list of allowed values for the option, by default None. If None, the value can be anything. If a list, the value must be in this list.
- iwutil.subplots_autolayout(n, *args, n_rows=None, figsize=None, layout='constrained', **kwargs)
Create a subplot element
- iwutil.check_and_combine_options(default_options, custom_options=None, filter_unknown=False) dict
Check that all required options are provided, and combine default and custom options
Parameters
- default_optionsdict
Dictionary of default options. Each key is an option name, and the value can be either:
The default value for that option
“[required]” if the option must be provided in custom_options
A list of allowed values for that option. If the option is not provided in custom_options, the first value in the list is used.
- custom_optionsdict, optional
Dictionary of custom options, by default None. If a key in custom_options is not in default_options, an error is raised (unless filter_unknown is True).
- filter_unknownbool, optional
If False (default), raise ValueError for any key in custom_options that is not in default_options. If True, ignore such keys and only use options that have a default.
Returns
- dict
Combined options
Raises
- ValueError
If filter_unknown is False and a key in custom_options is not in default_options. If a required option is not provided in custom_options. If a list option is not one of the allowed values.
- iwutil.read_df(file, **kwargs)
- iwutil.read_df(file: str, **kwargs)
- iwutil.read_df(file: Path, **kwargs)
- iwutil.read_df(file: DataFrame, **kwargs)
Read a dataframe from a file. Currently: supports csv, xls, xlsx, json, and parquet.
Parameters
- filestr or Path
File to read
- **kwargsdict
Additional keyword arguments to pass to the read function
- iwutil.iwutil_file_path_helper(file_name: str | Path, **kwargs)
- iwutil.read_json(file_name)
Read a json file and sanitize the input
Parameters
- file_namestr or Path
File to read
- iwutil.copyfile(src, dst)
Copy a file from src to dst, creating the parent directory if it does not exist
Parameters
- srcstr or Path
Source file
- dststr or Path
Destination file
- iwutil.this_dir(file)
Get the directory of the file
- iwutil.append_path(path)
Append a path to the current path
Parameters
- pathstr or Path
Path to append
- iwutil.random.current_time_integer() int
Get the current time as an integer in the format %Y%m%d%H%M%S%f
- iwutil.random.generate_seed() int
Get a new numpy seed from the current time. Numpy seeds are signed 32-bit integers.
- iwutil.random.seed(value: int | None = None)
Set the random seed for numpy to a random number.
- iwutil.random.get_seed() int
Get the random seed for numpy.
- iwutil.save.create_folder(filename)
Create a folder if it does not exist
Parameters
- filenamestr
File name in the folder to create
- iwutil.save.json(params, filename)
Save params to a json file
Parameters
- paramsdict
Dictionary of parameters
- filenamestr
Full path and name of the file to save
- iwutil.save.csv(df, filename)
- iwutil.save.csv(df: DataFrame, filename)
- iwutil.save.csv(df: DataFrame, filename)
Save df to a csv file
Parameters
- dfpandas.DataFrame or polars.DataFrame
DataFrame to save
- filenamestr
Full path and name of the file to save
- iwutil.save.parquet(df, filename)
- iwutil.save.parquet(df: DataFrame, filename)
- iwutil.save.parquet(df: DataFrame, filename)
Save df to a parquet file
Parameters
- dfpandas.DataFrame or polars.DataFrame
DataFrame to save
- filenamestr
Full path and name of the file to save