import numpy as np
import pandas as pd
import networkx as nx
from pgmpy.readwrite import BIFReader
rename_mapping_mildew = {
    # Crop status – dry mass (kg m-2)
    'dm_1': 'Dry Mass (kg m⁻²) at Stage 1',
    'dm_2': 'Dry Mass (kg m⁻²) at Stage 2',
    'dm_3': 'Dry Mass (kg m⁻²) at Stage 3',
    'dm_4': 'Dry Mass (kg m⁻²) at Stage 4',

    # Crop status – photosynthesis (kg m-2)
    'foto_1': 'Photosynthetic Rate (kg m⁻²) at Stage 1',
    'foto_2': 'Photosynthetic Rate (kg m⁻²) at Stage 2',
    'foto_3': 'Photosynthetic Rate (kg m⁻²) at Stage 3',
    'foto_4': 'Photosynthetic Rate (kg m⁻²) at Stage 4',

    # Meteorology – radiation (MJ m-2)
    'straaling_1': 'Solar Radiation (MJ m⁻²) during Stage 1',
    'straaling_2': 'Solar Radiation (MJ m⁻²) during Stage 2',
    'straaling_3': 'Solar Radiation (MJ m⁻²) during Stage 3',
    'straaling_4': 'Solar Radiation (MJ m⁻²) during Stage 4',

    # Meteorology – temperature (°C)
    'temp_1': 'Temperature (°C) during Stage 1',
    'temp_2': 'Temperature (°C) during Stage 2',
    'temp_3': 'Temperature (°C) during Stage 3',
    'temp_4': 'Temperature (°C) during Stage 4',

    # Leaf area index
    'lai_0': 'Initial Leaf Area Index',
    'lai_1': 'Leaf Area Index after Stage 1',
    'lai_2': 'Leaf Area Index after Stage 2',
    'lai_3': 'Leaf Area Index after Stage 3',
    'lai_4': 'Leaf Area Index after Stage 4',

    # Disease – mildew percentage
    'meldug_1': 'Mildew Infestation (%) at Stage 1',
    'meldug_2': 'Mildew Infestation (%) at Stage 2',
    'meldug_3': 'Mildew Infestation (%) at Stage 3',
    'meldug_4': 'Mildew Infestation (%) at Stage 4',

    # Micro-climate categorical states
    'mikro_1': 'Micro-climate Classified during Stage 1',
    'mikro_2': 'Micro-climate Classified during Stage 2',
    'mikro_3': 'Micro-climate Classified during Stage 3',

    # Management – fungicide dose (l ha-1)
    'middel_1': 'Fungicide Dose (l ha⁻¹) during Stage 1',
    'middel_2': 'Fungicide Dose (l ha⁻¹) during Stage 2',
    'middel_3': 'Fungicide Dose (l ha⁻¹) during Stage 3',

    # Hydrology – precipitation
    'nedboer_1': 'Precipitation (mm) during Stage 1',
    'nedboer_2': 'Precipitation (mm) during Stage 2',
    'nedboer_3': 'Precipitation (mm) during Stage 3',

    # Outcome
    'udbytte': 'Grain Yield (hkg ha⁻¹)'
}

