Skip to content

Commit f30e56f

Browse files
committed
Add tfjs functions
1 parent 28e2ce9 commit f30e56f

2 files changed

Lines changed: 113 additions & 26 deletions

File tree

js/histojs/designFunctionsv4.js

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -72,35 +72,47 @@
7272
* @since 1.0.0
7373
* @version 1.0.0
7474
* @category Array
75-
* @param {Array} array The array to process.
75+
* @param {Array} arr - The array to process.
7676
* @param {*} element The value to inspect and remove.
7777
* @returns {Array}
7878
* @example
7979
*
8080
* removeArrayElem(['a', 'b', 'c', 'd'], 'c')
8181
* => ['a', 'b', 'd']
82+
*---------------------------------------------*
83+
* await removeArrayElemTFJS( [1, 2, 3, 4], 3)
84+
* => [1, 2, 4]
8285
*
83-
* await removeArrayElemTFJS([1, 2, 3, 4], 3)
86+
* await removeArrayElemTFJS(['a', 'b', 'c', 'd'], 'c')
87+
* => ['a', 'b', 'd']
88+
*---------------------------------------------*
89+
* await removeArrayElemTFJSV2( [1, 2, 3, 4], 3)
8490
* => [1, 2, 4]
85-
* NOTE: TFJS version for numbers only
91+
* NOTE: removeArrayElemTFJSV2 for numbers only
92+
*
8693
*/
8794

88-
removeArrayElem = (array, element) => {
89-
const index = array.indexOf(element);
95+
removeArrayElem = (arr, element) => {
96+
const index = arr.indexOf(element);
9097
if(index >= 0 ) {
91-
array.splice(index, 1);
98+
arr.splice(index, 1);
9299
}
93100
}
94101

95-
removeArrayElemTFJS = async (array, element) => {
96-
const tensor = tf.tensor(array);
102+
removeArrayElemTFJS = async(arr, element) => {
103+
return await tf.data.array(arr).filter(elm => elm !== element).toArray();
104+
}
105+
106+
107+
removeArrayElemTFJSV2 = async (arr, element) => {
108+
const tensor = tf.tensor(arr);
97109
const mask = tf.notEqual(tensor, element);
98110
const result = await tf.booleanMaskAsync(tensor, mask);
99111
return result.arraySync();
100112
}
101113

102-
removeArrayElem_v2 = (array, element) => {
103-
return array.filter(elm => elm !== element);
114+
removeArrayElem_v2 = (arr, element) => {
115+
return arr.filter(elm => elm !== element);
104116
}
105117

106118

@@ -258,6 +270,13 @@
258270
* {id:"spx-7", Type:"Immune"} ])
259271
*
260272
* => Object { "spx-1": {id: "spx-1", Type: "Tumor"}, "spx-7": {id: "spx-7", Type: "Immune"} }
273+
*
274+
*
275+
* await array2ObjWithHashKeyTFJS( "id", [ {id:"spx-1", Type:"Tumor"},
276+
* {id:"spx-7", Type:"Immune"} ])
277+
*
278+
* => Object { "spx-1": {id: "spx-1", Type: "Tumor"}, "spx-7": {id: "spx-7", Type: "Immune"} }
279+
*
261280
*/
262281

263282
array2ObjWithHashKey = (hashKey, arrayOfObjs) => {
@@ -269,13 +288,32 @@
269288
convertedObject[ object[hashKey] ] = object;
270289
});
271290

272-
return convertedObject;
291+
return convertedObject;
292+
273293
} else {
274294
triggerHint("Not a valid array of objects .. ")
275295
return null;
276296
}
277297
}
278298

299+
array2ObjWithHashKeyTFJS = async(hashKey, arrayOfObjs) => {
300+
if(Object.keys(arrayOfObjs).length) {
301+
// create hash folarger arrayr the
302+
let convertedObject = {};
303+
304+
await tf.data.array(arrayOfObjs).forEachAsync(object => {
305+
convertedObject[ object[hashKey] ] = object;
306+
});
307+
308+
return convertedObject;
309+
310+
} else {
311+
triggerHint("Not a valid array of objects .. ")
312+
return null;
313+
}
314+
}
315+
316+
279317

