Skip to content

Commit a21df38

Browse files
committed
fix input object format for NDframe
1 parent e7cab1e commit a21df38

9 files changed

Lines changed: 3612 additions & 360 deletions

File tree

danfojs/src/core/frame.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Series } from "./series"
44
import * as tf from '@tensorflow/tfjs'
55
import { Utils } from "./utils"
66
import { GroupBy } from "./groupby"
7-
// import { Plot } from '../plotting/plot'
7+
import { Plot } from '../plotting/plot'
88
import { indexLoc } from '../core/indexing'
99

1010
const utils = new Utils
@@ -1895,10 +1895,10 @@ export class DataFrame extends Ndframe {
18951895
* @param {string} div Name of the div to show the plot
18961896
* @param {Object} config configuration options for making Plots, supports Plotly parameters
18971897
*/
1898-
// plot(div, config = {}) {
1899-
// const plt = new Plot()
1900-
// plt.plot(this, div, config)
1901-
// }
1898+
plot(div, config = {}) {
1899+
const plt = new Plot()
1900+
plt.plot(this, div, config)
1901+
}
19021902

19031903

19041904
/**

danfojs/src/core/generic.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ export default class NDframe {
3232
} else {
3333
this.series = false
3434
if (utils.__is_object(data[0])) { //check the type of the first object in the data
35-
this.__read_object(data)
35+
this.__read_object(data, 1) //type 1 object are of JSON form [{a: 1, b: 2}, {a: 30, b: 20}]
3636
} else if (Array.isArray(data[0]) || utils.__is_number(data[0]) || utils.__is_string(data[0])) {
3737
this.__read_array(data)
38+
} else if (utils.__is_object(data)) {
39+
this.__read_object(data, 2) //type 2 object are of the form {a: [1,2,3,4], b: [30,20, 30, 20}]
3840
} else {
3941
throw "File format not supported for now"
4042
}
@@ -92,19 +94,19 @@ export default class NDframe {
9294

9395

9496
//Reads a Javascript Object of arrays of data points
95-
__read_object(data) {
96-
let data_arr = []
97-
//check format of objects
98-
let _key = Object.keys(data[0])[0] //get first column name in data
99-
if (Array.isArray(data[0][_key])) {
100-
// format of data is [{"a": [1,2,3,4]}, {"b": [2,4,5,7]}]
97+
__read_object(data, type) {
98+
if (type == 2) {
99+
let data_arr = []
100+
101+
// format of data is {"a": [1,2,3,4], "b": [2,4,5,7]}
101102
let result = utils.__get_row_values(data)
102103
data_arr = result[0]
103104
this.kwargs['columns'] = result[1]
104-
//call read array on result since it is an array of array format
105105
this.__read_array(data_arr)
106106
} else {
107107
//format of data is [{"a": 1, "b" : 2}, {"a": 2, "b" : 4}, {"a": 4, "b" : 5}]
108+
let data_arr = []
109+
108110
data.forEach((item) => {
109111
data_arr.push(Object.values(item))
110112
});
@@ -355,7 +357,7 @@ export default class NDframe {
355357
return JSON.stringify(json_arr)
356358
}
357359
}
358-
360+
359361

360362
/**
361363
* Prints the data in a Series as a grid of row and columns

danfojs/src/core/utils.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -149,28 +149,22 @@ export class Utils {
149149
// let data = [{ alpha: ["A", "B", "C", "D"] }, { count: [1,2,3,4]}, {sum: [20.3, 30.456, 40.90, 90.1]}]
150150

151151

152-
//transpose column array into row array
152+
//retrieve row array from column object
153153
__get_row_values(data) {
154-
let rows_len = Object.values(data[0])[0].length
155-
let cols_len = data.length
154+
let col_names = Object.keys(data)
155+
let col_data = Object.values(data)
156+
let rows_len = col_data[0].length
157+
let cols_len = col_names.length
156158
var rows_arr = []
157159

158160
for (var i = 0; i <= rows_len - 1; i++) {
159161
var temp_row = []
160162
for (let j = 0; j < cols_len; j++) {
161-
let _arr = Object.values(data[j])[0]
163+
let _arr = col_data[j]
162164
temp_row.push(_arr[i])
163165
}
164166
rows_arr.push(temp_row)
165167
}
166-
//get column names
167-
let col_names = []
168-
for (let i = 0; i < cols_len; i++) {
169-
let _key = Object.keys(data[i])[0]
170-
col_names.push(_key)
171-
172-
}
173-
174168
return [rows_arr, col_names]
175169

176170
}
@@ -638,24 +632,24 @@ export class Utils {
638632
return zero_data
639633
}
640634

641-
__shuffle(num,array) {
635+
__shuffle(num, array) {
642636
//https://stackoverflow.com/questions/18806210/generating-non-repeating-random-numbers-in-js/18806417
643637
var i = array.length,
644638
j = 0,
645639
temp;
646-
640+
647641
while (i--) {
648-
649-
j = Math.floor(Math.random() * (i+1));
650-
642+
643+
j = Math.floor(Math.random() * (i + 1));
644+
651645
// swap randomly chosen element with current element
652646
temp = array[i];
653647
array[i] = array[j];
654648
array[j] = temp;
655-
649+
656650
}
657-
658-
return array.slice(0,num);
651+
652+
return array.slice(0, num);
659653
}
660654
}
661655

danfojs/src/plotting/plot.js

Lines changed: 85 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,85 @@
1-
// //perform plotting
2-
// // import {newPlot} from 'plotly.js/dist/plotly'
3-
// import { Utils } from "../core/utils"
4-
// const utils = new Utils()
5-
6-
7-
8-
// /**
9-
// * Plotting methods and Functions performed on Series and DataFrames
10-
// */
11-
// export class Plot {
12-
// constructor() {}
13-
14-
// plot(ndframe, div, config) {
15-
// let params = Object.keys(config)
16-
// let this_config = {}
17-
18-
// if (!utils.__key_in_object(config, "layout")){
19-
// this_config['layout'] = {}
20-
// }
21-
22-
// params.forEach(param => {
23-
// this_config[param] = config[param]
24-
// })
25-
26-
// if (ndframe.series) {
27-
// let x = ndframe.values
28-
// let trace = {
29-
// x: x,
30-
// type: this_config["kind"],
31-
// mode: this_config["mode"]
32-
// }
33-
// newPlot(div, [trace], this_config['layout']);
34-
35-
// } else {
36-
// //DataFrame
37-
// if (utils.__key_in_object(this_config, 'x') && utils.__key_in_object(this_config, 'y')) {
38-
// let x = ndframe[this_config['x']].values
39-
// let y = ndframe[this_config['y']].values
40-
41-
// let trace = {
42-
// x: x,
43-
// y: y,
44-
// type: config["kind"],
45-
// mode: config["mode"],
46-
// }
47-
48-
// let xaxis = {}; let yaxis = {}
49-
// xaxis['title'] = this_config['x']
50-
// yaxis['title'] = this_config['y']
51-
52-
// this_config['layout']['xaxis'] = xaxis
53-
// this_config['layout']['yaxis'] = yaxis
54-
55-
56-
// newPlot(div, [trace], this_config['layout']);
57-
58-
// } else {
59-
// //plot all
60-
// let y = ndframe.index
61-
// let data = []
62-
// ndframe.column_names.forEach(c_name => {
63-
// let trace = {
64-
// x: ndframe[c_name].values,
65-
// y: y,
66-
// type: this_config["kind"],
67-
// mode: this_config["mode"],
68-
// name: c_name
69-
// };
70-
// data.push(trace)
71-
72-
// })
73-
// newPlot(div, data, this_config['layout']);
74-
// }
75-
76-
77-
// }
78-
// }
79-
80-
// }
1+
//perform plotting
2+
import { newPlot } from 'plotly.js/dist/plotly'
3+
import { Utils } from "../core/utils"
4+
const utils = new Utils()
5+
6+
7+
8+
/**
9+
* Plotting methods and Functions performed on Series and DataFrames
10+
*/
11+
export class Plot {
12+
constructor() { }
13+
14+
plot(ndframe, div, config) {
15+
let params = Object.keys(config)
16+
let this_config = {}
17+
18+
if (!utils.__key_in_object(config, "layout")) {
19+
this_config['layout'] = {}
20+
}
21+
22+
params.forEach(param => {
23+
this_config[param] = config[param]
24+
})
25+
26+
if (ndframe.series) {
27+
let x = ndframe.values
28+
let trace = {}
29+
trace["x"] = x
30+
31+
params.forEach(param => {
32+
if (!param == 'layout') {
33+
trace[param] = config[param]
34+
}
35+
})
36+
37+
38+
newPlot(div, [trace], this_config['layout']);
39+
40+
} else {
41+
//DataFrame
42+
if (utils.__key_in_object(this_config, 'x') && utils.__key_in_object(this_config, 'y')) {
43+
let x = ndframe[this_config['x']].values
44+
let y = ndframe[this_config['y']].values
45+
46+
let trace = {
47+
x: x,
48+
y: y,
49+
type: config["type"],
50+
mode: config["mode"],
51+
}
52+
53+
let xaxis = {}; let yaxis = {}
54+
xaxis['title'] = this_config['x']
55+
yaxis['title'] = this_config['y']
56+
57+
this_config['layout']['xaxis'] = xaxis
58+
this_config['layout']['yaxis'] = yaxis
59+
60+
61+
newPlot(div, [trace], this_config['layout']);
62+
63+
} else {
64+
//plot all
65+
let y = ndframe.index
66+
let data = []
67+
ndframe.column_names.forEach(c_name => {
68+
let trace = {
69+
x: ndframe[c_name].values,
70+
y: y,
71+
type: this_config["type"],
72+
mode: this_config["mode"],
73+
name: c_name
74+
};
75+
data.push(trace)
76+
77+
})
78+
newPlot(div, data, this_config['layout']);
79+
}
80+
81+
82+
}
83+
}
84+
85+
}

danfojs/tests/core/generic.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,15 @@ describe("Generic (NDFrame)", function () {
113113
describe("NDframe Created from JavaScript Object of Arrays", function () {
114114

115115
it("prints the shape of a 2D ", function () {
116-
let data = [{ alpha: ["A", "B", "C", "D"] }, { count: [1, 2, 3, 4] }]
116+
let data = { alpha: ["A", "B", "C", "D"], count: [1, 2, 3, 4] }
117117
let ndframe = new NDframe(data)
118118
assert.deepEqual(ndframe.shape, [4, 2])
119119
})
120-
// it("prints the shape of a 2D ", function () {
121-
// let data = [{ alpha: ["A", "B", "C", "D"] }, { count: [1,2,3,4]}, {sum: [20.3, 30.456, 40.90, 90.1]}]
122-
// let ndframe = new NDframe(data)
123-
// assert.deepEqual(ndframe.shape, [4, 3])
124-
// })
120+
it("prints the shape of a 2D ", function () {
121+
let data = { alpha: ["A", "B", "C", "D"], count: [1,2,3,4], sum: [20.3, 30.456, 40.90, 90.1]}
122+
let ndframe = new NDframe(data)
123+
assert.deepEqual(ndframe.shape, [4, 3])
124+
})
125125
})
126126

127127
describe("__set_index", function () {

danfojs/tests/core/utils.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,18 @@ describe("Utils Functions", function () {
175175

176176
})
177177

178+
describe("__get_row_values", function () {
179+
it("retreive rows and labels from column object", function () {
180+
let data = { "Alpha": ["A", "B", "C", "D"], count: [1,2,3,4], sum: [20.3, 30.456, 40.90, 90.1]}
181+
let res = [["A", 1, 20.3], ["B", 2, 30.456], ["C", 3, 40.90], ["D", 4, 90.1]]
182+
assert.deepEqual(utils.__get_row_values(data)[0], res)
183+
assert.deepEqual(utils.__get_row_values(data)[1], ["Alpha", "count", "sum"])
184+
185+
})
186+
187+
188+
})
189+
178190

179191

180192

0 commit comments

Comments
 (0)