1+ import lsdb
2+ import nested_pandas as npd
3+ import numpy as np
4+ from lsdb .core .crossmatch .kdtree_match import KdTreeCrossmatch
5+ from lsdb_crossmatch .mag_difference_crossmatch import MyCrossmatchAlgorithm
6+
7+ def test_mag_difference_crossmatch (m67_delve_dir , m67_ps1_dir ):
8+ left_data = lsdb .open_catalog (m67_ps1_dir )
9+ right_data = lsdb .open_catalog (m67_delve_dir )
10+
11+ id_col_left = "objID_left"
12+ id_col_right = "QUICK_OBJECT_ID_right"
13+
14+ left_mag_col = "rMeanPSFMag"
15+ right_mag_col = "MAG_PSF_R"
16+
17+ kdtree_result = lsdb .crossmatch (
18+ left_data ,
19+ right_data ,
20+ suffixes = ["_left" , "_right" ],
21+ algorithm = KdTreeCrossmatch ,
22+ radius_arcsec = 0.01 * 3600 ,
23+ n_neighbors = 5 ,
24+ ).compute ()
25+ """
26+ print("\n KDTREE RESULTS")
27+ print("\n id col left")
28+ print(kdtree_result[id_col_left])
29+ print("\n id col right")
30+ print(kdtree_result[id_col_right])
31+ print("------------")
32+ """
33+ """
34+ target_left_id = 10493100048242
35+ count_in_left_catalog = left_data["objID"].value_counts().compute().get(target_left_id, 0)
36+ print(f"\n Number of times {target_left_id} appears in the original left catalog: {count_in_left_catalog}")
37+
38+ target_right_id = 10493100048242
39+ count_in_right_catalog = right_data["QUICK_OBJECT_ID"].value_counts().compute().get(target_right_id, 0)
40+
41+ print(f"\n Number of times {target_right_id} appears in the original right catalog: {count_in_right_catalog}")
42+ """
43+ result = lsdb .crossmatch (
44+ left_data ,
45+ right_data ,
46+ suffixes = ["_left" , "_right" ],
47+ algorithm = MyCrossmatchAlgorithm ,
48+ radius_arcsec = 0.01 * 3600 ,
49+ left_mag_col = left_mag_col ,
50+ right_mag_col = right_mag_col ,
51+ n_neighbors = 5
52+ ).compute ()
53+ """
54+ print("\n MAG CROSSMATCH RESULTS")
55+ print("\n id col left")
56+ print(result[id_col_left])
57+ print("\n id col right")
58+ print(result[id_col_right])
59+ print("------------")
60+ """
61+
62+ assert isinstance (result , npd .NestedFrame )
63+
64+ if not result .empty :
65+ assert len (result ) == len (result [id_col_left ].unique ())
66+ assert result [id_col_left ].value_counts ().max () == 1
67+
68+
69+ if not kdtree_result .empty :
70+ kdtree_result ["magnitude_difference" ] = np .abs (
71+ kdtree_result [right_mag_col + "_right" ] - kdtree_result [left_mag_col + "_left" ]
72+ )
73+
74+ target_left_id = 122650089529714672
75+
76+ if not kdtree_result .empty and target_left_id in kdtree_result [id_col_left ].values :
77+ potential_matches_for_target = kdtree_result [kdtree_result [id_col_left ] == target_left_id ]
78+
79+ if not potential_matches_for_target .empty :
80+ potential_matches_for_target = potential_matches_for_target .sort_values (
81+ by = "magnitude_difference"
82+ ).reset_index (drop = True )
83+
84+ expected_best_right_id = potential_matches_for_target .iloc [0 ][id_col_right ]
85+
86+ actual_match_from_my_algo = result [result [id_col_left ] == target_left_id ]
87+
88+ if not actual_match_from_my_algo .empty :
89+ actual_matched_right_id = actual_match_from_my_algo .iloc [0 ][id_col_right ]
90+
91+ assert actual_matched_right_id == expected_best_right_id
92+ else :
93+ print (f"No potential matches found in kdtree_result for { target_left_id } ." )
94+ else :
95+ print (f"Target left ID { target_left_id } not found in kdtree_result." )
96+
97+ """
98+ for target_left_id in result[id_col_left].values:
99+ if not kdtree_result.empty and target_left_id in kdtree_result[id_col_left].values:
100+ potential_matches_for_target = kdtree_result[kdtree_result[id_col_left] == target_left_id].copy()
101+
102+ if not potential_matches_for_target.empty:
103+ potential_matches_for_target = potential_matches_for_target.sort_values(
104+ by="magnitude_difference"
105+ ).reset_index(drop=True)
106+
107+ #print(f"\n All potential matches for {target_left_id}:")
108+ #print(potential_matches_for_target[[id_col_left, id_col_right, "magnitude_difference"]])
109+
110+ expected_best_right_id = potential_matches_for_target.iloc[0][id_col_right]
111+
112+ #print(f"\n Expected best match for {target_left_id}:")
113+ #print(f" Right ID: {expected_best_right_id}")
114+
115+ actual_match_from_my_algo = result[result[id_col_left] == target_left_id]
116+
117+ if not actual_match_from_my_algo.empty:
118+ actual_matched_right_id = actual_match_from_my_algo.iloc[0][id_col_right]
119+
120+ #print(f"\n Actual match from MyCrossmatchAlgorithm for {target_left_id}:")
121+ #print(f" Right ID: {actual_matched_right_id}")
122+
123+ assert actual_matched_right_id == expected_best_right_id
124+ else:
125+ print(f"No potential matches found in kdtree_result for {target_left_id}.")
126+ else:
127+ print(f"Target left ID {target_left_id} not found in kdtree_result.")
128+ """
0 commit comments