def shuff_par_str(shuffle=True, str_type="file"):
    """
    shuff_par_str()

    Returns string from shuffle parameter to print or for a filename.

    Optional args:
        - shuffle (bool): default: True
        - str_type (str): "print" for a printable string and "file" for a
                          string usable in a filename, "label" for a label.
                          default: "file"
    
    Returns:
        - shuff_str (str): shuffle parameter string
    """

    if shuffle:
        if str_type == "print":
            shuff_str = ", shuffled"
        elif str_type == "file":
            shuff_str = "_shuffled"
        elif str_type == "labels":
            shuff_str = " (shuffled labels)"
        else:
            gen_util.accepted_values_error("str_type", str_type,
                                           ["labels", "print", "file"])
    else:
        shuff_str = ""

    return shuff_str
def stim_par_str(stimtype="gabors",
                 visflow_dir=None,
                 visflow_size=None,
                 gabk=None,
                 str_type="file"):
    """
    stim_par_str(par)

    Returns a string with stim type, as well as gabor kappa or visual flow 
    square size and direction parameters, unless all possible parameters values 
    for the stim type are passed.

    Optional args:
        - stimtype (str)            : type of stimulus
                                      default: "gabors"
        - visflow_dir (str or list) : visual flow direction parameter
                                      default: None
        - visflow_size (int or list): visual flow square size parameter
                                      default: None
        - gabk (int or list)        : gabor kappa parameter
                                      default: None
        - str_type (str)            : use of output str, i.e., for a filename 
                                      ("file") or to print the info to console 
                                      ("print")
                                      default: "file"

    Returns:
        - pars (str): string containing stim type and parameter values, unless
                      all parameter values for the stim type are passed.
    """

    all_pars = []
    if stimtype in ["gabors", "both"]:
        if gabk is None:
            raise ValueError("If stimulus is gabors, must pass gabk "
                             "parameters.")
        pars = gabk_par_str(gabk, str_type)
        if stimtype == "both":
            all_pars.append(pars)
    elif stimtype in ["visflow", "both"]:
        if visflow_size is None or visflow_dir is None:
            raise ValueError("If stimulus is visual flow, must pass direction "
                             "and square size parameters.")
        pars = visflow_par_str(visflow_dir, visflow_size, str_type=str_type)
        if stimtype == "both":
            all_pars.append(pars)
    else:
        gen_util.accepted_values_error("stimtype", stimtype,
                                       ["gabors", "visflow", "both"])

    if stimtype == "both":
        if str_type == "file":
            pars = "_".join(all_pars)
        elif str_type == "print":
            pars = ", ".join(all_pars)
        else:
            gen_util.accepted_values_error("str_type", str_type,
                                           ["file", "print"])

    return pars
def base_par_str(baseline=None, str_type="file"):
    """
    base_par_str()

    Returns string from baseline parameter to print or for a filename.

    Optional args:
        - baseline (num)  : baseline value, in seconds
                            default: None
        - str_type (str)  : "print" for a printable string and "file" for a
                            string usable in a filename.
                            default: "file"
    
    Returns:
        - base_str (str): baseline parameter string
    """

    if baseline is not None:
        if str_type == "print":
            baseline = gen_util.num_to_str(baseline, n_dec=2, dec_sep=".")
            base_str = f" ({baseline}s baseline)"
        elif str_type == "file":
            baseline = gen_util.num_to_str(baseline, n_dec=2, dec_sep="-")
            base_str = f"_b{baseline}"
        else:
            gen_util.accepted_values_error("str_type", str_type,
                                           ["print", "file"])
    else:
        base_str = ""

    return base_str
def scale_par_str(scale=True, str_type="file"):
    """
    scale_par_str()

    Returns a string from scaling parameter to print or for a filename.

    Optional args:
        - scale (str or bool): if scaling is used or type of scaling used 
                               (e.g., "roi", "all", "none", True, False)
                               default: None
        - str_type (str)     : "print" for a printable string and "file" for a
                               string usable in a filename.
                               default: "file"
    
    Returns:
        - scale_str (str): scale parameter string
    """

    if scale not in ["None", "none"] and scale:
        if str_type == "print":
            scale_str = " (scaled)"
        elif str_type == "file":
            scale_str = "_scaled"
        else:
            gen_util.accepted_values_error("str_type", str_type,
                                           ["print", "file"])
    else:
        scale_str = ""

    return scale_str
