-
Notifications
You must be signed in to change notification settings - Fork 38
Use random-shuffle list shuffling instead of random >= 1.3 #169
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b127411
Use random-shuffle list shuffling instead of random >= 1.3
jisantuc 7f57b1f
Now with tests
jisantuc 184ae00
module rename, woops
jisantuc 7f53743
Add new test preserving column names
jisantuc f8fdae4
Fix sort order test
jisantuc b1def3d
Rename shuffleOnlyShuffles
jisantuc 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
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
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 |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| {-# LANGUAGE OverloadedStrings #-} | ||
| {-# LANGUAGE TypeApplications #-} | ||
|
|
||
| module Operations.Shuffle where | ||
|
|
||
| import qualified DataFrame as D | ||
|
|
||
| import DataFrame.Operations.Permutation (shuffle) | ||
| import System.Random (mkStdGen) | ||
| import Test.HUnit (Test (..), assertEqual) | ||
|
|
||
| testDataFrame :: D.DataFrame | ||
| testDataFrame = | ||
| D.fromNamedColumns | ||
| [ ("numbers", D.fromList @Int [1 .. 26]) | ||
| ] | ||
|
|
||
| -- Test that shuffling does anything at all | ||
| shuffleShuffles :: Test | ||
| shuffleShuffles = | ||
| let gen = mkStdGen 1234 | ||
| shuffled = shuffle gen testDataFrame | ||
| initialNumbers = D.extractNumericColumn "numbers" testDataFrame | ||
| shuffledNumbers = D.extractNumericColumn "numbers" shuffled | ||
| in TestCase | ||
| ( assertEqual | ||
| "Shuffled column unequal to initial column" | ||
| False | ||
| (initialNumbers == shuffledNumbers) | ||
| ) | ||
|
|
||
| shufflePreservesColumnNames :: Test | ||
| shufflePreservesColumnNames = | ||
| let gen = mkStdGen 837 | ||
| shuffled = shuffle gen testDataFrame | ||
| in TestCase | ||
| ( assertEqual | ||
| "Column names are unchanged" | ||
| (D.columnNames shuffled) | ||
| (D.columnNames testDataFrame) | ||
| ) | ||
|
|
||
| -- Test that un-shuffling restores the original dataframe | ||
| -- which is known to be sorted in this case | ||
| shufflePreservesData :: Test | ||
| shufflePreservesData = | ||
| let gen = mkStdGen 1234 | ||
| shuffled = shuffle gen testDataFrame | ||
| sortedShuffled = D.sortBy [D.Asc (D.col @Int "numbers")] shuffled | ||
| in TestCase | ||
| (assertEqual "sort recovers initial numbers" testDataFrame sortedShuffled) | ||
|
|
||
| -- Test that shuffling isn't doing anything sneaky with summoning | ||
| -- random numbers somehow | ||
| shuffleSameSeedIsSameShuffle :: Test | ||
| shuffleSameSeedIsSameShuffle = | ||
| let gen = mkStdGen 1234 | ||
| shuffled1 = shuffle gen testDataFrame | ||
| shuffled2 = shuffle gen testDataFrame | ||
| in TestCase | ||
| (assertEqual "shuffle with same seed gives same result" shuffled1 shuffled2) | ||
|
|
||
| -- Test that different seeds give different results | ||
| shuffleDifferentSeedIsDifferent :: Test | ||
| shuffleDifferentSeedIsDifferent = | ||
| let gen1 = mkStdGen 1234 | ||
| gen2 = mkStdGen 4321 | ||
| shuffled1 = shuffle gen1 testDataFrame | ||
| shuffled2 = shuffle gen2 testDataFrame | ||
| in TestCase | ||
| ( assertEqual | ||
| "shuffle with different seeds gives different results" | ||
| False | ||
| (shuffled1 == shuffled2) | ||
| ) | ||
|
|
||
| tests :: [Test] | ||
| tests = | ||
| [ TestLabel "shuffleShuffles" shuffleShuffles | ||
| , TestLabel "shufflePreservesData" shufflePreservesData | ||
| , TestLabel "shufflePreservesColumnNames" shufflePreservesColumnNames | ||
| , TestLabel "shuffleSameSeedIsSameShuffle" shuffleSameSeedIsSameShuffle | ||
| , TestLabel "shuffleDifferentSeedIsDifferent" shuffleDifferentSeedIsDifferent | ||
| ] |
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.
Pretty unclear to me how to test this. I thought about a shuffle/un-shuffle identity test, but unshuffle didn't get me very far in search results 😅
Any thoughts?
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.
There are a couple of things I would test then.
test that the shuffling doesn't do anything else than shuffle. So basically sort the shuffled and unshuffled and see if it's equal to the same thing. This ensures that shuffling isn't doing anything else than permuting the indices.
check that shuffling with equivalent seeds result in the same shuffle.
That's about all I can think of
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.
Yeah. No need to round trip. Checking that shuffling preserves length (even when there are duplicates) is probably important. Plus that different seeds are different shuffle orders.
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.
Also on second thought the intermediate list allocation is wasteful. I'll add it as a GSOC task to implement fisher yates 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.
that task shouldn't be GSOC, it should be anyone! Also
mwc-randomdoes that I think.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.
Why not? It seems simple and self contained enough since it's reading the algorithm and implementing it through.
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.
there could be some task pipeline for people who are not interested only in GSOC, but more interested generally in contributing!