Skip to content

Task 23/tab pfn testing #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
data
.venv
.idea
**/__pycache__
**/__pycache__
src/Load_data/load_model_to_s3.py
22 changes: 22 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from src.models.RandomForestClassifier.fire_stats_model_training import preprocess_data
from src.models.DataPreprocessing import extract
from src.models.TabPFN.TabPFN import TabPFN_model

# fire_data = extract()
# buffer_data = train_fire_data(fire_data)
# upload_model_to_s3(
# pickle_buffer=buffer_data,
# bucket_name = FIRE_PREDICTION_S3_BUCKET,
# object_key = FIRE_PREDICTION_OBJECT_KEY
# )

fire_data = extract()
X_train, X_test, y_train, y_test = preprocess_data(fire_data)

random_forest_model(X_train, X_test, y_train, y_test)
fcnn_model(X_train, X_test, y_train, y_test)
TabPFN_model(X_train, X_test, y_train, y_test)




13 changes: 13 additions & 0 deletions src/Load_data/load_model_to_s3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import boto3
from src.configurations.config import region

def upload_model_to_s3(pickle_buffer, bucket_name, object_key):
s3 = boto3.client('s3', aws_access_key_id= 'AKIAQUFLQIGOKPK3W5GG', aws_secret_access_key = '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', region_name = region)
pickle_buffer.seek(0)
s3.upload_fileobj(pickle_buffer, bucket_name, object_key)






8 changes: 7 additions & 1 deletion src/configurations/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
from constants import DEBUG_LOG

FIRE_STATISTICS_CSV = getPathFromEnv()('FIRE_STATISTICS_CSV')

FIRE_PREDICTION_S3_BUCKET = 'fire-prediction-Load_data'
FIRE_PREDICTION_OBJECT_KEY = 's3://fire-prediction-Load_data/fire_prediction/fire_occurence.pkl'
# aws_access_key_id = getPathFromEnv()('AWS_ACCESS_KEY_ID')
# aws_secret_key = getPathFromEnv()('AWS_SECRET_ACCESS_KEY')
region = getPathFromEnv()('AWS_REGION')
# FIRE_PREDICTION_S3_BUCKET = getPathFromEnv()('FIRE_PREDICTION_MODEL_S3')
# FIRE_PREDICTION_OBJECT_KEY = getPathFromEnv()('FIRE_PREDICTION_S3_OBJECT')
if DEBUG_LOG:
print(FIRE_STATISTICS_CSV)
print("Config loaded successfully")
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def extract():

fire_df = pd.read_csv(file_path, low_memory=False)

