Esempio n. 1
0
def reformat_args(args):
    """
    reformat_args(args)

    Returns reformatted args for analyses, specifically 
        - Sets stimulus parameters to "none" if they are irrelevant to the 
          stimtype
        - Changes stimulus parameters from "both" to actual values
        - Sets seed, though doesn't seed
        - Modifies analyses (if "all" or "all_" in parameter)

    Adds the following args:
        - dend (str)     : type of dendrites to use ("allen", "extr")
        - omit_sess (str): sess to omit
        - omit_mice (str): mice to omit

    Required args:
        - args (Argument parser): parser with the following attributes: 
            runtype (str)        : runtype ("pilot" or "prod")
            stimtype (str)       : stimulus to analyse (visflow or gabors)
    
    Returns:
        - args (Argument parser): input parser, with the following attributes 
                                  added:
                                      visflow_dir, visflow_size, gabfr, gabk, 
                                      gab_oriomit_sess, omit_mice, dend, 
                                      analyses, seed
    """

    args = copy.deepcopy(args)

    [args.visflow_dir, args.visflow_size, args.gabfr, args.gabk,
     args.gab_ori] = sess_gen_util.get_params(args.stimtype, "both", 128,
                                              "any", 16, "any")

    if args.plane == "soma":
        args.dend = "allen"

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

    # choose a seed if none is provided (i.e., args.seed=-1), but seed later
    args.seed = rand_util.seed_all(args.seed,
                                   "cpu",
                                   log_seed=False,
                                   seed_now=False)

    # collect analysis letters
    all_analyses = "".join(get_analysis_fcts().keys())
    if "all" in args.analyses:
        if "_" in args.analyses:
            excl = args.analyses.split("_")[1]
            args.analyses, _ = gen_util.remove_lett(all_analyses, excl)
        else:
            args.analyses = all_analyses
    elif "_" in args.analyses:
        raise ValueError("Use '_' in args.analyses only with 'all'.")

    return args
def get_specific_params(sess_n="1-3",
                        mouse_n="any",
                        plane="all",
                        line="all",
                        stimtype="gabors",
                        gabfr=3,
                        gab_ori="any",
                        pre=0,
                        post=0.6,
                        tails=2,
                        idx_feature="by_exp",
                        comp="Dori",
                        error="sem",
                        scale=True,
                        tracked=False,
                        rem_bad=True,
                        roi=True,
                        run=False,
                        pupil=False):
    """
    get_specific_params()

    Returns specific parameters for the analysis.

    Optional args:
        - sess_n (str): 
            which sessions numbers to include
            default: "1-3"
        - mouse_n (str): 
            which mouse number to include
            default: "any"
        - plane (str): 
            which planes to include
            default: "all"
        - line (str): 
            which lines to include
            default: "all"
        - stimtype (str): 
            which stimtypes to include
            default: "gabors"
        - gabfr (str): 
            reference Gabor frame
            default: 3
        - gab_ori (str): 
            Gabor mean orientations to include
            default: "any"
        - pre (num): 
            number of seconds before reference to include
            default: 0
        - post (num): 
            number of seconds after reference to include
            default: 0.6
        - tails (str or int): 
            p-value tails (2, "hi" or "lo")
            default: 2
        - idx_feature (str): 
            type of feature to measure indices on
            default: "by_exp"
        - comp (str):
            logistic regression comparison defining classes to decode
            default: "Dori"
        - error (str):
            type of error statistic to calculate for analyses
            default: "sem"
        - scale (bool): 
            whether to use ROI scaling
            default: True
        - tracked (bool):
            if True, only tracked ROIs are included
            default: False
        - rem_bad (bool):
            if True, invalid ROIs are removed, or for pupil/running data, 
            missing data is interpolated
            default: True
        - roi (bool): 
            whether ROI data needs to be loaded
            default: True
        - run (bool): 
            whether run data needs to be loaded
            default: True
        - pupil (bool): 
            whether pupil data needs to be loaded
            default: False

    Returns:
        - specific_params (dict): dictionary with the specified parameters
    """

    visflow_dir, visflow_size, gabfr, gabk, gab_ori = sess_gen_util.get_params(
        stimtype, gabfr=gabfr, gab_ori=gab_ori)

    specific_params = {
        "sess_n": sess_n,
        "mouse_n": mouse_n,
        "plane": plane,
        "line": line,
        "stimtype": stimtype,
        "visflow_dir": visflow_dir,
        "visflow_size": visflow_size,
        "gabfr": gabfr,
        "gabk": gabk,
        "gab_ori": gab_ori,
        "pre": pre,
        "post": post,
        "tails": tails,
        "idx_feature": idx_feature,
        "comp": comp,
        "error": error,
        "scale": scale,
        "tracked": tracked,
        "rem_bad": rem_bad,
        "roi": roi,
        "run": run,
        "pupil": pupil,
    }

    return specific_params
