From bc2cbab6b964ca5f70734c2890d67cf9b63a959d Mon Sep 17 00:00:00 2001 From: Leo-GG Date: Tue, 23 Jan 2024 17:20:19 +0100 Subject: [PATCH] Update core.py Removes the warning from pandas triggered by using append() to extend the r2 dataframe by replacing each call to r2_df.append() with a call to pd.concat() --- mofax/core.py | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/mofax/core.py b/mofax/core.py index d33452d..63dfbb3 100644 --- a/mofax/core.py +++ b/mofax/core.py @@ -1015,14 +1015,13 @@ def calculate_variance_explained( ), Y=np.array(self.data[view][group]), ) - r2_df = r2_df.append( - { + r2_df = pd.concat([r2_df, pd.DataFrame( + [{ "View": view, "Group": group, "R2": r2, "Factor": factor_name, - }, - ignore_index=True, + }])], ignore_index=True ) else: r2 = calculate_r2( @@ -1032,9 +1031,13 @@ def calculate_variance_explained( W=np.array(self.expectations["W"][view][factor_indices, :]), Y=np.array(self.data[view][group]), ) - r2_df = r2_df.append( - {"View": view, "Group": group, "R2": r2}, ignore_index=True - ) + r2_df = pd.concat([r2_df, pd.DataFrame( + [{ + "View": view, + "Group": group, + "R2": r2 + }])], ignore_index=True + ) # use custom groups # note that when calculating for a custom set of groups, @@ -1073,14 +1076,13 @@ def calculate_variance_explained( ), Y=np.array(data_view[group]), ) - r2_df = r2_df.append( - { + r2_df = pd.concat([r2_df, pd.DataFrame( + [{ "View": view, "Group": group, "R2": r2, "Factor": factor_name, - }, - ignore_index=True, + }])], ignore_index=True ) else: r2 = calculate_r2( @@ -1088,9 +1090,13 @@ def calculate_variance_explained( W=np.array(self.expectations["W"][view][factor_indices, :]), Y=np.array(data_view[group]), ) - r2_df = r2_df.append( - {"View": view, "Group": group, "R2": r2}, ignore_index=True - ) + r2_df = pd.concat([r2_df, pd.DataFrame( + [{ + "View": view, + "Group": group, + "R2": r2 + }])], ignore_index=True + ) return r2_df def get_variance_explained( @@ -1256,15 +1262,14 @@ def _get_factor_r2_null( y = np.array(data_view[group]) a = np.sum((y - crossprod) ** 2) b = np.sum(y ** 2) - r2_df = r2_df.append( - { + r2_df = pd.concat([r2_df, pd.DataFrame( + [{ "View": view, "Group": group, "Factor": f"Factor{factor_index+1}", "R2": 1 - a / b, "Iteration": i, - }, - ignore_index=True, + }])], ignore_index=True ) if return_full: