Skip to content

carbon

zeus.monitor.carbon

Carbon intensity providers used for carbon-aware optimizers.

ElectrictyMapsClient fetches live hourly carbon intensity from the Electricity Maps API. TraceCarbonIntensityProvider reads carbon intensity from a downloaded file, which is useful for reproducing a CarbonEmissionMonitor result or replaying a past day. Supported formats:

  • Electricity Maps CSV exports (hourly): from_electricitymaps_csv.
  • U.S. EIA-930 Hourly Electric Grid Monitor balancing-authority workbooks (.xlsx, hourly): from_eia930_xlsx.
  • GB Carbon Intensity API responses saved as JSON (30-minute intervals, past data or 48-hour forecasts): from_gb_carbon_intensity_json.
  • Other CSV files with one timestamp column and one intensity column: from_csv.

ZeusCarbonIntensityHTTPError

Bases: ZeusBaseError

Exception when HTTP request to carbon intensity provider fails.

Source code in zeus/monitor/carbon.py
50
51
52
53
54
55
class ZeusCarbonIntensityHTTPError(ZeusBaseError):
    """Exception when HTTP request to carbon intensity provider fails."""

    def __init__(self, message: str) -> None:
        """Initialize HTTP request exception."""
        super().__init__(message)

__init__

__init__(message)
Source code in zeus/monitor/carbon.py
53
54
55
def __init__(self, message: str) -> None:
    """Initialize HTTP request exception."""
    super().__init__(message)

ZeusCarbonIntensityNotFoundError

Bases: ZeusBaseError

Exception when carbon intensity measurement could not be retrieved.

Source code in zeus/monitor/carbon.py
58
59
60
61
62
63
class ZeusCarbonIntensityNotFoundError(ZeusBaseError):
    """Exception when carbon intensity measurement could not be retrieved."""

    def __init__(self, message: str) -> None:
        """Initialize carbon not found exception."""
        super().__init__(message)

__init__

__init__(message)
Source code in zeus/monitor/carbon.py
61
62
63
def __init__(self, message: str) -> None:
    """Initialize carbon not found exception."""
    super().__init__(message)

CarbonIntensityProvider

Bases: ABC

Abstract class for implementing ways to fetch carbon intensity.

Source code in zeus/monitor/carbon.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
class CarbonIntensityProvider(abc.ABC):
    """Abstract class for implementing ways to fetch carbon intensity."""

    @abc.abstractmethod
    def get_current_carbon_intensity(self) -> float:
        """Abstract method for fetching the current carbon intensity of the set location of the class."""
        pass

    @abc.abstractmethod
    def get_recent_carbon_intensity(self) -> dict[datetime, float]:
        """Abstract method for fetching the current carbon intensity of the set location of the class."""
        pass

    @property
    @abc.abstractmethod
    def update_period(self) -> timedelta:
        """Abstract method for how long each carbon intensity value in the history dict remains current."""
        pass

    @property
    @abc.abstractmethod
    def history_length(self) -> int:
        """Abstract method for how many carbon intensity values are in the history dict."""
        pass

update_period abstractmethod property

update_period

Abstract method for how long each carbon intensity value in the history dict remains current.

history_length abstractmethod property

history_length

Abstract method for how many carbon intensity values are in the history dict.

get_current_carbon_intensity abstractmethod

get_current_carbon_intensity()

Abstract method for fetching the current carbon intensity of the set location of the class.

Source code in zeus/monitor/carbon.py
69
70
71
72
@abc.abstractmethod
def get_current_carbon_intensity(self) -> float:
    """Abstract method for fetching the current carbon intensity of the set location of the class."""
    pass

get_recent_carbon_intensity abstractmethod

get_recent_carbon_intensity()

Abstract method for fetching the current carbon intensity of the set location of the class.

Source code in zeus/monitor/carbon.py
74
75
76
77
@abc.abstractmethod
def get_recent_carbon_intensity(self) -> dict[datetime, float]:
    """Abstract method for fetching the current carbon intensity of the set location of the class."""
    pass

ElectrictyMapsClient

Bases: CarbonIntensityProvider

Carbon Intensity Provider with ElectricityMaps API.

Reference:

  1. ElectricityMaps
  2. ElectricityMaps API
  3. ElectricityMaps GitHub
Source code in zeus/monitor/carbon.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
class ElectrictyMapsClient(CarbonIntensityProvider):
    """Carbon Intensity Provider with ElectricityMaps API.

    Reference:

    1. [ElectricityMaps](https://www.electricitymaps.com/)
    2. [ElectricityMaps API](https://static.electricitymaps.com/api/docs/index.html)
    3. [ElectricityMaps GitHub](https://github.com/electricitymaps/electricitymaps-contrib)
    """

    def __init__(
        self,
        location: tuple[float, float],
        estimate: bool = False,
        emission_factor_type: Literal["direct", "lifecycle"] = "direct",
    ) -> None:
        """Iniitializes ElectricityMaps Carbon Provider.

        Args:
            location: tuple of latitude and longitude (latitude, longitude)
            estimate: bool to toggle whether carbon intensity is estimated or not
            emission_factor_type: emission factor to be measured (`direct` or `lifestyle`)
        """
        self.lat, self.long = location
        self.estimate = estimate
        self.emission_factor_type = emission_factor_type

    def get_current_carbon_intensity(self) -> float:
        """Fetches current carbon intensity of the location of the class.

        In some locations, there is no recent carbon intensity data. `self.estimate` can be used to approximate the carbon intensity in such cases.
        """
        try:
            url = (
                f"https://api.electricitymap.org/v3/carbon-intensity/latest?lat={self.lat}&lon={self.long}"
                + f"&disableEstimations={not self.estimate}&emissionFactorType={self.emission_factor_type}"
            )
            resp = requests.get(url)
        except requests.exceptions.RequestException as e:
            raise ZeusCarbonIntensityHTTPError(f"Failed to retrieve current carbon intensity measurement: {e}") from e

        try:
            return resp.json()["carbonIntensity"]
        except KeyError as e:
            # Raise exception when carbonIntensity does not exist in response
            raise ZeusCarbonIntensityNotFoundError(
                f"Current carbon intensity measurement not found at `({self.lat}, {self.long})` "
                f"with estimate set to `{self.estimate}` and emission_factor_type set to `{self.emission_factor_type}`\n"
                f"JSON Response: {resp.text}"
            ) from e

    def get_recent_carbon_intensity(self) -> dict[datetime, float]:
        """Fetches recent (within last 24 hours) carbon intensity of the location of the class.

        In some locations, there is no recent carbon intensity data. `self.estimate` can be used to approximate the carbon intensity in such cases.
        """
        try:
            url = (
                f"https://api.electricitymap.org/v3/carbon-intensity/history?lat={self.lat}&lon={self.long}"
                + f"&disableEstimations={not self.estimate}&emissionFactorType={self.emission_factor_type}"
            )
            resp = requests.get(url)
        except requests.exceptions.RequestException as e:
            raise ZeusCarbonIntensityHTTPError(f"Failed to retrieve recent carbon intensity measurement: {e}") from e

        try:
            recent_carbon_intensities: dict[datetime, float] = {
                parser.parse(measurement["datetime"]): measurement["carbonIntensity"]
                for measurement in resp.json()["history"]
            }
            return recent_carbon_intensities
        except KeyError as e:
            # Raise exception when carbonIntensity does not exist in response
            raise ZeusCarbonIntensityNotFoundError(
                f"Recent carbon intensity measurement not found at `({self.lat}, {self.long})` "
                f"with estimate set to `{self.estimate}` and emission_factor_type set to `{self.emission_factor_type}`\n"
                f"JSON Response: {resp.text}"
            ) from e

    @property
    def update_period(self) -> timedelta:
        """Returns timedelta for how long each carbon intensity value in the history dict remains current."""
        return timedelta(hours=1)

    @property
    def history_length(self) -> int:
        """Returns number of carbon intensity values in history dict."""
        return 24

update_period property

update_period

Returns timedelta for how long each carbon intensity value in the history dict remains current.

history_length property

history_length

Returns number of carbon intensity values in history dict.

__init__

__init__(
    location, estimate=False, emission_factor_type="direct"
)

Parameters:

Name Type Description Default
location tuple[float, float]

tuple of latitude and longitude (latitude, longitude)

required
estimate bool

bool to toggle whether carbon intensity is estimated or not

False
emission_factor_type Literal['direct', 'lifecycle']

emission factor to be measured (direct or lifestyle)

'direct'
Source code in zeus/monitor/carbon.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
def __init__(
    self,
    location: tuple[float, float],
    estimate: bool = False,
    emission_factor_type: Literal["direct", "lifecycle"] = "direct",
) -> None:
    """Iniitializes ElectricityMaps Carbon Provider.

    Args:
        location: tuple of latitude and longitude (latitude, longitude)
        estimate: bool to toggle whether carbon intensity is estimated or not
        emission_factor_type: emission factor to be measured (`direct` or `lifestyle`)
    """
    self.lat, self.long = location
    self.estimate = estimate
    self.emission_factor_type = emission_factor_type

get_current_carbon_intensity

get_current_carbon_intensity()

Fetches current carbon intensity of the location of the class.

