Skip to content
Snippets Groups Projects
Select Git revision
  • fc714356abfaabf0a8934ed46039ee43356465eb
  • main default protected
2 results

args.py

Blame
  • user avatar
    Loic-Lenof authored
    fc714356
    History
    args.py 2.66 KiB
    ##### IMPORTATIONS #####
    import os
    import argparse
    from argparse import RawTextHelpFormatter
    
    
    ##### ARGPARSER #####
    def fetch_inputs():
    	"""
    	A function to fetch inputs from cmd terminal. 
    	Run `$python ARGS.py -h` for more information.
    
    	...
    
    	Parameters
    	----------
    	None : Inputs are fetched from cmd terminal as arguments.
    
    	Return
    	------
    	explore : str
    		Path to a directory containing wavefiles.
    	contour : int
    		Maximum number of contours that can be drawn at once.
    	resampl : int
    		Resampling rate for audio data.
    	outputs : int
    		Path to folder that will contain outputs.
    	"""
    
    	# setting up arg parser
    	parser = argparse.ArgumentParser(
    		formatter_class=RawTextHelpFormatter,
    		description=
    		("This script requires LIBRARIES."
    		"\nAnd it does things, TO BE DESCRIBED.")
    	)
    
    	parser.add_argument(
    		'-dir', '--directory',
    		type=str,
    		default=".",
    		nargs=1, 
    		required=False,
    		help=("Directory in which the file explorer will open."
    		"\nPick any .wav file to begin annotation. "
    		"\nDefault value is '.' (current Directory).\n\n")
    	)
    
    	parser.add_argument(
    		'-max', '--max_contours',
    		type=int,
    		default=15,
    		nargs=1, 
    		required=False,
    		help=("Number of contours that can be annotated at the same time."
    		"\nDue to restrictions with the module used,"
    		"\nonly a fixed number of contours can be annotated at once."
    		"\nValue cannot exceed 50. Default value is '15'.\n\n")
    	)
    
    	parser.add_argument(
    		'-new', '--resampling_rate',
    		type=int,
    		default=96_000,
    		nargs=1, 
    		required=False,
    		help=("Resampling rate for the Wavefile. "
    		"\nCan be used to speed up spectrogram visualisation. "
    		"\nDefault value is '96,000'kHz.\n\n")
    	)
    
    	parser.add_argument(
    		'-out', '--output', 
    		type=str,
    		nargs=1, 
    		default=os.path.join(".","outputs"),
    		required=False,
    		help=("Path where the contours will be saved."
    		"\nContours will be saved in a .json file, different for each wavefile."
    		"\nDefault value is './outputs'.\n\n")
    	)
    
    	# fetching arguments
    	args = parser.parse_args()
    	outputs = args.output
    	explore = args.directory
    	contour = args.max_contours
    	resampl = args.resampling_rate
    
    	# verifying arguments
    	try:
    		assert (os.path.exists(outputs)), (
    			f"\nInputError: Could not find dir {outputs}."
    			"\n\tCreate a folder that will contain outputs,"
    			" or type in an existing folder with `-out folder_name`.")
    		assert (os.path.exists(explore)), (
    			f"\nInputError: Could not find dir {explore}.")
    		assert (contour <= 50), (
    			f"\nInputError: Max number of contours cannot exceed 50, got {contour}.")
    	except Exception as e:
    		print(e)
    		exit()
    
    	return (explore, contour, resampl, outputs)
    
    # if running `$python ARGS.py -h` for help.
    if __name__ == '__main__':
    	fetch_inputs()