-
Notifications
You must be signed in to change notification settings - Fork 14.4k
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
feat: add rolling window support to 'Big Number with Trendline' viz #9107
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,31 +97,32 @@ def load_world_bank_health_n_pop( | |
db.session.commit() | ||
tbl.fetch_metadata() | ||
|
||
metric = "sum__SP_POP_TOTL" | ||
metrics = ["sum__SP_POP_TOTL"] | ||
secondary_metric = { | ||
"aggregate": "SUM", | ||
"column": { | ||
"column_name": "SP_RUR_TOTL", | ||
"optionName": "_col_SP_RUR_TOTL", | ||
"type": "DOUBLE", | ||
}, | ||
"expressionType": "SIMPLE", | ||
"hasCustomLabel": True, | ||
"label": "Rural Population", | ||
} | ||
Comment on lines
+102
to
+112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For readability later on in the code, calling this |
||
|
||
defaults = { | ||
"compare_lag": "10", | ||
"compare_suffix": "o10Y", | ||
"limit": "25", | ||
"granularity_sqla": "year", | ||
"groupby": [], | ||
"metric": "sum__SP_POP_TOTL", | ||
"metrics": ["sum__SP_POP_TOTL"], | ||
"row_limit": config["ROW_LIMIT"], | ||
"since": "2014-01-01", | ||
"until": "2014-01-02", | ||
"time_range": "2014-01-01 : 2014-01-02", | ||
"markup_type": "markdown", | ||
"country_fieldtype": "cca3", | ||
"secondary_metric": { | ||
"aggregate": "SUM", | ||
"column": { | ||
"column_name": "SP_RUR_TOTL", | ||
"optionName": "_col_SP_RUR_TOTL", | ||
"type": "DOUBLE", | ||
}, | ||
"expressionType": "SIMPLE", | ||
"hasCustomLabel": True, | ||
"label": "Rural Population", | ||
}, | ||
"entity": "country_code", | ||
"show_bubbles": True, | ||
} | ||
|
@@ -207,6 +208,7 @@ def load_world_bank_health_n_pop( | |
viz_type="world_map", | ||
metric="sum__SP_RUR_TOTL_ZS", | ||
num_period_compare="10", | ||
secondary_metric=secondary_metric, | ||
), | ||
), | ||
Slice( | ||
|
@@ -264,6 +266,8 @@ def load_world_bank_health_n_pop( | |
groupby=["region", "country_name"], | ||
since="2011-01-01", | ||
until="2011-01-01", | ||
metric=metric, | ||
secondary_metric=secondary_metric, | ||
), | ||
), | ||
Slice( | ||
|
@@ -277,6 +281,7 @@ def load_world_bank_health_n_pop( | |
until="now", | ||
viz_type="area", | ||
groupby=["region"], | ||
metrics=metrics, | ||
), | ||
), | ||
Slice( | ||
|
@@ -292,6 +297,7 @@ def load_world_bank_health_n_pop( | |
x_ticks_layout="staggered", | ||
viz_type="box_plot", | ||
groupby=["region"], | ||
metrics=metrics, | ||
), | ||
), | ||
Slice( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -178,6 +178,26 @@ def run_extra_queries(self): | |
""" | ||
pass | ||
|
||
def apply_rolling(self, df): | ||
fd = self.form_data | ||
rolling_type = fd.get("rolling_type") | ||
rolling_periods = int(fd.get("rolling_periods") or 0) | ||
min_periods = int(fd.get("min_periods") or 0) | ||
|
||
if rolling_type in ("mean", "std", "sum") and rolling_periods: | ||
kwargs = dict(window=rolling_periods, min_periods=min_periods) | ||
if rolling_type == "mean": | ||
df = df.rolling(**kwargs).mean() | ||
elif rolling_type == "std": | ||
df = df.rolling(**kwargs).std() | ||
elif rolling_type == "sum": | ||
df = df.rolling(**kwargs).sum() | ||
elif rolling_type == "cumsum": | ||
df = df.cumsum() | ||
if min_periods: | ||
df = df[min_periods:] | ||
return df | ||
Comment on lines
+181
to
+199
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
def get_samples(self): | ||
query_obj = self.query_obj() | ||
query_obj.update( | ||
|
@@ -1101,6 +1121,18 @@ def query_obj(self): | |
self.form_data["metric"] = metric | ||
return d | ||
|
||
def get_data(self, df: pd.DataFrame) -> VizData: | ||
df = df.pivot_table( | ||
index=DTTM_ALIAS, | ||
columns=[], | ||
values=self.metric_labels, | ||
fill_value=0, | ||
aggfunc=sum, | ||
) | ||
df = self.apply_rolling(df) | ||
df[DTTM_ALIAS] = df.index | ||
return super().get_data(df) | ||
|
||
|
||
class BigNumberTotalViz(BaseViz): | ||
|
||
|
@@ -1225,23 +1257,7 @@ def process_data(self, df: pd.DataFrame, aggregate: bool = False) -> VizData: | |
dfs.sort_values(ascending=False, inplace=True) | ||
df = df[dfs.index] | ||
|
||
rolling_type = fd.get("rolling_type") | ||
rolling_periods = int(fd.get("rolling_periods") or 0) | ||
min_periods = int(fd.get("min_periods") or 0) | ||
|
||
if rolling_type in ("mean", "std", "sum") and rolling_periods: | ||
kwargs = dict(window=rolling_periods, min_periods=min_periods) | ||
if rolling_type == "mean": | ||
df = df.rolling(**kwargs).mean() | ||
elif rolling_type == "std": | ||
df = df.rolling(**kwargs).std() | ||
elif rolling_type == "sum": | ||
df = df.rolling(**kwargs).sum() | ||
elif rolling_type == "cumsum": | ||
df = df.cumsum() | ||
if min_periods: | ||
df = df[min_periods:] | ||
|
||
df = self.apply_rolling(df) | ||
if fd.get("contribution"): | ||
dft = df.T | ||
df = (dft / dft.sum()).T | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is more a general comment relating to the current structure of examples, not specifically this PR. But given the fact that
metrics
is already defined above (line 80), it might be a good idea to disambiguate here. For exampledefault_metrics
,total_population_metrics
or similar.Another option, which I personally would prefer, would be to remove the legacy metrics above, and replace both
metric
andmetrics
here with adhoc metrics. Something likeand later on where a
metrics
list is expected, just passing a[total_population_metric]
to highlight that it's a single value list.