-
-
Notifications
You must be signed in to change notification settings - Fork 75
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
Sum per partition histogram #512
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -201,6 +201,7 @@ def _list_to_contribution_histograms( | |
l0_contributions = l1_contributions = None | ||
linf_contributions = linf_sum_contributions = None | ||
count_per_partition = privacy_id_per_partition_count = None | ||
sum_per_partition_histogram = None | ||
for histogram in histograms: | ||
if histogram.name == hist.HistogramType.L0_CONTRIBUTIONS: | ||
l0_contributions = histogram | ||
|
@@ -214,10 +215,14 @@ def _list_to_contribution_histograms( | |
count_per_partition = histogram | ||
elif histogram.name == hist.HistogramType.COUNT_PRIVACY_ID_PER_PARTITION: | ||
privacy_id_per_partition_count = histogram | ||
elif histogram.name == hist.HistogramType.SUM_PER_PARTITION: | ||
sum_per_partition_histogram = histogram | ||
|
||
return hist.DatasetHistograms(l0_contributions, l1_contributions, | ||
linf_contributions, linf_sum_contributions, | ||
count_per_partition, | ||
privacy_id_per_partition_count) | ||
privacy_id_per_partition_count, | ||
sum_per_partition_histogram) | ||
|
||
|
||
def _to_dataset_histograms(histogram_list, | ||
|
@@ -321,7 +326,7 @@ def _compute_linf_sum_contributions_histogram( | |
NUMBER_OF_BUCKETS_IN_LINF_SUM_CONTRIBUTIONS_HISTOGRAM. | ||
|
||
Args: | ||
col: collection with elements (privacy_id, partition_key, value). | ||
col: collection with elements ((privacy_id, partition_key), value). | ||
backend: PipelineBackend to run operations on the collection. | ||
Returns: | ||
1 element collection, which contains the computed hist.Histogram. | ||
|
@@ -355,11 +360,15 @@ def _min_max_lowers(col, number_of_buckets, | |
""" | ||
min_max_values = pipeline_functions.min_max_elements( | ||
backend, col, "Min and max value in dataset") | ||
|
||
# min_max_values: 1 element collection with a pair (min, max) | ||
return backend.map( | ||
min_max_values, lambda min_max: np.linspace(min_max[0], min_max[1], | ||
(number_of_buckets + 1)), | ||
"map to lowers") | ||
def generate_lowers(min_max: Tuple[float, float]) -> List[float]: | ||
min_, max_ = min_max | ||
if min_ == max_: | ||
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. will it generate |
||
return [min_, min_] | ||
return list(np.linspace(min_, max_, (number_of_buckets + 1))) | ||
|
||
return backend.map(min_max_values, generate_lowers, "map to lowers") | ||
|
||
|
||
def _compute_partition_count_histogram( | ||
|
@@ -417,6 +426,34 @@ def _compute_partition_privacy_id_count_histogram( | |
col, backend, hist.HistogramType.COUNT_PRIVACY_ID_PER_PARTITION) | ||
|
||
|
||
def _compute_partition_sum_histogram(col, | ||
backend: pipeline_backend.PipelineBackend): | ||
"""Computes histogram of counts per partition. | ||
|
||
This histogram contains: todo the number of partitions with total count of | ||
dvadym marked this conversation as resolved.
Show resolved
Hide resolved
|
||
contributions = 1, 2 etc. | ||
|
||
Args: | ||
col: collection with elements ((privacy_id, partition_key), value). | ||
backend: PipelineBackend to run operations on the collection. | ||
Returns: | ||
1 element collection, which contains the computed hist.Histogram. | ||
""" | ||
|
||
col = backend.map_tuple(col, lambda pid_pk, value: (pid_pk[1], value), | ||
"Drop privacy id") | ||
col = backend.sum_per_key(col, "Sum of contributions per partition") | ||
# col: (pk, sum_per_partition) | ||
col = backend.values(col, "Drop keys") | ||
# col: (float) | ||
col = backend.to_multi_transformable_collection(col) | ||
lowers = _min_max_lowers( | ||
col, NUMBER_OF_BUCKETS_IN_LINF_SUM_CONTRIBUTIONS_HISTOGRAM, backend) | ||
|
||
return _compute_frequency_histogram_helper_with_lowers( | ||
col, backend, hist.HistogramType.SUM_PER_PARTITION, lowers) | ||
|
||
|
||
def compute_dataset_histograms(col, data_extractors: pipeline_dp.DataExtractors, | ||
backend: pipeline_backend.PipelineBackend): | ||
"""Computes dataset histograms. | ||
|
@@ -464,13 +501,16 @@ def compute_dataset_histograms(col, data_extractors: pipeline_dp.DataExtractors, | |
partition_count_histogram = _compute_partition_count_histogram(col, backend) | ||
partition_privacy_id_count_histogram = _compute_partition_privacy_id_count_histogram( | ||
col_distinct, backend) | ||
partition_sum_histogram = _compute_partition_sum_histogram( | ||
col_with_values, backend) | ||
# all histograms are 1 element collections which contains ContributionHistogram | ||
|
||
# Combine histograms to histograms.DatasetHistograms. | ||
return _to_dataset_histograms([ | ||
l0_contributions_histogram, l1_contributions_histogram, | ||
linf_contributions_histogram, linf_sum_contributions_histogram, | ||
partition_count_histogram, partition_privacy_id_count_histogram | ||
partition_count_histogram, partition_privacy_id_count_histogram, | ||
partition_sum_histogram | ||
], backend) | ||
|
||
|
||
|
@@ -613,6 +653,39 @@ def _compute_partition_count_histogram_on_preaggregated_data( | |
hist.HistogramType.COUNT_PER_PARTITION) | ||
|
||
|
||
def _compute_partition_sum_histogram_on_preaggregated_data( | ||
col, backend: pipeline_backend.PipelineBackend): | ||
"""Computes histogram of counts per partition. | ||
|
||
This histogram contains: the number of partitions with total count of | ||
contributions = 1, 2 etc. | ||
|
||
Args: | ||
col: collection with a pre-aggregated dataset, each element is | ||
(partition_key, (count, sum, n_partitions, n_contributions)). | ||
backend: PipelineBackend to run operations on the collection. | ||
Returns: | ||
1 element collection, which contains the computed histograms.Histogram. | ||
""" | ||
col = backend.map_values( | ||
col, | ||
lambda x: x[1], # x is (count, sum, n_partitions, n_contributions) | ||
"Extract sum per partition contribution") | ||
# col: (pk, int) | ||
col = backend.sum_per_key(col, "Sum per partition") | ||
# col: (pk, int), where each element is the total count per partition. | ||
dvadym marked this conversation as resolved.
Show resolved
Hide resolved
|
||
col = backend.values(col, "Drop partition keys") | ||
# col: (int,) | ||
col = backend.to_multi_transformable_collection(col) | ||
lowers = _min_max_lowers( | ||
col, NUMBER_OF_BUCKETS_IN_LINF_SUM_CONTRIBUTIONS_HISTOGRAM, backend) | ||
# lowers: (float,) where each value defines a lower of a bin in the | ||
# generated histogram. | ||
|
||
return _compute_frequency_histogram_helper_with_lowers( | ||
col, backend, hist.HistogramType.SUM_PER_PARTITION, lowers) | ||
|
||
|
||
def _compute_partition_privacy_id_count_histogram_on_preaggregated_data( | ||
col, backend: pipeline_backend.PipelineBackend): | ||
"""Computes a histogram of privacy id counts per partition. | ||
|
@@ -675,10 +748,13 @@ def compute_dataset_histograms_on_preaggregated_data( | |
col, backend) | ||
partition_privacy_id_count_histogram = _compute_partition_privacy_id_count_histogram_on_preaggregated_data( | ||
col, backend) | ||
partition_sum_histogram = _compute_partition_sum_histogram_on_preaggregated_data( | ||
col, backend) | ||
|
||
# Combine histograms to histograms.DatasetHistograms. | ||
return _to_dataset_histograms([ | ||
l0_contributions_histogram, l1_contributions_histogram, | ||
linf_contributions_histogram, linf_sum_contributions_histogram, | ||
partition_count_histogram, partition_privacy_id_count_histogram | ||
partition_count_histogram, partition_privacy_id_count_histogram, | ||
partition_sum_histogram | ||
], backend) |
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
Oops, something went wrong.
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.
weird formatting but maybe that's what the tool generates