Coverage for tests/test_integration.py: 0%
38 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-16 16:14 -0700
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-16 16:14 -0700
1import os
2import unittest
4import pytest
5import torch
6from torch.utils.data import DataLoader
8from copick_torch import CopickDataset
10# This test requires actual data
11# Skip it if the test config file is not available
12TEST_CONFIG_PATH = os.environ.get("COPICK_TEST_CONFIG", "./examples/czii_object_detection_training.json")
15@pytest.mark.skipif(not os.path.exists(TEST_CONFIG_PATH), reason="Test config file not available")
16class TestIntegration(unittest.TestCase):
17 """Integration tests that require actual data.
19 These tests will be skipped if the test data is not available.
20 """
22 def setUp(self):
23 # Create a cache directory if it doesn't exist
24 self.cache_dir = os.path.join(os.path.dirname(__file__), "test_cache")
25 os.makedirs(self.cache_dir, exist_ok=True)
27 def test_dataset_with_real_data(self):
28 """Test dataset with real data if available."""
29 # Skip if data not available
30 if not os.path.exists(TEST_CONFIG_PATH):
31 self.skipTest("Test config file not available")
33 # Initialize dataset with small boxsize and limited samples for faster testing
34 dataset = CopickDataset(
35 config_path=TEST_CONFIG_PATH,
36 boxsize=(16, 16, 16),
37 augment=False,
38 cache_dir=self.cache_dir,
39 max_samples=5,
40 )
42 # If no data was loaded (possibly because tomograms couldn't be accessed),
43 # skip the test instead of failing
44 if len(dataset) == 0:
45 self.skipTest("No data could be loaded from the test configuration")
47 # Test accessing an item only if dataset is not empty
48 if len(dataset) > 0:
49 volume, label = dataset[0]
50 self.assertIsInstance(volume, torch.Tensor)
51 self.assertEqual(volume.shape[0], 1) # Check channel dimension
53 # Test dataloader
54 dataloader = DataLoader(dataset, batch_size=2)
55 batch = next(iter(dataloader))
56 self.assertEqual(len(batch), 2) # Input and label
57 self.assertEqual(batch[0].shape[0], min(2, len(dataset))) # Batch dimension
59 def test_examples_method(self):
60 """Test the examples method with real data if available."""
61 # Skip if data not available
62 if not os.path.exists(TEST_CONFIG_PATH):
63 self.skipTest("Test config file not available")
65 # Initialize dataset
66 dataset = CopickDataset(
67 config_path=TEST_CONFIG_PATH,
68 boxsize=(16, 16, 16),
69 augment=False,
70 cache_dir=self.cache_dir,
71 max_samples=5,
72 )
74 # If no data was loaded, skip the test
75 if len(dataset) == 0:
76 self.skipTest("No data could be loaded from the test configuration")
78 # Get examples
79 examples, class_names = dataset.examples()
81 # Check that we got examples for each class
82 if examples is not None:
83 self.assertEqual(len(examples), len(class_names))
84 self.assertIsInstance(examples, torch.Tensor)
85 self.assertEqual(examples.shape[1], 1) # Channel dimension
86 self.assertEqual(examples.shape[2:], (16, 16, 16)) # Spatial dimensions
89if __name__ == "__main__":
90 unittest.main()