Skip to content

Plot the carbon intensity time series - #112

Merged
andreww merged 13 commits into
GreenScheduler:mainfrom
andreww:plotting
Jun 24, 2025
Merged

Plot the carbon intensity time series#112
andreww merged 13 commits into
GreenScheduler:mainfrom
andreww:plotting

Conversation

@andreww

@andreww andreww commented Aug 23, 2024

Copy link
Copy Markdown
Collaborator

Just a proof of concept for now, but plots the CI forecast time series alongside our planned run times.

Currently looks like this:

cats_plot

I think the optimal and now plots (green and red, respectively) are 30 mins out. Or background data is 30 mins out. Anyhow, this is a start.

@codecov

codecov Bot commented Aug 23, 2024

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 19.29825% with 46 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.80%. Comparing base (e792cff) to head (286d81b).
⚠️ Report is 40 commits behind head on main.

Files with missing lines Patch % Lines
cats/plotting.py 10.20% 44 Missing ⚠️
cats/__init__.py 50.00% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #112   +/-   ##
=======================================
  Coverage   81.80%   81.80%           
=======================================
  Files          14       14           
  Lines         687      687           
=======================================
  Hits          562      562           
  Misses        125      125           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.
We may want to think about this a bit
(it's a big package) and the version
number is a guess, but let's get the tests
running for now.
@abhidg

abhidg commented Feb 25, 2025

Copy link
Copy Markdown
Contributor

@andreww one way to handle matplotlib is by putting in as an optional dependency, in a plots group as an example. Then if users want the plotting functionality they can use pip install 'climate-aware-task-scheduler[plots]'

Add coloured text giving mean carbon intensity for best and now
start times. Make date format on x axis easer to read.
I was seeing a bunch of fractional seconds in the shell
output, which is a bit ugly. Explicity set formatting to match our
test cases.
This should also print a useful error if it's not installed
We just need to print out a message if --plot is passed.
@andreww
andreww marked this pull request as ready for review February 25, 2025 11:51
@andreww

andreww commented Feb 25, 2025

Copy link
Copy Markdown
Collaborator Author

@andreww one way to handle matplotlib is by putting in as an optional dependency, in a plots group as an example. Then if users want the plotting functionality they can use pip install 'climate-aware-task-scheduler[plots]'

Thanks - I did this. It ended up being a little fiddly because of the type testing but it seems to be working. Ready to merge?

@sadielbartholomew sadielbartholomew changed the title Plot the carbon intensity time serise Plot the carbon intensity time series Feb 25, 2025
@abhidg

abhidg commented Jun 6, 2025

Copy link
Copy Markdown
Contributor

@andreww I don't see any interpolation here, running with cats -d 60 --loc OX1 --plot, I get this output:


The.____ ..... __ .... ________ . ______...
.. /  __)...../  \....(__    __).)  ____)....
..|  /......./    \......|  |...(  (___........
..| |limate./  ()  \ware.|  |ask.\___  \cheduler
..|  \__...|   __   |....|  |....____)  )....
...\    )..|  (..)  |....|  |...(      (..

WARNING:root:config file not found
WARNING:root:Unspecified carbon intensity forecast service, using carbonintensity.org.uk

Best job start time                       = 2025-06-08 10:40:44
Carbon intensity if job started now       = 76.08 gCO2eq/kWh
Carbon intensity at optimal time          = 63.87 gCO2eq/kWh

Zooming in on the plots figure, I see a 30 min window highlighted, rather than 60 minutes (starting now, red):
Screenshot 2025-06-06 at 15 15 15

and the optimal time (green)

Screenshot 2025-06-06 at 15 16 14

@andreww

andreww commented Jun 6, 2025

Copy link
Copy Markdown
Collaborator Author

Ugh, yes, I see. I need to add the start and end times and interpolated values on the start and end of the series of points plotted in green and red. We must have this calculated somewhere - it'll be a case of getting it into the plotting function. Good spot.

For plotting we were only including points for the optimal and
now start time from within the dataset of point estimates (i.e.
every 30 mins) even if the start or end time was not on one of
these times. This means the plots missed time at the start or
end of the projected run time for the red and green segments.

This is fixed by adding two more fields to the CarbonIntensityAverageEstimate,
the start_value and end_value which correspond to the forcast
values from the start and end times. These are interpolated when
calculating the integral for the average estimate anyway, we just store the
data now. We then pick up and plot these data points at the start and end
of the red and green segments.

This adds information into the serialised output in JSON format and in
the expected CarbonIntensityAverageEstimates in the test cases. Hence the
test cases are updated.
@andreww

andreww commented Jun 13, 2025

Copy link
Copy Markdown
Collaborator Author

Thanks again for the spot @abhidg - fixed now.

@sadielbartholomew

Copy link
Copy Markdown
Member

@andreww @abhidg would a second review be useful at this stage, so we can get this merged - unless this is waiting on other work / something else?

@andreww

andreww commented Jun 23, 2025

Copy link
Copy Markdown
Collaborator Author

@andreww @abhidg would a second review be useful at this stage, so we can get this merged - unless this is waiting on other work / something else?

@sadielbartholomew - a review would be useful. If I've managed to fix the issue @abhidg spotted (I think I have) then I think this is ready to be merged

@sadielbartholomew

Copy link
Copy Markdown
Member

OK sure @andreww I will get that done with review submitted this afternoon. Thanks for your work on this, it will be a really cool feature!

@sadielbartholomew sadielbartholomew left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic stuff, thanks @andreww! Works very well, as tested across various durations across the valid range and with a selection of curves from choosing random postcodes for location.

A few general questions before we merge (plus some in-line suggestions):

  1. Why is there always a small gap before the curve is coloured red to indicate the 'immediate' start time? I would expect it to be red from the very leftmost point on the curve, but I might be missing something...

  2. Overlapping of the immediate start curve region and the optimal one: I like how, if these overlap there is a different colour to indicate clearly enough that the red and green both cover that region (even if the khaki itself is a bit ugly!), like so:

evidence4

However, there are cases where the optimal time is the immediate time (probably not too uncommon a case, since of about 10 runs I tried to test across duration and location inputs I saw this once), the whole thing is that colour which might be a bit confusing, i.e. if a user just sees this they can perhaps realise what is happening, but could easily not:

evidence2

Maybe a good way to solve that is to have the crossover region as a hatched pattern over the other region e.g. green dots on the red? Alternatively, just use a key and include the colour of the overlap region.

Happy to bump this one (and e.g. the units formatting as mentioned in-line) to a new PR that I can quickly put in though, if you prefer, in the interests of getting this great work merged!

Comment thread cats/__init__.py Outdated
Comment thread cats/plotting.py
Comment thread cats/plotting.py Outdated
andreww and others added 3 commits June 24, 2025 09:09
Co-authored-by: Sadie L. Bartholomew <sadie.bartholomew@ncas.ac.uk>
@andreww

andreww commented Jun 24, 2025

Copy link
Copy Markdown
Collaborator Author

Thanks @sadielbartholomew - I'll merge this and open an issue for improvements re. units markup and styling the overlap. In terms of:

Why is there always a small gap before the curve is coloured red to indicate the 'immediate' start time? I would expect it to be red from the very leftmost point on the curve, but I might be missing something...

I think this is because I always plot the whole of the carbon intensity forecast which starts at the start of the "current" 30 min time window but the "now" time is always now. This means there could be up to 29 minutes of stale forecast in the plot. It wouldn't be too hard to trim this off but I'm not sure if it's more confusing to show the full forecast or have our initial point be different to what a naive reading of data on the front page of carbonintensity.org.uk says.

@andreww
andreww merged commit f43833e into GreenScheduler:main Jun 24, 2025
This was referenced Jun 24, 2025
@sadielbartholomew

Copy link
Copy Markdown
Member

Thanks @andreww for clarifying and merging. All good.

It wouldn't be too hard to trim this off but I'm not sure if it's more confusing to show the full forecast or have our initial point be different to what a naive reading of data on the front page of carbonintensity.org.uk says.

Now you've explained I think it's fine to leave as-is, I agree that it might be confusing likewise to some to trim it and that's also extra work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants