src.slune.slune
1from typing import List, Optional, Union 2from slune.base import BaseSearcher, BaseSaver 3import subprocess 4import sys 5from slune.savers.csv import SaverCsv 6from slune.loggers.default import LoggerDefault 7 8def submit_job(sh_path: str, args: List[str]): 9 """ Submits a job using specified Bash script 10 11 Args: 12 - sh_path (string): Path to the Bash script to be run. 13 14 - args (list of str): List of strings containing the arguments to be passed to the Bash script. 15 16 """ 17 18 try: 19 # Run the Bash script using subprocess 20 command = [sh_path] + args 21 subprocess.run(['sbatch'] + command, check=True) 22 except subprocess.CalledProcessError as e: 23 print(f"Error running sbatch: {e}") 24 25def sbatchit(script_path: str, sbatch_path: str, searcher: BaseSearcher, cargs: Optional[List]=[], saver: Optional[BaseSaver]=None): 26 """ Submits jobs based on arguments given by searcher. 27 28 For each job runs the script stored at script_path with selected parameter values given by searcher 29 and the arguments given by cargs. 30 31 Uses the sbatch script with path sbatch_path to submit each job to the cluster. 32 33 If given a Saver object, uses it to check if there are existing runs for each job and skips them, 34 based on the number of runs we would like for each job (which is stored in the saver). 35 36 Args: 37 - script_path (str): Path to the script (of the model) to be run for each job. 38 39 - sbatch_path (str): Path to the sbatch script that will be used to submit each job. 40 Examples of sbatch scripts can be found in the templates folder. 41 42 - searcher (Searcher): Searcher object used to retrieve changing arguments for each job. 43 44 - cargs (list, optional): Contains arguments to be passed to the script for every job. 45 46 - saver (Saver, optional): Saver object used if we want to check if there are existing runs so we don't rerun. 47 Can simply not give a Saver object if you want to rerun all jobs. 48 49 """ 50 51 if saver != None: 52 searcher.check_existing_runs(saver) 53 # Create sbatch script for each job 54 for args in searcher: 55 # Submit job 56 submit_job(sbatch_path, [script_path] + cargs + args) 57 58def lsargs() -> (str, List[str]): 59 """ Returns the script name and a list of the arguments passed to the script.""" 60 args = sys.argv 61 return args[0], args[1:] 62 63def garg(args: List[str], arg_names: Union[str, List[str]]) -> Union[str, List[str]]: 64 """ Finds the argument/s with name arg_names in the list of arguments args_ls and returns its value/s. 65 66 Args: 67 - args (list of str): List of strings containing the arguments to be searched. 68 69 - arg_names (str or list of str): String or list of strings containing the names of the arguments to be searched for. 70 71 Returns: 72 - arg_value (str or list of str): String or list of strings containing the values of the arguments found. 73 74 """ 75 76 def single_garg(arg_name): 77 # Check if arg_name is a string 78 if type(arg_name) != str: 79 raise TypeError(f"arg_name must be a string, got {type(arg_name)}") 80 # Find index of argument 81 arg_index = [i for i, arg in enumerate(args) if arg_name in arg] 82 # Return value error if argument not found 83 if not arg_index: 84 raise ValueError(f"Argument {arg_name} not found in arguments {args}") 85 # Return value of argument 86 if len(arg_index) > 1: 87 raise ValueError(f"Multiple arguments with name {arg_name} found in arguments {args}") 88 return args[arg_index[0]].split("=")[1] 89 if type(arg_names) == list: 90 return [single_garg(arg_name) for arg_name in arg_names] 91 else: 92 return single_garg(arg_names) 93 94def get_csv_slog(params: Optional[dict]= None, root_dir: Optional[str]='slune_results') -> BaseSaver: 95 """ Returns a SaverCsv object with the given parameters and root directory. 96 97 Args: 98 - params (dict, optional): Dictionary of parameters to be passed to the SaverCsv object, default is None. 99 100 - root_dir (str, optional): Path to the root directory to be used by the SaverCsv object, default is 'slune_results'. 101 102 Returns: 103 - SaverCsv (Saver): Saver object with the given parameters and root directory. 104 Initialized with a LoggerDefault object as its logger. 105 106 """ 107 108 return SaverCsv(LoggerDefault(), params = params, root_dir=root_dir)
def
submit_job(sh_path: str, args: List[str]):
9def submit_job(sh_path: str, args: List[str]): 10 """ Submits a job using specified Bash script 11 12 Args: 13 - sh_path (string): Path to the Bash script to be run. 14 15 - args (list of str): List of strings containing the arguments to be passed to the Bash script. 16 17 """ 18 19 try: 20 # Run the Bash script using subprocess 21 command = [sh_path] + args 22 subprocess.run(['sbatch'] + command, check=True) 23 except subprocess.CalledProcessError as e: 24 print(f"Error running sbatch: {e}")
Submits a job using specified Bash script
Arguments:
- - sh_path (string): Path to the Bash script to be run.
- - args (list of str): List of strings containing the arguments to be passed to the Bash script.
def
sbatchit( script_path: str, sbatch_path: str, searcher: slune.base.BaseSearcher, cargs: Optional[List] = [], saver: Optional[slune.base.BaseSaver] = None):
26def sbatchit(script_path: str, sbatch_path: str, searcher: BaseSearcher, cargs: Optional[List]=[], saver: Optional[BaseSaver]=None): 27 """ Submits jobs based on arguments given by searcher. 28 29 For each job runs the script stored at script_path with selected parameter values given by searcher 30 and the arguments given by cargs. 31 32 Uses the sbatch script with path sbatch_path to submit each job to the cluster. 33 34 If given a Saver object, uses it to check if there are existing runs for each job and skips them, 35 based on the number of runs we would like for each job (which is stored in the saver). 36 37 Args: 38 - script_path (str): Path to the script (of the model) to be run for each job. 39 40 - sbatch_path (str): Path to the sbatch script that will be used to submit each job. 41 Examples of sbatch scripts can be found in the templates folder. 42 43 - searcher (Searcher): Searcher object used to retrieve changing arguments for each job. 44 45 - cargs (list, optional): Contains arguments to be passed to the script for every job. 46 47 - saver (Saver, optional): Saver object used if we want to check if there are existing runs so we don't rerun. 48 Can simply not give a Saver object if you want to rerun all jobs. 49 50 """ 51 52 if saver != None: 53 searcher.check_existing_runs(saver) 54 # Create sbatch script for each job 55 for args in searcher: 56 # Submit job 57 submit_job(sbatch_path, [script_path] + cargs + args)
Submits jobs based on arguments given by searcher.
For each job runs the script stored at script_path with selected parameter values given by searcher and the arguments given by cargs.
Uses the sbatch script with path sbatch_path to submit each job to the cluster.
If given a Saver object, uses it to check if there are existing runs for each job and skips them, based on the number of runs we would like for each job (which is stored in the saver).
Arguments:
- - script_path (str): Path to the script (of the model) to be run for each job.
- - sbatch_path (str): Path to the sbatch script that will be used to submit each job. Examples of sbatch scripts can be found in the templates folder.
- - searcher (Searcher): Searcher object used to retrieve changing arguments for each job.
- - cargs (list, optional): Contains arguments to be passed to the script for every job.
- - saver (Saver, optional): Saver object used if we want to check if there are existing runs so we don't rerun. Can simply not give a Saver object if you want to rerun all jobs.
def
lsargs() -> (<class 'str'>, typing.List[str]):
59def lsargs() -> (str, List[str]): 60 """ Returns the script name and a list of the arguments passed to the script.""" 61 args = sys.argv 62 return args[0], args[1:]
Returns the script name and a list of the arguments passed to the script.
def
garg( args: List[str], arg_names: Union[str, List[str]]) -> Union[str, List[str]]:
64def garg(args: List[str], arg_names: Union[str, List[str]]) -> Union[str, List[str]]: 65 """ Finds the argument/s with name arg_names in the list of arguments args_ls and returns its value/s. 66 67 Args: 68 - args (list of str): List of strings containing the arguments to be searched. 69 70 - arg_names (str or list of str): String or list of strings containing the names of the arguments to be searched for. 71 72 Returns: 73 - arg_value (str or list of str): String or list of strings containing the values of the arguments found. 74 75 """ 76 77 def single_garg(arg_name): 78 # Check if arg_name is a string 79 if type(arg_name) != str: 80 raise TypeError(f"arg_name must be a string, got {type(arg_name)}") 81 # Find index of argument 82 arg_index = [i for i, arg in enumerate(args) if arg_name in arg] 83 # Return value error if argument not found 84 if not arg_index: 85 raise ValueError(f"Argument {arg_name} not found in arguments {args}") 86 # Return value of argument 87 if len(arg_index) > 1: 88 raise ValueError(f"Multiple arguments with name {arg_name} found in arguments {args}") 89 return args[arg_index[0]].split("=")[1] 90 if type(arg_names) == list: 91 return [single_garg(arg_name) for arg_name in arg_names] 92 else: 93 return single_garg(arg_names)
Finds the argument/s with name arg_names in the list of arguments args_ls and returns its value/s.
Arguments:
- - args (list of str): List of strings containing the arguments to be searched.
- - arg_names (str or list of str): String or list of strings containing the names of the arguments to be searched for.
Returns:
- arg_value (str or list of str): String or list of strings containing the values of the arguments found.
def
get_csv_slog( params: Optional[dict] = None, root_dir: Optional[str] = 'slune_results') -> slune.base.BaseSaver:
95def get_csv_slog(params: Optional[dict]= None, root_dir: Optional[str]='slune_results') -> BaseSaver: 96 """ Returns a SaverCsv object with the given parameters and root directory. 97 98 Args: 99 - params (dict, optional): Dictionary of parameters to be passed to the SaverCsv object, default is None. 100 101 - root_dir (str, optional): Path to the root directory to be used by the SaverCsv object, default is 'slune_results'. 102 103 Returns: 104 - SaverCsv (Saver): Saver object with the given parameters and root directory. 105 Initialized with a LoggerDefault object as its logger. 106 107 """ 108 109 return SaverCsv(LoggerDefault(), params = params, root_dir=root_dir)
Returns a SaverCsv object with the given parameters and root directory.
Arguments:
- - params (dict, optional): Dictionary of parameters to be passed to the SaverCsv object, default is None.
- - root_dir (str, optional): Path to the root directory to be used by the SaverCsv object, default is 'slune_results'.
Returns:
- SaverCsv (Saver): Saver object with the given parameters and root directory. Initialized with a LoggerDefault object as its logger.