|
| 1 | +from math import isnan |
| 2 | + |
| 3 | +import pandas as pd |
1 | 4 | import pytest |
2 | 5 |
|
| 6 | +from bigwig_loader import config |
| 7 | + |
3 | 8 | torch = pytest.importorskip("torch") |
4 | 9 |
|
5 | 10 |
|
@@ -30,3 +35,81 @@ def test_input_and_target_is_torch_tensor(pytorch_dataset): |
30 | 35 | sequence, target = next(iter(pytorch_dataset)) |
31 | 36 | assert isinstance(sequence, torch.Tensor) |
32 | 37 | assert isinstance(target, torch.Tensor) |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.parametrize("default_value", [0.0, torch.nan, 4.0, 5.6]) |
| 41 | +def test_pytorch_dataset_with_window_function( |
| 42 | + default_value, bigwig_path, reference_genome_path, merged_intervals |
| 43 | +): |
| 44 | + from bigwig_loader.pytorch import PytorchBigWigDataset |
| 45 | + |
| 46 | + center_bin_to_predict = 2048 |
| 47 | + window_size = 128 |
| 48 | + reduced_dim = center_bin_to_predict // window_size |
| 49 | + |
| 50 | + batch_size = 16 |
| 51 | + |
| 52 | + df = pd.read_csv(config.example_positions, sep="\t") |
| 53 | + df = df[df["chr"].isin({"chr1", "chr3", "chr5"})] |
| 54 | + chromosomes = list(df["chr"])[:batch_size] |
| 55 | + centers = list(df["center"])[:batch_size] |
| 56 | + |
| 57 | + position_sampler = [(chrom, center) for chrom, center in zip(chromosomes, centers)] |
| 58 | + |
| 59 | + dataset = PytorchBigWigDataset( |
| 60 | + regions_of_interest=merged_intervals, |
| 61 | + collection=bigwig_path, |
| 62 | + reference_genome_path=reference_genome_path, |
| 63 | + sequence_length=center_bin_to_predict * 2, |
| 64 | + center_bin_to_predict=center_bin_to_predict, |
| 65 | + window_size=1, |
| 66 | + batch_size=batch_size, |
| 67 | + batches_per_epoch=1, |
| 68 | + maximum_unknown_bases_fraction=0.1, |
| 69 | + first_n_files=3, |
| 70 | + custom_position_sampler=position_sampler, |
| 71 | + default_value=default_value, |
| 72 | + return_batch_objects=True, |
| 73 | + ) |
| 74 | + |
| 75 | + dataset_with_window = PytorchBigWigDataset( |
| 76 | + regions_of_interest=merged_intervals, |
| 77 | + collection=bigwig_path, |
| 78 | + reference_genome_path=reference_genome_path, |
| 79 | + sequence_length=center_bin_to_predict * 2, |
| 80 | + center_bin_to_predict=center_bin_to_predict, |
| 81 | + window_size=window_size, |
| 82 | + batch_size=batch_size, |
| 83 | + batches_per_epoch=1, |
| 84 | + maximum_unknown_bases_fraction=0.1, |
| 85 | + first_n_files=3, |
| 86 | + custom_position_sampler=position_sampler, |
| 87 | + default_value=default_value, |
| 88 | + return_batch_objects=True, |
| 89 | + ) |
| 90 | + |
| 91 | + print(dataset_with_window._dataset.bigwig_collection.bigwig_paths) |
| 92 | + |
| 93 | + for batch, batch_with_window in zip(dataset, dataset_with_window): |
| 94 | + print(batch) |
| 95 | + print(batch_with_window) |
| 96 | + print(batch.chromosomes) |
| 97 | + print(batch_with_window.chromosomes) |
| 98 | + print(batch.starts) |
| 99 | + print(batch_with_window.starts) |
| 100 | + print(batch.ends) |
| 101 | + print(batch_with_window.ends) |
| 102 | + expected = batch.values.reshape( |
| 103 | + batch.values.shape[0], batch.values.shape[1], reduced_dim, window_size |
| 104 | + ) |
| 105 | + if not isnan(default_value) or default_value == 0: |
| 106 | + expected = torch.nan_to_num(expected, nan=default_value) |
| 107 | + expected = torch.nanmean(expected, axis=-1) |
| 108 | + print("---") |
| 109 | + print("expected") |
| 110 | + print(expected) |
| 111 | + print("batch_with_window") |
| 112 | + print(batch_with_window.values) |
| 113 | + assert torch.allclose(expected, batch_with_window.values, equal_nan=True) |
| 114 | + if isnan(default_value): |
| 115 | + assert torch.isnan(batch_with_window.values).any() |
0 commit comments