def sess_par_str(sess_n,
                 stimtype="gabors",
                 layer="soma",
                 visflow_dir=None,
                 visflow_size=None,
                 gabk=None,
                 str_type="file"):
    """
    sess_par_str(sess_n)

    Returns a string from session and stimulus parameters for a filename, 
    or to print or use in a title.

    Required args:
        - sess_n (int or list)  : session number aimed for

    Optional args:
        - stimtype (str)            : type of stimulus
                                      default: "gabors"
        - layer (str)               : layer ("soma", "dend", "L23_soma", 
                                             "L5_soma", "L23_dend", "L5_dend", 
                                             "L23_all", "L5_all")
                                      default: "soma"
        - visflow_dir (str or list) : visual flow direction parameter
                                      default: None
        - visflow_size (int or list): visual flow square size parameter
                                      default: None
        - gabk (int or list)        : gabor kappa parameter
                                      default: None
        - str_type (str)            : use of output str, i.e., for a filename 
                                      ("file") or to print the info to console 
                                      ("print")
                                      default: "file"
    Returns:
        - sess_str (list): string containing info on session and stimulus  
                           parameters
    """
    if gabk is None and (visflow_size is None or visflow_dir is None):
        raise ValueError("Must pass value for gabor k parameter or visual "
                         "flow square size and direction.")
    elif gabk is None:
        stimtype = "visflow"
    elif visflow_size is None or visflow_dir is None:
        stimtype = "gabors"

    stim_str = stim_par_str(stimtype, visflow_dir, visflow_size, gabk,
                            str_type)

    if isinstance(sess_n, list):
        sess_n = gen_util.intlist_to_str(sess_n)

    if str_type == "file":
        sess_str = f"sess{sess_n}_{stim_str}_{layer}"
    elif str_type == "print":
        stim_str = stim_str.replace(" (", ": ").replace(")", "")
        sess_str = f"{stim_str}, session: {sess_n}, layer: {layer}"
    else:
        gen_util.accepted_values_error("str_type", str_type, ["file", "print"])

    return sess_str
def ctrl_par_str(ctrl=False, str_type="file"):
    """
    ctrl_par_str()

    Returns string from control parameter to print or for a filename.

    Optional args:
        - ctrl (bool)   : default: False
        - str_type (str): "print" for a printable string and "file" for a
                          string usable in a filename, "label" for a label.
                          default: "file"
    
    Returns:
        - ctrl_str (str): shuffle parameter string
    """

    if ctrl:
        if str_type == "print":
            ctrl_str = " (control)"
        elif str_type == "file":
            ctrl_str = "_ctrl"
        else:
            gen_util.accepted_values_error("str_type", str_type,
                                           ["print", "file"])
    else:
        ctrl_str = ""

    return ctrl_str
Exemplo n.º 7
0
def log_sklearn_results(model, analyspar, name="bayes_ridge", var_names=None):
    """
    log_sklearn_results(model, analyspar)
    """

    logger.info(f"{name.replace('_', ' ').upper()} regression",
                extra={"spacing": "\n"})
    if var_names is None:
        var_names = [f"coef {i}" for i in range(len(model.coef_))]

    results = "\n".join(
        [f"{varn}: {coef:.5f}" for varn, coef in zip(var_names, model.coef_)])
    logger.info(results)
    logger.info(f"intercept: {model.intercept_:.5f}")
    logger.info(f"alpha: {model.alpha_:.5f}", extra={"spacing": "\n"})
    if name == "ridge_cv":
        alpha_idx = np.where(model.alphas == model.alpha_)[0]
        score_data = model.cv_values_[:, alpha_idx]
        score_name = "MSE"
    elif name == "bayes_ridge":
        score_data = model.scores_
        score_name = "Score"
    else:
        gen_util.accepted_values_error("name", name,
                                       ["ridge_cv", "bayes_ridge"])
    stats = math_util.get_stats(score_data,
                                stats=analyspar.stats,
                                error=analyspar.error)
    math_util.log_stats(stats, f"{score_name}")
def prepost_par_str(pre, post, str_type="file"):
    """
    prepost_par_str(pre, post)

    Returns a string for the pre and post values.

    Required args:
        - pre (num) : pre value (in seconds)
        - post (num): post value (in seconds) 

    Optional args:
        - str_type (str): use of output str, i.e., for a filename 
                          ("file") or to print the info to console 
                          ("print")
                          default: "file"
    Return:
        - prepost_str (str): string containing pre-post info
    """

    vals = [pre, post]

    # convert to int if equivalent
    for i in range(len(vals)):
        if int(vals[i]) == float(vals[i]):
            vals[i] = int(vals[i])

    if str_type == "file":
        # replace . by -
        prepost_str = "{}pre-{}post".format(*vals).replace(".", "")
    elif str_type == "print":
        prepost_str = "{}-{}s".format(*vals)
    else:
        gen_util.accepted_values_error("str_type", str_type, ["print", "file"])

    return prepost_str
