Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NL: misc fulfillment fixes #2923

Merged
merged 1 commit into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion server/lib/nl/common/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def extend_svs(svs: List[str]):
if sv2 == sv:
continue
reverse_map[sv2] = res[sv]
res_ordered = {sv: sorted(ext_svs) for sv, ext_svs in res.items()}
res_ordered = {sv: sorted(set(ext_svs)) for sv, ext_svs in res.items()}
return res_ordered


Expand Down Expand Up @@ -284,6 +284,18 @@ def get_only_svs(svs: List[str]) -> List[str]:
"FertilityRate_",
"GrowthRate_",
"sdg/",
"FemaNaturalHazardRiskIndex_",
"FemaCommunityResilience_",
"FemaSocialVulnerability_",
"MothersAge_",
"IntervalSinceLastBirth_",
"BirthWeight_",
"Covid19MobilityTrend_",
"Average_",
"Cancer_Risk",
"IncrementalCount_",
"HouseholdSize_",
"LmpGestationalAge_",
]

_SV_FULL_DCID_NO_PC = ["Count_Person"]
Expand Down
22 changes: 14 additions & 8 deletions server/lib/nl/fulfillment/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ def populate_charts(state: PopulateState) -> bool:
# If user has not provided a place, seek a place from the context.
# Otherwise the result seems unexpected to them.
for pl in context.places_from_context(state.uttr):
if (populate_charts_for_places(state, [pl])):
# Important to set this for maybe_set_fallback().
state.uttr.places = [pl]
if (populate_charts_for_places(state, state.uttr.places)):
state.uttr.place_source = FulfillmentResult.PAST_QUERY
state.uttr.past_source_context = pl.name
return True
Expand Down Expand Up @@ -249,6 +251,7 @@ def _add_charts(state: PopulateState, places: List[Place],
tracker = MainExistenceCheckTracker(state, places_to_check, svs)
tracker.perform_existence_check()

existing_svs = set()
found = False
num_charts = 0
for exist_state in tracker.exist_sv_states:
Expand All @@ -267,6 +270,7 @@ def _add_charts(state: PopulateState, places: List[Place],
state.uttr.counters.err('failed_populate_callback_primary_event', 1)
else:
if chart_vars.svs:
existing_svs.update(chart_vars.svs)
if state.main_cb(state, chart_vars, places,
ChartOriginType.PRIMARY_CHART):
found = True
Expand All @@ -282,13 +286,15 @@ def _add_charts(state: PopulateState, places: List[Place],
# for those we would construct a single bar chart comparing the differe
# variables. For other query-types like map/ranking/scatter, we will have
# individual "related" charts, and those don't look good.
if (state.uttr.query_type == QueryType.SIMPLE and
_add_charts_for_extended_svs(state=state,
places=places,
places_to_check=places_to_check,
svs=svs,
num_charts=num_charts)):
found = True
if state.uttr.query_type == QueryType.SIMPLE and existing_svs:
# Note that we want to expand on existing_svs only, and in the
# order of `svs`
ordered_existing_svs = [v for v in svs if v in existing_svs]
found |= _add_charts_for_extended_svs(state=state,
places=places,
places_to_check=places_to_check,
svs=ordered_existing_svs,
num_charts=num_charts)

if not found:
# Always clear fallback when returning False
Expand Down
2 changes: 2 additions & 0 deletions server/lib/nl/fulfillment/correlation.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def _populate_correlation_for_place_type(state: PopulateState) -> bool:
else:
state.uttr.counters.err('correlation_failed_populate_main_place', pl.dcid)
for pl in places_from_context(state.uttr):
# Important to set this for maybe_set_fallback().
state.uttr.places = [pl]
if (_populate_correlation_for_place(state, pl)):
state.uttr.place_source = FulfillmentResult.PAST_QUERY
state.uttr.past_source_context = pl.name
Expand Down
2 changes: 2 additions & 0 deletions server/lib/nl/fulfillment/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def _populate_event(state: base.PopulateState,
state.uttr.counters.err('event_failed_populate_main_place', pl.dcid)
if not state.uttr.places:
for pl in ctx.places_from_context(state.uttr):
# Important to set this for maybe_set_fallback().
state.uttr.places = [pl]
if (_populate_event_for_place(state, event_types, pl)):
state.uttr.place_source = nl_uttr.FulfillmentResult.PAST_QUERY
state.uttr.past_source_context = pl.name
Expand Down
35 changes: 30 additions & 5 deletions server/tests/lib/nl/test_utterance.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,12 @@
'contained_in_place_type': 'County',
'type': ClassificationType.CONTAINED_IN
}],
'places': [],
'places': [{
'country': 'country/USA',
'dcid': 'geoId/06',
'name': 'Foo Place',
'place_type': 'State'
}],
'llm_resp': {},
'placeFallback': {},
'query': 'foo sv in place',
Expand Down Expand Up @@ -441,7 +446,12 @@
'classifications': [{
'type': ClassificationType.CORRELATION
}],
'places': [],
'places': [{
'country': 'country/USA',
'dcid': 'geoId/06',
'name': 'Foo Place',
'place_type': 'State'
}],
'llm_resp': {},
'placeFallback': {},
'query': 'foo sv in place',
Expand Down Expand Up @@ -544,7 +554,12 @@
'contained_in_place_type': 'County',
'type': QueryType.CONTAINED_IN,
}],
'places': [],
'places': [{
'country': 'country/USA',
'dcid': 'geoId/06',
'name': 'Foo Place',
'place_type': 'State'
}],
'llm_resp': {},
'placeFallback': {},
'query': 'foo sv in place',
Expand Down Expand Up @@ -581,7 +596,12 @@
'ranking_type': [RankingType.HIGH],
'type': ClassificationType.RANKING
}],
'places': [],
'places': [{
'country': 'country/USA',
'dcid': 'geoId/06',
'name': 'Foo Place',
'place_type': 'State'
}],
'llm_resp': {},
'placeFallback': {},
'query': 'foo sv in place',
Expand Down Expand Up @@ -622,7 +642,12 @@
'time_delta_type': [TimeDeltaType.INCREASE],
'type': ClassificationType.TIME_DELTA
}],
'places': [],
'places': [{
'country': 'country/USA',
'dcid': 'geoId/06',
'name': 'Foo Place',
'place_type': 'State'
}],
'llm_resp': {},
'placeFallback': {},
'query': 'foo sv in place',
Expand Down
31 changes: 29 additions & 2 deletions static/js/apps/nl_interface/debug_info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { Col, Row } from "reactstrap";
import {
DebugInfo,
MultiSVCandidate,
SearchResult,
SVScores,
} from "../../types/app/nl_interface_types";