In some locations, there is no recent carbon intensity data. self.estimate can be used to approximate the carbon intensity in such cases.

Source code in zeus/monitor/carbon.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def get_current_carbon_intensity(self) -> float:
    """Fetches current carbon intensity of the location of the class.

    In some locations, there is no recent carbon intensity data. `self.estimate` can be used to approximate the carbon intensity in such cases.
    """
    try:
        url = (
            f"https://api.electricitymap.org/v3/carbon-intensity/latest?lat={self.lat}&lon={self.long}"
            + f"&disableEstimations={not self.estimate}&emissionFactorType={self.emission_factor_type}"
        )
        resp = requests.get(url)
    except requests.exceptions.RequestException as e:
        raise ZeusCarbonIntensityHTTPError(f"Failed to retrieve current carbon intensity measurement: {e}") from e

    try:
        return resp.json()["carbonIntensity"]
    except KeyError as e:
        # Raise exception when carbonIntensity does not exist in response
        raise ZeusCarbonIntensityNotFoundError(
            f"Current carbon intensity measurement not found at `({self.lat}, {self.long})` "
            f"with estimate set to `{self.estimate}` and emission_factor_type set to `{self.emission_factor_type}`\n"
            f"JSON Response: {resp.text}"
        ) from e

get_recent_carbon_intensity

get_recent_carbon_intensity()

Fetches recent (within last 24 hours) carbon intensity of the location of the class.

In some locations, there is no recent carbon intensity data. self.estimate can be used to approximate the carbon intensity in such cases.

Source code in zeus/monitor/carbon.py
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
def get_recent_carbon_intensity(self) -> dict[datetime, float]:
    """Fetches recent (within last 24 hours) carbon intensity of the location of the class.

    In some locations, there is no recent carbon intensity data. `self.estimate` can be used to approximate the carbon intensity in such cases.
    """
    try:
        url = (
            f"https://api.electricitymap.org/v3/carbon-intensity/history?lat={self.lat}&lon={self.long}"
            + f"&disableEstimations={not self.estimate}&emissionFactorType={self.emission_factor_type}"
        )
        resp = requests.get(url)
    except requests.exceptions.RequestException as e:
        raise ZeusCarbonIntensityHTTPError(f"Failed to retrieve recent carbon intensity measurement: {e}") from e

    try:
        recent_carbon_intensities: dict[datetime, float] = {
            parser.parse(measurement["datetime"]): measurement["carbonIntensity"]
            for measurement in resp.json()["history"]
        }
        return recent_carbon_intensities
    except KeyError as e:
        # Raise exception when carbonIntensity does not exist in response
        raise ZeusCarbonIntensityNotFoundError(
            f"Recent carbon intensity measurement not found at `({self.lat}, {self.long})` "
            f"with estimate set to `{self.estimate}` and emission_factor_type set to `{self.emission_factor_type}`\n"
            f"JSON Response: {resp.text}"
        ) from e

TraceCarbonIntensityProvider

Bases: CarbonIntensityProvider

Carbon intensity provider backed by a downloaded trace.

The trace is a time series of carbon intensity in gCO2eq/kWh. It is grouped into periods of length period (for example one hour) on a UTC grid. A lookup returns the value of the period that contains the requested time. Values are not interpolated or filled in, and carbon_intensity_at and get_current_carbon_intensity raise ZeusCarbonIntensityNotFoundError for periods without data.

To compute a run's emissions from past data, pass replay_start. Otherwise the trace must cover the run itself, for example a GB Carbon Intensity 48-hour forecast (/intensity/{from}/fw48h) saved just before the run and loaded with value="forecast".

The from_* class methods load common file formats.

Note

Do not mix direct (combustion) and lifecycle emission factors when you compare results.