280318
/**
281319
* Find unique values of array and return as new array. This is helpful for heatmapColor scale function
@@ -336,27 +374,27 @@
336374
*
337375
* => [{ Type: "1", val: 1}, { Type: "2", val: 2}, { Type: "3", val: 3}, { Type: "4", val: 4}]
338376
*********************************************************************************************
339-
* fastArraysConcatTFJS( [1, 1, 2, 3], [5, 2, 5])
377+
* fastArraysConcatTFJSV2( [1, 1, 2, 3], [5, 2, 5])
340378
*
341379
* => [1, 1, 2, 3, 5, 2, 5]
342380
*
343-
* NOTE: fastArraysConcatTFJS can not concate Array of objects.
381+
* NOTE: fastArraysConcatTFJSV2 can not concate Array of objects.
344382
*
345-
* fastArraysConcatTFJS( array1, array2)
383+
* fastArraysConcatTFJSV2( array1, array2)
346384
*
347385
* => [ NaN, NaN, NaN, NaN ]
348386
**********************************************************************************************
349-
* NOTE: fastArraysConcatTFJSData returns promise
387+
* NOTE: fastArraysConcatTFJS returns promise
350388
*
351-
* await fastArraysConcatTFJSData( [1, 1, 2, 3], [5, 2, 5])
389+
* await fastArraysConcatTFJS( [1, 1, 2, 3], [5, 2, 5])
352390
* OR
353-
* fastArraysConcatTFJSData([1, 1, 2, 3], [5, 2, 5]).then(function(res) { console.log(res) })
391+
* fastArraysConcatTFJS([1, 1, 2, 3], [5, 2, 5]).then(function(res) { console.log(res) })
354392
*
355393
* => [1, 1, 2, 3, 5, 2, 5]
356394
*
357-
* await fastArraysConcatTFJSData( array1, array2)
395+
* await fastArraysConcatTFJS( array1, array2)
358396
* OR
359-
* fastArraysConcatTFJSData(array1,array2).then(function(res) { console.log(res) })
397+
* fastArraysConcatTFJS(array1,array2).then(function(res) { console.log(res) })
360398
*
361399
* => [{ Type: "1", val: 1}, { Type: "2", val: 2}, { Type: "3", val: 3}, { Type: "4", val: 4}]
362400
*/
@@ -366,18 +404,16 @@
366404
}
367405

368406
fastArraysConcatTFJS = (array1, array2) => {
369-
const tensor1 = tf.tensor1d(array1);
370-
const tensor2 = tf.tensor1d(array2);
371-
return tf.concat([tensor1, tensor2]).arraySync();
372-
}
373-
374-
375-
fastArraysConcatTFJSData = (array1, array2) => {
376407
const dataArr1 = tf.data.array(array1);
377408
const dataArr2 = tf.data.array(array2);
378409
return dataArr1.concatenate(dataArr2).toArray();
379410
}
380411

412+
fastArraysConcatTFJSV2 = (array1, array2) => {
413+
const tensor1 = tf.tensor1d(array1);
414+
const tensor2 = tf.tensor1d(array2);
415+
return tf.concat([tensor1, tensor2]).arraySync();
416+
}
381417

