Skip to content

Commit b75cc01

Browse files
committed
Refactor app to use single folder and share types
1 parent 4c09ff0 commit b75cc01

38 files changed

Lines changed: 6643 additions & 423 deletions

src/danfojs-base/core/frame.ts

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ import {
1616
ArrayType1D,
1717
ArrayType2D,
1818
DataFrameInterface,
19-
BaseDataOptionType
19+
BaseDataOptionType,
20+
CsvOutputOptionsBrowser,
21+
ExcelOutputOptionsBrowser,
22+
JsonOutputOptionsBrowser,
23+
CsvOutputOptionsNode,
24+
ExcelOutputOptionsNode,
25+
JsonOutputOptionsNode
2026
} from "../shared/types";
2127
import dummyEncode from "../transformers/encoders/dummy.encoder"
2228
import { variance, std, median, mode, mean } from 'mathjs';
@@ -30,6 +36,9 @@ import NDframe from "./generic";
3036
import { table } from "table";
3137
import Series from './series';
3238
import Groupby from '../aggregators/groupby'
39+
import { PlotlyLib } from "../plotting";
40+
import { toCSVBrowser, toExcelBrowser, toJSONBrowser } from "../io/browser";
41+
import { toCSVNode, toExcelNode, toJSONNode } from "../io/node";
3342

3443
const utils = new Utils();
3544

@@ -2450,4 +2459,77 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
24502459
).group()
24512460

24522461
}
2462+
2463+
/**
2464+
* Make plots of Series or DataFrame.
2465+
* Uses the Plotly as backend, so supports Plotly's configuration parameters
2466+
* @param divId Name of the div to show the plot
2467+
* @returns Plotly class that expoese different plot type
2468+
*/
2469+
plot(divId: string) {
2470+
//TODO: Add support for check plot library to use
2471+
// So we can support other plot library like d3, vega, etc
2472+
if (utils.isBrowserEnv()) {
2473+
const plt = new PlotlyLib(this, divId);
2474+
return plt;
2475+
}else{
2476+
throw new Error("Not supported in NodeJS");
2477+
}
2478+
}
2479+
2480+
/**
2481+
* Converts a DataFrame or Series to CSV.
2482+
* @param options Configuration object. Supports the following options:
2483+
* - `filePath`: Local file path to write the CSV file. If not specified, the CSV will be returned as a string.
2484+
* - `header`: Boolean indicating whether to include a header row in the CSV file.
2485+
* - `sep`: Character to be used as a separator in the CSV file.
2486+
*/
2487+
toCSV(options?: CsvOutputOptionsBrowser | CsvOutputOptionsNode): string
2488+
toCSV(options?: CsvOutputOptionsBrowser | CsvOutputOptionsNode): string | void {
2489+
if (utils.isBrowserEnv()) {
2490+
return toCSVBrowser(this, options as CsvOutputOptionsBrowser)
2491+
} else {
2492+
return toCSVNode(this, options as CsvOutputOptionsNode)
2493+
}
2494+
}
2495+
2496+
/**
2497+
* Converts a DataFrame or Series to JSON.
2498+
* @param options Configuration object. Supported options:
2499+
* - `filePath`: The file path to write the JSON to. If not specified, the JSON object is returned.
2500+
* - `format`: The format of the JSON. Defaults to `'column'`. E.g for using `column` format:
2501+
* ```
2502+
* [{ "a": 1, "b": 2, "c": 3, "d": 4 },
2503+
* { "a": 5, "b": 6, "c": 7, "d": 8 }]
2504+
* ```
2505+
* and `row` format:
2506+
* ```
2507+
* { "a": [1, 5, 9],
2508+
* "b": [2, 6, 10]
2509+
* }
2510+
* ```
2511+
*/
2512+
toJSON(options?: JsonOutputOptionsBrowser | JsonOutputOptionsNode): object
2513+
toJSON(options?: JsonOutputOptionsBrowser | JsonOutputOptionsNode): object | void {
2514+
if (utils.isBrowserEnv()) {
2515+
return toJSONBrowser(this, options as JsonOutputOptionsBrowser)
2516+
} else {
2517+
return toJSONNode(this, options as JsonOutputOptionsNode)
2518+
}
2519+
}
2520+
2521+
2522+
/**
2523+
* Converts a DataFrame or Series to Excel Sheet.
2524+
* @param options Configuration object. Supported options:
2525+
* - `sheetName`: The sheet name to be written to. Defaults to `'Sheet1'`.
2526+
* - `filePath`: The filePath to be written to. Defaults to `'./output.xlsx'`.
2527+
*/
2528+
toExcel(options?: ExcelOutputOptionsBrowser | ExcelOutputOptionsNode): void {
2529+
if (utils.isBrowserEnv()) {
2530+
toExcelBrowser(this, options as ExcelOutputOptionsBrowser)
2531+
} else {
2532+
return toExcelNode(this, options as ExcelOutputOptionsNode)
2533+
}
2534+
}
24532535
}

