From 1b10e7db5e8e5b8ff2f8c66189134f9e558a3e97 Mon Sep 17 00:00:00 2001 From: Daniel Hou Date: Mon, 30 Sep 2024 02:53:25 -0400 Subject: [PATCH] CarbonIntensityProvider and ElectricityMaps implementation --- ...ctricity_maps_carbon_intensity_provider.py | 8 ++--- zeus/carbon/__init__.py | 21 ------------- zeus/carbon/carbon.py | 4 +-- zeus/carbon/carbon_intensity_provider.py | 19 ------------ ...ctricity_maps_carbon_intensity_provider.py | 30 ------------------- 5 files changed, 6 insertions(+), 76 deletions(-) delete mode 100644 zeus/carbon/__init__.py delete mode 100644 zeus/carbon/carbon_intensity_provider.py delete mode 100644 zeus/carbon/electricity_maps_carbon_intensity_provider.py diff --git a/tests/carbon/test_electricity_maps_carbon_intensity_provider.py b/tests/carbon/test_electricity_maps_carbon_intensity_provider.py index 4362e5c4..46636e57 100644 --- a/tests/carbon/test_electricity_maps_carbon_intensity_provider.py +++ b/tests/carbon/test_electricity_maps_carbon_intensity_provider.py @@ -1,15 +1,15 @@ from __future__ import annotations -import requests -import pytest import json +import pytest +import requests from unittest.mock import patch from zeus.carbon.carbon import ( ElectrictyMapsClient, get_ip_lat_long, - CarbonIntensityNotFoundException, + CarbonIntensityNotFoundError, ) @@ -100,5 +100,5 @@ def test_get_current_carbon_intensity_no_response(mock_requests): assert latlong == (pytest.approx(42.2776), pytest.approx(-83.7409)) provider = ElectrictyMapsClient(latlong) - with pytest.raises(CarbonIntensityNotFoundException): + with pytest.raises(CarbonIntensityNotFoundError): provider.get_current_carbon_intensity() diff --git a/zeus/carbon/__init__.py b/zeus/carbon/__init__.py deleted file mode 100644 index dfb15f09..00000000 --- a/zeus/carbon/__init__.py +++ /dev/null @@ -1,21 +0,0 @@ -"""Carbon intensity providers used for carbon-aware optimizers.""" - -from zeus.carbon.electricity_maps_carbon_intensity_provider import ( - ElectricityMapsCarbonIntensityProvider, -) - -import requests - - -def get_ip_lat_long() -> tuple[float, float]: - """Retrieve the latitude and longitude of the current IP position.""" - try: - ip_url = "http://ipinfo.io/json" - resp = requests.get(ip_url) - loc = resp.json()["loc"] - lat, long = map(float, loc.split(",")) - print(f"Retrieve latitude and longitude: {lat}, {long}") - return lat, long - except Exception as e: - print(f"Failed to Retrieve Current IP's Latitude and Longitude: {e}") - raise (e) diff --git a/zeus/carbon/carbon.py b/zeus/carbon/carbon.py index 091afe9b..fdac2223 100644 --- a/zeus/carbon/carbon.py +++ b/zeus/carbon/carbon.py @@ -2,10 +2,10 @@ from __future__ import annotations -import requests -import logging import abc import json +import logging +import requests logger = logging.getLogger(__name__) diff --git a/zeus/carbon/carbon_intensity_provider.py b/zeus/carbon/carbon_intensity_provider.py deleted file mode 100644 index 8c39207c..00000000 --- a/zeus/carbon/carbon_intensity_provider.py +++ /dev/null @@ -1,19 +0,0 @@ -"""Abstract Carbon Intensity Provider Class.""" -import abc - - -class CarbonIntensityProvider(abc.ABC): - """Abstract class for implementing ways to fetch carbon intensity.""" - - def __init__(self, location: tuple[float, float]) -> None: - """Initializes carbon intensity provider location to the latitude and longitude of the input `location`. - - Location is a tuple of floats where latitude is the first float and longitude is the second float. - """ - self.lat = location[0] - self.long = location[1] - - @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 diff --git a/zeus/carbon/electricity_maps_carbon_intensity_provider.py b/zeus/carbon/electricity_maps_carbon_intensity_provider.py deleted file mode 100644 index 8890117c..00000000 --- a/zeus/carbon/electricity_maps_carbon_intensity_provider.py +++ /dev/null @@ -1,30 +0,0 @@ -"""Carbon Intensity Provider using ElectrictyMaps API.""" -from zeus.carbon.carbon_intensity_provider import CarbonIntensityProvider -import requests - - -class ElectricityMapsCarbonIntensityProvider(CarbonIntensityProvider): - """Carbon Intensity Provider with ElectricityMaps API.""" - - def get_current_carbon_intensity( - self, estimate: bool = False, emission_factor_type: str = "direct" - ) -> float: - """Fetches current carbon intensity of the location of the class. - - Args: - estimate: bool to toggle whether carbon intensity is estimated or not - emission_factor_type: emission factor to be measured (`direct` or `lifestyle`) - - !!! Note - In some locations, there is no recent carbon intensity data. `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 estimate}&emissionFactorType={emission_factor_type}" - ) - resp = requests.get(url) - return resp.json()["carbonIntensity"] - except Exception as e: - print(f"Failed to retrieve live carbon intensity data: {e}") - raise (e)