Exemplo n.º 9
0
def get_frame_type_by_datatype(datatype="roi"):
    """
    get_frame_type_by_datatype()

    Returns frame type corresponding to datatype.

    Required args:
        - datatype (str):
            type of data ("roi", "run" or "pupil")
            default: "roi"
        
    Returns:
        - frame_type (str):
            correct frame type needed for the data type
    """

    if datatype == "run":
        frame_type = "stim"
    elif datatype == "pupil":
        frame_type = "twop"
    elif datatype == "roi":
        frame_type = "twop"
    else:
        gen_util.accepted_values_error("datatype", datatype,
                                       ["roi", "run", "pupil"])

    return frame_type
def ext_test_str(q1v4=False, evu=False, comp="unexp", str_type="file"):
    """
    ext_test_str()
    
    Returns the string for the extra test set for logistic regressions, based 
    on the parameters. Returns "" if neither q1v4 nor exp_v_unexp is True.

    Optional args:
        - q1v4 (bool)    : if True, analysis is separated across first and last 
                           quartiles
                           default: False
        - evu (bool)     : if True, analysis is separated across expected and 
                           unexpected sequences 
                           default: False
        - comp (str)     : type of comparison
                           default: "unexp"
        - str_type (str) : use of output str, i.e., for a filename 
                           ("file") or to print the info to console ("print")
                           or for a label ("label")
                           default: "file"

    Returns:
        - ext_str (str): string for the extra dataset ("" if neither 
                         q1v4 nor evu is True), and comparison details
    """
    if q1v4 + evu > 1:
        raise ValueError("'q1v4' and 'evu' cannot both be True.")

    if str_type not in ["file", "print", "label"]:
        gen_util.accepted_values_error("str_type", str_type,
                                       ["file", "print", "label"])

    split_oris = get_split_oris(comp)

    if q1v4:
        if str_type == "file":
            ext_str = "test_Q4"
        elif str_type == "label":
            ext_str = " (only Q1)"
        else:
            ext_str = " (trained on Q1 and tested on Q4)"
    elif evu:
        if str_type == "file":
            ext_str = "test_unexp"
        elif str_type == "label":
            ext_str = " (only exp)"
        else:
            ext_str = " (trained on exp and tested on unexp)"
    elif split_oris is not False:
        if str_type == "file":
            ext_str = f"test_{split_oris[1]}"
        elif str_type == "label":
            ext_str = f" ({split_oris[0]} Gabors)"
        else:
            ext_str = " (trained on {} and tested on {})".format(*split_oris)
    else:
        ext_str = ""

    return ext_str
def get_line_plane_idxs(line="L23-Cux2", plane="soma", flat=False):
    """
    get_line_plane_idxs()

    Returns parameters for a line/plane combination graph.

    Optional args:
        - line (str):
            line name
            default: "L2/3-Cux2"
        - plane (str):
            plane_name
            default: "soma"

    Returns:
        if flat:
        - idx (int):
            line/plane index
        
        else:
        - li (int): 
            line index
        - pl (int): 
            plane index

        and in both cases:
        - col (str): 
            color hex code
        - dash (tuple or None): 
            dash pattern
    """

    lines, planes = ["L23-Cux2", "L5-Rbp4"], ["dend", "soma"]
    pla_col_names = ["green", "blue"]

    if line not in lines:
        gen_util.accepted_values_error("line", line, lines)
    if plane not in planes:
        gen_util.accepted_values_error("plane", plane, planes)

    li = lines.index(line)
    pl = planes.index(plane)
    col = get_colors(pla_col_names[pl], line=line)
    dash = VDASH if "L5" in line else None

    if flat:
        idx = pl + li * len(lines)
        return idx, col, dash

    else:
        return li, pl, col, dash
