Skip to content

Commit a05c8fa

Browse files
[pre-commit.ci] pre-commit autoupdate (#105)
* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/pre-commit/pre-commit-hooks: v4.2.0 → v4.5.0](pre-commit/pre-commit-hooks@v4.2.0...v4.5.0) - [github.com/PyCQA/isort: 5.12.0 → 5.13.2](PyCQA/isort@5.12.0...5.13.2) - [github.com/asottile/pyupgrade: v2.32.1 → v3.15.0](asottile/pyupgrade@v2.32.1...v3.15.0) - [github.com/psf/black: 22.3.0 → 24.1.1](psf/black@22.3.0...24.1.1) - [github.com/PyCQA/pylint: v2.13.8 → v3.0.3](pylint-dev/pylint@v2.13.8...v3.0.3) - [github.com/pre-commit/mirrors-mypy: v0.991 → v1.8.0](pre-commit/mirrors-mypy@v0.991...v1.8.0) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Make pre-commit happy about code style --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Michael Osthege <m.osthege@fz-juelich.de>
1 parent cddbf05 commit a05c8fa

File tree

8 files changed

+38
-20
lines changed

8 files changed

+38
-20
lines changed

.pre-commit-config.yaml

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
exclude: ^(docs|mcbackend/testdata)/
22
repos:
33
- repo: https://github.com/pre-commit/pre-commit-hooks
4-
rev: v4.2.0
4+
rev: v4.5.0
55
hooks:
66
- id: check-merge-conflict
77
- id: check-toml
@@ -10,28 +10,28 @@ repos:
1010
- id: end-of-file-fixer
1111
- id: requirements-txt-fixer
1212
- repo: https://github.com/PyCQA/isort
13-
rev: 5.12.0
13+
rev: 5.13.2
1414
hooks:
1515
- id: isort
1616
name: isort
1717
args: ["--profile", "black"]
1818
- repo: https://github.com/asottile/pyupgrade
19-
rev: v2.32.1
19+
rev: v3.15.0
2020
hooks:
2121
- id: pyupgrade
2222
args: [--py37-plus]
2323
- repo: https://github.com/psf/black
24-
rev: 22.3.0
24+
rev: 24.1.1
2525
hooks:
2626
- id: black
2727
- repo: https://github.com/PyCQA/pylint
28-
rev: v2.13.8
28+
rev: v3.0.3
2929
hooks:
3030
- id: pylint
3131
args: [--rcfile=.pylintrc]
3232
exclude: (test_*|mcbackend/meta.py|mcbackend/npproto/)
3333
files: ^mcbackend/
3434
- repo: https://github.com/pre-commit/mirrors-mypy
35-
rev: v0.991
35+
rev: v1.8.0
3636
hooks:
3737
- id: mypy

mcbackend/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
A framework agnostic implementation for storage of MCMC draws.
33
"""
4+
45
from .backends.numpy import NumPyBackend
56
from .core import Backend, Chain, Run
67
from .meta import ChainMeta, Coordinate, DataVariable, ExtendedValue, RunMeta, Variable

mcbackend/backends/clickhouse.py

+19-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
This module implements a backend for sample storage in ClickHouse.
33
"""
4+
45
import base64
56
import logging
67
import time
@@ -45,6 +46,10 @@
4546
}
4647

4748

