chore: add schema types (#2369)

This commit is contained in:
Timmy Huang
2021-08-04 13:16:18 -07:00
committed by GitHub
parent 95ce39f2e9
commit 26de334274
8 changed files with 157 additions and 82 deletions
@@ -3,6 +3,8 @@ Helper functions for user-editable annotations state management.
See also reducers/annotations.js
*/
import { Schema } from "../../common/types/entities";
/*
There are a number of state constraints assumed throughout the
application:
@@ -16,34 +18,36 @@ application:
In addition, the current state management only allows for
categorical annotations to be writable.
*/
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'schema' implicitly has an 'any' type.
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types --- FIXME: disabled temporarily on migrate to TS.
export function isCategoricalAnnotation(schema, name) {
/*
export function isCategoricalAnnotation(
schema: Schema,
name: string
): boolean | undefined {
/*
we treat any string, categorical or boolean as a categorical.
Return true/false/undefined (for unkonwn fields)
*/
const colSchema = schema.annotations.obsByName[name];
if (colSchema === undefined) return undefined;
const { type } = colSchema;
return type === "string" || type === "boolean" || type === "categorical";
}
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'schema' implicitly has an 'any' type.
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types --- FIXME: disabled temporarily on migrate to TS.
export function isContinuousAnnotation(schema, name) {
/*
Return true/false/undefined
*/
export function isContinuousAnnotation(
schema: Schema,
name: string
): boolean | undefined {
const colSchema = schema.annotations.obsByName[name];
if (colSchema === undefined) return undefined;
const { type } = colSchema;
return !(type === "string" || type === "boolean" || type === "categorical");
}
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'schema' implicitly has an 'any' type.
function _isUserAnnotation(schema, name) {
function _isUserAnnotation(schema: Schema, name: string): boolean {
return schema.annotations.obsByName[name]?.writable || false;
}
@@ -72,9 +76,8 @@ export function allHaveLabelByMask(df, colName, label, mask) {
}
const legalCharacters = /^(\w|[ .()-])+$/;
// @ts-expect-error ts-migrate(7006) FIXME: Parameter 'name' implicitly has an 'any' type.
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types --- FIXME: disabled temporarily on migrate to TS.
export function annotationNameIsErroneous(name) {
export function annotationNameIsErroneous(name: string): boolean | string {
/*
Validate the name - return:
* false - a valid name
@@ -101,6 +104,6 @@ export function annotationNameIsErroneous(name) {
}
}
/* all is well! Indicte not erroneous with a false */
/* all is well! Indicate not erroneous with a false */
return false;
}
+50 -45
View File
@@ -8,6 +8,12 @@ import cloneDeep from "lodash.clonedeep";
import fromEntries from "../fromEntries";
import catLabelSort from "../catLabelSort";
import {
RawSchema,
Schema,
LayoutColumn,
AnnotationColumn,
} from "../../common/types/entities";
/*
System wide schema assumptions:
@@ -15,31 +21,25 @@ System wide schema assumptions:
- schema will be internally self-consistent (eg, index matches columns)
*/
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any -- - FIXME: disabled temporarily on migrate to TS.
export function indexEntireSchema(schema: any) {
export function indexEntireSchema(schema: RawSchema): Schema {
/* Index schema for ease of use */
schema.annotations.obsByName = fromEntries(
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
schema.annotations?.obs?.columns?.map((v: any) => [v.name, v]) ?? []
(schema as Schema).annotations.obsByName = fromEntries(
schema.annotations?.obs?.columns?.map((v) => [v.name, v]) || []
);
schema.annotations.varByName = fromEntries(
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
schema.annotations?.var?.columns?.map((v: any) => [v.name, v]) ?? []
(schema as Schema).annotations.varByName = fromEntries(
schema.annotations?.var?.columns?.map((v) => [v.name, v]) || []
);
schema.layout.obsByName = fromEntries(
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
schema.layout?.obs?.map((v: any) => [v.name, v]) ?? []
(schema as Schema).layout.obsByName = fromEntries(
schema.layout?.obs?.map((v) => [v.name, v]) || []
);
schema.layout.varByName = fromEntries(
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
schema.layout?.var?.map((v: any) => [v.name, v]) ?? []
(schema as Schema).layout.varByName = fromEntries(
schema.layout?.var?.map((v) => [v.name, v]) || []
);
return schema;
return schema as Schema;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
function _copyObsAnno(schema: any) {
function _copyObsAnno(schema: Schema): Schema {
/* redux copy conventions - WARNING, only for modifying obs annotations */
return {
...schema,
@@ -50,8 +50,7 @@ function _copyObsAnno(schema: any) {
};
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
function _copyObsLayout(schema: any) {
function _copyObsLayout(schema: Schema): Schema {
return {
...schema,
layout: {
@@ -61,57 +60,62 @@ function _copyObsLayout(schema: any) {
};
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
function _reindexObsAnno(schema: any) {
function _reindexObsAnno(schema: Schema): Schema {
/* reindex obs annotations ONLY */
schema.annotations.obsByName = fromEntries(
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
schema.annotations.obs.columns.map((v: any) => [v.name, v])
schema.annotations.obs.columns.map((v) => [v.name, v])
);
return schema;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
function _reindexObsLayout(schema: any) {
function _reindexObsLayout(schema: Schema) {
schema.layout.obsByName = fromEntries(
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
schema.layout.obs.map((v: any) => [v.name, v])
schema.layout.obs.map((v) => [v.name, v])
);
return schema;
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any -- - FIXME: disabled temporarily on migrate to TS.
export function removeObsAnnoColumn(schema: any, name: any) {
export function removeObsAnnoColumn(schema: Schema, name: string): Schema {
const newSchema = _copyObsAnno(schema);
newSchema.annotations.obs.columns = schema.annotations.obs.columns.filter(
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
(v: any) => v.name !== name
(v) => v.name !== name
);
return _reindexObsAnno(newSchema);
}
// @ts-expect-error ts-migrate(6133) FIXME: 'name' is declared but its value is never read.
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any -- - FIXME: disabled temporarily on migrate to TS.
export function addObsAnnoColumn(schema: any, name: any, defn: any) {
export function addObsAnnoColumn(
schema: Schema,
_: string,
defn: AnnotationColumn
): Schema {
const newSchema = _copyObsAnno(schema);
newSchema.annotations.obs.columns.push(defn);
return _reindexObsAnno(newSchema);
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any -- - FIXME: disabled temporarily on migrate to TS.
export function removeObsAnnoCategory(schema: any, name: any, category: any) {
export function removeObsAnnoCategory(
schema: Schema,
name: string,
category: string
): Schema {
/* remove a category from a categorical annotation */
const categories = schema.annotations.obsByName[name]?.categories;
if (!categories)
if (!categories) {
throw new Error("column does not exist or is not categorical");
}
const idx = categories.indexOf(category);
if (idx === -1) throw new Error("category does not exist");
const newSchema = _reindexObsAnno(_copyObsAnno(schema));
/* remove category. Do not need to resort as this can't change presentation order */
newSchema.annotations.obsByName[name].categories.splice(idx, 1);
newSchema.annotations.obsByName[name].categories?.splice(idx, 1);
return newSchema;
}
@@ -129,26 +133,27 @@ export function addObsAnnoCategory(schema: any, name: any, category: any) {
/* add category, retaining presentation sort order */
const catAnno = newSchema.annotations.obsByName[name];
catAnno.categories = catLabelSort(catAnno.writable, [
...catAnno.categories,
...(catAnno.categories || []),
category,
]);
return newSchema;
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any -- - FIXME: disabled temporarily on migrate to TS.
export function addObsLayout(schema: any, layout: any) {
export function addObsLayout(schema: Schema, layout: LayoutColumn): Schema {
/* add or replace a layout */
const newSchema = _copyObsLayout(schema);
newSchema.layout.obs.push(layout);
return _reindexObsLayout(newSchema);
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any -- - FIXME: disabled temporarily on migrate to TS.
export function removeObsLayout(schema: any, name: any) {
export function removeObsLayout(schema: Schema, name: string): Schema {
/* remove a layout */
const newSchema = _copyObsLayout(schema);
// eslint-disable-next-line @typescript-eslint/no-explicit-any --- FIXME: disabled temporarily on migrate to TS.
newSchema.layout.obs = schema.layout.obs.filter((v: any) => v.name !== name);
newSchema.layout.obs = schema.layout.obs.filter((v) => v.name !== name);
return _reindexObsLayout(newSchema);
}