Skip to content

Code to reproduce the controlled release plots

This code loads the results from the controlled releases experiment that are stored in Hugging Face.

To eval the model in the controlled releases run:

# Run eval in the controlled releases MARS S2L model
python -m marss2l.eval_final \
           --split control_releases_test \
           --output_dir trained_models/MARSS2L_20250326 \
           --device cpu \
           --num_workers 16\
           --suffix_output controlled_releases\
           --csv_path /path/qto/localdir/MARS-S2L/validated_images_all.csv

Install marss2l package

pip install marss2l
import pandas as pd
import matplotlib
import os

from huggingface_hub import hf_file_system
from marss2l.huggingface import REPO_ID

fs = hf_file_system.HfFileSystem()
from marss2l.plot import C0, C1, C2, C3, C4

os.makedirs("figures", exist_ok=True)
/home/gonzalo/mambaforge/envs/marss2ltacopy312/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from .autonotebook import tqdm as notebook_tqdm

releases_path = f"datasets/{REPO_ID}/data/control_releases/releases_cleaned_up_data.csv" 
with fs.open(releases_path) as f:
    releases_gt = pd.read_csv(f)
releases_gt_tile_indexed = releases_gt.set_index("tile")
releases_gt_tile_indexed
date Satellite ch4_kgh_mean experiment tile_date
tile
S2B_MSIL1C_20211019T181359_N0301_R084_T11SQT_20211019T202307 2021-10-19 Sentinel-2 7175.659697 Sherwin 2023 2021-10-19 18:13:59+00:00
LC08_L1TP_038037_20211021_20211103_02_T1 2021-10-21 Landsat 8 4195.198122 Sherwin 2023 2021-10-21 18:10:37.528000+00:00
S2B_MSIL1C_20211022T182419_N0301_R127_T11SQT_20211022T202829 2021-10-22 Sentinel-2 1675.643780 Sherwin 2023 2021-10-22 18:24:19+00:00
S2A_MSIL1C_20211027T182451_N0301_R127_T11SQT_20211027T202957 2021-10-27 Sentinel-2 3494.031669 Sherwin 2023 2021-10-27 18:24:51+00:00
LC08_L1TP_039036_20211028_20211108_02_T1 2021-10-28 Landsat 8 2077.955636 Sherwin 2023 2021-10-28 18:16:24.380000+00:00
S2B_MSIL1C_20211029T181459_N0301_R084_T11SQT_20211029T202248 2021-10-29 Sentinel-2 5024.149730 Sherwin 2023 2021-10-29 18:14:59+00:00
S2B_MSIL1C_20211101T182519_N0301_R127_T11SQT_20211104T151400 2021-11-01 Sentinel-2 0.000000 Sherwin 2023 2021-11-01 18:25:19+00:00
S2A_MSIL1C_20211103T181531_N0301_R084_T11SQT_20211105T103659 2021-11-03 Sentinel-2 1386.180208 Sherwin 2023 2021-11-03 18:15:31+00:00
LC08_L1TP_036037_20221010_20221020_02_T1 2022-10-10 Landsat 8 0.000000 Sherwin 2024 2022-10-10 17:58:22.152000+00:00
LC08_L1TP_037037_20221017_20221031_02_T1 2022-10-17 Landsat 8 0.000000 Sherwin 2024 2022-10-17 18:04:33.324000+00:00
LC09_L1TP_037037_20221025_20230324_02_T1 2022-10-25 Landsat 8 984.882546 Sherwin 2024 2022-10-25 18:04:27.931000+00:00
S2A_MSIL1C_20221026T180431_N0400_R041_T12SVB_20221026T212647 2022-10-26 Sentinel-2 1047.002074 Sherwin 2024 2022-10-26 18:04:31+00:00
LC08_L1TP_037037_20221102_20221114_02_T1 2022-11-02 Landsat 8 755.914735 Sherwin 2024 2022-11-02 18:04:39.434000+00:00
LC09_L1TP_036037_20221103_20230323_02_T1 2022-11-03 Landsat 8 1302.146253 Sherwin 2024 2022-11-03 17:58:13.713000+00:00
S2A_MSIL1C_20221105T180531_N0400_R041_T12SVB_20221105T212905 2022-11-05 Sentinel-2 0.000000 Sherwin 2024 2022-11-05 18:05:31+00:00
S2A_MSIL1C_20221108T181551_N0400_R084_T12SVB_20221108T213000 2022-11-08 Sentinel-2 1190.093758 Sherwin 2024 2022-11-08 18:15:51+00:00
LC09_L1TP_037037_20221110_20230322_02_T1 2022-11-10 Landsat 8 1161.728969 Sherwin 2024 2022-11-10 18:04:28.343000+00:00
LC08_L1TP_036037_20221111_20221121_02_T1 2022-11-11 Landsat 8 1386.794745 Sherwin 2024 2022-11-11 17:58:25.353000+00:00
S2A_MSIL1C_20221115T180621_N0400_R041_T12SVB_20221115T200428 2022-11-15 Sentinel-2 1495.703000 Sherwin 2024 2022-11-15 18:06:21+00:00
LC08_L1TP_037037_20221118_20221128_02_T1 2022-11-18 Landsat 8 1475.165735 Sherwin 2024 2022-11-18 18:04:31.693000+00:00
S2A_MSIL1C_20221118T181641_N0400_R084_T12SVB_20221118T202906 2022-11-18 Sentinel-2 1428.390345 Sherwin 2024 2022-11-18 18:16:41+00:00
S2A_MSIL1C_20221125T180701_N0400_R041_T12SVB_20221125T202627 2022-11-25 Sentinel-2 0.000000 Sherwin 2024 2022-11-25 18:07:01+00:00
S2A_MSIL1C_20221128T181711_N0400_R084_T12SVB_20221128T201235 2022-11-28 Sentinel-2 4.949894 Sherwin 2024 2022-11-28 18:17:11+00:00
path_test_control_releases = f"datasets/{REPO_ID}/trained_models/MARSS2L_20250326/preds_control_releases_testth100.csv" 
with fs.open(path_test_control_releases) as f:
    model_output = pd.read_csv(f)

