Skip to content

Commit

Permalink
Initial proof of concept to plot plan
Browse files Browse the repository at this point in the history
Makes use of our 'output' and the 'forecast'
to plot a timeseries of the CI alongside
highlights for the CI of running now or running
at the optimal time.
  • Loading branch information
andreww committed Aug 23, 2024
1 parent fa1a8cc commit ef99e0d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .configure import get_runtime_config
from .forecast import CarbonIntensityAverageEstimate
from .optimise_starttime import get_avg_estimates # noqa: F401
from .plotting import plotplan

__version__ = "1.0.0"

Expand Down Expand Up @@ -182,6 +183,11 @@ def positive_integer(string):
type=positive_integer,
help="Amount of memory used by the job, in GB",
)
parser.add_argument(
"--plot",
help="Create a plot of the forcast and optimised plan for the job",
action="store_true",
)

return parser

Expand Down Expand Up @@ -312,6 +318,8 @@ def main(arguments=None) -> int:
print(output.to_json(dateformat, sort_keys=True, indent=2))
else:
print(output)
if args.plot:
plotplan(CI_forecast, output)
if args.command and args.scheduler == "at":
if err := schedule_at(output, args.command.split()):
print(err)
Expand Down
38 changes: 38 additions & 0 deletions cats/plotting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import numpy as np
import matplotlib.pyplot as plt

def plotplan(CI_forecast, output):
"""
Plot the carbon intensity forcast and optimised plan
"""
# Just for now pull the CI forecast apart... probably belongs as method...
values = []
times = []
now_values = []
now_times = []
opt_values = []
opt_times = []
for point in CI_forecast:
values.append(point.value)
times.append(point.datetime)
if (point.datetime >= output.carbonIntensityOptimal.start and
point.datetime <= output.carbonIntensityOptimal.end):
opt_values.append(point.value)
opt_times.append(point.datetime)
if (point.datetime >= output.carbonIntensityNow.start and
point.datetime <= output.carbonIntensityNow.end):
now_values.append(point.value)
now_times.append(point.datetime)

# Make the plot (should probably take fig and ax as opt args...)
fig, ax = plt.subplots()
ax.fill_between(times, 0.0, values, alpha=0.2, color='b')
ax.fill_between(now_times, 0.0, now_values, alpha=0.6, color='r')
ax.fill_between(opt_times, 0.0, opt_values, alpha=0.6, color='g')
ax.set_xlabel("Time (mm-dd hh)")
ax.set_ylabel("Forecast carbon intensity (gCO2eq/kWh)")
ax.grid(True)
ax.label_outer()
fig.autofmt_xdate()
plt.show()
return None

0 comments on commit ef99e0d

Please sign in to comment.