Skip to content

Commit

Permalink
Support both absolute and relative rankings for time-delta queries (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pradh authored Feb 25, 2023
1 parent b89e76b commit 0b0b3a4
Show file tree
Hide file tree
Showing 9 changed files with 873 additions and 92 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,120 @@
"categories": [
{
"blocks": [
{
"columns": [
{
"tiles": [
{
"statVarKey": [
"Count_Worker_NAICSHealthCareSocialAssistance"
],
"title": "Health Care and Social Assistance Industry in Placer County",
"type": "LINE"
},
{
"statVarKey": [
"dc/p69tpsldf99h7"
],
"title": "Retail Trade in Placer County",
"type": "LINE"
},
{
"statVarKey": [
"Count_Worker_NAICSConstruction"
],
"title": "Construction Industry in Placer County",
"type": "LINE"
},
{
"statVarKey": [
"Count_Worker_NAICSAccommodationFoodServices"
],
"title": "Accommodation and Food Services Industry in Placer County",
"type": "LINE"
},
{
"statVarKey": [
"Count_Worker_NAICSAdministrativeSupportWasteManagementRemediationServices"
],
"title": "Administrative and Support and Waste Management Services Industry in Placer County",
"type": "LINE"
},
{
"statVarKey": [
"Count_Worker_NAICSProfessionalScientificTechnicalServices"
],
"title": "Professional, Scientific, and Technical Services in Placer County",
"type": "LINE"
},
{
"statVarKey": [
"Count_Worker_NAICSEducationalServices"
],
"title": "Educational Services Industry in Placer County",
"type": "LINE"
},
{
"statVarKey": [
"Count_Worker_NAICSFinanceInsurance"
],
"title": "Finance and Insurance Industry in Placer County",
"type": "LINE"
},
{
"statVarKey": [
"Count_Worker_NAICSOtherServices"
],
"title": "Other Services, Except Public Administration in Placer County",
"type": "LINE"
},
{
"statVarKey": [
"Count_Worker_NAICSArtsEntertainmentRecreation"
],
"title": "Arts, Entertainment, and Recreation Industry in Placer County",
"type": "LINE"
},
{
"statVarKey": [
"Count_Worker_NAICSRealEstateRentalLeasing"
],
"title": "Real Estate and Rental and Leasing in Placer County",
"type": "LINE"
},
{
"statVarKey": [
"Count_Worker_NAICSWholesaleTrade"
],
"title": "Wholesale Trade in Placer County",
"type": "LINE"
},
{
"statVarKey": [
"Count_Worker_NAICSPublicAdministration"
],
"title": "Public Administration in Placer County",
"type": "LINE"
},
{
"statVarKey": [
"Count_Worker_NAICSInformation"
],
"title": "Information Industry in Placer County",
"type": "LINE"
},
{
"statVarKey": [
"Count_Worker_NAICSMiningQuarryingOilGasExtraction"
],
"title": "Mining, Quarrying, and Oil and Gas Extraction Industry in Placer County",
"type": "LINE"
}
]
}
],
"title": "Increase over time (by absolute change)"
},
{
"columns": [
{
Expand Down Expand Up @@ -115,7 +229,7 @@
]
}
],
"title": "Categories of Jobs"
"title": "Increase over time (by percent change)"
}
],
"statVarSpec": {
Expand Down
47 changes: 30 additions & 17 deletions server/lib/nl/fulfillment/time_delta_across_places.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from server.lib.nl import utils
from server.lib.nl.detection import Place
from server.lib.nl.detection import TimeDeltaType
from server.lib.nl.fulfillment.base import add_chart_to_utterance
from server.lib.nl.fulfillment.base import ChartVars
from server.lib.nl.fulfillment.base import populate_charts
Expand Down Expand Up @@ -88,26 +89,38 @@ def _populate_cb(state: PopulateState, chart_vars: ChartVars,
dcid2place = {c.dcid: c for c in child_places}
dcids = list(dcid2place.keys())

ranked_child_dcids = utils.rank_places_by_growth_rate(
direction = state.time_delta_types[0]
ranked_children = utils.rank_places_by_series_growth(
places=dcids,
sv=chart_vars.svs[0],
growth_direction=state.time_delta_types[0],
growth_direction=direction,
rank_order=rank_order)

utils.update_counter(state.uttr.counters, 'time-delta_reranked_places', {
'orig': dcids,
'ranked': ranked_child_dcids,
})
ranked_child_places = []
for d in ranked_child_dcids:
ranked_child_places.append(dcid2place[d])
utils.update_counter(
state.uttr.counters, 'time-delta_reranked_places', {
'orig': dcids,
'ranked_abs': ranked_children.abs,
'ranked_pct': ranked_children.pct,
})
block_id = chart_vars.block_id
i = 0
for ranked_dcids in [ranked_children.abs, ranked_children.pct]:
ranked_places = []
for d in ranked_dcids:
ranked_places.append(dcid2place[d])

# No per-capita charts.
chart_vars.include_percapita = False
# Override the "main-place" (i.e., parent) with the child place.
chart_vars.set_place_override_for_line = True
for p in ranked_child_places[:_MAX_PLACES_TO_RETURN]:
logging.info('Processing %s' % p.name)
found |= add_chart_to_utterance(ChartType.TIMELINE_CHART, state, chart_vars,
[p], chart_origin)
# No per-capita charts.
chart_vars.include_percapita = False
chart_vars.block_id = block_id
# Override the "main-place" (i.e., parent) with the child place.
chart_vars.set_place_override_for_line = True
chart_vars.title = utils.get_time_delta_title(
direction=direction, is_absolute=True if i == 0 else False)
for p in ranked_places[:_MAX_PLACES_TO_RETURN]:
found |= add_chart_to_utterance(ChartType.TIMELINE_CHART, state,
chart_vars, [p], chart_origin)
# Avoid having the second set of charts use the same block_id than
# others.
block_id += 10
i += 1
return found
47 changes: 30 additions & 17 deletions server/lib/nl/fulfillment/time_delta_across_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,34 @@ def _populate_cb(state: PopulateState, chart_vars: ChartVars,
# Compute time-delta ranks.
rank_order = state.ranking_types[0] if state.ranking_types else None
logging.info('Attempting to compute growth rate stats')
ranked_svs = utils.rank_svs_by_growth_rate(
place=places[0].dcid,
svs=chart_vars.svs,
growth_direction=state.time_delta_types[0],
rank_order=rank_order)
utils.update_counter(state.uttr.counters,
'time-delta-across-vars_reranked_svs', {
'orig': chart_vars.svs,
'ranked': ranked_svs,
})
for sv in ranked_svs:
cv = chart_vars
cv.svs = [sv]
cv.response_type = "growth chart"
# TODO: desc string should take into account rank order
found |= add_chart_to_utterance(ChartType.TIMELINE_CHART, state, cv, places,
chart_origin)

direction = state.time_delta_types[0]
ranked_lists = utils.rank_svs_by_series_growth(place=places[0].dcid,
svs=chart_vars.svs,
growth_direction=direction,
rank_order=rank_order)

utils.update_counter(
state.uttr.counters, 'time-delta-across-vars_reranked_svs', {
'orig': chart_vars.svs,
'ranked_abs': ranked_lists.abs,
'ranked_pct': ranked_lists.pct,
})

block_id = chart_vars.block_id
i = 0
for ranked_svs in [ranked_lists.abs, ranked_lists.pct]:
for sv in ranked_svs:
cv = chart_vars
cv.svs = [sv]
cv.block_id = block_id
cv.title = utils.get_time_delta_title(
direction=direction, is_absolute=True if i == 0 else False)
found |= add_chart_to_utterance(ChartType.TIMELINE_CHART, state, cv,
places, chart_origin)
# Avoid having the second set of charts use the same block_id than
# others.
block_id += 10
i += 1

return found
Loading

0 comments on commit 0b0b3a4

Please sign in to comment.