model_output["experiment"] = model_output.tile.apply(lambda x: releases_gt_tile_indexed.loc[x,"experiment"])
model_output["ch4_kgh_mean"] = model_output.tile.apply(lambda x: releases_gt_tile_indexed.loc[x,"ch4_kgh_mean"])
model_output["Satellite"] = model_output.tile.apply(lambda x: releases_gt_tile_indexed.loc[x,"Satellite"])
model_output["tile_date"] = model_output.tile.apply(lambda x: releases_gt_tile_indexed.loc[x,"tile_date"])
model_output.loc[pd.isna(model_output.Q), "Q"] = 0
model_output.loc[pd.isna(model_output.sigma_Q), "sigma_Q"] = 0
model_output = model_output.rename(columns={"Q": "pred_ch4_kgh_mean","sigma_Q": "pred_ch4_kgh_sigma"})
model_output["isplumepred"] = (model_output["scene_pred"] > 0.5).astype(int)
model_output[["scene_pred","target","ch4_kgh_mean","pred_ch4_kgh_mean","pred_ch4_kgh_sigma","TP","FP","TN","FN","experiment","tile_date","Satellite"]].sort_values("tile_date")
scene_pred target ch4_kgh_mean pred_ch4_kgh_mean pred_ch4_kgh_sigma TP FP TN FN experiment tile_date Satellite
22 0.998579 1 7175.659697 6490.546650 1967.428247 1464.0 224.0 37582.0 730.0 Sherwin 2023 2021-10-19 18:13:59+00:00 Sentinel-2
21 0.999749 1 4195.198122 2569.293798 786.369483 1221.0 1734.0 36601.0 444.0 Sherwin 2023 2021-10-21 18:10:37.528000+00:00 Landsat 8
20 0.910979 1 1675.643780 1826.993159 346.864601 0.0 465.0 39234.0 301.0 Sherwin 2023 2021-10-22 18:24:19+00:00 Sentinel-2
19 0.998115 1 3494.031669 3655.419927 1304.479275 670.0 63.0 38882.0 385.0 Sherwin 2023 2021-10-27 18:24:51+00:00 Sentinel-2
18 0.000615 1 2077.955636 0.000000 0.000000 0.0 0.0 39577.0 423.0 Sherwin 2023 2021-10-28 18:16:24.380000+00:00 Landsat 8
17 0.995646 1 5024.149730 4148.442469 1390.068992 1434.0 267.0 38003.0 296.0 Sherwin 2023 2021-10-29 18:14:59+00:00 Sentinel-2
16 0.058903 0 0.000000 0.000000 0.000000 0.0 0.0 40000.0 0.0 Sherwin 2023 2021-11-01 18:25:19+00:00 Sentinel-2
15 0.118345 1 1386.180208 0.000000 0.000000 0.0 0.0 39476.0 524.0 Sherwin 2023 2021-11-03 18:15:31+00:00 Sentinel-2
14 0.007112 0 0.000000 0.000000 0.000000 0.0 0.0 40000.0 0.0 Sherwin 2024 2022-10-10 17:58:22.152000+00:00 Landsat 8
13 0.001577 0 0.000000 0.000000 0.000000 0.0 0.0 40000.0 0.0 Sherwin 2024 2022-10-17 18:04:33.324000+00:00 Landsat 8
12 0.071371 1 984.882546 0.000000 0.000000 0.0 0.0 39100.0 900.0 Sherwin 2024 2022-10-25 18:04:27.931000+00:00 Landsat 8
11 0.012411 1 1047.002074 0.000000 0.000000 0.0 0.0 39765.0 235.0 Sherwin 2024 2022-10-26 18:04:31+00:00 Sentinel-2
10 0.000703 1 755.914735 0.000000 0.000000 0.0 0.0 39802.0 198.0 Sherwin 2024 2022-11-02 18:04:39.434000+00:00 Landsat 8
9 0.001837 1 1302.146253 0.000000 0.000000 0.0 0.0 39739.0 261.0 Sherwin 2024 2022-11-03 17:58:13.713000+00:00 Landsat 8
8 0.027292 0 0.000000 0.000000 0.000000 0.0 0.0 40000.0 0.0 Sherwin 2024 2022-11-05 18:05:31+00:00 Sentinel-2
7 0.608253 1 1190.093758 1944.862830 651.556294 120.0 145.0 39675.0 60.0 Sherwin 2024 2022-11-08 18:15:51+00:00 Sentinel-2
6 0.002017 1 1161.728969 0.000000 0.000000 0.0 0.0 39712.0 288.0 Sherwin 2024 2022-11-10 18:04:28.343000+00:00 Landsat 8
5 0.885327 1 1386.794745 2768.545052 914.156828 279.0 331.0 39381.0 9.0 Sherwin 2024 2022-11-11 17:58:25.353000+00:00 Landsat 8
4 0.223100 1 1495.703000 0.000000 0.000000 0.0 0.0 39539.0 461.0 Sherwin 2024 2022-11-15 18:06:21+00:00 Sentinel-2
3 0.999496 1 1475.165735 627.142820 139.912406 424.0 45.0 39181.0 350.0 Sherwin 2024 2022-11-18 18:04:31.693000+00:00 Landsat 8
2 0.809604 1 1428.390345 1460.074800 315.296660 230.0 200.0 39487.0 83.0 Sherwin 2024 2022-11-18 18:16:41+00:00 Sentinel-2
1 0.004459 0 0.000000 0.000000 0.000000 0.0 0.0 40000.0 0.0 Sherwin 2024 2022-11-25 18:07:01+00:00 Sentinel-2
0 0.000958 0 4.949894 0.000000 0.000000 0.0 0.0 40000.0 0.0 Sherwin 2024 2022-11-28 18:17:11+00:00 Sentinel-2
model_output["sensor"] = model_output.tile.apply(lambda x: x.split("_")[0])

