352 lines
13 KiB
Python
Executable File

from django.shortcuts import render
import base.pipeline as pipeline
from dict_and_html import *
from . import methods
from .methods import PIPELINE_PATH
import random
import base.pipeline as pipeline
import os
from . handlers import home_handler, counterfactuals_handler, charts_handler, train_handler
def home(request):
# ajax request condition
if request.headers.get("X-Requested-With") == "XMLHttpRequest":
return home_handler(request.POST.get("action"), request)
if "upload" in request.session:
upload = request.session.get("upload")
else:
upload = 0
## get the type of the active dataset
datasets_types_PipelineJSON_path = os.path.join(
PIPELINE_PATH + "dataset_types_pipeline.json"
)
datasets_types_PipelineJSON = pipeline.PipelineJSON(
datasets_types_PipelineJSON_path
)
uploaded_files = datasets_types_PipelineJSON.get_keys_with_value(
"uploaded"
)
if "df_name" in request.session and request.session["df_name"] != "upload":
df_name = request.session.get("df_name")
dataset_type = datasets_types_PipelineJSON.read_from_json([df_name])
if type(dataset_type) is list:
dataset_type = dataset_type[0]
# if request.session.get("df_name") != "upload" and type(dataset_type) is not list:
if dataset_type == "tabular":
model_name_dir_path = os.path.join(PIPELINE_PATH + f"{df_name}")
data_to_display = None
if not os.path.exists(model_name_dir_path):
context = {
"dataset_type": dataset_type,
"df_name": df_name,
"data_to_display": data_to_display,
}
else:
context = {
"dataset_type": dataset_type,
"df_name": df_name,
"data_to_display": request.session.get("data_to_display"),
"features": request.session.get("features"), # error if not a list
"feature1": request.session.get("feature1"),
"feature2": request.session.get("feature2"),
"labels": request.session.get("labels"),
"curlabel": request.session.get("curlabel"),
"fig": request.session.get("fig"),
}
elif dataset_type == "timeseries":
context = {
"upload": upload,
"df_name": df_name,
"dataset_type": dataset_type,
"fig": request.session.get("fig"),
"fig1": request.session.get("fig1"),
}
else:
context = {}
else:
context = {}
context.update({"uploaded_files": uploaded_files, "upload": upload})
return render(request, "base/home.html", context)
def counterfactuals(request):
# ajax request condition
if request.headers.get("X-Requested-With") == "XMLHttpRequest":
return counterfactuals_handler(request.POST.get("action", None), request)
available_pretrained_models_info = []
if "df_name" in request.session:
df_name = request.session.get("df_name")
if df_name == "upload":
df_name = request.session.get("df_name_upload_base_name")
json_path = os.path.join(PIPELINE_PATH + f"{df_name}" + "/pipeline.json")
if not os.path.exists(json_path):
df_dir = os.path.join(PIPELINE_PATH + f"{df_name}")
if not os.path.exists(df_dir):
df_name = None
# pre trained models do not exist
context = {
"df_name": df_name,
"available_pretrained_models_info": available_pretrained_models_info,
}
else:
# if it does not exist, obviously there are no pre trained
# models
available_pretrained_models = []
jsonFile = pipeline.PipelineJSON(json_path)
if not jsonFile.key_exists("classifier"):
# pre trained models do not exist
available_pretrained_models = []
else:
# if it exists
# check the section of "classifiers"
# folder path
if jsonFile.key_exists("classifier"):
available_pretrained_models = jsonFile.read_from_json(
["classifier"]
).keys()
available_pretrained_models_info = (
methods.create_tuple_of_models_text_value(
available_pretrained_models
)
)
## get the type of the active dataset
datasets_types_PipelineJSON_path = os.path.join(
PIPELINE_PATH + "/dataset_types_pipeline.json"
)
datasets_types_PipelineJSON = pipeline.PipelineJSON(
datasets_types_PipelineJSON_path
)
dataset_type = datasets_types_PipelineJSON.read_from_json([df_name])
if type(dataset_type) is list:
dataset_type = dataset_type[0]
# model_name_path = os.path.join(
# PIPELINE_PATH + f"{df_name}" + "/trained_models/" + pre_trained_model_name
# )
# tsne = joblib.load(model_name_path + "/tsne.sav")
if dataset_type == "tabular":
context = {
"dataset_type": dataset_type,
"df_name": df_name,
"available_pretrained_models_info": available_pretrained_models_info,
}
elif dataset_type == "timeseries":
available_pretrained_models_info = (
methods.create_tuple_of_models_text_value(
available_pretrained_models
)
)
positive = 1
negative = 0
target_label_value = [negative, positive]
target_label_text = ["Negative class label", "Positive class label"]
target_label_info = zip(target_label_value, target_label_text)
available_pre_computed_counterfactuals = []
if jsonFile.key_exists("classifier"):
if jsonFile.key_exists("glacier"):
if jsonFile.key_exists("experiments"):
# applies only to glacier
# there are pre computed counterfactuals
experiments_dict = jsonFile.read_from_json(
["classifier", "glacier", "experiments"]
)
list_of_experiment_keys = list(experiments_dict.keys())
for key in list_of_experiment_keys:
available_pre_computed_counterfactuals.append(
experiments_dict[key]["constraint"]
)
print(available_pre_computed_counterfactuals)
else:
available_pre_computed_counterfactuals = None
print(available_pre_computed_counterfactuals)
context = {
"df_name": df_name,
"dataset_type": dataset_type,
"available_pretrained_models_info": available_pretrained_models_info,
"range": range(1, 6),
"target_label_info": target_label_info,
"available_pre_computed_counterfactuals": available_pre_computed_counterfactuals,
}
else:
context = {}
return render(request, "base/counterfactuals.html", context)
def train(request):
# ajax request condition
if request.headers.get("X-Requested-With") == "XMLHttpRequest":
return train_handler(request.POST.get("action"), request)
df_name = request.session.get("df_name")
if df_name == "upload":
df_name = request.session.get("df_name_upload_base_name")
## get the type of the active dataset
datasets_types_PipelineJSON_path = os.path.join(
PIPELINE_PATH + "/dataset_types_pipeline.json"
)
datasets_types_PipelineJSON = pipeline.PipelineJSON(
datasets_types_PipelineJSON_path
)
dataset_type = datasets_types_PipelineJSON.read_from_json([df_name])
if type(dataset_type) is list:
dataset_type = dataset_type[0]
if dataset_type == "tabular":
df_dir = os.path.join(PIPELINE_PATH + f"{df_name}")
if not os.path.exists(df_dir):
df_name = None
context = {"df_name": df_name}
else:
excel_file_name_path = os.path.join(
PIPELINE_PATH + f"{df_name}" + "/" + df_name + ".csv"
)
df = methods.get_dataframe(excel_file_name_path)
df.columns = df.columns.str.replace(" ", "_")
labels = list(df.select_dtypes(include=["object", "category"]).columns)
# Find binary columns (columns with only two unique values, including numerics)
binary_columns = [col for col in df.columns if df[col].nunique() == 2]
# Combine categorical and binary columns into one list
labels = list(set(labels + binary_columns))
label = random.choice(labels)
preprocessing_value = ["std", "imp", "onehot"]
preprocessing_text = ["Standardization", "Imputations", "One Hot Encoding"]
preprocessing_info = zip(preprocessing_value, preprocessing_text)
classifiers_value = ["svm", "dt", "rf", "xgb", "lr"]
classifiers_text = [
"Support Vector Machine (SVM)",
"Decision Tree (DT)",
"Random Forest (RF)",
"XGBoost (XGB)",
"Logistic Regression (LR)",
]
classifiers_info = zip(classifiers_value, classifiers_text)
context = {
"dataset_type": dataset_type,
"df_name": df_name,
"labels": labels,
"label": label,
"classifiers_info": classifiers_info,
"preprocessing_info": preprocessing_info,
}
context["fig"] = methods.get_info_of_dataframe(df)
else:
excel_file_name_path = os.path.join(
PIPELINE_PATH + f"{df_name}" + "/" + df_name + ".csv"
)
df = methods.get_dataframe(excel_file_name_path)
df.columns = df.columns.str.replace(" ", "_")
preprocessing_value = ["std", "denoise", "imp"]
preprocessing_text = [
"Standardization",
"Denoising",
"Imputations",
]
preprocessing_info = zip(preprocessing_value, preprocessing_text)
classifiers_value = ["wildboar_knn", "wildboar_rsf", "glacier"]
classifiers_text = [
"Wildboar-k-Nearest Neighbors (classifier train)",
"Wildboar-Random Shapelet Forest (classifier train)",
"Glacier 1DCNN",
]
classifiers_info = zip(classifiers_value, classifiers_text)
context = {
"dataset_type": dataset_type,
"df_name": request.session.get("df_name"),
"classifiers_info": classifiers_info,
"preprocessing_info": preprocessing_info,
}
context["fig"] = methods.get_info_of_dataframe(df)
return render(request, "base/train.html", context)
def charts(request):
# ajax request condition
if request.headers.get("X-Requested-With") == "XMLHttpRequest":
return charts_handler(request.POST.get("action"), request)
if "df_name" in request.session:
df_name = request.session["df_name"]
if df_name == "upload":
df_name = request.session.get("df_name_upload_base_name")
available_pretrained_models_info = []
json_path = os.path.join(PIPELINE_PATH + f"{df_name}" + "/pipeline.json")
if os.path.exists(json_path):
jsonFile = pipeline.PipelineJSON(json_path)
# if it does not exist, obviously there are no pre trained
# models
if not jsonFile.key_exists("classifier"):
# pre trained models do not exist
# check if dataset directory exists
df_dir = os.path.join(PIPELINE_PATH + f"{df_name}")
if not os.path.exists(df_dir):
df_name = None
context = {
"df_name": df_name,
"available_pretrained_models_info": [],
}
else:
# if it exists
# check the section of "classifiers"
# folder path
available_pretrained_models = jsonFile.read_from_json(
["classifier"]
).keys()
available_pretrained_models_info = (
methods.create_tuple_of_models_text_value(
available_pretrained_models
)
)
context = {
"df_name": df_name,
"available_pretrained_models_info": available_pretrained_models_info,
}
else:
context = {}
else:
context = {}
return render(request, "base/charts.html", context)