Skip to content

Commit 2936641

Browse files
feat: generate defaults by template type, make adtech and publisher take corporate emissions parameter not call nested add in corporate emissions standalone calculation
1 parent b4a998f commit 2936641

13 files changed

Lines changed: 304 additions & 100 deletions

File tree

.github/workflows/defaults_release.yaml

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,37 @@ jobs:
4949
poetry install
5050
env:
5151
POETRY_VIRTUALENVS_CREATE: false
52-
- name: Generate Defaults File
52+
- name: Generate Defaults Files
5353
run: python ./scope3_methodology/compute_defaults.py
54-
- name: Upload Defaults Files to Release
55-
id: upload_defaults_files
54+
- name: Upload ATP Defaults File to Release
55+
id: upload_atp_defaults_files
5656
uses: actions/upload-release-asset@v1.0.1
5757
env:
5858
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5959
with:
6060
upload_url: ${{ steps.create_release.outputs.upload_url }}
61-
asset_path: defaults.yaml
62-
asset_name: methodology_model_defaults.yaml
61+
asset_path: atp-defaults.yaml
62+
asset_name: methodology_atp_defaults.yaml
63+
asset_content_type: text/plain
64+
- name: Upload Organization Defaults File to Release
65+
id: upload_org_defaults_files
66+
uses: actions/upload-release-asset@v1.0.1
67+
env:
68+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
69+
with:
70+
upload_url: ${{ steps.create_release.outputs.upload_url }}
71+
asset_path: organization-defaults.yaml
72+
asset_name: methodology_organization_defaults.yaml
73+
asset_content_type: text/plain
74+
- name: Upload Property Defaults Files to Release
75+
id: upload_prop_defaults_files
76+
uses: actions/upload-release-asset@v1.0.1
77+
env:
78+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
79+
with:
80+
upload_url: ${{ steps.create_release.outputs.upload_url }}
81+
asset_path: property-defaults.yaml
82+
asset_name: methodology_property_defaults.yaml
6383
asset_content_type: text/plain
6484
- name: Publish Release
6585
uses: eregon/publish-release@v1

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Output files
2-
defaults.yaml
2+
atp-defaults.yaml
3+
organization-defaults.yaml
4+
property-defaults.yaml
35

46
# git
57
*.swp

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,21 @@ To write defaults from latest sources:
6161
./scope3_methodology/compute_defaults.py
6262
```
6363

64+
To compute the corporate emissions, pass in its YAML file:
65+
66+
```sh
67+
./scope3_methodology/corporate.py --verbose [--publisher] [--adTechPlatform] [your_model.yaml]
68+
```
69+
6470
To compute the emissions for an ad tech company, pass in its YAML file:
6571

6672
```sh
67-
./scope3_methodology/adtech_model.py -v sources/companies/criteo/data.yaml
73+
./scope3_methodology/adtech_model.py -v [--corporateEmissionsG] [--corporateEmissionsGPerRequest] sources/companies/criteo/data.yaml
6874
```
6975

7076
To compute the emissions for publisher, pass in its YAML file:
7177

7278
```sh
73-
./scope3_methodology/publisher.py -vsources/companies/theguardian/data.yaml
79+
./scope3_methodology/publisher.py -v [--corporateEmissionsG] [--corporateEmissionsGPerImp] sources/companies/theguardian/data.yaml
7480

