Skip to content

Commit

Permalink
Merge pull request #113 from opensource9ja/danfojs-browser
Browse files Browse the repository at this point in the history
Fix minor bug and release latest version on Jsdelrv (Danfojs browser)
  • Loading branch information
risenW authored Feb 14, 2021
2 parents 364c82a + 861d864 commit 3f6d547
Show file tree
Hide file tree
Showing 17 changed files with 788 additions and 23,998 deletions.
17 changes: 15 additions & 2 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@

{
"presets": [
"@babel/preset-env"
"presets": [
[
"@babel/env",
{
"targets": {
"edge": "17",
"firefox": "60",
"chrome": "67",
"safari": "11.1"
},
"useBuiltIns": "usage",
"corejs": "3.6.5"
}
]
]
}
34 changes: 17 additions & 17 deletions danfojs/src/core/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* limitations under the License.
*/

import * as tf from "@tensorflow/tfjs";
import { tensor } from "@tensorflow/tfjs";
import Ndframe from "./generic";
import { Series } from "./series";
import { Utils } from "./utils";
Expand Down Expand Up @@ -183,7 +183,7 @@ export class DataFrame extends Ndframe {
index: new_index
});
} else {
this.row_data_tensor = tf.tensor(new_data);
this.row_data_tensor = tensor(new_data);
this.data = new_data;
this.__set_index(new_index);
}
Expand Down Expand Up @@ -916,7 +916,7 @@ export class DataFrame extends Ndframe {
}

values.map((arr) => {
let temp_sum = tf.tensor(arr).sum().arraySync();
let temp_sum = tensor(arr).sum().arraySync();
val_sums.push(Number(temp_sum.toFixed(5)));
});

