Skip to content
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/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ jobs:
uses: astral-sh/setup-uv@v6
- name: Install project
run: uv sync --locked --dev
- name: Run ruff
- name: Run ruff lint
run: uv run ruff check
- name: Run black
run: uv run black --check .
- name: Run ruff format check
run: uv run ruff format --check
- name: Run mypy
run: uv run make mypy
14 changes: 9 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
RUFF_FLAGS =
BLACK_FLAGS =
FORMAT_FLAGS =
MYPY_FLAGS =

MYPY_TARGETS = \
src/odil/history.py \

default:
default: lint format

data:
git clone -b data --single-branch git@github.com:cselab/odil.git data
Expand All @@ -14,10 +14,14 @@ release:
V=$$(sed -rn 's/^version = "(.*)"$$/\1/p' pyproject.toml) && git archive --prefix="odil-$$V/" -o "odil-$$V.tar.gz" HEAD

lint:
ruff check --fix $(RUFF_FLAGS) .
black $(BLACK_FLAGS) .
ruff check --fix $(RUFF_FLAGS)

format:
ruff format $(FORMAT_FLAGS)

check: lint format

mypy:
mypy $(MYPY_FLAGS) $(MYPY_TARGETS)

.PHONY: default release lint mypy
.PHONY: default data release lint format check mypy
1 change: 0 additions & 1 deletion examples/basic/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def parse_args():


def plot(problem, state, epoch, frame, cbinfo=None):

domain = problem.domain
fig, ax = plt.subplots(figsize=(4, 2))
kw = dict(vmin=0, vmax=1, cmap="Greys", clip_on=False, lw=0.5)
Expand Down
16 changes: 8 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,27 @@ jax = ["jax[cuda13]>=0.7.0"]
[dependency-groups]
dev = [
"pytest>=7.4.4",
"black[jupyter]>=25.0.0",
"ruff>=0.14.0",
"mypy>=1.18.2",
]

[tool.black]
[tool.ruff]
line-length = 120
target-version = ['py312']
target-version = "py312"

