Skip to content

add test for dataclusters #54

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

Merged
merged 13 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 43 additions & 14 deletions src/diffpy/srmise/dataclusters.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,36 @@ def __init__(self, x, y, res):
def __iter__(self):
return self

def __eq__(self, other):
if not isinstance(other, DataClusters):
return False
return (
np.array_equal(self.x, other.x)
and np.array_equal(self.y, other.y)
and np.array_equal(self.data_order, other.data_order)
and np.array_equal(self.clusters, other.clusters)
and self.res == other.res
and self.current_idx == other.current_idx
and self.lastcluster_idx == other.lastcluster_idx
and self.lastpoint_idx == other.lastpoint_idx
and self.status == other.status
)

def clear(self):
"""Clear all members, including user data."""
"""
Clear all data and reset the cluster object to a transient initial state.

The purpose of this method is to provide a clean state before creating new clustering operations.
The object is updated in-place and no new instance is returned.

Returns
-------
None
"""
self.x = np.array([])
self.y = np.array([])
self.data_order = np.array([], dtype=np.int32)
self.clusters = np.array([[]], dtype=np.int32)
self.data_order = np.array([])
self.clusters = np.array([[]])
self.res = 0
self.current_idx = 0
self.lastcluster_idx = None
Expand Down Expand Up @@ -106,21 +130,26 @@ def setdata(self, x, y, res):
# 3) r isn't sorted?
if len(x) != len(y):
raise ValueError("Sequences x and y must have the same length.")
if res <= 0:
raise ValueError("Resolution res must be greater than 0.")
if res < 0:
raise ValueError("Resolution res must be non-negative.")
# Test for sorting?

self.x = x
self.y = y
self.res = res

self.data_order = self.y.argsort() # Defines order of clustering
self.clusters = np.array([[self.data_order[-1], self.data_order[-1]]])
self.current_idx = len(self.data_order) - 1
self.lastcluster_idx = 0
self.lastpoint_idx = self.data_order[-1]

self.status = self.READY
# If x sequence size is empty, set the object into Initialized state.
if x.size == 0 and res == 0:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what will happen if x.size == 0 but res != 0?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not what I meant. I think that we want the clear method test to pass (which it was) but if we were to write tests for this method, there would be other situations than x.size == 0 and res == 0 and these were not being handled in your code fix.

Let's just revert this commit and I will merge, since it is passing tests, but write tests for this setdata method on a separate PR.

self.data_order = np.array([])
self.clusters = np.array([[]])
self.current_idx = 0
self.lastpoint_idx = None
self.status = self.INIT
else:
self.data_order = self.y.argsort() # Defines order of clustering
self.clusters = np.array([[self.data_order[-1], self.data_order[-1]]])
self.current_idx = len(self.data_order) - 1
self.lastpoint_idx = self.data_order[-1]
self.status = self.READY
self.lastcluster_idx = None
return

def next(self):
Expand Down
2 changes: 1 addition & 1 deletion src/diffpy/srmise/pdfpeakextraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def setvars(self, quiet=False, **kwds):
quiet: [False] Log changes quietly.

Keywords
cres: The clustering resolution, must be > 0.
cres: The clustering resolution, must be >= 0.
effective_dy: The uncertainties actually used during extraction
dg: Alias for effective_dy
pf: Sequence of PeakFunctionBase subclass instances.
Expand Down
12 changes: 12 additions & 0 deletions src/diffpy/srmise/tests/test_dataclusters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import numpy as np

from diffpy.srmise.dataclusters import DataClusters


def test_clear():
# Initialize DataClusters with input parameters
actual = DataClusters(x=np.array([1, 2, 3]), y=np.array([3, 2, 1]), res=4)
expected = DataClusters(x=np.array([]), y=np.array([]), res=0)
# Perform the clear operation
actual.clear()
assert actual == expected
Loading