Source code in zeus/monitor/carbon.py
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
class TraceCarbonIntensityProvider(CarbonIntensityProvider):
    """Carbon intensity provider backed by a downloaded trace.

    The trace is a time series of carbon intensity in gCO2eq/kWh. It is grouped into
    periods of length `period` (for example one hour) on a UTC grid. A lookup returns the
    value of the period that contains the requested time. Values are not interpolated or
    filled in, and `carbon_intensity_at` and `get_current_carbon_intensity` raise
    `ZeusCarbonIntensityNotFoundError` for periods without data.

    To compute a run's emissions from past data, pass `replay_start`. Otherwise the trace
    must cover the run itself, for example a GB Carbon Intensity 48-hour forecast
    (`/intensity/{from}/fw48h`) saved just before the run and loaded with `value="forecast"`.

    The `from_*` class methods load common file formats.

    !!! Note
        Do not mix direct (combustion) and lifecycle emission factors when you compare
        results.
    """

    def __init__(
        self,
        intensities: Mapping[datetime, float],
        period: timedelta,
        data_spacing: timedelta,
        replay_start: datetime | None = None,
    ) -> None:
        """Initialize the provider.

        Args:
            intensities: Maps the start of each data point (timezone-aware) to the carbon
                intensity in gCO2eq/kWh. Values must be finite and non-negative. Every timestamp
                must be on a UTC grid of `data_spacing` (for example, the top of each hour for
                hourly data). Gaps are allowed.
            period: Length of the periods carbon is accounted in. Must be a whole multiple
                of the data's spacing, so the data can't be coarser than `period`. Each
                period gets the mean of its data points, and periods missing any point are
                left out.
            data_spacing: Time between data points, for example one hour for hourly data.
            replay_start: A timezone-aware time inside the trace. If given, the wall-clock
                period in which the provider is created is mapped to the trace period that
                contains this time, and later periods follow one to one.
        """
        if period <= timedelta(0):
            raise ValueError("`period` must be positive.")
        if data_spacing <= timedelta(0):
            raise ValueError("`data_spacing` must be positive.")
        if data_spacing > period:
            raise ValueError(
                f"The data is coarser than `period`: `data_spacing` is {data_spacing}, but `period` is {period}."
            )
        if period % data_spacing:
            raise ValueError(f"`period` ({period}) must be a whole multiple of `data_spacing` ({data_spacing}).")
        if not intensities:
            raise ValueError("The carbon intensity trace is empty.")
        points: dict[datetime, float] = {}
        for start, value in intensities.items():
            if start.tzinfo is None:
                raise ValueError(f"Trace time {start} has no timezone. Use timezone-aware times.")
            if not math.isfinite(value) or value < 0:
                raise ValueError(f"Carbon intensity {value} at {start} is not a finite non-negative number.")
            points[start.astimezone(timezone.utc)] = float(value)

        starts = sorted(points)
        for start in starts:
            if _period_floor(start, data_spacing) != start:
                raise ValueError(
                    f"Trace time {start} is not on the {data_spacing} grid (for example, the top of each hour "
                    "for hourly data)."
                )

        per_period = period // data_spacing
        grouped: dict[datetime, list[float]] = defaultdict(list)
        for start in starts:
            grouped[_period_floor(start, period)].append(points[start])
        self.period = period
        self.trace = {p: sum(v) / len(v) for p, v in sorted(grouped.items()) if len(v) == per_period}
        if not self.trace:
            raise ValueError(f"No {period} period is fully covered by the data.")
        if len(self.trace) < len(grouped):
            logger.warning(
                "Left out %d of %d periods of length %s that the data covers only partly.",
                len(grouped) - len(self.trace),
                len(grouped),
                period,
            )

        self.replay_start = replay_start
        self.offset = timedelta(0)
        if replay_start is None:
            now = _period_floor(datetime.now(timezone.utc), period)
            if now not in self.trace:
                logger.warning(
                    "The trace has no value for the current period (%s). Pass `replay_start` to replay past data.",
                    now.isoformat(),
                )
        else:
            if replay_start.tzinfo is None:
                raise ValueError("`replay_start` must be timezone-aware.")
            start = _period_floor(replay_start, period)
            if start not in self.trace:
                raise ValueError(f"`replay_start` {replay_start} is not inside the trace.")
            self.offset = start - _period_floor(datetime.now(timezone.utc), period)

    @classmethod
    def _from_series(
        cls,
        times: pd.Series,
        values: pd.Series,
        period: timedelta,
        data_spacing: timedelta,
        scale: float = 1.0,
        time_label: Literal["start", "end"] = "start",
        replay_start: datetime | None = None,
    ) -> TraceCarbonIntensityProvider:
        """Build the provider from timezone-aware times and intensities, skipping empty values."""
        times = pd.Series(pd.to_datetime(times, utc=True)).reset_index(drop=True)
        values = pd.to_numeric(pd.Series(values), errors="coerce").reset_index(drop=True) * scale
        keep = values.notna()
        times, values = times[keep], values[keep]
        if times.duplicated().any():
            raise ValueError("The trace has repeated timestamps.")
        if time_label == "end":
            # Each timestamp ends one data interval, so move it back by `data_spacing`.
            times = times - data_spacing
        trace = {t.to_pydatetime(): float(v) for t, v in zip(times, values)}
        return cls(trace, period=period, data_spacing=data_spacing, replay_start=replay_start)

    @classmethod
    def from_csv(
        cls,
        path: str | Path,
        time_column: str,
        intensity_column: str,
        period: timedelta,
        data_spacing: timedelta,
        time_zone: str | None = None,
        time_label: Literal["start", "end"] = "start",
        scale: float = 1.0,
        replay_start: datetime | None = None,
    ) -> TraceCarbonIntensityProvider:
        """Build the provider from a CSV file with one timestamp column and one intensity column.

        The other columns are ignored.

        A file with UTC offsets in its timestamps and values in gCO2eq/kWh, stamped at the
        start of each hour:

        ```
        timestamp,carbon_intensity
        2024-07-19T00:00:00+00:00,383.4
        2024-07-19T01:00:00+00:00,379.6
        ```

        ```python
        TraceCarbonIntensityProvider.from_csv(
            "trace.csv",
            "timestamp",
            "carbon_intensity",
            period=timedelta(hours=1),
            data_spacing=timedelta(hours=1),
        )
        ```

        A file with UTC times without offsets, every 15 minutes, accounted in hourly periods
        (each hour gets the mean of its four values):

        ```
        datetime_utc,gco2_per_kwh
        2024-07-19 00:00,383.4
        2024-07-19 00:15,381.0
        ...
        ```

        ```python
        TraceCarbonIntensityProvider.from_csv(
            "trace.csv",
            "datetime_utc",
            "gco2_per_kwh",
            period=timedelta(hours=1),
            data_spacing=timedelta(minutes=15),
            time_zone="UTC",
        )
        ```

        A file with local times without offsets and values in lb/kWh, stamped at the end of
        each hour:

        ```
        Local time,CO2 intensity (lb/kWh)
        2024-07-19 01:00:00,0.84
        2024-07-19 02:00:00,0.83
        ```

        ```python
        TraceCarbonIntensityProvider.from_csv(
            "trace.csv",
            "Local time",
            "CO2 intensity (lb/kWh)",
            period=timedelta(hours=1),
            data_spacing=timedelta(hours=1),
            time_zone="America/Chicago",
            time_label="end",
            scale=453.59237,  # lb/kWh to g/kWh
        )
        ```

        Args:
            path: Path to the CSV file.
            time_column: Name of the timestamp column.
            intensity_column: Name of the carbon intensity column.
            period: Length of the periods carbon is accounted in. Must be a whole multiple
                of the data's spacing, so the data can't be coarser than `period`. Each
                period gets the mean of its data points, and periods missing any point are
                left out.
            data_spacing: Time between rows, for example one hour for hourly data.
            time_zone: IANA time zone (for example `"UTC"` or `"America/Chicago"`) for
                timestamps without an offset. Must be omitted if the timestamps have an
                offset (such as `Z`, `+00:00` or ` UTC`). Prefer UTC timestamps, since local
                times are ambiguous when daylight saving time ends.
            time_label: Whether each timestamp marks the `"start"` or the `"end"` of its interval.
            scale: Factor that converts the column to gCO2eq/kWh. For example, 453.59237
                for lb/kWh, or 1000 for t/MWh.
            replay_start: A timezone-aware time inside the trace. If given, the wall-clock
                period in which the provider is created is mapped to the trace period that
                contains this time, and later periods follow one to one.
        """
        df = pd.read_csv(path)
        for column in (time_column, intensity_column):
            if column not in df.columns:
                raise ValueError(f"Column '{column}' is not in {path}.")
        text = df[time_column].astype(str).str.strip()
        # An offset after the time of day: Z, UTC, +00, +0530 or +05:30.
        has_offset = text.str.contains(r"\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?\s*(?:Z|UTC|[+-]\d{2}(?::?\d{2})?)$")
        if has_offset.all():
            if time_zone is not None:
                raise ValueError(f"Column '{time_column}' already has UTC offsets. Do not pass `time_zone`.")
            times = _parse_times(text, utc=True)
        elif not has_offset.any():
            if time_zone is None:
                raise ValueError(f"Column '{time_column}' has no UTC offsets. Pass `time_zone`.")
            times = _parse_times(text).dt.tz_localize(time_zone)
        else:
            raise ValueError(f"Column '{time_column}' mixes times with and without UTC offsets.")
        return cls._from_series(times, df[intensity_column], period, data_spacing, scale, time_label, replay_start)

    @classmethod
    def from_electricitymaps_csv(
        cls,
        path: str | Path,
        period: timedelta,
        data_spacing: timedelta,
        emission_factor_type: Literal["direct", "lifecycle"] = "direct",
        replay_start: datetime | None = None,
    ) -> TraceCarbonIntensityProvider:
        """Load a CSV file exported from Electricity Maps.

        Reads `Datetime (UTC)` and the direct or lifecycle carbon intensity column. Both
        the older headers (`Carbon Intensity gCOâ‚‚eq/kWh (LCA)`) and the newer headers
        (`Carbon intensity gCOâ‚‚eq/kWh (Life cycle)`) work. Use the hourly exports, with
        `data_spacing=timedelta(hours=1)`.

        Args:
            path: Path to the CSV file.
            period: Length of the periods carbon is accounted in. Must be a whole multiple
                of the data's spacing, so the data can't be coarser than `period`. Each
                period gets the mean of its data points, and periods missing any point are
                left out.
            data_spacing: Time between rows, for example one hour for the hourly exports.
            emission_factor_type: `"direct"` or `"lifecycle"`. The default is `"direct"`, as in
                `ElectrictyMapsClient`.
            replay_start: A timezone-aware time inside the trace. If given, the wall-clock
                period in which the provider is created is mapped to the trace period that
                contains this time, and later periods follow one to one.
        """
        df = pd.read_csv(path)
        names = {
            "direct": ("Carbon intensity gCOâ‚‚eq/kWh (direct)", "Carbon Intensity gCOâ‚‚eq/kWh (direct)"),
            "lifecycle": ("Carbon intensity gCOâ‚‚eq/kWh (Life cycle)", "Carbon Intensity gCOâ‚‚eq/kWh (LCA)"),
        }[emission_factor_type]
        column = next((c for c in names if c in df.columns), None)
        if column is None or "Datetime (UTC)" not in df.columns:
            raise ValueError(
                f"{path} does not look like an Electricity Maps CSV: expected 'Datetime (UTC)' and one of {names}."
            )
        times = pd.to_datetime(df["Datetime (UTC)"], utc=True)
        return cls._from_series(times, df[column], period, data_spacing, replay_start=replay_start)

    @classmethod
    def from_eia930_xlsx(
        cls,
        path: str | Path,
        period: timedelta,
        electricity: Literal["consumed", "generated"] = "consumed",
        replay_start: datetime | None = None,
    ) -> TraceCarbonIntensityProvider:
        """Load a balancing-authority workbook from the U.S. EIA Hourly Electric Grid Monitor.

        These are the `.xlsx` files under Download Data, Balancing Authority/Regional
        files (for example `ERCO.xlsx`). The `Published Hourly Data` sheet has `UTC time`
        (the end of each hour) and the CO2 emissions intensity of consumed or generated
        electricity in lb/kWh (direct emissions, from July 2018). Hours without a CO2
        estimate are skipped. Reading `.xlsx` needs `python-calamine` (used if installed)
        or `openpyxl`.

        Args:
            path: Path to the workbook.
            period: Length of the periods carbon is accounted in. Must be a whole multiple
                of the data's spacing, so the data can't be coarser than `period`. Each
                period gets the mean of its data points, and periods missing any point are
                left out.
            electricity: `"consumed"` (accounts for imports and exports) or `"generated"`.
            replay_start: A timezone-aware time inside the trace. If given, the wall-clock
                period in which the provider is created is mapped to the trace period that
                contains this time, and later periods follow one to one.
        """
        column = f"CO2 Emissions Intensity for {electricity.capitalize()} Electricity"
        engine = "calamine" if importlib.util.find_spec("python_calamine") else None
        try:
            df = pd.read_excel(path, sheet_name="Published Hourly Data", engine=engine)
        except ImportError as e:
            raise ImportError("Reading EIA-930 workbooks needs `pip install python-calamine` (or openpyxl).") from e
        if "UTC time" not in df.columns or column not in df.columns:
            raise ValueError(
                f"{path} does not look like an EIA-930 balancing-authority workbook: expected 'UTC time' and "
                f"'{column}' in the 'Published Hourly Data' sheet."
            )
        return cls._from_series(
            pd.to_datetime(df["UTC time"], utc=True),
            df[column],
            period,
            scale=453.59237,  # lb/kWh to g/kWh
            time_label="end",
            replay_start=replay_start,
            data_spacing=timedelta(hours=1),
        )

    @classmethod
    def from_gb_carbon_intensity_json(
        cls,
        path: str | Path,
        period: timedelta,
        value: Literal["actual", "forecast"] = "actual",
        replay_start: datetime | None = None,
    ) -> TraceCarbonIntensityProvider:
        """Load a saved response from the GB Carbon Intensity API (api.carbonintensity.org.uk).

        Takes national `/intensity` responses, such as `/intensity/{from}/{to}` for past
        data or `/intensity/{from}/fw48h` for a 48-hour forecast: 30-minute intervals with
        `from`, `to` and `intensity.actual` / `intensity.forecast` in gCO2/kWh. Intervals
        without the chosen value are skipped.

        Args:
            path: Path to the saved JSON response.
            period: Length of the periods carbon is accounted in. Must be a whole multiple
                of the data's spacing, so the data can't be coarser than `period`. Each
                period gets the mean of its data points, and periods missing any point are
                left out.
            value: `"actual"` or `"forecast"`.
            replay_start: A timezone-aware time inside the trace. If given, the wall-clock
                period in which the provider is created is mapped to the trace period that
                contains this time, and later periods follow one to one.
        """
        with open(path) as fh:
            data = json.load(fh)["data"]
        times = pd.to_datetime([d["from"] for d in data], utc=True)
        values = [d["intensity"].get(value) for d in data]
        return cls._from_series(
            pd.Series(times),
            pd.Series(values, dtype="float64"),
            period,
            replay_start=replay_start,
            data_spacing=timedelta(minutes=30),
        )

    def carbon_intensity_at(self, when: datetime) -> float:
        """Return the carbon intensity of the trace period that contains `when`, in trace time."""
        if when.tzinfo is None:
            raise ValueError("`when` must be timezone-aware.")
        start = _period_floor(when, self.period)
        try:
            return self.trace[start]
        except KeyError as e:
            first, last = next(iter(self.trace)), next(reversed(self.trace))
            message = (
                f"No carbon intensity for the period starting {start.isoformat()}. "
                f"The trace covers {first.isoformat()} to {last.isoformat()} (UTC) and may have gaps."
            )
            if self.replay_start is None:
                message += " Pass `replay_start` to replay past data."
            raise ZeusCarbonIntensityNotFoundError(message) from e

    def get_current_carbon_intensity(self) -> float:
        """Return the carbon intensity of the trace period mapped to the current wall-clock period."""
        return self.carbon_intensity_at(datetime.now(timezone.utc) + self.offset)

    def get_recent_carbon_intensity(self) -> dict[datetime, float]:
        """Return every period of the trace, keyed by the wall-clock period it is mapped to."""
        return {start - self.offset: value for start, value in self.trace.items()}

    @property
    def update_period(self) -> timedelta:
        """Length of each period."""
        return self.period

    @property
    def history_length(self) -> int:
        """Number of periods in the trace."""
        return len(self.trace)

