Skip to content

preview_submission

Logic to preview submission predictions.

kelp.nn.inference.preview_submission.PreviewSubmissionConfig

Bases: ConfigBase

Config for previewing the submission files.

Source code in kelp/nn/inference/preview_submission.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class PreviewSubmissionConfig(ConfigBase):
    """Config for previewing the submission files."""

    test_data_dir: Path
    output_dir: Path
    submission_dir: Path
    first_n: int = 10

    @field_validator("submission_dir", mode="before")
    def validate_submission_dir(cls, value: Union[str, Path]) -> Union[str, Path]:
        if value == "latest":
            submission_dir = sorted([d for d in Path("data/submissions").iterdir() if d.is_dir()])[-1]
            return submission_dir
        return value

kelp.nn.inference.preview_submission.main

Main entrypoint for plotting sample predictions from submission directory.

Source code in kelp/nn/inference/preview_submission.py
83
84
85
86
87
88
89
90
91
def main() -> None:
    """Main entrypoint for plotting sample predictions from submission directory."""
    cfg = parse_args()
    plot_first_n_samples(
        data_dir=cfg.test_data_dir,
        submission_dir=cfg.submission_dir,
        output_dir=cfg.output_dir / cfg.submission_dir.name,
        n=cfg.first_n,
    )

kelp.nn.inference.preview_submission.parse_args

Parse command line arguments.

Returns: An instance of PreviewSubmissionConfig.

Source code in kelp/nn/inference/preview_submission.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def parse_args() -> PreviewSubmissionConfig:
    """
    Parse command line arguments.

    Returns: An instance of PreviewSubmissionConfig.

    """
    parser = argparse.ArgumentParser()
    parser.add_argument("--test_data_dir", type=str, required=True)
    parser.add_argument("--output_dir", type=str, required=True)
    parser.add_argument("--submission_dir", type=str, required=True)
    parser.add_argument("--first_n", type=int, default=10)
    args = parser.parse_args()
    cfg = PreviewSubmissionConfig(**vars(args))
    cfg.log_self()
    cfg.output_dir.mkdir(exist_ok=True, parents=True)
    return cfg

kelp.nn.inference.preview_submission.plot_first_n_samples

Plots first N samples from the submission directory.

Parameters:

Name Type Description Default
data_dir Path

The path to the data directory with files used to generate predictions in the submission dir.

required
submission_dir Path

The path to the submission directory.

required
output_dir Path

The path to the output directory.

required
n int

The number of samples to plot.

10
Source code in kelp/nn/inference/preview_submission.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def plot_first_n_samples(data_dir: Path, submission_dir: Path, output_dir: Path, n: int = 10) -> None:
    """
    Plots first N samples from the submission directory.

    Args:
        data_dir: The path to the data directory with files used to generate predictions in the submission dir.
        submission_dir: The path to the submission directory.
        output_dir: The path to the output directory.
        n: The number of samples to plot.

    """
    output_dir.mkdir(exist_ok=True, parents=True)
    for fp in tqdm(sorted(list(data_dir.glob("*.tif")))[:n], "Plotting predictions"):
        tile = fp.stem.split("_")[0]
        with rasterio.open(data_dir / f"{tile}_satellite.tif") as src:
            input = src.read()
        with rasterio.open(submission_dir / "predictions" / f"{tile}_kelp.tif") as src:
            prediction = src.read(1)
        fig = plot_sample(
            input_arr=input,
            predictions_arr=prediction,
            suptitle=tile,
        )
        fig.savefig(output_dir / f"{fp.stem}.png")
        plt.close(fig)