Skip to content

Fix/839 unit magic for low and high emissions #844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Estimate and track carbon emissions from your computer, quantify and analyze the

# About CodeCarbon 💡

**CodeCarbon** started with a quite simple question:
**CodeCarbon** started with a quite simple question:

**What is the carbon emission impact of my computer program? :shrug:**

Expand Down Expand Up @@ -63,11 +63,27 @@ To see more installation options please refer to the documentation: [**Installat

## Start to estimate your impact 📏

### Without using the online dashboard

```python
from codecarbon import track_emissions
@track_emissions()
def your_function_to_track():
# your code
```

After running your code, you will find an `emissions.csv` that you can visualize with `carbonboard --filepath="examples/emissions.csv"`.

### With the online dashboard

To use the online dashboard you need to create an account on [**CodeCarbon Dashboard**](https://dashboard.codecarbon.io/).
Once you have an account, you can create an experiment_id to track your emissions.

To get an experiment_id enter:
```python
! codecarbon init
! codecarbon login
```
You can now store it in a **.codecarbon.config** at the root of your project
You can now store it in a **.codecarbon.config** at the root of your project
```python
[codecarbon]
log_level = DEBUG
Expand Down Expand Up @@ -98,7 +114,6 @@ There is other ways to use **codecarbon** package, please refer to the documenta
You can now visualize your experiment emissions on the [dashboard](https://dashboard.codecarbon.io/).
![dashboard](docs/edit/images/dashboard.png)

*Note that for now, all emissions data send to codecarbon API are public.*

> Hope you enjoy your first steps monitoring your carbon computing impact!
> Thanks to the incredible codecarbon community 💪🏼 a lot more options are available using *codecarbon* including:
Expand Down Expand Up @@ -126,8 +141,8 @@ Contact [@vict0rsch](https://github.com/vict0rsch) to be added to our slack work

If you find CodeCarbon useful for your research, you can find a citation under a variety of formats on [Zenodo](https://zenodo.org/records/11171501).

Here is a sample for BibTeX:
```tex
Here is a sample for BibTeX:
```tex
@software{benoit_courty_2024_11171501,
author = {Benoit Courty and
Victor Schmidt and
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" add codecarbon version
"""add codecarbon version

Revision ID: 298059b19bde
Revises: edcd10edf11d
Expand Down
2 changes: 1 addition & 1 deletion codecarbon/viz/carbonboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def update_cloud_emissions_barchart(hidden_project_summary: dcc.Store):
def viz(filepath: str, port: int = 8050, debug: bool = False) -> None:
df = pd.read_csv(filepath)
app = render_app(df)
app.run_server(port=port, debug=debug)
app.run(port=port, debug=debug)


def main():
Expand Down
2 changes: 1 addition & 1 deletion codecarbon/viz/carbonboard_on_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def viz(port: int = 8051, debug: bool = False) -> None:
conf = get_hierarchical_config()
df = Data.get_data_from_api(conf.get("api_endpoint", "http://localhost:8000"))
app = render_app(df)
app.run_server(port=port, debug=debug)
app.run(port=port, debug=debug)


def main():
Expand Down
19 changes: 17 additions & 2 deletions codecarbon/viz/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
from dash import dash_table as dt
from dash import dcc, html

from codecarbon.viz.units import (
EmissionUnit,
extends_emissions_units,
get_emissions_unit,
)


class Components:
def __init__(self):
Expand Down Expand Up @@ -656,15 +662,24 @@ def get_project_time_series_figure(project_data: dt.DataTable):
def get_project_emissions_bar_chart_figure(project_data: dt.DataTable):
# Note: necessary to both convert to pandas and replace null values for hover value
project_data = pd.DataFrame(project_data)
project_data = extends_emissions_units(project_data)
project_data = project_data.replace(np.nan, "", regex=True)
unit = get_emissions_unit(project_data)
hover_data = {c: True for c in project_data.columns}
bar = (
px.bar(
project_data,
y="emissions",
y=(
f"emissions_in_{unit.value}"
if unit != EmissionUnit.KILOGRAM
else "emissions"
),
hover_data=hover_data,
labels={
"emissions": "Carbon Equivalent (KgCO2eq)",
"index": "Entry",
"emissions": "Carbon Equivalent (kgCO2eq)",
"emissions_in_g": "Carbon Equivalent (gCO2eq)",
"emissions_in_t": "Carbon Equivalent (tCO2eq)",
"energy_consumed": "Energy Consumed (kWh)",
"timestamp": "Timestamp",
"project_name": "Project Name",
Expand Down
24 changes: 24 additions & 0 deletions codecarbon/viz/units.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from enum import Enum

import pandas as pd


class EmissionUnit(str, Enum):
GRAM = "g"
KILOGRAM = "kg"
TON = "t"


def get_emissions_unit(data: pd.DataFrame) -> EmissionUnit:
unit = EmissionUnit.KILOGRAM
if (data.emissions < 1).all():
unit = EmissionUnit.GRAM
if (data.emissions > 1000).all():
unit = EmissionUnit.TON
return unit


def extends_emissions_units(data: pd.DataFrame) -> pd.DataFrame:
data["emissions_in_g"] = data.emissions * 1000
data["emissions_in_t"] = data.emissions / 1000
return data
4 changes: 1 addition & 3 deletions dashboard/carbon_board_API.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@
app.layout = serve_layout

if __name__ == "__main__":
app.run_server(
debug=True, use_reloader=True, dev_tools_silence_routes_logging=False
)
app.run(debug=True, use_reloader=True, dev_tools_silence_routes_logging=False)
2 changes: 1 addition & 1 deletion dashboard/other/CarbonBoard.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@
" return container, fig\n",
" \n",
"if __name__ == '__main__':\n",
" app.run_server(debug=True, use_reloader=False)"
" app.run(debug=True, use_reloader=False)"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion dashboard/other/carbonBoard.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,4 +739,4 @@ def update_map(start_date, end_date, project, kpi):


if __name__ == "__main__":
app.run_server(debug=True, use_reloader=False)
app.run(debug=True, use_reloader=False)
6 changes: 3 additions & 3 deletions docs/edit/visualize.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ region to host infrastructure for the concerned cloud provider.
:height: 450px
:width: 750px

Online (Beta)
Online
--------------

A ``Dash App`` is also available for those who chose to connect the package to the API then data are public and available for all to explore.
`preview <https://dashboard.codecarbon.io/>`_
A dashboard is also available for those who chose to connect the package to the public API.
`Got to online dashboard <https://dashboard.codecarbon.io/>`_

from global...
~~~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion examples/intel_rapl_show.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def monitor_power(self, interval=1, duration=10):
power = self.read_power_consumption(domain)
if power is not None:
print(
f"Domain '{domain.get("path").split('/')[-1]}/{domain.get("name")}' as a power consumption of {power:.2f} Watts"
f"Domain '{domain.get('path').split('/')[-1]}/{domain.get('name')}' as a power consumption of {power:.2f} Watts"
)
total_power += power
print(f"Total Power Consumption: {total_power:.2f} Watts")
Expand Down
13 changes: 1 addition & 12 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Changelog = "https://github.com/mlco2/codecarbon/releases"

viz = [
"dash",
"dash_bootstrap_components < 1.0.0",
"dash_bootstrap_components > 1.0.0",
"fire",
]

Expand Down Expand Up @@ -128,16 +128,6 @@ python = ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
[tool.hatch.envs.docs.scripts]
build = "cd docs/edit && make docs"

[tool.hatch.envs.dashboard]
dependencies = [
"dash>=2.2.0",
"dash_bootstrap_components",
"plotly>=5.6.0",
]

[tool.hatch.envs.dashboard.scripts]
run = "cd dashboard && python carbon_board_API.py"

[tool.hatch.envs.api]
python = "3.11"
dependencies = [
Expand All @@ -156,7 +146,6 @@ dependencies = [
"sqlalchemy<2.0.0",
"uvicorn[standard]<1.0.0",
"fastapi-pagination==0.9.1",
"pytest",
"mock",
"pytest",
"responses",
Expand Down
16 changes: 8 additions & 8 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ cffi==1.17.1
# via cryptography
charset-normalizer==3.4.2
# via requests
click==8.1.8
click==8.2.1
# via
# hatch.envs.default
# typer
cryptography==45.0.2
cryptography==45.0.3
# via jwcrypto
fief-client==0.20.0
# via hatch.envs.default
Expand All @@ -57,13 +57,13 @@ markdown-it-py==3.0.0
# via rich
mdurl==0.1.2
# via markdown-it-py
numpy==2.2.6
numpy==2.3.0
# via pandas
nvidia-ml-py==12.575.51
# via pynvml
pandas==2.2.3
pandas==2.3.0
# via hatch.envs.default
prometheus-client==0.22.0
prometheus-client==0.22.1
# via hatch.envs.default
prompt-toolkit==3.0.51
# via questionary
Expand Down Expand Up @@ -91,7 +91,7 @@ questionary==2.1.0
# via hatch.envs.default
rapidfuzz==3.13.0
# via hatch.envs.default
requests==2.32.3
requests==2.32.4
# via hatch.envs.default
rich==14.0.0
# via
Expand All @@ -107,11 +107,11 @@ sniffio==1.3.1
# httpx
termcolor==2.3.0
# via yaspin
typer==0.15.4
typer==0.16.0
# via hatch.envs.default
types-python-dateutil==2.9.0.20250516
# via arrow
typing-extensions==4.13.2
typing-extensions==4.14.0
# via
# anyio
# jwcrypto
Expand Down
Loading
Loading