Skip to content

Commit a59b4af

Browse files
committed
Update tf function imports
1 parent 364c82a commit a59b4af

9 files changed

Lines changed: 66 additions & 371 deletions

File tree

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ export { NDframe };
1515
export { Str } from "./core/strings";
1616
export { Utils } from "./core/utils";
1717

18-
export const _version = "0.2.1";
18+
export const _version = "0.2.3";

danfojs/src/io/reader.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as tf from "@tensorflow/tfjs";
1+
import { data } from "@tensorflow/tfjs";
22
import fetch from "node-fetch";
33
import XLSX from "xlsx";
44
import { open, Dataset, isDataset } from "frictionless.js";
@@ -27,11 +27,12 @@ export const read_csv = async (source, chunk) => {
2727
)
2828
) {
2929
//probabily a relative path, append file:// to it
30+
// eslint-disable-next-line no-undef
3031
source = `file://${process.cwd()}/${source}`;
3132
}
3233

3334
let data = [];
34-
const csvDataset = tf.data.csv(source);
35+
const csvDataset = data.csv(source);
3536
const column_names = await csvDataset.columnNames();
3637
const sample = csvDataset.take(chunk);
3738
await sample.forEachAsync((row) => data.push(Object.values(row)));

danfojs/src/preprocessing/scalers.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as tf from "@tensorflow/tfjs";
1+
import { tensor, moments } from "@tensorflow/tfjs";
22
import { Series } from "../core/series";
33
import { DataFrame } from "../core/frame";
44
import { Utils } from "../core/utils";
@@ -14,12 +14,12 @@ export class MinMaxScaler {
1414
fit(data) {
1515
let tensor_data = null;
1616
if (Array.isArray(data)) {
17-
tensor_data = tf.tensor(data);
17+
tensor_data = tensor(data);
1818
} else if (data instanceof DataFrame || data instanceof Series) {
1919
if (data.dtypes.includes("string")) {
2020
throw Error("Dtype Error: Cannot perform operation on string dtypes");
2121
}
22-
tensor_data = tf.tensor(data.values);
22+
tensor_data = tensor(data.values);
2323
} else {
2424
throw new Error("data must either be an Array, DataFrame or Series");
2525
}
@@ -49,14 +49,14 @@ export class MinMaxScaler {
4949
if (data.dtypes.includes("string")) {
5050
throw Error("Dtype Error: Cannot perform operation on string dtypes");
5151
}
52-
let tensor_data = tf.tensor(data.values);
52+
let tensor_data = tensor(data.values);
5353
let output_data = tensor_data
5454
.sub(this.min)
5555
.div(this.max.sub(this.min))
5656
.arraySync();
5757
return new Series(output_data);
5858
} else if (Array.isArray(data)) {
59-
let tensor_data = tf.tensor(data);
59+
let tensor_data = tensor(data);
6060
let output_data = tensor_data
6161
.sub(this.min)
6262
.div(this.max.sub(this.min))
@@ -70,7 +70,7 @@ export class MinMaxScaler {
7070
if (data.dtypes.includes("string")) {
7171
throw Error("Dtype Error: Cannot perform operation on string dtypes");
7272
}
73-
let tensor_data = tf.tensor(data.values);
73+
let tensor_data = tensor(data.values);
7474
let output_data = tensor_data
7575
.sub(this.min)
7676
.div(this.max.sub(this.min))
@@ -91,17 +91,17 @@ export class StandardScaler {
9191
fit(data) {
9292
let tensor_data = null;
9393
if (Array.isArray(data)) {
94-
tensor_data = tf.tensor(data);
94+
tensor_data = tensor(data);
9595
} else if (data instanceof DataFrame || data instanceof Series) {
9696
if (data.dtypes.includes("string")) {
9797
throw Error("Dtype Error: Cannot perform operation on string dtypes");
9898
}
99-
tensor_data = tf.tensor(data.values);
99+
tensor_data = tensor(data.values);
100100
} else {
101101
throw new Error("data must either be an Array, DataFrame or Series");
102102
}
103103

104-
this.std = tf.moments(tensor_data, 0).variance.sqrt();
104+
this.std = moments(tensor_data, 0).variance.sqrt();
105105
this.mean = tensor_data.mean(0);
106106
let output_data = tensor_data.sub(this.mean).div(this.std).arraySync();
107107

@@ -121,11 +121,11 @@ export class StandardScaler {
121121
if (data.dtypes.includes("string")) {
122122
throw Error("Dtype Error: Cannot perform operation on string dtypes");
123123
}
124-
let tensor_data = tf.tensor(data.values);
124+
let tensor_data = tensor(data.values);
125125
let output_data = tensor_data.sub(this.mean).div(this.std).arraySync();
126126
return new Series(output_data);
127127
} else if (Array.isArray(data)) {
128-
let tensor_data = tf.tensor(data);
128+
let tensor_data = tensor(data);
129129
let output_data = tensor_data.sub(this.mean).div(this.std).arraySync();
130130
if (utils.__is_1D_array(data)) {
131131
return new Series(output_data);
@@ -136,7 +136,7 @@ export class StandardScaler {
136136
if (data.dtypes.includes("string")) {
137137
throw Error("Dtype Error: Cannot perform operation on string dtypes");
138138
}
139-
let tensor_data = tf.tensor(data.values);
139+
let tensor_data = tensor(data.values);
140140
let output_data = tensor_data.sub(this.mean).div(this.std).arraySync();
141141
return new DataFrame(output_data);
142142
} else {
@@ -245,21 +245,21 @@ export class StandardScaler {
245245
// let tensor_data = null
246246
// let isTensor = false;
247247
// if(Array.isArray(data)){
248-
// tensor_data = tf.tensor(data)
248+
// tensor_data = tensor(data)
249249
// }
250250
// else if((data instanceof DataFrame)){
251-
// tensor_data = tf.tensor(data.values)
251+
// tensor_data = tensor(data.values)
252252
// isTensor = true;
253253
// }
254254
// else if((data instanceof Series)){
255-
// tensor_data = tf.tensor(data.values)
255+
// tensor_data = tensor(data.values)
256256
// }
257257
// else{
258258
// throw new Error("data must either be an Array, DataFrame or Series")
259259
// }
260260

261261
// let [q1, q3, median] = this.quantile(data,isTensor)
262-
// let q3_tensor = tf.tensor(q3)
262+
// let q3_tensor = tensor(q3)
263263
// let output_data = tensor_data.sub(median).div(q3_tensor.sub(q1)).arraySync()
264264

265265
// return output_data;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,6 @@
8888
"lcov",
8989
"text"
9090
]
91-
}
91+
},
92+
"sideEffects": false
9293
}

0 commit comments

Comments
 (0)