
    Q3jw                     P   d Z ddlZddlZddlZddl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 d Zd Z ej                   ed	       ej                   ed
	      gZej$                  j'                  de      d        Zej$                  j'                  de      d        Zd Zd Zy)zCommon pickle round-trip tests for callbacks.

These tests guard the contract that callbacks (and estimators they are attached to)
must be picklable, and that an estimator pickled after a successful fit can be
unpickled in a fresh Python interpreter.
    N)ProgressBarScoringMonitor)MaxIterEstimator)make_regressionc                  @    t        j                  d       t               S )Nrich)pytestimportorskipr        O/DATA/.local/lib/python3.12/site-packages/sklearn/callback/tests/test_pickle.py_pbr      s    
=r   c                      t        d      S )Nr2scoring)r   r   r   r   _smr      s    $''r   r   )idr   factoryc                     t               j                   |              }t        j                  t        j                  |            }t        |      t        |      u sJ t        |j                        dk(  sJ y)zJAn estimator with the callback registered but not yet fitted is picklable.   N)r   set_callbackspickleloadsdumpstypelen_skl_callbacks)r   	estimatorrestoreds      r   5test_estimator_with_callback_pickle_roundtrip_pre_fitr!   '   s_     !"00;I||FLL34H>T)_,,,x&&'1,,,r   c                     |        }t        d      j                  |      }|j                          t        j                  t        j
                  |            }t        |      t        |      u sJ t        |j                        dk(  sJ y)zBAn estimator with the callback registered and fitted is picklable.   max_iterr   N)	r   r   fitr   r   r   r   r   r   )r   callbackr   r    s       r   6test_estimator_with_callback_pickle_roundtrip_post_fitr(   0   so     yH !,::8DIMMO||FLL34H>T)_,,,x&&'1,,,r   c                    t        j                  d       t        ddd      \  }}t        d      }t	        d	      j                  t               |      }|j                  ||
       | j                         }t        j                  d|j                        sJ t        j                  d|j                        sJ |j                  d      }t        |      dk(  sJ t        j                  t        j                   |            }|j                  ||
       | j                         }t        j                  d|j                        sJ t        j                  d|j                        sJ |j"                  d   j                  d      }t        |      dk(  sJ |d   j$                  |d   j$                  k(  sJ y)zAn estimator with callbacks survives an in-process pickle round-trip.

    It also supports re-fitting after being unpickled and the callbacks accumulate new
    data from the re-fit.
    r         r   	n_samples
n_featuresrandom_stater   r   r#   r$   XyMaxIterEstimator - fit100%allselectr   N)r	   r
   r   r   r   r   r   r&   
readouterrresearchoutget_logsr   r   r   r   r   data)	capsysr1   r2   smr   capturedoriginal_logsr    restored_logss	            r   1test_callbacks_refit_after_pickle_in_same_processrC   ;   st    RAAFDAq		%B !,::;="MIMMAM  "H99.===99Whll+++KKuK-M}"""||FLL34HLL1L  "H99.===99Whll+++++A.77u7EM}"""  M!$4$9$9999r   c                    t        j                  d       t        ddd      \  }}t        d      }t	        d      j                  t               |      }|j                  ||	       |j                         }t        j                  d
|j                        sJ t        j                  d|j                        sJ |j                  d      }t        |      dk(  sJ | dz  }t        |d      5 }	t        j                   ||	       ddd       t#        j$                  dt'        |      d|d   j(                   d      }
t+        j,                  t.        j0                  d|
gdd      }|j2                  j5                         }t        j                  d
|      sJ t        j                  d|      sJ y# 1 sw Y   xY w)zAn estimator with callbacks survives unpickling in a fresh interpreter.

    It also supports re-fitting after being unpickled and the callbacks accumulate new
    data from the re-fit.
    r      r#   r   r,   r   r   r$   r0   r3   r4   r5   r6   r   zest.pklwbNz
        import pickle
        from sklearn.callback import ScoringMonitor
        from sklearn.datasets import make_regression

        with open(a*  , "rb") as f:
            est = pickle.load(f)

        X, y = make_regression(n_samples=20, n_features=3, random_state=1)
        est.fit(X=X, y=y)

        restored_logs = est._skl_callbacks[1].get_logs(select="all")
        assert len(restored_logs) == 2
        assert restored_logs[0].data == z	
        z-cTx   )capture_outputtimeout)r	   r
   r   r   r   r   r   r&   r8   r9   r:   r;   r<   r   openr   dumptextwrapdedentstrr=   
subprocessrunsys
executablestdoutdecode)tmp_pathr>   r1   r2   r?   r   r@   rA   pkl_pathfload_scriptresultrS   s                r   0test_callbacks_refit_after_load_in_fresh_processrZ   \   s    RAAFDAq		%B !,::;="MIMMAM  "H99.===99Whll+++KKuK-M}""")#H	h	Iq! 
 //
 x=# $) *7q)9)>)>(? @		K$ ^^	{+D#F ]]!!#F99.77799Wf%%%7 
	s   1F66F?)__doc__r   r9   rO   rQ   rL   r	   sklearn.callbackr   r   sklearn.callback.tests._utilsr   sklearn.datasetsr   r   r   paramCALLBACK_FACTORIESmarkparametrizer!   r(   rC   rZ   r   r   r   <module>rc      s     	  
   8 : ,
(
 FLL'FLL)*  $67- 8- $67- 8-:B1&r   