File size: 9,441 Bytes
69066c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"""
Input validation utilities for NeuroSAM 3 application.
Provides validation functions for user inputs, files, and parameters.
"""

import os
from typing import Optional, Tuple
from pathlib import Path
from logger_config import logger
from config import (
    MAX_FILE_SIZE_BYTES,
    ALLOWED_IMAGE_EXTENSIONS,
    ALLOWED_ANNOTATION_EXTENSIONS,
    MIN_THRESHOLD,
    MAX_THRESHOLD,
    MIN_MASK_THRESHOLD,
    MAX_MASK_THRESHOLD,
    MAX_COORDINATE_VALUE,
    MIN_NUM_MASKS,
    MAX_NUM_MASKS,
)


class ValidationError(Exception):
    """Custom exception for validation errors."""
    pass


def validate_file_path(file_path: Optional[str]) -> Tuple[bool, Optional[str]]:
    """
    Validate that a file path exists and is accessible.
    
    Args:
        file_path: Path to validate
    
    Returns:
        Tuple of (is_valid, error_message)
    """
    if file_path is None:
        return False, "File path is None"
    
    if not isinstance(file_path, (str, Path)):
        return False, f"Invalid file path type: {type(file_path)}"
    
    file_path = str(file_path)
    
    if not os.path.exists(file_path):
        return False, f"File not found: {file_path}"
    
    if not os.path.isfile(file_path):
        return False, f"Path is not a file: {file_path}"
    
    return True, None


def validate_file_size(file_path: str) -> Tuple[bool, Optional[str]]:
    """
    Validate that a file size is within limits.
    
    Args:
        file_path: Path to file to validate
    
    Returns:
        Tuple of (is_valid, error_message)
    """
    try:
        file_size = os.path.getsize(file_path)
        if file_size > MAX_FILE_SIZE_BYTES:
            size_mb = file_size / (1024 * 1024)
            max_mb = MAX_FILE_SIZE_BYTES / (1024 * 1024)
            return False, f"File size ({size_mb:.2f} MB) exceeds maximum ({max_mb} MB)"
        return True, None
    except OSError as e:
        return False, f"Could not check file size: {e}"


def validate_file_extension(file_path: str, allowed_extensions: tuple = ALLOWED_IMAGE_EXTENSIONS) -> Tuple[bool, Optional[str]]:
    """
    Validate file extension.
    
    Args:
        file_path: Path to file
        allowed_extensions: Tuple of allowed extensions (default: image extensions)
    
    Returns:
        Tuple of (is_valid, error_message)
    """
    ext = os.path.splitext(file_path)[1].lower()
    if ext not in allowed_extensions:
        return False, f"File extension '{ext}' not allowed. Allowed: {', '.join(allowed_extensions)}"
    return True, None


def validate_image_file(file_path: Optional[str]) -> Tuple[bool, Optional[str]]:
    """
    Comprehensive validation for image files.
    
    Args:
        file_path: Path to image file
    
    Returns:
        Tuple of (is_valid, error_message)
    """
    # Check if path is valid
    is_valid, error = validate_file_path(file_path)
    if not is_valid:
        return False, error
    
    file_path = str(file_path)
    
    # Check extension
    is_valid, error = validate_file_extension(file_path, ALLOWED_IMAGE_EXTENSIONS)
    if not is_valid:
        return False, error
    
    # Check file size
    is_valid, error = validate_file_size(file_path)
    if not is_valid:
        return False, error
    
    return True, None


def validate_threshold(threshold: float) -> Tuple[bool, Optional[str]]:
    """
    Validate threshold value.
    
    Args:
        threshold: Threshold value to validate
    
    Returns:
        Tuple of (is_valid, error_message)
    """
    if not isinstance(threshold, (int, float)):
        return False, f"Threshold must be a number, got {type(threshold)}"
    
    if threshold < MIN_THRESHOLD or threshold > MAX_THRESHOLD:
        return False, f"Threshold must be between {MIN_THRESHOLD} and {MAX_THRESHOLD}, got {threshold}"
    
    return True, None


def validate_mask_threshold(mask_threshold: float) -> Tuple[bool, Optional[str]]:
    """
    Validate mask threshold value.
    
    Args:
        mask_threshold: Mask threshold value to validate
    
    Returns:
        Tuple of (is_valid, error_message)
    """
    if not isinstance(mask_threshold, (int, float)):
        return False, f"Mask threshold must be a number, got {type(mask_threshold)}"
    
    if mask_threshold < MIN_MASK_THRESHOLD or mask_threshold > MAX_MASK_THRESHOLD:
        return False, f"Mask threshold must be between {MIN_MASK_THRESHOLD} and {MAX_MASK_THRESHOLD}, got {mask_threshold}"
    
    return True, None


