Skip to content

Commit

Permalink
Consolidate implementations of createLitTypes and migrate it into t…
Browse files Browse the repository at this point in the history
…he `utils` module.

PiperOrigin-RevId: 463679897
  • Loading branch information
cjqian authored and LIT team committed Jul 27, 2022
1 parent 0f8ff8e commit 58970de
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 222 deletions.
2 changes: 1 addition & 1 deletion lit_nlp/client/lib/generated_text_utils_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import 'jasmine';

import {canonicalizeGenerationResults, getAllOutputTexts, getAllReferenceTexts, getFlatTexts, getTextDiff} from './generated_text_utils';
import {LitType} from './lit_types';
import {createLitType} from './lit_types_utils';
import {Input, Preds, Spec} from './types';
import {createLitType} from './utils';

function textSegmentType(): LitType {
return createLitType('TextSegment', {
Expand Down
105 changes: 0 additions & 105 deletions lit_nlp/client/lib/lit_types_utils.ts

This file was deleted.

91 changes: 0 additions & 91 deletions lit_nlp/client/lib/lit_types_utils_test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion lit_nlp/client/lib/testing_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import 'jasmine';

import {createLitType} from './lit_types_utils';
import {LitMetadata} from './types';
import {createLitType} from './utils';

/**
* Cleans state between tests.
Expand Down
91 changes: 90 additions & 1 deletion lit_nlp/client/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@
* Shared helper functions used across the app.
*/

// For consistency with types.ts.
// tslint:disable: enforce-name-casing

import * as d3 from 'd3'; // Used for array helpers.

import {html, TemplateResult} from 'lit';
import {unsafeHTML} from 'lit/directives/unsafe-html.js';

import {marked} from 'marked';
import {LitName, LitType, LitTypeWithParent, MulticlassPreds, REGISTRY} from './lit_types';
import {FacetMap, ModelInfoMap, Spec} from './types';
import {FacetMap, LitMetadata, ModelInfoMap, Spec} from './types';

/** Calculates the mean for a list of numbers */
export function mean(values: number[]): number {
Expand Down Expand Up @@ -90,6 +93,89 @@ export function mapsContainSame<T>(mapA: Map<string, T>, mapB: Map<string, T>) {
return true;
}

/**
* Creates and returns a new LitType instance.
* @param typeName: The name of the desired LitType.
* @param constructorParams: A dictionary of properties to set on the LitType.
* For example, {'show_in_data_table': true}.
*/
export function createLitType(
typeName: LitName, constructorParams: {[key: string]: unknown} = {}) {
const litType = REGISTRY[typeName];
// tslint:disable-next-line:no-any
const newType = new (litType as any)();
newType.__name__ = typeName;

// Excluded properties are passed through in the Python serialization
// of LitTypes and can be ignored by the frontend.
const excluded = ['__mro__'];
for (const key in constructorParams) {
if (excluded.includes(key)) {
continue;
} else if (key in newType) {
newType[key] = constructorParams[key];
} else {
throw new Error(
`Attempted to set unrecognized property ${key} on ${newType}.`);
}
}

return newType;
}


interface SerializedSpec {
[key: string]: {__name__: string};
}

/**
* Converts serialized LitTypes within a Spec into LitType instances.
*/
export function deserializeLitTypesInSpec(serializedSpec: SerializedSpec): Spec {
const typedSpec: Spec = {};
for (const key of Object.keys(serializedSpec)) {
typedSpec[key] =
createLitType(serializedSpec[key].__name__, serializedSpec[key] as {});
}
return typedSpec;
}


/**
* Converts serialized LitTypes within the LitMetadata into LitType instances.
*/
export function deserializeLitTypesInLitMetadata(metadata: LitMetadata):
LitMetadata {
for (const model of Object.keys(metadata.models)) {
metadata.models[model].spec.input =
deserializeLitTypesInSpec(metadata.models[model].spec.input);
metadata.models[model].spec.output =
deserializeLitTypesInSpec(metadata.models[model].spec.output);
}

for (const dataset of Object.keys(metadata.datasets)) {
metadata.datasets[dataset].spec =
deserializeLitTypesInSpec(metadata.datasets[dataset].spec);
}

for (const generator of Object.keys(metadata.generators)) {
metadata.generators[generator].configSpec =
deserializeLitTypesInSpec(metadata.generators[generator].configSpec);
metadata.generators[generator].metaSpec =
deserializeLitTypesInSpec(metadata.generators[generator].metaSpec);
}

for (const interpreter of Object.keys(metadata.interpreters)) {
metadata.interpreters[interpreter].configSpec = deserializeLitTypesInSpec(
metadata.interpreters[interpreter].configSpec);
metadata.interpreters[interpreter].metaSpec =
deserializeLitTypesInSpec(metadata.interpreters[interpreter].metaSpec);
}

metadata.littypes = deserializeLitTypesInSpec(metadata.littypes);
return metadata;
}

/**
* Returns whether the litType is a subtype of any of the typesToFind.
* @param litType: The LitType to check.
Expand Down Expand Up @@ -122,6 +208,7 @@ export function isLitSubtype(
*/
export function findSpecKeys(
spec: Spec, typesToFind: LitName|LitName[]): string[] {
// TODO(b/240199145): Change this implementation to use classes.
return Object.keys(spec).filter(
key => isLitSubtype(spec[key], typesToFind));
}
Expand Down Expand Up @@ -349,6 +436,8 @@ export function copyToClipboard(value: string) {
tempInput.value = value;
document.body.appendChild(tempInput);
tempInput.select();
// TODO(b/240439975): Resolve deprecated execCommand.
// tslint:disable:deprecation
document.execCommand("copy");
document.body.removeChild(tempInput);
}
Expand Down

0 comments on commit 58970de

Please sign in to comment.