Skip to content

Commit 70e0a4c

Browse files
committed
Merge branch 'master' of https://github.com/opensource9ja/panjs
2 parents 5444602 + ef28b3e commit 70e0a4c

9 files changed

Lines changed: 3693 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: 166 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,166 @@
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+
import { Series } from "../core/series"
5+
6+
const utils = new Utils()
7+
8+
9+
10+
/**
11+
* Plotting methods and Functions performed on Series and DataFrames
12+
*/
13+
export class Plot {
14+
constructor() { }
15+
16+
plot(ndframe, div, config) {
17+
let params = Object.keys(config)
18+
let this_config = {}
19+
20+
params.forEach(param => {
21+
this_config[param] = config[param]
22+
})
23+
24+
if (!utils.__key_in_object(config, "layout")) {
25+
this_config['layout'] = {}
26+
}
27+
28+
if (ndframe instanceof Series) {
29+
let trace = {}
30+
31+
if (this_config['type'] == 'histogram') {
32+
let x = ndframe.values
33+
trace["x"] = x
34+
} else {
35+
let y = ndframe.values
36+
trace["y"] = y
37+
}
38+
39+
params.forEach(param => {
40+
trace[param] = config[param]
41+
})
42+
newPlot(div, [trace], this_config['layout']);
43+
44+
} else {
45+
46+
//DataFrame
47+
if (utils.__key_in_object(this_config, 'x') && utils.__key_in_object(this_config, 'y')) {
48+
if (!ndframe.column_names.includes(this_config['x'])) {
49+
throw Error(`Column Error: ${this_config['x']} not found in columns`)
50+
}
51+
if (!ndframe.column_names.includes(this_config['y'])) {
52+
throw Error(`Column Error: ${this_config['y']} not found in columns`)
53+
}
54+
55+
56+
let x = ndframe[this_config['x']].values
57+
let y = ndframe[this_config['y']].values
58+
59+
let trace = {
60+
x: x,
61+
y: y,
62+
type: config["type"],
63+
mode: config["mode"],
64+
}
65+
66+
let xaxis = {}; let yaxis = {}
67+
xaxis['title'] = this_config['x']
68+
yaxis['title'] = this_config['y']
69+
70+
this_config['layout']['xaxis'] = xaxis
71+
this_config['layout']['yaxis'] = yaxis
72+
73+
74+
newPlot(div, [trace], this_config['layout']);
75+
76+
77+
} else if (this_config['type'] == 'pie') {
78+
if (!ndframe.column_names.includes(this_config['values'])) {
79+
throw Error(`Column Error: ${this_config['values']} not found in columns`)
80+
}
81+
if (!ndframe.column_names.includes(this_config['labels'])) {
82+
throw Error(`Column Error: ${this_config['labels']} not found in columns`)
83+
}
84+
let data = [{
85+
values: ndframe[this_config['values']].values,
86+
labels: ndframe[this_config['labels']].values,
87+
type: 'pie'
88+
}];
89+
90+
newPlot(div, data, this_config['layout'])
91+
92+
} else if (this_config['type'] == 'table') {
93+
let header = {}
94+
let cells = {}
95+
96+
header['values'] = ndframe.column_names
97+
cells['values'] = ndframe.col_data
98+
99+
if (this_config['header_style']) {
100+
Object.keys(this_config['header_style']).forEach(param => {
101+
header[param] = this_config['header_style'][param]
102+
})
103+
}
104+
105+
if (this_config['cell_style']) {
106+
Object.keys(this_config['cell_style']).forEach(param => {
107+
header[param] = this_config['cell_style'][param]
108+
})
109+
}
110+
var data = [{
111+
type: 'table',
112+
header: header,
113+
cells: cells
114+
}]
115+
newPlot(div, data, this_config['layout']);
116+
117+
} else if (this_config['type'] == 'box') {
118+
let cols_2_show = []
119+
let data = []
120+
121+
if (this_config['columns'] != undefined) {
122+
cols_2_show = this_config['columns']
123+
cols_2_show.forEach(col => {
124+
if (!ndframe.column_names.includes(col)) {
125+
throw Error(`Column Error: ${this_config['labels']} not found in columns`)
126+
}
127+
})
128+
} else {
129+
cols_2_show = ndframe.column_names
130+
}
131+
132+
133+
cols_2_show.forEach(col => {
134+
let col_idx = ndframe.column_names.indexOf(col)
135+
let trace = []
136+
trace['y'] = ndframe.col_data[col_idx]
137+
trace["type"] = 'box'
138+
trace["name"] = col
139+
140+
data.push(trace)
141+
})
142+
newPlot(div, data, this_config['layout']);
143+
144+
} else {
145+
//plot all
146+
let x = ndframe.index
147+
let data = []
148+
ndframe.column_names.forEach(c_name => {
149+
let trace = {}
150+
trace["x"] = x
151+
trace["y"] = ndframe[c_name].values
152+
trace['name'] = c_name
153+
params.forEach(param => {
154+
trace[param] = config[param]
155+
})
156+
data.push(trace)
157+
158+
})
159+
newPlot(div, data, this_config['layout']);
160+
}
161+
162+
163+
}
164+
}
165+
166+
}

0 commit comments

Comments
 (0)