#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
from pathlib import Path

from wildfires.utils import handle_array_job_args

try:
    # This will only work after the path modification carried out in the job script.
    from specific import (
        CACHE_DIR,
        SimpleCache,
        get_model,
        data_split_cache,
        get_shap_values,
    )
except ImportError:
    """Not running as an HPC job yet."""


def func():
    # Used to re-compute specific failed jobs, `None` otherwise.
    indices = [
        2,
        3,
        49,
        87,
        88,
        99,
        108,
        148,
        152,
        153,
        170,
        238,
        239,
        275,
        276,
        296,
        297,
        302,
        303,
        348,
        349,
        350,
        373,
        440,
        441,
        442,
        454,
        515,
        532,
        551,
        552,
        579,
        581,
        603,
        607,
        608,
        618,
        620,
        625,
        646,
        647,
        648,
        669,
        686,
        701,
        743,
        750,
        752,
        850,
        852,
        867,
        894,
        896,
        944,
        945,
        980,
        983,
        984,
        985,
        987,
        989,
        990,
        991,
        1006,
        1009,
        1010,
        1020,
        1021,
        1072,
        1099,
        1182,
        1205,
        1211,
        1215,
        1237,
        1262,
        1263,
        1264,
        1271,
        1295,
        1296,
        1297,
        1310,
        1311,
        1314,
        1315,
        1316,
        1324,
        1339,
        1345,
        1390,
        1405,
        1521,
        1545,
        1546,
        1555,
        1582,
        1671,
        1835,
        1875,
        1876,
        2182,
        2399,
        2551,
        2605,
        2663,
        2726,
        2807,
        2826,
        2918,
        2971,
        2972,
        3006,
        3054,
        3055,
        3061,
        3068,
        3088,
        3092,
        3093,
        3094,
        3105,
        3108,
        3126,
        3175,
        3211,
        3215,
        3226,
        3227,
        3233,
        3235,
        3247,
        3251,
        3284,
        3290,
        3291,
        3292,
        3297,
        3308,
        3352,
        3375,
        3376,
        3392,
        3396,
        3397,
        3400,
        3444,
        3477,
        3478,
        3502,
        3515,
        3530,
        3650,
        3651,
        3675,
        3731,
        3757,
        3758,
        3768,
        3812,
        3813,
        3841,
        3852,
        3886,
        3919,
        3922,
        3925,
        3961,
        3963,
        4020,
        4021,
        4090,
        4125,
        4148,
        4177,
        4203,
        4212,
        4246,
        4247,
        4276,
        4310,
        4312,
        4339,
        4340,
        4365,
        4366,
        4371,
        4396,
        4406,
        4407,
        4408,
        4409,
        4428,
        4430,
        4431,
        4462,
        4483,
        4484,
        4486,
        4508,
        4546,
        4559,
        4595,
        4605,
        4656,
        4684,
        4698,
        4713,
        4746,
        4806,
        4826,
        4874,
        4887,
        4909,
        4993,
        4994,
        4995,
        4996,
        4997,
        5001,
        5002,
        5006,
        5008,
        5021,
        5055,
        5068,
        5084,
        5086,
        5137,
        5148,
        5187,
        5271,
        5272,
        5273,
        5274,
        5290,
        5291,
        5303,
        5304,
        5412,
        5413,
        5416,
        5432,
        5485,
        5507,
        5509,
        5511,
        5512,
        5513,
        5514,
        5515,
        5516,
        5517,
        5518,
        5519,
        5520,
        5584,
        5586,
        5607,
        5654,
        5655,
        5656,
        5658,
        5714,
        5715,
        5719,
        5720,
        5721,
        5722,
        5727,
        5728,
        5729,
        5735,
        5736,
        5739,
        5741,
        5827,
        5828,
        5887,
        5888,
        5889,
        5890,
        5891,
        5892,
        5893,
        5934,
        5935,
        5936,
        5937,
        5938,
        5939,
        5940,
        5965,
        5966,
        5967,
        5968,
    ]

    index = int(os.environ["PBS_ARRAY_INDEX"])

    if indices is not None:
        index = indices[index]

    print("Index:", index)

    X_train, X_test, y_train, y_test = data_split_cache.load()
    rf = get_model()

    job_samples = 50

    tree_path_dependent_shap_interact_cache = SimpleCache(
        f"tree_path_dependent_shap_interact_{index}_{job_samples}",
        cache_dir=os.path.join(CACHE_DIR, "shap_interaction"),
    )

    @tree_path_dependent_shap_interact_cache
    def cached_get_interact_shap_values(model, X):
        return get_shap_values(model, X, interaction=True)

    cached_get_interact_shap_values(
        rf, X_train[index * job_samples : (index + 1) * job_samples]
    )


if __name__ == "__main__":
    handle_array_job_args(
        Path(__file__).resolve(),
        func,
        ncpus=1,
        mem="7gb",
        walltime="10:00:00",
        max_index=295,
    )

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
from pathlib import Path

from wildfires.utils import handle_array_job_args

