Skip to content

Commit

Permalink
Fix crash from Metrics and Curves on unlabeled data.
Browse files Browse the repository at this point in the history
- Metrics updated to check that parent field is in the dataset; each metric will
  return None if this is missing.
- Curves module checks if parent field is in the dataset; will hide entirely if
  this is missing.

PiperOrigin-RevId: 555362695
  • Loading branch information
iftenney authored and LIT team committed Aug 10, 2023
1 parent dde3006 commit 0f24400
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lit_nlp/api/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ def to_json(self) -> dtypes.JsonDict:
description=(
'The default LIT layout, which includes the data table and data point '
'editor, the performance and metrics, predictions, explanations, and '
'counterfactuals.'),
'counterfactuals.'
),
)

DEFAULT_LAYOUTS = {
Expand Down
7 changes: 4 additions & 3 deletions lit_nlp/client/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,10 @@ export function isBinaryClassification(litType: LitType) {
return false;
}

/** Returns if a LitType has a parent field. */
export function hasParent(litType: LitType) {
return (litType as LitTypeWithParent).parent != null;
/** Returns if a LitType has a parent field which is found in the (data) spec */
export function hasValidParent(litType: LitType, spec: Spec) {
const parent = (litType as LitTypeWithParent).parent;
return parent != null && spec[parent] != null;
}

/**
Expand Down
24 changes: 18 additions & 6 deletions lit_nlp/client/modules/curves_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@
// tslint:disable:no-new-decorators
import '../elements/expansion_panel';
import '../elements/line_chart';
import {customElement} from 'lit/decorators.js';

import {html, TemplateResult} from 'lit';
import {customElement} from 'lit/decorators.js';
import {action, computed, observable} from 'mobx';
import {FacetsChange} from '../core/faceting_control';

import {app} from '../core/app';
import {FacetsChange} from '../core/faceting_control';
import {LitModule} from '../core/lit_module';
import {MulticlassPreds} from '../lib/lit_types';
import {styles as sharedStyles} from '../lib/shared_styles.css';
import {GroupedExamples, IndexedInput, ModelInfoMap, SCROLL_SYNC_CSS_CLASS, Spec} from '../lib/types';
import {doesOutputSpecContain, findSpecKeys, hasParent} from '../lib/utils';
import {GroupService} from '../services/services';
import {findSpecKeys, hasValidParent} from '../lib/utils';
import {NumericFeatureBins} from '../services/group_service';
import {GroupService} from '../services/services';

import {styles} from './curves_module.css';
import {styles as sharedStyles} from '../lib/shared_styles.css';

// Response from backend curves interpreter.
interface CurvesResponse {
Expand Down Expand Up @@ -367,7 +370,16 @@ export class CurvesModule extends LitModule {

static override shouldDisplayModule(
modelSpecs: ModelInfoMap, datasetSpec: Spec) {
return doesOutputSpecContain(modelSpecs, MulticlassPreds, hasParent);
// We need a MulticlassPreds field, where parent is in the dataset spec.
for (const modelInfo of Object.values(modelSpecs)) {
const outputSpec = modelInfo.spec.output;
for (const outputFieldName of findSpecKeys(outputSpec, MulticlassPreds)) {
if (hasValidParent(outputSpec[outputFieldName], datasetSpec)) {
return true;
}
}
}
return false;
}
}

Expand Down
10 changes: 9 additions & 1 deletion lit_nlp/components/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,15 @@ def map_pred_keys(
logging.info("Skipping '%s': No parent provided.", pred_key)
continue

parent_spec: Optional[LitType] = data_spec.get(parent_key)
if parent_key not in data_spec:
logging.info(
"Skipping '%s': parent field '%s' not found in dataset.",
pred_key,
parent_key,
)
continue

parent_spec: LitType = data_spec[parent_key]
if predicate(pred_spec, parent_spec):
ret[pred_key] = parent_key
else:
Expand Down

0 comments on commit 0f24400

Please sign in to comment.