Exemple #1
0
def export_sample_messages_csv(messages, consent_withdrawn_field, analysis_configurations, f,
                               filter_code_ids=None, limit_per_code=sys.maxsize):
    """
    Computes the sample messages and exports them to a CSV.

    :param messages: Objects to sample the messages from.
    :type messages: iterable of core_data_modules.traced_data.TracedData
    :param consent_withdrawn_field: Field in each messages object which records if consent is withdrawn.
    :type consent_withdrawn_field: str
    :param analysis_configurations: Configurations for the datasets to include in the sample_messages.
    :type analysis_configurations: iterable of core_data_modules.analysis.AnalysisConfiguration
    :param f: File to write the sample_messages CSV to.
    :type f: file-like
    :param filter_code_ids: The code ids to sample messages for, or None. If None, exports sample messages for all
                            code ids.
    :type filter_code_ids: list of str | None
    :param limit_per_code: The maximum number of sample messages to export per code.
                           Defaults to sys.maxsize, i.e. to no practical limit.
    :type limit_per_code: int
    """
    analysis_utils.write_csv(
        compute_sample_messages(messages, consent_withdrawn_field, analysis_configurations,
                                filter_code_ids, limit_per_code),
        sample_messages_keys,
        f
    )
def export_theme_distributions_csv(individuals, consent_withdrawn_field,
                                   theme_configurations, breakdown_configurations, f):
    """
    Computes the theme_distributions and exports them to a CSV.

    The CSV will contain the headers:
     - Dataset, set to the dataset_name in each of the `theme_configurations`, de-duplicated for clarity).
     - Theme, set to {dataset_name}_{code.string_value}, for each code in each theme_configuration.
     - Total Participants
     - Total Participants %
    and raw total and % headers for each scheme and code in the `breakdown_configurations`.

    :param individuals: Individuals to compute the theme_distributions for.
    :type individuals: iterable of core_data_modules.traced_data.TracedData
    :param consent_withdrawn_field: Field in each individuals object which records if consent is withdrawn.
    :type consent_withdrawn_field: str
    :param theme_configurations: Configuration for the themes. Each configuration should contain:
                                  - dataset_name. This will be used to index the returned theme_distributions dict
                                    (the "theme dataset_name" above).
                                  - code_scheme. This will be used to index each themes dictionary in the returned
                                    theme_distributions dict (the "theme" above). Themes are formatted as
                                    {theme_configuration.dataset_name}_{code.string_value} for each code in the
                                    code_scheme.
    :type theme_configurations: iterable of core_data_modules.analysis.AnalysisConfiguration
    :param breakdown_configurations: Configuration for the breakdowns dict.
                                     For details, see `theme_distributions._make_breakdowns_dict`.
    :type breakdown_configurations: iterable of core_data_modules.analysis.AnalysisConfiguration
    :param f: File to write the theme_distributions CSV to.
    :type f: file-like
    """
    theme_distributions = compute_theme_distributions(individuals, consent_withdrawn_field,
                                                      theme_configurations, breakdown_configurations)

    # Denormalize the theme_distributions into a flat array of dicts that can be written to disk.
    csv_data = []
    last_dataset_name = None
    for dataset_name, themes in theme_distributions.items():
        for theme, breakdowns in themes.items():
            row = {
                "Dataset": dataset_name if dataset_name != last_dataset_name else "",
                "Theme": theme
            }
            row.update(breakdowns)
            csv_data.append(row)
            last_dataset_name = dataset_name

    headers = ["Dataset", "Theme"]
    headers.extend(_make_breakdowns_dict(breakdown_configurations).keys())

    analysis_utils.write_csv(
        csv_data,
        headers,
        f
    )
