Skip to content
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

feature: new tests added for tsne to expand test coverage #2229

Open
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

yuejiaointel
Copy link

@yuejiaointel yuejiaointel commented Dec 17, 2024

Description

Added additional tests in sklearnex/manifold/tests/test_tsne.py to expand the test coverage for t-SNE algorithm.

PR completeness and readability

  • I have reviewed my changes thoroughly before submitting this pull request.
  • I have commented my code, particularly in hard-to-understand areas.
  • Git commit message contains an appropriate signed-off-by string (see CONTRIBUTING.md for details).

Testing

  • I have run it locally and tested the changes extensively.
  • All CI jobs are green or I have provided justification why they aren't.
  • I have extended testing suite if new functionality was introduced in this PR.

Copy link

codecov bot commented Dec 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Flag Coverage Δ
github 83.18% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

@ethanglaser ethanglaser marked this pull request as draft December 17, 2024 19:02
@yuejiaointel
Copy link
Author

/intelci: run

@yuejiaointel yuejiaointel marked this pull request as ready for review December 19, 2024 00:00
@ethanglaser
Copy link
Contributor

/intelci: run

sklearnex/manifold/tests/test_tsne.py Show resolved Hide resolved
sklearnex/manifold/tests/test_tsne.py Outdated Show resolved Hide resolved
sklearnex/manifold/tests/test_tsne.py Outdated Show resolved Hide resolved
sklearnex/manifold/tests/test_tsne.py Outdated Show resolved Hide resolved
sklearnex/manifold/tests/test_tsne.py Outdated Show resolved Hide resolved
sklearnex/manifold/tests/test_tsne.py Outdated Show resolved Hide resolved
sklearnex/manifold/tests/test_tsne.py Outdated Show resolved Hide resolved
yue.jiao added 2 commits December 19, 2024 08:37
@yuejiaointel
Copy link
Author

/intelci: run

sklearnex/manifold/tests/test_tsne.py Outdated Show resolved Hide resolved
sklearnex/manifold/tests/test_tsne.py Outdated Show resolved Hide resolved
sklearnex/manifold/tests/test_tsne.py Outdated Show resolved Hide resolved
sklearnex/manifold/tests/test_tsne.py Outdated Show resolved Hide resolved
sklearnex/manifold/tests/test_tsne.py Outdated Show resolved Hide resolved
sklearnex/manifold/tests/test_tsne.py Show resolved Hide resolved
@david-cortes-intel
Copy link
Contributor

It looks like we don't have any test here nor in daal4py that would be checking that the results from TSNE make sense beyond having the right shape and non-missingness.

Since there's a very particular dataset here for the last test, it'd be helpful to add other assertions there along the lines of checking that the embeddings end up making some points closer than others as would be expected given the input data.

…or parametrization names, removed extra tests
@yuejiaointel
Copy link
Author

Hi David,
About the last comment, I think that is a good test to add! I spent some time thinking it through and have added a logic check in the final test to evaluate the overlap of close neighbors. Here’s a summary of the steps I implemented:

  1. get a distance array where [i, j] is Euclidean distance of point i and j in original space, same for tsne embedding space
  2. rank distances for each point wrt first column in original space, also for embedding space
  3. get top 5 neighbors of each point in original and embedding space see how many are same by dividing them
  4. get a mean of all fractions it should represent how the original and embedding space are similar for the most 5 closest points
  5. check if that mean is > 0.6
    Let me know your thoughts on this approach or if you believe it could be improved further.
    Thx a lot :D
    Yue

@david-cortes-intel
Copy link
Contributor

Hi David, About the last comment, I think that is a good test to add! I spent some time thinking it through and have added a logic check in the final test to evaluate the overlap of close neighbors. Here’s a summary of the steps I implemented:

  1. get a distance array where [i, j] is Euclidean distance of point i and j in original space, same for tsne embedding space
  2. rank distances for each point wrt first column in original space, also for embedding space
  3. get top 5 neighbors of each point in original and embedding space see how many are same by dividing them
  4. get a mean of all fractions it should represent how the original and embedding space are similar for the most 5 closest points
  5. check if that mean is > 0.6
    Let me know your thoughts on this approach or if you believe it could be improved further.
    Thx a lot :D
    Yue

