Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated functionality for add_rfree and copy_rfree #170

Merged
merged 8 commits into from
Jul 30, 2022
Prev Previous commit
Next Next commit
Revisions to r-free functionality requested by Jack Greisman
 - renamed custom_rfree_key to custom_rfree_key
 - removed obsolete unittest code
  • Loading branch information
dennisbrookner committed Jul 29, 2022
commit 7930bc8bbae371c6cd7a7c7e1d14af0d54cccb71
11 changes: 5 additions & 6 deletions reciprocalspaceship/utils/rfree.py
Original file line number Diff line number Diff line change
@@ -52,7 +52,7 @@ def add_rfree(dataset, fraction=0.05, ccp4_convention=False, inplace=False, seed
return dataset


def copy_rfree(dataset, dataset_with_rfree, inplace=False, custom_rfree_key=None):
def copy_rfree(dataset, dataset_with_rfree, inplace=False, rfree_key=None):
"""
Copy the rfree flag from one dataset object to another.

@@ -64,7 +64,7 @@ def copy_rfree(dataset, dataset_with_rfree, inplace=False, custom_rfree_key=None
A dataset with desired r-free flags.
inplace : bool, optional
Whether to operate in place or return a copy
custom_rfree_key : str, optional
rfree_key : str, optional
Name of the column containing rfree flags in dataset_with_rfree.
If None, dataset_with_rfree will be checked for column "R-free-flags"
(phenix convention) then column "FreeR_flag" (ccp4 convention)
@@ -76,12 +76,11 @@ def copy_rfree(dataset, dataset_with_rfree, inplace=False, custom_rfree_key=None
if not inplace:
dataset = dataset.copy()

if custom_rfree_key is not None:
if custom_rfree_key not in dataset_with_rfree.columns:
if rfree_key is not None:
if rfree_key not in dataset_with_rfree.columns:
raise ValueError(
f"""Supplied dataset_with_rfree contains no column {custom_rfree_key}"""
f"""Supplied dataset_with_rfree contains no column {rfree_key}"""
)
rfree_key = custom_rfree_key
elif "R-free-flags" in dataset_with_rfree.columns:
rfree_key = "R-free-flags"
elif "FreeR_flag" in dataset_with_rfree.columns:
51 changes: 7 additions & 44 deletions tests/utils/test_rfree.py
Original file line number Diff line number Diff line change
@@ -58,8 +58,8 @@ def test_add_rfree(data_fmodel, fraction, ccp4_convention, inplace, seed):

@pytest.mark.parametrize("ccp4_convention", [False, True])
@pytest.mark.parametrize("inplace", [False, True])
@pytest.mark.parametrize("custom_rfree_key", [None, "custom-rfree-key"])
def test_copy_rfree(data_fmodel, ccp4_convention, inplace, custom_rfree_key):
@pytest.mark.parametrize("rfree_key", [None, "custom-rfree-key"])
def test_copy_rfree(data_fmodel, ccp4_convention, inplace, rfree_key):
"""
Test rs.utils.copy_rfree
"""
@@ -71,22 +71,21 @@ def test_copy_rfree(data_fmodel, ccp4_convention, inplace, custom_rfree_key):
)

# handle different possible column names for rfree flags
if custom_rfree_key is not None:
if rfree_key is not None:
if ccp4_convention:
rename_dict = {"FreeR_flag": custom_rfree_key}
rename_dict = {"FreeR_flag": rfree_key}
else:
rename_dict = {"R-free-flags": custom_rfree_key}
rename_dict = {"R-free-flags": rfree_key}

data_with_rfree.rename(columns=rename_dict, inplace=True)
rfree_key = custom_rfree_key
else:
if ccp4_convention:
rfree_key = "FreeR_flag"
else:
rfree_key = "R-free-flags"

data_with_copied_rfree = rs.utils.copy_rfree(
data_fmodel, data_with_rfree, inplace=inplace, custom_rfree_key=custom_rfree_key
data_fmodel, data_with_rfree, inplace=inplace, rfree_key=rfree_key
)

if inplace:
@@ -112,40 +111,4 @@ def test_copy_rfree_errors(data_fmodel):
rs.utils.copy_rfree(data_fmodel, data_fmodel)

with pytest.raises(ValueError):
rs.utils.copy_rfree(data_fmodel, data_fmodel, custom_rfree_key="missing key")


class TestRfree(unittest.TestCase):
"""
Test rs.utils.copy_free - legacy version using the unittest framework.
"""

def test_copy_rfree(self):

datadir = join(abspath(dirname(__file__)), "../data/fmodel")
data = rs.read_mtz(join(datadir, "9LYZ.mtz"))
data_rfree = rs.utils.add_rfree(data, inplace=False)

# Test copy of R-free to copy of data
rfree = rs.utils.copy_rfree(data, data_rfree, inplace=False)
self.assertFalse(id(data) == id(rfree))
self.assertFalse("R-free-flags" in data.columns)
self.assertTrue("R-free-flags" in rfree.columns)
self.assertTrue(
np.array_equal(
rfree["R-free-flags"].values, data_rfree["R-free-flags"].values
)
)

# Test copy of R-free inplace
rfree = rs.utils.copy_rfree(data, data_rfree, inplace=True)
self.assertTrue(id(data) == id(rfree))
self.assertTrue("R-free-flags" in data.columns)
self.assertTrue("R-free-flags" in rfree.columns)
self.assertTrue(
np.array_equal(
rfree["R-free-flags"].values, data_rfree["R-free-flags"].values
)
)

return
rs.utils.copy_rfree(data_fmodel, data_fmodel, rfree_key="missing key")