Skip to content

Commit

Permalink
feat(*): add error hadling in defineConfig() api
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjayaksaxena committed Oct 6, 2018
1 parent 04eaac4 commit 5e1cecd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
17 changes: 13 additions & 4 deletions src/wink-perceptron.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ var perceptron = function () {

v = features[ f ];
adjustWt( f, v, truth );
if ( guess ) adjustWt( f, -v, guess );
if ( guess !== 'unknown' ) adjustWt( f, -v, guess );
}
adjustBs( +1, truth );
if ( guess ) adjustBs( -1, guess );
if ( guess !== 'unknown' ) adjustBs( -1, guess );
}; // adjustWeights()

var learnFromData = function ( data ) {
Expand All @@ -196,7 +196,7 @@ var perceptron = function () {
var j, k;

// Starting from **1** ensures that we iterate **maxIterations** times.
for ( j = 1; j < maxIterations; j += 1 ) {
for ( j = 0; j < maxIterations; j += 1 ) {
for ( k = 0; k < data.length; k += 1 ) {
guess = predict( data[ k ][ 0 ] );
if ( guess !== data[ k ][ 1 ].label ) adjustWeights( data[ k ], guess );
Expand All @@ -216,7 +216,7 @@ var perceptron = function () {
var j, k, l;

// Starting from **1** ensures that we iterate **maxIterations** times.
for ( j = 1; j < maxIterations; j += 1 ) {
for ( j = 0; j < maxIterations; j += 1 ) {
for ( k = 0; k < data.length; k += 1 ) {
features = featureExtractor( data[ k ] );
for ( l = 0; l < features.length; l += 1 ) {
Expand Down Expand Up @@ -282,13 +282,22 @@ var perceptron = function () {
* // -> { shuffleData: true, maxIterations: 9, featureExtractor: null }
*/
var defineConfig = function ( config ) {
if ( !helpers.object.isObject( config ) ) {
throw Error( 'wink-perceptron: config must be an object, instead found: ' + ( typeof config ) );
}
// Convert 'truthy -> true' or `falsy -> false`. This also implies that
// default is **`false`**.
shuffleData = !!config.shuffleData;
// Default # of maximum iteration is **6**.
maxIterations = config.maxIterations || maxIterations;
if ( maxIterations < 1 ) {
throw Error( 'wink-perceptron: maxIterations should be >1' );
}
// Ordered Set Of Features Extractor function; default is none!
featureExtractor = config.featureExtractor || featureExtractor;
if ( ( featureExtractor !== null ) && ( typeof featureExtractor !== 'function' ) ) {
throw Error( 'wink-perceptron: featureExtractor must be a function, instead found: ' + ( typeof featureExtractor ) );
}

return ( { shuffleData: shuffleData, maxIterations: maxIterations, featureExtractor: featureExtractor } );
}; // defineConfig()
Expand Down
25 changes: 23 additions & 2 deletions test/wink-perceptron-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,20 @@ var extractFeatures = function ( e ) {
// IRIS Data is sourced from UC Irvine Machine Learning Repository;
// source: https://archive.ics.uci.edu/ml/datasets/iris
// Prepare data:
// Training data preparation.
lines = fs.readFileSync( './test/data/iris-data-train.csv', 'utf8' ).split( '\n' );
lines.pop();
td = [];
lines.forEach( function ( e ) {
td.push( e.split( ',' ) );
} );

// The `rawTrainingData` will now be a copy of `td` array.
rawTrainingData = td.slice( 0 );
td.forEach( function ( e ) {
trainingData.push( extractFeatures( e )[ 0 ] );
} );

// Test data preparation.
lines = fs.readFileSync( './test/data/iris-data-test.csv', 'utf8' ).split( '\n' );
lines.pop();
td = [];
Expand All @@ -70,12 +72,31 @@ td.forEach( function ( e ) {
} );

// Tests
describe( 'instantiate perceptron ', function () {
describe( 'instantiate perceptron', function () {
it( 'must return 3 methods', function () {
expect( Object.keys( perceptron() ).length ).to.equal( 3 );
} );
} );

describe( 'defineConfig', function () {
it( 'should throw error if config is not passed', function () {
expect( perceptron().defineConfig.bind( undefined, undefined ) ).to.throw( 'wink-perceptron: config must be an object, instead found:' );
} );

it( 'should throw error maxIterations <1', function () {
expect( perceptron().defineConfig.bind( undefined, { maxIterations: -1 } ) ).to.throw( 'wink-perceptron: maxIterations should be >1' );
} );

it( 'should throw error featureExtractor must be a function', function () {
expect( perceptron().defineConfig.bind( undefined, { featureExtractor: 1 } ) ).to.throw( 'wink-perceptron: featureExtractor must be a function, instead found:' );
} );

it( 'should default configuration with empty config input', function () {
expect( perceptron().defineConfig( {} ) )
.to.deep.equal( { shuffleData: false, maxIterations: 9, featureExtractor: null } );
} );
} );

describe( 'train & predict using extracted features from iris data', function () {
var p = perceptron();

Expand Down

0 comments on commit 5e1cecd

Please sign in to comment.