update_period property

update_period

Length of each period.

history_length property

history_length

Number of periods in the trace.

__init__

__init__(
    intensities, period, data_spacing, replay_start=None
)

Parameters:

Name Type Description Default
intensities Mapping[datetime, float]

Maps the start of each data point (timezone-aware) to the carbon intensity in gCO2eq/kWh. Values must be finite and non-negative. Every timestamp must be on a UTC grid of data_spacing (for example, the top of each hour for hourly data). Gaps are allowed.

required
period timedelta

Length of the periods carbon is accounted in. Must be a whole multiple of the data's spacing, so the data can't be coarser than period. Each period gets the mean of its data points, and periods missing any point are left out.

required
data_spacing timedelta

Time between data points, for example one hour for hourly data.

required
replay_start datetime | None

A timezone-aware time inside the trace. If given, the wall-clock period in which the provider is created is mapped to the trace period that contains this time, and later periods follow one to one.

None
Source code in zeus/monitor/carbon.py
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
def __init__(
    self,
    intensities: Mapping[datetime, float],
    period: timedelta,
    data_spacing: timedelta,
    replay_start: datetime | None = None,
) -> None:
    """Initialize the provider.

    Args:
        intensities: Maps the start of each data point (timezone-aware) to the carbon
            intensity in gCO2eq/kWh. Values must be finite and non-negative. Every timestamp
            must be on a UTC grid of `data_spacing` (for example, the top of each hour for
            hourly data). Gaps are allowed.
        period: Length of the periods carbon is accounted in. Must be a whole multiple
            of the data's spacing, so the data can't be coarser than `period`. Each
            period gets the mean of its data points, and periods missing any point are
            left out.
        data_spacing: Time between data points, for example one hour for hourly data.
        replay_start: A timezone-aware time inside the trace. If given, the wall-clock
            period in which the provider is created is mapped to the trace period that
            contains this time, and later periods follow one to one.
    """
    if period <= timedelta(0):
        raise ValueError("`period` must be positive.")
    if data_spacing <= timedelta(0):
        raise ValueError("`data_spacing` must be positive.")
    if data_spacing > period:
        raise ValueError(
            f"The data is coarser than `period`: `data_spacing` is {data_spacing}, but `period` is {period}."
        )
    if period % data_spacing:
        raise ValueError(f"`period` ({period}) must be a whole multiple of `data_spacing` ({data_spacing}).")
    if not intensities:
        raise ValueError("The carbon intensity trace is empty.")
    points: dict[datetime, float] = {}
    for start, value in intensities.items():
        if start.tzinfo is None:
            raise ValueError(f"Trace time {start} has no timezone. Use timezone-aware times.")
        if not math.isfinite(value) or value < 0:
            raise ValueError(f"Carbon intensity {value} at {start} is not a finite non-negative number.")
        points[start.astimezone(timezone.utc)] = float(value)

    starts = sorted(points)
    for start in starts:
        if _period_floor(start, data_spacing) != start:
            raise ValueError(
                f"Trace time {start} is not on the {data_spacing} grid (for example, the top of each hour "
                "for hourly data)."
            )

    per_period = period // data_spacing
    grouped: dict[datetime, list[float]] = defaultdict(list)
    for start in starts:
        grouped[_period_floor(start, period)].append(points[start])
    self.period = period
    self.trace = {p: sum(v) / len(v) for p, v in sorted(grouped.items()) if len(v) == per_period}
    if not self.trace:
        raise ValueError(f"No {period} period is fully covered by the data.")
    if len(self.trace) < len(grouped):
        logger.warning(
            "Left out %d of %d periods of length %s that the data covers only partly.",
            len(grouped) - len(self.trace),
            len(grouped),
            period,
        )

    self.replay_start = replay_start
    self.offset = timedelta(0)
    if replay_start is None:
        now = _period_floor(datetime.now(timezone.utc), period)
        if now not in self.trace:
            logger.warning(
                "The trace has no value for the current period (%s). Pass `replay_start` to replay past data.",
                now.isoformat(),
            )
    else:
        if replay_start.tzinfo is None:
            raise ValueError("`replay_start` must be timezone-aware.")
        start = _period_floor(replay_start, period)
        if start not in self.trace:
            raise ValueError(f"`replay_start` {replay_start} is not inside the trace.")
        self.offset = start - _period_floor(datetime.now(timezone.utc), period)

_from_series classmethod

_from_series(
    times,
    values,
    period,
    data_spacing,
    scale=1.0,
    time_label="start",
    replay_start=None,
)

Build the provider from timezone-aware times and intensities, skipping empty values.

Source code in zeus/monitor/carbon.py
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
@classmethod
def _from_series(
    cls,
    times: pd.Series,
    values: pd.Series,
    period: timedelta,
    data_spacing: timedelta,
    scale: float = 1.0,
    time_label: Literal["start", "end"] = "start",
    replay_start: datetime | None = None,
) -> TraceCarbonIntensityProvider:
    """Build the provider from timezone-aware times and intensities, skipping empty values."""
    times = pd.Series(pd.to_datetime(times, utc=True)).reset_index(drop=True)
    values = pd.to_numeric(pd.Series(values), errors="coerce").reset_index(drop=True) * scale
    keep = values.notna()
    times, values = times[keep], values[keep]
    if times.duplicated().any():
        raise ValueError("The trace has repeated timestamps.")
    if time_label == "end":
        # Each timestamp ends one data interval, so move it back by `data_spacing`.
        times = times - data_spacing
    trace = {t.to_pydatetime(): float(v) for t, v in zip(times, values)}
    return cls(trace, period=period, data_spacing=data_spacing, replay_start=replay_start)

from_csv classmethod

from_csv(
    path,
    time_column,
    intensity_column,
    period,
    data_spacing,
    time_zone=None,
    time_label="start",
    scale=1.0,
    replay_start=None,
)

Build the provider from a CSV file with one timestamp column and one intensity column.

The other columns are ignored.

A file with UTC offsets in its timestamps and values in gCO2eq/kWh, stamped at the start of each hour:

timestamp,carbon_intensity
2024-07-19T00:00:00+00:00,383.4
2024-07-19T01:00:00+00:00,379.6
TraceCarbonIntensityProvider.from_csv(
    "trace.csv",
    "timestamp",
    "carbon_intensity",
    period=timedelta(hours=1),
    data_spacing=timedelta(hours=1),
)

A file with UTC times without offsets, every 15 minutes, accounted in hourly periods (each hour gets the mean of its four values):

datetime_utc,gco2_per_kwh
2024-07-19 00:00,383.4
2024-07-19 00:15,381.0
...
TraceCarbonIntensityProvider.from_csv(
    "trace.csv",
    "datetime_utc",
    "gco2_per_kwh",
    period=timedelta(hours=1),
    data_spacing=timedelta(minutes=15),
    time_zone="UTC",
)

A file with local times without offsets and values in lb/kWh, stamped at the end of each hour:

Local time,CO2 intensity (lb/kWh)
2024-07-19 01:00:00,0.84
2024-07-19 02:00:00,0.83
TraceCarbonIntensityProvider.from_csv(
    "trace.csv",
    "Local time",
    "CO2 intensity (lb/kWh)",
    period=timedelta(hours=1),
    data_spacing=timedelta(hours=1),
    time_zone="America/Chicago",
    time_label="end",
    scale=453.59237,  # lb/kWh to g/kWh
)

