aboutsummaryrefslogtreecommitdiff
path: root/src/lib/info.ts
blob: f7d3378177205a014aa8347231c72f9870c892e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import Izuku from '../index';
import { table } from 'table';
import { sizeof } from '../helpers/memorySize';
/**
 * size returns the total number of elements in the frame
 * @returns the total number of elements in the frame
 * @returns 0 if the frame is empty
 */
export function getSize(rowdata: any[]): number {
  // Get the number of elements in 2D array, do not count nulls
  const numberOfElements = rowdata.reduce(
    (
      acc: any,
      row: {
        filter: (arg0: (item: any) => boolean) => {
          (): any;
          new (): any;
          length: any;
        };
      }
    ) => {
      return acc + row.filter((item: null) => item !== null).length;
    },
    0
  );
  return numberOfElements;
}

/**
 * info returns the type of data present in each column of the frame
 * @returns the type of data present in each column of the frame
 */
export function info(this: Izuku): void {
  const info: Array<any[]> = [];
  info.push(['#', 'Column Name', 'Types', 'Empty Values']);
  let counter = 0;
  const countDataTypes = {} as any;
  this.columns.forEach((column: string, index: number) => {
    // get all the types of data in the column, do not repeat the same type
    let nullValuesPresentInRow = false;
    const columnDataTypes = this.rowdata.map((row: any[]) => {
      if (
        row[index] === null ||
        row[index] === undefined ||
        row[index] === ''
      ) {
        nullValuesPresentInRow = true;
      }
      const currentType = String(typeof row[index]);
      if (!Object.keys(countDataTypes).includes(currentType)) {
        Object.assign(countDataTypes, { [currentType]: 1 });
      } else {
        const getKeyValue = <T, K extends keyof T>(obj: T, key: K): T[K] =>
          obj[key];
        const currentValue = getKeyValue(countDataTypes, currentType);
        Object.assign(countDataTypes, { [currentType]: currentValue + 1 });
      }
      return typeof row[index];
    });
    const uniqueDataTypes = [...new Set(columnDataTypes)];
    info.push([counter, column, [...uniqueDataTypes], nullValuesPresentInRow]);
    counter++;
  });
  let dataTypesString = '';
  // Iterate through the countDataTypes object and create a string of the data types
  Object.keys(countDataTypes).forEach((key: string) => {
    dataTypesString += `${key}(${countDataTypes[key]})`;
    // check if the key is not the last key in the object
    if (
      Object.keys(countDataTypes).indexOf(key) !==
      Object.keys(countDataTypes).length - 1
    ) {
      dataTypesString += ', ';
    }
  });

  console.log(`RangeIndex: ${this.size} elements, 0 to ${this.size - 1}`);
  console.log(
    `Shape: ${this.shape.split(' x ')[0].trim()} rows, ${this.shape
      .split(' x ')[1]
      .trim()} columns`
  );
  console.log(table(info));
  // Remove the previous printed newline
  process.stdout.write('\x1B[1A\x1B[2K');
  console.log(`Data Types: ${dataTypesString}`);
  console.log(`Memory Usage: ${sizeof(this)} bytes`);
}