ils_desc = {
    "dm_1": "Represents the observed dry mass of the winter wheat at stage 1 of growth. The values are given in kg/m².",
    "dm_2": "Represents the observed dry mass of the winter wheat at stage 2 of growth. The values are given in kg/m².",
    "dm_3": "Represents the observed dry mass of the winter wheat at stage 3 of growth. The values are given in kg/m².",
    "dm_4": "Represents the observed dry mass of the winter wheat at stage 4 of growth. The values are given in kg/m².",
    "foto_1": "Represents the observed photosynthetic rate of the winter wheat at the stage 1. The values are given in kg/m².",
    "foto_2": "Represents the observed photosynthetic rate of the winter wheat at the stage 2. The values are given in kg/m².",
    "foto_3": "Represents the observed photosynthetic rate of the winter wheat at the stage 3. The values are given in kg/m².",
    "foto_4": "Represents the observed photosynthetic rate of the winter wheat at the stage 4. The values are given in kg/m².",
    "straaling_1": "Represents solar radiation received by the winter wheat during the stage 1, preset with fixed value. The values are given in MJ/m².",
    "straaling_2": "Represents solar radiation received by the winter wheat during the stage 2, preset with fixed value. The values are given in MJ/m².",
    "straaling_3": "Represents solar radiation received by the winter wheat during the stage 3, preset with fixed value. The values are given in MJ/m².",
    "straaling_4": "Represents solar radiation received by the winter wheat during the stage 4, preset with fixed value. The values are given in MJ/m².",
    "temp_1": "Represents environmental temperature at the stage 1 of winter wheat growth, preset with fixed value. The values are given in degrees Celsius.",
    "temp_2": "Represents environmental temperature at the stage 2 of winter wheat growth, preset with fixed value. The values are given in degrees Celsius.",
    "temp_3": "Represents environmental temperature at the stage 3 of winter wheat growth, preset with fixed value. The values are given in degrees Celsius.",
    "temp_4": "Represents environmental temperature at the stage 4 of winter wheat growth, preset with fixed value. The values are given in degrees Celsius.",
    "lai_0": "Represents the Initial Leaf Area Index (LAI) of the winter wheat, preset in stage 0 and observed in following stages. LAI is a measure of the amount of leaf area per unit ground area.",
    "lai_1": "Represents the observed Leaf Area Index (LAI) of the winter wheat at the stage 1. LAI is a measure of the amount of leaf area per unit ground area.",
    "lai_2": "Represents the observed Leaf Area Index (LAI) of the winter wheat at the stage 2. LAI is a measure of the amount of leaf area per unit ground area.",
    "lai_3": "Represents the observed Leaf Area Index (LAI) of the winter wheat at the stage 3. LAI is a measure of the amount of leaf area per unit ground area.",
    "lai_4": "Represents the observed Leaf Area Index (LAI) of the winter wheat at the stage 4. LAI is a measure of the amount of leaf area per unit ground area.",
    "meldug_1": "Represents the degree of mildew infestation at the stage 1 of winter wheat growth, preset in stage 1. The values are given as a percentage.",
    "meldug_2": "Represents the degree of observed mildew infestation at the stage 2 of winter wheat growth. The values are given as a percentage.",
    "meldug_3": "Represents the degree of observed mildew infestation at the stage 3 of winter wheat growth. The values are given as a percentage.",
    "meldug_4": "Represents the degree of observed mildew infestation at the stage 4 of winter wheat growth. The values are given as a percentage.",
    "mikro_1": "Represents the microclimate environment classified of the winter wheat during the stage 1. The values can be dry and cold (T_rt_og_koldt), wet and cold (V_dt_og_koldt), dry and warm (T_rt_og_varmt), or wet and warm (V_dt_og_varmt).",
    "mikro_2": "Represents the microclimate environment classified of the winter wheat during the stage 2. The values can be dry and cold (T_rt_og_koldt), wet and cold (V_dt_og_koldt), dry and warm (T_rt_og_varmt), or wet and warm (V_dt_og_varmt).",
    "mikro_3": "Represents the microclimate environment classified of the winter wheat during the stage 3. The values can be dry and cold (T_rt_og_koldt), wet and cold (V_dt_og_koldt), dry and warm (T_rt_og_varmt), or wet and warm (V_dt_og_varmt).",
    "middel_1": "Represents the average amount of fungicide set to a fixed value at the stage 1 of winter wheat growth. The values are given in liters per hectare (l/ha).",
    "middel_2": "Represents the average amount of fungicide set to a fixed value at the stage 2 of winter wheat growth. The values are given in liters per hectare (l/ha).",
    "middel_3": "Represents the average amount of fungicide set to a fixed value at the stage 3 of winter wheat growth. The values are given in liters per hectare (l/ha).",
    "nedboer_1": "Represents the amount of precipitation during the stage 1 of winter wheat growth. The values are given in integer format.",
    "nedboer_2": "Represents the amount of precipitation during the stage 2 of winter wheat growth. The values are given in integer format.",
    "nedboer_3": "Represents the amount of precipitation during the stage 3 of winter wheat growth. The values are given in integer format.",
    "udbytte": "Represents the yield of final winter wheat crop (at stage 4). The values are given in hectograms per hectare (hkg/ha)."
}

