Skip to main content

A Hydra plugin to extend the basic sweeper with customizable filters.

Project description

Hydra Filter Sweeper Plugin

PyPI Version Python Versions License

hydra-filter-sweeper is a plugin for Hydra that extends the basic sweeper by the addition of filters, enabling more targeted parameter sweeps.

The plugin is compatible with any Hydra launcher plugin. The minimum required Hydra version is 1.3.2.

Features

Customizable Filters

Flexible Filter Conditions

  • Include fail-safe conditions to gracefully handle possible exceptions raised by filters.

Interpolation Support

Installation

To install the plugin, use pip:

pip install hydra-filter-sweeper

Usage

To use hydra-filter-sweeper, override the default sweeper with filter at the end of the defaults list.

Filters are specified as a list of dictionaries and can be of type expr, exists, or class.

If any filter evaluates to True, the current configuration is excluded from the sweep.

If no filters list is provided or all filters evaluate to False, all configurations are included and the sweeper resembles the default behavior of Hydra's basic sweeper.

Example Configuration

defaults:
  - _self_
  - override hydra/sweeper: filter

some_value: four

hydra:
  mode: MULTIRUN
  sweeper:
    params:
      +foo: 1,2,3
      +bar: one, two, three
    filters:
      - type: expr
        expr: foo == 1 and bar == "two"
      - type: exists
        path: some_directory/some.file
      - type: class
        target: some_filter.SomeFilter
        some_arg: ${some_value}

Filters

Expression Filter (expr)

Filter configurations based on a Python expression that evaluates to True or False. The context of the expression is the configuration itself. The configuration is excluded if the expression evaluates to True.

Parameters:

  • expr (str): Python expression to evaluate.
  • fail (bool): Whether to fail if the filter raises an exception. Default is True.
  • log (bool): Whether to log the configuration if the filter evaluates to True. Default is True.

Example Configuration

hydra/sweeper/filters:
  - type: expr
    expr: foo == 1 and bar == "two"
  - type: expr
    expr: bar == ${some_value}
  - type: expr
    expr: undefined == 1 and bar == "two"
    fail: false
    log: false

Exists Filter (exists)

Checks if a specified file or directory exists in the run's directory. The configuration is excluded if the file or directory exists.

Parameters:

  • path (str): Path to the file or directory to check if it exists in the run's directory.
  • fail (bool): Whether to fail if the filter raises an exception. Default is True.
  • log (bool): Whether to log the configuration if the filter evaluates to True. Default is True.

Example Configuration

hydra/sweeper/filters:
  - type: exists
    path: some_directory/some.file
  - type: exists
    path: some_directory
    log: false
  - type: exists
    path: some_directory/${some_value}.file
  - type: exists
    path: null
    fail: false

Class Filter (class)

Applies a custom filter class to the sweep. The configuration is excluded if the filter method returns True.

Parameters:

  • target (str): Python relative import path to the class.
  • * (Any): Additional keyword arguments passed to the filter method of the class.
  • fail (bool): Whether to fail if the filter raises an exception. Default is True.
  • log (bool): Whether to log the configuration if the filter evaluates to True. Default is True.

Example Configuration

hydra/sweeper/filters:
  - type: class
    target: some_filter.SomeFilter
    some_arg: ${some_value}
    log: false
  - type: class
    target: some_filter.NonExistentFilter
    some_arg: ${some_value}
    fail: false

The SomeFilter class should inherit from AbstractFilter and implement the filter method that returns True if the configuration should be excluded.

The filter method receives the configuration, the run's directory, and any additional keyword arguments as parameters.

from omegaconf import DictConfig

from hydra_filter_sweeper import AbstractFilter


class SomeFilter(AbstractFilter):
    def filter(self, config: DictConfig, directory: str, some_arg: str) -> bool:
        return config.foo == 1 and config.bar == "two" and some_arg == "four"

Contributing

Contributions are welcome! For bug reports or requests, please submit an issue.

To contribute, please fork the repository and submit a pull request.

We use Poetry for dependency management, Ruff for code formatting, codespell for spell checking, pytest for testing, and pre-commit for managing the hooks.

Install the development dependencies with:

poetry install
pre-commit install

Both formatting and spell checking are enforced by pre-commit hooks.

We strive for 100% test coverage. To run the tests locally, use:

pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hydra_filter_sweeper-1.1.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

hydra_filter_sweeper-1.1.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file hydra_filter_sweeper-1.1.0.tar.gz.

File metadata

  • Download URL: hydra_filter_sweeper-1.1.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.9.18 Linux/6.8.0-1014-azure

File hashes

Hashes for hydra_filter_sweeper-1.1.0.tar.gz
Algorithm Hash digest
SHA256 099f6f57abcdc2763bb4f4bed41d8a9f0f228ec536793aff15d832f3ca9399b0
MD5 9dab75e059bb0b3a252bfc594db55245
BLAKE2b-256 6032cd35dbddf6fad0cb71f2e1640053190ca6bd151ae0e795e51d974c1102bd

See more details on using hashes here.

File details

Details for the file hydra_filter_sweeper-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for hydra_filter_sweeper-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 52ec5d71b45c058e67efc529923cd4e94823c93ec750b5550c0929c26dd9c1e9
MD5 309089b04b3af2d174413c51698a5672
BLAKE2b-256 7ad83ce40ba804896de7712157eb235e03d04301c4c2ba1812b99295c9ba70a8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page