Skip to content

Commit 3f6d547

Browse files
authored
Merge pull request #113 from opensource9ja/danfojs-browser
Fix minor bug and release latest version on Jsdelrv (Danfojs browser)
2 parents 364c82a + 861d864 commit 3f6d547

17 files changed

Lines changed: 788 additions & 23998 deletions

File tree

babel.config.json

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
12
{
2-
"presets": [
3-
"@babel/preset-env"
3+
"presets": [
4+
[
5+
"@babel/env",
6+
{
7+
"targets": {
8+
"edge": "17",
9+
"firefox": "60",
10+
"chrome": "67",
11+
"safari": "11.1"
12+
},
13+
"useBuiltIns": "usage",
14+
"corejs": "3.6.5"
15+
}
16+
]
417
]
518
}

danfojs/src/core/frame.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* limitations under the License.
1313
*/
1414

15-
import * as tf from "@tensorflow/tfjs";
15+
import { tensor } from "@tensorflow/tfjs";
1616
import Ndframe from "./generic";
1717
import { Series } from "./series";
1818
import { Utils } from "./utils";
@@ -183,7 +183,7 @@ export class DataFrame extends Ndframe {
183183
index: new_index
184184
});
185185
} else {
186-
this.row_data_tensor = tf.tensor(new_data);
186+
this.row_data_tensor = tensor(new_data);
187187
this.data = new_data;
188188
this.__set_index(new_index);
189189
}
@@ -916,7 +916,7 @@ export class DataFrame extends Ndframe {
916916
}
917917

918918
values.map((arr) => {
919-
let temp_sum = tf.tensor(arr).sum().arraySync();
919+
let temp_sum = tensor(arr).sum().arraySync();
920920
val_sums.push(Number(temp_sum.toFixed(5)));
921921
});
922922

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

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