variable_description_mildew = {
    rename_mapping_mildew[k]: v for k, v in ils_desc.items() if k in rename_mapping_mildew
}

states_by_var = {
    # ───── Stage-1 ─────
    'dm_1': [
        '0_00_kg_m2','0_02_kg_m2','0_04_kg_m2','0_06_kg_m2','0_08_kg_m2','0_10_kg_m2',
        '0_12_kg_m2','0_14_kg_m2','0_16_kg_m2','0_18_kg_m2','0_20_kg_m2','0_22_kg_m2',
        '0_24_kg_m2','0_26_kg_m2','0_28_kg_m2','0_30_kg_m2','0_32_kg_m2','0_34_kg_m2',
        '0_36_kg_m2','0_38_kg_m2','0_40_kg_m2','0_42_kg_m2','0_44_kg_m2','0_46_kg_m2',
        '0_48_kg_m2','0_50_kg_m2','0_52_kg_m2','0_54_kg_m2','0_56_kg_m2','0_58_kg_m2',
        '0_60_kg_m2','0_62_kg_m2'
    ],
    'foto_1': [
        '0_00_kg_m2','0_05_kg_m2','0_10_kg_m2','0_15_kg_m2','0_20_kg_m2','0_25_kg_m2',
        '0_30_kg_m2','0_35_kg_m2','0_40_kg_m2','0_45_kg_m2','0_50_kg_m2','0_55_kg_m2',
        '0_60_kg_m2','0_65_kg_m2','0_70_kg_m2','0_75_kg_m2','0_80_kg_m2','0_85_kg_m2',
        '0_90_kg_m2','0_95_kg_m2'
    ],
    'straaling_1': [
        '___150_MJ_m2','150___199_MJ_m2','200___249_MJ_m2','____250_MJ_m2'
    ],
    'temp_1': [
        '___13_0_C','13_0___14_9_C','15_0___16_9_C','____17_0_C'
    ],
    'lai_1': [
        '0_0___0_5','0_5___1_5','1_5___2_5','2_5___3_5',
        '3_5___4_5','4_5___5_5','5_5___6_5','___6_5'
    ],
    'meldug_1': [
        '0_00___0_01_%', '0_01___0_5_%', '0_5___1_%', '1___2_%', '2___6_%', '6___10_%', '10___25_%', '25___50_%', '50___100_%'
    ],
    'lai_0': [
        '0_0___0_5','0_5___1_5','1_5___2_5','2_5___3_5',
        '3_5___4_5','4_5___5_5','5_5___6_5','___6_5'
    ],

    # ───── Stage-2 ─────
    'dm_2': [
        '0_00_kg_m2','0_02_kg_m2','0_04_kg_m2','0_06_kg_m2','0_08_kg_m2','0_10_kg_m2',
        '0_12_kg_m2','0_14_kg_m2','0_16_kg_m2','0_18_kg_m2','0_20_kg_m2','0_22_kg_m2',
        '0_24_kg_m2','0_26_kg_m2','0_28_kg_m2','0_30_kg_m2','0_32_kg_m2','0_34_kg_m2',
        '0_36_kg_m2','0_38_kg_m2','0_40_kg_m2','0_42_kg_m2','0_44_kg_m2','0_46_kg_m2',
        '0_48_kg_m2','0_50_kg_m2','0_52_kg_m2','0_54_kg_m2','0_56_kg_m2','0_58_kg_m2',
        '0_60_kg_m2','0_62_kg_m2','0_64_kg_m2','0_66_kg_m2','0_68_kg_m2','0_70_kg_m2',
        '0_72_kg_m2','0_74_kg_m2','0_76_kg_m2','0_78_kg_m2','0_80_kg_m2','0_82_kg_m2',
        '0_84_kg_m2','0_86_kg_m2','0_88_kg_m2','0_90_kg_m2','0_92_kg_m2','0_94_kg_m2',
        '0_96_kg_m2','0_98_kg_m2','1_00_kg_m2','1_02_kg_m2','1_04_kg_m2','1_06_kg_m2',
        '1_08_kg_m2','1_10_kg_m2','1_12_kg_m2','1_14_kg_m2','1_16_kg_m2','1_18_kg_m2',
        '1_20_kg_m2'
    ],
    'foto_2': [
        '0_00_kg_m2','0_05_kg_m2','0_10_kg_m2','0_15_kg_m2','0_20_kg_m2','0_25_kg_m2',
        '0_30_kg_m2','0_35_kg_m2','0_40_kg_m2','0_45_kg_m2','0_50_kg_m2','0_55_kg_m2',
        '0_60_kg_m2','0_65_kg_m2','0_70_kg_m2','0_75_kg_m2','0_80_kg_m2','0_85_kg_m2',
        '0_90_kg_m2','0_95_kg_m2'
    ],
    'straaling_2': [
        '___150_MJ_m2','150___199_MJ_m2','200___249_MJ_m2','____250_MJ_m2'
    ],
    'temp_2': [
        '___13_0_C','13_0___14_9_C','15_0___16_9_C','____17_0_C'
    ],
    'lai_2': [
        '0_0___0_5','0_5___1_5','1_5___2_5','2_5___3_5',
        '3_5___4_5','4_5___5_5','5_5___6_5','___6_5'
    ],
    'meldug_2': [
        '0_00___0_01__','0_01___0_5__','0_5___1__','1___2__',
        '2___6__','6___10__','10___25__','25___50__','50___100__'
    ],

    # ───── Stage-3 ─────
    'dm_3': [
        '0_00_kg_m2', '0_02_kg_m2', '0_04_kg_m2', '0_06_kg_m2', '0_08_kg_m2', '0_10_kg_m2', '0_12_kg_m2', '0_14_kg_m2', '0_16_kg_m2', '0_18_kg_m2', '0_20_kg_m2', '0_22_kg_m2', '0_24_kg_m2', '0_26_kg_m2', '0_28_kg_m2', '0_30_kg_m2', '0_32_kg_m2', '0_34_kg_m2', '0_36_kg_m2', '0_38_kg_m2', '0_40_kg_m2', '0_42_kg_m2', '0_44_kg_m2', '0_46_kg_m2', '0_48_kg_m2', '0_50_kg_m2', '0_52_kg_m2', '0_54_kg_m2', '0_56_kg_m2', '0_58_kg_m2', '0_60_kg_m2', '0_62_kg_m2', '0_64_kg_m2', '0_66_kg_m2', '0_68_kg_m2', '0_70_kg_m2', '0_72_kg_m2', '0_74_kg_m2', '0_76_kg_m2', '0_78_kg_m2', '0_80_kg_m2', '0_82_kg_m2', '0_84_kg_m2', '0_86_kg_m2', '0_88_kg_m2', '0_90_kg_m2', '0_92_kg_m2', '0_94_kg_m2', '0_96_kg_m2', '0_98_kg_m2', '1_00_kg_m2', '1_02_kg_m2', '1_04_kg_m2', '1_06_kg_m2', '1_08_kg_m2', '1_10_kg_m2', '1_12_kg_m2', '1_14_kg_m2', '1_16_kg_m2', '1_18_kg_m2', '1_20_kg_m2', '1_22_kg_m2', '1_24_kg_m2', '1_26_kg_m2', '1_28_kg_m2', '1_30_kg_m2', '1_32_kg_m2', '1_34_kg_m2', '1_36_kg_m2', '1_38_kg_m2', '1_40_kg_m2', '1_42_kg_m2', '1_44_kg_m2', '1_46_kg_m2', '1_48_kg_m2', '1_50_kg_m2', '1_52_kg_m2', '1_54_kg_m2', '1_56_kg_m2', '1_58_kg_m2', '1_60_kg_m2', '1_62_kg_m2', '1_64_kg_m2', '1_66_kg_m2', '1_68_kg_m2', '1_70_kg_m2', '1_72_kg_m2', '1_74_kg_m2', '1_76_kg_m2', '1_78_kg_m2', '1_80_kg_m2', '1_82_kg_m2', '1_84_kg_m2', '1_86_kg_m2', '1_88_kg_m2', '1_90_kg_m2', '1_92_kg_m2', '1_94_kg_m2', '1_96_kg_m2', '1_98_kg_m2'
    ],
    'foto_3': [
        '0_00_kg_m2', '0_05_kg_m2', '0_10_kg_m2', '0_15_kg_m2', '0_20_kg_m2', '0_25_kg_m2', '0_30_kg_m2', '0_35_kg_m2', '0_40_kg_m2', '0_45_kg_m2', '0_50_kg_m2', '0_55_kg_m2', '0_60_kg_m2', '0_65_kg_m2', '0_70_kg_m2', '0_75_kg_m2', '0_80_kg_m2', '0_85_kg_m2', '0_90_kg_m2', '0_95_kg_m2', '1_00_kg_m2', '1_05_kg_m2', '1_10_kg_m2', '1_15_kg_m2', '1_20_kg_m2', '1_25_kg_m2', '1_30_kg_m2', '1_35_kg_m2', '1_40_kg_m2', '1_45_kg_m2', '1_50_kg_m2', '1_55_kg_m2', '1_60_kg_m2'
    ],
    'straaling_3': [
        '___300_MJ_m2','300___349_MJ_m2','350___399_MJ_m2','____400_MJ_m2'
    ],
    'temp_3': [
        '___13_0_C','13_0___14_9_C','15_0___16_9_C','____17_0_C'
    ],
    'lai_3': [
        '0_0___0_5','0_5___1_5','1_5___2_5','2_5___3_5',
        '3_5___4_5','4_5___5_5','5_5___6_5'
    ],
    'meldug_3': [
        '0_00___0_01__','0_01___0_5__','0_5___1__','1___2__',
        '2___6__','6___10__','10___25__','25___50__','50___100__'
    ],

    # ───── Stage-4 ─────
    'dm_4': [
        '0_00_kg_m2', '0_02_kg_m2', '0_04_kg_m2', '0_06_kg_m2', '0_08_kg_m2', '0_10_kg_m2', '0_12_kg_m2', '0_14_kg_m2', '0_16_kg_m2', '0_18_kg_m2', '0_20_kg_m2', '0_22_kg_m2', '0_24_kg_m2', '0_26_kg_m2', '0_28_kg_m2', '0_30_kg_m2', '0_32_kg_m2', '0_34_kg_m2', '0_36_kg_m2', '0_38_kg_m2', '0_40_kg_m2', '0_42_kg_m2', '0_44_kg_m2', '0_46_kg_m2', '0_48_kg_m2', '0_50_kg_m2', '0_52_kg_m2', '0_54_kg_m2', '0_56_kg_m2', '0_58_kg_m2', '0_60_kg_m2', '0_62_kg_m2', '0_64_kg_m2', '0_66_kg_m2', '0_68_kg_m2', '0_70_kg_m2', '0_72_kg_m2', '0_74_kg_m2', '0_76_kg_m2', '0_78_kg_m2', '0_80_kg_m2', '0_82_kg_m2', '0_84_kg_m2', '0_86_kg_m2', '0_88_kg_m2', '0_90_kg_m2', '0_92_kg_m2', '0_94_kg_m2', '0_96_kg_m2', '0_98_kg_m2', '1_00_kg_m2', '1_02_kg_m2', '1_04_kg_m2', '1_06_kg_m2', '1_08_kg_m2', '1_10_kg_m2', '1_12_kg_m2', '1_14_kg_m2', '1_16_kg_m2', '1_18_kg_m2', '1_20_kg_m2', '1_22_kg_m2', '1_24_kg_m2', '1_26_kg_m2', '1_28_kg_m2', '1_30_kg_m2', '1_32_kg_m2', '1_34_kg_m2', '1_36_kg_m2', '1_38_kg_m2', '1_40_kg_m2', '1_42_kg_m2', '1_44_kg_m2', '1_46_kg_m2', '1_48_kg_m2', '1_50_kg_m2', '1_52_kg_m2', '1_54_kg_m2', '1_56_kg_m2', '1_58_kg_m2', '1_60_kg_m2', '1_62_kg_m2', '1_64_kg_m2', '1_66_kg_m2', '1_68_kg_m2', '1_70_kg_m2', '1_72_kg_m2', '1_74_kg_m2', '1_76_kg_m2', '1_78_kg_m2', '1_80_kg_m2', '1_82_kg_m2', '1_84_kg_m2', '1_86_kg_m2', '1_88_kg_m2', '1_90_kg_m2', '1_92_kg_m2', '1_94_kg_m2', '1_96_kg_m2', '1_98_kg_m2'
    ],
    'foto_4': [
        '0_00_kg_m2', '0_05_kg_m2', '0_10_kg_m2', '0_15_kg_m2', '0_20_kg_m2', '0_25_kg_m2', '0_30_kg_m2', '0_35_kg_m2', '0_40_kg_m2', '0_45_kg_m2', '0_50_kg_m2', '0_55_kg_m2', '0_60_kg_m2', '0_65_kg_m2', '0_70_kg_m2', '0_75_kg_m2', '0_80_kg_m2', '0_85_kg_m2', '0_90_kg_m2', '0_95_kg_m2', '1_00_kg_m2', '1_05_kg_m2', '1_10_kg_m2', '1_15_kg_m2', '1_20_kg_m2', '1_25_kg_m2', '1_30_kg_m2', '1_35_kg_m2'
    ],
    'straaling_4': [
        '___300_MJ_m2','300___349_MJ_m2','350___399_MJ_m2','____400_MJ_m2'
    ],
    'temp_4': [
        '___13_0_C','13_0___14_9_C','15_0___16_9_C','____17_0_C'
    ],
    'lai_4': [
        '0_0___0_5','0_5___1_5','1_5___2_5','2_5___3_5'
    ],
    'meldug_4': [
        '0_00___0_01__','0_01___0_5__','0_5___1__','1___2__',
        '2___6__','6___10__','10___25__','25___50__','50___100__'
    ],

    # ───── Categorical micro-climate & fungicide decisions ─────
    'mikro_1': ['T_rt_og_koldt','V_dt_og_koldt','T_rt_og_varmt','V_dt_og_varmt'],
    'mikro_2': ['T_rt_og_koldt','V_dt_og_koldt','T_rt_og_varmt','V_dt_og_varmt'],
    'mikro_3': ['T_rt_og_koldt','V_dt_og_koldt','T_rt_og_varmt','V_dt_og_varmt'],

    'middel_1': ['0_l_ha','0_25_l_ha','0_50_l_ha','0_75_l_ha'],
    'middel_2': ['0_l_ha','0_25_l_ha','0_50_l_ha','0_75_l_ha'],
    'middel_3': ['0_l_ha','0_25_l_ha','0_50_l_ha','0_75_l_ha'],

    # ───── Precipitation ─────
    'nedboer_1': ['0___1','2___3','____4'],
    'nedboer_2': ['0___1','2___3','____4'],
    'nedboer_3': ['0___1','2___3','____4'],

    # ───── Final outcome ─────
    'udbytte': [
        '0___1_hkg_ha', '1___3_hkg_ha', '3___5_hkg_ha', '5___7_hkg_ha', '7___9_hkg_ha', '9___11_hkg_ha', '11___13_hkg_ha', '13___15_hkg_ha', '15___17_hkg_ha', '17___19_hkg_ha', '19___21_hkg_ha', '21___23_hkg_ha', '23___25_hkg_ha', '25___27_hkg_ha', '27___29_hkg_ha', '29___31_hkg_ha', '31___33_hkg_ha', '33___35_hkg_ha', '35___37_hkg_ha', '37___39_hkg_ha', '39___41_hkg_ha', '41___43_hkg_ha', '43___45_hkg_ha', '45___47_hkg_ha', '47___49_hkg_ha', '49___51_hkg_ha', '51___53_hkg_ha', '53___55_hkg_ha', '55___57_hkg_ha', '57___59_hkg_ha', '59___61_hkg_ha', '61___63_hkg_ha', '63___65_hkg_ha', '65___67_hkg_ha', '67___69_hkg_ha', '69___71_hkg_ha', '71___73_hkg_ha', '73___75_hkg_ha', '75___77_hkg_ha', '77___79_hkg_ha', '79___81_hkg_ha', '81___83_hkg_ha', '83___85_hkg_ha', '85___87_hkg_ha', '87___89_hkg_ha', '89___91_hkg_ha', '91___93_hkg_ha', '93___95_hkg_ha', '95___97_hkg_ha', '97___99_hkg_ha', '99___101_hkg_ha', '101___103_hkg_ha', '103___105_hkg_ha', '105___107_hkg_ha', '107___109_hkg_ha', '109___111_hkg_ha', '111___113_hkg_ha', '113___115_hkg_ha', '115___117_hkg_ha', '117___119_hkg_ha', '119___121_hkg_ha', '121___123_hkg_ha', '123___125_hkg_ha', '125___127_hkg_ha', '127___129_hkg_ha', '129___131_hkg_ha', '131___133_hkg_ha', '133___135_hkg_ha', '135___137_hkg_ha', '137___139_hkg_ha', '139___141_hkg_ha', '141___143_hkg_ha', '143___145_hkg_ha', '145___147_hkg_ha', '147___149_hkg_ha', '149___151_hkg_ha', '151___153_hkg_ha', '153___155_hkg_ha', '155___157_hkg_ha', '157___159_hkg_ha', '159___161_hkg_ha', '161___163_hkg_ha', '163___165_hkg_ha', '165___167_hkg_ha', '167___169_hkg_ha', '___169_hkg_ha'
    ]
}