def visflow_par_str(direc, size, str_type="file"):
    """
    visflow_par_str()

    Returns a string with stim type, as well as size (e.g., 128, 256) and 
    direction (e.g., "right", "left") parameters, unless all possible visual 
    flow parameters values are passed.

    Required args:
        - direc (str or list) : visual flow direction parameter values
        - size (int or list): visual flow square size parameter values

    Optional args:
        - str_type (str) : use of output str, i.e., for a filename ("file") or
                           to print the info to console ("print")
                           default: "file"

    Returns:
        - pars (str): string containing stim type (visual flow) and parameter 
                      values, unless all parameter values for visual flow are 
                      passed.
    """

    if size is None or direc is None:
        raise ValueError("Must pass value for visual flow square size or "
                         "direction parameter.")

    dirstr = dir_par_str(direc, str_type=str_type)
    sizestr = size_par_str(size, str_type=str_type)
    if str_type == "print":
        if len(dirstr) > 10:  # specified direction
            if len(sizestr) > 10:  # specified size
                pars = (f"{sizestr.replace(')', '')}, "
                        f"{dirstr.replace('vis. flow (', '')}")
            else:
                pars = dirstr
        else:
            pars = sizestr
    elif str_type == "file":
        if len(dirstr) > 8:  # specified direction
            if len(sizestr) > 8:
                pars = f"{sizestr}_{dirstr[8:]}"
            else:
                pars = dirstr
        else:
            pars = sizestr
    else:
        gen_util.accepted_values_error("str_type", str_type, ["print", "file"])

    return pars
Exemplo n.º 13
0
def load_small_stim_pkl(stim_pkl, runtype="prod"):
    """
    load_small_stim_pkl(stim_pkl)

    Loads a smaller stimulus dictionary from the stimulus pickle file in which 
    "posbyframe" for visual flow stimuli is not included. 
    
    If it does not exist, small stimulus dictionary is created and saved as a
    pickle with "_small" appended to name.
    
    Reduces the pickle size about 10 fold.

    Required args:
        - stim_pkl (Path): full path name for the full stimulus pickle file
    
    Optional args:
        - runtype (str): runtype ("prod" or "pilot")
    """

    stim_pkl = Path(stim_pkl)
    stim_pkl_no_ext = Path(stim_pkl.parent, stim_pkl.stem)
    small_stim_pkl_name = Path(f"{stim_pkl_no_ext}_small.pkl")
    
    if small_stim_pkl_name.is_file():
        return file_util.loadfile(small_stim_pkl_name)
    else:
        logger.info("Creating smaller stimulus pickle.", extra={"spacing": TAB})

        stim_dict = file_util.loadfile(stim_pkl)

        if runtype == "pilot":
            stim_par_key = "stimParams"
        elif runtype == "prod":
            stim_par_key = "stim_params"
        else:
            gen_util.accepted_values_error(
                "runtype", runtype, ["prod", "pilot"])

        for i in range(len(stim_dict["stimuli"])):
            stim_keys = stim_dict["stimuli"][i][stim_par_key].keys()
            stim_par = stim_dict["stimuli"][i][stim_par_key]
            if runtype == "pilot" and "posByFrame" in stim_keys:
                _ = stim_par.pop("posByFrame")
            elif runtype == "prod" and "square_params" in stim_keys:
                _ = stim_par["session_params"].pop("posbyframe")
                
        file_util.saveinfo(stim_dict, small_stim_pkl_name)

        return stim_dict
def get_sess_dir_path(maindir, sessid, runtype="prod"):
    """
    get_sess_dir_path(maindir, sessid)

    Returns the path to the session directory, and whether a mouse directory 
    is included in the path.

    Required args:
        - maindir (Path): main directory
        - sessid (int)  : session ID

    Optional args:
        - runtype (str): "prod" (production) or "pilot" data
                          default: "prod"

    Returns:
        - sess_dir (Path) : path to the session directory
        - mouse_dir (bool): if True, session information is in a "mouse_*"
                            subdirectory
                            default: True 
    """

    if runtype not in ["pilot", "prod"]:
        gen_util.accepted_values_error("runtype", runtype, ["prod", "pilot"])

    # set the session directory (full path)
    wild_dir = Path(maindir, runtype, "mouse_*", f"ophys_session_{sessid}")
    name_dir = glob.glob(str(wild_dir))

    # pilot data may not be in a "mouse_" folder
    if len(name_dir) == 0:
        wild_dir = Path(maindir, runtype, f"ophys_session_{sessid}")
        name_dir = glob.glob(str(wild_dir))
        mouse_dir = False
    else:
        mouse_dir = True

    if len(name_dir) == 0:
        raise OSError(f"Could not find the directory for session {sessid} "
                      f"(runtype {runtype}) in {maindir} subfolders.")
    elif len(name_dir) > 1:
        raise OSError(f"Found {len(name_dir)} matching session folders in "
                      f"{maindir} instead of 1.")

    sess_dir = Path(name_dir[0])

    return sess_dir, mouse_dir
