def add_real_x(item):
    """fn to map over list of items returned by `get_groups_per_sweep`"""
    i_res = item.copy()
    abf = pyabf.ABF(item["fpath"])
    time = abf.sweepX
    peak_ints = db.str_list_to_list_ints(item["peaks"])
    i_res["peak_times"] = time[peak_ints]
    return i_res
def return_flat_list_of_treatment(dict_of_groups, treatment):
    try:
        abf = pyabf.ABF(dict_of_groups[0]["fpath"])
        time = abf.sweepX
    except TypeError as e:
        print(f"ERROR! \n {dict_of_groups}\n")
    target = [
        db.str_list_to_list_ints(peaks["peaks"])
        for peaks in dict_of_groups
        if peaks["treatment"] == treatment
    ]
    empty_less = [i for i in target if i]
    flat = [item for sublist in empty_less for item in sublist]
    x_time = time[flat]
    return x_time, flat
def count_peaks_treatments(data_dict, treatment):
    target = [
        db.str_list_to_list_ints(peaks["peaks"])
        for peaks in data_dict
        if peaks["treatment"] == treatment
    ]
    n_cells = len(target)
    n_spikes = [len(i) for i in target]
    mean_spikes = np.mean(n_spikes)
    sd_spikes = np.std(n_spikes)
    return {
        "treatment": treatment,
        "n_cells": n_cells,
        "n_spikes": n_spikes,
        "mean_spikes": mean_spikes,
        "sd_spikes": sd_spikes,
    }
def get_groups_per_sweep(sweep, include, query, db_path):
    """pulls out all the data in a sweep and structures it as a dict.
    String of integers (`peaks`) is transformed into a list of ints or empty list."""
    con = db.persistent_connection_to_db(db_path)
    con.row_factory = sqlite3.Row
    data = con.execute(query, (sweep, include))
    extracted = [{
        "fname": p["fname"],
        "fpath": p["fpath"],
        "mouse_id": p["mouse_id"],
        "cell_side": p["cell_side"],
        "cell_n": p["cell_n"],
        "memb_potential": p["membrane_potential_uncorrected"],
        "include": p["include"],
        "protocol": p["protocol"],
        "peaks": db.str_list_to_list_ints(p["peak_index"]),
        "treatment": p["treatment_group"],
        "sweep": sweep,
    } for p in data]
    con.close()
    return extracted
def test_list_to_ints_and_back_empty_list():
    list_empty = []
    l_str = database.list_of_ints_to_str(list_empty)
    decoded = database.str_list_to_list_ints(l_str)
    assert list_empty == decoded
def test_assertion_throw_float_in_ints():
    string_with_float = "1,2,3.5,4"
    with pytest.raises(ValueError):
        database.str_list_to_list_ints(string_with_float)
def test_list_to_ints_and_back():
    list_ints = [1, 2, 3, 4]
    l_str = database.list_of_ints_to_str(list_ints)
    decoded = database.str_list_to_list_ints(l_str)
    assert list_ints == decoded
def test_str_list_to_list_ints():
    string_ints = "1,2,3,4"
    converted = database.str_list_to_list_ints(string_ints)
    assert [1, 2, 3, 4] == converted
def peaks_to_int_list(ditem):
    """helper to convert indicies from database to integers"""
    ditem["peak_index"] = db.str_list_to_list_ints(ditem["peak_index"])
    return ditem