示例#1
0
def init_param_cont(args):
    """
    init_param_cont(args)

    Initializes parameter containers.

    Returns args:
        - in the following nametuples: analyspar, sesspar, stimpar, autocorr, 
                                       permpar, quantpar
        - in the following dictionary: figpar 

    Required args:
        - args (Argument parser): parser with the following attributes:

            visflow_dir (str or list): visual flow direction values to include
                                     ("right", "left", ["right", "left"])
            visflow_size (int or list): visual flow size values to include
                                     (128, 256 or [128, 256])
            closest (bool)         : if False, only exact session number is 
                                     retained, otherwise the closest.
            error (str)            : error statistic parameter ("std" or "sem")
            fontdir (str)          : path to directory containing additional 
                                     fonts
            gabfr (int)            : gabor frame at which sequences start 
                                     (0, 1, 2, 3)
            gabk (int or list)     : gabor kappa values to include 
                                     (4, 16 or [4, 16])
            gab_ori (int or list)  : gabor orientation values to include
                                     ([0, 45, 90, 135, 180, 225])
            incl (str)             : sessions to include ("yes", "no", "all")     
            keepnans (str)         : if True, the original running array is 
                                     used instead of the one where NaNs
                                     are interpolated.
            lag_s (num)            : lag for autocorrelation (in sec)
            line (str)             : line ("L23", "L5", "any")
            min_rois (int)         : min number of ROIs
            n_perms (int)          : nbr of permutations to run
            n_quants (int)         : number of quantiles
            ncols (int)            : number of columns
            no_datetime (bool)     : if True, figures are not saved in a 
                                     subfolder named based on the date and time.
            no_sharey (bool)       : if True, sharey figure parameter is set to 
                                     False.
            not_save_fig (bool)    : if True, figures are not saved
            output (str)           : general directory in which to save output
            overwrite (bool)       : if False, overwriting existing figures 
                                     is prevented by adding suffix numbers.
            pass_fail (str or list): pass/fail values of interest ("P", "F")
            plane (str)            : plane ("soma", "dend", "any")
            plt_bkend (str)        : mpl backend to use
            post (num)             : range of frames to include after each 
                                     reference frame (in s)
            pre (num)              : range of frames to include before each 
                                     reference frame (in s)
            runtype (str or list)  : runtype ("pilot" or "prod")
            scale (bool)           : whether to scale running data
            sess_n (int)           : session number
            stats (str)            : statistic parameter ("mean" or "median")
            stimtype (str)         : stimulus to analyse ("visflow" or "gabors")
            tails (str or int)     : which tail(s) to test ("hi", "lo", 2)

    Returns:
        - analysis_dict (dict): dictionary of analysis parameters
            ["analyspar"] (AnalysPar)    : named tuple of analysis parameters
            ["sesspar"] (SessPar)        : named tuple of session parameters
            ["stimpar"] (StimPar)        : named tuple of stimulus parameters
            ["autocorrpar"] (AutocorrPar): named tuple of autocorrelation 
                                           parameters
            ["permpar"] (PermPar)        : named tuple of permutation parameters
            ["quantpar"] (QuantPar)      : named tuple of quantile parameters
            ["figpar"] (dict)            : dictionary containing following 
                                           subdictionaries:
                ["init"]: dict with following inputs as attributes:
                    ["ncols"] (int)      : number of columns in the figures
                    ["sharex"] (bool)    : if True, x axis lims are shared 
                                           across subplots
                    ["sharey"] (bool)    : if True, y axis lims are shared 
                                           across subplots
                    ["subplot_hei"] (num): height of each subplot (inches)
                    ["subplot_wid"] (num): width of each subplot (inches)

                ["save"]: dict with the following inputs as attributes:
                    ["datetime"] (bool) : if True, figures are saved in a  
                                          subfolder named based on the date and 
                                          time.
                    ["fig_ext"] (str)   : figure extension
                    ["overwrite"] (bool): if True, existing figures can be 
                                          overwritten
                    ["save_fig"] (bool) : if True, figures are saved
                    ["use_dt"] (str)    : datetime folder to use

                ["dirs"]: dict with the following attributes:
                    ["figdir"] (str)   : main folder in which to save figures
                    ["roi"] (str)      : subdirectory name for ROI analyses
                    ["run"] (str)      : subdirectory name for running analyses
                    ["autocorr"] (str) : subdirectory name for autocorrelation 
                                         analyses
                    ["locori"] (str)   : subdirectory name for location and 
                                         orientation responses
                    ["oridir"] (str)   : subdirectory name for 
                                         orientation/direction analyses
                    ["unexp_qu"] (str)  : subdirectory name for unexpected, 
                                         quantile analyses
                    ["tune_curv"] (str): subdirectory name for tuning curves
                    ["grped"] (str)    : subdirectory name for ROI grps data
                    ["mags"] (str)     : subdirectory name for magnitude 
                                         analyses
                
                ["mng"]: dict with the following attributes:
                    ["plt_bkend"] (str): mpl backend to use
                    ["linclab"] (bool) : if True, Linclab mpl defaults are used
                    ["fontdir"] (str)  : path to directory containing 
                                         additional fonts
    """

    args = copy.deepcopy(args)

    analysis_dict = dict()

    # analysis parameters
    analysis_dict["analyspar"] = sess_ntuple_util.init_analyspar(
        "n/a", not (args.keepnans), args.stats, args.error, args.scale)

    # session parameters
    analysis_dict["sesspar"] = sess_ntuple_util.init_sesspar(
        args.sess_n, args.closest, args.plane, args.line, args.min_rois,
        args.pass_fail, args.incl, args.runtype)

    # stimulus parameters
    analysis_dict["stimpar"] = sess_ntuple_util.init_stimpar(
        args.stimtype, args.visflow_dir, args.visflow_size, args.gabfr,
        args.gabk, args.gab_ori, args.pre, args.post)

    # SPECIFIC ANALYSES
    # autocorrelation parameters
    analysis_dict["autocorrpar"] = sess_ntuple_util.init_autocorrpar(
        args.lag_s, byitem=False)

    # permutation parameters
    analysis_dict["permpar"] = sess_ntuple_util.init_permpar(
        args.n_perms, 0.05, args.tails, False)

    # quantile parameters
    analysis_dict["quantpar"] = sess_ntuple_util.init_quantpar(
        args.n_quants, [0, -1])

    # figure parameters
    analysis_dict["figpar"] = sess_plot_util.init_figpar(
        ncols=int(args.ncols),
        datetime=not (args.no_datetime),
        overwrite=args.overwrite,
        save_fig=not (args.not_save_fig),
        runtype=args.runtype,
        output=args.output,
        plt_bkend=args.plt_bkend,
        fontdir=args.fontdir,
        sharey=not (args.no_sharey))

    return analysis_dict
