datamodel
DataModel
DataModel is an in-browser representation of tabular data. It supports relational algebra operators as well as generic data processing operators. DataModel extends Relation class which defines all the relational algebra operators. DataModel gives definition of generic data processing operators which are not relational algebra compliant but needed for ease of use.
1. constructor
Syntax: constructor(formattedData, config)
parameters:
- formattedData:
required: true
type: Object
descriptions: An object with default data object
parameters:
- data:
required: true
type: Array of Array
descriptions: Input data in any of the mentioned formats.
- schema:
required: true
type: Object
description : The field definitions in Object format. Order of the variables in data
and order of the variables in schema has to be same.
- config:
required: false
type: Object
parameters:
- enableDomainCompletion:
type: boolean
description: Adds support for choosing whether to fill in missing bins when getting bins from a DataModel's Binned Field
Creates a new DataModel instance by providing a data (array of arrays) and schema.
Data of any other format can be loaded using the loadData api of DataModel. Also it is advised to use the loadData
api to better handle invalid values, especially for date-time fields.
DataModel also expects a Schema for identifying the variables present in data.
Here is a working example:
const Datamodel = muze.DataModel;
const data = [
[
"chevrolet chevelle malibu",
"ford fiesta",
"bmw 320i",
"chevrolet chevelle malibu",
"ford fiesta",
"bmw 320i",
],
[18, 36.1, 21.5, 18, 36.1, 21.5],
["8", "4", "4", "8", "4", "4"],
[130, 66, 110, 130, 66, 110],
["1970", "1978", "1977", "1970", "1978", "1977"],
];
const schema = [
{
name: "Name",
type: "dimension",
},
{
name: "Miles_per_Gallon",
type: "measure",
unit: "gallon",
},
{
name: "Cylinders",
type: "dimension",
},
{
name: "Horsepower",
type: "measure",
},
{
name: "Year",
type: "dimension",
format: "%Y",
},
];
let dm = new Datamodel({ data, schema });
printDM(dm);
Note: printDM
is a utility function to render DataModel's data for demostration purpose.
The output looks like this:
Name | Miles_per_Gallon | Cylinders | Horsepower | Year |
---|---|---|---|---|
chevrolet chevelle malibu | 18 | 8 | 130 | 1970 |
ford fiesta | 36.1 | 4 | 66 | 1978 |
bmw 320i | 21.5 | 4 | 110 | 1977 |
chevrolet chevelle malibu | 18 | 8 | 130 | 1970 |
ford fiesta | 36.1 | 4 | 66 | 1978 |
2. getData
Syntax: getData()
Retrieve the data attached to an instance in JSON format.
const Datamodel = muze.DataModel;
const data = [
{
Maker: "chevrolet",
Name: "chevrolet chevelle malibu",
Miles_per_Gallon: 18,
Cylinders: 8,
Displacement: 307,
Horsepower: 130,
Weight_in_lbs: 3504,
Acceleration: 12,
Year: "1970-01-01",
Origin: "USA",
},
{
Maker: "buick",
Name: "buick skylark 320",
Miles_per_Gallon: 15,
Cylinders: 8,
Displacement: -350,
Horsepower: 165,
Weight_in_lbs: 3693,
Acceleration: 11.5,
Year: "1970-01-01",
Origin: "USA",
},
// ... and so on...
];
const schema = [
{
name: "Name",
type: "dimension",
},
{
name: "Maker",
type: "dimension",
},
{
name: "Miles_per_Gallon",
type: "measure",
defAggFn: "avg",
},
{
name: "Displacement",
type: "measure",
defAggFn: "sum",
},
{
name: "Horsepower",
type: "measure",
defAggFn: "sum",
},
{
name: "Weight_in_lbs",
type: "measure",
defAggFn: "min",
},
{
name: "Acceleration",
type: "measure",
defAggFn: "sum",
},
{
name: "Origin",
type: "dimension",
},
{
name: "Cylinders",
type: "dimension",
},
{
name: "Year",
type: "dimension",
subtype: "temporal",
format: "%Y-%m-%d",
},
];
const formattedData = await Datamodel.loadData(data, schema);
let dm = new Datamodel(formattedData);
let names = schema.map((d) => d.name);
printTable(dm.getData().data, names, { rowLimit: 3 });
Note: printTable
is a utility function to render DataModel's data for demostration purpose.
Name | Maker | Miles_per_Gallon | Displacement | Horsepower | Weight_in_lbs | Acceleration | Origin | Cylinders | Year |
---|---|---|---|---|---|---|---|---|---|
chevrolet chevelle malibu | chevrolet | 18 | 307 | 130 | 3504 | 12 | USA | 8 | -19800000 |
buick skylark 320 | buick | 15 | 350 | 165 | 3693 | 11.5 | USA | 8 | -19800000 |
plymouth satellite | plymouth | 18 | 318 | 150 | 3436 | 11 | USA | 8 | -19800000 |
Returns:
Array: Returns a multidimensional array of the data with schema. The return format looks like
{ data, schema }
3. getSchema
Syntax: getSchema()
Retrieves the schema details for every field in an array format.
Returns:
Schema: Array of fields schema.
[
{ name: 'Name', type: 'dimension' },
{ name: 'Miles_per_Gallon', type: 'measure', format: (val) => ${val} miles / gallon },
{ name: 'Cylinder', type: 'dimension' },
{ name: 'Displacement', type: 'measure', defAggFn: 'max' },
{ name: 'HorsePower', type: 'measure', defAggFn: 'max' },
{ name: 'Weight_in_lbs', type: 'measure', defAggFn: 'avg', },
{ name: 'Acceleration', type: 'measure', defAggFn: 'avg' },
{ name: 'Year', type: 'dimension', subtype: 'datetime', format: '%Y' },
{ name: 'Origin' }
]
4. getField
Syntax: getField(fieldName)
parameters:
- fieldName:
required: true
type: string
descriptions: get the field instance from the field name
Returns:
Field Instance, this instance can be use to access field functions.
5. getDataMeta
Syntax: getDataMeta()
Returns:
Object of rowCount and columnCount