Public
Edited
Apr 17, 2023
Insert cell
md`# Website: Infer DataTypes`
Insert cell
function isAlphanumeric(value) { return /^[0-9a-zA-Z]+$/.test(value); }
function isNumeric2(value) { return /^\d+$/.test(value); }
function isNumeric1(value) { return /^[\-\+]?[\d]+\.?(\d+)?$/.test(value); }
function isPositiveInteger(value) { return /^\+?[1-9][\d]*$/.test(value); }
function isFloat(value) { return /^[0-9a-zA-Z]+$/.test(value); }

//
// INFER DATATYPES -> Function executes when the IMPORT broadcast is fired
//
const broadcastChannelImported = new BroadcastChannel('imported');
broadcastChannelImported.onmessage = async (msg) => {

// Variables
start();
function is_empty(n) { return ['', undefined].includes(n) }
function is_number(n) { return !isNaN(parseFloat(n)) && isFinite( n ); }
function is_symbol(n) { return ['$', '%'].includes(n) }

var tabledata = JSON.parse(await localforage.getItem('tabledata') );
var table = JSON.parse(await localforage.getItem('table') );
let numRecords = tabledata.length

console.log('INFERENCING IS STARTING');
let infr = dl.type.inferAll(tabledata)
console.log(infr)

// INSPECT/ CAST COLUMN VALUES
Object.keys(tabledata[0]).map( column => {

// INSPECT COLUMN NAMES
var possibleGeoColNames = ['ADDRESS', "GEOMETRY", "LAT", "LNG"]
var possibleDateTimeCol = ["DATE", "TIME", "YEAR"]
var possibleMiscColName = []
let colNameOfInterestGeo = possibleGeoColNames.includes(column);
let colNameOfInterestDat = possibleDateTimeCol.includes(column);
let colNameOfInterestMis = possibleMiscColName.includes(column);

let nullCount = 0
let stringCount = 0
let intCount = 0
let symbolCount = 0


// Inspect each Column datatype
for ( var i=0; i<numRecords; i++){

// Stop once 30 records have been identified
if( stringCount + intCount < 30){
let recColVal = tabledata[i][column]
let empty = false
let symbolAtEnd = false
let symbolAtStart = false

// EMPTY
if( is_empty(recColVal) ){ nullCount++; empty=true}

// PERCENT or DOLLAR. REMOVE IT FOR PARSING.
if( !empty && is_symbol(recColVal.charAt(0)) ){ symbolAtStart=true; symbolCount++; recColVal = recColVal.slice(1); }
if( !empty && is_symbol(recColVal.charAt(-1))){ symbolAtEnd=true; symbolCount++; recColVal = recColVal.slice(0, -1); }

// NUMBER
if( !empty && is_number(recColVal) ){
intCount++
// IS FLOAT
// IS POSITIVE
// IS GREATERTHANONE
}

// STRING
else if(!empty){
stringCount++
// ARRAY
if ( recColVal.charAt(0) == '['){ console.log('DEALING WITH ARRAY') }
}
}
}

// console.log(column, ' MISSING ', nullCount, ' STRINGS ', stringCount, ' INTEGERS ', intCount );

// INFER COLUMN DATATYPE
if(stringCount==0 && intCount==0){ console.log("ERROR EVERYTHING WAS MISSING")} //ERROR
else if(stringCount>0 && intCount==0){ null } //STRING
else if (stringCount==0 && intCount>0){ // CHECK IF USES SYMBOLS, POSITIVE, GREATERTHANNONE
for ( var i=0; i<numRecords; i++){
let recColVal = tabledata[i][column]
recColVal = parseFloat(recColVal)
tabledata[i][column] = recColVal
}
}
// GEOGRAPHY ETC...

} )

console.log('INFERENCING IS COMPLETE');

let infer = dl.type.all(tabledata)
console.log(infer)

let dom_EDA = document.querySelector("#EDA")
dom_EDA.innerHTML = 'THIS IS THE INNERTEXT NOW';

// PERFORM PRELIMINARY DATA EXPLORATION

// GIVE USER COLUMNS FOR ADV Data EXPL.

let specs = {
"data": {values: tabledata},
"mark": "point",
"encoding": {
"y": {"field": 'CSA2010', "type": 'nominal'},
"x": {"field": "male", "type": "quantitative"}
}
}

// Embed the visualization in the container with id `vis`
vegaEmbed('#EDA', specs);

// Explorer Selections!
// document.querySelector("#EDA").onclick = async (e) => {
end();
};

Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more