Parameters:

Name Type Description Default
path str | Path

Path to the CSV file.

required
time_column str

Name of the timestamp column.

required
intensity_column str

Name of the carbon intensity column.

required
period timedelta

Length of the periods carbon is accounted in. Must be a whole multiple of the data's spacing, so the data can't be coarser than period. Each period gets the mean of its data points, and periods missing any point are left out.

required
data_spacing timedelta

Time between rows, for example one hour for hourly data.

required
time_zone str | None

IANA time zone (for example "UTC" or "America/Chicago") for timestamps without an offset. Must be omitted if the timestamps have an offset (such as Z, +00:00 or UTC). Prefer UTC timestamps, since local times are ambiguous when daylight saving time ends.

None
time_label Literal['start', 'end']

Whether each timestamp marks the "start" or the "end" of its interval.

'start'
scale float

Factor that converts the column to gCO2eq/kWh. For example, 453.59237 for lb/kWh, or 1000 for t/MWh.

1.0
replay_start datetime | None

A timezone-aware time inside the trace. If given, the wall-clock period in which the provider is created is mapped to the trace period that contains this time, and later periods follow one to one.

None
Source code in zeus/monitor/carbon.py
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
@classmethod
def from_csv(
    cls,
    path: str | Path,
    time_column: str,
    intensity_column: str,
    period: timedelta,
    data_spacing: timedelta,
    time_zone: str | None = None,
    time_label: Literal["start", "end"] = "start",
    scale: float = 1.0,
    replay_start: datetime | None = None,
) -> TraceCarbonIntensityProvider:
    """Build the provider from a CSV file with one timestamp column and one intensity column.

    The other columns are ignored.

    A file with UTC offsets in its timestamps and values in gCO2eq/kWh, stamped at the
    start of each hour:

    ```
    timestamp,carbon_intensity
    2024-07-19T00:00:00+00:00,383.4
    2024-07-19T01:00:00+00:00,379.6
    ```

    ```python
    TraceCarbonIntensityProvider.from_csv(
        "trace.csv",
        "timestamp",
        "carbon_intensity",
        period=timedelta(hours=1),
        data_spacing=timedelta(hours=1),
    )
    ```

    A file with UTC times without offsets, every 15 minutes, accounted in hourly periods
    (each hour gets the mean of its four values):

    ```
    datetime_utc,gco2_per_kwh
    2024-07-19 00:00,383.4
    2024-07-19 00:15,381.0
    ...
    ```

    ```python
    TraceCarbonIntensityProvider.from_csv(
        "trace.csv",
        "datetime_utc",
        "gco2_per_kwh",
        period=timedelta(hours=1),
        data_spacing=timedelta(minutes=15),
        time_zone="UTC",
    )
    ```

    A file with local times without offsets and values in lb/kWh, stamped at the end of
    each hour:

    ```
    Local time,CO2 intensity (lb/kWh)
    2024-07-19 01:00:00,0.84
    2024-07-19 02:00:00,0.83
    ```

    ```python
    TraceCarbonIntensityProvider.from_csv(
        "trace.csv",
        "Local time",
        "CO2 intensity (lb/kWh)",
        period=timedelta(hours=1),
        data_spacing=timedelta(hours=1),
        time_zone="America/Chicago",
        time_label="end",
        scale=453.59237,  # lb/kWh to g/kWh
    )
    ```

    Args:
        path: Path to the CSV file.
        time_column: Name of the timestamp column.
        intensity_column: Name of the carbon intensity column.
        period: Length of the periods carbon is accounted in. Must be a whole multiple
            of the data's spacing, so the data can't be coarser than `period`. Each
            period gets the mean of its data points, and periods missing any point are
            left out.
        data_spacing: Time between rows, for example one hour for hourly data.
        time_zone: IANA time zone (for example `"UTC"` or `"America/Chicago"`) for
            timestamps without an offset. Must be omitted if the timestamps have an
            offset (such as `Z`, `+00:00` or ` UTC`). Prefer UTC timestamps, since local
            times are ambiguous when daylight saving time ends.
        time_label: Whether each timestamp marks the `"start"` or the `"end"` of its interval.
        scale: Factor that converts the column to gCO2eq/kWh. For example, 453.59237
            for lb/kWh, or 1000 for t/MWh.
        replay_start: A timezone-aware time inside the trace. If given, the wall-clock
            period in which the provider is created is mapped to the trace period that
            contains this time, and later periods follow one to one.
    """
    df = pd.read_csv(path)
    for column in (time_column, intensity_column):
        if column not in df.columns:
            raise ValueError(f"Column '{column}' is not in {path}.")
    text = df[time_column].astype(str).str.strip()
    # An offset after the time of day: Z, UTC, +00, +0530 or +05:30.
    has_offset = text.str.contains(r"\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?\s*(?:Z|UTC|[+-]\d{2}(?::?\d{2})?)$")
    if has_offset.all():
        if time_zone is not None:
            raise ValueError(f"Column '{time_column}' already has UTC offsets. Do not pass `time_zone`.")
        times = _parse_times(text, utc=True)
    elif not has_offset.any():
        if time_zone is None:
            raise ValueError(f"Column '{time_column}' has no UTC offsets. Pass `time_zone`.")
        times = _parse_times(text).dt.tz_localize(time_zone)
    else:
        raise ValueError(f"Column '{time_column}' mixes times with and without UTC offsets.")
    return cls._from_series(times, df[intensity_column], period, data_spacing, scale, time_label, replay_start)

from_electricitymaps_csv classmethod

from_electricitymaps_csv(
    path,
    period,
    data_spacing,
    emission_factor_type="direct",
    replay_start=None,
)

Load a CSV file exported from Electricity Maps.

Reads Datetime (UTC) and the direct or lifecycle carbon intensity column. Both the older headers (Carbon Intensity gCOâ‚‚eq/kWh (LCA)) and the newer headers (Carbon intensity gCOâ‚‚eq/kWh (Life cycle)) work. Use the hourly exports, with data_spacing=timedelta(hours=1).

Parameters:

Name Type Description Default
path str | Path

Path to the CSV file.

required
period timedelta

Length of the periods carbon is accounted in. Must be a whole multiple of the data's spacing, so the data can't be coarser than period. Each period gets the mean of its data points, and periods missing any point are left out.

required
data_spacing timedelta

Time between rows, for example one hour for the hourly exports.

required
emission_factor_type Literal['direct', 'lifecycle']

"direct" or "lifecycle". The default is "direct", as in ElectrictyMapsClient.

'direct'
replay_start datetime | None

A timezone-aware time inside the trace. If given, the wall-clock period in which the provider is created is mapped to the trace period that contains this time, and later periods follow one to one.

None
Source code in zeus/monitor/carbon.py
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
@classmethod
def from_electricitymaps_csv(
    cls,
    path: str | Path,
    period: timedelta,
    data_spacing: timedelta,
    emission_factor_type: Literal["direct", "lifecycle"] = "direct",
    replay_start: datetime | None = None,
) -> TraceCarbonIntensityProvider:
    """Load a CSV file exported from Electricity Maps.

    Reads `Datetime (UTC)` and the direct or lifecycle carbon intensity column. Both
    the older headers (`Carbon Intensity gCOâ‚‚eq/kWh (LCA)`) and the newer headers
    (`Carbon intensity gCOâ‚‚eq/kWh (Life cycle)`) work. Use the hourly exports, with
    `data_spacing=timedelta(hours=1)`.

    Args:
        path: Path to the CSV file.
        period: Length of the periods carbon is accounted in. Must be a whole multiple
            of the data's spacing, so the data can't be coarser than `period`. Each
            period gets the mean of its data points, and periods missing any point are
            left out.
        data_spacing: Time between rows, for example one hour for the hourly exports.
        emission_factor_type: `"direct"` or `"lifecycle"`. The default is `"direct"`, as in
            `ElectrictyMapsClient`.
        replay_start: A timezone-aware time inside the trace. If given, the wall-clock
            period in which the provider is created is mapped to the trace period that
            contains this time, and later periods follow one to one.
    """
    df = pd.read_csv(path)
    names = {
        "direct": ("Carbon intensity gCOâ‚‚eq/kWh (direct)", "Carbon Intensity gCOâ‚‚eq/kWh (direct)"),
        "lifecycle": ("Carbon intensity gCOâ‚‚eq/kWh (Life cycle)", "Carbon Intensity gCOâ‚‚eq/kWh (LCA)"),
    }[emission_factor_type]
    column = next((c for c in names if c in df.columns), None)
    if column is None or "Datetime (UTC)" not in df.columns:
        raise ValueError(
            f"{path} does not look like an Electricity Maps CSV: expected 'Datetime (UTC)' and one of {names}."
        )
    times = pd.to_datetime(df["Datetime (UTC)"], utc=True)
    return cls._from_series(times, df[column], period, data_spacing, replay_start=replay_start)

from_eia930_xlsx classmethod

from_eia930_xlsx(
    path, period, electricity="consumed", replay_start=None
)

