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

args.py

Blame
  • user avatar
    Loïc Lehnhoff authored
    +Added matplotlib.pyplot shortcuts
    48ded44d
    History
    args.py 3.86 KiB
    ##### IMPORTATIONS #####
    import os
    import sys
    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.
    	modify_file : str or False
    		Path to a json file containing annotations.
    	from_wav : str or False
    		Path to a wavefile.
    	"""
    
    	# setting up arg parser
    	parser = argparse.ArgumentParser(
    		formatter_class=RawTextHelpFormatter,
    		description=
    		("This script requires libraries that can be installed with: \n"
    		"'$ pip install -r requirements.txt'"
    		"Read README.md to get an overview of this software.")
    	)
    
    	parser.add_argument(
    		'-dir', '--directory',
    		type=str,
    		default="./audio_examples",
    		nargs='?', 
    		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=1,
    		nargs='?', 
    		required=False,
    		help=("Default number of contours available at launcg."
    		"\nDue to restrictions with the module used, max number of contours is 99 per file.\n\n")
    	)
    
    	parser.add_argument(
    		'-new', '--resampling_rate',
    		type=int,
    		default=96_000,
    		nargs='?', 
    		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='?', 
    		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")
    	)
    
    	parser.add_argument(
    		'-m', '--modify',
    		type=str,
    		nargs=2,
    		required=False,
    		help=("Name of a file generated by a previous annotation," 
    			"\nand name of the associated wavefile."
    			"\nThis will open the interface with the contours from this file"
    			"\nand enable modification of these contours."))
    
    	parser.add_argument(
    		'-p', '--parameters',
    		type=str,
    		nargs="?",
    		required=False,
    		default=None,
    		help=("Path to a file with spectrogram parameters." 
    			"\nMust come from a previous usage of PyAVA."
    			"\nParameters loaded from this file will overwrite default parameters."))
    
    	# fetching arguments
    	args = parser.parse_args()
    	outputs = args.output
    	explore = args.directory
    	contour = args.max_contours
    	resampl = args.resampling_rate
    	if args.modify == None:
    		modify_file, from_wav = False, False
    	else:
    		modify_file, from_wav = args.modify
    
    
    	# verifying arguments
    	try:
    		assert (os.path.exists(outputs)), (
    			f"\nInputError: Could not find dir '{outputs}'.")
    		if isinstance(args.parameters, str):
    			assert (os.path.exists(args.parameters)), (
    				f"\nInputError: Could not find dir '{args.parameters}'.")
    		assert (os.path.exists(explore)), (
    			f"\nInputError: Could not find dir '{explore}'.")
    		if modify_file:
    			assert (os.path.exists(os.path.join(outputs, modify_file))), (
    				f"\nInputError: Could not find file '{modify_file}' in '{outputs}'.")
    			assert (os.path.exists(os.path.join(explore, from_wav))), (
    				f"\nInputError: Could not find file '{from_wav}' in '{explore}'.")
    		assert (contour <= 50), (
    			f"\nInputError: Max number of contours cannot exceed 50, got {contour}.")
    	except Exception as e:
    		print(e)
    		sys.exit(1)
    
    	return (explore, contour, resampl, outputs, modify_file, from_wav, args.parameters)
    
    # if running `$python ARGS.py -h` for help.
    if __name__ == '__main__':
    	fetch_inputs()