|
| 1 | +# Import necessary modules and define fixtures if needed |
| 2 | +import os |
| 3 | +import pytest |
| 4 | +import torch |
| 5 | +from PIL import Image |
| 6 | +from swarms.models.bioclip import BioClip |
| 7 | + |
| 8 | + |
| 9 | +# Define fixtures if needed |
| 10 | +@pytest.fixture |
| 11 | +def sample_image_path(): |
| 12 | + return "path_to_sample_image.jpg" |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def clip_instance(): |
| 17 | + return BioClip("microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224") |
| 18 | + |
| 19 | + |
| 20 | +# Basic tests for the BioClip class |
| 21 | +def test_clip_initialization(clip_instance): |
| 22 | + assert isinstance(clip_instance.model, torch.nn.Module) |
| 23 | + assert hasattr(clip_instance, "model_path") |
| 24 | + assert hasattr(clip_instance, "preprocess_train") |
| 25 | + assert hasattr(clip_instance, "preprocess_val") |
| 26 | + assert hasattr(clip_instance, "tokenizer") |
| 27 | + assert hasattr(clip_instance, "device") |
| 28 | + |
| 29 | + |
| 30 | +def test_clip_call_method(clip_instance, sample_image_path): |
| 31 | + labels = [ |
| 32 | + "adenocarcinoma histopathology", |
| 33 | + "brain MRI", |
| 34 | + "covid line chart", |
| 35 | + "squamous cell carcinoma histopathology", |
| 36 | + "immunohistochemistry histopathology", |
| 37 | + "bone X-ray", |
| 38 | + "chest X-ray", |
| 39 | + "pie chart", |
| 40 | + "hematoxylin and eosin histopathology", |
| 41 | + ] |
| 42 | + result = clip_instance(sample_image_path, labels) |
| 43 | + assert isinstance(result, dict) |
| 44 | + assert len(result) == len(labels) |
| 45 | + |
| 46 | + |
| 47 | +def test_clip_plot_image_with_metadata(clip_instance, sample_image_path): |
| 48 | + metadata = { |
| 49 | + "filename": "sample_image.jpg", |
| 50 | + "top_probs": {"label1": 0.75, "label2": 0.65}, |
| 51 | + } |
| 52 | + clip_instance.plot_image_with_metadata(sample_image_path, metadata) |
| 53 | + |
| 54 | + |
| 55 | +# More test cases can be added to cover additional functionality and edge cases |
| 56 | + |
| 57 | + |
| 58 | +# Parameterized tests for different image and label combinations |
| 59 | +@pytest.mark.parametrize( |
| 60 | + "image_path, labels", |
| 61 | + [ |
| 62 | + ("image1.jpg", ["label1", "label2"]), |
| 63 | + ("image2.jpg", ["label3", "label4"]), |
| 64 | + # Add more image and label combinations |
| 65 | + ], |
| 66 | +) |
| 67 | +def test_clip_parameterized_calls(clip_instance, image_path, labels): |
| 68 | + result = clip_instance(image_path, labels) |
| 69 | + assert isinstance(result, dict) |
| 70 | + assert len(result) == len(labels) |
| 71 | + |
| 72 | + |
| 73 | +# Test image preprocessing |
| 74 | +def test_clip_image_preprocessing(clip_instance, sample_image_path): |
| 75 | + image = Image.open(sample_image_path) |
| 76 | + processed_image = clip_instance.preprocess_val(image) |
| 77 | + assert isinstance(processed_image, torch.Tensor) |
| 78 | + |
| 79 | + |
| 80 | +# Test label tokenization |
| 81 | +def test_clip_label_tokenization(clip_instance): |
| 82 | + labels = ["label1", "label2"] |
| 83 | + tokenized_labels = clip_instance.tokenizer(labels) |
| 84 | + assert isinstance(tokenized_labels, torch.Tensor) |
| 85 | + assert tokenized_labels.shape[0] == len(labels) |
| 86 | + |
| 87 | + |
| 88 | +# More tests can be added to cover other methods and edge cases |
| 89 | + |
| 90 | + |
| 91 | +# End-to-end tests with actual images and labels |
| 92 | +def test_clip_end_to_end(clip_instance, sample_image_path): |
| 93 | + labels = [ |
| 94 | + "adenocarcinoma histopathology", |
| 95 | + "brain MRI", |
| 96 | + "covid line chart", |
| 97 | + "squamous cell carcinoma histopathology", |
| 98 | + "immunohistochemistry histopathology", |
| 99 | + "bone X-ray", |
| 100 | + "chest X-ray", |
| 101 | + "pie chart", |
| 102 | + "hematoxylin and eosin histopathology", |
| 103 | + ] |
| 104 | + result = clip_instance(sample_image_path, labels) |
| 105 | + assert isinstance(result, dict) |
| 106 | + assert len(result) == len(labels) |
| 107 | + |
| 108 | + |
| 109 | +# Test label tokenization with long labels |
| 110 | +def test_clip_long_labels(clip_instance): |
| 111 | + labels = ["label" + str(i) for i in range(100)] |
| 112 | + tokenized_labels = clip_instance.tokenizer(labels) |
| 113 | + assert isinstance(tokenized_labels, torch.Tensor) |
| 114 | + assert tokenized_labels.shape[0] == len(labels) |
| 115 | + |
| 116 | + |
| 117 | +# Test handling of multiple image files |
| 118 | +def test_clip_multiple_images(clip_instance, sample_image_path): |
| 119 | + labels = ["label1", "label2"] |
| 120 | + image_paths = [sample_image_path, "image2.jpg"] |
| 121 | + results = clip_instance(image_paths, labels) |
| 122 | + assert isinstance(results, list) |
| 123 | + assert len(results) == len(image_paths) |
| 124 | + for result in results: |
| 125 | + assert isinstance(result, dict) |
| 126 | + assert len(result) == len(labels) |
| 127 | + |
| 128 | + |
| 129 | +# Test model inference performance |
| 130 | +def test_clip_inference_performance(clip_instance, sample_image_path, benchmark): |
| 131 | + labels = [ |
| 132 | + "adenocarcinoma histopathology", |
| 133 | + "brain MRI", |
| 134 | + "covid line chart", |
| 135 | + "squamous cell carcinoma histopathology", |
| 136 | + "immunohistochemistry histopathology", |
| 137 | + "bone X-ray", |
| 138 | + "chest X-ray", |
| 139 | + "pie chart", |
| 140 | + "hematoxylin and eosin histopathology", |
| 141 | + ] |
| 142 | + result = benchmark(clip_instance, sample_image_path, labels) |
| 143 | + assert isinstance(result, dict) |
| 144 | + assert len(result) == len(labels) |
| 145 | + |
| 146 | + |
| 147 | +# Test different preprocessing pipelines |
| 148 | +def test_clip_preprocessing_pipelines(clip_instance, sample_image_path): |
| 149 | + labels = ["label1", "label2"] |
| 150 | + image = Image.open(sample_image_path) |
| 151 | + |
| 152 | + # Test preprocessing for training |
| 153 | + processed_image_train = clip_instance.preprocess_train(image) |
| 154 | + assert isinstance(processed_image_train, torch.Tensor) |
| 155 | + |
| 156 | + # Test preprocessing for validation |
| 157 | + processed_image_val = clip_instance.preprocess_val(image) |
| 158 | + assert isinstance(processed_image_val, torch.Tensor) |
| 159 | + |
| 160 | + |
| 161 | +# ... |
0 commit comments