|
10 | 10 | from causarray.utils import reset_random_seeds, pprint, tqdm, comp_size_factor, _filter_params |
11 | 11 |
|
12 | 12 |
|
| 13 | +def _add_log2fc_columns(df_res): |
| 14 | + """Add base-2 LFC aliases while retaining natural-log result columns.""" |
| 15 | + log2 = np.log(2.0) |
| 16 | + log2fc = df_res['tau'].to_numpy() / log2 |
| 17 | + log2fc_se = df_res['std'].to_numpy() / log2 |
| 18 | + |
| 19 | + # Recompute existing aliases as well as missing ones. This normalizes |
| 20 | + # mixed old/new frames loaded from resumable batch caches and guarantees |
| 21 | + # that the aliases remain exact transformations of tau and std. |
| 22 | + for name in ('log2fc', 'log2fc_se'): |
| 23 | + if name in df_res.columns: |
| 24 | + df_res.pop(name) |
| 25 | + insert_at = df_res.columns.get_loc('std') + 1 |
| 26 | + df_res.insert(insert_at, 'log2fc', log2fc) |
| 27 | + df_res.insert(insert_at + 1, 'log2fc_se', log2fc_se) |
| 28 | + return df_res |
| 29 | + |
| 30 | + |
13 | 31 |
|
14 | 32 | def compute_causal_estimand( |
15 | 33 | estimand, |
@@ -369,9 +387,17 @@ def LFC( |
369 | 387 | Returns |
370 | 388 | ------- |
371 | 389 | df_res : DataFrame |
372 | | - Test results with effect estimates and inference columns, raw |
373 | | - ``mean_control`` and ``mean_treated`` counterfactual means, and an |
374 | | - ``estimable`` flag (plus ``trt`` for multiple treatments). |
| 390 | + Test results with natural-log effect estimate ``tau`` and standard |
| 391 | + error ``std``, together with their base-2 equivalents ``log2fc`` and |
| 392 | + ``log2fc_se``. The fold change is treatment relative to control, and |
| 393 | + ``log2fc_se`` is a standard error (not a sample standard deviation). |
| 394 | + The result also contains inference columns, raw ``mean_control`` and |
| 395 | + ``mean_treated`` counterfactual means, and an ``estimable`` flag (plus |
| 396 | + ``trt`` for multiple treatments). |
| 397 | +
|
| 398 | + .. versionadded:: 0.0.8 |
| 399 | + Added the ``log2fc`` and ``log2fc_se`` convenience columns. The |
| 400 | + original natural-log ``tau`` and ``std`` columns remain unchanged. |
375 | 401 | """ |
376 | 402 |
|
377 | 403 | def estimand(etas, A, **kwargs): |
@@ -458,12 +484,13 @@ def estimand(etas, A, **kwargs): |
458 | 484 | if K is None: |
459 | 485 | K = 2 if cross_est else 1 |
460 | 486 |
|
461 | | - return compute_causal_estimand( |
| 487 | + df_res, estimation = compute_causal_estimand( |
462 | 488 | estimand, Y, W, A, W_A, family, offset, |
463 | 489 | Y_hat=Y_hat, pi_hat=pi_hat, mask=mask, |
464 | 490 | fdx=fdx, fdx_alpha=fdx_alpha, fdx_c=fdx_c, |
465 | 491 | verbose=verbose, backend=backend, K=K, ps_clip=ps_clip, |
466 | 492 | ps_class_weight=ps_class_weight, **kwargs) |
| 493 | + return _add_log2fc_columns(df_res), estimation |
467 | 494 |
|
468 | 495 |
|
469 | 496 |
|
@@ -662,8 +689,10 @@ def gcate_lfc_batch( |
662 | 689 | Returns |
663 | 690 | ------- |
664 | 691 | df_res : DataFrame |
665 | | - Concatenated result from all batches. Includes a ``'batch'`` |
666 | | - column with the 0-based batch index so batches can be identified. |
| 692 | + Concatenated result from all batches. Includes natural-log ``tau`` and |
| 693 | + ``std`` columns, base-2 ``log2fc`` and ``log2fc_se`` columns, and a |
| 694 | + ``'batch'`` column with the 0-based batch index. Older compatible |
| 695 | + caches containing only ``tau`` and ``std`` are upgraded in memory. |
667 | 696 | """ |
668 | 697 | import gc |
669 | 698 | from causarray.gcate import fit_gcate_batch |
@@ -834,7 +863,7 @@ def gcate_lfc_batch( |
834 | 863 | result = pd.concat( |
835 | 864 | [new_dfs[i] for i in all_indices], axis=0 |
836 | 865 | ).reset_index(drop=True) |
837 | | - return result |
| 866 | + return _add_log2fc_columns(result) |
838 | 867 |
|
839 | 868 |
|
840 | 869 | def LFC_batch(*args, **kwargs): |
|
0 commit comments