fire_df.drop(['X', 'Y', 'OBJECTID', 'FIRE_NAME', 'LOCAL_FIRE_NUMBER', 'LOCATION', 'TOWNSHIP', 'RANGE', 'SECTION', 'SUB_SECTION', 'REPORT_UNIT', 'REPORT_UNIT_NAME', 'DISTRICT',
fire_df.drop(['X', 'Y', 'OBJECTID', 'FIRE_NAME', 'LOCAL_FIRE_NUMBER', 'LOCATION', 'TOWNSHIP', 'RANGE', 'SECTION', 'SUB_SECTION', 'REPORT_UNIT', 'DISTRICT',
'FIRE_NUMBER', 'ADMIN_UNIT', 'PROTECTION_UNIT', 'PROTECTION_UNIT_NAME', 'OWNERSHIP_UNIT', 'OWNERSHIP_UNIT_NAME', 'TOPO_LANDFORM_ORIGIN',
'STATE_CODE', 'COUNTY', 'COUNTY_NAME', 'COUNTY_STATE_CODE', 'FIRE_MANAGEMENT_CODE', 'DISCOVERED_BY_DESCR', 'OBJECTIVES',
'COMPLEX_FIRE', 'COMPLEX_NAME', 'CONTAINED', 'AGENCY_ACRES', 'OTHER_ACRES_INSIDE', 'OTHER_ACRES_OUTSIDE', 'FIRE_SIZE_CLASS',
Expand All @@ -30,7 +30,6 @@ def extract():
fire_df["DISCOVER_YEAR"] = fire_df["DISCOVER_YEAR"].fillna(0).astype(int)

fire_df["IGNITION"] = fire_df["IGNITION"].fillna('N/A').astype(str)

fire_df["DISCOVERY"] = fire_df["DISCOVERY"].fillna('N/A').astype(str)

fire_df["STATISTICAL_CAUSE"] = fire_df["STATISTICAL_CAUSE"].str.split('-').str[-1].str.strip()
Expand All @@ -50,4 +49,15 @@ def extract():

fire_df["ELEVATION"] = fire_df["ELEVATION"].fillna(0.0).astype(float)

intensity_map = {
"Flame Length 0-2'": 1, "Flame Length >2-4'": 2, "Flame Length >4-6'": 3,
"Flame Length >6-8'": 4, "Flame Length >8-12'": 5, "Flame Length >12'": 6,
"HISTORICAL": 0, "N/A": 0
}
fire_df["FIRE_INTENSITY_LEVEL"] = fire_df["FIRE_INTENSITY_LEVEL"].map(intensity_map)
#
# # fire_df["FIRE_OCCURRENCE"] = fire_df["FIRE_INTENSITY_LEVEL"].apply(lambda x: 1 if x > 0 else 0)

fire_df = fire_df.iloc[50000:60000]

return fire_df
76 changes: 76 additions & 0 deletions src/models/RandomForestClassifier/fire_stats_model_training.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.metrics import accuracy_score, classification_report
from sklearn.ensemble import RandomForestClassifier
from sklearn.utils.class_weight import compute_class_weight
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, BatchNormalization, Input
from tensorflow.keras.callbacks import EarlyStopping
import os
os.environ["TABPFN_ALLOW_CPU_LARGE_DATASET"] = "1"


def preprocess_data(fire_df):
le = LabelEncoder()
fire_df["ASPECT"] = le.fit_transform(fire_df["ASPECT"])
fire_df["STATISTICAL_CAUSE"] = le.fit_transform(fire_df["STATISTICAL_CAUSE"])
fire_df["STATE_NAME"] = le.fit_transform(fire_df["STATE_NAME"])

X = fire_df[["STATE_NAME", "LATITUDE", "LONGITUDE","SLOPE", "ASPECT", "ELEVATION"]]
y = fire_df["FIRE_INTENSITY_LEVEL"]

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

return train_test_split(X_scaled, y, test_size=0.2, random_state=0, stratify=y)

def random_forest_model(X_train, X_test, y_train, y_test):
model = RandomForestClassifier(n_estimators=200, max_depth=20, min_samples_split=10,
min_samples_leaf=4, max_features='sqrt', random_state=0, n_jobs = -1)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)

print("Random Forest Results:")
print("Accuracy:", accuracy_score(y_test, y_pred))
print(classification_report(y_test, y_pred))

def fcnn_model(X_train, X_test, y_train, y_test):
num_classes = len(np.unique(y_train))
class_weights = compute_class_weight(class_weight='balanced', classes=np.unique(y_train), y=y_train)
class_weights_dict = {i: weight for i, weight in enumerate(class_weights)}

model = Sequential([
Input(shape=(X_train.shape[1],)),
Dense(256, activation='relu'),
BatchNormalization(),
Dropout(0.4),
Dense(128, activation='relu'),
BatchNormalization(),
Dropout(0.3),
Dense(64, activation='relu'),
BatchNormalization(),
Dropout(0.2),
Dense(num_classes, activation='softmax')
])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

early_stop = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)

model.fit(
X_train, y_train,
validation_split=0.2,
epochs=100,
batch_size=32,
class_weight=class_weights_dict,
callbacks=[early_stop],
verbose=0
)

y_pred_prob = model.predict(X_test)
y_pred = y_pred_prob.argmax(axis=1)

print("FCNN Results:")
print("Accuracy:", accuracy_score(y_test, y_pred))
print(classification_report(y_test, y_pred))
28 changes: 28 additions & 0 deletions src/models/TabPFN/TabPFN.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import torch
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.metrics import accuracy_score
from tabpfn_client import init, TabPFNClassifier
import os
os.environ["TABPFN_ALLOW_CPU_LARGE_DATASET"] = "1"

def TabPFN_model(X_train, X_test, y_train, y_test):
# clf = TabPFNClassifier()
# clf.fit(X_train, y_train)
# y_pred = clf.predict(X_test)
# test_accuracy = clf.score(X_test, y_pred)
# print(f"Accuracy: {test_accuracy:.2f}")

# clf = AutoTabPFNClassifier(device="cuda" if torch.cuda.is_available() else "cpu")
param_grid = {
'n_estimators': [8, 16, 32],
'softmax_temperature': [0.7, 1.0, 1.3]
}
grid_search = GridSearchCV(TabPFNClassifier(), param_grid, cv=3)

grid_search.fit(X_train, y_train)

print(f"Best parameters: {grid_search.best_params_}")
y_grid_pred = grid_search.predict(X_test)
grid_accuracy = accuracy_score(y_test, y_grid_pred)
print(grid_accuracy)

28 changes: 0 additions & 28 deletions src/processor/fire_stats_model_training.py

This file was deleted.