Stats experiment for paragraph

model_output.groupby("experiment")["target"].agg(["count","sum"]).rename(columns={"count":"nimages", "sum":"nplumes"})
nimages nplumes
experiment
Sherwin 2023 8 7
Sherwin 2024 15 10
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay

cm = confusion_matrix(model_output.ch4_kgh_mean > 0,  
                      model_output.pred_ch4_kgh_mean > 0)
disp = ConfusionMatrixDisplay(confusion_matrix=cm)
disp.plot()
<sklearn.metrics._plot.confusion_matrix.ConfusionMatrixDisplay at 0x7f7e229eed20>
No description has been provided for this image
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(1,1,figsize=(5.75,3), tight_layout=True)

COLORS_SATELLITE = {"Sentinel-2": C0, "Landsat 8": C2}

ltys = []
for satellite in COLORS_SATELLITE:
    dsherwin2023 = model_output[(model_output["Satellite"] == satellite) &amp; (model_output["experiment"] == "Sherwin 2023")]
    dsherwin2024 = model_output[(model_output["Satellite"] == satellite) &amp; (model_output["experiment"] == "Sherwin 2024")]


    lty = ax.scatter(x=dsherwin2023["ch4_kgh_mean"], y=dsherwin2023["scene_pred"], c=COLORS_SATELLITE[satellite],marker="o",alpha=.9)
    lty2 = ax.scatter(x=dsherwin2024["ch4_kgh_mean"], y=dsherwin2024["scene_pred"], c=COLORS_SATELLITE[satellite], marker="x",alpha=.9)
    ltys.append([lty, lty2])

