
    LCjQ                         d Z ddlZddlZddlmZmZ ddlmZ ddlmZ  ej                  ej                  d       ded	ej                  fd
Zdededeeef   dee   deded	dfdZddededed	dfdZy)u   
Shared training utilities.

Common helpers used across all training scripts in this directory —
kept here once rather than copy-pasted six times, so a fix (e.g. to how
model metadata is saved) only needs to happen in one place.
    N)datetimetimezone)Path)Anyz%%(asctime)s %(levelname)s %(message)s)levelformatnamereturnc                 ,    t        j                  |       S )N)logging	getLogger)r	   s    PC:\Crop_Prediction\Backend\crop-ai-system\ml_pipeline\training\training_utils.py
get_loggerr      s    T""    
output_dir
model_namemetricsfeature_colsn_trainn_testc                    |t        j                  t        j                        j	                         ||||d}| | dz  }t        |dd      5 }t        j                  ||dt               ddd       y# 1 sw Y   yxY w)	u  
    Writes a metadata JSON alongside every .pkl artifact — this is what
    lets you (or the client) audit exactly when a model was trained, on
    how much data, and what its validation metrics were, without having
    to re-run training or dig through terminal scrollback.
    )r   trained_at_utcn_train_samplesn_test_samplesfeature_columnsr   z_metadata.jsonwzutf-8)encoding   )indentdefaultN)	r   nowr   utc	isoformatopenjsondumpstr)	r   r   r   r   r   r   metadatametadata_pathfs	            r   save_training_metadatar+      s{     !",,x||4>>@" 'H J<~!>>M	mS7	3 6q		(Aa56 6 6s   A66A?df_lenminimumc           	      8    | |k  rt        d|  d| d| d      y )NzOnly z! samples available for training 'z', minimum required is u   . Training on fewer samples than this produces unreliable models — collect more data or lower this threshold deliberately (not silently).)
ValueError)r,   r   r-   s      r   check_minimum_samplesr0   1   s?    F8<ZL I##*) ,:;
 	
 r   )d   )__doc__r%   r   r   r   pathlibr   typingr   basicConfigINFOr'   Loggerr   dictlistintr+   r0    r   r   <module>r<      s      '     ',,/V W#S #W^^ #666 #s(^6 s)	6
 6 6 
66
# 
3 
 
t 
r   