try:
    # This will only work after the path modification carried out in the job script.
    from specific import (
        CACHE_DIR,
        SimpleCache,
        get_model,
        data_split_cache,
        get_shap_values,
    )
except ImportError:
    """Not running as an HPC job yet."""


def func():
    # Used to re-compute specific failed jobs, `None` otherwise.
    indices = [
        2,
        3,
        49,
        87,
        88,
        99,
        108,
        148,
        152,
        153,
        170,
        238,
        239,
        275,
        276,
        296,
        297,
        302,
        303,
        348,
        349,
        350,
        373,
        440,
        441,
        442,
        454,
        515,
        532,
        551,
        552,
        579,
        581,
        603,
        607,
        608,
        618,
        620,
        625,
        646,
        647,
        648,
        669,
        686,
        701,
        743,
        750,
        752,
        850,
        852,
        867,
        894,
        896,
        944,
        945,
        980,
        983,
        984,
        985,
        987,
        989,
        990,
        991,
        1006,
        1009,
        1010,
        1020,
        1021,
        1072,
        1099,
        1182,
        1205,
        1211,
        1215,
        1237,
        1262,
        1263,
        1264,
        1271,
        1295,
        1296,
        1297,
        1310,
        1311,
        1314,
        1315,
        1316,
        1324,
        1339,
        1345,
        1390,
        1405,
        1521,
        1545,
        1546,
        1555,
        1582,
        1671,
        1835,
        1875,
        1876,
        2182,
        2399,
        2551,
        2605,
        2663,
        2726,
        2807,
        2826,
        2918,
        2971,
        2972,
        3006,
        3054,
        3055,
        3061,
        3068,
        3088,
        3092,
        3093,
        3094,
        3105,
        3108,
        3126,
        3175,
        3211,
        3215,
        3226,
        3227,
        3233,
        3235,
        3247,
        3251,
        3284,
        3290,
        3291,
        3292,
        3297,
        3308,
        3352,
        3375,
        3376,
        3392,
        3396,
        3397,
        3400,
        3444,
        3477,
        3478,
        3502,
        3515,
        3530,
        3650,
        3651,
        3675,
        3731,
        3757,
        3758,
        3768,
        3812,
        3813,
        3841,
        3852,
        3886,
        3919,
        3922,
        3925,
        3961,
        3963,
        4020,
        4021,
        4090,
        4125,
        4148,
        4177,
        4203,
        4212,
        4246,
        4247,
        4276,
        4310,
        4312,
        4339,
        4340,
        4365,
        4366,
        4371,
        4396,
        4406,
        4407,
        4408,
        4409,
        4428,
        4430,
        4431,
        4462,
        4483,
        4484,
        4486,
        4508,
        4546,
        4559,
        4595,
        4605,
        4656,
        4684,
        4698,
        4713,
        4746,
        4806,
        4826,
        4874,
        4887,
        4909,
        4993,
        4994,
        4995,
        4996,
        4997,
        5001,
        5002,
        5006,
        5008,
        5021,
        5055,
        5068,
        5084,
        5086,
        5137,
        5148,
        5187,
        5271,
        5272,
        5273,
        5274,
        5290,
        5291,
        5303,
        5304,
        5412,
        5413,
        5416,
        5432,
        5485,
        5507,
        5509,
        5511,
        5512,
        5513,
        5514,
        5515,
        5516,
        5517,
        5518,
        5519,
        5520,
        5584,
        5586,
        5607,
        5654,
        5655,
        5656,
        5658,
        5714,
        5715,
        5719,
        5720,
        5721,
        5722,
        5727,
        5728,
        5729,
        5735,
        5736,
        5739,
        5741,
        5827,
        5828,
        5887,
        5888,
        5889,
        5890,
        5891,
        5892,
        5893,
        5934,
        5935,
        5936,
        5937,
        5938,
        5939,
        5940,
        5965,
        5966,
        5967,
        5968,
    ]

    index = int(os.environ["PBS_ARRAY_INDEX"])

    if indices is not None:
        index = indices[index]

    print("Index:", index)

    X_train, X_test, y_train, y_test = data_split_cache.load()
    rf = get_model()

    job_samples = 50

    tree_path_dependent_shap_interact_cache = SimpleCache(
        f"tree_path_dependent_shap_interact_{index}_{job_samples}",
        cache_dir=os.path.join(CACHE_DIR, "shap_interaction"),
    )

    @tree_path_dependent_shap_interact_cache
    def cached_get_interact_shap_values(model, X):
        return get_shap_values(model, X, interaction=True)

    cached_get_interact_shap_values(
        rf, X_train[index * job_samples : (index + 1) * job_samples]
    )


if __name__ == "__main__":
    handle_array_job_args(
        Path(__file__).resolve(),
        func,
        ncpus=1,
        mem="7gb",
        walltime="10:00:00",
        max_index=295,
    )

#!/usr/bin/env python3 # -*- coding: utf-8 -*- # This will only work after the path modification carried out in the job script. Not running as an HPC job yet. # Used to re-compute specific failed jobs, `None` otherwise.