def run_regr(args):
    """
    run_regr(args)

    Does runs of a logistic regressions on the specified comparison and range
    of sessions.
    
    Required args:
        - args (Argument parser): parser with analysis parameters as attributes:
            alg (str)             : algorithm to use ("sklearn" or "pytorch")
            bal (bool)            : if True, classes are balanced
            batchsize (int)       : nbr of samples dataloader will load per 
                                    batch (for "pytorch" alg)
            visflow_dir (str)     : visual flow direction to analyse
            visflow_per (float)   : number of seconds to include before visual 
                                    flow segments
            visflow_size (int or list): visual flow square sizes to include
            comp (str)            : type of comparison
            datadir (str)         : data directory
            dend (str)            : type of dendrites to use ("allen" or "dend")
            device (str)          : device name (i.e., "cuda" or "cpu")
            ep_freq (int)         : frequency at which to log loss to 
                                    console
            error (str)           : error to take, i.e., "std" (for std 
                                    or quantiles) or "sem" (for SEM or MAD)
            fluor (str)           : fluorescence trace type
            fontdir (str)         : directory in which additional fonts are 
                                    located
            gabfr (int)           : gabor frame of reference if comparison 
                                    is "unexp"
            gabk (int or list)    : gabor kappas to include
            gab_ori (list or str) : gabor orientations to include
            incl (str or list)    : sessions to include ("yes", "no", "all")
            lr (num)              : model learning rate (for "pytorch" alg)
            mouse_n (int)         : mouse number
            n_epochs (int)        : number of epochs
            n_reg (int)           : number of regular runs
            n_shuff (int)         : number of shuffled runs
            scale (bool)          : if True, each ROI is scaled
            output (str)          : general directory in which to save 
                                    output
            parallel (bool)       : if True, runs are done in parallel
            plt_bkend (str)       : pyplot backend to use
            q1v4 (bool)           : if True, analysis is trained on first and 
                                    tested on last quartiles
            exp_v_unexp (bool)    : if True, analysis is trained on 
                                    expected and tested on unexpected sequences
            runtype (str)         : type of run ("prod" or "pilot")
            seed (int)            : seed to seed random processes with
            sess_n (int)          : session number
            stats (str)           : stats to take, i.e., "mean" or "median"
            stimtype (str)        : stim to analyse ("gabors" or "visflow")
            train_p (list)        : proportion of dataset to allocate to 
                                    training
            uniqueid (str or int) : unique ID for analysis
            wd (float)            : weight decay value (for "pytorch" arg)
    """

    args = copy.deepcopy(args)

    if args.datadir is None:
        args.datadir = DEFAULT_DATADIR
    else:
        args.datadir = Path(args.datadir)

    if args.uniqueid == "datetime":
        args.uniqueid = gen_util.create_time_str()
    elif args.uniqueid in ["None", "none"]:
        args.uniqueid = None

    reseed = False
    if args.seed in [None, "None"]:
        reseed = True

    # deal with parameters
    extrapar = {"uniqueid": args.uniqueid, "seed": args.seed}

    techpar = {
        "reseed": reseed,
        "device": args.device,
        "alg": args.alg,
        "parallel": args.parallel,
        "plt_bkend": args.plt_bkend,
        "fontdir": args.fontdir,
        "output": args.output,
        "ep_freq": args.ep_freq,
        "n_reg": args.n_reg,
        "n_shuff": args.n_shuff,
    }

    mouse_df = DEFAULT_MOUSE_DF_PATH

    stimpar = logreg.get_stimpar(args.comp,
                                 args.stimtype,
                                 args.visflow_dir,
                                 args.visflow_size,
                                 args.gabfr,
                                 args.gabk,
                                 gab_ori=args.gab_ori,
                                 visflow_pre=args.visflow_pre)

    analyspar = sess_ntuple_util.init_analyspar(args.fluor,
                                                stats=args.stats,
                                                error=args.error,
                                                scale=not (args.no_scale),
                                                dend=args.dend)

    if args.q1v4:
        quantpar = sess_ntuple_util.init_quantpar(4, [0, -1])
    else:
        quantpar = sess_ntuple_util.init_quantpar(1, 0)

    logregpar = sess_ntuple_util.init_logregpar(args.comp, not (args.not_ctrl),
                                                args.q1v4, args.exp_v_unexp,
                                                args.n_epochs, args.batchsize,
                                                args.lr, args.train_p, args.wd,
                                                args.bal, args.alg)

    omit_sess, omit_mice = sess_gen_util.all_omit(stimpar.stimtype,
                                                  args.runtype,
                                                  stimpar.visflow_dir,
                                                  stimpar.visflow_size,
                                                  stimpar.gabk)

    sessids = sorted(
        sess_gen_util.get_sess_vals(mouse_df,
                                    "sessid",
                                    args.mouse_n,
                                    args.sess_n,
                                    args.runtype,
                                    incl=args.incl,
                                    omit_sess=omit_sess,
                                    omit_mice=omit_mice))

    if len(sessids) == 0:
        logger.warning(
            f"No sessions found (mouse: {args.mouse_n}, sess: {args.sess_n}, "
            f"runtype: {args.runtype})")

    for sessid in sessids:
        sess = sess_gen_util.init_sessions(sessid,
                                           args.datadir,
                                           mouse_df,
                                           args.runtype,
                                           full_table=False,
                                           fluor=analyspar.fluor,
                                           dend=analyspar.dend,
                                           temp_log="warning")[0]
        logreg.run_regr(sess, analyspar, stimpar, logregpar, quantpar,
                        extrapar, techpar)

        plot_util.cond_close_figs()