def depth_vals(plane, line):
    """
    depth_vals(plane, line)

    Returns depth values corresponding to a specified plane.

    Required args:
        - plane (str): plane (e.g., "dend", "soma", "any")
        - line (str) : line (e.g., "L23", "L5", "any")
    Returns:
        - depths (str or list): depths corresponding to plane and line or "any"
    """

    if plane in ["any", "all"] and line in ["any", "all"]:
        return "any"

    depth_dict = {
        "L23_dend": [50, 75],
        "L23_soma": [175],
        "L5_dend": [20],
        "L5_soma": [375]
    }

    all_planes = ["dend", "soma"]
    if plane in ["any", "all"]:
        planes = all_planes
    else:
        planes = gen_util.list_if_not(plane)

    all_lines = ["L23", "L5"]
    if line in ["any", "all"]:
        lines = all_lines
    else:
        lines = gen_util.list_if_not(line)

    depths = []
    for plane in planes:
        if plane not in all_planes:
            allowed_planes = all_planes + ["any", "all"]
            gen_util.accepted_values_error("plane", plane, allowed_planes)
        for line in lines:
            if line not in all_lines:
                allowed_lines = all_lines + ["any", "all"]
                gen_util.accepted_values_error("line", line, allowed_lines)
            depths.extend(depth_dict[f"{line}_{plane}"])

    return depths
def quantile_str(qu_idx=0, n_quants=4, out_of=False, str_type="file"):
    """
    quantile_str()

    Returns a string from quantile values to print or for a filename.

    Optional args:
        - qu_idx (int)  : quantile index
                          default: 0
        - n_quants (int): total number of quantiles
                          default: 4
        - out_of (bool) : if True, quantile index is indicated out of the total
                          (applies to 'print' strings only)
                          default: False
        - str_type (str) : use of output str, i.e., for a filename ("file") or
                           to print the info to console ("print")
                           default: "file"
    
    Returns:
        - quant_str (str): quantile string
    """

    if isinstance(qu_idx, list):
        if len(qu_idx) > 1:
            raise ValueError("If qu_idx is a list, it must be of length 1.")
        qu_idx = qu_idx[0]

    if qu_idx > n_quants - 1:
        raise ValueError("qu_idx must be strictly smaller than n_quants.")

    if qu_idx < 0:
        qu_idx = qu_idx + n_quants

    if str_type == "file":
        quant_str = f"q{qu_idx + 1}"

    elif str_type == "print":
        quant_str = f"Q{qu_idx + 1}"
        if out_of:
            quant_str = f"{quant_str}/{n_quants}"

    else:
        gen_util.accepted_values_error("str_type", str_type, ["print", "file"])

    return quant_str
def size_par_str(size, str_type="file"):
    """
    size_par_str(size)

    Returns a string with stimulus type, as well as size parameters
    (e.g., 128, 256), unless only 128 is passed.

    Required args:
        - size (int or list): visual flow square size parameter

    Optional args:
        - str_type (str) : use of output str, i.e., for a filename ("file") or
                           to print the info to console ("print")
                           default: "file"

    Returns:
        - pars (str): string containing stim type (visual flow) and size, 
                      unless only 128 is passed.
    """

    size = gen_util.list_if_not(size)
    size = [int(s) for s in size]

    if str_type == "file":
        pars = "visflow"
    elif str_type == "print":
        pars = "vis. flow"
    else:
        gen_util.accepted_values_error("str_type", str_type, ["print", "file"])

    if 256 in size:
        if len(size) > 1:
            if str_type == "file":
                pars = f"{pars}_both_siz"
            elif str_type == "print":
                pars = f"{pars} (both square sizes)"
        else:
            if str_type == "file":
                pars = f"{pars}{size[0]}"
            elif str_type == "print":
                pars = f"{pars} ({size[0]})"

    return pars
def gabk_par_str(gabk, str_type="file"):
    """
    gabk_par_str(gabk)

    Returns a string with stim type, as well as kappa parameters
    (e.g., 4, 16), unless only 16 is passed.

    Required args:
        - gabk (int or list): gabor kappa parameter

    Optional args:
        - str_type (str) : use of output str, i.e., for a filename ("file") or
                           to print the info to console ("print")
                           default: "file"

    Returns:
        - pars (str): string containing stim type (gabors) and kappa, 
                      unless only 16 is passed.
    """

    gabk = gen_util.list_if_not(gabk)
    gabk = [int(g) for g in gabk]

    if str_type == "file":
        pars = "gab"
    elif str_type == "print":
        pars = "gabors"
    else:
        gen_util.accepted_values_error("str_type", str_type, ["print", "file"])

    if 4 in gabk:
        if len(gabk) > 1:
            if str_type == "file":
                pars = f"{pars}_both"
            elif str_type == "print":
                pars = f"{pars} (both)"
        else:
            if str_type == "file":
                pars = f"{pars}{gabk[0]}"
            elif str_type == "print":
                pars = f"{pars} ({gabk[0]})"

    return pars
