|
| 1 | +import pytest |
| 2 | +from os.path import join |
| 3 | + |
| 4 | +from spatialdata import read_zarr |
| 5 | + |
| 6 | +from vitessce.data_utils.spatialdata_points_zorder import ( |
| 7 | + # Function for computing codes and sorting |
| 8 | + sdata_morton_sort_points, |
| 9 | + # Functions for querying |
| 10 | + sdata_morton_query_rect_debug, |
| 11 | + row_ranges_to_row_indices, |
| 12 | + orig_coord_to_norm_coord, |
| 13 | +) |
| 14 | + |
| 15 | +def is_sorted(l): |
| 16 | + return all(l[i] <= l[i + 1] for i in range(len(l) - 1)) |
| 17 | + |
| 18 | +def get_sdata(): |
| 19 | + data_dir = join("docs", "notebooks", "data") |
| 20 | + spatialdata_filepath = join(data_dir, "xenium_rep1_io.spatialdata.zarr") |
| 21 | + |
| 22 | + sdata = read_zarr(spatialdata_filepath) |
| 23 | + return sdata |
| 24 | + |
| 25 | +@pytest.mark.skip(reason="Temporarily disable") |
| 26 | +def test_zorder_sorting(): |
| 27 | + # TODO: use fixture here |
| 28 | + sdata = get_sdata() |
| 29 | + |
| 30 | + sdata_morton_sort_points(sdata, "transcripts") |
| 31 | + |
| 32 | + # Check that the morton codes are sorted |
| 33 | + sorted_ddf = sdata.points["transcripts"] |
| 34 | + morton_sorted = sorted_ddf["morton_code_2d"].compute().values.tolist() |
| 35 | + |
| 36 | + assert is_sorted(morton_sorted) |
| 37 | + |
| 38 | + |
| 39 | +def test_zorder_query(): |
| 40 | + sdata = get_sdata() |
| 41 | + |
| 42 | + sdata_morton_sort_points(sdata, "transcripts") |
| 43 | + |
| 44 | + # Query a rectangle that should return some points |
| 45 | + orig_rect = [[50.0, 50.0], [100.0, 150.0]] # x0, y0, x1, y1 |
| 46 | + matching_row_ranges, rows_checked = sdata_morton_query_rect_debug(sdata, "transcripts", orig_rect) |
| 47 | + rect_row_indices = row_ranges_to_row_indices(matching_row_ranges) |
| 48 | + |
| 49 | + # Cannot use df.iloc on a dask dataframe, so convert it to pandas first |
| 50 | + ddf = sdata.points["transcripts"] |
| 51 | + df = ddf.compute() |
| 52 | + df = df.reset_index(drop=True) |
| 53 | + estimated_row_indices = df.iloc[rect_row_indices].index.tolist() |
| 54 | + |
| 55 | + assert df.shape[0] == 42638083 |
| 56 | + |
| 57 | + # Do the same query the "dumb" way, by checking all points |
| 58 | + in_rect = ( |
| 59 | + (df["x"] >= orig_rect[0][0]) |
| 60 | + & (df["x"] <= orig_rect[1][0]) |
| 61 | + & (df["y"] >= orig_rect[0][1]) |
| 62 | + & (df["y"] <= orig_rect[1][1]) |
| 63 | + ) |
| 64 | + dumb_df_subset = df.loc[in_rect] |
| 65 | + # Get the row indices of the points in the rectangle |
| 66 | + # (these are the indices in the original dataframe) |
| 67 | + exact_row_indices = dumb_df_subset.index.tolist() |
| 68 | + |
| 69 | + # Check that the estimated rows 100% contain the exact rows. |
| 70 | + # A.issubset(B) checks that all elements of A are in B ("A is a subset of B"). |
| 71 | + assert set(exact_row_indices).issubset(set(estimated_row_indices)) |
| 72 | + assert len(exact_row_indices) == 614 |
| 73 | + assert len(estimated_row_indices) <= 631 |
| 74 | + |
| 75 | + # Check that the number of rows checked is less than the total number of points |
| 76 | + assert len(rows_checked) <= 45237 |
| 77 | + assert len(matching_row_ranges) == 24 # Kind of an implementation detail. |
| 78 | + |
| 79 | + # Do a second check, this time against x_uint/y_uint (the normalized coordinates) |
| 80 | + # TODO: does this ensure that estimated == exact? |
| 81 | + |
| 82 | + bounding_box = ddf.attrs["bounding_box"] |
| 83 | + x_min = bounding_box["x_min"] |
| 84 | + x_max = bounding_box["x_max"] |
| 85 | + y_min = bounding_box["y_min"] |
| 86 | + y_max = bounding_box["y_max"] |
| 87 | + norm_rect = [ |
| 88 | + orig_coord_to_norm_coord(orig_rect[0], orig_x_min=x_min, orig_x_max=x_max, orig_y_min=y_min, orig_y_max=y_max), |
| 89 | + orig_coord_to_norm_coord(orig_rect[1], orig_x_min=x_min, orig_x_max=x_max, orig_y_min=y_min, orig_y_max=y_max) |
| 90 | + ] |
| 91 | + |
| 92 | + in_rect_norm = ( |
| 93 | + (df["x_uint"] >= norm_rect[0][0]) |
| 94 | + & (df["x_uint"] <= norm_rect[1][0]) |
| 95 | + & (df["y_uint"] >= norm_rect[0][1]) |
| 96 | + & (df["y_uint"] <= norm_rect[1][1]) |
| 97 | + ) |
| 98 | + dumb_df_subset_norm = df.loc[in_rect_norm] |
| 99 | + # Get the row indices of the points in the rectangle |
| 100 | + # (these are the indices in the original dataframe) |
| 101 | + exact_row_indices_norm = dumb_df_subset_norm.index.tolist() |
| 102 | + assert set(exact_row_indices_norm).issubset(set(estimated_row_indices)) |
| 103 | + assert len(exact_row_indices_norm) == 617 |
| 104 | + assert len(estimated_row_indices) <= 631 |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + """ |
| 109 | + # ========= Another query ========== |
| 110 | + orig_rect = [[500, 500], [600, 600]] # x0, y0, x1, y1 |
| 111 | +
|
| 112 | + # Query using z-order |
| 113 | + matching_row_ranges, rows_checked = sdata_morton_query_rect_debug(sdata, "transcripts", orig_rect) |
| 114 | + rect_row_indices = row_ranges_to_row_indices(matching_row_ranges) |
| 115 | + estimated_row_indices = df.iloc[rect_row_indices].index.tolist() |
| 116 | +
|
| 117 | + # Query the "dumb" way |
| 118 | + in_rect = ( |
| 119 | + (df["x"] >= orig_rect[0][0]) |
| 120 | + & (df["x"] <= orig_rect[1][0]) |
| 121 | + & (df["y"] >= orig_rect[0][1]) |
| 122 | + & (df["y"] <= orig_rect[1][1]) |
| 123 | + ) |
| 124 | + dumb_df_subset = df.loc[in_rect] |
| 125 | + exact_row_indices = dumb_df_subset.index.tolist() |
| 126 | +
|
| 127 | + diff_rows = set(estimated_row_indices) - set(exact_row_indices) |
| 128 | + # print("Rows in estimated but not exact:", diff_rows) |
| 129 | + print(df.iloc[list(diff_rows)]) |
| 130 | + raise NotImplementedError("Debugging") |
| 131 | +
|
| 132 | + # Check that the estimated rows contain all of the exact rows. |
| 133 | + assert len(set(exact_row_indices).intersection(set(estimated_row_indices))) == 0 |
| 134 | + assert len(exact_row_indices) <= 1123 # TODO: update |
| 135 | + assert len(estimated_row_indices) <= 1163 # TODO: update |
| 136 | + |
| 137 | + """ |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
0 commit comments