示例#3
0
def init_analysis(args):
    """
    init_analysis(args)

    Initializes analysis parameters based on input arguments containers.

    Required args:
        - args (dict): 
            parser argument dictionary

    Returns:
        - analysis_dict (dict): 
            dictionary of analysis parameters
            ["analyspar"] (AnalysPar): named tuple of analysis parameters
            ["sesspar"] (SessPar): named tuple with session parameters
            ["stimpar"] (StimPar): named tuple with stimulus parameters
            ["basepar"] (LatPar): named tuple with latency parameters
            ["idxpar"] (PermPar): named tuple with unexpected event index 
                parameters
            ["logregpar"] (LogRegPar): 
                named tuple with logistic regression parameters
            ["permpar"] (PermPar): named tuple with permutation parameters
            ["figpar"] (dict): dictionary containing subdictionaries 
                (see sess_plot_util.init_figpar), with fig_panel_analysis 
                added under the "fig_panel_analysis" key.
    """

    args = copy.deepcopy(args)

    fig_panel_analysis = paper_organization.FigurePanelAnalysis(
        figure=args.figure,
        panel=args.panel,
        datadir=args.datadir,
        mouse_df_path=args.mouse_df_path,
        output=args.output,
        full_power=args.full_power,
        seed=args.seed,
        parallel=args.parallel,
        plt_bkend=args.plt_bkend,
        fontdir=args.fontdir,
    )

    specific_params = fig_panel_analysis.specific_params
    sess_n = reformat_sess_n(specific_params["sess_n"])

    analysis_dict = dict()

    # analysis parameters
    analysis_dict["analyspar"] = sess_ntuple_util.init_analyspar(
        fluor="dff",  # type of fluorescence data to use (dF/F)
        rem_bad=specific_params[
            "rem_bad"],  # whether to remove bad ROIs OR interpolate bad values in run or pupil data
        stats="mean",  # type of statistic to measure (mean/median)
        error=specific_params["error"],  # type of error to measure (std/SEM)
        scale=specific_params[
            "scale"],  # whether to scale ROIs (robust scaling)
        tracked=specific_params["tracked"],  # whether to use only tracked ROIs
    )

    # session inclusion parameters
    analysis_dict["sesspar"] = sess_ntuple_util.init_sesspar(
        sess_n=sess_n,  # session number(s)
        plane=specific_params["plane"],  # recording plane(s)
        line=specific_params["line"],  # mouse line(s)
        pass_fail="P",  # include sessions that passed QC
        incl="all",  # include all remaining sessions
        runtype="prod",  # production run data
        mouse_n=specific_params["mouse_n"],  # mouse numbers
    )

    # stimulus analysis parameters
    analysis_dict["stimpar"] = sess_ntuple_util.init_stimpar(
        stimtype=specific_params["stimtype"],  # stimulus to analyse
        visflow_dir=specific_params["visflow_dir"],  # visual flow directions
        visflow_size=specific_params[
            "visflow_size"],  # visual flow square sizes
        gabfr=specific_params["gabfr"],  # Gabor frame to center analyses on
        gabk=specific_params["gabk"],  # Gabor orientation kappas
        gab_ori=specific_params["gab_ori"],  # mean Gabor orientations
        pre=specific_params["pre"],  # number of seconds pre reference frame
        post=specific_params["post"]  # number of seconds post reference frame
    )

    # baseline parameters
    analysis_dict["basepar"] = sess_ntuple_util.init_basepar(
        baseline=0,  # sequence baselining (None)
    )

    # USI analysis parameters
    analysis_dict["idxpar"] = sess_ntuple_util.init_idxpar(
        op="d-prime",  # USI measure
        feature=specific_params["idx_feature"],  # how to select sequences
    )

    # logistic regression parameters
    analysis_dict["logregpar"] = sess_ntuple_util.init_logregpar(
        comp=specific_params["comp"],  # classes
        ctrl=True,  # control for dataset size
        n_epochs=1000,  # number of training epochs
        batchsize=200,  # batch size
        lr=0.0001,  # learning rate
        train_p=0.75,  # train:test split
        wd=0,  # weight decay to use (None)
    )

    # permutation analysis parameters
    analysis_dict["permpar"] = sess_ntuple_util.init_permpar(
        n_perms=fig_panel_analysis.n_perms,  # number of permutations to run
        p_val=0.05,  # significance threshold to consider
        tails=specific_params["tails"],  # number of tails
        multcomp=False  # multiple comparisons
    )

    # figure plotting parameters
    analysis_dict["figpar"] = sess_plot_util.init_figpar(
        datetime=False,
        overwrite=args.overwrite,
        runtype="prod",
        output=args.output,
        plt_bkend=args.plt_bkend,
        fontdir=args.fontdir,
        paper=True,
    )

    analysis_dict["figpar"]["fig_panel_analysis"] = fig_panel_analysis

    return analysis_dict