382418
/**
383419
* Find if two arrays are identical.

test/histojs_test/designFunctions_test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ describe("Main Design Phase Functions", function () {
2525
});
2626
});
2727

28+
describe('#findObjectByKeyValueTFJS()', function () {
29+
it('return array element', function () {
30+
return findObjectByKeyValueTFJS( [{id: "1", value: "val1"}, {id: "2", value: "val2"}] , 'id', "2").then(result => {
31+
expect( result ).to.eql({id: "2", value: "val2"});
32+
});
33+
});
34+
});
35+
2836
describe('#removeArrayElem()', function () {
2937
it('return array after remove element', function () {
3038
let arr2Test = ['a', 'b', 'c', 'd'];
@@ -33,6 +41,14 @@ describe("Main Design Phase Functions", function () {
3341
});
3442
});
3543

44+
describe('#removeArrayElemTFJS()', function () {
45+
it('return array after remove element', function () {
46+
return removeArrayElemTFJS(['a', 'b', 'c', 'd'], 'c').then(result => {
47+
expect( result ).to.eql(['a', 'b', 'd']);
48+
});
49+
});
50+
});
51+
3652
describe('#insertArrayElem()', function () {
3753
it('return array after insert an element', function () {
3854
let arr2Test = ['b', 'c', 'd'];
@@ -61,6 +77,18 @@ describe("Main Design Phase Functions", function () {
6177
});
6278
});
6379

80+
describe('#mergeArrayOfObjByKeyTFJS()', function () {
81+
it('return merged two arrays of objects by common key ', function () {
82+
return mergeArrayOfObjByKeyTFJS( "id", [ {id:"spx-1", Type:"Tumor"},
83+
{id:"spx-7", Type:"Immune"} ],
84+
[{id : "spx-1", area: 250, solidity: 0.95},
85+
{id : "spx-3", area: 150, solidity: 0.85},
86+
{id : "spx-7", area: 100, solidity: 0.80} ]).then(result => {
87+
expect( result ).to.be.an('array');
88+
});
89+
});
90+
});
91+
6492
describe('#array2ObjWithHashKey()', function () {
6593
it('return a conversion of array of objects to object with hashing key', function () {
6694
expect(array2ObjWithHashKey( "id", [ {id:"spx-1", Type:"Tumor"},
@@ -69,18 +97,41 @@ describe("Main Design Phase Functions", function () {
6997
});
7098
});
7199

100+
describe('#array2ObjWithHashKeyTFJS()', function () {
101+
it('return a conversion of array of objects to object with hashing key', function () {
102+
return array2ObjWithHashKeyTFJS( "id", [ {id:"spx-1", Type:"Tumor"},
103+
{id:"spx-7", Type:"Immune"} ]).then(result => {
104+
expect( result ).to.eql({ "spx-1": {id: "spx-1", Type: "Tumor"}, "spx-7": {id: "spx-7", Type: "Immune"} });
105+
});
106+
});
107+
});
108+
72109
describe('#arrayUniqueValues()', function () {
73110
it('return unique values of array ', function () {
74111
expect( arrayUniqueValues( [1, 1, 2, 3, 2, 5]) ).to.eql([1, 2, 3, 5]);
75112
});
76113
});
77114

115+
describe('#arrayUniqueValuesTFJS()', function () {
116+
it('return unique values of array ', function () {
117+
expect( arrayUniqueValuesTFJS( [1, 1, 2, 3, 2, 5] ) ).to.eql([1, 2, 3, 5]);
118+
});
119+
});
120+
78121
describe('#fastArraysConcat()', function () {
79122
it('return fast arrays concatenation ', function () {
80123
expect( fastArraysConcat( [1, 1, 2, 3], [5, 2, 5]) ).to.eql([1, 1, 2, 3, 5, 2, 5]);
81124
});
82125
});
83126

127+
describe('#fastArraysConcatTFJS()', function () {
128+
it('return fast arrays concatenation ', function () {
129+
return fastArraysConcatTFJS( [1, 1, 2, 3], [5, 2, 5]).then(result => {
130+
expect( result ).to.eql([1, 1, 2, 3, 5, 2, 5]);
131+
});
132+
});
133+
});
134+
84135
describe('#areArraysEquals()', function () {
85136
it('return if two arrays are identical ', function () {
86137
expect( areArraysEquals( [1, 1, 2, 3], [1, 1, 2, 5]) ).to.be.false

0 commit comments

Comments
 (0)