Exemple #3
0
def export_repeat_participations_csv(individuals, consent_withdrawn_field,
                                     analysis_configurations, f):
    """
    Computes the repeat_participations and exports them to a CSV.

    :param individuals: Individuals to compute the repeat participations for.
    :type individuals: iterable of core_data_modules.traced_data.TracedData
    :param consent_withdrawn_field: Field in each individuals object which records if consent is withdrawn.
    :type consent_withdrawn_field: str
    :param analysis_configurations: Configurations for the datasets/coded fields to include in the engagement_counts.
    :type analysis_configurations: iterable of core_data_modules.analysis.AnalysisConfiguration
    :param f: File to write the engagement_counts CSV to.
    :type f: file-like
    """
    analysis_utils.write_csv(
        compute_repeat_participations(individuals, consent_withdrawn_field,
                                      analysis_configurations).values(),
        repeat_participations_keys, f)
def export_cross_tabs_csv(individuals, consent_withdrawn_field,
                          analysis_configuration_1, analysis_configuration_2,
                          f):
    """
    Computes cross-tabs and exports them to a file.

    :param individuals: Individuals to compute the cross-tabs for.
    :type individuals: iterable of core_data_modules.traced_data.TracedData
    :param consent_withdrawn_field: Field in each individuals object which records if consent is withdrawn.
    :type consent_withdrawn_field: str
    :param analysis_configuration_1: Configuration for the first cross-tab variable.
    :type analysis_configuration_1: core_data_modules.analysis.AnalysisConfiguration
    :param analysis_configuration_2: Configuration for the second cross-tab variable.
    :type analysis_configuration_2: core_data_modules.analysis.AnalysisConfiguration
    :param f: File to write the cross_tabs CSV to.
    :type f: file-like
    """
    cross_tabs = compute_cross_tabs(individuals, consent_withdrawn_field,
                                    analysis_configuration_1,
                                    analysis_configuration_2)

    analysis_utils.write_csv(cross_tabs, list(cross_tabs[0].keys()), f)
Exemple #5
0
def export_engagement_counts_csv(messages, individuals,
                                 consent_withdrawn_field,
                                 analysis_configurations, f):
    """
    Computes the engagement_counts and exports them to a CSV.

    :param messages: Messages to compute the engagement_counts for.
    :type messages: iterable of core_data_modules.traced_data.TracedData
    :param individuals: Individuals to compute the engagement_counts for.
    :type individuals: iterable of core_data_modules.traced_data.TracedData
    :param consent_withdrawn_field: Field in each messages/individuals object which records if consent is withdrawn.
    :type consent_withdrawn_field: str
    :param analysis_configurations: Configurations for the datasets/coded fields to include in the engagement_counts.
    :type analysis_configurations: iterable of core_data_modules.analysis.AnalysisConfiguration
    :return: Dictionary of dataset_name | "Total" -> dict with keys `engagement_counts_keys`.
    :rtype: OrderedDict of str -> dict
    :param f: File to write the engagement_counts CSV to.
    :type f: file-like
    """
    analysis_utils.write_csv(
        compute_engagement_counts(messages, individuals,
                                  consent_withdrawn_field,
                                  analysis_configurations).values(),
        engagement_counts_keys, f)
Exemple #6
0
def export_traffic_analysis_csv(messages, consent_withdrawn_field,
                                analysis_configurations, time_field,
                                traffic_labels, f):
    """
    Computes the traffic_analysis and exports the results to a CSV.

    :param messages: Messages to compute the engagement_counts for.
    :type messages: iterable of core_data_modules.traced_data.TracedData
    :param consent_withdrawn_field: Field in each messages object which records if consent is withdrawn.
    :type consent_withdrawn_field: str
    :param analysis_configurations: Configurations for the datasets/coded fields to check for opt-ins/relevance in
                                    the time ranges.
    :type analysis_configurations: iterable of core_data_modules.analysis.AnalysisConfiguration
    :param time_field: Field in each messages object which records when the message was sent.
    :type time_field: str
    :param traffic_labels: Time ranges to search and labels to apply.
    :type traffic_labels: iterable of TrafficLabel
    :param f: File to write the engagement_counts CSV to.
    :type f: file-like
    """
    analysis_utils.write_csv(
        compute_traffic_analysis(messages, consent_withdrawn_field,
                                 analysis_configurations, time_field,
                                 traffic_labels), traffic_analysis_keys, f)