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
68 changes: 67 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,74 @@
# IDE settings
.idea/
.vscode/
*.swp
*.swo
*~

# Logs and outputs
logs/
outputs/

# Model files
*.pth
*.pkl
*.h5

# Compiled files
*.so
*.pyc
*.pyo
__pycache__/
*.o
pycocotools
*_ext*

# Testing
.pytest_cache/
.coverage
htmlcov/
coverage.xml
*.cover
.hypothesis/
.tox/
.nox/

# Virtual environments
venv/
env/
ENV/
.venv/
.env

# Package management
*.egg-info/
dist/
build/
*.egg
pip-wheel-metadata/

# Claude settings
.claude/*

# Poetry
poetry.lock

# OS files
.DS_Store
Thumbs.db

# Jupyter
.ipynb_checkpoints/
*.ipynb_checkpoints

# Data files
data/
*.tfrecords
*.record

# COCO tools
pycocotools

# Temporary files
*.tmp
*.temp
.cache/
120 changes: 120 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
[tool.poetry]
name = "mask-rcnn-pytorch"
version = "0.1.0"
description = "PyTorch implementation of Mask R-CNN for object detection and instance segmentation"
authors = ["Your Name <your.email@example.com>"]
readme = "README.md"
packages = [{include = "nms"}, {include = "roialign"}]

[tool.poetry.dependencies]
python = "^3.8.1"
torch = ">=1.0.0"
torchvision = ">=0.2.1"
numpy = ">=1.14.5"
scipy = ">=1.1.0"
pillow = ">=5.2.0"
scikit-image = ">=0.14.0"
h5py = ">=2.8.0"
opencv-python = ">=3.4.2"
matplotlib = ">=2.2.2"
imageio = ">=2.3.0"

[tool.poetry.group.dev.dependencies]
pytest = "^7.4.0"
pytest-cov = "^4.1.0"
pytest-mock = "^3.11.1"
black = "^23.7.0"
isort = "^5.12.0"
flake8 = "^6.1.0"
mypy = "^1.5.1"

[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 = [
"-ra",
"--strict-markers",
"--strict-config",
"--cov=.",
"--cov-branch",
"--cov-report=term-missing:skip-covered",
"--cov-report=html:htmlcov",
"--cov-report=xml:coverage.xml",
"--cov-fail-under=80",
"-vv",
]
markers = [
"unit: Unit tests",
"integration: Integration tests",
"slow: Slow running tests",
]
filterwarnings = [
"ignore::DeprecationWarning",
"ignore::PendingDeprecationWarning",
]

[tool.coverage.run]
source = ["."]
omit = [
"*/tests/*",
"*/test_*",
"*/__pycache__/*",
"*/venv/*",
"*/env/*",
"*/.venv/*",
"*/site-packages/*",
"setup.py",
"*/migrations/*",
"*/__init__.py",
"*/conftest.py",
]

[tool.coverage.report]
exclude_lines = [
"pragma: no cover",
"def __repr__",
"if self.debug:",
"if __name__ == .__main__.:",
"raise AssertionError",
"raise NotImplementedError",
"if TYPE_CHECKING:",
"@abstract",
"@abstractmethod",
]
precision = 2
show_missing = true
skip_covered = true

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

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

[tool.isort]
profile = "black"
line_length = 88
known_first_party = ["nms", "roialign"]

[tool.black]
line-length = 88
target-version = ['py38', 'py39', 'py310', 'py311']
include = '\.pyi?$'

[tool.mypy]
python_version = "3.8"
warn_return_any = true
warn_unused_configs = true
disallow_untyped_defs = false
ignore_missing_imports = true

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
160 changes: 160 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# Testing Infrastructure

This directory contains the testing infrastructure for the Mask R-CNN PyTorch project.

## Setup

The testing infrastructure uses Poetry for dependency management and pytest as the testing framework.

### Installing Dependencies

To install all dependencies including testing tools:

```bash
poetry install --with dev
```

To install only testing dependencies:

```bash
poetry install --only dev
```

## Running Tests

There are two equivalent commands to run tests:

```bash
poetry run test
# or
poetry run tests
```

### Running Specific Tests

```bash
# Run a specific test file
poetry run test tests/test_basic_infrastructure.py

# Run tests with verbose output
poetry run test -v

# Run only unit tests
poetry run test -m unit

# Run only integration tests
poetry run test -m integration

# Skip slow tests
poetry run test -m "not slow"
```

### Coverage Reports

Coverage is automatically calculated when running tests. To generate reports:

```bash
# Run tests with coverage (default)
poetry run test

# Generate HTML coverage report (created in htmlcov/)
poetry run test --cov-report=html

# Run tests without coverage
poetry run test --no-cov
```

## Directory Structure

```
tests/
├── README.md # This file
├── __init__.py # Makes tests a package
├── conftest.py # Shared fixtures and configuration
├── conftest_full.py.bak # Full fixtures (requires all dependencies)
├── unit/ # Unit tests
│ └── __init__.py
├── integration/ # Integration tests
│ └── __init__.py
└── test_*.py # Test files
```

## Available Fixtures

The `conftest.py` file provides several useful fixtures:

- `temp_dir`: Creates a temporary directory
- `mock_config`: Mock configuration object for Mask R-CNN
- `capture_stdout`: Capture print statements in tests
- `mock_file_system`: Create a mock file system structure
- `environment_variables`: Set up test environment variables

When all dependencies are installed, additional fixtures are available in `conftest_full.py.bak`:

- `sample_image`: NumPy array image
- `sample_pil_image`: PIL Image object
- `sample_tensor`: PyTorch tensor
- `mock_dataset`: Mock dataset object
- `sample_annotations`: Sample annotation data
- `gpu_available`: Check GPU availability
- `device`: Get appropriate device (CPU/GPU)
- `mock_model`: Mock Mask R-CNN model
- `sample_batch`: Sample batch for model testing

## Test Markers

Tests can be marked with:

- `@pytest.mark.unit` - Unit tests
- `@pytest.mark.integration` - Integration tests
- `@pytest.mark.slow` - Slow running tests

## Configuration

All testing configuration is in `pyproject.toml`:

- Test discovery patterns
- Coverage settings (80% threshold)
- Output formatting
- Custom markers
- Coverage exclusions

## Writing Tests

Example test file:

```python
import pytest
from unittest.mock import Mock


class TestExample:
"""Test class for example functionality."""

@pytest.mark.unit
def test_simple_assertion(self):
"""Test simple assertion."""
assert 1 + 1 == 2

def test_with_fixture(self, temp_dir):
"""Test using a fixture."""
test_file = temp_dir / "test.txt"
test_file.write_text("content")
assert test_file.read_text() == "content"

@pytest.mark.parametrize("input,expected", [
(1, 2),
(2, 4),
(3, 6),
])
def test_parametrized(self, input, expected):
"""Test with parameters."""
assert input * 2 == expected
```

## Notes

1. The current `conftest.py` is minimal to allow testing without all project dependencies
2. Once all dependencies are installed, replace it with `conftest_full.py.bak`
3. Coverage threshold is set to 80% - tests will fail if coverage drops below this
4. The `.gitignore` file is configured to exclude all testing artifacts
Empty file added tests/__init__.py
Empty file.
Loading