This repository was archived by the owner on Oct 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 495
Expand file tree
/
Copy pathTypeGuardai.py
More file actions
81 lines (63 loc) · 2.45 KB
/
TypeGuardai.py
File metadata and controls
81 lines (63 loc) · 2.45 KB
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
# model_utils_original.py
# This is the original code without type hints.
def calculate_f1_score(precision, recall):
"""
Calculates the F1 score from precision and recall.
This function currently doesn't check the input types,
leading to potential runtime errors if non-numeric inputs are provided.
"""
if precision + recall == 0:
return 0.0
return 2 * (precision * recall) / (precision + recall)
def normalize_data(data):
"""
Normalizes a list of numeric data to a 0-1 range.
"""
min_val = min(data)
max_val = max(data)
if max_val == min_val:
# Handle case where all values are the same
return [0.0] * len(data)
return [(x - min_val) / (max_val - min_val) for x in data]
# Example usage (will not be included in the contribution):
# print(calculate_f1_score(0.8, 0.7))
# print(normalize_data([10, 20, 30, 40]))
# model_utils_typed.py
# 'TypeGuard AI' Contribution: Code with Python Type Hinting
from typing import List, Union
def calculate_f1_score(precision: Union[int, float], recall: Union[int, float]) -> float:
"""
Calculates the F1 score from precision and recall.
Args:
precision: The precision value (between 0 and 1).
recall: The recall value (between 0 and 1).
Returns:
The calculated F1 score as a float.
"""
# Type hints ensure static analysis tools (like MyPy) can check types
# before runtime, improving code stability.
if precision + recall == 0:
return 0.0
return 2 * (precision * recall) / (precision + recall)
def normalize_data(data: List[Union[int, float]]) -> List[float]:
"""
Normalizes a list of numeric data to a 0-1 range.
Args:
data: A list containing integers or floats.
Returns:
A new list containing normalized values as floats.
"""
if not data:
return []
min_val: Union[int, float] = min(data)
max_val: Union[int, float] = max(data)
if max_val == min_val:
return [0.0] * len(data)
return [(x - min_val) / (max_val - min_val) for x in data]
# Example usage (for testing, not part of the final contribution file):
if __name__ == '__main__':
# Type hints help developers understand expected input/output
f1: float = calculate_f1_score(precision=0.9, recall=0.8)
print(f"F1 Score: {f1}")
normalized: List[float] = normalize_data(data=[1, 5, 10, 25])
print(f"Normalized Data: {normalized}")