def lat_par_str(method="ttest", p_val_thr=0.005, rel_std=0.5, str_type="file"):
    """
    lat_par_str()

    Returns a string for the latency calculation info.

    Optional args:
        - method (str)     : latency calculating method ("ratio" or "ttest")
                             default: "ttest"
        - p_val_thr (float): p-value threshold for t-test method
                             default: 0.005
        - rel_std (flot)   : relative standard deviation threshold for ratio 
                             method
                             default: 0.5
        - str_type (str)   : use of output str, i.e., for a filename 
                             ("file") or to print the info to console 
                             ("print")
                             default: "file"
    Return:
        - lat_str (str): string containing latency info
    """

    if method == "ttest":
        ext_str = "pval"
        val = p_val_thr
    elif method == "ratio":
        ext_str = "std"
        val = rel_std
    else:
        gen_util.accepted_values_error("method", method, ["ttest", "ratio"])

    if str_type == "print":
        val = gen_util.num_to_str(val, n_dec=5, dec_sep=".")
        lat_str = f"{val} {ext_str}"
    elif str_type == "file":
        val = gen_util.num_to_str(val, n_dec=5, dec_sep="-")
        lat_str = f"{val}{ext_str}"
    else:
        gen_util.accepted_values_error("str_type", str_type, ["print", "file"])

    return lat_str
def datatype_par_str(datatype="roi"):
    """
    datatype_par_str()

    Returns a string for the datatype.

    Optional args:
        - datatype (str): type of data, i.e. "run" or "roi"
                          default: "roi"
    Returns:
        - data_str (list): string containing dimension
    """

    if datatype == "run":
        data_str = "running"
    elif datatype == "roi":
        data_str = "ROI"
    else:
        gen_util.accepted_values_error("datatype", datatype, ["roi", "run"])

    return data_str
def get_nrois(nrois,
              n_bad_rois=0,
              n_bad_rois_dff=0,
              rem_bad=True,
              fluor="dff"):
    """
    get_nrois(nrois)

    Returns number of ROIs based on whether dF/F traces are used and ROIs with 
    NaN/Infs are removed from the data.

    Required args:
        - nrois (int)        : number of ROIs in the session
    
    Optional args:
        - n_bad_rois (int)    : number of ROIs with NaN/Infs in the raw data
        - n_bad_rois_dff (int): number of ROIs with NaN/Infs in the dF/F data
        - rem_bad (bool)     : if True, the number of ROIs with NaN/Infs is  
                               removed from the total
                               default: True
        - fluor (str)        : if "raw", number of ROIs is calculated with 
                               n_bad_rois. If "dff", it is calculated with 
                               n_bad_rois_dff  
                               default: "dff"

    Returns:
        - nrois (int): resulting number of ROIs
    """

    if rem_bad:
        if fluor == "dff":
            n_rem = n_bad_rois_dff
        elif fluor == "raw":
            n_rem = n_bad_rois
        else:
            gen_util.accepted_values_error("fluor", fluor, ["raw", "dff"])

        nrois = nrois - n_rem

    return nrois
Exemplo n.º 22
0
def load_sess_stim_seed(stim_dict, runtype="prod"):
    """
    load_sess_stim_seed(stim_dict)

    Returns session's stimulus seed for this session. Expects all stimuli 
    stored in the session's stimulus dictionary to share the same seed.

    Required args:
        - stim_dict (dict): stimlus dictionary

    Optional args:
        - runtype (str): runtype
                         default: "prod"

    Returns:
        - seed (int): session's stimulus seed
    """

    if runtype == "pilot":
        stim_param_key = "stimParams"
        sess_param_key = "subj_params"
    elif runtype == "prod":
        stim_param_key = "stim_params"
        sess_param_key = "session_params"
    else:
        gen_util.accepted_values_error("runtype", runtype, ["pilot", "prod"])

    seeds = []
    for stimulus in stim_dict["stimuli"]:
        seeds.append(stimulus[stim_param_key][sess_param_key]["seed"])
    
    if np.max(seeds) != np.min(seeds):
        raise RuntimeError("Unexpectedly found different seeds for different "
        "stimuli for this session.")
    
    seed = seeds[0]

    return seed

    
