o
    ii>                     @   s*  d dl Z d dlZd dlZd dlmZ d dlZd dlmZ d dl	m
Z d dlmZ d dlmZ d dlmZ edZed	Zed
ZedZeeZdadadd Zdd ZdedejfddZdedejdej dejfddZ!dd Z"dd Z#defddZ$edkre  dZ%ee%Z&e'e&(  dS dS )     N)Path)get_prediction_input)update_data)retrain_model)evaluate_model_predictions)MODELS_FEATURESzmodels/fpl_model.pklmodelszdata/predictions/2526zdata/raw/2025-26/gwsc                   C   s6   t du rztta W t S  ty   da Y t S w t S )=
    Load the pre-trained model from the specified path.
    N)modeljoblibload
MODEL_PATHFileNotFoundError r   r   5/var/www/html/fantasy/fantasy_model/app/core/model.py
load_model   s   r   c                  C   sp   t du r6z%i a dd t D } t|  | D ]}t| d }t|t |< qW t S  ty5   da Y t S w t S )r	   Nc                 S   s$   g | ]}|  r|jd kr|jqS )zfpl_model.pkl)is_filenamestem).0fr   r   r   
<listcomp>+   s   $ z%load_other_models.<locals>.<listcomp>z.pkl)r   
MODELS_DIRiterdirprintr   r   r   )Zmodel_namesr   Z
model_pathr   r   r   load_other_models"   s   r   gwreturnc              	   C   s2  t du rtdtd|  d t| }td|  d |d }|jg ddd	}t j}t|t|j }|rAt	d
|  td|  d t 
|}td|  d t||djdd}|d dt|d< |d t|d< |jtd|  d dd td|  dtd|  d   |S )z
    Predict the target variable using the pre-trained model.
    
    :param features: DataFrame containing the features for prediction.
    :return: Series of predictions.
    NzEModel is not loaded. Please load the model before making predictions.z&Fetching Prediction Input Data for GW z	 Started. completed.id)r   Zteamr   ignore)columnserrorsz Missing features in input data: zData Preparation for GW z prediction completed.zMain model predictions for GW )r   xPbyr#   r   r   _predictions.csvFindexPredictions for GW z
 saved to )r
   
ValueErrorloggerinfor   dropZfeature_names_in_setr!   warningpredictpd	DataFramesort_valuesroundastypeintto_csvPREDICTIONS_PATH)r   Z
input_dataidsZmodel_feature_namesmissing_features
predictionresultr   r   r   r0   5   s2   	
"r0   featuresr9   c              
   C   sX  t  }|d u r	d S ztd|ijddt}| D ]h\}}|tv rlt| }t|t|j	 }|r>t
d| d|  |rF||  n| }	||	}
|
|d< |d dt|d< t
d|  d| d	 n	t
d
| d |jt| d|  d dd qt
d|  d W d S  ty } zt
d|  W Y d }~d S d }~ww )Nr   r$   zMissing features for model z: r#   r   r)   z using model r   z No feature list found for model z. Skipping prediction.z/gwr&   Fr'   zOther model predictions for GW z savedz&Error during other model predictions: )r   r1   r2   r3   r5   r6   itemsr   r.   r!   r+   r/   copyr0   r4   r,   r7   r8   	Exceptionerror)r   r=   r9   Zother_modelsresults
model_namer
   Zmodel_featuresr:   Zmodel_inputr;   er   r   r   make_other_model_predictions_   s2   
 rE   c               
   C   s   z@zt   W n ty  }  ztd|   W Y d } ~ nd } ~ ww W td t  td d S W td t  td d S td t  td w )NError Occured : z(Evaluation for Model Performance startedz4Evaluation for Model Performance completed and saved)updater@   r+   rA   r,   r   )rD   r   r   r   update_model_dataz   s"   



rH   c                 C   sl   t td|  d ddg }|jddid}|d d|d< |d d|d< |jdgd}|jd	d
S )Nr   z.csvelementZtotal_pointsr   )r!   Int64r$   records)orient)r1   read_csvPLAYERS_GW_PATHrenamer5   r3   to_dict)r   Z
players_dfr   r   r   get_player_data   s   rQ   c              
   C   s`   zt d| d tdtd| d d W d S  ty/ } ztd|  W Y d }~d S d }~ww )NZ2526         )Zvalidation_seasonZvalidation_gwZvalidation_lenrF   )r   minmaxr@   r+   rA   )r   rD   r   r   r   model_retarin   s   (rW   __main__   ))r   loggingospathlibr   pandasr1   Zscripts.get_prediction_inputr   Zscripts.update_datar   rG   Zscripts.retrain_modelr   Zscripts.model_evalr   Zapp.constantsr   r   r   r8   rN   	getLogger__name__r+   r   r
   r   r   r6   r2   r0   SeriesrE   rH   rQ   rW   r   predictionsr   headr   r   r   r   <module>   s<    
 *