Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Dec 6, 2024
1 parent 1c1d607 commit 8082e20
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
22 changes: 12 additions & 10 deletions reciprocalspaceship/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class DataSet(pd.DataFrame):
centrics : rs.DataSet
Access only the centric reflections in this dataset
hkls : ndarray, shape=(n_reflections, 3)
Miller indices in DataSet.
Miller indices in DataSet.
merged : bool
Whether this is a merged dataset or unmerged
Expand Down Expand Up @@ -157,27 +157,29 @@ def hkls(self):
return hkl

def get_hkls(self):
""" For backwards compatibility retain the get_hkls method in addition to the dataset.hkls attribute """
"""For backwards compatibility retain the get_hkls method in addition to the dataset.hkls attribute"""
return self.hkls

@hkls.setter
@range_indexed
def hkls(self, hkls):
if isinstance(hkls, DataSet):
""" Convert to numpy if hkls is a dataset """
"""Convert to numpy if hkls is a dataset"""
hkls = hkls.hkls
if isinstance(hkls, np.ndarray):
h,k,l = hkls[...,0], hkls[...,1], hkls[...,2]
h, k, l = hkls[..., 0], hkls[..., 1], hkls[..., 2]
else:
""" Try coercing to numpy """
"""Try coercing to numpy"""
try:
hkls = np.array(hkls)
h,k,l = hkls[...,0], hkls[...,1], hkls[...,2]
h, k, l = hkls[..., 0], hkls[..., 1], hkls[..., 2]
except:
raise ValueError("Unable to convert hkls to a suitable type. Please ensure hkls is a numpy array or rs.DataSet")
self['H'] = DataSeries(h, index = self.index, dtype='H')
self['K'] = DataSeries(k, index = self.index, dtype='H')
self['L'] = DataSeries(l, index = self.index, dtype='H')
raise ValueError(
"Unable to convert hkls to a suitable type. Please ensure hkls is a numpy array or rs.DataSet"
)
self["H"] = DataSeries(h, index=self.index, dtype="H")
self["K"] = DataSeries(k, index=self.index, dtype="H")
self["L"] = DataSeries(l, index=self.index, dtype="H")

@property
def centrics(self):
Expand Down
24 changes: 13 additions & 11 deletions tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,9 @@ def test_select_mtzdtype_ValueError(data_merged, dtype):
@pytest.mark.parametrize("merged", [True, False])
@pytest.mark.parametrize("hkl_as_ds", [True, False])
@pytest.mark.parametrize("range_index", [True, False])
def test_hkls_property_setter(data_merged, data_unmerged, merged, hkl_as_ds, range_index):
def test_hkls_property_setter(
data_merged, data_unmerged, merged, hkl_as_ds, range_index
):
"""
Test the setter for the .hkls property of rs datasets
"""
Expand All @@ -733,20 +735,20 @@ def test_hkls_property_setter(data_merged, data_unmerged, merged, hkl_as_ds, ran
hmax = 20
n = len(ds)

hkls = np.random.randint(-hmax, hmax+1, size=(n, 3))
hkls = np.random.randint(-hmax, hmax + 1, size=(n, 3))
if hkl_as_ds:
hkls = rs.DataSet({
'H' : hkls[...,0],
'K' : hkls[...,1],
'L' : hkls[...,2],
})

hkls = rs.DataSet(
{
"H": hkls[..., 0],
"K": hkls[..., 1],
"L": hkls[..., 2],
}
)

ds.hkls = hkls
assert np.array_equal(hkls, ds.hkls)

# Test that all data remained the same
for k in input_ds:
if k not in ['H', 'K', 'L']:
if k not in ["H", "K", "L"]:
assert np.array_equal(ds[k], input_ds[k])


0 comments on commit 8082e20

Please sign in to comment.