def dir_par_str(direc, str_type="file"):
    """
    dir_par_str(direc)

    Returns a string with stim type, as well as direction parameters
    (e.g., "right", "left"), unless both possible values are passed.

    Required args:
        - direc (str or list): visual flow direction parameter

    Optional args:
        - str_type (str) : use of output str, i.e., for a filename ("file") or
                           to print the info to console ("print")
                           default: "file"

    Returns:
        - pars (str): string containing stim type (visual flow) and direction, 
                      unless both possible values are passed.
    """

    direc = gen_util.list_if_not(direc)
    if str_type == "file":
        pars = "visflow"
    elif str_type == "print":
        pars = "vis. flow"
    else:
        gen_util.accepted_values_error("str_type", str_type, ["print", "file"])

    if len(direc) == 1 and direc[0] != "both":
        direc = direc[0]
        direc_detailed = sess_gen_util.get_visflow_screen_mouse_direc(direc)
        if str_type == "file":
            direc = direc_detailed[:5].strip(" ")  # get left/right
            pars = f"{pars}_{direc}"
        elif str_type == "print":
            direc_detailed = direc_detailed.replace(" (",
                                                    ", ").replace(")", "")
            pars = f"{pars} ({direc_detailed})"

    return pars
def datatype_dim_str(datatype="roi"):
    """
    datatype_dim_str()

    Returns a string for the dimension along which error is calculated for 
    the specified datatype.

    Optional args:
        - datatype (str): type of data, i.e. "run" or "roi"
                          default: "roi"
    Returns:
        - dim_str (list): string containing dimension
    """

    if datatype == "run":
        dim_str = "seqs"
    elif datatype == "roi":
        dim_str = "ROIs"
    else:
        gen_util.accepted_values_error("datatype", datatype, ["roi", "run"])

    return dim_str
Exemplo n.º 25
0
def get_comp_info(permpar):
    """
    get_comp_info(permpar)

    Returns p-value correction information.
    
    Required args:
        - permpar (PermPar or dict): 
            named tuple containing permutation parameters

    Returns:
        - full_comp_info (str): 
            string containing tails and multiple comparisons information
    """

    if isinstance(permpar, dict):
        permpar = sess_ntuple_util.init_permpar(**permpar)

    if permpar.tails == "lo":
        comp_info = "one-tailed"
    elif permpar.tails == "hi":
        comp_info = "one-tailed"
    elif int(permpar.tails) == 2:
        comp_info = "two-tailed"
    else:
        gen_util.accepted_values_error(
            "permpar.tails", permpar.tails, ["lo", "hi", 2]
            )

    if permpar.multcomp:
        comp_info = f"{int(permpar.multcomp)} comparisons, {comp_info}"
        corr_str = "Corrected"
    else:
        corr_str = "Raw"
    
    full_comp_info = f"{corr_str} p-values ({comp_info})"

    return full_comp_info
def get_nroi_strs(sess_info, empty=False, style="comma"):
    """
    get_nroi_strs(sess_info)

    Returns strings with number of ROIs for each session.

    Required args:
        - sess_info (dict): dictionary containing information from each
                            session 
            ["mouse_ns"] (list)   : mouse numbers
            if not empty:
            ["nrois"] (list)      : number of ROIs in session
        - empty (bool)    : if True, empty strings are returned for each session
                            default: False
        - style (str)     : style to use (following a comma ("comma") or in 
                            parentheses ("par"))

    Returns:
        - nroi_strs (list): list of strings containing number of ROIs for each 
                            session
    """

    if empty:
        nroi_strs = [""] * len(sess_info["mouse_ns"])

    else:
        nrois = sess_info["nrois"]

        if style == "comma":
            nroi_strs = [f", n={nroi}" for nroi in nrois]
        elif style == "par":
            nroi_strs = [f" (n={nroi})" for nroi in nrois]
        else:
            gen_util.accepted_values_error("style", style, ["comma", "par"])

    return nroi_strs
