<commit_before>from .enrichment_service import enrich

# TODO: process column name in metadata, remove spaces and points


def enrich_polygons(data, variables, agg_operators, data_geom_column='geometry', filters=dict(), credentials=None):

    data_enriched = enrich(_prepare_sql, data=data, variables=variables, agg_operators=agg_operators,
                           data_geom_column=data_geom_column, filters=filters, credentials=credentials)

    return data_enriched


def _prepare_sql(enrichment_id, filters_processed, variables_processed, enrichment_table,
                 enrichment_geo_table, user_dataset, working_project, data_table, **kwargs):

    grouper = 'group by data_table.{enrichment_id}'.format(enrichment_id=enrichment_id)
    agg_operators = None

    if 'agg_operators' in kwargs:
        variables_sql = ['{operator}({variable} * \
                         (ST_Area(ST_Intersection(enrichment_geo_table.geom, data_table.{data_geom_column}))\
                         / ST_area(data_table.{data_geom_column}))) as {variable}'.format(variable=variable,
                         data_geom_column=kwargs['data_geom_column'],
                         operator=kwargs['agg_operators'][variable]) for variable in variables_processed]

        if isinstance(kwargs['agg_operators'], str):
            agg_operators = {variable: kwargs['agg_operators'] for variable in variables_processed}

    elif agg_operators is None:
        variables_sql = variables_processed + ['ST_Area(ST_Intersection(geo_table.geom, data_table.{data_geom_column}))\
                                               / ST_area(data_table.{data_geom_column}) AS measures_proportion'.format(
                                                data_geom_column=kwargs['data_geom_column'])]
        grouper = ''

    sql = '''
        SELECT data_table.{enrichment_id}, {variables}
        FROM `{working_project}.{user_dataset}.{enrichment_table}` enrichment_table
        JOIN `{working_project}.{user_dataset}.{enrichment_geo_table}` enrichment_geo_table
          ON enrichment_table.geoid = enrichment_geo_table.geoid
        JOIN `{working_project}.{user_dataset}.{data_table}` data_table
          ON ST_Intersects(data_table.{data_geom_column}, enrichment_geo_table.geom)
        {filters}
        {grouper};
    '''.format(enrichment_id=enrichment_id, variables=', '.join(variables_sql),
               enrichment_table=enrichment_table, enrichment_geo_table=enrichment_geo_table,
               user_dataset=user_dataset, working_project=working_project,
               data_table=data_table, data_geom_column=kwargs['data_geom_column'],<commit_msg>Bug fix agg operator passed by params<commit_after>from .enrichment_service import enrich

# TODO: process column name in metadata, remove spaces and points


def enrich_polygons(data, variables, agg_operators, data_geom_column='geometry', filters=dict(), credentials=None):

    data_enriched = enrich(_prepare_sql, data=data, variables=variables, agg_operators=agg_operators,
                           data_geom_column=data_geom_column, filters=filters, credentials=credentials)

    return data_enriched


def _prepare_sql(enrichment_id, filters_processed, variables_processed, enrichment_table,
                 enrichment_geo_table, user_dataset, working_project, data_table, **kwargs):

    grouper = 'group by data_table.{enrichment_id}'.format(enrichment_id=enrichment_id)

    if 'agg_operators' in kwargs:

        if isinstance(kwargs['agg_operators'], str):
            agg_operators = {variable: kwargs['agg_operators'] for variable in variables_processed}
        else:
            agg_operators = kwargs['agg_operators']

        variables_sql = ['{operator}({variable} * \
                         (ST_Area(ST_Intersection(enrichment_geo_table.geom, data_table.{data_geom_column}))\
                         / ST_area(data_table.{data_geom_column}))) as {variable}'.format(variable=variable,
                         data_geom_column=kwargs['data_geom_column'],
                         operator=agg_operators[variable]) for variable in variables_processed]

    else:
        variables_sql = variables_processed + ['ST_Area(ST_Intersection(geo_table.geom, data_table.{data_geom_column}))\
                                               / ST_area(data_table.{data_geom_column}) AS measures_proportion'.format(
                                                data_geom_column=kwargs['data_geom_column'])]
        grouper = ''

    sql = '''
        SELECT data_table.{enrichment_id}, {variables}
        FROM `{working_project}.{user_dataset}.{enrichment_table}` enrichment_table
        JOIN `{working_project}.{user_dataset}.{enrichment_geo_table}` enrichment_geo_table
          ON enrichment_table.geoid = enrichment_geo_table.geoid
        JOIN `{working_project}.{user_dataset}.{data_table}` data_table
          ON ST_Intersects(data_table.{data_geom_column}, enrichment_geo_table.geom)
        {filters}
        {grouper};
    '''.format(enrichment_id=enrichment_id, variables=', '.join(variables_sql),
               enrichment_table=enrichment_table, enrichment_geo_table=enrichment_geo_table,
               user_dataset=user_dataset, working_project=working_project,
               data_table=data_table, data_geom_column=kwargs['data_geom_column'],
{'fraction_non_alphanumeric': 0.09454887218045113, 'fraction_numerical': 0.0, 'mean_word_length': 3.3401305057096247, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4549038', 'n_tokens_mistral': 1607, 'n_tokens_neox': 1522, 'n_words': 237}