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

Fix Histogram Binning #2086

Merged
merged 30 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a3112bc
Fix binning and remove empty charts
juliawu Jan 23, 2023
3fcf21d
Merge branch 'master' into hist-fix
juliawu Jan 23, 2023
50e32f1
Merge branch 'master' of https://github.com/datacommonsorg/website in…
juliawu Jan 23, 2023
a35074e
lint
juliawu Jan 23, 2023
d03cdf9
remove debugging statements
juliawu Jan 23, 2023
6e47972
Merge branch 'master' of https://github.com/datacommonsorg/website in…
juliawu Jan 27, 2023
441fe20
remove magic numbers
juliawu Jan 27, 2023
69a5989
Merge branch 'master' of https://github.com/datacommonsorg/website in…
juliawu Feb 1, 2023
c69d13a
Merge branch 'master' of https://github.com/datacommonsorg/website in…
juliawu Feb 7, 2023
de8dbb6
response to reviewer comments pt 1
juliawu Feb 7, 2023
79e56b2
move histogram to disaster event block
juliawu Feb 8, 2023
483db7f
fix histogram chart titles
juliawu Feb 8, 2023
e4a9c10
cleanup props
juliawu Feb 8, 2023
acb6bd5
lint
juliawu Feb 8, 2023
cdf3ec5
Merge branch 'master' of https://github.com/datacommonsorg/website in…
juliawu Feb 8, 2023
8a9088b
fix series.py merge errors
juliawu Feb 8, 2023
a295bb3
update allowed tile types test
juliawu Feb 9, 2023
60610e5
fix key error and lint
juliawu Feb 9, 2023
2dfef78
fix drought histogram title
juliawu Feb 9, 2023
5d41646
Merge branch 'master' of https://github.com/datacommonsorg/website in…
juliawu Feb 9, 2023
27622ab
respond to reviewer comments
juliawu Feb 9, 2023
5883cb8
remove unneccessary ChartTileContainer prop
juliawu Feb 9, 2023
3749c99
remove unused import
juliawu Feb 9, 2023
73fa537
sort disasterEventPoints in getMonthsArrray()
juliawu Feb 9, 2023
7ec4df8
remove old 'need to specify P1Y' comments
juliawu Feb 9, 2023
b4f4cd9
fix spacing issue in subject_page.proto
juliawu Feb 9, 2023
9fee1f8
add YYYY-MM handling in getMonthsArray
juliawu Feb 10, 2023
01166ed
handle null case
juliawu Feb 10, 2023
3788d06
add check for monthly granularity and console log statement
juliawu Feb 10, 2023
92062b4
Merge branch 'master' of https://github.com/datacommonsorg/website in…
juliawu Feb 10, 2023
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
88 changes: 51 additions & 37 deletions server/config/disaster_dashboard/Earth.textproto
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,13 @@ categories {
event_type_keys: "drought"
}
}
# tiles {
# type: HISTOGRAM
# title: "SPI - 9 month"
# stat_var_key: "spi_9"
# }
tiles {
type: HISTOGRAM
title: "Number of droughts"
histogram_tile_spec {
event_type_key: "drought"
}
}
}
columns {
tiles {
Expand Down Expand Up @@ -274,12 +276,14 @@ categories {
event_type_keys: "fire"
}
}
# tiles {
# # TODO: Need to specify P1Y
# type: HISTOGRAM
# title: "Number of fires last year"
# stat_var_key: "fire_count"
# }
tiles {
# TODO: Need to specify P1Y
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can delete this comment if it doesn't apply anymore (ditto for the rest)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

type: HISTOGRAM
title: "Number of fires"
histogram_tile_spec {
event_type_key: "fire"
}
}
# tiles {
# # TODO: Need to specify P1Y
# type: HISTOGRAM
Expand Down Expand Up @@ -331,12 +335,14 @@ categories {
event_type_keys: "flood"
}
}
# tiles {
# # TODO: Need to specify P1Y
# type: HISTOGRAM
# title: "Number of floods last year"
# stat_var_key: "flood_count"
# }
tiles {
# TODO: Need to specify P1Y
type: HISTOGRAM
title: "Number of floods"
histogram_tile_spec {
event_type_key: "flood"
}
}
# tiles {
# # TODO: Need to specify P1Y
# type: HISTOGRAM
Expand Down Expand Up @@ -388,11 +394,13 @@ categories {
event_type_keys: "storm"
}
}
# tiles {
# type: HISTOGRAM
# title: "Number of cyclones last year"
# stat_var_key: "cyclone_all"
# }
tiles {
type: HISTOGRAM
title: "Number of cyclones"
histogram_tile_spec {
event_type_key: "storm"
}
}
}
columns {
tiles {
Expand Down Expand Up @@ -431,11 +439,13 @@ categories {
event_type_keys: "wetbulb"
}
}
# tiles {
# type: HISTOGRAM
# title: "Number of wet bulb events last year"
# stat_var_key: "wetbulb_count"
# }
tiles {
type: HISTOGRAM
title: "Number of wet bulb events"
histogram_tile_spec {
event_type_key: "wetbulb"
}
}
# tiles {
# type: HISTOGRAM
# title: "Total area affected by wet bulb events last year"
Expand Down Expand Up @@ -484,11 +494,13 @@ categories {
event_type_keys: "heat"
}
}
# tiles {
# type: HISTOGRAM
# title: "Number of extreme heat events last year"
# stat_var_key: "heat_count"
# }
tiles {
type: HISTOGRAM
title: "Number of extreme heat events"
histogram_tile_spec {
event_type_key: "heat"
}
}
# tiles {
# type: HISTOGRAM
# title: "Total area affected by extreme heat events last year"
Expand Down Expand Up @@ -536,11 +548,13 @@ categories {
event_type_keys: "cold"
}
}
# tiles {
# type: HISTOGRAM
# title: "Number of extreme cold events last year"
# stat_var_key: "cold_count"
# }
tiles {
type: HISTOGRAM
title: "Number of extreme cold events"
histogram_tile_spec {
event_type_key: "cold"
}
}
# tiles {
# type: HISTOGRAM
# title: "Total area affected by extreme cold events last year"
Expand Down
7 changes: 6 additions & 1 deletion server/config/subject_page.proto
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ message DisasterEventMapTileSpec {
repeated string event_type_keys = 1;
}

