
    "]i                         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j8                  e      Z G d de	      Zy)zModel training agent    )Session)datetime	timedeltaN)	BaseAgent)MatchRepository)ModelRepository)MatchStatus)Feature)Model)MatchPredictorprepare_target)FeatureBuilder)settingsc                   4     e Zd ZdZdef fdZdefdZ xZS )TrainingAgentzAgent for training ML modelsdbc                     t         |   |d       t        |      | _        t	        |      | _        t        |      | _        d| _        t        j                  | j                  d       y )Nr   modelsT)exist_ok)super__init__r   
match_repor   
model_repor   feature_builder
models_dirosmakedirs)selfr   	__class__s     !/app/app/agents/training_agent.pyr   zTrainingAgent.__init__   sP    _-)"-)"--b1"
DOOd3    returnc                 @
   | j                         }	 t        j                         t        d      z
  }d}| j                  j                  ||      }t        j                  dt        |       d       t        |      t        j                  k  rGdt        |       dt        j                   }t        j                  |       | j                  d	
       y	|D cg c]  }|j                   }}| j                  j                  t               j#                  t         j$                  j'                  |            j)                         }|D 	ci c]  }	|	j$                  |	 }
}	g }g }g }|D ]  }|j                  |
vr|j*                  |j,                  +|
|j                     }| j.                  j1                  |      }|j3                  |       |j3                  t5        |             |j3                  |j6                          t        |      t        j                  k  rGdt        |       dt        j                   }t        j                  |       | j                  d	
       y	t9        j:                  |      }t9        j:                  |      }t=        t        |      t        j>                  z        }|d| }|d| }||d }||d }t        j                  dt        |       dt        |       d       tA        t        jB                        }|jE                  ||||      }t        j                  d|        dt        j                         jG                  d       }tH        jJ                  jM                  | jN                  d| d      }|jQ                  |       tS        |t        jB                  dt        jT                  i|jW                  d      |jW                  d      |jW                  d      |jW                  d      |jW                  d      |jW                  d      |t        |      d      }| jX                  j[                         }|D ]	  }d|_.         | jX                  j_                  |      }| j                  ja                          t        j                  d| d        | j                  d	
       y	c c}w c c}	w # tb        $ rB}te        |      }t        jg                  d!| d	"       | j                  d#|$       Y d}~y#d}~ww xY w)%zRun model trainingi  )daysN)min_datemax_datezFound z matches for trainingz!Not enough matches for training: z < T)successz"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(   
parametersr,   r-   r.   r/   r0   r1   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_runidr   queryr
   filtermatch_idin_all
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_allr7   createcommit	Exceptionstrr9   )r   run_idr%   r&   matches	error_msgm	match_idsfeaturesffeature_dictXymatch_datesmatchfeaturefeature_vector	split_idxX_trainy_trainX_valy_val	predictormetricsr3   r5   model
old_models	old_modeles                                 r    runzTrainingAgent.run   s]   !k	||~	w(??HHoo>>!! ? G
 KK&W.CDE7|h????G~SQYQrQrPst	y)- (//w!wI/ww}}W-44  $$Y/ce  4<<8aAJJM8L< AAK 88</##+u/?/?/G&uxx0!%!5!5!J!J7!S(./""5#3#34 ! 1v999@QHLmLmKno	y)-AA CFX%>%>>?I
mG
mGijMEijMEKK,s7|n4LSQVZLX`ab '(2E2EFIoogwuEGKK,WI67 (,,.11/BCDGGGLLF7)7:STMNN=) #..,h.M.MN&{{+;<%kk/:%kk/:$[[8#KK6";;|4+#&q6E  002J'	&'	# ( OO**51EGGNNKK&	);<=OODO)c 0
 =\  	AILL,YK84LHOOEO;		sE   CS S S.AS S!D	S +IS 
S 	T8TT)	__name__
__module____qualname____doc__r   r   boolrw   __classcell__)r   s   @r    r   r      s     &47 4oT or!   r   ) r{   sqlalchemy.ormr   r   r   loggingnumpyrN   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   r=   r    r!   r    <module>r      sR     " (   % E E 0 . * 8 * $ 				8	$zI zr!   