Skip to content

Commit

Permalink
Replace references to hard-coded List field types with references t…
Browse files Browse the repository at this point in the history
…o the `List` class hierarchy.

PiperOrigin-RevId: 463639695
  • Loading branch information
cjqian authored and LIT team committed Jul 27, 2022
1 parent 8c6ac11 commit 2522e4f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 31 deletions.
18 changes: 9 additions & 9 deletions lit_nlp/api/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,13 @@ class GeneratedText(TextSegment):


@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class _List(LitType):
class ListLitType(LitType):
"""List type."""
default: Sequence[Any] = None


@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class _StringCandidateList(_List):
class _StringCandidateList(ListLitType):
"""A list of (text, score) tuples."""
default: ScoredTextCandidates = None

Expand Down Expand Up @@ -227,7 +227,7 @@ class SearchQuery(TextSegment):


@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class _StringList(_List):
class _StringList(ListLitType):
"""A list of strings."""
default: Sequence[Text] = []

Expand All @@ -244,7 +244,7 @@ class Tokens(_StringList):


@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class TokenTopKPreds(_List):
class TokenTopKPreds(ListLitType):
"""Predicted tokens, as from a language model.
The inner list should contain (word, probability) in descending order.
Expand Down Expand Up @@ -272,7 +272,7 @@ class RegressionScore(Scalar):


@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class ReferenceScores(_List):
class ReferenceScores(ListLitType):
"""Score of one or more target sequences."""
default: Sequence[float] = None

Expand Down Expand Up @@ -319,7 +319,7 @@ class SequenceTags(_StringList):


@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class SpanLabels(_List):
class SpanLabels(ListLitType):
"""Span labels aligned to tokens.
Span labels can cover more than one token, may not cover all tokens in the
Expand All @@ -332,7 +332,7 @@ class SpanLabels(_List):


@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class EdgeLabels(_List):
class EdgeLabels(ListLitType):
"""Edge labels between pairs of spans.
This is a general form for structured prediction output; each entry consists
Expand All @@ -347,7 +347,7 @@ class EdgeLabels(_List):


@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class MultiSegmentAnnotations(_List):
class MultiSegmentAnnotations(ListLitType):
"""Very general type for in-line text annotations.
This is a more general version of SpanLabel, EdgeLabel, and other annotation
Expand Down Expand Up @@ -425,7 +425,7 @@ class AttentionHeads(_Tensor1D):


