-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(lib): Fix splitTest not correctly translating to test data length (…
…#17) * refactor(lib): Liberate splitTestData and improve its tests * fix(lib): Fix splitTest not correctly translating to test data length
- Loading branch information
Showing
3 changed files
with
105 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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,23 @@ | ||
import { CsvTable } from './loadCsv.models'; | ||
|
||
const splitTestData = ( | ||
features: CsvTable, | ||
labels: CsvTable, | ||
splitTest: true | number | ||
) => { | ||
const dataLength = features.length; | ||
const testLength = | ||
typeof splitTest === 'number' | ||
? Math.max(0, Math.min(splitTest, dataLength)) | ||
: Math.floor(features.length / 2); | ||
const testStartIndex = dataLength - testLength; | ||
|
||
return { | ||
features: features.slice(0, testStartIndex), | ||
labels: labels.slice(0, testStartIndex), | ||
testFeatures: features.slice(testStartIndex), | ||
testLabels: labels.slice(testStartIndex), | ||
}; | ||
}; | ||
|
||
export default splitTestData; |
This file contains 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,81 @@ | ||
import splitTestData from '../src/splitTestData'; | ||
|
||
const tables = { | ||
features: [ | ||
[1, 2], | ||
[3, 4], | ||
[5, 6], | ||
[7, 8], | ||
], | ||
labels: [[9], [10], [11], [12]], | ||
}; | ||
|
||
test('Default splitting, splits in half', () => { | ||
const { features, labels, testFeatures, testLabels } = splitTestData( | ||
tables.features, | ||
tables.labels, | ||
true | ||
); | ||
expect(features).toMatchObject([ | ||
[1, 2], | ||
[3, 4], | ||
]); | ||
expect(labels).toMatchObject([[9], [10]]); | ||
expect(testFeatures).toMatchObject([ | ||
[5, 6], | ||
[7, 8], | ||
]); | ||
expect(testLabels).toMatchObject([[11], [12]]); | ||
}); | ||
|
||
test('Splitting a fixed amount works', () => { | ||
const { features, labels, testFeatures, testLabels } = splitTestData( | ||
tables.features, | ||
tables.labels, | ||
1 | ||
); | ||
expect(features).toMatchObject([ | ||
[1, 2], | ||
[3, 4], | ||
[5, 6], | ||
]); | ||
expect(labels).toMatchObject([[9], [10], [11]]); | ||
expect(testFeatures).toMatchObject([[7, 8]]); | ||
expect(testLabels).toMatchObject([[12]]); | ||
}); | ||
|
||
test('Splitting more than row length splits all rows into test data', () => { | ||
const { features, labels, testFeatures, testLabels } = splitTestData( | ||
tables.features, | ||
tables.labels, | ||
tables.features.length * 2 | ||
); | ||
expect(features).toMatchObject([]); | ||
expect(labels).toMatchObject([]); | ||
expect(testFeatures).toMatchObject([ | ||
[1, 2], | ||
[3, 4], | ||
[5, 6], | ||
[7, 8], | ||
]); | ||
expect(testLabels).toMatchObject([[9], [10], [11], [12]]); | ||
}); | ||
|
||
test('Splitting less than or equal to 0 places all rows into normal data', () => { | ||
[0, -1].forEach((splitLength) => { | ||
const { features, labels, testFeatures, testLabels } = splitTestData( | ||
tables.features, | ||
tables.labels, | ||
splitLength | ||
); | ||
expect(features).toMatchObject([ | ||
[1, 2], | ||
[3, 4], | ||
[5, 6], | ||
[7, 8], | ||
]); | ||
expect(labels).toMatchObject([[9], [10], [11], [12]]); | ||
expect(testFeatures).toMatchObject([]); | ||
expect(testLabels).toMatchObject([]); | ||
}); | ||
}); |