Esempio n. 3
0
def reformat_args(args):
    """
    reformat_args(args)

    Returns reformatted args for analyses, specifically 
        - Sets stimulus parameters to "none" if they are irrelevant to the 
          stimtype
        - Changes stimulus parameters from "both" to actual values
        - Sets seed, though doesn't seed
        - Modifies analyses (if "all" or "all_" in parameter)

    Adds the following args:
        - omit_sess (str): sess to omit
        - omit_mice (str): mice to omit

    Required args:
        - args (Argument parser): parser with the following attributes: 
            visflow_dir (str)    : visual flow direction values to include
                                   (e.g., "right", "left" or "both")
            visflow_size (int or str): visual flow size values to include
                                   (e.g., 128, 256, "both")
            gabfr (int)          : gabor frame value to start sequences at
                                   (e.g., 0, 1, 2, 3)
            gabk (int or str)    : gabor kappa values to include 
                                   (e.g., 4, 16 or "both")
            gab_ori (int or str) : gabor orientation values to include
                                   (e.g., 0, 45, 90, 135, 180, 225 or "all")
            runtype (str)        : runtype ("pilot" or "prod")
            stimtype (str)       : stimulus to analyse (visflow or gabors)
    
    Returns:
        - args (Argument parser): input parser, with the following attributes
                                  modified: 
                                      visflow_dir, visflow_size, gabfr, gabk, 
                                      gab_ori, grps, analyses, seed
                                  and the following attributes added:
                                      omit_sess, omit_mice
    """
    args = copy.deepcopy(args)

    [args.visflow_dir, args.visflow_size, args.gabfr, args.gabk,
     args.gab_ori] = sess_gen_util.get_params(args.stimtype, args.visflow_dir,
                                              args.visflow_size, args.gabfr,
                                              args.gabk, args.gab_ori)

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

    # choose a seed if none is provided (i.e., args.seed=-1), but seed later
    args.seed = rand_util.seed_all(args.seed,
                                   "cpu",
                                   log_seed=False,
                                   seed_now=False)

    # collect analysis letters
    all_analyses = "".join(get_analysis_fcts().keys())
    if "all" in args.analyses:
        if "_" in args.analyses:
            excl = args.analyses.split("_")[1]
            args.analyses, _ = gen_util.remove_lett(all_analyses, excl)
        else:
            args.analyses = all_analyses
    elif "_" in args.analyses:
        raise ValueError("Use '_' in args.analyses only with 'all'.")

    return args
