Skip to content

Commit a646d95

Browse files
added function to bin continuous covariates
1 parent eae5c98 commit a646d95

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

batchglm/api/data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from batchglm.data import design_matrix
22
from batchglm.data import constraint_matrix_from_dict, constraint_matrix_from_string, string_constraints_from_dict, \
33
constraint_system_from_star
4-
from batchglm.data import view_coef_names, preview_coef_names
4+
from batchglm.data import view_coef_names, preview_coef_names, bin_continuous_covariate

batchglm/data.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,3 +453,31 @@ def constraint_matrix_from_string(
453453
)
454454

455455
return constraint_mat
456+
457+
458+
def bin_continuous_covariate(
459+
sample_description: pd.DataFrame,
460+
factor_to_bin: str,
461+
bins: Union[int, list, np.ndarray, Tuple]
462+
):
463+
r"""
464+
Bin a continuous covariate.
465+
466+
Adds the binned covariate to the table. Binning is performed on quantiles of the distribution.
467+
468+
:param sample_description: Sample description table.
469+
:param factor_to_bin: Name of columns of factor to bin.
470+
:param bins: Number of bins or iteratable with bin borders. If given as integer, the bins are defined on the
471+
quantiles of the covariate, ie the bottom 20% of observations are in the first bin if bins==5.
472+
:return: Sample description table with binned covariate added.
473+
"""
474+
if isinstance(bins, list) or isinstance(bins, np.ndarray) or isinstance(bins, Tuple):
475+
bins = np.asarray(bins)
476+
else:
477+
bins = np.arange(0, 1, 1 / bins)
478+
479+
sample_description[factor_to_bin + "_binned"] = np.digitize(
480+
np.argsort(np.argsort(sample_description[factor_to_bin].values)) / sample_description.shape[0],
481+
bins
482+
)
483+
return sample_description

0 commit comments

Comments
 (0)