[tool.ruff]
lint.select = [
[tool.ruff.lint]
select = [
"E", # Style errors
"W", # Style warnings
"F", # Logical errors
"I", # Import sorting
]
lint.ignore = [
ignore = [
"E741", # Ambiguous variable name
]
output-format = 'concise'
line-length = 120

[tool.ruff.format]
quote-style = "double"

[tool.mypy]
python_version = "3.12"
Expand Down
4 changes: 0 additions & 4 deletions src/odil/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@


class ModBase:

def __init__(self, mod=None):
assert mod is not None
self.mod = mod
Expand Down Expand Up @@ -43,7 +42,6 @@ def __init__(self, mod=None):


class ModNumpy(ModBase):

def __init__(self, mod=None, modsp=None, jax=None):
mod = mod or np
super().__init__(mod)
Expand Down Expand Up @@ -183,7 +181,6 @@ def conv_transpose(input, filters, output_shape=None, strides=None, padding=None


class ModTensorflow(ModBase):

def __init__(self, mod=None, modsp=None):
super().__init__(mod)
self.batch_to_space = mod.batch_to_space
Expand Down Expand Up @@ -280,7 +277,6 @@ def csr_matrix(*args, **kwargs):


class ModCupy(ModBase):

def __init__(self, mod=None, modsp=None):
super().__init__(mod)
self.cast = lambda x, dtype: mod.array(x, dtype=dtype)
Expand Down
1 change: 0 additions & 1 deletion src/odil/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ def save(content, path):
raise ValueError("Unrecognized extension '{}', expected .pickle.".format(ext))

def decorator(func):

def inner(*args, **kwargs):
name, ext = os.path.splitext(targetbase)
if len(args) and name_with_arg0:
Expand Down
14 changes: 2 additions & 12 deletions src/odil/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@


class Domain:

def __init__(
self,
cshape,
Expand Down Expand Up @@ -504,7 +503,6 @@ def get_context(self, state, extra=None, tracers=None):


class Field:

def __init__(self, array=None, loc=None, cshape=None):
"""
array: `ndarray`
Expand All @@ -527,7 +525,6 @@ def __repr__(self):


class MultigridField:

def __init__(self, terms=None, loc=None, factors=None, axes=None, method=None):
"""
terms: `list` of `Field`
Expand All @@ -550,7 +547,6 @@ def __init__(self, terms=None, loc=None, factors=None, axes=None, method=None):


class NeuralNet:

def __init__(self, weights=None, biases=None, func_in=None, func_out=None, activation=None):
"""
weights: `list` of `ndarray`
Expand All @@ -574,7 +570,6 @@ def __init__(self, weights=None, biases=None, func_in=None, func_out=None, activ


class Array:

def __init__(self, array=None, shape=None):
"""
array: `ndarray`
Expand All @@ -593,7 +588,6 @@ def __repr__(self):


class State:

def __init__(self, fields=None, initialized=False):
"""
fields: A dict mapping field names to state variables
Expand Down Expand Up @@ -863,9 +857,7 @@ def eval_neural_net(net: NeuralNet, inputs, mod, frozen=False):


class Context:

class Raw:

def __init__(self, value):
self.value = value

Expand Down Expand Up @@ -991,7 +983,6 @@ def res(*inputs):


class Problem:

def __init__(self, operator, domain, extra=None, tracers=None, jit=None):
"""
operator: callable(mod, ctx)
Expand Down Expand Up @@ -1055,7 +1046,8 @@ def func(arrays):
assert len(nonempty) == len(set(nonempty)), "Name of fields must be unique, got {}".format(nonempty)
values = [f[1] if isinstance(f, tuple) else f for f in ff]
terms = [
mod.mean(v.value) if isinstance(v, Context.Raw) else mod.mean(mod.square(v)) for v in values #
mod.mean(v.value) if isinstance(v, Context.Raw) else mod.mean(mod.square(v))
for v in values #
]
loss = sum(terms)
norms = [t if isinstance(v, Context.Raw) else mod.sqrt(t) for t, v in zip(terms, values)] #
Expand Down Expand Up @@ -1321,7 +1313,6 @@ def _eval_operator_grad_tf(self, state):

def func(arrays, tracers):
with tf.GradientTape(persistent=True, watch_accessed_variables=False) as tape:

domain.arrays_to_state(arrays, cache["state"])
ctx = Context(
domain,
Expand Down Expand Up @@ -1477,7 +1468,6 @@ def latin_hypercube(ndim, size, dtype):


class Approx:

def __init__(self, domain):
self.domain = domain
self.mod = domain.mod
Expand Down
1 change: 0 additions & 1 deletion src/odil/core_min.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Domain:

def __init__(self, domain=None, ndim=None, lower=None, upper=None, dimnames=None, dtype=None, cshape=None):
domain = domain or Namespace(ndim=None, lower=0.0, upper=1.0, dimnames=None, dtype=None, cshape=None)
dtype = dtype or domain.dtype
Expand Down
1 change: 0 additions & 1 deletion src/odil/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@


class History:

def __init__(self, csvpath: str | None = None, warmup: int = 0):
"""
warmup: `int`
Expand Down
4 changes: 1 addition & 3 deletions src/odil/linsolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ def add_arguments(parser):
parser.add_argument("--smooth_pre", type=int, default=2, help="Pre-smoothing steps")
parser.add_argument("--smooth_post", type=int, default=2, help="Post-smoothing steps")
parser.add_argument("--omega", type=float, default=0.6, help="Jacobi smoother relaxation factor")
parser.add_argument(
"--ndirect", type=int, default=3, help="Systems on smaller grids " "are solved with direct solver"
)
parser.add_argument("--ndirect", type=int, default=3, help="Systems on smaller grids are solved with direct solver")
parser.add_argument(
"--restriction",
type=str,
Expand Down
11 changes: 0 additions & 11 deletions src/odil/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Optimizer:

def __init__(self, name=None, displayname=None, dtype=None):
self.name = name
self.displayname = displayname if displayname is not None else name
Expand All @@ -20,14 +19,12 @@ def run(self, x0, loss_grad, epochs, callback=None, epoch_start=0, **kwargs):


class EarlyStopError(Exception):

def __init__(self, msg, optinfo):
super().__init__(msg)
self.optinfo = optinfo


class LbfgsbOptimizer(Optimizer):

def __init__(self, pgtol=1e-16, m=50, maxls=50, factr=0, dtype=None, mod=None, **kwargs):
"""
pgtol: `float`
Expand Down Expand Up @@ -118,7 +115,6 @@ def func_wrap(x):


class LbfgsOptimizer(Optimizer):

def __init__(self, pgtol=1e-16, m=50, maxls=50, factr=0, dtype=None, mod=None, **kwargs):
"""
pgtol: `float`
Expand Down Expand Up @@ -206,18 +202,15 @@ def stopping_condition(converged, failed):


class AdamTfOptimizer(Optimizer):

def __init__(self, dtype=None, mod=None, **kwargs):
super().__init__(name="adam_tf", displayname="AdamTf", dtype=dtype)
self.mod = mod

def run(self, x0, loss_grad, epochs=None, callback=None, lr=1e-3, epoch_start=0, **kwargs):

mod = self.mod
tf = mod.tf

class CustomModel(tf.keras.Model):

def __init__(self, x):
super().__init__()
self.x = x
Expand All @@ -238,7 +231,6 @@ def train_step(self, _):
model = CustomModel(x)

class CustomCallback(tf.keras.callbacks.Callback):

def on_epoch_end(self, epoch, logs=None):
model.epoch = epoch_start + epoch
if epoch > 0 and callback:
Expand All @@ -254,13 +246,11 @@ def on_epoch_end(self, epoch, logs=None):


class GdOptimizer(Optimizer):

def __init__(self, dtype=None, mod=None, **kwargs):
super().__init__(name="gd", displayname="GD", dtype=dtype)
self.mod = mod

def run(self, x0, loss_grad, epochs=None, callback=None, lr=1e-3, epoch_start=0, **kwargs):

mod = self.mod
x = [mod.copy(e) for e in x0]
for epoch in range(epoch_start + 1, epoch_start + epochs + 1):
Expand All @@ -278,7 +268,6 @@ def run(self, x0, loss_grad, epochs=None, callback=None, lr=1e-3, epoch_start=0,


class AdamNativeOptimizer(Optimizer):

def __init__(self, dtype=None, mod=None, **kwargs):
super().__init__(name="adamn", displayname="AdamNative", dtype=dtype)
self.mod = mod
Expand Down
2 changes: 1 addition & 1 deletion src/odil/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

if not int(os.environ.get("ODIL_MT", 0)):
os.environ["OMP_NUM_THREADS"] = "1"
os.environ["XLA_FLAGS"] = "--xla_cpu_multi_thread_eigen=false " "intra_op_parallelism_threads=1"
os.environ["XLA_FLAGS"] = "--xla_cpu_multi_thread_eigen=false intra_op_parallelism_threads=1"
os.environ["TENSORFLOW_INTER_OP_PARALLELISM"] = "1"
os.environ["TENSORFLOW_INTRA_OP_PARALLELISM"] = "1"

Expand Down
Loading
Loading