o
    "]i                     @   s   d Z ddlmZ ddlmZmZ ddl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 dd
lmZ ddlmZmZ ddlmZ ddlmZ ddlZeeZG dd de	ZdS )zModel training agent    )Session)datetime	timedeltaN)	BaseAgent)MatchRepository)ModelRepository)MatchStatus)Feature)Model)MatchPredictorprepare_target)FeatureBuilder)settingsc                       s4   e Zd ZdZdef fddZdefddZ  ZS )TrainingAgentzAgent for training ML modelsdbc                    sF   t  |d t|| _t|| _t|| _d| _t	j
| jdd d S )Nr   modelsT)exist_ok)super__init__r   
match_repor   
model_repor   feature_builder
models_dirosmakedirs)selfr   	__class__ app/agents/training_agent.pyr      s   


zTrainingAgent.__init__returnc                 C   sD  |   }zst tdd }d}| jj||d}tdt| d t|t	j
k rDdt| dt	j
 }t| | jd	d
 W d	S dd |D }| jttj| }dd |D }g }	g }
g }|D ]0}|j|vrqqi|jdu s{|jdu r|qi||j }| j|}|	| |
t| ||j qit|	t	j
k rdt|	 dt	j
 }t| | jd	d
 W d	S t|	}	t|
}
tt|	t	j }|	d| }|
d| }|	|d }|
|d }tdt| dt| d t t	j!d}|"||||}td|  dt #d }t$j%&| j'd| d}|(| t)|t	j!dt	j*i|+d|+d|+d|+d|+d|+d|t|	d d!}| j,- }|D ]}d"|_.qU| j,/|}| j0  td#| d$ | jd	d
 W d	S  t1y } zt2|}tj3d%| d	d& | jd'|d( W Y d}~d'S d}~ww ))zRun model trainingi  )daysN)min_datemax_datezFound z matches for trainingz!Not enough matches for training: z < T)successc                 S   s   g | ]}|j qS r   )id).0mr   r   r   
<listcomp>7   s    z%TrainingAgent.run.<locals>.<listcomp>c                 S   s   i | ]}|j |qS r   )match_id)r&   fr   r   r   
<dictcomp><   s    z%TrainingAgent.run.<locals>.<dictcomp>z"Not enough matches with features: zTraining on z samples, validating on z samples)
model_typezTraining metrics: vz%Y%m%d_%H%M%Smodel_z.joblibrolling_windowtrain_accuracytest_accuracytrain_loglosstest_loglosstrain_brier
test_brier   )versionr,   
parametersr0   r1   r2   r3   r4   r5   artifact_pathtrained_on_matches	is_activer   zModel z trained and savedzTraining failed: )exc_infoF)r$   error)4	start_runr   nowr   r   get_matches_for_trainingloggerinfolenr   min_matches_for_trainingwarning
finish_runr   queryr	   filterr)   in_allr%   
home_score
away_scorer   build_feature_vectorappendr   
match_datenparrayinttrain_test_splitr   r,   trainstrftimer   pathjoinr   saver
   rolling_window_matchesgetr   get_allr;   createcommit	Exceptionstrr=   )r   run_idr"   r#   matches	error_msg	match_idsfeaturesfeature_dictXymatch_datesmatchfeaturefeature_vector	split_idxX_trainy_trainX_valy_val	predictormetricsr7   r9   model
old_models	old_modeler   r   r   run   s   







 



zTrainingAgent.run)	__name__
__module____qualname____doc__r   r   boolrw   __classcell__r   r   r   r   r      s    r   ) r{   sqlalchemy.ormr   r   r   loggingnumpyrP   app.agents.baser   )app.core.db.repositories.match_repositoryr   )app.core.db.repositories.model_repositoryr   app.core.db.models.matchr   app.core.db.models.featurer	   app.core.db.models.modelr
   app.ml.modelsr   r   app.ml.featuresr   app.core.configr   r   	getLoggerrx   rA   r   r   r   r   r   <module>   s"    
