TS typing for Dataframe (#2382)

* initial TS typing

* first cut at Dataframe TS typing

* more Dataframe typing

* comments

* more Dataframe cleanup

* PR review fixes and improvements
This commit is contained in:
Bruce Martin
2021-08-17 10:43:40 -07:00
committed by GitHub
parent 3fdf5cac9d
commit 45cecad76a
39 changed files with 1298 additions and 1124 deletions
@@ -4,6 +4,7 @@ See also reducers/annotations.js
*/
import { Schema } from "../../common/types/schema";
import { Dataframe, LabelType } from "../dataframe";
/*
There are a number of state constraints assumed throughout the
@@ -57,9 +58,12 @@ export function isUserAnnotation(annoMatrix, name) {
return _isUserAnnotation(annoMatrix.schema, name);
}
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'df' implicitly has an 'any' type.
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types --- FIXME: disabled temporarily on migrate to TS.
export function allHaveLabelByMask(df, colName, label, mask) {
export function allHaveLabelByMask(
df: Dataframe,
colName: LabelType,
label: string,
mask: Uint8Array
): boolean {
// return true if all rows as indicated by mask have the colname set to label.
// False if not.
const col = df.col(colName);
+12 -9
View File
@@ -7,6 +7,7 @@ import memoize from "memoize-one";
import * as globals from "../../globals";
import parseRGB from "../parseRGB";
import { range } from "../range";
import { Dataframe, LabelType } from "../dataframe";
/*
given a color mode & accessor, generate an annoMatrix query that will
@@ -95,18 +96,19 @@ Returns:
}
*/
function _createColorTable(
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
colorMode: any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
colorByAccessor: any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
colorByData: any,
colorMode: string | null,
colorByAccessor: LabelType | null,
colorByData: Dataframe | null,
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
schema: any,
userColors = null
) {
if (colorMode === null || colorByData === null)
return defaultColors(schema.dataframe.nObs);
switch (colorMode) {
case "color by categorical metadata": {
if (colorByAccessor === null) return defaultColors(schema.dataframe.nObs);
const data = colorByData.col(colorByAccessor).asArray();
// @ts-expect-error ts-migrate(2531) FIXME: Object is possibly 'null'.
if (userColors && colorByAccessor in userColors) {
@@ -115,18 +117,19 @@ function _createColorTable(
return createColorsByCategoricalMetadata(data, colorByAccessor, schema);
}
case "color by continuous metadata": {
if (colorByAccessor === null) return defaultColors(schema.dataframe.nObs);
const col = colorByData.col(colorByAccessor);
const { min, max } = col.summarize();
const { min, max } = col.summarizeContinuous();
return createColorsByContinuousMetadata(col.asArray(), min, max);
}
case "color by expression": {
const col = colorByData.icol(0);
const { min, max } = col.summarize();
const { min, max } = col.summarizeContinuous();
return createColorsByContinuousMetadata(col.asArray(), min, max);
}
case "color by geneset mean expression": {
const col = colorByData.icol(0);
const { min, max } = col.summarize();
const { min, max } = col.summarizeContinuous();
return createColorsByContinuousMetadata(col.asArray(), min, max);
}
default: {
+21 -27
View File
@@ -1,6 +1,10 @@
import { flatbuffers } from "flatbuffers";
import { NetEncoding } from "./matrix_generated";
import { isTypedArray, isFloatTypedArray } from "../../common/types/arraytypes";
import {
TypedArray,
isTypedArray,
isFloatTypedArray,
} from "../../common/types/arraytypes";
import {
Dataframe,
IdentityInt32Index,
@@ -94,14 +98,13 @@ function encodeTypedArray(builder: any, uType: any, uData: any) {
return builder.endObject();
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any -- - FIXME: disabled temporarily on migrate to TS.
export function encodeMatrixFBS(df: any) {
export function encodeMatrixFBS(df: Dataframe): Uint8Array {
/*
encode the dataframe as an FBS Matrix
*/
/* row indexing not supported currently */
if (df.rowIndex.constructor !== IdentityInt32Index) {
if (!(df.rowIndex instanceof IdentityInt32Index)) {
throw new Error("FBS does not support row index encoding at this time");
}
@@ -115,11 +118,9 @@ export function encodeMatrixFBS(df: any) {
let encColumns;
if (shape[0] > 0 && shape[1] > 0) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
const columns = df.columns().map((col: any) => col.asArray());
const columns = df.columns().map((col) => col.asArray());
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
const cols = columns.map((carr: any) => {
const cols = columns.map((carr) => {
let uType;
let tarr;
if (isTypedArray(carr)) {
@@ -179,8 +180,7 @@ export function encodeMatrixFBS(df: any) {
return builder.asUint8Array();
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
function promoteTypedArray(o: any) {
function promoteTypedArray(o: TypedArray) {
/*
Decide what internal data type to use for the data returned from
the server.
@@ -212,8 +212,9 @@ function promoteTypedArray(o: any) {
return new TypedArrayCtor(o);
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any -- - FIXME: disabled temporarily on migrate to TS.
export function matrixFBSToDataframe(arrayBuffers: any) {
export function matrixFBSToDataframe(
arrayBuffers: ArrayBuffer | ArrayBuffer[]
): Dataframe {
/*
Convert array of Matrix FBS to a Dataframe.
@@ -229,37 +230,30 @@ export function matrixFBSToDataframe(arrayBuffers: any) {
arrayBuffers = [arrayBuffers];
}
if (arrayBuffers.length === 0) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
return (Dataframe as any).Dataframe.empty();
return Dataframe.empty();
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
const fbs = arrayBuffers.map((ab: any) => decodeMatrixFBS(ab, true)); // leave in place
const fbs = arrayBuffers.map((ab) => decodeMatrixFBS(ab, true)); // leave in place
/* check that all FBS have same row dimensionality */
const { nRows } = fbs[0];
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
fbs.forEach((b: any) => {
fbs.forEach((b) => {
if (b.nRows !== nRows)
throw new Error("FBS with inconsistent dimensionality");
});
const columns = fbs // eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
.map((fb: any) =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
fb.columns.map((c: any) => {
const columns = fbs
.map((fb) =>
fb.columns.map((c) => {
if (isFloatTypedArray(c) || Array.isArray(c)) return c;
return promoteTypedArray(c);
})
)
.flat();
// colIdx may be TypedArray or Array
const colIdx = fbs // eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
.map((b: any) =>
Array.isArray(b.colIdx) ? b.colIdx : Array.from(b.colIdx)
)
const colIdx = fbs
.map((b) => (Array.isArray(b.colIdx) ? b.colIdx : Array.from(b.colIdx)))
.flat();
const nCols = columns.length;
// @ts-expect-error ts-migrate(2345) FIXME: Argument of type 'KeyIndex' is not assignable to p... Remove this comment to see the full error message
const df = new Dataframe([nRows, nCols], columns, null, new KeyIndex(colIdx));
return df;
}
@@ -37,6 +37,7 @@ Views can be interogated for their type with the following:
import { clip, isubsetMask, isubset } from "../../annoMatrix";
import { memoize } from "../dataframe/util";
import { Dataframe, LabelIndex } from "../dataframe";
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'annoMatrix' implicitly has an 'any' typ... Remove this comment to see the full error message
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types --- FIXME: disabled temporarily on migrate to TS.
@@ -101,7 +102,7 @@ export function _userResetSubsetAnnoMatrix(annoMatrix) {
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'annoMatrix' implicitly has an 'any' typ... Remove this comment to see the full error message
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types --- FIXME: disabled temporarily on migrate to TS.
export function _setEmbeddingSubset(annoMatrix, embeddingDf) {
export function _setEmbeddingSubset(annoMatrix, embeddingDf: Dataframe) {
/*
Set the embedding subset view. Only create a subset view for the embedding
when it is needed, ie, there are NaN values in the embeddings.
@@ -141,8 +142,10 @@ export function _setEmbeddingSubset(annoMatrix, embeddingDf) {
return annoMatrix;
}
// @ts-expect-error ts-migrate(6133) FIXME: 'baseRowIndex' is declared but its value is never ... Remove this comment to see the full error message
function _getEmbeddingRowOffsets(baseRowIndex, embeddingDf) {
function _getEmbeddingRowOffsets(
_baseRowIndex: LabelIndex,
embeddingDf: Dataframe
) {
/*
given a dataframe containing an embedding:
- if the embedding contains no NaN coordinates, return null
@@ -167,17 +170,16 @@ function _getEmbeddingRowOffsets(baseRowIndex, embeddingDf) {
return offsets.subarray(0, numOffsets);
}
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'embeddingDf' implicitly has an 'any' ty... Remove this comment to see the full error message
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types --- FIXME: disabled temporarily on migrate to TS.
export function _getDiscreteCellEmbeddingRowIndex(embeddingDf) {
export function _getDiscreteCellEmbeddingRowIndex(
embeddingDf: Dataframe
): LabelIndex {
const idx = _getEmbeddingRowOffsets(embeddingDf.rowIndex, embeddingDf);
if (idx === null) return embeddingDf.rowIndex;
return embeddingDf.rowIndex.isubset(idx);
}
export const getDiscreteCellEmbeddingRowIndex = memoize(
_getDiscreteCellEmbeddingRowIndex,
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'df' implicitly has an 'any' type.
(df) => df.__id
(df: Dataframe) => df.__id
);
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'annoMatrix' implicitly has an 'any' typ... Remove this comment to see the full error message