Пример #1
0
def handle_args(fig_args=None, plot_args=None, scatter_args=None, axis_args=None, fill_args=None,
                legend_args=None, date_args=None, show_args=None, mpl_args=None, **kwargs):
    ''' Handle input arguments -- merge user input with defaults; see sim.plot for documentation '''

    # Set defaults
    defaults = sc.objdict()
    defaults.fig     = sc.objdict(figsize=(10, 8))
    defaults.plot    = sc.objdict(lw=1.5, alpha= 0.7)
    defaults.scatter = sc.objdict(s=20, marker='s', alpha=0.7, zorder=0)
    defaults.axis    = sc.objdict(left=0.10, bottom=0.08, right=0.95, top=0.95, wspace=0.30, hspace=0.30)
    defaults.fill    = sc.objdict(alpha=0.2)
    defaults.legend  = sc.objdict(loc='best', frameon=False)
    defaults.date    = sc.objdict(as_dates=True, dateformat=None, interval=None, rotation=None, start_day=None, end_day=None)
    defaults.show    = sc.objdict(data=True, ticks=True, interventions=True, legend=True)
    defaults.mpl     = sc.objdict(dpi=None, fontsize=None, fontfamily=None) # Use Covasim global defaults

    # Handle directly supplied kwargs
    for dkey,default in defaults.items():
        keys = list(kwargs.keys())
        for kw in keys:
            if kw in default.keys():
                default[kw] = kwargs.pop(kw)

    # Merge arguments together
    args = sc.objdict()
    args.fig     = sc.mergedicts(defaults.fig,     fig_args)
    args.plot    = sc.mergedicts(defaults.plot,    plot_args)
    args.scatter = sc.mergedicts(defaults.scatter, scatter_args)
    args.axis    = sc.mergedicts(defaults.axis,    axis_args)
    args.fill    = sc.mergedicts(defaults.fill,    fill_args)
    args.legend  = sc.mergedicts(defaults.legend,  legend_args)
    args.date    = sc.mergedicts(defaults.date,    fill_args)
    args.show    = sc.mergedicts(defaults.show,    show_args)
    args.mpl     = sc.mergedicts(defaults.mpl,     mpl_args)

    # If unused keyword arguments remain, raise an error
    if len(kwargs):
        notfound = sc.strjoin(kwargs.keys())
        valid = sc.strjoin(sorted(set([k for d in defaults.values() for k in d.keys()]))) # Remove duplicates and order
        errormsg = f'The following keywords could not be processed:\n{notfound}\n\n'
        errormsg += f'Valid keywords are:\n{valid}\n\n'
        errormsg += 'For more precise plotting control, use fig_args, plot_args, etc.'
        raise sc.KeyNotFoundError(errormsg)

    # Handle what to show
    show_keys = defaults.show.keys()
    args.show = {k:True for k in show_keys}
    if show_args in [True, False]: # Handle all on or all off
        args.show = {k:show_args for k in show_keys}
    else:
        args.show = sc.mergedicts(args.show, show_args)

    # Handle global Matplotlib arguments
    args.mpl_orig = sc.objdict()
    for key,value in args.mpl.items():
        if value is not None:
            args.mpl_orig[key] = cvset.options.get(key)
            cvset.options.set(key, value)

    return args
Пример #2
0
def handle_args(fig_args=None,
                plot_args=None,
                scatter_args=None,
                axis_args=None,
                fill_args=None,
                legend_args=None,
                date_args=None,
                show_args=None,
                style_args=None,
                **kwargs):
    ''' Handle input arguments -- merge user input with defaults; see sim.plot for documentation '''

    # Set defaults
    defaults = sc.objdict()
    defaults.fig = sc.objdict(figsize=(10, 8), num=None)
    defaults.plot = sc.objdict(lw=1.5, alpha=0.7)
    defaults.scatter = sc.objdict(
        s=20, marker='s', alpha=0.7, zorder=1.75,
        datastride=1)  # NB: 1.75 is above grid lines but below plots
    defaults.axis = sc.objdict(left=0.10,
                               bottom=0.08,
                               right=0.95,
                               top=0.95,
                               wspace=0.30,
                               hspace=0.30)
    defaults.fill = sc.objdict(alpha=0.2)
    defaults.legend = sc.objdict(loc='best', frameon=False)
    defaults.date = sc.objdict(as_dates=True,
                               dateformat=None,
                               rotation=None,
                               start=None,
                               end=None)
    defaults.show = sc.objdict(data=True,
                               ticks=True,
                               interventions=True,
                               legend=True,
                               outer=False,
                               tight=False,
                               maximize=False)
    defaults.style = sc.objdict(style=None,
                                dpi=None,
                                font=None,
                                fontsize=None,
                                grid=None,
                                facecolor=None)  # Use Covasim global defaults

    # Handle directly supplied kwargs
    for dkey, default in defaults.items():
        keys = list(kwargs.keys())
        for kw in keys:
            if kw in default.keys():
                default[kw] = kwargs.pop(kw)

    # Merge arguments together
    args = sc.objdict()
    args.fig = sc.mergedicts(defaults.fig, fig_args)
    args.plot = sc.mergedicts(defaults.plot, plot_args)
    args.scatter = sc.mergedicts(defaults.scatter, scatter_args)
    args.axis = sc.mergedicts(defaults.axis, axis_args)
    args.fill = sc.mergedicts(defaults.fill, fill_args)
    args.legend = sc.mergedicts(defaults.legend, legend_args)
    args.date = sc.mergedicts(defaults.date, date_args)
    args.show = sc.mergedicts(defaults.show, show_args)
    args.style = sc.mergedicts(defaults.style, style_args)

    # Handle potential rcParams keys
    keys = list(kwargs.keys())
    for key in keys:
        if key in pl.rcParams:
            args.style[key] = kwargs.pop(key)

    # If unused keyword arguments remain, parse or raise an error
    if len(kwargs):

        # Everything remaining is not found
        notfound = sc.strjoin(kwargs.keys())
        valid = sc.strjoin(
            sorted(set([k for d in defaults.values()
                        for k in d.keys()])))  # Remove duplicates and order
        errormsg = f'The following keywords could not be processed:\n{notfound}\n\n'
        errormsg += f'Valid keywords are:\n{valid}\n\n'
        errormsg += 'For more precise plotting control, use fig_args, plot_args, etc.'
        raise sc.KeyNotFoundError(errormsg)

    # Handle what to show
    show_keys = ['data', 'ticks', 'interventions', 'legend']
    if show_args in [True, False]:  # Handle all on or all off
        for k in show_keys:
            args.show[k] = show_args

    return args