@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class SubwordOffsets(_List):
class SubwordOffsets(ListLitType):
"""Offsets to align input tokens to wordpieces or characters.
offsets[i] should be the index of the first wordpiece for input token i.
Expand Down
21 changes: 11 additions & 10 deletions lit_nlp/client/lib/lit_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,17 @@ export class GeneratedText extends TextSegment {
}

/**
* A list type.
* A list type. Named `ListListType` to avoid conflicts with TypeScript List.
*/
class _List extends LitType {
@registered
export class ListLitType extends LitType {
override default: unknown[] = [];
}

/**
* A list of (text, score) tuples.
*/
class _StringCandidateList extends _List {
class _StringCandidateList extends ListLitType {
override default: ScoredTextCandidates = [];
}

Expand Down Expand Up @@ -172,7 +173,7 @@ export class SearchQuery extends TextSegment {
* A list of strings.
*/
@registered
export class StringList extends _List {
export class StringList extends ListLitType {
override default: string[] = [];
}

Expand All @@ -194,7 +195,7 @@ export class Tokens extends StringList {
* The inner list should contain (word, probability) in descending order.
*/
@registered
export class TokenTopKPreds extends _List {
export class TokenTopKPreds extends ListLitType {
override default: ScoredTextCandidates[] = [];
align?: string = undefined;
parent?: string = undefined;
Expand Down Expand Up @@ -223,7 +224,7 @@ export class RegressionScore extends Scalar {
* Score of one or more target sequences.
*/
@registered
export class ReferenceScores extends _List {
export class ReferenceScores extends ListLitType {
override default: number[] = [];

/** Name of a TextSegment or ReferenceTexts field in the input. */
Expand Down Expand Up @@ -289,7 +290,7 @@ export class SequenceTags extends StringList {
* sentence, and may overlap with each other.
*/
@registered
export class SpanLabels extends _List {
export class SpanLabels extends ListLitType {
/** Name of Tokens field. **/
align: string = '';
parent?: string = undefined;
Expand All @@ -302,7 +303,7 @@ export class SpanLabels extends _List {
* of (span1, span2, label).
*/
@registered
export class EdgeLabels extends _List {
export class EdgeLabels extends ListLitType {
/** Name of Tokens field. **/
align: string = '';
}
Expand All @@ -318,7 +319,7 @@ export class EdgeLabels extends _List {
* specific segment from the input.
*/
@registered
export class MultiSegmentAnnotations extends _List {
export class MultiSegmentAnnotations extends ListLitType {
/** If true, treat as candidate list. */
exclusive: boolean = false;
/** If true, don't emphasize in visualization. */
Expand Down Expand Up @@ -401,7 +402,7 @@ export class AttentionHeads extends _Tensor1D {
* offsets[i] should be the index of the first wordpiece for input token i.
*/
@registered
export class SubwordOffsets extends _List {
export class SubwordOffsets extends ListLitType {
override default: number[] = [];
/** Name of field in data spec. */
align_in: string = '';
Expand Down
8 changes: 2 additions & 6 deletions lit_nlp/client/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@
import * as d3 from 'd3';
import {TemplateResult} from 'lit';

import {LitName, LitType} from './lit_types';
import {ListLitType, LitName, LitType} from './lit_types';
import {chunkWords, isLitSubtype} from './utils';

// tslint:disable-next-line:no-any
export type D3Selection = d3.Selection<any, any, any, any>;

// TODO(b/162269499): Replace this with class-based lists.
export const listFieldTypes: LitName[] =
['Tokens', 'SequenceTags', 'SpanLabels', 'EdgeLabels', 'SparseMultilabel'];

export interface Spec {
[key: string]: LitType;
}
Expand Down Expand Up @@ -310,7 +306,7 @@ export function defaultValueByField(key: string, spec: Spec) {
return 0;
}

if (isLitSubtype(fieldSpec, listFieldTypes)) {
if (fieldSpec instanceof ListLitType) {
return [];
}

Expand Down
1 change: 1 addition & 0 deletions lit_nlp/client/modules/data_table_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class DataTableModule extends LitModule {
get keys(): ColumnHeader[] {
const createColumnHeader = (name: string, type: LitType) => {
const header = {name, vocab: (type as LitTypeWithVocab).vocab};
// TODO(b/162269499): Rename Boolean to BooleanLitType.
if (isLitSubtype(type, 'Boolean')) {
header.vocab = ['✔', ' '];
}
Expand Down
5 changes: 2 additions & 3 deletions lit_nlp/client/modules/datapoint_editor_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {computed, observable, when} from 'mobx';

import {app} from '../core/app';
import {LitModule} from '../core/lit_module';
import {LitTypeWithVocab, SparseMultilabel, StringLitType} from '../lib/lit_types';
import {ListLitType, LitTypeWithVocab, SparseMultilabel, StringLitType} from '../lib/lit_types';
import {styles as sharedStyles} from '../lib/shared_styles.css';
import {AnnotationCluster, defaultValueByField, EdgeLabel, formatAnnotationCluster, formatEdgeLabel, formatSpanLabel, IndexedInput, Input, ModelInfoMap, SCROLL_SYNC_CSS_CLASS, SpanLabel, Spec} from '../lib/types';
import {findSpecKeys, isLitSubtype} from '../lib/utils';
Expand Down Expand Up @@ -87,8 +87,7 @@ export class DatapointEditorModule extends LitModule {

// Skip fields with value type string[]
const fieldSpec = this.appState.currentDatasetSpec[key];
// TODO(b/162269499) Replace this with list check.
const isListField = fieldSpec.__mro__.includes('_List');
const isListField = fieldSpec instanceof ListLitType;
if (isListField) continue;
dataTextKeys.push(key);
}
Expand Down
6 changes: 3 additions & 3 deletions lit_nlp/client/services/url_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import {autorun} from 'mobx';

import {defaultValueByField, IndexedInput, Input, listFieldTypes, ServiceUser, Spec} from '../lib/types';
import {isLitSubtype} from '../lib/utils';
import {ListLitType} from '../lib/lit_types';
import {defaultValueByField, IndexedInput, Input, ServiceUser, Spec} from '../lib/types';

import {LitService} from './lit_service';

Expand Down Expand Up @@ -139,7 +139,7 @@ export class UrlService extends LitService {
private parseDataFieldValue(fieldKey: string, encoded: string, spec: Spec) {
const fieldSpec = spec[fieldKey];
// If array type, unpack as an array.
if (isLitSubtype(fieldSpec, listFieldTypes)) {
if (fieldSpec instanceof ListLitType) {
return this.urlParseArray(encoded);
} else { // String-like.
return this.urlParseString(encoded) ??
Expand Down

0 comments on commit 2522e4f

Please sign in to comment.