From 1c1d607c30b43ec46008d4e66d5b42b40bae0578 Mon Sep 17 00:00:00 2001 From: Kevin Dalton Date: Fri, 6 Dec 2024 16:37:35 -0500 Subject: [PATCH 01/10] add hkls attribute rs.DataSet --- reciprocalspaceship/dataset.py | 48 ++++++++++++++++++++++++++++++++++ tests/test_dataset.py | 39 +++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/reciprocalspaceship/dataset.py b/reciprocalspaceship/dataset.py index aa187a49..408df066 100644 --- a/reciprocalspaceship/dataset.py +++ b/reciprocalspaceship/dataset.py @@ -43,6 +43,24 @@ class DataSet(pd.DataFrame): and attributes, please see the `Pandas.DataFrame documentation`_. .. _Pandas.DataFrame documentation: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html + + Attributes + ---------- + acentrics : rs.DataSet + Access only the acentric reflections in this dataset + cell : gemmi.UnitCell + The unit cell + centrics : rs.DataSet + Access only the centric reflections in this dataset + hkls : ndarray, shape=(n_reflections, 3) + Miller indices in DataSet. + merged : bool + Whether this is a merged dataset or unmerged + + spacegroup : gemmi.SpaceGroup + The space group + reindexing_ops : list + Possible reindexing ops consistent with the cell and spacegroup """ _metadata = ["_spacegroup", "_cell", "_index_dtypes", "_merged"] @@ -131,6 +149,36 @@ def merged(self): def merged(self, val): self._merged = val + @property + @range_indexed + def hkls(self): + """Miller indices""" + hkl = self[["H", "K", "L"]].to_numpy(dtype=np.int32) + return hkl + + def get_hkls(self): + """ 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 """ + hkls = hkls.hkls + if isinstance(hkls, np.ndarray): + h,k,l = hkls[...,0], hkls[...,1], hkls[...,2] + else: + """ Try coercing to numpy """ + try: + hkls = np.array(hkls) + 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') + @property def centrics(self): """Access centric reflections in DataSet""" diff --git a/tests/test_dataset.py b/tests/test_dataset.py index e0b4bf61..d47d8e28 100644 --- a/tests/test_dataset.py +++ b/tests/test_dataset.py @@ -711,3 +711,42 @@ def test_select_mtzdtype_ValueError(data_merged, dtype): """ with pytest.raises(ValueError): data_merged.select_mtzdtype(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): + """ + Test the setter for the .hkls property of rs datasets + """ + if merged: + input_ds = data_merged + else: + input_ds = data_unmerged + + ds = input_ds.copy() + + if range_index: + ds = ds.reset_index() + + hmax = 20 + n = len(ds) + + 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], + }) + + 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']: + assert np.array_equal(ds[k], input_ds[k]) + + From 8082e20ba14c12eb501cf438e59bb8df509bbfc4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Dec 2024 21:39:51 +0000 Subject: [PATCH 02/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- reciprocalspaceship/dataset.py | 22 ++++++++++++---------- tests/test_dataset.py | 24 +++++++++++++----------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/reciprocalspaceship/dataset.py b/reciprocalspaceship/dataset.py index 408df066..c2433548 100644 --- a/reciprocalspaceship/dataset.py +++ b/reciprocalspaceship/dataset.py @@ -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 @@ -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): diff --git a/tests/test_dataset.py b/tests/test_dataset.py index d47d8e28..d202b00b 100644 --- a/tests/test_dataset.py +++ b/tests/test_dataset.py @@ -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 """ @@ -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]) - - From dcafb5b153e69935795ddaf5cd90a03eb3714de6 Mon Sep 17 00:00:00 2001 From: Kevin Dalton Date: Wed, 18 Dec 2024 10:35:29 -0500 Subject: [PATCH 03/10] fix whitespace --- reciprocalspaceship/dataset.py | 1 - 1 file changed, 1 deletion(-) diff --git a/reciprocalspaceship/dataset.py b/reciprocalspaceship/dataset.py index c2433548..ce53c67f 100644 --- a/reciprocalspaceship/dataset.py +++ b/reciprocalspaceship/dataset.py @@ -56,7 +56,6 @@ class DataSet(pd.DataFrame): Miller indices in DataSet. merged : bool Whether this is a merged dataset or unmerged - spacegroup : gemmi.SpaceGroup The space group reindexing_ops : list From a4fb02335a4e863de140ec976f940320409cbc84 Mon Sep 17 00:00:00 2001 From: Kevin Dalton Date: Wed, 18 Dec 2024 10:37:27 -0500 Subject: [PATCH 04/10] update docstring --- reciprocalspaceship/dataset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reciprocalspaceship/dataset.py b/reciprocalspaceship/dataset.py index ce53c67f..0fa5271c 100644 --- a/reciprocalspaceship/dataset.py +++ b/reciprocalspaceship/dataset.py @@ -156,7 +156,7 @@ def hkls(self): return hkl def get_hkls(self): - """For backwards compatibility retain the get_hkls method in addition to the dataset.hkls attribute""" + """Get the Miller indices of the dataset.""" return self.hkls @hkls.setter From 4c950c4d39e93bfa320e41bdb4ff19659113c073 Mon Sep 17 00:00:00 2001 From: Kevin Dalton Date: Thu, 19 Dec 2024 09:28:23 -0500 Subject: [PATCH 05/10] use fixtures for miller indices --- tests/conftest.py | 60 ++++++++++++++++++++++++------------------- tests/test_dataset.py | 44 ++++++++++++++++++++----------- 2 files changed, 62 insertions(+), 42 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index b454edbb..69059419 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,33 +8,6 @@ import reciprocalspaceship as rs - -@pytest.fixture -def hkls(): - """ - Return all Miller indices with H, K, L values between [-5, 5] - """ - hmin, hmax = -5, 5 - H = np.mgrid[hmin : hmax + 1, hmin : hmax + 1, hmin : hmax + 1].reshape((3, -1)).T - return H - - -@pytest.fixture -def dataset_hkl(): - """ - Build DataSet for testing containing only Miller indices - """ - hmin, hmax = -5, 5 - H = ( - np.mgrid[hmin : hmax + 1 : 2, hmin : hmax + 1 : 2, hmin : hmax + 1 : 2] - .reshape((3, -1)) - .T - ) - dataset = rs.DataSet({"H": H[:, 0], "K": H[:, 1], "L": H[:, 2]}) - dataset.set_index(["H", "K", "L"], inplace=True) - return dataset - - def load_dataset(datapath, as_gemmi=False): """ Load dataset at given datapath. Datapath is expected to be a list of @@ -64,6 +37,39 @@ def data_unmerged(): datapath = ["data", "data_unmerged.mtz"] return load_dataset(datapath) +@pytest.fixture +def hkls(data_merged): + """ + Return all Miller indices with H, K, L values between [-5, 5] + """ + return data_merged.hkls + +@pytest.fixture +def dataset_hkl(data_merged): + """ + Build DataSet for testing containing only Miller indices + """ + H = data_merged.hkls + dataset = rs.DataSet({"H": H[:, 0], "K": H[:, 1], "L": H[:, 2]}) + dataset.set_index(["H", "K", "L"], inplace=True) + return dataset + +@pytest.fixture +def hkls_unmerged(data_unmerged): + """ + Return all Miller indices with H, K, L values between [-5, 5] + """ + return data_unmerged.hkls + +@pytest.fixture +def dataset_hkl_unmerged(data_unmerged): + """ + Build DataSet for testing containing only Miller indices + """ + H = data_unmerged.hkls + dataset = rs.DataSet({"H": H[:, 0], "K": H[:, 1], "L": H[:, 2]}) + dataset.set_index(["H", "K", "L"], inplace=True) + return dataset @pytest.fixture( params=[ diff --git a/tests/test_dataset.py b/tests/test_dataset.py index d202b00b..5cf7c4e2 100644 --- a/tests/test_dataset.py +++ b/tests/test_dataset.py @@ -714,39 +714,53 @@ 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("hkl_type", ['ds', 'index', 'numpy']) @pytest.mark.parametrize("range_index", [True, False]) def test_hkls_property_setter( - data_merged, data_unmerged, merged, hkl_as_ds, range_index + data_merged, data_unmerged, hkls, hkls_unmerged, dataset_hkl, dataset_hkl_unmerged, merged, hkl_type, range_index ): """ Test the setter for the .hkls property of rs datasets """ if merged: input_ds = data_merged + hkls = dataset_hkl else: input_ds = data_unmerged + hkls = dataset_hkl_unmerged - ds = input_ds.copy() + if hkl_type == 'ds': + hkls = hkls.reset_index() + elif hkl_type == 'numpy': + hkls = hkls.hkls + ds = input_ds.copy() if range_index: ds = ds.reset_index() - hmax = 20 - n = len(ds) + # Confirm we're starting with equivalent miller indices + expected = ds.hkls + value = hkls + if hkl_type != 'numpy': + value = value.hkls + assert np.array_equal(value, expected) + + # Shuffle the hkls + if hkl_type != 'numpy': + hkls = hkls.sample(frac=1.) + else: + np.random.shuffle(hkls) #inplace - 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], - } - ) + # confirm shuffling + assert not np.array_equal(hkls, ds.hkls) + # confirm setter ds.hkls = hkls - assert np.array_equal(hkls, ds.hkls) + expected = ds.hkls + value = hkls + if hkl_type != 'numpy': + value = value.hkls + assert np.array_equal(value, expected) # Test that all data remained the same for k in input_ds: From d91f81ff4a9dc86ab019666fdfdd1158a2320acd Mon Sep 17 00:00:00 2001 From: Kevin Dalton Date: Thu, 19 Dec 2024 09:46:13 -0500 Subject: [PATCH 06/10] update syntax to avoid deprecation --- reciprocalspaceship/io/precognition.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/reciprocalspaceship/io/precognition.py b/reciprocalspaceship/io/precognition.py index 3ad146ce..5eee47e3 100644 --- a/reciprocalspaceship/io/precognition.py +++ b/reciprocalspaceship/io/precognition.py @@ -31,7 +31,7 @@ def read_precognition(hklfile, spacegroup=None, cell=None, logfile=None): F = pd.read_csv( hklfile, header=None, - delim_whitespace=True, + sep="\\s+", names=["H", "K", "L", "F(+)", "SigF(+)", "F(-)", "SigF(-)"], usecols=usecols, ) @@ -49,7 +49,7 @@ def read_precognition(hklfile, spacegroup=None, cell=None, logfile=None): F = pd.read_csv( hklfile, header=None, - delim_whitespace=True, + sep="\\s+", names=[ "H", "K", From 53d76b3fcd536ea233570b42b9a4fbc272b3659c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 19 Dec 2024 19:27:57 +0000 Subject: [PATCH 07/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/conftest.py | 6 ++++++ tests/test_dataset.py | 26 +++++++++++++++++--------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 69059419..3782345a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,6 +8,7 @@ import reciprocalspaceship as rs + def load_dataset(datapath, as_gemmi=False): """ Load dataset at given datapath. Datapath is expected to be a list of @@ -37,6 +38,7 @@ def data_unmerged(): datapath = ["data", "data_unmerged.mtz"] return load_dataset(datapath) + @pytest.fixture def hkls(data_merged): """ @@ -44,6 +46,7 @@ def hkls(data_merged): """ return data_merged.hkls + @pytest.fixture def dataset_hkl(data_merged): """ @@ -54,6 +57,7 @@ def dataset_hkl(data_merged): dataset.set_index(["H", "K", "L"], inplace=True) return dataset + @pytest.fixture def hkls_unmerged(data_unmerged): """ @@ -61,6 +65,7 @@ def hkls_unmerged(data_unmerged): """ return data_unmerged.hkls + @pytest.fixture def dataset_hkl_unmerged(data_unmerged): """ @@ -71,6 +76,7 @@ def dataset_hkl_unmerged(data_unmerged): dataset.set_index(["H", "K", "L"], inplace=True) return dataset + @pytest.fixture( params=[ ["data", "data_merged.mtz"], diff --git a/tests/test_dataset.py b/tests/test_dataset.py index 5cf7c4e2..d44f160d 100644 --- a/tests/test_dataset.py +++ b/tests/test_dataset.py @@ -714,10 +714,18 @@ def test_select_mtzdtype_ValueError(data_merged, dtype): @pytest.mark.parametrize("merged", [True, False]) -@pytest.mark.parametrize("hkl_type", ['ds', 'index', 'numpy']) +@pytest.mark.parametrize("hkl_type", ["ds", "index", "numpy"]) @pytest.mark.parametrize("range_index", [True, False]) def test_hkls_property_setter( - data_merged, data_unmerged, hkls, hkls_unmerged, dataset_hkl, dataset_hkl_unmerged, merged, hkl_type, range_index + data_merged, + data_unmerged, + hkls, + hkls_unmerged, + dataset_hkl, + dataset_hkl_unmerged, + merged, + hkl_type, + range_index, ): """ Test the setter for the .hkls property of rs datasets @@ -729,9 +737,9 @@ def test_hkls_property_setter( input_ds = data_unmerged hkls = dataset_hkl_unmerged - if hkl_type == 'ds': + if hkl_type == "ds": hkls = hkls.reset_index() - elif hkl_type == 'numpy': + elif hkl_type == "numpy": hkls = hkls.hkls ds = input_ds.copy() @@ -741,15 +749,15 @@ def test_hkls_property_setter( # Confirm we're starting with equivalent miller indices expected = ds.hkls value = hkls - if hkl_type != 'numpy': + if hkl_type != "numpy": value = value.hkls assert np.array_equal(value, expected) # Shuffle the hkls - if hkl_type != 'numpy': - hkls = hkls.sample(frac=1.) + if hkl_type != "numpy": + hkls = hkls.sample(frac=1.0) else: - np.random.shuffle(hkls) #inplace + np.random.shuffle(hkls) # inplace # confirm shuffling assert not np.array_equal(hkls, ds.hkls) @@ -758,7 +766,7 @@ def test_hkls_property_setter( ds.hkls = hkls expected = ds.hkls value = hkls - if hkl_type != 'numpy': + if hkl_type != "numpy": value = value.hkls assert np.array_equal(value, expected) From d7ec1a808a130e7be2918ed0ba1ad499b6c35ec7 Mon Sep 17 00:00:00 2001 From: Kevin Dalton Date: Wed, 1 Jan 2025 13:50:58 -0500 Subject: [PATCH 08/10] revert pytest fixtures --- tests/conftest.py | 64 +++++++++++++++++++---------------------------- 1 file changed, 26 insertions(+), 38 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 3782345a..b454edbb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -9,6 +9,32 @@ import reciprocalspaceship as rs +@pytest.fixture +def hkls(): + """ + Return all Miller indices with H, K, L values between [-5, 5] + """ + hmin, hmax = -5, 5 + H = np.mgrid[hmin : hmax + 1, hmin : hmax + 1, hmin : hmax + 1].reshape((3, -1)).T + return H + + +@pytest.fixture +def dataset_hkl(): + """ + Build DataSet for testing containing only Miller indices + """ + hmin, hmax = -5, 5 + H = ( + np.mgrid[hmin : hmax + 1 : 2, hmin : hmax + 1 : 2, hmin : hmax + 1 : 2] + .reshape((3, -1)) + .T + ) + dataset = rs.DataSet({"H": H[:, 0], "K": H[:, 1], "L": H[:, 2]}) + dataset.set_index(["H", "K", "L"], inplace=True) + return dataset + + def load_dataset(datapath, as_gemmi=False): """ Load dataset at given datapath. Datapath is expected to be a list of @@ -39,44 +65,6 @@ def data_unmerged(): return load_dataset(datapath) -@pytest.fixture -def hkls(data_merged): - """ - Return all Miller indices with H, K, L values between [-5, 5] - """ - return data_merged.hkls - - -@pytest.fixture -def dataset_hkl(data_merged): - """ - Build DataSet for testing containing only Miller indices - """ - H = data_merged.hkls - dataset = rs.DataSet({"H": H[:, 0], "K": H[:, 1], "L": H[:, 2]}) - dataset.set_index(["H", "K", "L"], inplace=True) - return dataset - - -@pytest.fixture -def hkls_unmerged(data_unmerged): - """ - Return all Miller indices with H, K, L values between [-5, 5] - """ - return data_unmerged.hkls - - -@pytest.fixture -def dataset_hkl_unmerged(data_unmerged): - """ - Build DataSet for testing containing only Miller indices - """ - H = data_unmerged.hkls - dataset = rs.DataSet({"H": H[:, 0], "K": H[:, 1], "L": H[:, 2]}) - dataset.set_index(["H", "K", "L"], inplace=True) - return dataset - - @pytest.fixture( params=[ ["data", "data_merged.mtz"], From e739e3ff4f76831409a41104494c99800708fc13 Mon Sep 17 00:00:00 2001 From: Kevin Dalton Date: Wed, 1 Jan 2025 13:54:23 -0500 Subject: [PATCH 09/10] revert and simplify hkl setter test --- tests/test_dataset.py | 38 ++++++++++++-------------------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/tests/test_dataset.py b/tests/test_dataset.py index d44f160d..302ff919 100644 --- a/tests/test_dataset.py +++ b/tests/test_dataset.py @@ -717,30 +717,19 @@ def test_select_mtzdtype_ValueError(data_merged, dtype): @pytest.mark.parametrize("hkl_type", ["ds", "index", "numpy"]) @pytest.mark.parametrize("range_index", [True, False]) def test_hkls_property_setter( - data_merged, - data_unmerged, - hkls, - hkls_unmerged, - dataset_hkl, - dataset_hkl_unmerged, - merged, - hkl_type, - range_index, + data_merged, + data_unmerged, + merged, hkl_type, range_index ): """ Test the setter for the .hkls property of rs datasets """ if merged: input_ds = data_merged - hkls = dataset_hkl else: input_ds = data_unmerged - hkls = dataset_hkl_unmerged - if hkl_type == "ds": - hkls = hkls.reset_index() - elif hkl_type == "numpy": - hkls = hkls.hkls + hkls = input_ds.copy().reset_index()[['H', 'K', 'L']] ds = input_ds.copy() if range_index: @@ -749,25 +738,22 @@ def test_hkls_property_setter( # Confirm we're starting with equivalent miller indices expected = ds.hkls value = hkls - if hkl_type != "numpy": - value = value.hkls - assert np.array_equal(value, expected) # Shuffle the hkls - if hkl_type != "numpy": - hkls = hkls.sample(frac=1.0) - else: - np.random.shuffle(hkls) # inplace + hkls = hkls.sample(frac=1.) # confirm shuffling assert not np.array_equal(hkls, ds.hkls) # confirm setter - ds.hkls = hkls + if hkl_type == 'ds': + ds.hkls = hkls + elif hkl_type == 'index': + ds.hkls = hkls.set_index(['H', 'K', 'L']) + elif hkl_type == 'numpy': + ds.hkls = hkls.to_numpy() expected = ds.hkls - value = hkls - if hkl_type != "numpy": - value = value.hkls + value = hkls.hkls assert np.array_equal(value, expected) # Test that all data remained the same From 03c3bef66ffafb25ae5698150c783f573c9be83d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 1 Jan 2025 18:54:48 +0000 Subject: [PATCH 10/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_dataset.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/tests/test_dataset.py b/tests/test_dataset.py index 302ff919..f49a6277 100644 --- a/tests/test_dataset.py +++ b/tests/test_dataset.py @@ -717,9 +717,7 @@ def test_select_mtzdtype_ValueError(data_merged, dtype): @pytest.mark.parametrize("hkl_type", ["ds", "index", "numpy"]) @pytest.mark.parametrize("range_index", [True, False]) def test_hkls_property_setter( - data_merged, - data_unmerged, - merged, hkl_type, range_index + data_merged, data_unmerged, merged, hkl_type, range_index ): """ Test the setter for the .hkls property of rs datasets @@ -729,7 +727,7 @@ def test_hkls_property_setter( else: input_ds = data_unmerged - hkls = input_ds.copy().reset_index()[['H', 'K', 'L']] + hkls = input_ds.copy().reset_index()[["H", "K", "L"]] ds = input_ds.copy() if range_index: @@ -740,17 +738,17 @@ def test_hkls_property_setter( value = hkls # Shuffle the hkls - hkls = hkls.sample(frac=1.) + hkls = hkls.sample(frac=1.0) # confirm shuffling assert not np.array_equal(hkls, ds.hkls) # confirm setter - if hkl_type == 'ds': + if hkl_type == "ds": ds.hkls = hkls - elif hkl_type == 'index': - ds.hkls = hkls.set_index(['H', 'K', 'L']) - elif hkl_type == 'numpy': + elif hkl_type == "index": + ds.hkls = hkls.set_index(["H", "K", "L"]) + elif hkl_type == "numpy": ds.hkls = hkls.to_numpy() expected = ds.hkls value = hkls.hkls