scope3data / methodology

Measurement methodology for advertising emissions
https://methodology.scope3.com
Apache License 2.0
32 stars 11 forks source link

Fix ad tech platform computation #150

Closed EmmaLouise2018 closed 8 months ago

EmmaLouise2018 commented 8 months ago

Fix the ad tech platform computation when cookie sync information is 0. Verify that the denominator is valid before dividing by 0

Given the input:

{
    "atp_template": "ssp",
    "allocation_of_corporate_emissions_pct": null,
    "corporate_emissions_g_co2e": null,
    "bid_requests_processed_from_ad_tech_platforms_pct": 1.15,
    "bid_requests_processed_from_publishers_pct": 100,
    "bid_request_size_in_bytes": 10000,
    "server_to_server_emissions_g_co2e_per_gb": 1.379,
    "server_emissions_mt_co2e_per_month": 0.0001,
    "servers_processing_bid_requests_pct": 100,
    "cookie_syncs_processed_per_bid_request": 0,
    "datacenter_water_intensity_h2o_m_3_per_mwh": 17.1,
    "server_emissions_g_co2e_per_kwh": 84.4,
    "servers_processing_cookie_syncs_pct": 0,
    "cookie_sync_distribution_ratio": 0,
    "bid_requests_processed_billion_per_month": 0.3,
    "cookie_syncs_processed_billion_per_month": 0
}

Previously this triggered an internal server error

  File "/scope3_methodology/ad_tech_platform/model.py", line 395, in model_product
    primary_emissions_per_cookie_sync = self.comp_primary_emissions_g_co2e_per_cookie_sync(
  File "/scope3_methodology/ad_tech_platform/model.py", line 295, in comp_primary_emissions_g_co2e_per_cookie_sync
    primary_emissions_per_cookie_sync = (
decimal.InvalidOperation: [<class 'decimal.DivisionUndefined'>]

Now it will correctly return the response

{
    "name": "",
    "identifier": "",
    "primary_bid_request_emissions_g_co2e": 2.947627163728078e-05,
    "primary_cookie_sync_emissions_g_co2e": 0,
    "corporate_emissions_g_co2e_per_bid_request": 1.63e-05,
    "cookie_sync_distribution_ratio": 0,
    "atp_block_rate": 0.9885,
    "publisher_block_rate": 0,
    "secondary_bid_request_emissions_g_co2e": null,
    "secondary_cookie_sync_emissions_g_co2e": null
}