def init_param_cont(args):
    """
    init_param_cont(args)

    Initializes parameter containers.

    Returns args:
        - in the following nametuples: analyspar, sesspar, stimpar, permpar, 
                                       basepar, idxpar, latpar
        - in the following dictionary: figpar 

    Required args:
        - args (Argument parser): parser with the following attributes:

            base (float)             : baseline value to use
            visflow_dir (str or list): visual flow direction values to include
                                       ("right", "left", ["right", "left"])
            visflow_size (int or list): visual flow size values to include
                                       (128, 256 or [128, 256])
            dend (str)               : type of dendrites to use 
                                       ("allen" or "dend")
            error (str)              : error statistic parameter 
                                       ("std" or "sem")
            fluor (str)              : if "raw", raw ROI traces are used. If 
                                       "dff", dF/F ROI traces are used.
            fontdir (str)            : path to directory containing additional 
                                       fonts
            gabfr (int)              : gabor frame at which sequences start 
                                       (0, 1, 2, 3)
            gabk (int or list)       : gabor kappa values to include 
                                       (4, 16 or [4, 16])
            gab_ori (int or list)    : gabor orientation values to include
                                       ([0, 45, 90, 135, 180, 225])
            idx_feature (str)        : feature used to calculate index
                                       ("by_exp", "unexp_lock", "prog_unexp")
            idx_op (str)             : type of index to use 
                                       ("d-prime", "rel_diff", "diff")
            idx_position (int)       : position to use if using a "prog" feature 
                                       to calculate index (e.g., 0)
            incl (str)               : sessions to include ("yes", "no", "all") 
            keepnans (str)           : if True, ROIs with NaN/Inf values are 
                                       kept in the analyses.
            lag_s (num)              : lag for autocorrelation (in sec)
            lat_method (str)         : latency calculation method 
                                       (ratio or ttest)
            lat_not_unexp_resp (bool): if False, only unexpected event 
                                       responsive ROIs are used for latency 
                                       analysis
            lat_p_val_thr (float)    : p-value threshold for ttest latency 
                                       method
            lat_std (float)          : standard deviation threshold for ratio 
                                       latency method
            line (str)               : "L23", "L5", "any"
            min_rois (int)           : min number of ROIs
            n_perms (int)            : nbr of permutations to run
            ncols (int)              : number of columns
            no_datetime (bool)       : if True, figures are not saved in a 
                                       subfolder named based on the date and 
                                       time.
            no_scale (bool)          : if True, data is not scaled
            not_save_fig (bool)      : if True, figures are not saved
            output (str)             : general directory in which to save output
            overwrite (bool)         : if False, overwriting existing figures 
                                       is prevented by adding suffix numbers.
            pass_fail (str or list)  : pass/fail values of interest ("P", "F")
            p_val (float)            : p-value threshold for significane tests
            plane (str)              : plane ("soma", "dend", "any")
            plt_bkend (str)          : mpl backend to use
            post (num)               : range of frames to include after each 
                                        reference frame (in s)
            pre (num)                : range of frames to include before each 
                                       reference frame (in s)
            rel_std (float)          : relative st. dev. threshold for ratio 
                                       latency method
            runtype (str or list)    : runtype ("pilot" or "prod")
            sess_n (int)             : session number
            stats (str)              : statistic parameter ("mean" or "median")
            stimtype (str)           : stimulus to analyse 
                                       ("visflow" or "gabors")
            tails (str or int)       : which tail(s) to test ("hi", "lo", 2)

    Returns:
        - analysis_dict (dict): dictionary of analysis parameters
            ["analyspar"] (AnalysPar): named tuple of analysis parameters
            ["sesspar"] (SessPar)    : named tuple of session parameters
            ["stimpar"] (StimPar)    : named tuple of stimulus parameters
            ["permpar"] (PermPar)    : named tuple of permutation parameters
            ["basepar"] (BasePar)    : named tuple of baseline parameters
            ["idxpar"] (IdxPar)      : named tuple of unexpected index parameters
            ["latpar"] (LatPar)      : named tuple of latency parameters
            ["figpar"] (dict)        : dictionary containing following 
                                       subdictionaries:
                ["init"]: dict with following inputs as attributes:
                    ["ncols"] (int)      : number of columns in the figures
                    ["sharex"] (bool)    : if True, x axis lims are shared 
                                           across subplots
                    ["sharey"] (bool)    : if True, y axis lims are shared 
                                           across subplots
                    ["subplot_hei"] (num): height of each subplot (inches)
                    ["subplot_wid"] (num): width of each subplot (inches)

                ["save"]: dict with the following inputs as attributes:
                    ["datetime"] (bool) : if True, figures are saved in a  
                                          subfolder named based on the date and 
                                          time.
                    ["fig_ext"] (str)   : figure extension
                    ["overwrite"] (bool): if True, existing figures can be 
                                          overwritten
                    ["save_fig"] (bool) : if True, figures are saved
                    ["use_dt"] (str)    : datetime folder to use
                    
                ["dirs"]: dict with the following attributes:
                    ["figdir"] (str)   : main folder in which to save figures
                    ["roi"] (str)      : subdirectory name for ROI analyses
                    ["run"] (str)      : subdirectory name for running analyses
                    ["autocorr"] (str) : subdirectory name for autocorrelation 
                                         analyses
                    ["locori"] (str)   : subdirectory name for location and 
                                         orientation responses
                    ["oridir"] (str)   : subdirectory name for 
                                         orientation/direction analyses
                    ["unexp_qu"] (str) : subdirectory name for unexpected, 
                                         quantile analyses
                    ["tune_curv"] (str): subdirectory name for tuning curves
                    ["grped"] (str)    : subdirectory name for ROI grps data
                    ["mags"] (str)     : subdirectory name for magnitude 
                                         analyses
                
                ["mng"]: dict with the following attributes:
                    ["plt_bkend"] (str): mpl backend to use
                    ["linclab"] (bool) : if True, Linclab mpl defaults are used
                    ["fontdir"] (str)  : path to directory containing 
                                         additional fonts
    """

    args = copy.deepcopy(args)

    analysis_dict = dict()

    # analysis parameters
    analysis_dict["analyspar"] = sess_ntuple_util.init_analyspar(
        args.fluor,
        not (args.keepnans),
        args.stats,
        args.error,
        scale=not (args.no_scale),
        dend=args.dend)

    # session parameters
    analysis_dict["sesspar"] = sess_ntuple_util.init_sesspar(
        args.sess_n, False, args.plane, args.line, args.min_rois,
        args.pass_fail, args.incl, args.runtype, args.mouse_ns)

    # stimulus parameters
    analysis_dict["stimpar"] = sess_ntuple_util.init_stimpar(
        args.stimtype, args.visflow_dir, args.visflow_size, args.gabfr,
        args.gabk, args.gab_ori, args.pre, args.post)

    # SPECIFIC ANALYSES
    analysis_dict["permpar"] = sess_ntuple_util.init_permpar(
        args.n_perms, args.p_val, args.tails, False)

    analysis_dict["basepar"] = sess_ntuple_util.init_basepar(args.base)

    analysis_dict["idxpar"] = sess_ntuple_util.init_idxpar(
        args.idx_op, args.idx_feature, args.idx_position)

    analysis_dict["latpar"] = sess_ntuple_util.init_latpar(
        args.lat_method, args.lat_p_val_thr, args.lat_rel_std,
        not (args.lat_not_unexp_resp))

    # figure parameters
    analysis_dict["figpar"] = sess_plot_util.init_figpar(
        ncols=int(args.ncols),
        datetime=not (args.no_datetime),
        overwrite=args.overwrite,
        save_fig=not (args.not_save_fig),
        runtype=args.runtype,
        output=args.output,
        plt_bkend=args.plt_bkend,
        fontdir=args.fontdir)

    return analysis_dict
