Skip to content

Commit 89c8c0b

Browse files
add test for dataclusters (#54)
* add test for dataclusters * define eq method in dataclusters.py * change parametrization form * add one more case and change reference name to actual * delete comment * add two more tests for DataClusters class function. * change in docstring for clearer explanation for clear method, remove duplicated case for testing behavior, remove other tests. * change clear method docstring into numpydoc format. Delete dtype for numpy array. * remove block * Make edition to condition on res, refactor for setdata to make behavior of the test passed. * change condition on res * add condition on x and res are incompatible, update test. * revert change in setdata method.
1 parent 8270200 commit 89c8c0b

File tree

3 files changed

+56
-15
lines changed

3 files changed

+56
-15
lines changed

Diff for: src/diffpy/srmise/dataclusters.py

+43-14
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,36 @@ def __init__(self, x, y, res):
6868
def __iter__(self):
6969
return self
7070

71+
def __eq__(self, other):
72+
if not isinstance(other, DataClusters):
73+
return False
74+
return (
75+
np.array_equal(self.x, other.x)
76+
and np.array_equal(self.y, other.y)
77+
and np.array_equal(self.data_order, other.data_order)
78+
and np.array_equal(self.clusters, other.clusters)
79+
and self.res == other.res
80+
and self.current_idx == other.current_idx
81+
and self.lastcluster_idx == other.lastcluster_idx
82+
and self.lastpoint_idx == other.lastpoint_idx
83+
and self.status == other.status
84+
)
85+
7186
def clear(self):
72-
"""Clear all members, including user data."""
87+
"""
88+
Clear all data and reset the cluster object to a transient initial state.
89+
90+
The purpose of this method is to provide a clean state before creating new clustering operations.
91+
The object is updated in-place and no new instance is returned.
92+
93+
Returns
94+
-------
95+
None
96+
"""
7397
self.x = np.array([])
7498
self.y = np.array([])
75-
self.data_order = np.array([], dtype=np.int32)
76-
self.clusters = np.array([[]], dtype=np.int32)
99+
self.data_order = np.array([])
100+
self.clusters = np.array([[]])
77101
self.res = 0
78102
self.current_idx = 0
79103
self.lastcluster_idx = None
@@ -106,21 +130,26 @@ def setdata(self, x, y, res):
106130
# 3) r isn't sorted?
107131
if len(x) != len(y):
108132
raise ValueError("Sequences x and y must have the same length.")
109-
if res <= 0:
110-
raise ValueError("Resolution res must be greater than 0.")
133+
if res < 0:
134+
raise ValueError("Resolution res must be non-negative.")
111135
# Test for sorting?
112-
113136
self.x = x
114137
self.y = y
115138
self.res = res
116-
117-
self.data_order = self.y.argsort() # Defines order of clustering
118-
self.clusters = np.array([[self.data_order[-1], self.data_order[-1]]])
119-
self.current_idx = len(self.data_order) - 1
120-
self.lastcluster_idx = 0
121-
self.lastpoint_idx = self.data_order[-1]
122-
123-
self.status = self.READY
139+
# If x sequence size is empty, set the object into Initialized state.
140+
if x.size == 0 and res == 0:
141+
self.data_order = np.array([])
142+
self.clusters = np.array([[]])
143+
self.current_idx = 0
144+
self.lastpoint_idx = None
145+
self.status = self.INIT
146+
else:
147+
self.data_order = self.y.argsort() # Defines order of clustering
148+
self.clusters = np.array([[self.data_order[-1], self.data_order[-1]]])
149+
self.current_idx = len(self.data_order) - 1
150+
self.lastpoint_idx = self.data_order[-1]
151+
self.status = self.READY
152+
self.lastcluster_idx = None
124153
return
125154

126155
def next(self):

Diff for: src/diffpy/srmise/pdfpeakextraction.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def setvars(self, quiet=False, **kwds):
119119
quiet: [False] Log changes quietly.
120120
121121
Keywords
122-
cres: The clustering resolution, must be > 0.
122+
cres: The clustering resolution, must be >= 0.
123123
effective_dy: The uncertainties actually used during extraction
124124
dg: Alias for effective_dy
125125
pf: Sequence of PeakFunctionBase subclass instances.

Diff for: src/diffpy/srmise/tests/test_dataclusters.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import numpy as np
2+
3+
from diffpy.srmise.dataclusters import DataClusters
4+
5+
6+
def test_clear():
7+
# Initialize DataClusters with input parameters
8+
actual = DataClusters(x=np.array([1, 2, 3]), y=np.array([3, 2, 1]), res=4)
9+
expected = DataClusters(x=np.array([]), y=np.array([]), res=0)
10+
# Perform the clear operation
11+
actual.clear()
12+
assert actual == expected

0 commit comments

Comments
 (0)