Skip to content

Commit

Permalink
[pre-commit.ci] pre-commit autoupdate (#105)
Browse files Browse the repository at this point in the history
* [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 <[email protected]>
  • Loading branch information
pre-commit-ci[bot] and michaelosthege authored Jan 30, 2024
1 parent cddbf05 commit a05c8fa
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 20 deletions.
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
exclude: ^(docs|mcbackend/testdata)/
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.2.0
rev: v4.5.0
hooks:
- id: check-merge-conflict
- id: check-toml
Expand All @@ -10,28 +10,28 @@ repos:
- id: end-of-file-fixer
- id: requirements-txt-fixer
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
rev: 5.13.2
hooks:
- id: isort
name: isort
args: ["--profile", "black"]
- repo: https://github.com/asottile/pyupgrade
rev: v2.32.1
rev: v3.15.0
hooks:
- id: pyupgrade
args: [--py37-plus]
- repo: https://github.com/psf/black
rev: 22.3.0
rev: 24.1.1
hooks:
- id: black
- repo: https://github.com/PyCQA/pylint
rev: v2.13.8
rev: v3.0.3
hooks:
- id: pylint
args: [--rcfile=.pylintrc]
exclude: (test_*|mcbackend/meta.py|mcbackend/npproto/)
files: ^mcbackend/
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.991
rev: v1.8.0
hooks:
- id: mypy
1 change: 1 addition & 0 deletions mcbackend/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
A framework agnostic implementation for storage of MCMC draws.
"""

from .backends.numpy import NumPyBackend
from .core import Backend, Chain, Run
from .meta import ChainMeta, Coordinate, DataVariable, ExtendedValue, RunMeta, Variable
Expand Down
28 changes: 19 additions & 9 deletions mcbackend/backends/clickhouse.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This module implements a backend for sample storage in ClickHouse.
"""

import base64
import logging
import time
Expand Down Expand Up @@ -45,6 +46,10 @@
}


class ClickHouseBackendError(Exception):
"""Something bad happened in the ClickHouse backend."""


def create_runs_table(client: clickhouse_driver.Client):
query = """
CREATE TABLE IF NOT EXISTS runs (
Expand Down Expand Up @@ -79,7 +84,7 @@ def create_chain_table(client: clickhouse_driver.Client, meta: ChainMeta, rmeta:
# Check that it does not already exist
cid = chain_id(meta)
if client.execute(f"SHOW TABLES LIKE '{cid}';"):
raise Exception(f"A table for {cid} already exists.")
raise ClickHouseBackendError(f"A table for {cid} already exists.")

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

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

if client_fn is None:
client_fn = lambda: client

def client_fn():
return client

if client is None:
client = client_fn()

Expand All @@ -381,11 +389,11 @@ def init_run(self, meta: RunMeta) -> ClickHouseRun:
else:
created_at = datetime.now().astimezone(timezone.utc)
query = "INSERT INTO runs (created_at, rid, proto) VALUES"
params = dict(
created_at=created_at,
rid=meta.rid,
proto=base64.encodebytes(bytes(meta)).decode("ascii"),
)
params = {
"created_at": created_at,
"rid": meta.rid,
"proto": base64.encodebytes(bytes(meta)).decode("ascii"),
}
self._client.execute(query, [params])
return ClickHouseRun(meta, client_fn=self._client_fn, created_at=created_at)

Expand All @@ -407,7 +415,9 @@ def get_run(self, rid: str) -> ClickHouseRun:
{"rid": rid},
)
if len(rows) != 1:
raise Exception(f"Unexpected number of {len(rows)} results for rid='{rid}'.")
raise ClickHouseBackendError(
f"Unexpected number of {len(rows)} results for rid='{rid}'."
)
data = base64.decodebytes(rows[0][2].encode("ascii"))
meta = RunMeta().parse(data)
return ClickHouseRun(
Expand Down
7 changes: 3 additions & 4 deletions mcbackend/backends/numpy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This backend holds draws in memory, managing them via NumPy arrays.
"""

import math
from typing import Dict, List, Mapping, Optional, Sequence, Tuple

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

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

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

def __init__(self, preallocate: int = 1_000) -> None:
self._settings = dict(
preallocate=preallocate,
)
self._settings = {"preallocate": preallocate}
super().__init__()

def init_run(self, meta: RunMeta) -> NumPyRun:
Expand Down
7 changes: 6 additions & 1 deletion mcbackend/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Module with metadata structures and abstract classes.
"""

import collections
import logging
from typing import Dict, List, Mapping, Optional, Sequence, Sized, TypeVar, Union, cast
Expand All @@ -26,6 +27,10 @@
__all__ = ("is_rigid", "chain_id", "Chain", "Run", "Backend")


class ChainError(Exception):
"""Something is not right in one chain."""


def is_rigid(nshape: Optional[Shape]):
"""Determines wheather the shape is constant.
Expand Down Expand Up @@ -118,7 +123,7 @@ def __len__(self) -> int:
]:
for var in items:
return len(method(var.name))
raise Exception("This chain has no variables or sample stats.")
raise ChainError("This chain has no variables or sample stats.")

@property
def cid(self) -> str:
Expand Down
1 change: 1 addition & 0 deletions mcbackend/npproto/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Helper functions such as converters between ``ndarray`` and ``Ndarray``.
"""

import numpy

from . import Ndarray
Expand Down
1 change: 1 addition & 0 deletions mcbackend/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Contains helper functions that are independent of McBackend components."""

from typing import Sequence

import numpy as np
Expand Down
1 change: 1 addition & 0 deletions protobufs/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
This script regenerates the `meta.py` module
from protobuf definitions of core metadata structures.
"""

import pathlib
import shutil
import subprocess
Expand Down

0 comments on commit a05c8fa

Please sign in to comment.