def op_par_str(plot_vals="both", op="diff", str_type="file"):
    """
    op_par_str()

    Returns a string from plot values and operation parameters to print or  
    for a filename.

    Optional args:
        - plot_vals (str): "both", "unexp" or "exp"
        - op (str)       : "diff", "ratio"
                           default: "diff"
        - str_type (str) : use of output str, i.e., for a filename ("file") or
                           to print the info to console ("print")
                           default: "file"
    
    Returns:
        - op_str (str): operation type string
    """

    if op not in ["diff", "ratio"]:
        gen_util.accepted_values_error("op", op, ["diff", "ratio"])

    if plot_vals not in ["both", "exp", "unexp"]:
        gen_util.accepted_values_error("plot_vals", plot_vals,
                                       ["both", "exp", "unexp"])

    if plot_vals == "both":
        if str_type == "print":
            op_str = "for unexp v. exp"
        elif str_type == "file":
            op_str = op
        else:
            gen_util.accepted_values_error("str_type", str_type,
                                           ["print", "file"])
    else:
        if str_type == "print":
            op_str = f"for {plot_vals}"
        elif str_type == "file":
            op_str = plot_vals
        else:
            gen_util.accepted_values_error("str_type", str_type,
                                           ["print", "file"])

    return op_str
def stat_par_str(stats="mean", error="sem", str_type="file"):
    """
    stat_par_str()

    Returns a string from statistical analysis parameters to print or for a 
    filename.

    Optional args:
        - stats (str)   : "mean" or "median"
                          default: "mean"
        - error (str)   : "std" (for std or quartiles) or "sem" (for SEM or MAD)
                          or "None" is no error
                          default: "sem"
        - str_type (str): use of output str, i.e., for a filename ("file") or
                          to print the info to console or for title ("print")
                          default: "file"
    
    Returns:
        - stat_str (str): statistics combo string
    """
    if error in ["None", "none"]:
        stat_str = stats
    else:
        if str_type == "print":
            sep = u" \u00B1 "  # +- symbol
        elif str_type == "file":
            sep = "_"
        else:
            gen_util.accepted_values_error("str_type", str_type,
                                           ["print", "file"])

        if stats == "mean":
            if error == "sem":
                error = "SEM"
            stat_str = u"{}{}{}".format(stats, sep, error)
        elif stats == "median":
            if error == "std":
                stat_str = u"{}{}qu".format(stats, sep)
            elif error == "sem":
                stat_str = u"{}{}MAD".format(stats, sep)
            else:
                gen_util.accepted_values_error("error", error,
                                               ["std", "sem", "None", "none"])
        else:
            gen_util.accepted_values_error("stats", stats, ["mean", "median"])
    return stat_str
def fluor_par_str(fluor="dff", str_type="file"):
    """
    fluor_par_str()

    Returns a string from the fluorescence parameter to print or for a 
    filename.

    Optional args:
        - fluor (str)   : whether "raw" or processed fluorescence traces "dff"  
                          are used  
                          default: "dff"
        - str_type (str): "print" for a printable string and "file" for a
                          string usable in a filename.
                          default: "file"
    
    Returns:
        - fluor_str (str): fluorescence parameter string
    """

    if fluor == "raw":
        if str_type == "print":
            fluor_str = "raw fluorescence intensity"
        elif str_type == "file":
            fluor_str = "raw"
        else:
            gen_util.accepted_values_error("str_type", str_type,
                                           ["print", "file"])
    elif fluor == "dff":
        if str_type == "print":
            delta = u"\u0394"
            fluor_str = u"{}F/F".format(delta)
        elif str_type == "file":
            fluor_str = "dff"
        else:
            gen_util.accepted_values_error("str_type", str_type,
                                           ["print", "file"])
    else:
        gen_util.accepted_values_error("fluor", fluor, ["raw", "dff"])

    return fluor_str
def dend_par_str(dend="extr", plane="dend", datatype="roi", str_type="file"):
    """
    dend_par_str()

    Returns a string from dendrite parameter to print or for a filename.

    Optional args:
        - dend (str)     : type of dendrite ("aibs" or "extr")
                           default: "extr"
        - plane (str)    : plane ("dend" or "soma")
                           default: "dend"
        - datatype (str) : type of data ("roi", "run")
                           default: "roi"
        - str_type (str) : use of output str, i.e., for a filename ("file") or
                           to print the info to console ("print")
                           default: "file"
    
    Returns:
        - dend_str (str): dendrite type string
    """

    planes = ["dend", "soma", "any"]
    if plane not in planes:
        gen_util.accepted_values_error("plane", plane, planes)

    datatypes = ["roi", "run"]
    if datatype not in datatypes:
        gen_util.accepted_values_error("datatype", datatype, datatypes)

    dend_str = ""
    if plane in ["dend", "any"] and datatype == "roi" and dend == "aibs":
        if str_type == "file":
            dend_str = "_aibs"
        elif str_type == "print":
            dend_str = " (aibs)"
        else:
            gen_util.accepted_values_error("str_type", str_type,
                                           ["print", "file"])

    return dend_str