14081408
for (let i = 0; i < df_data.length; i++) {
1409-
let value = tf.tensor(df_data[i]);
1409+
let value = tensor(df_data[i]);
14101410
let callable_data;
14111411
try {
14121412
callable_data = callable(value).arraySync();
@@ -1659,18 +1659,18 @@ export class DataFrame extends Ndframe {
16591659
`Shape Error: Operands could not be broadcast together with shapes ${this.shape} and ${val.values.length}.`
16601660
);
16611661
}
1662-
other = tf.tensor(val.values);
1662+
other = tensor(val.values);
16631663
} else {
16641664
if (val.values.length != this.shape[1]) {
16651665
throw Error(
16661666
`Shape Error: Operands could not be broadcast together with shapes ${this.shape} and ${val.values.length}.`
16671667
);
16681668
}
1669-
other = tf.tensor(val.values);
1669+
other = tensor(val.values);
16701670
}
16711671
} else if (Array.isArray(val)) {
16721672
//Array of Array
1673-
other = tf.tensor(val);
1673+
other = tensor(val);
16741674
} else {
16751675
//DataFrame
16761676
other = val.row_data_tensor;
@@ -1679,22 +1679,22 @@ export class DataFrame extends Ndframe {
16791679

16801680
switch (logical_type) {
16811681
case "lt":
1682-
int_vals = tf.tensor(this.values).less(other).arraySync();
1682+
int_vals = tensor(this.values).less(other).arraySync();
16831683
break;
16841684
case "gt":
1685-
int_vals = tf.tensor(this.values).greater(other).arraySync();
1685+
int_vals = tensor(this.values).greater(other).arraySync();
16861686
break;
16871687
case "le":
1688-
int_vals = tf.tensor(this.values).lessEqual(other).arraySync();
1688+
int_vals = tensor(this.values).lessEqual(other).arraySync();
16891689
break;
16901690
case "ge":
1691-
int_vals = tf.tensor(this.values).greaterEqual(other).arraySync();
1691+
int_vals = tensor(this.values).greaterEqual(other).arraySync();
16921692
break;
16931693
case "ne":
1694-
int_vals = tf.tensor(this.values).notEqual(other).arraySync();
1694+
int_vals = tensor(this.values).notEqual(other).arraySync();
16951695
break;
16961696
case "eq":
1697-
int_vals = tf.tensor(this.values).equal(other).arraySync();
1697+
int_vals = tensor(this.values).equal(other).arraySync();
16981698
break;
16991699
}
17001700
let bool_vals = utils.__map_int_to_bool(int_vals, 2);
@@ -1754,7 +1754,7 @@ export class DataFrame extends Ndframe {
17541754

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

17721772
this_tensor = tensors[0].row_data_tensor;
17731773
if (tensors[1].series) {
1774-
other_tensor = tf.tensor(tensors[1].values, [
1774+
other_tensor = tensor(tensors[1].values, [
17751775
tensors[1].values.length,
17761776
1
17771777
]);
@@ -2153,7 +2153,7 @@ export class DataFrame extends Ndframe {
21532153
let val = sorted_val[row_i];
21542154
let index = null;
21552155

2156-
if (duplicate_obj.hasOwnProperty(val)) {
2156+
if (val in duplicate_obj) {
21572157
index = duplicate_obj[val]["index"][0];
21582158
duplicate_obj[val]["index"].splice(0, 1);
21592159
} else {

danfojs/src/core/generic.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
*/
1515

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

41-
if (data instanceof tf.Tensor) {
41+
if (data instanceof Tensor) {
4242
data = data.arraySync();
4343
}
4444

@@ -71,15 +71,15 @@ export default class NDframe {
7171
*/
7272
_read_array(data) {
7373
this.data = utils.__replace_undefined_with_NaN(data, this.series);
74-
this.row_data_tensor = tf.tensor(this.data);
74+
this.row_data_tensor = tensor(this.data);
7575

7676
if (this.series) {
7777
this.col_data = [ this.values ];
7878
} else {
7979
this.col_data = utils.__get_col_values(this.data);
8080
}
8181

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

8484
if ("index" in this.kwargs) {
8585
this.__set_index(this.kwargs["index"]);
@@ -133,7 +133,7 @@ export default class NDframe {
133133
});
134134

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

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

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

147147
if ("index" in this.kwargs) {
148148
this.__set_index(this.kwargs["index"]);

danfojs/src/core/series.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515

1616

17-
import * as tf from "@tensorflow/tfjs";
17+
import { tensor, round } from "@tensorflow/tfjs";
1818
import { variance, std } from 'mathjs';
1919
import { Utils } from "./utils";
2020
import { Str } from "./strings";
@@ -55,7 +55,7 @@ export class Series extends NDframe {
5555
* @returns {1D Tensor}
5656
*/
5757
get tensor() {
58-
return tf.tensor(this.values).asType(this.dtypes[0]);
58+
return tensor(this.values).asType(this.dtypes[0]);
5959
}
6060

6161

@@ -250,7 +250,7 @@ export class Series extends NDframe {
250250
mean() {
251251
utils._throw_str_dtype_error(this, 'mean');
252252
let values = utils._remove_nans(this.values);
253-
let mean = tf.tensor(values).mean().arraySync();
253+
let mean = tensor(values).mean().arraySync();
254254
return mean;
255255
}
256256

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

388388
} else {

danfojs/src/core/utils.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import * as tf from "@tensorflow/tfjs";
2-
import '@tensorflow/tfjs-backend-cpu';
1+
import { tensor, linspace } from "@tensorflow/tfjs";
32
import { Configs } from "../config/config";
43

54
const config = new Configs();
@@ -113,7 +112,7 @@ export class Utils {
113112

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

@@ -689,7 +688,7 @@ export class Utils {
689688
let rslt_obj = {};
690689

691690
arr.forEach((val, index) => {
692-
if (temp_obj.hasOwnProperty(val)) {
691+
if (val in temp_obj) {
693692
temp_obj[val]["count"] += 1;
694693
temp_obj[val]["index"].push(index);
695694
} else {

danfojs/src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import NDframe from "./core/generic";
33
export { Series } from "./core/series";
44
export { DataFrame } from "./core/frame";
55
export { to_datetime } from "./core/timeseries";
6-
export { read_csv, read_json, read_excel, read } from "./io/reader";
6+
export { read_csv, read_json, read_excel } from "./io/reader";
77
export { merge } from "./core/merge";
88
export { concat } from "./core/concat";
99
export { LabelEncoder, OneHotEncoder } from "./preprocessing/encodings";
@@ -14,5 +14,6 @@ export { Configs } from "./config/config";
1414
export { NDframe };
1515
export { Str } from "./core/strings";
1616
export { Utils } from "./core/utils";
17+
export * as tf from "@tensorflow/tfjs";
1718

18-
export const _version = "0.2.1";
19+
export const _version = "0.2.2";

danfojs/src/io/reader.js

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import * as tf from "@tensorflow/tfjs";
22
import fetch from "node-fetch";
33
import XLSX from "xlsx";
4-
import { open, Dataset, isDataset } from "frictionless.js";
5-
import toArray from "stream-to-array";
64
import { Utils } from "../core/utils";
75
import { DataFrame } from "../core/frame";
86

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

@@ -154,64 +153,3 @@ export const read_excel = async (kwargs) => {
154153
throw new Error(err);
155154
}
156155
};
157-
158-
/**
159-
* Opens a file using frictionless.js specification.
160-
* @param {string} pathOrDescriptor A path to the file/resources. It can be a local file,
161-
* a URL to a tabular data (CSV, EXCEL) or Datahub.io Data Resource.
162-
* Data comes with extra properties and specification conforming to the Frictionless Data standards.
163-
* @param {object} configs { data_num (Defaults => 0): The specific dataset to load, when reading data from a datapackage.json,
164-
* header (Defaults => true): Whether the dataset contains header or not.
165-
* }
166-
* @returns {DataFrame} Danfo DataFrame/Series
167-
*/
168-
export const read = async (
169-
path_or_descriptor,
170-
configs = { data_num: 0, header: true }
171-
) => {
172-
let data_num = configs["data_num"];
173-
let header = configs["header"];
174-
let rows, file;
175-
176-
if (isDataset(path_or_descriptor)) {
177-
console.log(
178-
"datapackage.json found. Loading Dataset package from Datahub.io"
179-
);
180-
const dataset = await Dataset.load(path_or_descriptor);
181-
file = dataset.resources[data_num];
182-
//TODO: toArray does not work in browser env, so this feature breaks when build for the web.
183-
// To fix this, we need a function to convert stream into text
184-
rows = await toArray(await file.rows());
185-
} else {
186-
try {
187-
file = open(path_or_descriptor);
188-
rows = await toArray(await file.rows());
189-
} catch (error) {
190-
console.log(error);
191-
}
192-
}
193-
194-
if ([ "csv", "xls", "xlsx" ].includes(await file.descriptor.format)) {
195-
if (header) {
196-
let df = new DataFrame(rows.slice(1), { columns: rows[0] });
197-
return df;
198-
} else {
199-
let df = new DataFrame(rows);
200-
return df;
201-
}
202-
} else {
203-
let df = new DataFrame(rows);
204-
return df;
205-
}
206-
};
207-
208-
// /**
209-
// * Reads a Database into DataFrame
210-
// *
211-
// * @param {source} URL or local file path to retreive JSON file.
212-
// * @returns {Promise} DataFrame structure of parsed CSV data
213-
// */
214-
// export const read_sql = async (source) => {
215-
216-
// return "TODO"
217-
// }

0 commit comments

Comments
 (0)