Coverage for tadkit/catalog/learners/_confiance_components/_kcpdi_wrapper.py: 15%

20 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-04 15:09 +0000

1import numpy as np 

2 

3 

4def get_wrapped_kcplearner(): 

5 """Return the TADlearner wrapped from Kernel Change Point Detection's KcpLearner method. 

6 

7 The function is intended for use if the dependency is available. 

8 """ 

9 

10 from kcpdi.kcp_ss_learner import KcpLearner 

11 

12 KcpLearner.params_description["kernel"] = { 

13 "description": "One of (linear, cosine, rbf)", 

14 "value_type": "choice", 

15 "set": ["linear", "cosine", "rbf"], 

16 "default": "linear", 

17 } 

18 KcpLearner.params_description["max_n_time_points"] = { 

19 "description": "max_n_time_points", 

20 "value_type": "range", 

21 "start": 1000, 

22 "stop": 10000, 

23 "step": 500, 

24 "default": 1000, 

25 } 

26 KcpLearner.params_description["expected_frac_anomaly"] = { 

27 "description": "expected_frac_anomaly", 

28 "value_type": "real_range", 

29 "start": 0.00001, 

30 "stop": 0.5, 

31 "step": 0.0001, 

32 "default": 0.00001, 

33 } 

34 

35 KcpLearner.oldfit = KcpLearner.fit 

36 

37 # we rebind fit to set the offset_ parameter so as to be able to implement predict like other methods do 

38 def fit(self, X, y=None, sample_weight=None): 

39 self.oldfit(X=X, y=y) 

40 contamination = self.expected_frac_anomaly 

41 self.offset_ = np.percentile(self.score_samples(X), 100.0 * contamination) 

42 return self 

43 

44 KcpLearner.fit = fit 

45 

46 def predict(self, X): 

47 decision_func = self.score_samples(X) - self.offset_ 

48 is_inlier = np.ones_like(decision_func, dtype=int) 

49 is_inlier[decision_func < 0] = -1 

50 return is_inlier 

51 

52 KcpLearner.predict = predict 

53 

54 return KcpLearner