value_mappings_mildew = {
    rename_mapping_mildew[var]: {state: idx for idx, state in enumerate(states)}
    for var, states in states_by_var.items()
}

dataset_description_mildew = (
"A dataset of winter wheat mildew control, where the observed variables are in different stages."
"Each record represents one growing season split into four phenological stages (stem-elongation to grain-filling). "
"For every stage the data contain crop status variables (dry mass, photosynthetic rate, leaf area index), "
"meteorological inputs (temperature, solar radiation, precipitation), micro-climate categories, mildew infection "
"levels, and the fungicide decision taken. Stage-0 LAI is an initial condition. The target variable 'udbytte' "
"captures final grain yield in hectograms per hectare. "
)

def fetch_mildew():
    df = pd.read_csv(f'/net/dali/home/mscbio/rul98/CausalLLM/data/mildew_20000.csv')

    # Rename columns
    df = df.rename(columns=rename_mapping_mildew)

    # Apply value mappings
    for col, mapping in value_mappings_mildew.items():
        if col in df.columns:
            df[col] = df[col].astype("str").map(mapping).astype("Int64")  

    reader = BIFReader(f'/net/dali/home/mscbio/rul98/CausalLLM/data/mildew.bif')
    G_model = reader.get_model()

    # Create a directed graph from the edges
    GroundTruth = nx.DiGraph()
    GroundTruth.add_nodes_from(G_model.nodes())
    GroundTruth.add_edges_from(G_model.edges())
    GroundTruth = nx.relabel_nodes(GroundTruth, rename_mapping_mildew)
    pos_data = nx.spring_layout(GroundTruth)
    # print(set(GroundTruth.nodes()) - set(df.columns), set(df.columns) - set(GroundTruth.nodes()))
    return df, GroundTruth, pos_data