def validate_coordinates(x: float, y: float, max_value: int = MAX_COORDINATE_VALUE) -> Tuple[bool, Optional[str]]:
    """
    Validate coordinate values.
    
    Args:
        x: X coordinate
        y: Y coordinate
        max_value: Maximum allowed coordinate value
    
    Returns:
        Tuple of (is_valid, error_message)
    """
    if not isinstance(x, (int, float)) or not isinstance(y, (int, float)):
        return False, f"Coordinates must be numbers, got x={type(x)}, y={type(y)}"
    
    if x < 0 or y < 0:
        return False, f"Coordinates must be non-negative, got x={x}, y={y}"
    
    if x > max_value or y > max_value:
        return False, f"Coordinates exceed maximum value ({max_value}), got x={x}, y={y}"
    
    return True, None


def validate_bounding_box(x1: float, y1: float, x2: float, y2: float) -> Tuple[bool, Optional[str]]:
    """
    Validate bounding box coordinates.
    
    Args:
        x1, y1: Top-left corner coordinates
        x2, y2: Bottom-right corner coordinates
    
    Returns:
        Tuple of (is_valid, error_message)
    """
    # Validate individual coordinates
    for coord, name in [(x1, 'x1'), (y1, 'y1'), (x2, 'x2'), (y2, 'y2')]:
        if not isinstance(coord, (int, float)):
            return False, f"{name} must be a number, got {type(coord)}"
        if coord < 0:
            return False, f"{name} must be non-negative, got {coord}"
        if coord > MAX_COORDINATE_VALUE:
            return False, f"{name} exceeds maximum ({MAX_COORDINATE_VALUE}), got {coord}"
    
    # Validate box dimensions
    if x2 <= x1:
        return False, f"x2 ({x2}) must be greater than x1 ({x1})"
    
    if y2 <= y1:
        return False, f"y2 ({y2}) must be greater than y1 ({y1})"
    
    return True, None


def validate_num_masks(num_masks: int) -> Tuple[bool, Optional[str]]:
    """
    Validate number of masks parameter.
    
    Args:
        num_masks: Number of masks to generate
    
    Returns:
        Tuple of (is_valid, error_message)
    """
    if not isinstance(num_masks, int):
        return False, f"Number of masks must be an integer, got {type(num_masks)}"
    
    if num_masks < MIN_NUM_MASKS or num_masks > MAX_NUM_MASKS:
        return False, f"Number of masks must be between {MIN_NUM_MASKS} and {MAX_NUM_MASKS}, got {num_masks}"
    
    return True, None


def validate_prompt_text(prompt_text: Optional[str]) -> Tuple[bool, Optional[str], str]:
    """
    Validate and sanitize prompt text.
    
    Args:
        prompt_text: Text prompt to validate
    
    Returns:
        Tuple of (is_valid, error_message, sanitized_prompt)
    """
    if prompt_text is None:
        return True, None, "brain"  # Default prompt
    
    if not isinstance(prompt_text, str):
        return False, f"Prompt must be a string, got {type(prompt_text)}", ""
    
    # Sanitize: strip whitespace
    sanitized = prompt_text.strip()
    
    # Check length (reasonable limit)
    if len(sanitized) > 500:
        return False, "Prompt text is too long (max 500 characters)", ""
    
    # Use default if empty
    if not sanitized:
        sanitized = "brain"
    
    return True, None, sanitized


def validate_modality(modality: Optional[str]) -> Tuple[bool, Optional[str]]:
    """
    Validate imaging modality.
    
    Args:
        modality: Modality string (CT or MRI)
    
    Returns:
        Tuple of (is_valid, error_message)
    """
    if modality is None:
        return False, "Modality is required"
    
    if not isinstance(modality, str):
        return False, f"Modality must be a string, got {type(modality)}"
    
    modality_upper = modality.upper()
    if modality_upper not in ("CT", "MRI"):
        return False, f"Modality must be 'CT' or 'MRI', got '{modality}'"
    
    return True, None


def validate_transparency(transparency: float) -> Tuple[bool, Optional[str]]:
    """
    Validate transparency value.
    
    Args:
        transparency: Transparency value (0.0-1.0)
    
    Returns:
        Tuple of (is_valid, error_message)
    """
    if not isinstance(transparency, (int, float)):
        return False, f"Transparency must be a number, got {type(transparency)}"
    
    if transparency < 0.0 or transparency > 1.0:
        return False, f"Transparency must be between 0.0 and 1.0, got {transparency}"
    
    return True, None


def validate_brightness_contrast(value: float, name: str = "value") -> Tuple[bool, Optional[str]]:
    """
    Validate brightness or contrast value.
    
    Args:
        value: Brightness or contrast value
        name: Name of the parameter for error messages
    
    Returns:
        Tuple of (is_valid, error_message)
    """
    if not isinstance(value, (int, float)):
        return False, f"{name} must be a number, got {type(value)}"
    
    if value < 0.0 or value > 3.0:
        return False, f"{name} must be between 0.0 and 3.0, got {value}"
    
    return True, None