49+
class ClickHouseBackendError(Exception):
50+
"""Something bad happened in the ClickHouse backend."""
51+
52+
4853
def create_runs_table(client: clickhouse_driver.Client):
4954
query = """
5055
CREATE TABLE IF NOT EXISTS runs (
@@ -79,7 +84,7 @@ def create_chain_table(client: clickhouse_driver.Client, meta: ChainMeta, rmeta:
7984
# Check that it does not already exist
8085
cid = chain_id(meta)
8186
if client.execute(f"SHOW TABLES LIKE '{cid}';"):
82-
raise Exception(f"A table for {cid} already exists.")
87+
raise ClickHouseBackendError(f"A table for {cid} already exists.")
8388

8489
# Create a table with columns corresponding to the model variables
8590
columns = []
@@ -235,7 +240,7 @@ def _get_row_at(
235240
query = f"SELECT (`{names}`,) FROM {self.cid} WHERE _draw_idx={idx};"
236241
data = self._client.execute(query)
237242
if not data:
238-
raise Exception(f"No record found for draw index {idx}.")
243+
raise ClickHouseBackendError(f"No record found for draw index {idx}.")
239244
result = dict(zip(var_names, data[0][0]))
240245
return result
241246

@@ -363,7 +368,10 @@ def __init__(
363368
raise ValueError("Either a `client` or a `client_fn` must be provided.")
364369

365370
if client_fn is None:
366-
client_fn = lambda: client
371+
372+
def client_fn():
373+
return client
374+
367375
if client is None:
368376
client = client_fn()
369377

@@ -381,11 +389,11 @@ def init_run(self, meta: RunMeta) -> ClickHouseRun:
381389
else:
382390
created_at = datetime.now().astimezone(timezone.utc)
383391
query = "INSERT INTO runs (created_at, rid, proto) VALUES"
384-
params = dict(
385-
created_at=created_at,
386-
rid=meta.rid,
387-
proto=base64.encodebytes(bytes(meta)).decode("ascii"),
388-
)
392+
params = {
393+
"created_at": created_at,
394+
"rid": meta.rid,
395+
"proto": base64.encodebytes(bytes(meta)).decode("ascii"),
396+
}
389397
self._client.execute(query, [params])
390398
return ClickHouseRun(meta, client_fn=self._client_fn, created_at=created_at)
391399

@@ -407,7 +415,9 @@ def get_run(self, rid: str) -> ClickHouseRun:
407415
{"rid": rid},
408416
)
409417
if len(rows) != 1:
410-
raise Exception(f"Unexpected number of {len(rows)} results for rid='{rid}'.")
418+
raise ClickHouseBackendError(
419+
f"Unexpected number of {len(rows)} results for rid='{rid}'."
420+
)
411421
data = base64.decodebytes(rows[0][2].encode("ascii"))
412422
meta = RunMeta().parse(data)
413423
return ClickHouseRun(

mcbackend/backends/numpy.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
This backend holds draws in memory, managing them via NumPy arrays.
33
"""
4+
45
import math
56
from typing import Dict, List, Mapping, Optional, Sequence, Tuple
67

@@ -110,7 +111,7 @@ class NumPyRun(Run):
110111
"""An MCMC run where samples are kept in memory."""
111112

112113
def __init__(self, meta: RunMeta, *, preallocate: int) -> None:
113-
self._settings = dict(preallocate=preallocate)
114+
self._settings = {"preallocate": preallocate}
114115
self._chains: List[NumPyChain] = []
115116
super().__init__(meta)
116117

@@ -128,9 +129,7 @@ class NumPyBackend(Backend):
128129
"""An in-memory backend using NumPy."""
129130

130131
def __init__(self, preallocate: int = 1_000) -> None:
131-
self._settings = dict(
132-
preallocate=preallocate,
133-
)
132+
self._settings = {"preallocate": preallocate}
134133
super().__init__()
135134

136135
def init_run(self, meta: RunMeta) -> NumPyRun:

mcbackend/core.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Module with metadata structures and abstract classes.
33
"""
4+
45
import collections
56
import logging
67
from typing import Dict, List, Mapping, Optional, Sequence, Sized, TypeVar, Union, cast
@@ -26,6 +27,10 @@
2627
__all__ = ("is_rigid", "chain_id", "Chain", "Run", "Backend")
2728

2829

30+
class ChainError(Exception):
31+
"""Something is not right in one chain."""
32+
33+
2934
def is_rigid(nshape: Optional[Shape]):
3035
"""Determines wheather the shape is constant.
3136
@@ -118,7 +123,7 @@ def __len__(self) -> int:
118123
]:
119124
for var in items:
120125
return len(method(var.name))
121-
raise Exception("This chain has no variables or sample stats.")
126+
raise ChainError("This chain has no variables or sample stats.")
122127

123128
@property
124129
def cid(self) -> str:

mcbackend/npproto/utils.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Helper functions such as converters between ``ndarray`` and ``Ndarray``.
33
"""
4+
45
import numpy
56

67
from . import Ndarray

mcbackend/utils.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Contains helper functions that are independent of McBackend components."""
2+
23
from typing import Sequence
34

45
import numpy as np

protobufs/generate.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
This script regenerates the `meta.py` module
33
from protobuf definitions of core metadata structures.
44
"""
5+
56
import pathlib
67
import shutil
78
import subprocess

0 commit comments

Comments
 (0)