def plot_summ(output, savename, stimtype="gabors", comp="unexp", ctrl=False, 
              visflow_dir="both", fluor="dff", scale=True, CI=0.95, 
              plt_bkend=None, fontdir=None, modif=False):
    """
    plot_summ(output)

    Plots summary data for a specific comparison, for each datatype in a 
    separate figure and saves figures. 

    Required args:
        - output (str)  : general directory in which summary dataframe 
                          is saved (runtype and q1v4 values are inferred from 
                          the directory name)
        - savename (str): name of the dataframe containing summary data to plot
            
    Optional args:
        - stimtype (str)   : stimulus type
                             default: "gabors"
        - comp (str)       : type of comparison
                             default: "unexp"
        - ctrl (bool)      : if True, control comparisons are analysed
                             default: False                           
        - visflow_dir (str): visual flow direction
                             default: "both"
        - fluor (str)      : fluorescence trace type
                             default: "dff"
        - scale (bool)     : whether ROIs are scaled
                             default: True
        - CI (num)         : CI for shuffled data
                             default: 0.95
        - plt_bkend (str)  : mpl backend to use for plotting (e.g., "agg")
                             default: None
        - fontdir (str)    : directory in which additional fonts are located
                             default: None
        - modif (bool)     : if True, plots are made in a modified (simplified 
                             way)
                             default: False

    """
    
    plot_util.manage_mpl(plt_bkend, fontdir=fontdir)

    summ_scores_file = Path(output, savename)
    
    if summ_scores_file.is_file():
        summ_scores = file_util.loadfile(summ_scores_file)
    else:
        warnings.warn(f"{summ_scores_file} not found.", 
            category=RuntimeWarning, stacklevel=1)
        return

    if len(summ_scores) == 0:
        warnings.warn(f"No data in {summ_scores_file}.", 
            category=RuntimeWarning, stacklevel=1)
        return

    # drop NaN lines
    summ_scores = summ_scores.loc[~summ_scores["epoch_n_mean"].isna()]

    data_types  = ["epoch_n", "test_acc", "test_acc_bal"]
    data_titles = ["Epoch nbrs", "Test accuracy", "Test accuracy (balanced)"]

    stats = ["mean", "sem", "sem"]
    shuff_stats = ["median"] + math_util.get_percentiles(CI)[1]

    q1v4, evu = False, False
    if "_q1v4" in str(output):
        q1v4 = True
    elif "_evu" in str(output):
        evu = True
    
    runtype = "prod"
    if "pilot" in str(output):
        runtype = "pilot"

    if stimtype == "gabors":
        visflow_dir = "none"
        stim_str = "gab"
        stim_str_pr = "gabors"

    else:
        visflow_dir = sess_gen_util.get_params(stimtype, visflow_dir)[0]
        if isinstance(visflow_dir, list) and len(visflow_dir) == 2:
            visflow_dir = "both"
        stim_str = sess_str_util.dir_par_str(visflow_dir, str_type="file")
        stim_str_pr = sess_str_util.dir_par_str(visflow_dir, str_type="print")

    scale_str = sess_str_util.scale_par_str(scale, "file")
    scale_str_pr = sess_str_util.scale_par_str(scale, "file").replace("_", " ")
    ctrl_str = sess_str_util.ctrl_par_str(ctrl)
    ctrl_str_pr = sess_str_util.ctrl_par_str(ctrl, str_type="print")
    modif_str = "_modif" if modif else ""

    save_dir = Path(output, f"figures_{fluor}")
    save_dir.mkdir(exist_ok=True)
    
    cols = ["scale", "fluor", "visflow_dir", "runtype"]
    cri  = [scale, fluor, visflow_dir, runtype]
    plot_lines = gen_util.get_df_vals(summ_scores, cols, cri)
    cri_str = ", ".join([f"{col}: {crit}" for col, crit in zip(cols, cri)])
    if len(plot_lines) == 0: # no data
        warnings.warn(f"No data found for {cri_str}", category=RuntimeWarning, 
            stacklevel=1)
        return
    else: # shuffle or non shuffle missing
        skip = False
        for shuff in [False, True]:
            if shuff not in plot_lines["shuffle"].tolist():
                warnings.warn(f"No shuffle={shuff} data found for {cri_str}", 
                    category=RuntimeWarning, stacklevel=1)
                skip = True
        if skip:
            return

    for data, data_title in zip(data_types, data_titles):
        if not modif:
            title = (f"{stim_str_pr.capitalize()} {comp}{ctrl_str_pr} - "
                f"{data_title} for log regr on\n" + 
                u"{} {} ".format(scale_str_pr, fluor) + 
                f"data ({runtype})")
        else:
            title = (f"{stim_str_pr.capitalize()} {comp}{ctrl_str_pr}\n"
                f"{data_title}")

        if "_" in title:
            title = title.replace("_", " ")

        savename = (f"{data}_{stim_str}_{comp}{ctrl_str}{scale_str}"
            f"{modif_str}.svg")
        full_savename = Path(save_dir, savename)
        plot_data_summ(plot_lines, data, stats, shuff_stats, title, 
            full_savename, CI, q1v4, evu, comp, modif)

    plot_util.cond_close_figs()
