-
Notifications
You must be signed in to change notification settings - Fork 8
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
Changes from all commits
5bf4556
e3fe16f
fdab59c
82888bb
f63abd5
b959a09
de2e964
b25e9a4
17582f0
3638253
ca8a169
2105474
cdaa3f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what will happen if There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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): | ||
|
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 |
Uh oh!
There was an error while loading. Please reload this page.