Load a balancing-authority workbook from the U.S. EIA Hourly Electric Grid Monitor.

These are the .xlsx files under Download Data, Balancing Authority/Regional files (for example ERCO.xlsx). The Published Hourly Data sheet has UTC time (the end of each hour) and the CO2 emissions intensity of consumed or generated electricity in lb/kWh (direct emissions, from July 2018). Hours without a CO2 estimate are skipped. Reading .xlsx needs python-calamine (used if installed) or openpyxl.

Parameters:

Name Type Description Default
path str | Path

Path to the workbook.

required
period timedelta

Length of the periods carbon is accounted in. Must be a whole multiple of the data's spacing, so the data can't be coarser than period. Each period gets the mean of its data points, and periods missing any point are left out.

required
electricity Literal['consumed', 'generated']

"consumed" (accounts for imports and exports) or "generated".

'consumed'
replay_start datetime | None

A timezone-aware time inside the trace. If given, the wall-clock period in which the provider is created is mapped to the trace period that contains this time, and later periods follow one to one.

None
Source code in zeus/monitor/carbon.py
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
@classmethod
def from_eia930_xlsx(
    cls,
    path: str | Path,
    period: timedelta,
    electricity: Literal["consumed", "generated"] = "consumed",
    replay_start: datetime | None = None,
) -> TraceCarbonIntensityProvider:
    """Load a balancing-authority workbook from the U.S. EIA Hourly Electric Grid Monitor.

    These are the `.xlsx` files under Download Data, Balancing Authority/Regional
    files (for example `ERCO.xlsx`). The `Published Hourly Data` sheet has `UTC time`
    (the end of each hour) and the CO2 emissions intensity of consumed or generated
    electricity in lb/kWh (direct emissions, from July 2018). Hours without a CO2
    estimate are skipped. Reading `.xlsx` needs `python-calamine` (used if installed)
    or `openpyxl`.

    Args:
        path: Path to the workbook.
        period: Length of the periods carbon is accounted in. Must be a whole multiple
            of the data's spacing, so the data can't be coarser than `period`. Each
            period gets the mean of its data points, and periods missing any point are
            left out.
        electricity: `"consumed"` (accounts for imports and exports) or `"generated"`.
        replay_start: A timezone-aware time inside the trace. If given, the wall-clock
            period in which the provider is created is mapped to the trace period that
            contains this time, and later periods follow one to one.
    """
    column = f"CO2 Emissions Intensity for {electricity.capitalize()} Electricity"
    engine = "calamine" if importlib.util.find_spec("python_calamine") else None
    try:
        df = pd.read_excel(path, sheet_name="Published Hourly Data", engine=engine)
    except ImportError as e:
        raise ImportError("Reading EIA-930 workbooks needs `pip install python-calamine` (or openpyxl).") from e
    if "UTC time" not in df.columns or column not in df.columns:
        raise ValueError(
            f"{path} does not look like an EIA-930 balancing-authority workbook: expected 'UTC time' and "
            f"'{column}' in the 'Published Hourly Data' sheet."
        )
    return cls._from_series(
        pd.to_datetime(df["UTC time"], utc=True),
        df[column],
        period,
        scale=453.59237,  # lb/kWh to g/kWh
        time_label="end",
        replay_start=replay_start,
        data_spacing=timedelta(hours=1),
    )

from_gb_carbon_intensity_json classmethod

from_gb_carbon_intensity_json(
    path, period, value="actual", replay_start=None
)

Load a saved response from the GB Carbon Intensity API (api.carbonintensity.org.uk).

Takes national /intensity responses, such as /intensity/{from}/{to} for past data or /intensity/{from}/fw48h for a 48-hour forecast: 30-minute intervals with from, to and intensity.actual / intensity.forecast in gCO2/kWh. Intervals without the chosen value are skipped.

Parameters:

Name Type Description Default
path str | Path

Path to the saved JSON response.

required
period timedelta

Length of the periods carbon is accounted in. Must be a whole multiple of the data's spacing, so the data can't be coarser than period. Each period gets the mean of its data points, and periods missing any point are left out.

required
value Literal['actual', 'forecast']

"actual" or "forecast".

'actual'
replay_start datetime | None

A timezone-aware time inside the trace. If given, the wall-clock period in which the provider is created is mapped to the trace period that contains this time, and later periods follow one to one.

None
Source code in zeus/monitor/carbon.py
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
@classmethod
def from_gb_carbon_intensity_json(
    cls,
    path: str | Path,
    period: timedelta,
    value: Literal["actual", "forecast"] = "actual",
    replay_start: datetime | None = None,
) -> TraceCarbonIntensityProvider:
    """Load a saved response from the GB Carbon Intensity API (api.carbonintensity.org.uk).

    Takes national `/intensity` responses, such as `/intensity/{from}/{to}` for past
    data or `/intensity/{from}/fw48h` for a 48-hour forecast: 30-minute intervals with
    `from`, `to` and `intensity.actual` / `intensity.forecast` in gCO2/kWh. Intervals
    without the chosen value are skipped.

    Args:
        path: Path to the saved JSON response.
        period: Length of the periods carbon is accounted in. Must be a whole multiple
            of the data's spacing, so the data can't be coarser than `period`. Each
            period gets the mean of its data points, and periods missing any point are
            left out.
        value: `"actual"` or `"forecast"`.
        replay_start: A timezone-aware time inside the trace. If given, the wall-clock
            period in which the provider is created is mapped to the trace period that
            contains this time, and later periods follow one to one.
    """
    with open(path) as fh:
        data = json.load(fh)["data"]
    times = pd.to_datetime([d["from"] for d in data], utc=True)
    values = [d["intensity"].get(value) for d in data]
    return cls._from_series(
        pd.Series(times),
        pd.Series(values, dtype="float64"),
        period,
        replay_start=replay_start,
        data_spacing=timedelta(minutes=30),
    )

carbon_intensity_at

carbon_intensity_at(when)

Return the carbon intensity of the trace period that contains when, in trace time.

Source code in zeus/monitor/carbon.py
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
def carbon_intensity_at(self, when: datetime) -> float:
    """Return the carbon intensity of the trace period that contains `when`, in trace time."""
    if when.tzinfo is None:
        raise ValueError("`when` must be timezone-aware.")
    start = _period_floor(when, self.period)
    try:
        return self.trace[start]
    except KeyError as e:
        first, last = next(iter(self.trace)), next(reversed(self.trace))
        message = (
            f"No carbon intensity for the period starting {start.isoformat()}. "
            f"The trace covers {first.isoformat()} to {last.isoformat()} (UTC) and may have gaps."
        )
        if self.replay_start is None:
            message += " Pass `replay_start` to replay past data."
        raise ZeusCarbonIntensityNotFoundError(message) from e

get_current_carbon_intensity

get_current_carbon_intensity()

Return the carbon intensity of the trace period mapped to the current wall-clock period.

Source code in zeus/monitor/carbon.py
574
575
576
def get_current_carbon_intensity(self) -> float:
    """Return the carbon intensity of the trace period mapped to the current wall-clock period."""
    return self.carbon_intensity_at(datetime.now(timezone.utc) + self.offset)

get_recent_carbon_intensity

get_recent_carbon_intensity()

Return every period of the trace, keyed by the wall-clock period it is mapped to.

Source code in zeus/monitor/carbon.py
578
579
580
def get_recent_carbon_intensity(self) -> dict[datetime, float]:
    """Return every period of the trace, keyed by the wall-clock period it is mapped to."""
    return {start - self.offset: value for start, value in self.trace.items()}

CarbonEmissionMeasurement dataclass

Measurement result of one window.

Attributes:

Name Type Description
time float

Time elapsed (in seconds) during the measurement window.

gpu_energy dict[int, float]

Maps GPU indices to the energy consumed (in Joules) during the measurement window. GPU indices are from the DL framework's perspective after applying CUDA_VISIBLE_DEVICES.

gpu_carbon_emission dict[int, float]

Maps GPU indices to the carbon emission produced (in gCO2eq) during the measurement window. GPU indices are from the DL framework's perspective after applying CUDA_VISIBLE_DEVICES.

cpu_energy dict[int, float] | None

Maps CPU indices to the energy consumed (in Joules) during the measurement window. Each CPU index refers to one powerzone exposed by RAPL (intel-rapl:d). This can be 'None' if CPU measurement is not available.

cpu_carbon_emission dict[int, float] | None

Maps CPU indices to the carbon emission produced (in gCO2eq) during the measurement window. Each CPU index refers to one powerzone exposed by RAPL (intel-rapl:d). This can be 'None' if CPU measurement is not available.

dram_energy dict[int, float] | None

Maps CPU indices to the energy consumed (in Joules) during the measurement window. Each CPU index refers to one powerzone exposed by RAPL (intel-rapl:d) and DRAM measurements are taken from sub-packages within each powerzone. This can be 'None' if CPU measurement is not available or DRAM measurement is not available.

dram_carbon_emission dict[int, float] | None

Maps CPU indices to the carbon emission produced (in gCO2eq) during the measurement window. Each CPU index refers to one powerzone exposed by RAPL (intel-rapl:d). This can be 'None' if CPU measurement is not available or DRAM measurement is not available.