def reformat_args(args):
    """
    reformat_args(args)

    Returns reformatted args for analyses, specifically 
        - Sets stimulus parameters to "none" if they are irrelevant to the 
          stimtype
        - Changes stimulus parameters from "both" to actual values
        - Modifies the session number parameter
        - Sets seed, though doesn't seed
        - Modifies analyses (if "all" or "all_" in parameter)
        - Sets latency parameters based on lat_method

    Adds the following args:
        - dend (str)     : type of dendrites to use ("allen" or "extr")
        - omit_sess (str): sess to omit
        - omit_mice (str): mice to omit

    Required args:
        - args (Argument parser): parser with the following attributes: 
            visflow_dir (str)        : visual flow direction values to include
                                   (e.g., "right", "left" or "both")
            visflow_size (int or str): visual flow size values to include
                                   (e.g., 128, 256, "both")
            gabfr (int)          : gabor frame value to start sequences at
                                   (e.g., 0, 1, 2, 3)
            gabk (int or str)    : gabor kappa values to include 
                                   (e.g., 4, 16 or "both")
            gab_ori (int or str) : gabor orientation values to include
                                   (e.g., 0, 45, 90, 135, 180, 225 or "all")
            mouse_ns (str)       : mouse numbers or range 
                                   (e.g., 1, "1,3", "1-3", "all")
            runtype (str)        : runtype ("pilot" or "prod")
            sess_n (str)         : session number range (e.g., "1-1", "all")
            stimtype (str)       : stimulus to analyse (visflow or gabors)
    
    Returns:
        - args (Argument parser): input parser, with the following attributes
                                  modified: 
                                      visflow_dir, visflow_size, gabfr, gabk, 
                                      gab_ori, sess_n, mouse_ns, analyses, seed, 
                                      lat_p_val_thr, lat_rel_std
                                  and the following attributes added:
                                      omit_sess, omit_mice, dend
    """

    args = copy.deepcopy(args)

    if args.plane == "soma":
        args.dend = "allen"

    [args.visflow_dir, args.visflow_size, args.gabfr, args.gabk,
     args.gab_ori] = sess_gen_util.get_params(args.stimtype, args.visflow_dir,
                                              args.visflow_size, args.gabfr,
                                              args.gabk, args.gab_ori)

    if args.datatype == "run":
        args.fluor = "n/a"
    if args.plane == "soma":
        args.dend = "allen"

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

    if "-" in str(args.sess_n):
        vals = str(args.sess_n).split("-")
        if len(vals) != 2:
            raise ValueError(
                "If args.sess_n is a range, must have format 1-3.")
        st = int(vals[0])
        end = int(vals[1]) + 1
        args.sess_n = list(range(st, end))

    if args.lat_method == "ratio":
        args.lat_p_val_thr = None
    elif args.lat_method == "ttest":
        args.lat_rel_std = None

    # choose a seed if none is provided (i.e., args.seed=-1), but seed later
    args.seed = rand_util.seed_all(args.seed,
                                   "cpu",
                                   log_seed=False,
                                   seed_now=False)

    # collect mouse numbers from args.mouse_ns
    if "," in args.mouse_ns:
        args.mouse_ns = [int(n) for n in args.mouse_ns.split(",")]
    elif "-" in args.mouse_ns:
        vals = str(args.mouse_ns).split("-")
        if len(vals) != 2:
            raise ValueError(
                "If args.mouse_ns is a range, must have format 1-3.")
        st = int(vals[0])
        end = int(vals[1]) + 1
        args.mouse_ns = list(range(st, end))
    elif args.mouse_ns not in ["all", "any"]:
        args.mouse_ns = int(args.mouse_ns)

    # collect analysis letters
    all_analyses = "".join(get_analysis_fcts().keys())
    if "all" in args.analyses:
        if "_" in args.analyses:
            excl = args.analyses.split("_")[1]
            args.analyses, _ = gen_util.remove_lett(all_analyses, excl)
        else:
            args.analyses = all_analyses
    elif "_" in args.analyses:
        raise ValueError("Use '_' in args.analyses only with 'all'.")

    return args