-
Notifications
You must be signed in to change notification settings - Fork 29k
StatCounter on NumPy arrays [PYSPARK][SPARK-2012] #1725
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
Closed
Closed
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
176a127
Use numpy arrays in StatCounter
freeman-lab 1c8a832
Unit tests for StatCounter with NumPy arrays
freeman-lab 875414c
Fixed indents
freeman-lab 8e764dd
Explicit numpy imports
freeman-lab 7f0e397
Refactored check for numpy
freeman-lab fe973b1
Avoid duplicate array import in tests
freeman-lab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,12 +38,19 @@ | |
| from pyspark.shuffle import Aggregator, InMemoryMerger, ExternalMerger | ||
|
|
||
| _have_scipy = False | ||
| _have_numpy = False | ||
| try: | ||
| import scipy.sparse | ||
| _have_scipy = True | ||
| except: | ||
| # No SciPy, but that's okay, we'll skip those tests | ||
| pass | ||
| try: | ||
| from numpy import array | ||
|
Contributor
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. just try to import numpy, this
Contributor
Author
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. @davies thanks, good catch, should be fixed now! |
||
| _have_numpy = True | ||
| except: | ||
| # No NumPy, but that's okay, we'll skip those tests | ||
| pass | ||
|
|
||
|
|
||
| SPARK_HOME = os.environ["SPARK_HOME"] | ||
|
|
@@ -914,9 +921,27 @@ def test_serialize(self): | |
| self.assertEqual(expected, observed) | ||
|
|
||
|
|
||
| @unittest.skipIf(not _have_numpy, "NumPy not installed") | ||
| class NumPyTests(PySparkTestCase): | ||
| """General PySpark tests that depend on numpy """ | ||
|
|
||
| def test_statcounter_array(self): | ||
| from numpy import array | ||
| x = self.sc.parallelize([array([1.0,1.0]), array([2.0,2.0]), array([3.0,3.0])]) | ||
| s = x.stats() | ||
| self.assertSequenceEqual([2.0,2.0], s.mean().tolist()) | ||
| self.assertSequenceEqual([1.0,1.0], s.min().tolist()) | ||
| self.assertSequenceEqual([3.0,3.0], s.max().tolist()) | ||
| self.assertSequenceEqual([1.0,1.0], s.sampleStdev().tolist()) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| if not _have_scipy: | ||
| print "NOTE: Skipping SciPy tests as it does not seem to be installed" | ||
| if not _have_numpy: | ||
| print "NOTE: Skipping NumPy tests as it does not seem to be installed" | ||
| unittest.main() | ||
| if not _have_scipy: | ||
| print "NOTE: SciPy tests were skipped as it does not seem to be installed" | ||
| if not _have_numpy: | ||
| print "NOTE: NumPy tests were skipped as it does not seem to be installed" | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to have ImportError here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about do in this way:
try:
from numpy import maximum, minimum, sqrt
except ImportError:
maximum = max
minimum = min
sqrt = math.sqrt
This will simplify later codes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! This is much better, updating the PR now...