示例#5
0
def init_param_cont(args):
    """
    init_param_cont(args)

    Initializes parameter containers.

    Returns args:
        - in the following nametuples: analyspar, sesspar, stimpar, autocorr, 
                                       permpar, quantpar, roigrppar, tcurvpar
        - in the following dictionary: figpar 

    Required args:
        - args (Argument parser): parser with the following attributes:

            visflow_dir (str or list): visual flow direction values to include
                                     ("right", "left", ["right", "left"])
            visflow_size (int or list): visual flow size values to include
                                     (128, 256 or [128, 256])
            closest (bool)         : if False, only exact session number is 
                                     retained, otherwise the closest.
            dend (str)             : type of dendrites to use ("allen" or "dend")
            error (str)            : error statistic parameter ("std" or "sem")
            fluor (str)            : if "raw", raw ROI traces are used. If 
                                     "dff", dF/F ROI traces are used.
            fontdir (str)          : path to directory containing additional 
                                     fonts
            gabfr (int)            : gabor frame at which sequences start 
                                     (0, 1, 2, 3)
            gabk (int or list)     : gabor kappa values to include 
                                     (4, 16 or [4, 16])
            gab_ori (int or list)  : gabor orientation values to include
                                     ([0, 45, 90, 135, 180, 225])
            grps (str or list)     : set or sets of groups to return, 
                                     ("all", "change", "no_change", "reduc", 
                                     "incr".)
            incl (str)             : sessions to include ("yes", "no", "all") 
            keepnans (str)         : if True, ROIs with NaN/Inf values are 
                                     kept in the analyses.
            lag_s (num)            : lag for autocorrelation (in sec)
            line (str)             : "L23", "L5", "any"
            min_rois (int)         : min number of ROIs
            n_perms (int)          : nbr of permutations to run
            n_quants (int)         : number of quantiles
            ncols (int)            : number of columns
            no_add_exp (bool)      : if True, the group of ROIs showing no 
                                     significance in either is not added to   
                                     the groups returned
            no_datetime (bool)     : if True, figures are not saved in a 
                                     subfolder named based on the date and time.
            not_byitem (bool)      : if True, autocorrelation statistics are
                                     taken across items (e.g., ROIs)
            not_save_fig (bool)    : if True, figures are not saved
            op (str)               : operation on values, if plotvals if "both" 
                                     ("ratio" or "diff") 
            output (str)           : general directory in which to save output
            overwrite (bool)       : if False, overwriting existing figures 
                                     is prevented by adding suffix numbers.
            pass_fail (str or list): pass/fail values of interest ("P", "F")
            plot_vals (str)        : values to plot ("unexp", "exp", "both")
            plane (str)            : plane ("soma", "dend", "any")
            plt_bkend (str)        : mpl backend to use
            post (num)             : range of frames to include after each 
                                     reference frame (in s)
            pre (num)              : range of frames to include before each 
                                     reference frame (in s)
            runtype (str or list)  : runtype ("pilot" or "prod")
            scale (bool)           : whether to scale ROI data
            sess_n (int)           : session number
            stats (str)            : statistic parameter ("mean" or "median")
            stimtype (str)         : stimulus to analyse ("visflow" or "gabors")
            tails (str or int)     : which tail(s) to test ("hi", "lo", 2)
            tc_gabfr (int or str)  : gabor frame at which sequences start 
                                     (0, 1, 2, 3) for tuning curve analysis
                                     (x_x, interpreted as 2 gabfrs)
            tc_grp2 (str)          : second group: either unexp, exp or rand 
                                     (random subsample of exp, the size of 
                                     unexp)
            tc_post (num)          : range of frames to include after each 
                                     reference frame (in s) for tuning curve 
                                     analysis
            tc_vm_estim (bool)     : runs analysis using von Mises parameter 
                                     estimation method
            tc_test (bool)         : if True, tuning curve analysis is run on a 
                                     small subset of ROIs and gabors

    Returns:
        - analysis_dict (dict): dictionary of analysis parameters
            ["analyspar"] (AnalysPar)    : named tuple of analysis parameters
            ["sesspar"] (SessPar)        : named tuple of session parameters
            ["stimpar"] (StimPar)        : named tuple of stimulus parameters
            ["autocorrpar"] (AutocorrPar): named tuple of autocorrelation 
                                           parameters
            ["permpar"] (PermPar)        : named tuple of permutation parameters
            ["quantpar"] (QuantPar)      : named tuple of quantile parameters
            ["roigrppar"] (RoiGrpPar)    : named tuple of roi grp parameters
            ["tcurvpar"] (TCurvPar)      : named tuple of tuning curve 
                                           parameters
            ["figpar"] (dict)            : dictionary containing following 
                                           subdictionaries:
                ["init"]: dict with following inputs as attributes:
                    ["ncols"] (int)      : number of columns in the figures
                    ["sharex"] (bool)    : if True, x axis lims are shared 
                                           across subplots
                    ["sharey"] (bool)    : if True, y axis lims are shared 
                                           across subplots
                    ["subplot_hei"] (num): height of each subplot (inches)
                    ["subplot_wid"] (num): width of each subplot (inches)

                ["save"]: dict with the following inputs as attributes:
                    ["datetime"] (bool) : if True, figures are saved in a  
                                          subfolder named based on the date and 
                                          time.
                    ["fig_ext"] (str)   : figure extension
                    ["overwrite"] (bool): if True, existing figures can be 
                                          overwritten
                    ["save_fig"] (bool) : if True, figures are saved
                    ["use_dt"] (str)    : datetime folder to use

                ["dirs"]: dict with the following attributes:
                    ["figdir"] (str)   : main folder in which to save figures
                    ["roi"] (str)      : subdirectory name for ROI analyses
                    ["run"] (str)      : subdirectory name for running analyses
                    ["autocorr"] (str) : subdirectory name for autocorrelation 
                                         analyses
                    ["locori"] (str)   : subdirectory name for location and 
                                         orientation responses
                    ["oridir"] (str)   : subdirectory name for 
                                         orientation/direction analyses
                    ["unexp_qu"] (str)  : subdirectory name for unexpected, 
                                         quantile analyses
                    ["tune_curv"] (str): subdirectory name for tuning curves
                    ["grped"] (str)    : subdirectory name for ROI grps data
                    ["mags"] (str)     : subdirectory name for magnitude 
                                         analyses
                
                ["mng"]: dict with the following attributes:
                    ["plt_bkend"] (str): mpl backend to use
                    ["linclab"] (bool) : if True, Linclab mpl defaults are used
                    ["fontdir"] (str)  : path to directory containing 
                                         additional fonts
    """

    args = copy.deepcopy(args)

    analysis_dict = dict()

    # analysis parameters
    analysis_dict["analyspar"] = sess_ntuple_util.init_analyspar(
        args.fluor, not(args.keepnans), args.stats, args.error, args.scale, 
        dend=args.dend)

    # session parameters
    analysis_dict["sesspar"] = sess_ntuple_util.init_sesspar(
        args.sess_n, args.closest, args.plane, args.line, args.min_rois, 
        args.pass_fail, args.incl, args.runtype)
    
    # stimulus parameters
    analysis_dict["stimpar"] = sess_ntuple_util.init_stimpar(
        args.stimtype, args.visflow_dir, args.visflow_size, args.gabfr, args.gabk, 
        args.gab_ori, args.pre, args.post)

    # SPECIFIC ANALYSES    
    # autocorrelation parameters
    analysis_dict["autocorrpar"] = sess_ntuple_util.init_autocorrpar(
        args.lag_s, not(args.not_byitem))
    
    # permutation parameters
    analysis_dict["permpar"] = sess_ntuple_util.init_permpar(
        args.n_perms, 0.05, args.tails)
    
    # quantile parameters
    analysis_dict["quantpar"] = sess_ntuple_util.init_quantpar(
        args.n_quants, [0, -1])

    # roi grp parameters
    analysis_dict["roigrppar"] = sess_ntuple_util.init_roigrppar(
        args.grps, not(args.no_add_exp), args.op, args.plot_vals)

    # tuning curve parameters
    analysis_dict["tcurvpar"] = sess_ntuple_util.init_tcurvpar(
        args.tc_gabfr, 0, args.tc_post, args.tc_grp2, args.tc_test, 
        args.tc_vm_estim)

    # figure parameters
    analysis_dict["figpar"] = sess_plot_util.init_figpar(
        ncols=int(args.ncols), datetime=not(args.no_datetime), 
        overwrite=args.overwrite, save_fig=not(args.not_save_fig), 
        runtype=args.runtype, output=args.output, plt_bkend=args.plt_bkend, 
        fontdir=args.fontdir)

    return analysis_dict
def init_param_cont(args):
    """
    init_param_cont(args)

    Returns args:
        - in the following nametuples: analyspar, sesspar, stimpar, autocorr, 
                                       permpar
        - in the following dictionary: figpar 

    Required args:
        - args (Argument parser): parser with the following attributes:

            visflow_dir (str or list): visual flow direction values to include
                                      ("right", "left", ["right", "left"])
            visflow_size (int or list): visual flow square size values to include
                                     (128, 256 or [128, 256])
            dend (str)             : type of dendrites to use ("allen" or "dend")
            error (str)            : error statistic parameter ("std" or "sem")
            fontdir (str)          : path to directory containing additional 
                                     fonts
            fluor (str)            : if "raw", raw ROI traces are used. If 
                                     "dff", dF/F ROI traces are used.
            gabfr (int)            : gabor frame at which sequences start 
                                     (0, 1, 2, 3)
            gabk (int or list)     : gabor kappa values to include 
                                     (4, 16 or [4, 16])
            gab_ori (int or list)  : gabor orientation values to include
                                     ([0, 45, 90, 135, 180, 225])
            incl (str)             : 
            lag_s (num)            : lag for autocorrelation (in sec)
            line (str)             : line ("L23", "L5", "any")
            min_rois (int)         : min number of ROIs
            ncols (int)            : number of columns
            no_datetime (bool)     : if True, figures are not saved in a 
                                     subfolder named based on the date and time.
            not_save_fig (bool)    : if True, figures are not saved
            output (str)           : general directory in which to save output
            overwrite (bool)       : if False, overwriting existing figures 
                                     is prevented by adding suffix numbers.
            pass_fail (str or list): pass/fail values of interest ("P", "F")
            plane (str)            : plane ("soma", "dend", "any")
            plt_bkend (str)        : mpl backend to use
            post (num)             : range of frames to include after each 
                                     reference frame (in s)
            pre (num)              : range of frames to include before each 
                                     reference frame (in s)
            runtype (str or list)  : runtype ("pilot" or "prod")
            sess_n (int)           : session number
            stats (str)            : statistic parameter ("mean" or "median")

    Returns:
        - analysis_dict (dict): dictionary of analysis parameters
            ["analyspar"] (AnalysPar): named tuple of analysis parameters
            ["sesspar"] (SessPar)    : named tuple of session parameters
            ["stimpar"] (StimPar)    : named tuple of stimulus parameters
            ["glmpar"] (GLMPar)      : named tuple of GLM parameters
            ["figpar"] (dict)        : dictionary containing following 
                                       subdictionaries:
                ["init"]: dict with following inputs as attributes:
                    ["ncols"] (int)      : number of columns in the figures
                    ["sharex"] (bool)    : if True, x axis lims are shared 
                                           across subplots
                    ["sharey"] (bool)    : if True, y axis lims are shared 
                                           across subplots
                    ["subplot_hei"] (num): height of each subplot (inches)
                    ["subplot_wid"] (num): width of each subplot (inches)

                ["save"]: dict with the following inputs as attributes:
                    ["datetime"] (bool) : if True, figures are saved in a  
                                          subfolder named based on the date and 
                                          time.
                    ["fig_ext"] (str)   : figure extension
                    ["overwrite"] (bool): if True, existing figures can be 
                                          overwritten
                    ["save_fig"] (bool) : if True, figures are saved
                    ["use_dt"] (str)    : datetime folder to use

                ["dirs"]: dict with the following attributes:
                    ["figdir"] (str)   : main folder in which to save figures
                    ["roi"] (str)      : subdirectory name for ROI analyses
                    ["run"] (str)      : subdirectory name for running analyses
                    ["autocorr"] (str) : subdirectory name for autocorrelation 
                                         analyses
                    ["locori"] (str)   : subdirectory name for location and 
                                         orientation responses
                    ["oridir"] (str)   : subdirectory name for 
                                         orientation/direction analyses
                    ["unexp_qu"] (str) : subdirectory name for unexpected, 
                                         quantile analyses
                    ["tune_curv"] (str): subdirectory name for tuning curves
                    ["grped"] (str)    : subdirectory name for ROI grps data
                    ["mags"] (str)     : subdirectory name for magnitude 
                                         analyses
                
                ["mng"]: dict with the following attributes:
                    ["plt_bkend"] (str): mpl backend to use
                    ["linclab"] (bool) : if True, Linclab mpl defaults are used
                    ["fontdir"] (str)  : path to directory containing 
                                         additional fonts
    """

    args = copy.deepcopy(args)

    analysis_dict = dict()

    # analysis parameters
    analysis_dict["analyspar"] = sess_ntuple_util.init_analyspar(
        args.fluor, True, args.stats, args.error, dend=args.dend)

    # session parameters
    analysis_dict["sesspar"] = sess_ntuple_util.init_sesspar(args.sess_n,
                                                             False,
                                                             args.plane,
                                                             args.line,
                                                             args.min_rois,
                                                             args.pass_fail,
                                                             args.incl,
                                                             args.runtype,
                                                             mouse_n=1)

    # stimulus parameters
    analysis_dict["stimpar"] = sess_ntuple_util.init_stimpar(
        args.stimtype, args.visflow_dir, args.visflow_size, args.gabfr,
        args.gabk, args.gab_ori, args.pre, args.post)

    # SPECIFIC ANALYSES
    # autocorrelation parameters
    analysis_dict["glmpar"] = sess_ntuple_util.init_glmpar(
        args.each_roi, args.k, args.test)

    # figure parameters
    analysis_dict["figpar"] = sess_plot_util.init_figpar(
        ncols=int(args.ncols),
        datetime=not (args.no_datetime),
        overwrite=args.overwrite,
        save_fig=not (args.not_save_fig),
        runtype=args.runtype,
        output=args.output,
        plt_bkend=args.plt_bkend,
        fontdir=args.fontdir)

    return analysis_dict