Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
Pipfile.lock

# poetry
# Note: We do NOT ignore poetry.lock as it should be committed
# poetry.lock

# pdm
.pdm.toml

# PEP 582
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# Claude settings
.claude/*

# IDE
.vscode/
.idea/
*.swp
*.swo
*~
.DS_Store

# Project specific
*.zip
*.tar.gz
282 changes: 282 additions & 0 deletions poetry.lock

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
[tool.poetry]
name = "ml-algorithms"
version = "0.1.0"
description = "Machine Learning Algorithms Implementation"
authors = ["Your Name <you@example.com>"]
readme = "README.md"
packages = [{include = "ch2"}, {include = "ch3"}, {include = "ch4"}, {include = "ch5"}, {include = "ch6"}, {include = "ch7"}, {include = "ch8"}]

[tool.poetry.dependencies]
python = "^3.8"

[tool.poetry.group.dev.dependencies]
pytest = "^7.4.0"
pytest-cov = "^4.1.0"
pytest-mock = "^3.11.0"

[tool.poetry.scripts]
test = "pytest:main"
tests = "pytest:main"

[tool.pytest.ini_options]
minversion = "7.0"
testpaths = ["tests"]
python_files = ["test_*.py", "*_test.py"]
python_classes = ["Test*"]
python_functions = ["test_*"]
addopts = [
"--strict-markers",
"--verbose",
"-ra",
"--cov=ch2",
"--cov=ch3",
"--cov=ch4",
"--cov=ch5",
"--cov=ch6",
"--cov=ch7",
"--cov=ch8",
"--cov-report=term-missing",
"--cov-report=html",
"--cov-report=xml",
"--cov-fail-under=0", # Set to 80 when you have sufficient test coverage
]
markers = [
"unit: marks tests as unit tests",
"integration: marks tests as integration tests",
"slow: marks tests as slow running",
]

[tool.coverage.run]
source = ["ch2", "ch3", "ch4", "ch5", "ch6", "ch7", "ch8"]
omit = [
"*/tests/*",
"*/__pycache__/*",
"*/venv/*",
"*/.venv/*",
]

[tool.coverage.report]
precision = 2
show_missing = true
skip_covered = false
fail_under = 0 # Set to 80 when you have sufficient test coverage

[tool.coverage.html]
directory = "htmlcov"

[tool.coverage.xml]
output = "coverage.xml"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
Empty file added tests/__init__.py
Empty file.
78 changes: 78 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""Shared pytest fixtures and configuration for all tests."""

import os
import tempfile
from pathlib import Path
from typing import Generator

import pytest


@pytest.fixture
def temp_dir() -> Generator[Path, None, None]:
"""Create a temporary directory for test files."""
with tempfile.TemporaryDirectory() as tmp_dir:
yield Path(tmp_dir)


@pytest.fixture
def sample_data_file(temp_dir: Path) -> Path:
"""Create a sample data file for testing."""
data_file = temp_dir / "sample_data.txt"
data_file.write_text("1,2,3\n4,5,6\n7,8,9\n")
return data_file


@pytest.fixture
def mock_config() -> dict:
"""Provide a mock configuration dictionary."""
return {
"algorithm": "knn",
"k": 3,
"distance_metric": "euclidean",
"normalize": True,
"random_seed": 42,
}


@pytest.fixture
def sample_dataset() -> list:
"""Provide a sample dataset for testing ML algorithms."""
return [
{"features": [1.0, 2.0, 3.0], "label": "A"},
{"features": [4.0, 5.0, 6.0], "label": "B"},
{"features": [7.0, 8.0, 9.0], "label": "C"},
{"features": [2.0, 3.0, 4.0], "label": "A"},
{"features": [5.0, 6.0, 7.0], "label": "B"},
]


@pytest.fixture
def sample_matrix() -> list:
"""Provide a sample 2D matrix for testing."""
return [
[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0],
]


@pytest.fixture(autouse=True)
def reset_environment():
"""Reset environment variables before each test."""
original_env = os.environ.copy()
yield
os.environ.clear()
os.environ.update(original_env)


@pytest.fixture
def capture_stdout(monkeypatch):
"""Capture stdout for testing print statements."""
import io
import sys

captured = io.StringIO()
monkeypatch.setattr(sys, 'stdout', captured)
yield captured
captured.close()
Empty file added tests/integration/__init__.py
Empty file.
Loading