Skip to content

Commit f0602ac

Browse files
committed
fix ci & pyproject deps
1 parent 38c3f6e commit f0602ac

File tree

5 files changed

+64
-42
lines changed

5 files changed

+64
-42
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ jobs:
7171
strategy:
7272
fail-fast: true
7373
matrix:
74-
python-version: ["3.9", "3.10", "3.11", "3.12"]
74+
python-version: ['3.9']
7575
steps:
7676
- name: Checkout
7777
uses: actions/checkout@v4

albucore/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,11 @@ def is_grayscale_image(image: np.ndarray, has_batch_dim: bool = False, has_depth
247247
def get_opencv_dtype_from_numpy(value: np.ndarray | int | np.dtype | object) -> int:
248248
if isinstance(value, np.ndarray):
249249
value = value.dtype
250-
return NPDTYPE_TO_OPENCV_DTYPE[value]
250+
return int(NPDTYPE_TO_OPENCV_DTYPE[value])
251251

252252

253253
def is_rgb_image(image: np.ndarray) -> bool:
254-
return get_num_channels(image) == NUM_RGB_CHANNELS
254+
return bool(get_num_channels(image) == NUM_RGB_CHANNELS)
255255

256256

257257
def is_multispectral_image(image: np.ndarray) -> bool:
@@ -278,7 +278,7 @@ def convert_value(value: np.ndarray | float, num_channels: int) -> float | np.nd
278278
return float(value) if isinstance(value, (float, int)) else value.item()
279279

280280
# Handle numpy arrays
281-
if isinstance(value, np.ndarray):
281+
if isinstance(value, np.ndarray): # type: ignore[unreachable]
282282
# Return scalars and 0-dim arrays as float
283283
if value.ndim == 0:
284284
return value.item()

benchmark/albucore_benchmark/benchmark.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ def get_package_versions() -> dict[str, str]:
8484

8585

8686
class BenchmarkTest(ABC):
87+
# Direct mapping of library names to their transform method names
88+
LIBRARY_ATTR_MAP: dict[str, str] = {
89+
"albucore": "albucore_transform",
90+
"opencv": "opencv_transform",
91+
"numpy": "numpy_transform",
92+
"lut": "lut_transform",
93+
"kornia-rs": "kornia_transform",
94+
"torchvision": "torchvision_transform",
95+
"simsimd": "simsimd_transform",
96+
}
97+
8798
def __init__(self, num_channels: int) -> None:
8899
self.num_channels = num_channels
89100
self.img_type = None
@@ -131,30 +142,17 @@ def simsimd(self, img: np.ndarray) -> np.ndarray:
131142
return clip(self.simsimd_transform(img), img.dtype)
132143

133144
def is_supported_by(self, library: str) -> bool:
134-
library_attr_map = {
135-
"albucore": "albucore_transform",
136-
"opencv": "opencv_transform",
137-
"numpy": "numpy_transform",
138-
"lut": "lut_transform",
139-
"kornia-rs": "kornia_transform",
140-
"torchvision": "torchvision_transform",
141-
"simsimd": "simsimd_transform",
142-
}
143-
144-
# Check if the library is in the map
145-
if library in library_attr_map:
146-
attrs = library_attr_map[library]
147-
# Ensure attrs is a list for uniform processing
148-
if not isinstance(attrs, list):
149-
attrs = [attrs] # type: ignore[assignment]
150-
# Return True if any of the specified attributes exist
151-
return any(hasattr(self, attr) for attr in attrs)
152-
153-
# Fallback: checks if the class has an attribute with the library's name
154-
return hasattr(self, library)
145+
# Check if the library has a specific mapping
146+
transform_attr = self.LIBRARY_ATTR_MAP.get(library, f"{library}_transform")
147+
148+
# Return True if the transform method exists
149+
return hasattr(self, transform_attr)
155150

156151
def run(self, library: str, imgs: list[np.ndarray]) -> list[np.ndarray] | None:
157-
transform = getattr(self, library)
152+
# Get the appropriate transform method
153+
transform_attr = self.LIBRARY_ATTR_MAP.get(library, f"{library}_transform")
154+
transform = getattr(self, transform_attr)
155+
158156
transformed_images = []
159157
for img in imgs:
160158
result = transform(img)

benchmark/utils.py

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def _highlight_best_result(self, results: list[float | str]) -> list[str]:
3333
elif isinstance(result, (int, float)):
3434
parsed_results.append((float(result), 0))
3535
else:
36-
parsed_results.append((np.nan, np.nan))
36+
parsed_results.append((np.nan, np.nan)) # type: ignore[unreachable]
3737

3838
if not parsed_results or all(np.isnan(mean) for mean, _ in parsed_results):
3939
return [str(r) for r in results]
@@ -71,21 +71,40 @@ def _make_value_matrix(self) -> list[list[str]]:
7171
return value_matrix
7272

7373
def generate_markdown_table(self) -> str:
74-
# Convert DataFrame to markdown
75-
md_table = self.df.to_markdown()
76-
77-
# Add header
78-
header = f"# Benchmark Results: {self.df.index[0]}\n\n"
79-
header += f"Number of images: {self.num_images}\n\n"
80-
header += "## CPU Information\n\n"
81-
header += f"- CPU: {self.cpu_info['name']}\n"
82-
header += f"- Frequency: {self.cpu_info['freq']}\n"
83-
header += f"- Physical cores: {self.cpu_info['physical_cores']}\n"
84-
header += f"- Total cores: {self.cpu_info['total_cores']}\n\n"
85-
header += "## Package Versions\n\n"
86-
header += pd.DataFrame([self.package_versions]).to_markdown(index=False) + "\n\n"
87-
header += "## Performance (images/second)\n\n"
88-
return header + md_table
74+
# Template for the markdown report
75+
REPORT_TEMPLATE = """
76+
# Benchmark Results: {benchmark_name}
77+
78+
Number of images: {num_images}
79+
80+
## CPU Information
81+
82+
- CPU: {cpu_name}
83+
- Frequency: {cpu_freq}
84+
- Physical cores: {physical_cores}
85+
- Total cores: {total_cores}
86+
87+
## Package Versions
88+
89+
{package_versions_table}
90+
91+
## Performance (images/second)
92+
93+
{performance_table}"""
94+
95+
# Prepare the data for the template
96+
template_data = {
97+
'benchmark_name': self.df.index[0],
98+
'num_images': self.num_images,
99+
'cpu_name': self.cpu_info['name'],
100+
'cpu_freq': self.cpu_info['freq'],
101+
'physical_cores': self.cpu_info['physical_cores'],
102+
'total_cores': self.cpu_info['total_cores'],
103+
'package_versions_table': pd.DataFrame([self.package_versions]).to_markdown(index=False),
104+
'performance_table': self.df.to_markdown()
105+
}
106+
107+
return REPORT_TEMPLATE.format(**template_data)
89108

90109
def _make_versions_text(self) -> str:
91110
libraries = ["numpy", "opencv-python-headless"]

pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,8 @@ no_implicit_reexport = true
138138

139139
# for strict mypy: (this is the tricky one :-))
140140
disallow_untyped_defs = true
141+
142+
# more strict rules
143+
warn_return_any = true
144+
strict_equality = true
145+
warn_unreachable = true

0 commit comments

Comments
 (0)