Skip to content

Commit

Permalink
refactor(pandas): enable copy_on_write for pandas (cytomining#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
d33bs authored Apr 12, 2024
1 parent f746dd0 commit 62eec0b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
9 changes: 9 additions & 0 deletions pycytominer/__config__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
Module used for pycytominer configuration details.
"""

import pandas as pd

# configure pandas copy_on_write for 3.0.0 requirements
# see: https://pandas.pydata.org/pandas-docs/version/2.2.0/whatsnew/v2.2.0.html#copy-on-write
pd.options.mode.copy_on_write = True
2 changes: 1 addition & 1 deletion pycytominer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pycytominer import __about__
from pycytominer import __about__, __config__

from .aggregate import aggregate
from .annotate import annotate
Expand Down
11 changes: 10 additions & 1 deletion pycytominer/cyto_utils/modz.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import pandas as pd
from pycytominer.cyto_utils.util import (
get_pairwise_correlation,
check_correlation_method,
Expand Down Expand Up @@ -43,9 +44,17 @@ def modz_base(population_df, method="spearman", min_weight=0.01, precision=4):
# Round correlation results
pair_df = pair_df.round(precision)

# create a copy of cor_df values for use with np.fill_diagonal
cor_df_values = cor_df.values.copy()

# Step 2: Identify sample weights
# Fill diagonal of correlation_matrix with np.nan
np.fill_diagonal(cor_df.values, np.nan)
np.fill_diagonal(cor_df_values, np.nan)

# reconstitute the changed data as a new dataframe to avoid read-only behavior
cor_df = pd.DataFrame(
data=cor_df_values, index=cor_df.index, columns=cor_df.columns
)

# Remove negative values
cor_df = cor_df.clip(lower=0)
Expand Down

0 comments on commit 62eec0b

Please sign in to comment.