src/danfojs-base/core/series.ts

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,17 @@
1212
* limitations under the License.
1313
* ==========================================================================
1414
*/
15-
import { ArrayType1D, BaseDataOptionType, SeriesInterface } from "../shared/types";
15+
import {
16+
ArrayType1D,
17+
BaseDataOptionType,
18+
SeriesInterface,
19+
CsvOutputOptionsBrowser,
20+
ExcelOutputOptionsBrowser,
21+
JsonOutputOptionsBrowser,
22+
CsvOutputOptionsNode,
23+
ExcelOutputOptionsNode,
24+
JsonOutputOptionsNode
25+
} from "../shared/types";
1626
import { variance, std, median, mode } from 'mathjs';
1727
import { _genericMathOp } from "./math.ops";
1828
import { DATA_TYPES } from '../shared/defaults'
@@ -26,6 +36,9 @@ import Dt from './datetime';
2636
import dummyEncode from "../transformers/encoders/dummy.encoder";
2737
import DataFrame from "./frame";
2838
import tensorflow from '../shared/tensorflowlib'
39+
import { PlotlyLib } from "../plotting";
40+
import { toCSVBrowser, toExcelBrowser, toJSONBrowser } from "../io/browser";
41+
import { toCSVNode, toExcelNode, toJSONNode } from "../io/node";
2942

3043
const utils = new Utils();
3144

@@ -1358,4 +1371,78 @@ export default class Series extends NDframe implements SeriesInterface {
13581371
}): DataFrame {
13591372
return dummyEncode(this, options)
13601373
}
1374+
1375+
1376+
/**
1377+
* Make plots of Series or DataFrame.
1378+
* Uses the Plotly as backend, so supports Plotly's configuration parameters
1379+
* @param divId Name of the div to show the plot
1380+
* @returns Plotly class that expoese different plot type
1381+
*/
1382+
plot(divId: string) {
1383+
//TODO: Add support for check plot library to use
1384+
// So we can support other plot library like d3, vega, etc
1385+
if (utils.isBrowserEnv()) {
1386+
const plt = new PlotlyLib(this, divId);
1387+
return plt;
1388+
} else {
1389+
throw new Error("Not supported in NodeJS");
1390+
}
1391+
}
1392+
1393+
/**
1394+
* Converts a DataFrame or Series to CSV.
1395+
* @param options Configuration object. Supports the following options:
1396+
* - `filePath`: Local file path to write the CSV file. If not specified, the CSV will be returned as a string.
1397+
* - `header`: Boolean indicating whether to include a header row in the CSV file.
1398+
* - `sep`: Character to be used as a separator in the CSV file.
1399+
*/
1400+
toCSV(options?: CsvOutputOptionsBrowser | CsvOutputOptionsNode): string
1401+
toCSV(options?: CsvOutputOptionsBrowser | CsvOutputOptionsNode): string | void {
1402+
if (utils.isBrowserEnv()) {
1403+
return toCSVBrowser(this, options as CsvOutputOptionsBrowser)
1404+
} else {
1405+
return toCSVNode(this, options as CsvOutputOptionsNode)
1406+
}
1407+
}
1408+
1409+
/**
1410+
* Converts a DataFrame or Series to JSON.
1411+
* @param options Configuration object. Supported options:
1412+
* - `filePath`: The file path to write the JSON to. If not specified, the JSON object is returned.
1413+
* - `format`: The format of the JSON. Defaults to `'column'`. E.g for using `column` format:
1414+
* ```
1415+
* [{ "a": 1, "b": 2, "c": 3, "d": 4 },
1416+
* { "a": 5, "b": 6, "c": 7, "d": 8 }]
1417+
* ```
1418+
* and `row` format:
1419+
* ```
1420+
* { "a": [1, 5, 9],
1421+
* "b": [2, 6, 10]
1422+
* }
1423+
* ```
1424+
*/
1425+
toJSON(options?: JsonOutputOptionsBrowser | JsonOutputOptionsNode): object
1426+
toJSON(options?: JsonOutputOptionsBrowser | JsonOutputOptionsNode): object | void {
1427+
if (utils.isBrowserEnv()) {
1428+
return toJSONBrowser(this, options as JsonOutputOptionsBrowser)
1429+
} else {
1430+
return toJSONNode(this, options as JsonOutputOptionsNode)
1431+
}
1432+
}
1433+
1434+
1435+
/**
1436+
* Converts a DataFrame or Series to Excel Sheet.
1437+
* @param options Configuration object. Supported options:
1438+
* - `sheetName`: The sheet name to be written to. Defaults to `'Sheet1'`.
1439+
* - `filePath`: The filePath to be written to. Defaults to `'./output.xlsx'`.
1440+
*/
1441+
toExcel(options?: ExcelOutputOptionsBrowser | ExcelOutputOptionsNode): void {
1442+
if (utils.isBrowserEnv()) {
1443+
toExcelBrowser(this, options as ExcelOutputOptionsBrowser)
1444+
} else {
1445+
return toExcelNode(this, options as ExcelOutputOptionsNode)
1446+
}
1447+
}
13611448
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ import {
2626
import { $readExcel, $toExcel } from "./io.excel"
2727

2828
export {
29-
$readCSV as readCSV,
30-
$streamCSV as streamCSV,
31-
$toCSV as toCSV,
29+
$readCSV as readCSVBrowser,
30+
$streamCSV as streamCSVBrowser,
31+
$toCSV as toCSVBrowser,
3232
// $openCsvInputStream as openCsvInputStream,
3333
// $writeCsvOutputStream as writeCsvOutputStream,
34-
$readJSON as readJSON,
35-
$toJSON as toJSON,
36-
$readExcel as readExcel,
37-
$toExcel as toExcel,
34+
$readJSON as readJSONBrowser,
35+
$toJSON as toJSONBrowser,
36+
$readExcel as readExcelBrowser,
37+
$toExcel as toExcelBrowser,
3838
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
* ==========================================================================
1414
*/
1515
import Papa from 'papaparse'
16-
import { DataFrame, NDframe, Series } from '../index'
17-
import { ArrayType2D } from "../../../danfojs-base/shared/types"
18-
import { CsvInputOptions, CsvOutputOptionsBrowser } from "../types"
16+
import { DataFrame, NDframe, Series } from '../../'
17+
import { CsvInputOptionsBrowser, CsvOutputOptionsBrowser, ArrayType2D} from "../../shared/types"
1918

2019

2120
/**
@@ -46,7 +45,7 @@ import { CsvInputOptions, CsvOutputOptionsBrowser } from "../types"
4645
* const df = await readCSV("./data/sample.csv")
4746
* ```
4847
*/
49-
const $readCSV = async (file: any, options?: CsvInputOptions): Promise<DataFrame> => {
48+
const $readCSV = async (file: any, options?: CsvInputOptionsBrowser): Promise<DataFrame> => {
5049
return new Promise(resolve => {
5150
Papa.parse(file, {
5251
header: true,
@@ -75,7 +74,7 @@ const $readCSV = async (file: any, options?: CsvInputOptions): Promise<DataFrame
7574
* })
7675
* ```
7776
*/
78-
const $streamCSV = async (file: string, callback: (df: DataFrame) => void, options: CsvInputOptions,): Promise<null> => {
77+
const $streamCSV = async (file: string, callback: (df: DataFrame) => void, options: CsvInputOptionsBrowser,): Promise<null> => {
7978
return new Promise(resolve => {
8079
let count = -1
8180
Papa.parse(file, {
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
* ==========================================================================
1414
*/
1515
import XLSX from 'xlsx';
16-
import { DataFrame, NDframe, Series } from '../index'
17-
import { ArrayType1D, ArrayType2D } from '../../../danfojs-base/shared/types'
18-
import { ExcelOutputOptionsBrowser, ExcelInputOptions } from "../types"
16+
import { DataFrame, NDframe, Series } from '../../'
17+
import { ArrayType1D, ArrayType2D, ExcelOutputOptionsBrowser, ExcelInputOptionsBrowser } from "../../shared/types"
1918

2019
/**
2120
* Reads a JSON file from local or remote location into a DataFrame.
@@ -40,7 +39,7 @@ import { ExcelOutputOptionsBrowser, ExcelInputOptions } from "../types"
4039
* })
4140
* ```
4241
*/
43-
const $readExcel = async (file: any, options?: ExcelInputOptions) => {
42+
const $readExcel = async (file: any, options?: ExcelInputOptionsBrowser) => {
4443
const { sheet, method, headers } = { sheet: 0, method: "GET", headers: {}, ...options }
4544

4645
if (typeof file === "string" && file.startsWith("http")) {
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { DataFrame, NDframe, Series } from '../index'
2-
import { ArrayType1D, ArrayType2D } from '../../../danfojs-base/shared/types'
3-
import { JsonInputOptions, JsonOutputOptionsBrowser } from '../types'
1+
import { DataFrame, NDframe, Series } from '../../'
2+
import { ArrayType1D, ArrayType2D, JsonInputOptionsBrowser, JsonOutputOptionsBrowser } from '../../shared/types'
43
/**
54
* Reads a JSON file from local or remote location into a DataFrame.
65
* @param fileName URL or local file path to JSON file.
@@ -28,7 +27,7 @@ import { JsonInputOptions, JsonOutputOptionsBrowser } from '../types'
2827
* const df = await readJSON("./data/sample.json")
2928
* ```
3029
*/
31-
const $readJSON = async (file: any, options?: JsonInputOptions) => {
30+
const $readJSON = async (file: any, options?: JsonInputOptionsBrowser) => {
3231
const { method, headers } = { method: "GET", headers: {}, ...options }
3332

3433
if (typeof file === "string" && file.startsWith("http")) {
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ import {
2727
import { $readExcel, $toExcel } from "./io.excel"
2828

2929
export {
30-
$readCSV as readCSV,
31-
$streamCSV as streamCSV,
32-
$toCSV as toCSV,
33-
$openCsvInputStream as openCsvInputStream,
34-
$writeCsvOutputStream as writeCsvOutputStream,
35-
$readJSON as readJSON,
36-
$toJSON as toJSON,
37-
$streamJSON as streamJSON,
38-
$readExcel as readExcel,
39-
$toExcel as toExcel,
30+
$readCSV as readCSVNode,
31+
$streamCSV as streamCSVNode,
32+
$toCSV as toCSVNode,
33+
$openCsvInputStream as openCsvInputStreamNode,
34+
$writeCsvOutputStream as writeCsvOutputStreamNode,
35+
$readJSON as readJSONNode,
36+
$toJSON as toJSONNode,
37+
$streamJSON as streamJSONNode,
38+
$readExcel as readExcelNode,
39+
$toExcel as toExcelNode,
4040
}
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ import fs from 'fs'
1616
import Papa from 'papaparse'
1717
import request from "request"
1818
import stream from "stream"
19-
import { DataFrame, NDframe, Series } from '../index'
20-
import { ArrayType2D } from "../../../danfojs-base/shared/types"
21-
import { CsvInputOptions, CsvOutputOptionsNode } from "../types"
19+
import { DataFrame, NDframe, Series } from '../../'
20+
import { CsvInputOptionsNode, CsvOutputOptionsNode, ArrayType2D } from "../../shared/types"
2221

2322
/**
2423
* Reads a CSV file from local or remote location into a DataFrame.
@@ -48,7 +47,7 @@ import { CsvInputOptions, CsvOutputOptionsNode } from "../types"
4847
* const df = await readCSV("./data/sample.csv")
4948
* ```
5049
*/
51-
const $readCSV = async (filePath: string, options?: CsvInputOptions): Promise<DataFrame> => {
50+
const $readCSV = async (filePath: string, options?: CsvInputOptionsNode): Promise<DataFrame> => {
5251
if (filePath.startsWith("http") || filePath.startsWith("https")) {
5352
return new Promise(resolve => {
5453
const optionsWithDefaults = {
@@ -100,7 +99,7 @@ const $readCSV = async (filePath: string, options?: CsvInputOptions): Promise<Da
10099
* })
101100
* ```
102101
*/
103-
const $streamCSV = async (filePath: string, callback: (df: DataFrame) => void, options?: CsvInputOptions): Promise<null> => {
102+
const $streamCSV = async (filePath: string, callback: (df: DataFrame) => void, options?: CsvInputOptionsNode): Promise<null> => {
104103

105104
if (filePath.startsWith("http") || filePath.startsWith("https")) {
106105
const optionsWithDefaults = {
@@ -211,7 +210,7 @@ const $toCSV = (df: NDframe | DataFrame | Series, options?: CsvOutputOptionsNode
211210
* const csvStream = openCsvInputStream("./data/sample.csv")
212211
* ```
213212
*/
214-
const $openCsvInputStream = (filePath: string, options: CsvInputOptions) => {
213+
const $openCsvInputStream = (filePath: string, options: CsvInputOptionsNode) => {
215214
const { header } = { header: true, ...options }
216215
let isFirstChunk = true
217216
let ndFrameColumnNames: any = []
@@ -306,7 +305,7 @@ const $openCsvInputStream = (filePath: string, options: CsvInputOptions) => {
306305
* csvStream.pipe(convertFunctionTotransformer(transformer)).pipe(outStream)
307306
* ```
308307
*/
309-
const $writeCsvOutputStream = (filePath: string, options: CsvInputOptions) => {
308+
const $writeCsvOutputStream = (filePath: string, options: CsvInputOptionsNode) => {
310309
let isFirstRow = true
311310
const fileOutputStream = fs.createWriteStream(filePath)
312311
const csvOutputStream = new stream.Writable({ objectMode: true })

0 commit comments

Comments
 (0)