Skip to content

Commit

Permalink
Add CLI support with python scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
PedramBakh committed Sep 8, 2023
1 parent 614ebe0 commit f535905
Show file tree
Hide file tree
Showing 15 changed files with 148 additions and 85 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Publish Dev Release to TestPyPI

on:
push:
tags:
- 'dev-v*.*.*'

jobs:
publish:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: "3.10"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install wheel
- name: Extract version from tag
run: echo "PACKAGE_VERSION=${GITHUB_REF#refs/tags/dev-v}" >> $GITHUB_ENV

- name: Build package
run: |
python setup.py sdist bdist_wheel
- name: Publish to TestPyPI
uses: pypa/gh-action-pypi-publish@v1.4.2
with:
user: __token__
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
repository_url: https://test.pypi.org/legacy/
41 changes: 0 additions & 41 deletions .github/workflows/publish-pypi.yml

This file was deleted.

33 changes: 33 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Deploy to PyPi

on:
push:
tags:
- 'v*.*.*'

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: "3.10"

- name: Install pypa/build
run: |
python -m pip install --upgrade pip
python -m pip install build
- name: Build the package
run: |
python -m build
- name: Publish to PyPi
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}

21 changes: 9 additions & 12 deletions .github/workflows/build.yml → .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: build
name: Test, Lint, and Format

on:
push:
branches: [ master ]
branches:
- master
pull_request:
branches: [ master ]
branches:
- master

jobs:
build:

runs-on: ubuntu-20.04
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8, 3.9, '3.10']
Expand All @@ -26,11 +24,10 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8
pip install flake8 black
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 carbontracker --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 carbontracker --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Formatting with Black
run: black --line-length 120 carbontracker
27 changes: 27 additions & 0 deletions carbontracker/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import argparse
import subprocess
from carbontracker.tracker import CarbonTracker
def main():
parser = argparse.ArgumentParser(description="CarbonTracker CLI")
parser.add_argument("--script", type=str, required=True, help="Python file to execute")
parser.add_argument("--log_dir", type=str, help="Log directory", default="./logs")
# Add more arguments as needed

args = parser.parse_args()

tracker = CarbonTracker(epochs=1, log_dir=args.log_dir, epochs_before_pred=0)
tracker.epoch_start()

# Execute script with python
try:
subprocess.run(["python", args.script], check=True)
except subprocess.CalledProcessError:
print(f"Error executing script: {args.script}")
# Handle errors or exceptions if needed

tracker.epoch_end()
tracker.stop()


if __name__ == "__main__":
main()
7 changes: 5 additions & 2 deletions carbontracker/components/apple_silicon/powermetrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
import time
from carbontracker.components.handler import Handler

class PowerMetricsUnified():

class PowerMetricsUnified:
_output = None
_last_updated = None

@staticmethod
def get_output():
if PowerMetricsUnified._output is None or time.time() - PowerMetricsUnified._last_updated > 1:
PowerMetricsUnified._output = subprocess.check_output(["sudo", "powermetrics", "-n", "1", "-i", "1000", "--samplers", "all"], universal_newlines=True)
PowerMetricsUnified._output = subprocess.check_output(
["sudo", "powermetrics", "-n", "1", "-i", "1000", "--samplers", "all"], universal_newlines=True
)
PowerMetricsUnified._last_updated = time.time()
return PowerMetricsUnified._output

Expand Down
12 changes: 10 additions & 2 deletions carbontracker/components/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@
from carbontracker.components.apple_silicon.powermetrics import AppleSiliconCPU, AppleSiliconGPU

COMPONENTS = [
{"name": "gpu", "error": exceptions.GPUError("No GPU(s) available."), "handlers": [nvidia.NvidiaGPU, AppleSiliconGPU]},
{"name": "cpu", "error": exceptions.CPUError("No CPU(s) available."), "handlers": [intel.IntelCPU, AppleSiliconCPU]},
{
"name": "gpu",
"error": exceptions.GPUError("No GPU(s) available."),
"handlers": [nvidia.NvidiaGPU, AppleSiliconGPU],
},
{
"name": "cpu",
"error": exceptions.CPUError("No CPU(s) available."),
"handlers": [intel.IntelCPU, AppleSiliconCPU],
},
]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

API_URL = "https://api-access.electricitymaps.com/free-tier/carbon-intensity/latest"


class ElectricityMap(IntensityFetcher):
_api_key = None

Expand Down
27 changes: 10 additions & 17 deletions carbontracker/emissions/intensity/intensity.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ def get_default_intensity():
try:
g_location = geocoder.ip("me")
if not g_location.ok:
raise exceptions.IPLocationError(
"Failed to retrieve location based on IP."
)
raise exceptions.IPLocationError("Failed to retrieve location based on IP.")
address = g_location.address
country = g_location.country
except Exception as err:
Expand All @@ -29,24 +27,17 @@ def get_default_intensity():

try:
carbon_intensities_df = pd.read_csv(
pkg_resources.resource_filename(
"carbontracker", "data/carbon-intensities.csv"
)
pkg_resources.resource_filename("carbontracker", "data/carbon-intensities.csv")
)
intensity_row = carbon_intensities_df[
carbon_intensities_df["alpha-2"] == country
].iloc[0]
intensity_row = carbon_intensities_df[carbon_intensities_df["alpha-2"] == country].iloc[0]
intensity = intensity_row["Carbon intensity of electricity (gCO2/kWh)"]
year = intensity_row["Year"]
description = f"Defaulted to average carbon intensity for {country} in {year} of {intensity:.2f} gCO2/kWh."
except Exception as err:
intensity = constants.WORLD_2019_CARBON_INTENSITY
description = f"Defaulted to average carbon intensity for world in 2019 of {intensity:.2f} gCO2/kWh."

description = (
f"Live carbon intensity could not be fetched at detected location: {address}. "
+ description
)
description = f"Live carbon intensity could not be fetched at detected location: {address}. " + description
default_intensity = {
"carbon_intensity": intensity,
"description": description,
Expand Down Expand Up @@ -100,9 +91,7 @@ def carbon_intensity(logger, time_dur=None):
try:
g_location = geocoder.ip("me")
if not g_location.ok:
raise exceptions.IPLocationError(
"Failed to retrieve location based on IP."
)
raise exceptions.IPLocationError("Failed to retrieve location based on IP.")
carbon_intensity.address = g_location.address
except:
err_str = traceback.format_exc()
Expand All @@ -123,7 +112,11 @@ def carbon_intensity(logger, time_dur=None):
logger.err_info(err_str)

if not carbon_intensity.success:
logger.err_warn("Failed to retrieve carbon intensity: Defaulting to average carbon intensity {} gCO2/kWh.".format(default_intensity["carbon_intensity"]))
logger.err_warn(
"Failed to retrieve carbon intensity: Defaulting to average carbon intensity {} gCO2/kWh.".format(
default_intensity["carbon_intensity"]
)
)
return carbon_intensity


Expand Down
2 changes: 1 addition & 1 deletion carbontracker/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def __init__(
"CPUs with the RAPL interface and NVIDIA GPUs."
),
*args,
**kwargs
**kwargs,
):
super().__init__(msg, *args, **kwargs)

Expand Down
9 changes: 7 additions & 2 deletions carbontracker/loggerutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@


def convert_to_timestring(seconds, add_milliseconds=False):
negative = False
if seconds < 0:
negative = True
seconds = abs(seconds)

m, s = divmod(seconds, 60)
if not add_milliseconds:
s = int(round(s))
Expand All @@ -22,9 +27,9 @@ def convert_to_timestring(seconds, add_milliseconds=False):
h = int(h)
m = int(m)
if not add_milliseconds:
return f"{h:d}:{m:02d}:{s:02d}"
return f"-{h:d}:{m:02d}:{s:02d}" if negative else f"{h:d}:{m:02d}:{s:02d}"
else:
return f"{h:d}:{m:02d}:{s:05.2f}"
return f"-{h:d}:{m:02d}:{s:05.2f}" if negative else f"{h:d}:{m:02d}:{s:05.2f}"


class TrackerFormatter(logging.Formatter):
Expand Down
1 change: 0 additions & 1 deletion carbontracker/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ def get_consumption(output_log_data):
return actual, pred



def get_early_stop(std_log_data):
early_stop_re = re.compile(r"(?i)Training was interrupted")
early_stop = re.findall(early_stop_re, std_log_data)
Expand Down
2 changes: 1 addition & 1 deletion carbontracker/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def __init__(
log_file_prefix="",
verbose=1,
decimal_precision=12,
api_keys=None
api_keys=None,
):
if api_keys is not None:
self.set_api_keys(api_keys)
Expand Down
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ homepage = "https://github.com/lfwa/carbontracker"
repository = "https://github.com/lfwa/carbontracker"

[tool.setuptools_scm]

[project.scripts]
carbontracker = "carbontracker.cli:main"

8 changes: 2 additions & 6 deletions scripts/create_carbon_intensity_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ def main(args):

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--input_csv", help="Path to input csv.", required=True
)
parser.add_argument(
"--output_csv", help="Path to output csv.", required=True
)
parser.add_argument("--input_csv", help="Path to input csv.", required=True)
parser.add_argument("--output_csv", help="Path to output csv.", required=True)
args = parser.parse_args()
main(args)

0 comments on commit f535905

Please sign in to comment.