I think given the characteristics of the data that you are passing, it could be done by selecting some hard-coded set of points by index from "Complex Dataset1" that should end up being similar, and some selected set of points that should end up being dissimilar to the earlier ones; with the test then checking that the euclidean distances in the embedding space among each point from the first set are smaller than the distances between each point in the first set and each point in the second set.

Also maybe "Complex Dataset2" is not needed.

@yuejiaointel
Copy link
Author

yuejiaointel commented Jan 8, 2025

Hi David, About the last comment, I think that is a good test to add! I spent some time thinking it through and have added a logic check in the final test to evaluate the overlap of close neighbors. Here’s a summary of the steps I implemented:

  1. get a distance array where [i, j] is Euclidean distance of point i and j in original space, same for tsne embedding space
  2. rank distances for each point wrt first column in original space, also for embedding space
  3. get top 5 neighbors of each point in original and embedding space see how many are same by dividing them
  4. get a mean of all fractions it should represent how the original and embedding space are similar for the most 5 closest points
  5. check if that mean is > 0.6
    Let me know your thoughts on this approach or if you believe it could be improved further.
    Thx a lot :D
    Yue

I think given the characteristics of the data that you are passing, it could be done by selecting some hard-coded set of points by index from "Complex Dataset1" that should end up being similar, and some selected set of points that should end up being dissimilar to the earlier ones; with the test then checking that the euclidean distances in the embedding space among each point from the first set are smaller than the distances between each point in the first set and each point in the second set.

Also maybe "Complex Dataset2" is not needed.

Hi David!
I fixed the logic based on your suggestion, and here is my understanding. First get a group A with similar points and group B with different points from group A, then check in embedding space distance b/t any 2 points in group A should be less than that point to any point in group B. I run the CI many times and one problem with this approach is that it fails sometimes for GPU devices, in these cases the embedding did not keep close points close, and it only occur on pipeline runs without problem on local machine. Not sure if I should create another ticket to investigate on that. I also removed complex test 2.
Best,
Yue

@yuejiaointel
Copy link
Author

/intelci: run

assert_allclose(tsne_1, tsne_2, rtol=1e-5)


def compute_pairwise_distances(data):
Copy link
Contributor

@david-cortes-intel david-cortes-intel Jan 9, 2025

Choose a reason for hiding this comment

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


# Ensure close points in original space remain close in embedding
group_a_indices = [0, 1, 2] # Hardcoded index of similar points
group_b_indices = [3, 4, 5] # Hardcoded index of dissimilar points from a
Copy link
Contributor

Choose a reason for hiding this comment

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

How about using the points that were already available and had also differences in signatures - e.g.

[2e9, 2e-9, -2e9, -2e-9]

@pytest.mark.parametrize(
"X,n_components,perplexity,expected_shape",
[
pytest.param(
Copy link
Contributor

Choose a reason for hiding this comment

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

Since there's only one parameterization, maybe these could be defined inside the function body.

dtype,
):
"""
TSNE test covering multiple functionality and edge cases using parameterization.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this comment is redundant given the function name.

# Check for distance b/t two points in group A < distance of this point and any point in group B
for i in group_a_indices:
for j in group_a_indices:
if i != j:
Copy link
Contributor

Choose a reason for hiding this comment

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

This condition should not be needed,

0.5,
(10, 2),
False,
id="Extremely low perplexity",
Copy link
Contributor

Choose a reason for hiding this comment

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

Considering what this is testing, perhaps it could add a check for no infinites , no NaNs, and no all-zeros in the embeddings in this test too.

X_df = _convert_to_dataframe(X, sycl_queue=queue, target_df=dataframe)
tsne = TSNE(n_components=2, perplexity=2.0).fit(X_df)
assert "daal4py" in tsne.__module__
assert hasattr(tsne, "n_components"), "TSNE missing 'n_components' attribute."
Copy link
Contributor

Choose a reason for hiding this comment

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

This one should already get tested as part of the line that comes next.

tsne = TSNE(n_components=2, perplexity=2.0).fit(X_df)
assert "daal4py" in tsne.__module__
assert hasattr(tsne, "n_components"), "TSNE missing 'n_components' attribute."
assert tsne.n_components == 2, "TSNE 'n_components' attribute is incorrect."
Copy link
Contributor

Choose a reason for hiding this comment

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

Could also check for 'perplexity' as it was passed to the constructor too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants