Skip to content

Commit

Permalink
Merge pull request #149 from Hekstra-Lab/append
Browse files Browse the repository at this point in the history
Remove `DataSet.append()` due to upcoming deprecation in Pandas
  • Loading branch information
JBGreisman committed May 23, 2022
2 parents f7d2a2c + c040f92 commit 86ea1cb
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 112 deletions.
4 changes: 0 additions & 4 deletions reciprocalspaceship/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ def concat(*args, check_isomorphous=True, **kwargs):
rs.DataSet or rs.DataSeries
Returns rs.DataSeries when concatenating rs.DataSeries along the
index (axis=0). Returns rs.DataSet in all other cases.
See Also
--------
DataSet.append : Concatenate DataSets
"""
objs = kwargs.get("objs", args[0])
objs, objs_tee = tee(objs)
Expand Down
52 changes: 4 additions & 48 deletions reciprocalspaceship/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pandas as pd
from pandas.api.types import is_complex_dtype

from reciprocalspaceship import dtypes
from reciprocalspaceship import concat, dtypes
from reciprocalspaceship.dataseries import DataSeries
from reciprocalspaceship.decorators import (
cellify,
Expand Down Expand Up @@ -404,50 +404,6 @@ def from_structurefactor(self, sf_key):
"""
return from_structurefactor(self[sf_key])

def append(self, *args, check_isomorphous=True, **kwargs):
"""
Append rows of `other` to the end of calling DataSet, returning
a new DataSet object. Any columns in `other` that are not present
in the calling DataSet are added as new columns.
For additional documentation on accepted arguments, see the
`Pandas DataFrame.append() API <https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.append.html>`_.
Parameters
----------
check_isomorphous : bool
If True, the spacegroup and cell attributes of DataSets in `other`
will be compared to those of the calling DataSet to ensure
they are isomorphous.
Returns
-------
rs.DataSet
See Also
--------
concat : Concatenate ``rs`` objects
"""
other = kwargs.get("other", args[0])
if check_isomorphous:
if isinstance(other, (list, tuple)):
for o in other:
if isinstance(o, DataSet) and not self.is_isomorphous(o):
raise ValueError("DataSet in `other` is not isomorphous")
else:
if isinstance(other, DataSet) and not self.is_isomorphous(other):
raise ValueError("`other` DataSet is not isomorphous")
result = super().append(*args, **kwargs)

# If `ignore_index=True`, the _index_dtypes attribute should
# be reset.
if isinstance(result.index, pd.RangeIndex) and self._index_dtypes != {}:
result.__finalize__(self)
result._index_dtypes = {}
return result

return result.__finalize__(self)

def merge(self, *args, check_isomorphous=True, **kwargs):
"""
Merge DataSet or named DataSeries using a database-style join on
Expand Down Expand Up @@ -912,7 +868,7 @@ def stack_anomalous(
column_mapping_minus = dict(zip(minus_labels, new_labels))
dataset_plus.rename(columns=column_mapping_plus, inplace=True)
dataset_minus.rename(columns=column_mapping_minus, inplace=True)
F = dataset_plus.append(dataset_minus)
F = concat([dataset_plus, dataset_minus])
for label in new_labels:
F[label] = F[label].from_friedel_dtype()

Expand Down Expand Up @@ -1216,7 +1172,7 @@ def expand_to_p1(self):
ds = self.copy()
ds["M/ISYM"] = isym
ds["M/ISYM"] = ds["M/ISYM"].astype("M/ISYM")
p1 = p1.append(ds.hkl_to_observed(m_isym="M/ISYM"))
p1 = concat([p1, ds.hkl_to_observed(m_isym="M/ISYM")])
p1.drop_duplicates(subset=["H", "K", "L"], inplace=True)

# Restrict to p1 ASU
Expand All @@ -1236,7 +1192,7 @@ def expand_anomalous(self):
DataSet
"""
friedel = self.apply_symop("-x,-y,-z")
return self.append(friedel)
return concat([self, friedel])

@inplace
def canonicalize_phases(self, inplace=False):
Expand Down
2 changes: 1 addition & 1 deletion reciprocalspaceship/utils/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def compute_redundancy(
spacegroup=spacegroup,
).set_index(["H", "K", "L"])
ASU = ASU.loc[ASU.index.difference(mult.index)]
mult = mult.append(ASU)
mult = rs.concat([mult, ASU])

mult = mult.sort_index()
return mult.get_hkls(), mult["Count"].to_numpy(np.int32)
59 changes: 0 additions & 59 deletions tests/test_dataset_preserve_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,65 +37,6 @@ def test_concat(data_fmodel, check_isomorphous, sg, ignore_index):
assert result.__getattr__(attr) == data_fmodel.__getattr__(attr)


@pytest.mark.parametrize("check_isomorphous", [True, False])
@pytest.mark.parametrize("sg", [gemmi.SpaceGroup(19), gemmi.SpaceGroup(96)])
@pytest.mark.parametrize("ignore_index", [True, False])
def test_append(data_fmodel, check_isomorphous, sg, ignore_index):
"""
Test whether attributes of DataSet are preserved through calls to
DataSet.append()
"""
other = data_fmodel.copy(deep=True)
other.spacegroup = sg
if check_isomorphous and sg.number == 19:
with pytest.raises(ValueError):
result = data_fmodel.append(
other, ignore_index, check_isomorphous=check_isomorphous
)
else:
result = data_fmodel.append(
other, ignore_index, check_isomorphous=check_isomorphous
)
assert isinstance(result, rs.DataSet)
assert len(result) == len(data_fmodel) * 2
if ignore_index:
assert result._index_dtypes == {}
for attr in data_fmodel._metadata:
if attr == "_index_dtypes":
continue
assert result.__getattr__(attr) == data_fmodel.__getattr__(attr)


@pytest.mark.parametrize("check_isomorphous", [True, False])
@pytest.mark.parametrize("sg", [gemmi.SpaceGroup(19), gemmi.SpaceGroup(96)])
@pytest.mark.parametrize("ignore_index", [True, False])
def test_append_list(data_fmodel, check_isomorphous, sg, ignore_index):
"""
Test whether attributes of DataSet are preserved through calls to
DataSet.append() using a list for `other` argument
"""
other = data_fmodel.copy(deep=True)
other.spacegroup = sg
other = [other] * 3
if check_isomorphous and sg.number == 19:
with pytest.raises(ValueError):
result = data_fmodel.append(
other, ignore_index, check_isomorphous=check_isomorphous
)
else:
result = data_fmodel.append(
other, ignore_index, check_isomorphous=check_isomorphous
)
assert isinstance(result, rs.DataSet)
assert len(result) == len(data_fmodel) * 4
if ignore_index:
assert result._index_dtypes == {}
for attr in data_fmodel._metadata:
if attr == "_index_dtypes":
continue
assert result.__getattr__(attr) == data_fmodel.__getattr__(attr)


@pytest.mark.parametrize("check_isomorphous", [True, False])
@pytest.mark.parametrize("sg", [gemmi.SpaceGroup(19), gemmi.SpaceGroup(96)])
def test_merge(data_fmodel, check_isomorphous, sg):
Expand Down

0 comments on commit 86ea1cb

Please sign in to comment.