Wednesday, October 17, 2018

Tiny Dataset Toolkit in JavaScript




I developed a tiny toolkit written in JavaScript containing helper functions to manipulate data...


I usually put all my JavaScript scripts in a sub-directory of the plugins directory (for example, My_Scripts) and use my favorite text editor as described in this post [configure my dev environment].

1. Install

Now, in your plugins directory, create a new directory termed javascript , your folder tree is now like this...

ImageJ/
  +--luts/
  +--macros/ 
  +--plugins/
      +--My_Scripts/
      +--javascript/


Then download the following files tip-gist.js and nashorn-polyfill.js located in github.
  • The first file contains various tools to manipulate the data.
  • The second file contains missing JS system functions (polyfills in the jargon JS).

ImageJ/
  +--luts/
  +--macros/
  +--plugins/
      +--My_Scripts/
      +--javascript/
         +--tip-gist.js
         +--nashorn_polyfill.js 
            

Now, we are ready to play with the data.

2. How to use it

In the folder My_Scripts, create a new file hello_ML.js and restart ImageJ. Now, in the menu, Plugins > My_Scripts, we'll see the file hello_ML in the sub-menu.

With your favorite text editor, copy the following lines...


// Import helper functions
const IJ_PLUGINS = IJ.getDir('plugins');
load(`${IJ_PLUGINS}/javascript/nashorn_polyfill.js`);
load(`${IJ_PLUGINS}/javascript/tip-gist.js`);

These three lines import the JS functions located in the two files nashorn-polyfill.js and tip-gist.js.

Note: The following JS script only works if you have a Java version greater or equal to 1.9 (it doesn't work with Java 1.8). To check your java version, in the main IJ window, go to Help > About ImageJ.

3. Some examples

The detailed API is located in this post [Link] but here, a simple example showing the various methods available for the object DataSet.

+++ Script hello_ML.js+++
/*
* TIPJS: Tools for Image(J) Processing JavaScript
* Copyright (C) 2018 Jean-Christophe Taveau.
*
* This file is part of TIPJS, module TDS
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,Image
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TIPJS. If not, see <http://www.gnu.org/licenses/>.
*
*
* Authors:
* Jean-Christophe Taveau
*/
'use strict';
const IJ_PLUGINS = IJ.getDir('plugins'); //('user.dir');
load(`${IJ_PLUGINS}/javascript/nashorn_polyfill.js`);
load(`${IJ_PLUGINS}/javascript/tip-gist.js`);
let csv = `
ID,filename,width,height,type
1,blobs.gif,256,254,8-bit,
2,boats.gif,720,576,8-bit,
3,clown.jpg,320,200,RGB,
4,gel.gif,276,476,8-bit,
5,LineGraph.jpg,516,356,8-bit
`;
// Create a dataset from CSV
let dataset = TDS.fromCSV(csv);
console.log(dataset.toString() );
// length: Number of observations
console.log('length ' + dataset.length);
console.log('3rd observation ' + JSON.stringify(dataset.vector(3)) );
// slice(..) for splitting the dataset
console.log('3rd observation ' + dataset.slice(0,3).toString() );
// Extract columns
let cols = dataset.columns(['filename','width','height']);
console.log(cols.toString());
// Filter by features
console.log('clean...' );
let clean = dataset.filter(TDS.byFeatures(['ID','width','height']) );
console.log(clean.toString() );
view raw hello_ML.js hosted with ❤ by GitHub
+++ End of Script hello_ML.js+++


<<  Previous: Features Next:Cleaning Data  >>


4. Other crazybiocomputing posts

Further readings are available in ...

No comments:

Post a Comment