ax.grid(axis="x")
ax.set_xlabel("Metered emission rate (t/h)")
ax.set_ylabel("predicted probability")
ticks = np.arange(0, 7100, 1000)
ax.set_xticks(ticks, (ticks/1000).astype(int))

legend1 = plt.legend(ltys[0],["Sherwin 2023", "Sherwin 2024"], loc="center right")
legend2 = plt.legend([l[0] for l in ltys], COLORS_SATELLITE.keys(), loc=4)
ax.add_artist(legend1)

plt.savefig("figures/scatterplot_controlled_releases.pdf")
# pyplot.gca().add_artist(legend1)
No description has been provided for this image
controlled_releases_positives = model_output[(model_output.pred_ch4_kgh_mean&gt;0) &amp; (model_output.ch4_kgh_mean&gt;10)]

fig, ax = plt.subplots(1,1,figsize=(3,3), tight_layout=True)

for satame, data in controlled_releases_positives.reset_index().groupby("Satellite"):
    # ax.scatter(insitu_survey_s2_l89_gt_indexed_pred_plume.ch4_kgh_mean, insitu_survey_s2_l89_gt_indexed_pred_plume.pred_ch4_kgh_mean, label=satame)
    ax.errorbar(data.ch4_kgh_mean, data.pred_ch4_kgh_mean,
                yerr=2*data.pred_ch4_kgh_sigma,fmt="o",label=satame,color=COLORS_SATELLITE[satame])
ax.legend()
ax.set_xlabel("Metered emission rate (t/h)")
ax.set_ylabel("Estimated emission rate (t/h)")

ticks = np.arange(0, 8100, 1000)
ax.set_xticks(ticks, (ticks/1000).astype(int))
ax.set_yticks(ticks, (ticks/1000).astype(int))

ax.plot([0,8000],[0,8000] ,c="black")
ax.set_xlim(0,8000)
ax.set_ylim(0,8000)
ax.grid()
plt.savefig("figures/quantified_controlled_releases.pdf")
No description has been provided for this image