Skip to content

IO Utilities Module

io_utils

file_utils

get_df_subj(df, i)

This function creates a subject-specific data frame with adjusted index.

Parameters:

Name Type Description Default
df DataFrame

Subject data frame.

required
i int

Subject number.

required

Returns:

Type Description
DataFrame

Index-adjusted subject-specific data frame (df_subj).

Source code in allinpy/io_utils/file_utils.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def get_df_subj(df: pd.DataFrame, i: int) -> pd.DataFrame:
    """This function creates a subject-specific data frame with adjusted index.

    Parameters
    ----------
    df : pd.DataFrame
        Subject data frame.
    i : int
        Subject number.

    Returns
    -------
    pd.DataFrame
        Index-adjusted subject-specific data frame (df_subj).
    """

    df_subj = df[(df["subj_num"] == i + 1)].copy()
    df_subj = df_subj.reset_index(drop=True)  # adjust index

    return df_subj

load_data(f_names, expected_n_trials=400)

This function loads the adaptive learning BIDS data and checks if they are complete.

Parameters:

Name Type Description Default
f_names list[Path]

List with all file names.

required
expected_n_trials int

Expected number of trials.

400

Returns:

Name Type Description
pd DataFrame

Data frame that contains all data.

Source code in allinpy/io_utils/file_utils.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def load_data(f_names: list[Path], expected_n_trials: int = 400) -> pd.DataFrame:
    """This function loads the adaptive learning BIDS data and checks if they are complete.

    Parameters
    ----------
    f_names : list[Path]
        List with all file names.
    expected_n_trials : int
        Expected number of trials.

    Returns
    -------
    pd:DataFrame
        Data frame that contains all data.
    """

    all_dfs = []
    n_trials = []

    for i, fname in enumerate(f_names):
        df = pd.read_csv(fname, sep="\t", header=0)

        n = len(df)
        n_trials.append(n)

        if n_trials[-1] != expected_n_trials:
            print(f"{fname}: {n} trials")

        df["trial"] = np.arange(n, dtype=np.int64)
        all_dfs.append(df)

    all_data = pd.concat(all_dfs, ignore_index=True)

    return all_data

sorted_nicely(input_list)

This function sorts the given iterable in the way that is expected.

Obtained from: https://arcpy.wordpress.com/2012/05/11/sorting-alphanumeric-strings-in-python

Parameters:

Name Type Description Default
input_list list

The iterable to be sorted.

required

Returns:

Type Description
list

Sorted iterable.

Source code in allinpy/io_utils/file_utils.py
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def sorted_nicely(input_list: list) -> list:
    """This function sorts the given iterable in the way that is expected.

    Obtained from:
    https://arcpy.wordpress.com/2012/05/11/sorting-alphanumeric-strings-in-python

    Parameters
    ----------
    input_list : list
        The iterable to be sorted.

    Returns
    -------
    list
        Sorted iterable.
    """

    convert = lambda text: int(text) if text.isdigit() else text
    alphanum_key = lambda key: [convert(c) for c in re.split("([0-9]+)", key)]

    return sorted(input_list, key=alphanum_key)

get_file_paths(folder_path, identifier)

This function extracts the file path.

Parameters:

Name Type Description Default
folder_path Path

Relative path to current folder.

required
identifier Path

Identifier for file of interest.

required

Returns:

Type Description
list[str]

Absolute path to file (file_paths).

Source code in allinpy/io_utils/file_utils.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def get_file_paths(folder_path: Path, identifier: str) -> list[str]:
    """This function extracts the file path.

    Parameters
    ----------
    folder_path : Path
        Relative path to current folder.
    identifier : Path
        Identifier for file of interest.

    Returns
    -------
    list[str]
        Absolute path to file (file_paths).
    """

    file_paths = []
    for path, subdirs, files in os.walk(folder_path):

        for name in files:
            if fnmatch(name, identifier):
                file_paths.append(os.path.join(path, name))

    return file_paths