Skip to content

predict_and_submit

Logic for predicting and creating submission file for a single model.

kelp.xgb.inference.predict_and_submit.copy_run_artifacts

Copies run artifacts from run_dir to output_dir.

Parameters:

Name Type Description Default
run_dir Path

The directory to copy run artifacts from.

required
output_dir Path

The output directory.

required
Source code in kelp/xgb/inference/predict_and_submit.py
37
38
39
40
41
42
43
44
45
46
def copy_run_artifacts(run_dir: Path, output_dir: Path) -> None:
    """
    Copies run artifacts from run_dir to output_dir.

    Args:
        run_dir: The directory to copy run artifacts from.
        output_dir: The output directory.

    """
    shutil.copytree(run_dir, output_dir / run_dir.name, dirs_exist_ok=True)

kelp.xgb.inference.predict_and_submit.main

Main entrypoint for running XGBoost model predictions and creating a submission file.

Source code in kelp/xgb/inference/predict_and_submit.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def main() -> None:
    """Main entrypoint for running XGBoost model predictions and creating a submission file."""
    cfg = parse_args()
    now = datetime.utcnow().isoformat()
    out_dir = cfg.output_dir / now
    preds_dir = cfg.output_dir / now / "predictions"
    preds_dir.mkdir(exist_ok=False, parents=True)
    (out_dir / "predict_config.yaml").write_text(yaml.dump(cfg.model_dump(mode="json")))
    run_prediction(
        data_dir=cfg.data_dir,
        output_dir=preds_dir,
        model_dir=cfg.model_path,
        spectral_indices=cfg.training_config.spectral_indices,
    )
    create_submission_tar(
        preds_dir=preds_dir,
        output_dir=out_dir,
    )
    copy_run_artifacts(
        run_dir=cfg.run_dir,  # type: ignore[arg-type]
        output_dir=out_dir,
    )
    if cfg.preview_submission:
        plot_first_n_samples(
            data_dir=cfg.data_dir,
            submission_dir=out_dir,
            output_dir=out_dir / "previews",
            n=cfg.preview_first_n,
        )

kelp.xgb.inference.predict_and_submit.parse_args

Parse command line arguments.

Returns: An instance of :class:PredictAndSubmitConfig.

Source code in kelp/xgb/inference/predict_and_submit.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def parse_args() -> PredictAndSubmitConfig:
    """
    Parse command line arguments.

    Returns: An instance of :class:`PredictAndSubmitConfig`.

    """
    parser = build_prediction_arg_parser()
    parser.add_argument("--preview_submission", action="store_true")
    parser.add_argument("--submission_preview_output_dir", type=str)
    parser.add_argument("--preview_first_n", type=int, default=10)
    args = parser.parse_args()
    cfg = PredictAndSubmitConfig(**vars(args))
    cfg.log_self()
    cfg.output_dir.mkdir(exist_ok=True, parents=True)
    return cfg