Source code in zeus/monitor/carbon.py
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
@dataclass
class CarbonEmissionMeasurement:
    """Measurement result of one window.

    Attributes:
        time: Time elapsed (in seconds) during the measurement window.
        gpu_energy: Maps GPU indices to the energy consumed (in Joules) during the
            measurement window. GPU indices are from the DL framework's perspective
            after applying `CUDA_VISIBLE_DEVICES`.
        gpu_carbon_emission: Maps GPU indices to the carbon emission produced (in gCO2eq) during the
            measurement window. GPU indices are from the DL framework's perspective
            after applying `CUDA_VISIBLE_DEVICES`.
        cpu_energy: Maps CPU indices to the energy consumed (in Joules) during the measurement
            window. Each CPU index refers to one powerzone exposed by RAPL (intel-rapl:d). This can
            be 'None' if CPU measurement is not available.
        cpu_carbon_emission: Maps CPU indices to the carbon emission produced (in gCO2eq) during the measurement
            window. Each CPU index refers to one powerzone exposed by RAPL (intel-rapl:d). This can
            be 'None' if CPU measurement is not available.
        dram_energy: Maps CPU indices to the energy consumed (in Joules) during the measurement
            window. Each CPU index refers to one powerzone exposed by RAPL (intel-rapl:d) and DRAM
            measurements are taken from sub-packages within each powerzone. This can be 'None' if
            CPU measurement is not available or DRAM measurement is not available.
        dram_carbon_emission: Maps CPU indices to the carbon emission produced (in gCO2eq) during the measurement
            window. Each CPU index refers to one powerzone exposed by RAPL (intel-rapl:d). This can be 'None' if
            CPU measurement is not available or DRAM measurement is not available.
    """

    time: float
    gpu_energy: dict[int, float]
    gpu_carbon_emission: dict[int, float]
    cpu_energy: dict[int, float] | None = None
    cpu_carbon_emission: dict[int, float] | None = None
    dram_energy: dict[int, float] | None = None
    dram_carbon_emission: dict[int, float] | None = None

Op

Bases: Enum

Enum used to communicate between CarbonEmissionMonitor and _polling_process.

Source code in zeus/monitor/carbon.py
648
649
650
651
652
653
class Op(Enum):
    """Enum used to communicate between CarbonEmissionMonitor and _polling_process."""

    BEGIN = 0
    END = 1
    NEXTITER = 2

CarbonEmissionMonitor

Measure the carbon emission, GPU energy, and time consumption of a block of code.

Works for multi-GPU and heterogeneous GPU types. Aware of CUDA_VISIBLE_DEVICES. For instance, if CUDA_VISIBLE_DEVICES=2,3, GPU index 1 passed into gpu_indices will be interpreted as CUDA device 3.

You can mark the beginning and end of a measurement window, during which the carbon emission, GPU energy, and time consumed will be recorded. Multiple concurrent measurement windows are supported.

Note

carbon_intensity_provider must have estimate turned on because during some hours, ElectricityMaps does not have carbon intensity values available and has to rely on estimation.

Warning

This monitor uses multiprocessing with the spawn start method to measure energy and carbon emissions in a background process. Spawned processes re-import your main module, so keep heavy setup under if __name__ == "__main__": or inside functions. See also the "Safe importing of main module" section in the Python documentation.

Source code in zeus/monitor/carbon.py
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
class CarbonEmissionMonitor:
    """Measure the carbon emission, GPU energy, and time consumption of a block of code.

    Works for multi-GPU and heterogeneous GPU types. Aware of `CUDA_VISIBLE_DEVICES`.
    For instance, if `CUDA_VISIBLE_DEVICES=2,3`, GPU index `1` passed into `gpu_indices`
    will be interpreted as CUDA device `3`.

    You can mark the beginning and end of a measurement window, during which the carbon
    emission, GPU energy, and time consumed will be recorded. Multiple concurrent
    measurement windows are supported.

    !!! Note
        `carbon_intensity_provider` must have `estimate` turned on because during some hours,
        ElectricityMaps does not have carbon intensity values available and has to rely on
        estimation.

    !!! Warning
        This monitor uses multiprocessing with the spawn start method to measure energy and carbon emissions
        in a background process. Spawned processes re-import your main module, so keep
        heavy setup under `if __name__ == "__main__":` or inside functions.
        See also the "Safe importing of main module" section in the [Python documentation](
        https://docs.python.org/3/library/multiprocessing.html#the-spawn-and-forkserver-start-methods).
    """

    def __init__(
        self,
        carbon_intensity_provider: CarbonIntensityProvider,
        gpu_indices: list[int] | None = None,
        cpu_indices: list[int] | None = None,
        sync_execution_with: Literal["torch", "jax", "cupy"] = "torch",
    ) -> None:
        """Initializes Carbon Emission Monitor.

        Args:
            carbon_intensity_provider: provider for which carbon intensity values will be fetched from
            gpu_indices: Indices of all the CUDA devices to monitor. Time/Energy measurements
                will begin and end at the same time for these GPUs (i.e., synchronized).
                If None, all the GPUs available will be used. `CUDA_VISIBLE_DEVICES`
                is respected if set, e.g., GPU index `1` passed into `gpu_indices` when
                `CUDA_VISIBLE_DEVICES=2,3` will be interpreted as CUDA device `3`.
                `CUDA_VISIBLE_DEVICES`s formatted with comma-separated indices are supported.
            cpu_indices: Indices of the CPU packages to monitor. If None, all CPU packages will
                be used.
            sync_execution_with: Deep learning framework to use to synchronize CPU/GPU computations.
                Defaults to `"torch"`, in which case `torch.cuda.synchronize` will be used.
                See [`sync_execution`][zeus.utils.framework.sync_execution] for more details.
        """
        # Warn if instantiated as a global variable in a subprocess.
        warn_if_global_in_subprocess(self)

        self.zeus_monitor = ZeusMonitor(
            gpu_indices=gpu_indices,
            cpu_indices=cpu_indices,
            sync_execution_with=sync_execution_with,
        )
        self.carbon_intensity_provider = carbon_intensity_provider
        self.current_keys = set()

        # set up process and shared queues
        self.context = mp.get_context("spawn")
        self.command_q = self.context.Queue()
        self.finished_q = self.context.Queue()
        self._stop_poller: mp_util.Finalize | None = None

    def begin_window(self, key: str, sync_execution: bool = True) -> None:
        """Begin a new measurement window.

        Args:
            key: Unique name of the measurement window.
            sync_execution: Whether to wait for asynchronously dispatched computations
                to finish before starting the measurement window. For instance, PyTorch
                and JAX will run GPU computations asynchronously, and waiting them to
                finish is necessary to ensure that the measurement window captures all
                and only the computations dispatched within the window.
        """
        # check if key is already used
        if key in self.current_keys:
            raise ValueError(f"Measurement window '{key}' already exists")
        self.current_keys.add(key)

        # start window
        self.zeus_monitor.begin_window(key, sync_execution=sync_execution)

        # if there were previously no active windows, start polling process
        if len(self.current_keys) == 1:
            self.polling_process = self.context.Process(
                target=_polling_process,
                args=(
                    self.command_q,
                    self.finished_q,
                    self.zeus_monitor.gpu_indices,
                    self.zeus_monitor.cpu_indices,
                    self.carbon_intensity_provider,
                ),
            )
            self.polling_process.start()

            # If user code raises inside a window, Op.END never arrives and the poller blocks exit.
            # exitpriority=0 runs this before multiprocessing joins child processes.
            if self._stop_poller is not None:
                self._stop_poller.cancel()  # the previous poller has exited
            self._stop_poller = mp_util.Finalize(
                self, _stop_polling_process, args=(self.polling_process,), exitpriority=0
            )

        # start subwindows
        self.command_q.put((Op.BEGIN, key))

    def end_window(self, key: str, sync_execution: bool = True) -> CarbonEmissionMeasurement:
        """End a measurement window and return the time, energy consumption, and carbon emission.

        Args:
            key: Name of an active measurement window.
            sync_execution: Whether to wait for asynchronously dispatched computations
                to finish before starting the measurement window. For instance, PyTorch
                and JAX will run GPU computations asynchronously, and waiting them to
                finish is necessary to ensure that the measurement window captures all
                and only the computations dispatched within the window.
        """
        # check if begin_window has been called with key before
        if key not in self.current_keys:
            raise ValueError(f"Measurement window '{key}' does not exist")

        # end window
        self.command_q.put((Op.END, key))
        try:
            (
                gpu_carbon_emissions,
                cpu_carbon_emissions,
                dram_carbon_emissions,
            ) = get_or_raise_if_dead(self.finished_q, self.polling_process)
        except RuntimeError:
            # The polling process is gone, and with it the data of every open window. Drop them,
            # and use new queues so the next poller doesn't read commands meant for this one.
            for open_key in self.current_keys:
                self.zeus_monitor.end_window(open_key, sync_execution=False, cancel=True)
            self.current_keys.clear()
            self.command_q = self.context.Queue()
            self.finished_q = self.context.Queue()
            raise
        self.current_keys.remove(key)

        overall_measurement = self.zeus_monitor.end_window(key, sync_execution=sync_execution)

        measurement = CarbonEmissionMeasurement(
            time=overall_measurement.time,
            gpu_energy=overall_measurement.gpu_energy,
            cpu_energy=overall_measurement.cpu_energy,
            dram_energy=overall_measurement.dram_energy,
            gpu_carbon_emission=gpu_carbon_emissions,
            cpu_carbon_emission=cpu_carbon_emissions or None,
            dram_carbon_emission=dram_carbon_emissions or None,
        )

        return measurement

