Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace black with ruff and add pre-commit #110

Merged
merged 17 commits into from
Oct 22, 2024
Merged
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
6 changes: 3 additions & 3 deletions .github/workflows/style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install Black
- name: Install Ruff
run: |
python -m pip install --upgrade pip
pip install black
pip install ruff
- name: Check style
run: |
black --check *.py deepsensor tests
ruff check
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.7.0
hooks:
# Run the linter.
- id: ruff
# Run the formatter.
- id: ruff-format
14 changes: 14 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ That is, please do not use the [rebase](https://help.github.com/en/articles/abou
command to edit previous commit messages, combine multiple commits into one, or delete or revert
commits that are no longer necessary.

Make sure you're using the developer dependencies.
If you're working locally on the source code, *before* commiting, please run `pip install -r requirements/requirements.dev.txt` to install some useful dependencies just for development.
This includes `pre-commit` and `ruff` which are used to check and format your code style when you run `git commit`, so that you don't have to.
davidwilby marked this conversation as resolved.
Show resolved Hide resolved

Using pre-commit:
+ To make this work, just run `pre-commit install` and when you commit, `ruff` will be run to check the style of the files that you've changed.
+ Note, you may find that if `ruff` needs to edit your files, you'll have to run `git add ...` and `git commit ..` again as the pre-commit hook will stop the commit until the changes pass its tests, `ruff` will also have slightly edited the files you added, so you'll need to stage and commit again.

Without pre-commit:
+ Alternatively, you can run `ruff` yourself (without) `pre-commit` by installing `ruff` as above and just running `ruff format`.

You should also run `pytest` and check that your changes don't break any of the existing tests.
If you've made changes to the source code, you may need to add some tests to make sure that they don't get broken in the future.

#### 4. Open a Pull Request

We encourage you to open a pull request as early in your contributing process as possible.
Expand Down
2 changes: 1 addition & 1 deletion deepsensor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Backend:
"""Backend for deepsensor
"""Backend for deepsensor.

This class is used to provide a consistent interface for either tensorflow or
pytorch backends. It is used to assign the backend to the deepsensor module.
Expand Down
81 changes: 29 additions & 52 deletions deepsensor/active_learning/acquisition_fns.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@


class AcquisitionFunction:
"""
Parent class for acquisition functions.
"""
"""Parent class for acquisition functions."""

# Class attribute to indicate whether the acquisition function should be
# minimised or maximised
Expand All @@ -24,23 +22,21 @@ def __init__(
context_set_idx: int = 0,
target_set_idx: int = 0,
):
"""
Args:
model (:class:`~.model.model.ProbabilisticModel`):
[Description of the model parameter.]
context_set_idx (int):
Index of context set to add new observations to when computing
the acquisition function.
target_set_idx (int):
Index of target set to compute acquisition function for.
"""Args:
model (:class:`~.model.model.ProbabilisticModel`):
[Description of the model parameter.]
context_set_idx (int):
Index of context set to add new observations to when computing
the acquisition function.
target_set_idx (int):
Index of target set to compute acquisition function for.
"""
self.model = model
self.context_set_idx = context_set_idx
self.target_set_idx = target_set_idx

def __call__(self, task: Task, *args, **kwargs) -> np.ndarray:
"""
...
"""...

:no-index:

Expand All @@ -61,21 +57,18 @@ def __call__(self, task: Task, *args, **kwargs) -> np.ndarray:


class AcquisitionFunctionOracle(AcquisitionFunction):
"""
Signifies that the acquisition function is computed using the true
"""Signifies that the acquisition function is computed using the true
target values.
"""


class AcquisitionFunctionParallel(AcquisitionFunction):
"""
Parent class for acquisition functions that are computed across all search
"""Parent class for acquisition functions that are computed across all search
points in parallel.
"""

def __call__(self, task: Task, X_s: np.ndarray, **kwargs) -> np.ndarray:
"""
...
"""...

:param **kwargs:
:no-index:
Expand Down Expand Up @@ -104,8 +97,7 @@ class MeanStddev(AcquisitionFunction):
min_or_max = "min"

def __call__(self, task: Task):
"""
...
"""...

:no-index:

Expand All @@ -126,8 +118,7 @@ class MeanVariance(AcquisitionFunction):
min_or_max = "min"

def __call__(self, task: Task):
"""
...
"""...

:no-index:

Expand All @@ -148,8 +139,7 @@ class pNormStddev(AcquisitionFunction):
min_or_max = "min"

def __init__(self, *args, p: int = 1, **kwargs):
"""
...
"""...

:no-index:

Expand All @@ -161,8 +151,7 @@ def __init__(self, *args, p: int = 1, **kwargs):
self.p = p

def __call__(self, task: Task):
"""
...
"""...

:no-index:

Expand All @@ -185,8 +174,7 @@ class MeanMarginalEntropy(AcquisitionFunction):
min_or_max = "min"

def __call__(self, task):
"""
...
"""...

:no-index:

Expand All @@ -208,8 +196,7 @@ class JointEntropy(AcquisitionFunction):
min_or_max = "min"

def __call__(self, task: Task):
"""
...
"""...

:no-index:

Expand All @@ -230,8 +217,7 @@ class OracleMAE(AcquisitionFunctionOracle):
min_or_max = "min"

def __call__(self, task: Task):
"""
...
"""...

:no-index:

Expand All @@ -256,8 +242,7 @@ class OracleRMSE(AcquisitionFunctionOracle):
min_or_max = "min"

def __call__(self, task: Task):
"""
...
"""...

:no-index:

Expand All @@ -282,8 +267,7 @@ class OracleMarginalNLL(AcquisitionFunctionOracle):
min_or_max = "min"

def __call__(self, task: Task):
"""
...
"""...

:no-index:

Expand All @@ -308,8 +292,7 @@ class OracleJointNLL(AcquisitionFunctionOracle):
min_or_max = "min"

def __call__(self, task: Task):
"""
...
"""...

:no-index:

Expand All @@ -330,8 +313,7 @@ class Random(AcquisitionFunctionParallel):
min_or_max = "max"

def __init__(self, *args, seed: int = 42, **kwargs):
"""
...
"""...

:no-index:

Expand All @@ -343,8 +325,7 @@ def __init__(self, *args, seed: int = 42, **kwargs):
self.rng = np.random.default_rng(seed)

def __call__(self, task: Task, X_s: np.ndarray, **kwargs):
"""
...
"""...

:param **kwargs:
:no-index:
Expand All @@ -368,8 +349,7 @@ class ContextDist(AcquisitionFunctionParallel):
min_or_max = "max"

def __call__(self, task: Task, X_s: np.ndarray, **kwargs):
"""
...
"""...

:param **kwargs:
:no-index:
Expand Down Expand Up @@ -410,8 +390,7 @@ class Stddev(AcquisitionFunctionParallel):
min_or_max = "max"

def __call__(self, task: Task, X_s: np.ndarray, **kwargs):
"""
...
"""...

:param **kwargs:
:no-index:
Expand All @@ -434,8 +413,7 @@ def __call__(self, task: Task, X_s: np.ndarray, **kwargs):


class ExpectedImprovement(AcquisitionFunctionParallel):
"""
Expected improvement acquisition function.
"""Expected improvement acquisition function.

.. note::

Expand All @@ -446,8 +424,7 @@ class ExpectedImprovement(AcquisitionFunctionParallel):
min_or_max = "max"

def __call__(self, task: Task, X_s: np.ndarray, **kwargs) -> np.ndarray:
"""
:param **kwargs:
""":param **kwargs:
:no-index:

Args:
Expand Down
20 changes: 8 additions & 12 deletions deepsensor/active_learning/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def _validate_n_new_context(
)

def _get_times_from_tasks(self):
"""Get times from tasks"""
"""Get times from tasks."""
times = [task["time"] for task in self.tasks]
# Check for any repeats
if len(times) != len(set(times)):
Expand Down Expand Up @@ -285,7 +285,7 @@ def _model_infill_at_search_points(
return infill_ds

def _sample_y_infill(self, infill_ds, time, x1, x2):
"""Sample infill values at a single location"""
"""Sample infill values at a single location."""
assert isinstance(infill_ds, (xr.Dataset, xr.DataArray))
y = infill_ds.sel(time=time, x1=x1, x2=x2)
if isinstance(y, xr.Dataset):
Expand All @@ -298,9 +298,8 @@ def _sample_y_infill(self, infill_ds, time, x1, x2):
return y

def _build_acquisition_fn_ds(self, X_s: Union[xr.Dataset, xr.DataArray]):
"""
Initialise xr.DataArray for storing acquisition function values on
search grid
"""Initialise xr.DataArray for storing acquisition function values on
search grid.
"""
prepend_dims = ["iteration"] # , "sample"] # MC sample TODO
prepend_coords = {
Expand All @@ -320,7 +319,7 @@ def _build_acquisition_fn_ds(self, X_s: Union[xr.Dataset, xr.DataArray]):
return acquisition_fn_ds

def _init_acquisition_fn_object(self, X_s: xr.Dataset):
"""Instantiate acquisition function object"""
"""Instantiate acquisition function object."""
# Unnormalise before instantiating
X_s = self.model.data_processor.map_coords(X_s, unnorm=True)
if isinstance(X_s, (xr.Dataset, xr.DataArray)):
Expand All @@ -335,8 +334,7 @@ def _init_acquisition_fn_object(self, X_s: xr.Dataset):
raise TypeError(f"Unsupported type for X_s: {type(X_s)}")

def _search(self, acquisition_fn: AcquisitionFunction):
"""
Run one greedy pass by looping over each point in ``X_s`` and
"""Run one greedy pass by looping over each point in ``X_s`` and
computing the acquisition function.
"""
importances_list = []
Expand Down Expand Up @@ -434,8 +432,7 @@ def _select_best(self, importances, X_s_arr):
return best_x_query

def _single_greedy_iteration(self, acquisition_fn: AcquisitionFunction):
"""
Run a single greedy grid search iteration and append the optimal
"""Run a single greedy grid search iteration and append the optimal
context location to self.X_new.
"""
importances = self._search(acquisition_fn)
Expand All @@ -451,8 +448,7 @@ def __call__(
tasks: Union[List[Task], Task],
diff: bool = False,
) -> Tuple[pd.DataFrame, xr.Dataset]:
"""
Iteratively propose new context points using the greedy sensor placement algorithm.
"""Iteratively propose new context points using the greedy sensor placement algorithm.

Args:
acquisition_fn (:class:`~.active_learning.acquisition_fns.AcquisitionFunction`):
Expand Down
4 changes: 1 addition & 3 deletions deepsensor/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
Configuration file for deepsensor
"""
"""Configuration file for deepsensor."""

DEFAULT_LAB_EPSILON = 1e-6
"""
Expand Down
Loading
Loading