Skip to content

neurostatx.utils.preprocessing

Functions

NameDescription
binary_to_yes_noRecode 1/0 columns to Yes / No in place.
compute_correlation_coefficientCompute a Pearson correlation matrix and write a heatmap.
compute_pcaFit a PCA and return scores, diagnostics, and the model.
compute_shapiro_wilk_testRun a Shapiro-Wilk normality test on each column.
get_column_indicesReturn integer positions for the requested column names.
merge_dataframesJoin several tables on a shared index column.
plot_distributionsWrite a histogram and ECDF for each column in df.
remove_nansSplit a table into rows with missing values and complete rows.
rename_columnsRename selected columns and return a copy of the table.

binary_to_yes_nofunction#

def binary_to_yes_no(df, cols)

Recode 1/0 columns to Yes / No in place.

Values of 1 become Yes, 0 become No, and other values become Don't know or missing value.

Parameters

NameTypeDescription
dfpd.DataFrameInput table. Matching columns are modified in place.
colslist of strColumn names to recode.

Returns

dfpd.DataFrame
The same table after recoding.

Examples

>>> import pandas as pd
>>> from neurostatx.utils.preprocessing import binary_to_yes_no
>>> df = pd.DataFrame({"flag": [1.0, 0.0]})
>>> binary_to_yes_no(df, ["flag"])["flag"].tolist()
['Yes', 'No']

compute_correlation_coefficientfunction#

def compute_correlation_coefficient(df, out_folder, context='poster', font_scale=0.2, cmap=None, annot=False)

Compute a Pearson correlation matrix and write a heatmap.

Parameters

NameTypeDefaultDescription
dfpd.DataFramerequiredInput table.
out_folderstrrequiredDirectory where correlation_heatmap.png is written.
contextstr'poster'Seaborn context. Defaults to "poster".
font_scalefloat0.2Seaborn font scale. Defaults to 0.2.
cmapstrNoneHeatmap colormap. Defaults to None.
annotboolFalseIf True, write correlation values on the heatmap. Defaults to False.

Returns

corr_matpd.DataFrame
Pearson correlation matrix.

Examples

>>> import pandas as pd
>>> from neurostatx.utils.preprocessing import (
... compute_correlation_coefficient)
>>> df = pd.DataFrame({"a": [1, 2, 3], "b": [2, 4, 6]})
>>> corr = compute_correlation_coefficient(df, ".")
>>> corr.loc["a", "b"]
1.0

compute_pcafunction#

def compute_pca(X, n_components)

Fit a PCA and return scores, diagnostics, and the model.

Parameters

NameTypeDescription
Xpd.DataFrameInput table.
n_componentsintNumber of components to keep.

Returns

scoresarray
Transformed data.
pcaPCA
Fitted PCA model.
exp_vararray
Explained variance ratio.
componentsarray
Principal axes.
p_valuefloat
Bartlett sphericity p-value.
kmo_modelfloat
Overall KMO statistic.

Examples

>>> import pandas as pd
>>> from neurostatx.utils.preprocessing import compute_pca
>>> X = pd.DataFrame({"a": [1.0, 2.0, 3.0, 4.0],
... "b": [2.0, 1.0, 4.0, 3.0]})
>>> scores, pca, exp_var, components, p_value, kmo = compute_pca(X, 1)
>>> scores.shape
(4, 1)

compute_shapiro_wilk_testfunction#

def compute_shapiro_wilk_test(df)

Run a Shapiro-Wilk normality test on each column.

Parameters

NameTypeDescription
dfpd.DataFrameInput table.

Returns

wilklist of float
Shapiro-Wilk W statistics.
pvalueslist of float
Corresponding p-values.

Examples

>>> import pandas as pd
>>> from neurostatx.utils.preprocessing import compute_shapiro_wilk_test
>>> df = pd.DataFrame({"a": [1.0, 2.0, 3.0, 4.0]})
>>> wilk, pvalues = compute_shapiro_wilk_test(df)
>>> len(wilk)
1

get_column_indicesfunction#

def get_column_indices(df, column_names)

Return integer positions for the requested column names.

Parameters

NameTypeDescription
dfpd.DataFrameInput table.
column_nameslist of strColumn names to look up.

Returns

indiceslist of int
Column positions. Missing names are skipped.

Examples

>>> import pandas as pd
>>> from neurostatx.utils.preprocessing import get_column_indices
>>> df = pd.DataFrame({"a": [1], "b": [2]})
>>> get_column_indices(df, ["b", "a"])
[1, 0]

merge_dataframesfunction#

def merge_dataframes(dict_df, index, repeated_columns=False)

Join several tables on a shared index column.

Index values must be unique within each table.

Parameters

NameTypeDefaultDescription
dict_dfdictrequiredMapping of labels to DataFrames.
indexstrrequiredShared column used as the join index.
repeated_columnsboolFalseIf True, disambiguate overlapping column names with suffixes. Defaults to False.

Returns

outpd.DataFrame
Joined table.

Examples

>>> import pandas as pd
>>> from neurostatx.utils.preprocessing import merge_dataframes
>>> left = pd.DataFrame({"id": [1, 2], "a": [10, 20]})
>>> right = pd.DataFrame({"id": [1, 2], "b": [3, 4]})
>>> merge_dataframes({"l": left, "r": right}, "id")["a"].tolist()
[10, 20]

plot_distributionsfunction#

def plot_distributions(df, out_folder, context='poster', font_scale=1)

Write a histogram and ECDF for each column in df.

Parameters

NameTypeDefaultDescription
dfpd.DataFramerequiredInput table.
out_folderstrrequiredDirectory where {column}.png files are written.
contextstr'poster'Seaborn context. Defaults to "poster".
font_scalefloat1Seaborn font scale. Defaults to 1.

Examples

>>> import pandas as pd
>>> from neurostatx.utils.preprocessing import plot_distributions
>>> df = pd.DataFrame({"a": [1, 2, 3]})
>>> plot_distributions(df, ".")

remove_nansfunction#

def remove_nans(df)

Split a table into rows with missing values and complete rows.

Parameters

NameTypeDescription
dfpd.DataFrameInput table.

Returns

rows_with_nanspd.DataFrame
Rows that contain at least one NaN.
complete_rowspd.DataFrame
Rows with no missing values.

Examples

>>> import pandas as pd
>>> import numpy as np
>>> from neurostatx.utils.preprocessing import remove_nans
>>> df = pd.DataFrame({"a": [1.0, np.nan], "b": [2.0, 3.0]})
>>> nans, complete = remove_nans(df)
>>> len(complete)
1

rename_columnsfunction#

def rename_columns(df, old_names, new_names)

Rename selected columns and return a copy of the table.

Parameters

NameTypeDescription
dfpd.DataFrameInput table.
old_nameslist of strCurrent column names.
new_nameslist of strReplacement column names, same length as old_names.

Returns

new_dfpd.DataFrame
Copy of df with renamed columns.

Examples

>>> import pandas as pd
>>> from neurostatx.utils.preprocessing import rename_columns
>>> df = pd.DataFrame({"a": [1], "b": [2]})
>>> rename_columns(df, ["a"], ["x"]).columns.tolist()
['x', 'b']