message HistogramTileSpec {
string event_type_key = 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nit: please check indent spacing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good eye! Fixed spacing

}

message TopEventTileSpec {
// Key for event type specs defined in the PageMetadata that should be displayed
// Ranked by severity prop.
Expand Down Expand Up @@ -147,7 +151,7 @@ message Tile {
DESCRIPTION = 8;
// allowed in BlockTypes: TYPE_NONE
HISTOGRAM = 10;
// allowed in BlockTypes: TYPE_NONE
// allowed in BlockTypes: DISASTER_EVENT
PLACE_OVERVIEW = 11;
// allowed in BlockTypes: DISASTER_EVENT
TOP_EVENT = 12;
Expand All @@ -170,6 +174,7 @@ message Tile {
DisasterEventMapTileSpec disaster_event_map_tile_spec = 6;
TopEventTileSpec top_event_tile_spec = 8;
ScatterTileSpec scatter_tile_spec = 9;
HistogramTileSpec histogram_tile_spec = 10;
}
}

Expand Down
44 changes: 23 additions & 21 deletions server/config/subject_page_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 0 additions & 78 deletions server/routes/api/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,58 +22,6 @@
bp = Blueprint("series", __name__, url_prefix='/api/observations/series')


# TODO(juliawu): Handle case where dates are not "YYYY-MM"
# TODO(juliawu): We should adjust how we bin based on the data,
# instead of cutting it off.
def get_binned_series(entities, variables, year):
"""Get observation series for entities and variables, for a given year.

Bins observations from a series for plotting in the histogram tile.
Currently, binning is done by only returning observations for a given year.
This is done by assuming the dates of the series are in 'YYYY-MM' format,
filtering for observations from the given year, and then imputing 0 for
any months missing, up to the end of the series.

Note: This assumes the dates of the series are in 'YYYY-MM' format.

Args:
entities (list): DCIDs of entities to query
variables (list): DCIDs of variables to query
year (str): year in "YYYY" format to get observations for

Returns:
JSON response from server, with series containing only observations from
the specified year.
"""
# Get raw series from mixer
data = util.series_core(entities, variables, False)

for stat_var in variables:
for location in data['data'][stat_var].keys():

# filter series to just observations from the selected year
series = data['data'][stat_var][location]['series']
pruned_series = []
dates_with_data = []
for obs in series:
if obs['date'][:4] == year:
pruned_series.append(obs)
dates_with_data.append(obs['date'])

if len(pruned_series) > 0:
# fill in missing periods with 0s, from January to end of series
last_month = int(dates_with_data[-1][-2:])
months_to_fill = [str(mm).zfill(2) for mm in range(1, last_month + 1)]
for month in months_to_fill:
date = f"{year}-{month}"
if date not in dates_with_data:
pruned_series.append({'date': date, 'value': 0})
pruned_series.sort(key=lambda x: x['date'])

data['data'][stat_var][location]['series'] = pruned_series
return data


@bp.route('', strict_slashes=False)
@cache.cached(timeout=3600 * 24, query_string=True)
def series():
Expand Down Expand Up @@ -105,7 +53,6 @@ def series_all():
def series_within():
"""Gets the observation for child entities of a certain place
type contained in a parent entity at a given date.

Note: the perferred facet is returned.
"""
parent_entity = request.args.get('parent_entity')
Expand All @@ -125,7 +72,6 @@ def series_within():
def series_within_all():
"""Gets the observation for child entities of a certain place
type contained in a parent entity at a given date.

Note: all the facets are returned.
"""
parent_entity = request.args.get('parent_entity')
Expand All @@ -138,27 +84,3 @@ def series_within_all():
if not variables:
return 'error: must provide a `variables` field', 400
return util.series_within_core(parent_entity, child_type, variables, True)


@bp.route('/binned')
@bp.route('/binned/<path:year>')
def series_binned(year='2022'):
"""Get observations binned by time-period.

Used for pre-binning data for the histogram tile. Currently only "bins" data
by returning only observations from a specific year.

Args:
year: the year to get observations for

Returns:
JSON response from server, with series containing only observations from
the specified year.
"""
entities = list(filter(lambda x: x != "", request.args.getlist('entities')))
variables = list(filter(lambda x: x != "", request.args.getlist('variables')))
if not entities:
return 'error: must provide a `entities` field', 400
if not variables:
return 'error: must provide a `variables` field', 400
return get_binned_series(entities, variables, year)
Loading