Coverage for examples/2nn_estimator_id.py: 57%

71 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-14 12:31 +0000

1from pathlib import Path 

2 

3import joblib 

4import matplotlib.pyplot as plt 

5import numpy as np 

6from sklearn.neighbors import NearestNeighbors 

7 

8 

9MODEL_PATH = Path("simulated_sampling_nickel_t3/models/umap_retrained_umap_run3.joblib") 

10 

11USE_SIMULATED = False 

12USE_EXPERIMENTAL = True 

13USE_ROI_ONLY = True 

14 

15 

16def compute_2nn_id(Z): 

17 nn = NearestNeighbors(n_neighbors=3, algorithm="auto") 

18 nn.fit(Z) 

19 dist, _ = nn.kneighbors(Z) 

20 

21 r1 = dist[:, 1] 

22 r2 = dist[:, 2] 

23 mask = (r1 > 1e-12) & (r2 > r1) 

24 mu = r2[mask] / r1[mask] 

25 

26 if len(mu) < 50: 

27 print("Too few samples for reliable 2NN.") 

28 return None 

29 

30 if np.std(mu) < 1e-12: 30 ↛ 31line 30 didn't jump to line 31 because the condition on line 30 was never true

31 print("mu variance is approximately zero; 2NN is invalid.") 

32 return None 

33 

34 mu_sorted = np.sort(mu) 

35 sample_count = len(mu_sorted) 

36 # Keep the empirical plotting positions below one so the log transform is finite. 

37 empirical_cdf = np.arange(1, sample_count + 1) / (sample_count + 1) 

38 x = np.log(mu_sorted) 

39 y = -np.log(1 - empirical_cdf) 

40 slope, _intercept = np.polyfit(x, y, 1) 

41 return slope, x, y 

42 

43 

44def main(): 

45 bundle = joblib.load(MODEL_PATH) 

46 simulated = bundle["Z_sim"] 

47 experimental_roi = bundle.get("Z_roi") 

48 selected = [] 

49 

50 if USE_SIMULATED: 50 ↛ 51line 50 didn't jump to line 51 because the condition on line 50 was never true

51 print("Using simulated data") 

52 selected.append(simulated) 

53 

54 if USE_EXPERIMENTAL: 

55 if USE_ROI_ONLY: 55 ↛ 61line 55 didn't jump to line 61 because the condition on line 55 was always true

56 print("Using ROI experimental data") 

57 if experimental_roi is None: 57 ↛ 59line 57 didn't jump to line 59 because the condition on line 57 was always true

58 raise RuntimeError("Z_exp_roi not found.") 

59 selected.append(experimental_roi) 

60 else: 

61 experimental_all = bundle.get("Z_exp_all") 

62 print("Using full experimental data") 

63 if experimental_all is None: 

64 raise RuntimeError("Z_exp_all not found.") 

65 selected.append(experimental_all) 

66 

67 if not selected: 67 ↛ 70line 67 didn't jump to line 70 because the condition on line 67 was always true

68 raise RuntimeError("No dataset selected.") 

69 

70 all_samples = np.vstack(selected) 

71 print("Total samples used:", len(all_samples)) 

72 result = compute_2nn_id(all_samples) 

73 if result is None: 

74 print("2NN estimation failed.") 

75 return 

76 

77 dimension, x, y = result 

78 print(f"\nEstimated intrinsic dimension d = {dimension:.3f}") 

79 plt.figure(figsize=(6, 5)) 

80 plt.scatter(x, y, s=5, alpha=0.7) 

81 plt.plot(x, dimension * x, linewidth=2) 

82 plt.xlabel("log(mu)") 

83 plt.ylabel("-log(1 - F_emp(mu))") 

84 plt.title(f"2NN Intrinsic Dimension (d = {dimension:.2f})") 

85 plt.tight_layout() 

86 plt.savefig("2nn_id_plot.png", dpi=300) 

87 plt.close() 

88 print("Saved 2nn_id_plot.png") 

89 

90 

91if __name__ == "__main__": 91 ↛ 92line 91 didn't jump to line 92 because the condition on line 91 was never true

92 main()