Skip to content

Commit

Permalink
chore: extract schema types (#2375)
Browse files Browse the repository at this point in the history
* chore: extract schema types

* address comments
  • Loading branch information
tihuan authored Aug 12, 2021
1 parent fc60b2a commit 59c475b
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 67 deletions.
2 changes: 1 addition & 1 deletion client/__tests__/util/annoMatrix/serverMocks/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function makeMockColumn(s: any, length: any) {
return new Array(length).fill(s.categories[0]);

default:
throw new Error("unkonwn type");
throw new Error("unknown type");
}
}

Expand Down
2 changes: 1 addition & 1 deletion client/__tests__/util/annoMatrix/serverMocks/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RawSchema } from "../../../../src/common/types/entities";
import { RawSchema } from "../../../../src/common/types/schema";

export const schema: { schema: RawSchema } = {
schema: {
Expand Down
2 changes: 1 addition & 1 deletion client/__tests__/util/stateManager/sampleResponses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import zip from "lodash.zip";
import _ from "lodash";
import { flatbuffers } from "flatbuffers";
import { NetEncoding } from "../../../src/util/stateManager/matrix_generated";
import { RawSchema } from "../../../src/common/types/entities";
import { RawSchema } from "../../../src/common/types/schema";

/*
test data mocking REST 0.2 API responses. Used in several tests.
Expand Down
59 changes: 1 addition & 58 deletions client/src/common/types/entities.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,3 @@
// If a globally shared type or interface doesn't have a clear owner, put it here

export type Category = number | string | boolean;

export interface AnnotationColumn {
categories?: Category[];
name: string;
type: "string" | "float32" | "int32" | "categorical" | "boolean";
writable: boolean;
}

interface DataFrame {
nObs: number;
nVar: number;
// TODO(thuang): Not sure what other types are available
type: "float32";
}

export interface LayoutColumn {
dims: string[];
name: string;
// TODO(thuang): Not sure what other types are available
type: "float32";
}
interface RawLayout {
obs: LayoutColumn[];
var?: LayoutColumn[];
}

interface RawAnnotations {
obs: {
columns: AnnotationColumn[];
index: string;
};
var: {
columns: AnnotationColumn[];
index: string;
};
}

export interface RawSchema {
annotations: RawAnnotations;
dataframe: DataFrame;
layout: RawLayout;
}

interface Annotations extends RawAnnotations {
obsByName: { [name: string]: AnnotationColumn };
varByName: { [name: string]: AnnotationColumn };
}

interface Layout extends RawLayout {
obsByName: { [name: string]: LayoutColumn };
varByName: { [name: string]: LayoutColumn };
}

export interface Schema extends RawSchema {
annotations: Annotations;
layout: Layout;
}
export {};
58 changes: 58 additions & 0 deletions client/src/common/types/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
type Category = number | string | boolean;

export interface AnnotationColumnSchema {
categories?: Category[];
name: string;
type: "string" | "float32" | "int32" | "categorical" | "boolean";
writable: boolean;
}

interface XMatrixSchema {
nObs: number;
nVar: number;
// TODO(thuang): Not sure what other types are available
type: "float32";
}

export interface EmbeddingSchema {
dims: string[];
name: string;
// TODO(thuang): Not sure what other types are available
type: "float32";
}
interface RawLayoutSchema {
obs: EmbeddingSchema[];
var?: EmbeddingSchema[];
}

interface RawAnnotationsSchema {
obs: {
columns: AnnotationColumnSchema[];
index: string;
};
var: {
columns: AnnotationColumnSchema[];
index: string;
};
}

export interface RawSchema {
annotations: RawAnnotationsSchema;
dataframe: XMatrixSchema;
layout: RawLayoutSchema;
}

interface AnnotationsSchema extends RawAnnotationsSchema {
obsByName: { [name: string]: AnnotationColumnSchema };
varByName: { [name: string]: AnnotationColumnSchema };
}

interface LayoutSchema extends RawLayoutSchema {
obsByName: { [name: string]: EmbeddingSchema };
varByName: { [name: string]: EmbeddingSchema };
}

export interface Schema extends RawSchema {
annotations: AnnotationsSchema;
layout: LayoutSchema;
}
2 changes: 1 addition & 1 deletion client/src/util/stateManager/annotationsHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Helper functions for user-editable annotations state management.
See also reducers/annotations.js
*/

import { Schema } from "../../common/types/entities";
import { Schema } from "../../common/types/schema";

/*
There are a number of state constraints assumed throughout the
Expand Down
10 changes: 5 additions & 5 deletions client/src/util/stateManager/schemaHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import catLabelSort from "../catLabelSort";
import {
RawSchema,
Schema,
LayoutColumn,
AnnotationColumn,
} from "../../common/types/entities";
EmbeddingSchema,
AnnotationColumnSchema,
} from "../../common/types/schema";

/*
System wide schema assumptions:
Expand Down Expand Up @@ -86,7 +86,7 @@ export function removeObsAnnoColumn(schema: Schema, name: string): Schema {
export function addObsAnnoColumn(
schema: Schema,
_: string,
defn: AnnotationColumn
defn: AnnotationColumnSchema
): Schema {
const newSchema = _copyObsAnno(schema);

Expand Down Expand Up @@ -142,7 +142,7 @@ export function addObsAnnoCategory(schema: any, name: any, category: any) {
return newSchema;
}

export function addObsLayout(schema: Schema, layout: LayoutColumn): Schema {
export function addObsLayout(schema: Schema, layout: EmbeddingSchema): Schema {
/* add or replace a layout */
const newSchema = _copyObsLayout(schema);
newSchema.layout.obs.push(layout);
Expand Down

0 comments on commit 59c475b

Please sign in to comment.