-
Notifications
You must be signed in to change notification settings - Fork 64
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
Support several variables #1056
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0cc26e5
Suport several variables
5b74752
Apply suggestions from simon
8032991
Fix import in test
5b929c2
Fix test
7d86bd7
Remove print from tests
a0feb03
Add sorting when comparing queries
997cb9e
Test one and two variables
bf05cd0
Fix notebooks
9d79f75
Fix PEP8 space in test
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ | |
|
||
_ENRICHMENT_ID = 'enrichment_id' | ||
_WORKING_PROJECT = 'carto-do-customers' | ||
_PUBLIC_PROJECT = 'carto-do-public-data' | ||
_PUBLIC_DATASET = 'open_data' | ||
|
||
|
||
def enrich(query_function, **kwargs): | ||
|
@@ -22,9 +24,9 @@ def enrich(query_function, **kwargs): | |
data_copy = _prepare_data(kwargs['data'], kwargs['data_geom_column']) | ||
tablename = _upload_dataframe(bq_client, user_dataset, data_copy, kwargs['data_geom_column']) | ||
|
||
query = _enrichment_query(user_dataset, tablename, query_function, **kwargs) | ||
queries = _enrichment_query(user_dataset, tablename, query_function, **kwargs) | ||
|
||
return _execute_enrichment(bq_client, query, data_copy, kwargs['data_geom_column']) | ||
return _execute_enrichment(bq_client, queries, data_copy, kwargs['data_geom_column']) | ||
|
||
|
||
def _get_credentials(credentials=None): | ||
|
@@ -55,17 +57,28 @@ def _upload_dataframe(bq_client, user_dataset, data_copy, data_geom_column): | |
|
||
|
||
def _enrichment_query(user_dataset, tablename, query_function, **kwargs): | ||
table_data_enrichment, table_geo_enrichment, variables_list = __get_tables_and_variables(kwargs['variables']) | ||
table_to_geotable, table_to_variables, table_to_project, table_to_dataset =\ | ||
__get_tables_and_variables(kwargs['variables'], user_dataset) | ||
|
||
filters_str = __process_filters(kwargs['filters']) | ||
|
||
return query_function(_ENRICHMENT_ID, filters_str, variables_list, table_data_enrichment, | ||
table_geo_enrichment, user_dataset, _WORKING_PROJECT, tablename, **kwargs) | ||
return query_function(_ENRICHMENT_ID, filters_str, table_to_geotable, table_to_variables, table_to_project, | ||
table_to_dataset, user_dataset, _WORKING_PROJECT, tablename, **kwargs) | ||
|
||
|
||
def _execute_enrichment(bq_client, queries, data_copy, data_geom_column): | ||
|
||
dfs_enriched = list() | ||
|
||
for query in queries: | ||
df_enriched = bq_client.query(query).to_dataframe() | ||
|
||
def _execute_enrichment(bq_client, query, data_copy, data_geom_column): | ||
df_enriched = bq_client.query(query).to_dataframe() | ||
dfs_enriched.append(df_enriched) | ||
|
||
data_copy = data_copy.merge(df_enriched, on=_ENRICHMENT_ID, how='left').drop(_ENRICHMENT_ID, axis=1) | ||
for df in dfs_enriched: | ||
data_copy = data_copy.merge(df, on=_ENRICHMENT_ID, how='left') | ||
|
||
data_copy.drop(_ENRICHMENT_ID, axis=1, inplace=True) | ||
data_copy[data_geom_column] = data_copy[data_geom_column].apply(geojson_to_wkt) | ||
|
||
return data_copy | ||
|
@@ -111,7 +124,7 @@ def __process_filters(filters_dict): | |
return filters | ||
|
||
|
||
def __get_tables_and_variables(variables): | ||
def __get_tables_and_variables(variables, user_dataset): | ||
|
||
if isinstance(variables, pd.Series): | ||
variables_id = [variables['id']] | ||
|
@@ -120,29 +133,63 @@ def __get_tables_and_variables(variables): | |
else: | ||
raise EnrichmentException('Variable(s) to enrich should be an instance of Series or DataFrame') | ||
|
||
table_to_variables = __process_enrichment_variables(variables_id) | ||
table_data_enrichment = list(table_to_variables.keys()).pop() | ||
table_geo_enrichment = __get_name_geotable_from_datatable(table_data_enrichment) | ||
variables_list = list(table_to_variables.values()).pop() | ||
table_to_geotable, table_to_variables, table_to_project, table_to_dataset =\ | ||
__process_enrichment_variables(variables_id, user_dataset) | ||
|
||
return table_data_enrichment, table_geo_enrichment, variables_list | ||
return table_to_geotable, table_to_variables, table_to_project, table_to_dataset | ||
simon-contreras-deel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def __process_enrichment_variables(variables): | ||
def __process_enrichment_variables(variables, user_dataset): | ||
table_to_geotable = dict() | ||
table_to_variables = defaultdict(list) | ||
table_to_project = dict() | ||
table_to_dataset = dict() | ||
|
||
for variable in variables: | ||
variable_split = variable.split('.') | ||
table, variable = variable_split[-2], variable_split[-1] | ||
project_part, dataset_part, table_part, variable_part = variable_split | ||
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. I would use 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. Sure, better naming! |
||
|
||
if project_part != _PUBLIC_PROJECT: | ||
table_part = '{dataset}_{table}'.format(dataset=dataset_part, | ||
table=table_part, | ||
user_dataset=user_dataset) | ||
|
||
if table_part not in table_to_dataset: | ||
if project_part != _PUBLIC_PROJECT: | ||
table_to_dataset[table_part] = user_dataset | ||
else: | ||
table_to_dataset[table_part] = _PUBLIC_DATASET | ||
|
||
table_to_variables[table].append(variable) | ||
if table_part not in table_to_geotable: | ||
geotable = __get_name_geotable_from_datatable(table_part) | ||
|
||
return table_to_variables | ||
if project_part != _PUBLIC_PROJECT: | ||
geotable = '{dataset}_{geotable}'.format(dataset=dataset_part, | ||
geotable=geotable, | ||
user_dataset=user_dataset) | ||
|
||
table_to_geotable[table_part] = geotable | ||
|
||
if table_part not in table_to_project: | ||
if project_part == _PUBLIC_PROJECT: | ||
table_to_project[table_part] = _PUBLIC_PROJECT | ||
else: | ||
table_to_project[table_part] = _WORKING_PROJECT | ||
|
||
table_to_variables[table_part].append(variable_part) | ||
|
||
return table_to_geotable, table_to_variables, table_to_project, table_to_dataset | ||
|
||
|
||
def __get_name_geotable_from_datatable(datatable): | ||
|
||
datatable_split = datatable.split('_') | ||
geo_information = datatable_split[2:5] | ||
|
||
if len(datatable_split) == 8: | ||
simon-contreras-deel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
geo_information = datatable_split[3:6] | ||
elif len(datatable_split) == 7: | ||
geo_information = datatable_split[2:5] | ||
|
||
geotable = 'geography_{geo_information_joined}'.format(geo_information_joined='_'.join(geo_information)) | ||
|
||
return geotable |
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
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.
I would rename method to:
_enrichment_queries
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.
You are right!