__init__

__init__(
    carbon_intensity_provider,
    gpu_indices=None,
    cpu_indices=None,
    sync_execution_with="torch",
)

Parameters:

Name Type Description Default
carbon_intensity_provider CarbonIntensityProvider

provider for which carbon intensity values will be fetched from

required
gpu_indices list[int] | None

Indices of all the CUDA devices to monitor. Time/Energy measurements will begin and end at the same time for these GPUs (i.e., synchronized). If None, all the GPUs available will be used. CUDA_VISIBLE_DEVICES is respected if set, e.g., GPU index 1 passed into gpu_indices when CUDA_VISIBLE_DEVICES=2,3 will be interpreted as CUDA device 3. CUDA_VISIBLE_DEVICESs formatted with comma-separated indices are supported.

None
cpu_indices list[int] | None

Indices of the CPU packages to monitor. If None, all CPU packages will be used.

None
sync_execution_with Literal['torch', 'jax', 'cupy']

Deep learning framework to use to synchronize CPU/GPU computations. Defaults to "torch", in which case torch.cuda.synchronize will be used. See sync_execution for more details.

'torch'
Source code in zeus/monitor/carbon.py
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
def __init__(
    self,
    carbon_intensity_provider: CarbonIntensityProvider,
    gpu_indices: list[int] | None = None,
    cpu_indices: list[int] | None = None,
    sync_execution_with: Literal["torch", "jax", "cupy"] = "torch",
) -> None:
    """Initializes Carbon Emission Monitor.

    Args:
        carbon_intensity_provider: provider for which carbon intensity values will be fetched from
        gpu_indices: Indices of all the CUDA devices to monitor. Time/Energy measurements
            will begin and end at the same time for these GPUs (i.e., synchronized).
            If None, all the GPUs available will be used. `CUDA_VISIBLE_DEVICES`
            is respected if set, e.g., GPU index `1` passed into `gpu_indices` when
            `CUDA_VISIBLE_DEVICES=2,3` will be interpreted as CUDA device `3`.
            `CUDA_VISIBLE_DEVICES`s formatted with comma-separated indices are supported.
        cpu_indices: Indices of the CPU packages to monitor. If None, all CPU packages will
            be used.
        sync_execution_with: Deep learning framework to use to synchronize CPU/GPU computations.
            Defaults to `"torch"`, in which case `torch.cuda.synchronize` will be used.
            See [`sync_execution`][zeus.utils.framework.sync_execution] for more details.
    """
    # Warn if instantiated as a global variable in a subprocess.
    warn_if_global_in_subprocess(self)

    self.zeus_monitor = ZeusMonitor(
        gpu_indices=gpu_indices,
        cpu_indices=cpu_indices,
        sync_execution_with=sync_execution_with,
    )
    self.carbon_intensity_provider = carbon_intensity_provider
    self.current_keys = set()

    # set up process and shared queues
    self.context = mp.get_context("spawn")
    self.command_q = self.context.Queue()
    self.finished_q = self.context.Queue()
    self._stop_poller: mp_util.Finalize | None = None

begin_window

begin_window(key, sync_execution=True)

Begin a new measurement window.

Parameters:

Name Type Description Default
key str

Unique name of the measurement window.

required
sync_execution bool

Whether to wait for asynchronously dispatched computations to finish before starting the measurement window. For instance, PyTorch and JAX will run GPU computations asynchronously, and waiting them to finish is necessary to ensure that the measurement window captures all and only the computations dispatched within the window.

True
Source code in zeus/monitor/carbon.py
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
def begin_window(self, key: str, sync_execution: bool = True) -> None:
    """Begin a new measurement window.

    Args:
        key: Unique name of the measurement window.
        sync_execution: Whether to wait for asynchronously dispatched computations
            to finish before starting the measurement window. For instance, PyTorch
            and JAX will run GPU computations asynchronously, and waiting them to
            finish is necessary to ensure that the measurement window captures all
            and only the computations dispatched within the window.
    """
    # check if key is already used
    if key in self.current_keys:
        raise ValueError(f"Measurement window '{key}' already exists")
    self.current_keys.add(key)

    # start window
    self.zeus_monitor.begin_window(key, sync_execution=sync_execution)

    # if there were previously no active windows, start polling process
    if len(self.current_keys) == 1:
        self.polling_process = self.context.Process(
            target=_polling_process,
            args=(
                self.command_q,
                self.finished_q,
                self.zeus_monitor.gpu_indices,
                self.zeus_monitor.cpu_indices,
                self.carbon_intensity_provider,
            ),
        )
        self.polling_process.start()

        # If user code raises inside a window, Op.END never arrives and the poller blocks exit.
        # exitpriority=0 runs this before multiprocessing joins child processes.
        if self._stop_poller is not None:
            self._stop_poller.cancel()  # the previous poller has exited
        self._stop_poller = mp_util.Finalize(
            self, _stop_polling_process, args=(self.polling_process,), exitpriority=0
        )

    # start subwindows
    self.command_q.put((Op.BEGIN, key))

end_window

end_window(key, sync_execution=True)

End a measurement window and return the time, energy consumption, and carbon emission.

Parameters:

Name Type Description Default
key str

Name of an active measurement window.

required
sync_execution bool

Whether to wait for asynchronously dispatched computations to finish before starting the measurement window. For instance, PyTorch and JAX will run GPU computations asynchronously, and waiting them to finish is necessary to ensure that the measurement window captures all and only the computations dispatched within the window.

True
Source code in zeus/monitor/carbon.py
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
def end_window(self, key: str, sync_execution: bool = True) -> CarbonEmissionMeasurement:
    """End a measurement window and return the time, energy consumption, and carbon emission.

    Args:
        key: Name of an active measurement window.
        sync_execution: Whether to wait for asynchronously dispatched computations
            to finish before starting the measurement window. For instance, PyTorch
            and JAX will run GPU computations asynchronously, and waiting them to
            finish is necessary to ensure that the measurement window captures all
            and only the computations dispatched within the window.
    """
    # check if begin_window has been called with key before
    if key not in self.current_keys:
        raise ValueError(f"Measurement window '{key}' does not exist")

    # end window
    self.command_q.put((Op.END, key))
    try:
        (
            gpu_carbon_emissions,
            cpu_carbon_emissions,
            dram_carbon_emissions,
        ) = get_or_raise_if_dead(self.finished_q, self.polling_process)
    except RuntimeError:
        # The polling process is gone, and with it the data of every open window. Drop them,
        # and use new queues so the next poller doesn't read commands meant for this one.
        for open_key in self.current_keys:
            self.zeus_monitor.end_window(open_key, sync_execution=False, cancel=True)
        self.current_keys.clear()
        self.command_q = self.context.Queue()
        self.finished_q = self.context.Queue()
        raise
    self.current_keys.remove(key)

    overall_measurement = self.zeus_monitor.end_window(key, sync_execution=sync_execution)

    measurement = CarbonEmissionMeasurement(
        time=overall_measurement.time,
        gpu_energy=overall_measurement.gpu_energy,
        cpu_energy=overall_measurement.cpu_energy,
        dram_energy=overall_measurement.dram_energy,
        gpu_carbon_emission=gpu_carbon_emissions,
        cpu_carbon_emission=cpu_carbon_emissions or None,
        dram_carbon_emission=dram_carbon_emissions or None,
    )

    return measurement

_period_floor

_period_floor(when, period)

Return the start of the period of length period (on a UTC grid from the Unix epoch) that contains when.

Source code in zeus/monitor/carbon.py
596
597
598
def _period_floor(when: datetime, period: timedelta) -> datetime:
    """Return the start of the period of length `period` (on a UTC grid from the Unix epoch) that contains `when`."""
    return _EPOCH + ((when.astimezone(timezone.utc) - _EPOCH) // period) * period

_parse_times

_parse_times(text, utc=False)

Parse timestamps, also when ISO 8601 rows differ in precision.

pandas guesses one format from the first row, so 00:00:00Z followed by 01:00:00.000Z fails.

Source code in zeus/monitor/carbon.py
601
602
603
604
605
606
607
608
609
def _parse_times(text: pd.Series, utc: bool = False) -> pd.Series:
    """Parse timestamps, also when ISO 8601 rows differ in precision.

    pandas guesses one format from the first row, so `00:00:00Z` followed by `01:00:00.000Z` fails.
    """
    try:
        return pd.to_datetime(text, utc=utc)
    except ValueError:
        return pd.to_datetime(text, utc=utc, format="ISO8601")

_stop_polling_process

_stop_polling_process(process)

Stop a polling process that is still running.

Source code in zeus/monitor/carbon.py
813
814
815
816
817
def _stop_polling_process(process: mp.process.BaseProcess) -> None:
    """Stop a polling process that is still running."""
    if process.is_alive():
        process.terminate()
        process.join(timeout=5.0)