Expand Down Expand Up @@ -157,7 +158,7 @@ const multiVarScoresElement = (svScores: SVScores): JSX.Element => {

export interface DebugInfoProps {
debugData: any; // from the server response
pageConfig: any;
chartsData: SearchResult;
}

export function DebugInfo(props: DebugInfoProps): JSX.Element {
Expand Down Expand Up @@ -320,6 +321,26 @@ export function DebugInfo(props: DebugInfoProps): JSX.Element {
</Row>
<Row>
<Col>
Place Query Source: {props.chartsData.placeSource}
{props.chartsData.pastSourceContext
? "(" + props.chartsData.pastSourceContext + ")"
: ""}
</Col>
</Row>
<Row>
<Col>Variable Query Source: {props.chartsData.svSource}</Col>
</Row>
{props.chartsData.placeFallback && (
<Row>
<Col>
Place Fallback: &quot;{props.chartsData.placeFallback.origStr}
&quot; to &quot;{props.chartsData.placeFallback.newStr}&quot;
</Col>
</Row>
)}
<Row>
<Col>
<b>Counters:</b>
<pre>{JSON.stringify(debugInfo.counters, null, 2)}</pre>
</Col>
</Row>
Expand All @@ -328,7 +349,13 @@ export function DebugInfo(props: DebugInfoProps): JSX.Element {
</Row>
<Row>
<Col>
<pre>{JSON.stringify(props.pageConfig, null, 2)}</pre>
<pre>
{JSON.stringify(
props.chartsData ? props.chartsData.config : null,
null,
2
)}
</pre>
</Col>
</Row>
</div>
Expand Down
84 changes: 55 additions & 29 deletions static/js/apps/nl_interface/query_result.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,39 +174,65 @@ export const QueryResult = memo(function QueryResult(
{debugData && (
<DebugInfo
debugData={debugData}
pageConfig={chartsData ? chartsData.config : null}
chartsData={chartsData}
></DebugInfo>
)}
{chartsData &&
!chartsData.placeFallback &&
(chartsData.placeSource === "PAST_QUERY" ||
chartsData.placeSource === "PAST_QUERY") && (
<div className="nl-query-info">
Could not recognize any{" "}
{chartsData.placeSource !== "PAST_QUERY" && <span>topic</span>}
{chartsData.svSource !== "PAST_QUERY" && <span>place</span>}
{chartsData.svSource === "PAST_QUERY" &&
chartsData.placeSource === "PAST_QUERY" && (
<span>place or topic</span>
)}{" "}
in this query, so here are relevant statistics for{" "}
{chartsData.pastSourceContext} based on what you previously
asked.
</div>
)}
{chartsData &&
!chartsData.placeFallback &&
chartsData.svSource === "UNRECOGNIZED" && (
<div className="nl-query-info">
Could not recognize any topic from the query, but below are
topic categories with statistics for {chartsData.place.name}{" "}
that you could explore further.
</div>
)}
{chartsData &&
!chartsData.placeFallback &&
chartsData.svSource === "UNFULFILLED" && (
<div className="nl-query-info">
Sorry, there were no relevant statistics about the topic for{" "}
{chartsData.place.name}. Below are topic categories with
statistics for {chartsData.place.name} that you could explore
further.
</div>
)}
{chartsData && chartsData.placeFallback && (
<div className="nl-query-info">
Sorry, there was no relevant statistics for &quot;
{chartsData.placeFallback.origStr}&quot;. &nbsp; Here are results
about &quot;{chartsData.placeFallback.newStr}&quot; instead.
</div>
)}
{chartsData && chartsData.placeSource === "PAST_QUERY" && (
<div className="nl-query-info">
Could not recognize any place in this query, so using{" "}
{chartsData.pastSourceContext} from a prior query in this session.
</div>
)}
{chartsData && chartsData.svSource === "PAST_QUERY" && (
<div className="nl-query-info">
Could not recognize any topic in this query, so using a topic you
previously asked about in this session.
</div>
)}
{chartsData && chartsData.svSource === "UNRECOGNIZED" && (
<div className="nl-query-info">
Could not recognize any topic from the query. Below are some topic
categories with statistics for {chartsData.place.name}.
</div>
)}
{chartsData && chartsData.svSource === "UNFULFILLED" && (
<div className="nl-query-info">
Sorry, there was no relevant statistics about the topic for{" "}
{chartsData.place.name}. Below are some topic categories with
data.
{chartsData.placeSource !== "PAST_QUERY" &&
chartsData.svSource !== "PAST_QUERY" && (
<span>
Sorry, there were no relevant statistics on this topic for
&quot;{chartsData.placeFallback.origStr}&quot;. &nbsp; Here
are results for &quot;{chartsData.placeFallback.newStr}
&quot; instead.
</span>
)}
{(chartsData.placeSource === "PAST_QUERY" ||
chartsData.svSource === "PAST_QUERY") && (
<span>
Tried looking up relevant statistics for &quot;
{chartsData.placeFallback.origStr}&quot; based on your prior
queries, but found no results. &nbsp; Here are results for
&quot;{chartsData.placeFallback.newStr}&quot; instead.
</span>
)}
</div>
)}
{chartsData && chartsData.config && (
Expand Down