Created
June 16, 2014 08:31
-
-
Save kmkurn/32d3978331401b3fd07c to your computer and use it in GitHub Desktop.
Parser builder for common script arguments for Pattern Information Processing course assignments.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 | |
""" | |
Parser builder for common script arguments. | |
""" | |
import argparse | |
import numpy as np | |
__all__ = ['target_fn', 'build_parser'] | |
target_fn = dict( | |
sin=np.sin, | |
sin2=lambda x: np.sin(2*x), | |
lin=lambda x: 2*x + 1, | |
quad=lambda x: (x+1) * (x-2), | |
cube=lambda x: (x+1) * (x-1) * (x-3), | |
exp=np.exp | |
) | |
def build_parser(): | |
"""Build argument parser for common script arguments.""" | |
# Build argument parser | |
parser = argparse.ArgumentParser() | |
# Learning-related arguments | |
parser.add_argument('-N', '--sample-size', type=int, default=32, | |
help='Sample size') | |
parser.add_argument('-l', '--lagrange-multiplier', type=float, default=0.1, | |
help='Lagrange multiplier') | |
parser.add_argument('-b', '--bandwith-size', type=float, default=0.25, | |
help='Bandwith value for Gaussian kernel') | |
parser.add_argument('-n', '--noise-level', type=float, default=0.05, | |
help='Maximum noise value') | |
parser.add_argument('-f', '--target-function', type=str, default='sin', | |
choices=target_fn.keys(), help='Target function') | |
parser.add_argument('-lc', '--lagrange-candidate', type=float, nargs='*', | |
help='Lagrange multipliers candidates to search') | |
parser.add_argument('-bc', '--bandwith-candidate', type=float, nargs='*', | |
help='Bandwith size candidates to search') | |
parser.add_argument('-k', '--num-fold', type=int, default=2, | |
help='Number of folds in cross-validation') | |
parser.add_argument('--outlier', action='store_true', | |
help='Insert an outlier') | |
# Drawing-related arguments | |
parser.add_argument('--max-n', type=int, default=1024, | |
help='Maximum number of points to draw') | |
parser.add_argument('-lb', '--left-bound', type=float, default=-5, | |
help='Left boundary of x-axis') | |
parser.add_argument('-rb', '--right-bound', type=float, default=5, | |
help='Right boundary of x-axis') | |
parser.add_argument('--show-legend', action='store_true', | |
help='Show legend in the plot') | |
parser.add_argument('--save-as', type=str, default=None, | |
help='Filename to save') | |
return parser |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment