Skip to content

Commit

Permalink
Add parser to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
PedramBakh committed Sep 13, 2024
1 parent 28c14d3 commit 2e4c761
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
28 changes: 21 additions & 7 deletions carbontracker/cli.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import argparse
import subprocess
from carbontracker.tracker import CarbonTracker
from carbontracker import parser
import ast


def parse_logs(log_dir):
parser.print_aggregate(log_dir=log_dir)


def main():
"""
The **carbontracker** CLI allows the user to track the energy consumption and carbon intensity of any program.
[Make sure that you have relevant permissions before running this.](/#permissions)
Args:
--log_dir (path, optional): Log directory. Defaults to `./logs`.
--api_keys (str, optional): API keys in a dictionary-like format, e.g. `\'{"electricitymaps": "YOUR_KEY"}\'`
--parse (path, optional): Directory containing the log files to parse.
Example:
Tracking the carbon intensity of `script.py`.
Expand All @@ -23,21 +28,30 @@ def main():
$ carbontracker --log_dir='./logs' --api_keys='{"electricitymaps": "API_KEY_EXAMPLE"}' python script.py
Parsing logs:
$ carbontracker --parse ./internal_logs
"""

# Create a parser for the known arguments
parser = argparse.ArgumentParser(description="CarbonTracker CLI", add_help=True)
parser.add_argument("--log_dir", type=str, default="./logs", help="Log directory")
parser.add_argument(
cli_parser = argparse.ArgumentParser(description="CarbonTracker CLI", add_help=True)
cli_parser.add_argument("--log_dir", type=str, default="./logs", help="Log directory")
cli_parser.add_argument(
"--api_keys",
type=str,
help="API keys in a dictionary-like format, e.g., "
'\'{"electricitymaps": "YOUR_KEY"}\'',
'\'{"electricitymaps": "YOUR_KEY"}\'',
default=None,
)
cli_parser.add_argument("--parse", type=str, help="Directory containing the log files to parse.")

# Parse known arguments only
known_args, remaining_args = parser.parse_known_args()
known_args, remaining_args = cli_parser.parse_known_args()

# Check if the --parse argument is provided
if known_args.parse:
parse_logs(known_args.parse)
return

# Parse the API keys string into a dictionary
api_keys = ast.literal_eval(known_args.api_keys) if known_args.api_keys else None
Expand All @@ -61,4 +75,4 @@ def main():


if __name__ == "__main__":
main()
main()
4 changes: 2 additions & 2 deletions carbontracker/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ def print_aggregate(log_dir):
"""
energy, co2eq, equivalents = aggregate_consumption(log_dir)

equivalents_p = " or ".join([f"{v:.3f} {k}" for k, v in equivalents.items()])
equivalents_p = " or ".join([f"{v:.16f} {k}" for k, v in equivalents.items()])

printable = f"The training of models in this work is estimated to use {energy:.3f} kWh of electricity contributing to {co2eq / 1000:.3f} kg of CO2eq. "
printable = f"The training of models in this work is estimated to use {energy:.16f} kWh of electricity contributing to {co2eq / 1000:.16f} kg of CO2eq. "
if equivalents_p:
printable += f"This is equivalent to {equivalents_p}. "

Expand Down

0 comments on commit 2e4c761

Please sign in to comment.