Expand All @@ -940,7 +940,7 @@ export class DataFrame extends Ndframe {
abs() {
let data = this.values;

let tensor_data = tf.tensor(data);
let tensor_data = tensor(data);
let abs_data = tensor_data.abs().arraySync();
let df = new DataFrame(utils.__round(abs_data, 2, false), {
columns: this.column_names,
Expand Down Expand Up @@ -1406,7 +1406,7 @@ export class DataFrame extends Ndframe {
}

for (let i = 0; i < df_data.length; i++) {
let value = tf.tensor(df_data[i]);
let value = tensor(df_data[i]);
let callable_data;
try {
callable_data = callable(value).arraySync();
Expand Down Expand Up @@ -1659,18 +1659,18 @@ export class DataFrame extends Ndframe {
`Shape Error: Operands could not be broadcast together with shapes ${this.shape} and ${val.values.length}.`
);
}
other = tf.tensor(val.values);
other = tensor(val.values);
} else {
if (val.values.length != this.shape[1]) {
throw Error(
`Shape Error: Operands could not be broadcast together with shapes ${this.shape} and ${val.values.length}.`
);
}
other = tf.tensor(val.values);
other = tensor(val.values);
}
} else if (Array.isArray(val)) {
//Array of Array
other = tf.tensor(val);
other = tensor(val);
} else {
//DataFrame
other = val.row_data_tensor;
Expand All @@ -1679,22 +1679,22 @@ export class DataFrame extends Ndframe {

switch (logical_type) {
case "lt":
int_vals = tf.tensor(this.values).less(other).arraySync();
int_vals = tensor(this.values).less(other).arraySync();
break;
case "gt":
int_vals = tf.tensor(this.values).greater(other).arraySync();
int_vals = tensor(this.values).greater(other).arraySync();
break;
case "le":
int_vals = tf.tensor(this.values).lessEqual(other).arraySync();
int_vals = tensor(this.values).lessEqual(other).arraySync();
break;
case "ge":
int_vals = tf.tensor(this.values).greaterEqual(other).arraySync();
int_vals = tensor(this.values).greaterEqual(other).arraySync();
break;
case "ne":
int_vals = tf.tensor(this.values).notEqual(other).arraySync();
int_vals = tensor(this.values).notEqual(other).arraySync();
break;
case "eq":
int_vals = tf.tensor(this.values).equal(other).arraySync();
int_vals = tensor(this.values).equal(other).arraySync();
break;
}
let bool_vals = utils.__map_int_to_bool(int_vals, 2);
Expand Down Expand Up @@ -1754,7 +1754,7 @@ export class DataFrame extends Ndframe {

this_tensor = tensors[0].row_data_tensor; //tensorflow uses 1 for rows axis and 0 for column axis
if (tensors[1].series) {
other_tensor = tf.tensor(tensors[1].values, [
other_tensor = tensor(tensors[1].values, [
1,
tensors[1].values.length
]);
Expand All @@ -1771,7 +1771,7 @@ export class DataFrame extends Ndframe {

this_tensor = tensors[0].row_data_tensor;
if (tensors[1].series) {
other_tensor = tf.tensor(tensors[1].values, [
other_tensor = tensor(tensors[1].values, [
tensors[1].values.length,
1
]);
Expand Down Expand Up @@ -2153,7 +2153,7 @@ export class DataFrame extends Ndframe {
let val = sorted_val[row_i];
let index = null;

if (duplicate_obj.hasOwnProperty(val)) {
if (val in duplicate_obj) {
index = duplicate_obj[val]["index"][0];
duplicate_obj[val]["index"].splice(0, 1);
} else {
Expand Down
12 changes: 6 additions & 6 deletions danfojs/src/core/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
*/

import * as tf from "@tensorflow/tfjs";
import { tensor, Tensor } from "@tensorflow/tfjs";
import { table } from "table";
import { Utils } from "./utils";
import { Configs } from "../config/config";
Expand All @@ -38,7 +38,7 @@ export default class NDframe {
constructor(data, kwargs = {}) {
this.kwargs = kwargs;

if (data instanceof tf.Tensor) {
if (data instanceof Tensor) {
data = data.arraySync();
}

Expand Down Expand Up @@ -71,15 +71,15 @@ export default class NDframe {
*/
_read_array(data) {
this.data = utils.__replace_undefined_with_NaN(data, this.series);
this.row_data_tensor = tf.tensor(this.data);
this.row_data_tensor = tensor(this.data);

if (this.series) {
this.col_data = [ this.values ];
} else {
this.col_data = utils.__get_col_values(this.data);
}

this.col_data_tensor = tf.tensor(this.col_data); //data saved as 2D column tensors
this.col_data_tensor = tensor(this.col_data); //data saved as 2D column tensors

if ("index" in this.kwargs) {
this.__set_index(this.kwargs["index"]);
Expand Down Expand Up @@ -133,7 +133,7 @@ export default class NDframe {
});

this.data = utils.__replace_undefined_with_NaN(data_arr, this.series); //Defualt array data in row format
this.row_data_tensor = tf.tensor(this.data); //data saved as row tensors
this.row_data_tensor = tensor(this.data); //data saved as row tensors
this.kwargs["columns"] = Object.keys(Object.values(data)[0]); //get names of the column from the first entry

if (this.series) {
Expand All @@ -142,7 +142,7 @@ export default class NDframe {
this.col_data = utils.__get_col_values(this.data);
}

this.col_data_tensor = tf.tensor(this.col_data); //data saved as 2D column tensors
this.col_data_tensor = tensor(this.col_data); //data saved as 2D column tensors

if ("index" in this.kwargs) {
this.__set_index(this.kwargs["index"]);
Expand Down
8 changes: 4 additions & 4 deletions danfojs/src/core/series.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/


import * as tf from "@tensorflow/tfjs";
import { tensor, round } from "@tensorflow/tfjs";
import { variance, std } from 'mathjs';
import { Utils } from "./utils";
import { Str } from "./strings";
Expand Down Expand Up @@ -55,7 +55,7 @@ export class Series extends NDframe {
* @returns {1D Tensor}
*/
get tensor() {
return tf.tensor(this.values).asType(this.dtypes[0]);
return tensor(this.values).asType(this.dtypes[0]);
}


Expand Down Expand Up @@ -250,7 +250,7 @@ export class Series extends NDframe {
mean() {
utils._throw_str_dtype_error(this, 'mean');
let values = utils._remove_nans(this.values);
let mean = tf.tensor(values).mean().arraySync();
let mean = tensor(values).mean().arraySync();
return mean;
}

Expand Down Expand Up @@ -382,7 +382,7 @@ export class Series extends NDframe {
round(dp) {
if (utils.__is_undefined(dp)) {
//use tensorflow round function to roound to the nearest whole number
let result = tf.round(this.row_data_tensor).arraySync();
let result = round(this.row_data_tensor).arraySync();
return new Series(result, { columns: this.column_names, index: this.index });

} else {
Expand Down
7 changes: 3 additions & 4 deletions danfojs/src/core/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as tf from "@tensorflow/tfjs";
import '@tensorflow/tfjs-backend-cpu';
import { tensor, linspace } from "@tensorflow/tfjs";
import { Configs } from "../config/config";

const config = new Configs();
Expand Down Expand Up @@ -113,7 +112,7 @@ export class Utils {

//generate integers between two set of numbers
__range(start, end) {
let value = tf.linspace(start, end, end - start + 1).arraySync();
let value = linspace(start, end, end - start + 1).arraySync();
return value;
}

Expand Down Expand Up @@ -689,7 +688,7 @@ export class Utils {
let rslt_obj = {};

arr.forEach((val, index) => {
if (temp_obj.hasOwnProperty(val)) {
if (val in temp_obj) {
temp_obj[val]["count"] += 1;
temp_obj[val]["index"].push(index);
} else {
Expand Down
5 changes: 3 additions & 2 deletions danfojs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import NDframe from "./core/generic";
export { Series } from "./core/series";
export { DataFrame } from "./core/frame";
export { to_datetime } from "./core/timeseries";
export { read_csv, read_json, read_excel, read } from "./io/reader";
export { read_csv, read_json, read_excel } from "./io/reader";
export { merge } from "./core/merge";
export { concat } from "./core/concat";
export { LabelEncoder, OneHotEncoder } from "./preprocessing/encodings";
Expand All @@ -14,5 +14,6 @@ export { Configs } from "./config/config";
export { NDframe };
export { Str } from "./core/strings";
export { Utils } from "./core/utils";
export * as tf from "@tensorflow/tfjs";

export const _version = "0.2.1";
export const _version = "0.2.2";
64 changes: 1 addition & 63 deletions danfojs/src/io/reader.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import * as tf from "@tensorflow/tfjs";
import fetch from "node-fetch";
import XLSX from "xlsx";
import { open, Dataset, isDataset } from "frictionless.js";
import toArray from "stream-to-array";
import { Utils } from "../core/utils";
import { DataFrame } from "../core/frame";

Expand All @@ -27,6 +25,7 @@ export const read_csv = async (source, chunk) => {
)
) {
//probabily a relative path, append file:// to it
// eslint-disable-next-line no-undef
source = `file://${process.cwd()}/${source}`;
}

Expand Down Expand Up @@ -154,64 +153,3 @@ export const read_excel = async (kwargs) => {
throw new Error(err);
}
};

/**
* Opens a file using frictionless.js specification.
* @param {string} pathOrDescriptor A path to the file/resources. It can be a local file,
* a URL to a tabular data (CSV, EXCEL) or Datahub.io Data Resource.
* Data comes with extra properties and specification conforming to the Frictionless Data standards.
* @param {object} configs { data_num (Defaults => 0): The specific dataset to load, when reading data from a datapackage.json,
* header (Defaults => true): Whether the dataset contains header or not.
* }
* @returns {DataFrame} Danfo DataFrame/Series
*/
export const read = async (
path_or_descriptor,
configs = { data_num: 0, header: true }
) => {
let data_num = configs["data_num"];
let header = configs["header"];
let rows, file;

if (isDataset(path_or_descriptor)) {
console.log(
"datapackage.json found. Loading Dataset package from Datahub.io"
);
const dataset = await Dataset.load(path_or_descriptor);
file = dataset.resources[data_num];
//TODO: toArray does not work in browser env, so this feature breaks when build for the web.
// To fix this, we need a function to convert stream into text
rows = await toArray(await file.rows());
} else {
try {
file = open(path_or_descriptor);
rows = await toArray(await file.rows());
} catch (error) {
console.log(error);
}
}

if ([ "csv", "xls", "xlsx" ].includes(await file.descriptor.format)) {
if (header) {
let df = new DataFrame(rows.slice(1), { columns: rows[0] });
return df;
} else {
let df = new DataFrame(rows);
return df;
}
} else {
let df = new DataFrame(rows);
return df;
}
};

// /**
// * Reads a Database into DataFrame
// *
// * @param {source} URL or local file path to retreive JSON file.
// * @returns {Promise} DataFrame structure of parsed CSV data
// */
// export const read_sql = async (source) => {

// return "TODO"
// }
Loading

0 comments on commit 3f6d547

Please sign in to comment.