7581
```

docs/adTechModel.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ The file must have:
4242

4343
To model emissions, make sure defaults have been computed. Then run:
4444
```sh
45-
./scope3_methodology/adtech_model.py --verbose [your_model.yaml]
45+
./scope3_methodology/adtech_model.py --verbose [--corporateEmissionsG] [--corporateEmissionsGPerRequest] [your_model.yaml]
4646
```
4747

4848
If you want to see how the secondary emissions would be modeled, pass in `-p N` where N is the number of partners to simulate.

docs/corporate.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,28 @@ Includes home office emissions
1414

1515

1616
## Increased detail in financial data
17+
18+
19+
# Usage
20+
Create a YAML file that describes the company you would like to model. The YAML file should look like this:
21+
```
22+
company:
23+
name: Criteo
24+
products:
25+
- product:
26+
name: Criteo
27+
template: network
28+
identifier: criteo.com
29+
sources:
30+
- source:
31+
facts:
32+
- fact:
33+
employees: 2810
34+
```
35+
The file must have:
36+
- Some number of facts, with `employees` required at a minimum unless you provide full corporate emissions data
37+
38+
To model emissions, make sure defaults have been computed. Then run:
39+
```sh
40+
./scope3_methodology/corporate.py --verbose [--publisher] [--adTechPlatform] [your_model.yaml]
41+
```

poetry.lock

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

requirements.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
pyyaml==6.0 ; python_version >= "3.10" and python_version < "4.0" \
2+
--hash=sha256:0283c35a6a9fbf047493e3a0ce8d79ef5030852c51e9d911a27badfde0605293 \
3+
--hash=sha256:055d937d65826939cb044fc8c9b08889e8c743fdc6a32b33e2390f66013e449b \
4+
--hash=sha256:07751360502caac1c067a8132d150cf3d61339af5691fe9e87803040dbc5db57 \
5+
--hash=sha256:0b4624f379dab24d3725ffde76559cff63d9ec94e1736b556dacdfebe5ab6d4b \
6+
--hash=sha256:0ce82d761c532fe4ec3f87fc45688bdd3a4c1dc5e0b4a19814b9009a29baefd4 \
7+
--hash=sha256:1e4747bc279b4f613a09eb64bba2ba602d8a6664c6ce6396a4d0cd413a50ce07 \
8+
--hash=sha256:213c60cd50106436cc818accf5baa1aba61c0189ff610f64f4a3e8c6726218ba \
9+
--hash=sha256:231710d57adfd809ef5d34183b8ed1eeae3f76459c18fb4a0b373ad56bedcdd9 \
10+
--hash=sha256:277a0ef2981ca40581a47093e9e2d13b3f1fbbeffae064c1d21bfceba2030287 \
11+
--hash=sha256:2cd5df3de48857ed0544b34e2d40e9fac445930039f3cfe4bcc592a1f836d513 \
12+
--hash=sha256:40527857252b61eacd1d9af500c3337ba8deb8fc298940291486c465c8b46ec0 \
13+
--hash=sha256:473f9edb243cb1935ab5a084eb238d842fb8f404ed2193a915d1784b5a6b5fc0 \
14+
--hash=sha256:48c346915c114f5fdb3ead70312bd042a953a8ce5c7106d5bfb1a5254e47da92 \
15+
--hash=sha256:50602afada6d6cbfad699b0c7bb50d5ccffa7e46a3d738092afddc1f9758427f \
16+
--hash=sha256:68fb519c14306fec9720a2a5b45bc9f0c8d1b9c72adf45c37baedfcd949c35a2 \
17+
--hash=sha256:77f396e6ef4c73fdc33a9157446466f1cff553d979bd00ecb64385760c6babdc \
18+
--hash=sha256:819b3830a1543db06c4d4b865e70ded25be52a2e0631ccd2f6a47a2822f2fd7c \
19+
--hash=sha256:897b80890765f037df3403d22bab41627ca8811ae55e9a722fd0392850ec4d86 \
20+
--hash=sha256:98c4d36e99714e55cfbaaee6dd5badbc9a1ec339ebfc3b1f52e293aee6bb71a4 \
21+
--hash=sha256:9df7ed3b3d2e0ecfe09e14741b857df43adb5a3ddadc919a2d94fbdf78fea53c \
22+
--hash=sha256:9fa600030013c4de8165339db93d182b9431076eb98eb40ee068700c9c813e34 \
23+
--hash=sha256:a80a78046a72361de73f8f395f1f1e49f956c6be882eed58505a15f3e430962b \
24+
--hash=sha256:b3d267842bf12586ba6c734f89d1f5b871df0273157918b0ccefa29deb05c21c \
25+
--hash=sha256:b5b9eccad747aabaaffbc6064800670f0c297e52c12754eb1d976c57e4f74dcb \
26+
--hash=sha256:c5687b8d43cf58545ade1fe3e055f70eac7a5a1a0bf42824308d868289a95737 \
27+
--hash=sha256:cba8c411ef271aa037d7357a2bc8f9ee8b58b9965831d9e51baf703280dc73d3 \
28+
--hash=sha256:d15a181d1ecd0d4270dc32edb46f7cb7733c7c508857278d3d378d14d606db2d \
29+
--hash=sha256:d4db7c7aef085872ef65a8fd7d6d09a14ae91f691dec3e87ee5ee0539d516f53 \
30+
--hash=sha256:d4eccecf9adf6fbcc6861a38015c2a64f38b9d94838ac1810a9023a0609e1b78 \
31+
--hash=sha256:d67d839ede4ed1b28a4e8909735fc992a923cdb84e618544973d7dfc71540803 \
32+
--hash=sha256:daf496c58a8c52083df09b80c860005194014c3698698d1a57cbcfa182142a3a \
33+
--hash=sha256:e61ceaab6f49fb8bdfaa0f92c4b57bcfbea54c09277b1b4f7ac376bfb7a7c174 \
34+
--hash=sha256:f84fbc98b019fef2ee9a1cb3ce93e3187a6df0b2538a651bfb890254ba9f90b5

scope3_methodology/adtech_model.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import logging
55

66
import yaml
7-
from corporate import get_corporate_emissions
87
from utils import get_fact_or_default, get_facts_from_sources, log_result, log_step
98
from yaml.loader import SafeLoader
109

@@ -74,17 +73,17 @@ def getProductInfo(key: str, default: float | None, product: dict[str, float], d
7473

7574

7675
def getCorporateEmissionsPerBidRequest(
77-
corporateAllocation: float,
76+
corporate_emissions_g: float,
77+
corporate_allocation: float,
7878
facts: dict[str, float],
7979
defaults: dict[str, float],
8080
depth: int,
8181
) -> float:
82-
corporateEmissionsG = get_corporate_emissions(facts, defaults, depth - 1) * 1000000
8382
bidRequests = (
8483
get_fact_or_default("bid requests processed billion per month", facts, defaults, depth - 1)
8584
* 1000000000
8685
)
87-
corporateEmissionsPerBidRequest = corporateAllocation * corporateEmissionsG / bidRequests
86+
corporateEmissionsPerBidRequest = corporate_allocation * corporate_emissions_g / bidRequests
8887
log_result(
8988
"corporate emissions g per bid request",
9089
f"{corporateEmissionsPerBidRequest}:.8f",
@@ -180,15 +179,12 @@ def getServerEmissionsPerBidRequest(
180179

181180

182181
def getPrimaryEmissionsPerBidRequest(
182+
corporateEmissionsPerBidRequest: float,
183183
serverAllocation: float,
184-
corporateAllocation: float,
185184
facts: dict[str, float],
186185
defaults: dict[str, float],
187186
depth: int,
188187
) -> float:
189-
corporateEmissionsPerBidRequest = getCorporateEmissionsPerBidRequest(
190-
corporateAllocation, facts, defaults, depth - 1
191-
)
192188
dataTransferEmissionsPerBidRequest = getDataTransferEmissionsPerBidRequest(
193189
facts, defaults, depth - 1
194190
)
@@ -299,8 +295,8 @@ def main():
299295
parser.add_argument(
300296
"-d",
301297
"--defaultsFile",
302-
default="defaults.yaml",
303-
help="Set the defaults file to use (overrides defaults.yaml)",
298+
default="atp-defaults.yaml",
299+
help="Set the defaults file to use (overrides atp-defaults.yaml)",
304300
)
305301
parser.add_argument("-v", "--verbose", action="store_true", help="Show derivation of output")
306302
parser.add_argument(
@@ -312,6 +308,20 @@ def main():
312308
nargs="?",
313309
help="Simulate distribution partners",
314310
)
311+
parser.add_argument(
312+
"--corporateEmissionsG",
313+
const=1,
314+
type=float,
315+
nargs="?",
316+
help="Provide the corporate emissionsfor organization",
317+
)
318+
parser.add_argument(
319+
"--corporateEmissionsGPerImp",
320+
const=1,
321+
type=float,
322+
nargs="?",
323+
help="Provide the corporate emissions for organization per bid request",
324+
)
315325
parser.add_argument("companyFile", nargs=1, help="The company file to parse in YAML format")
316326
args = parser.parse_args()
317327
if args.verbose:
@@ -332,6 +342,12 @@ def main():
332342
raise Exception("No 'sources' field found in company file")
333343
facts = get_facts_from_sources(document["company"]["sources"])
334344

345+
corporate_emissions_g = args.corporateEmissionsG
346+
corporate_emissions_g_per_imp = args.corporateEmissionsGPerImp
347+
348+
if not corporate_emissions_g and not corporate_emissions_g_per_imp:
349+
raise Exception("Must provide either --corporateEmissionsG or --corporateEmissionsGPerImp")
350+
335351
depth = 4 if args.verbose else 0
336352
productModels = []
337353
for product in document["company"]["products"]:
@@ -346,8 +362,15 @@ def main():
346362
if template not in defaultsDocument["defaults"]:
347363
raise Exception(f"Template {template} not found in defaults")
348364
defaults = defaultsDocument["defaults"][template]
365+
366+
corporate_emissions_per_bid_request = corporate_emissions_g_per_imp
367+
if corporate_emissions_g:
368+
corporate_emissions_per_bid_request = getCorporateEmissionsPerBidRequest(
369+
corporate_emissions_g, corporateAllocation, facts, defaults, depth - 1
370+
)
371+
349372
primaryEmissionsPerBidRequest = getPrimaryEmissionsPerBidRequest(
350-
serverAllocation, corporateAllocation, facts, defaults, depth
373+
corporate_emissions_per_bid_request, serverAllocation, facts, defaults, depth
351374
)
352375
primaryEmissionsPerCookieSync = getPrimaryEmissionsPerCookieSync(
353376
serverAllocation, facts, defaults, depth

0 commit comments

Comments
 (0)