├── test └── index.js ├── package.json ├── README.md ├── LICENSE ├── .gitignore └── index.js /test/index.js: -------------------------------------------------------------------------------- 1 | let test = require('tape'); 2 | let ndarray = require('ndarray'); 3 | let corr2 = require('../index'); 4 | 5 | let ndarray1 = ndarray([1, 2, 3, 4, 5, 6]); 6 | let ndarray2 = ndarray([6, 5, 4, 3, 2, 1]); 7 | let ndarray3 = ndarray([2, 31, 21]); 8 | let ndarray4 = ndarray([8, 56, 97]); 9 | 10 | test('Basic arrays tests', function (t) { 11 | t.throws(() => corr2([1, 2], [1, 3]), 'corr2 of plain arrays should throw'); 12 | t.isEqual(corr2(ndarray1, ndarray1), 1, 'corr2 of the same array should equal 1'); 13 | t.isEqual(corr2(ndarray1, ndarray2), -1, 'corr2 of reversed arrays should equal -1'); 14 | 15 | t.isEqual( 16 | corr2(ndarray3, ndarray4), 17 | 0.6789073766772663, 18 | 'corr2 of [2, 31, 21] & [8, 56, 97] should equal 0.6789073766772663' 19 | ); 20 | 21 | t.throws( 22 | () => corr2(ndarray1, ndarray3), 23 | /size/, 24 | 'corr2 of arrays with different sizes should throw' 25 | ); 26 | t.end() 27 | }); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ndarray-corr2", 3 | "version": "0.1.1", 4 | "description": "Calculate 2-D correlation coefficient between two ndarrays.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node test/index.js | tap-spec" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/Abdelaziz18003/ndarray-corr2.git" 12 | }, 13 | "keywords": [ 14 | "ndarray", 15 | "correlation", 16 | "corr2", 17 | "matrix", 18 | "image processing" 19 | ], 20 | "author": "Abdelaziz Mokhnache", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/Abdelaziz18003/ndarray-corr2/issues" 24 | }, 25 | "homepage": "https://github.com/Abdelaziz18003/ndarray-corr2#readme", 26 | "dependencies": { 27 | "ndarray": "^1.0.18", 28 | "ndarray-ops": "^1.2.2" 29 | }, 30 | "devDependencies": { 31 | "tap-spec": "^5.0.0", 32 | "tape": "^4.11.0" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ndarray-corr2 2 | Calculate 2-D correlation coefficient between two ndarrays. 3 | 4 | ## Installation 5 | 6 | ```bash 7 | npm install ndarray-corr2 8 | ``` 9 | 10 | ## Usage 11 | 12 | ```js 13 | const ndarray = require("ndarray"); 14 | const getPixels = require("get-pixels"); 15 | const corr2 = require("ndarray-corr2"); 16 | 17 | // example 1 18 | let ndarray1 = ndarray([1, 2, 3, 4, 5, 6]); 19 | let ndarray2 = ndarray([1, 2, 3, 4, 5, 6]); 20 | console.log(corr2(ndarray1, ndarray2)); 21 | // print "1" 22 | 23 | // example 2 24 | let ndarray1 = ndarray([1, 2, 3, 4, 5, 6], [2, 3]); 25 | let ndarray2 = ndarray([6, 5, 4, 3, 2, 1], [2, 3]); 26 | console.log(corr2(ndarray1, ndarray2)); 27 | // print "-1" 28 | 29 | // example 3 30 | let ndarray1 = ndarray([23, 16, 85, 58], [2, 2]); 31 | let ndarray2 = ndarray([25, 56, 32, 41], [2, 2]); 32 | console.log(corr2(ndarray1, ndarray2)); 33 | // print "-0.33985761679300736" 34 | 35 | // example 4 36 | getPixels("lena.png", function(err, pixels) { 37 | if(!err && pixels) { 38 | console.log(corr2(pixels, pixels)); 39 | // print "1" because the two images are similar 40 | } 41 | }); 42 | ``` 43 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Abdelaziz Mokhnache 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const ndarray = require('ndarray'); 4 | const {sum, mul, subs} = require('ndarray-ops'); 5 | 6 | function corr2 (ndarray1, ndarray2) { 7 | validateInputs(ndarray1, ndarray2); 8 | const mean1 = sum(ndarray1) / ndarray1.size; 9 | const mean2 = sum(ndarray2) / ndarray2.size; 10 | 11 | let a = ndarray(new Array(ndarray1.size), ndarray1.shape); 12 | let b = ndarray(new Array(ndarray2.size), ndarray2.shape); 13 | let c = ndarray(new Array(ndarray2.size), ndarray2.shape); 14 | 15 | subs(a, ndarray1, mean1); 16 | subs(b, ndarray2, mean2); 17 | 18 | return sum(mul(c, a, b)) / Math.sqrt(sum(mul(c, a, a)) * sum(mul(c, b, b))); 19 | } 20 | 21 | function isNdarray (array) { 22 | return ( 23 | array.hasOwnProperty('data') && 24 | array.hasOwnProperty('shape') && 25 | array.hasOwnProperty('stride') && 26 | array.hasOwnProperty('offset') 27 | ) 28 | } 29 | 30 | function validateInputs (input1, input2) { 31 | if ( 32 | !isNdarray(input1) || 33 | !isNdarray(input2) || 34 | input1.dimension > 2 || 35 | input2.dimension > 2 36 | ) { 37 | throw new Error('Expected inputs to be two-dimensional ndarrays.'); 38 | } 39 | if (input1.size !== input2.size) { 40 | throw new Error('Inputs must be ndarrays of the same size') 41 | } 42 | } 43 | 44 | module.exports = corr2; --------------------------------------------------------------------------------