Coding Style
Philosophy
- Think first. State assumptions and uncertainties. Present trade-offs before implementing.
- Simplicity first. No abstractions for single-use code. No unrequested contingency handling. If a senior engineer would say "overcomplicated" — simplify.
- Surgical edits. Touch only what you must. Mention issues elsewhere; fix only the mess.
- Verify your work. Transform tasks into verifiable goals: write a test, make it pass. For multi-step tasks, state a brief plan with checks.
Be a collaborator, not a code bot. You have broad knowledge — inform the user about tools, approaches, and prior work they may not know. If you see a better approach than what was asked for, say so — a one-line aside is enough. Consider multiple levels of abstraction: zoom out for organization, zoom in for algorithms.
This is the real world and often out of your training distribution. Prefer careful incremental progress over one-shot attempts. When uncertain, ask.
Naming
- Variables: long, hierarchical, type-first.
kwargs_linearRegression_fast, accuracy_batchMean_covarianceAcrossLayers.
- Functions: descriptive verbs.
prepare_params, generate_latents.
- Paths:
filepath_* for files, dir_* for directories, plurals for collections (filepaths_models).
- Args: descriptive (not single letters unless math context). Always use explicit keywords:
func(x=x, y=y).
- Use
pathlib.Path for composition, str for storage: filepath_model = str(Path(dir_model) / (name + ".pth")).
Imports
Import top-level libraries, call via namespace (torch.nn.functional.cosine_similarity(...)). Exceptions: from tqdm.auto import tqdm, from pathlib import Path.
Group by category with blank-line separators:
from typing import List ## typing
import os
import sys
from pathlib import Path ## built-ins
import numpy as np
import torch ## third-party
import basic_neural_processing_modules as bnpm ## personal
from .model_def import build_model ## local
Documentation
Docstrings: RST/Google-style, nested descriptions for Args and Returns. Don't restate defaults visible in the signature. Even small utilities need docstrings. Document Raises: for non-obvious exceptions.
def phase_correlation(
im_template: Union[np.ndarray, torch.Tensor],
im_moving: Union[np.ndarray, torch.Tensor],
mask_fft: Optional[Union[np.ndarray, torch.Tensor]] = None,
eps: float = 1e-8,
) -> Tuple[np.ndarray, ...]:
"""
Perform phase correlation on two images along last two axes (height, width).
Args:
im_template (np.ndarray):
Template image(s). Shape: (..., height, width).
im_moving (np.ndarray):
Moving image. Must broadcast with template.
mask_fft (Optional[np.ndarray]):
2D FFT mask. ``None`` means no masking.
eps (float):
Prevents division by zero.
Returns:
(Tuple[np.ndarray, ...]):
cc (np.ndarray): Phase correlation coefficient.
"""
Comments: inline for shapes, types, broadcasting. Step headers (## Section) every 3–10 lines. References to papers and equations encouraged.
Libraries & Abstraction
- Use third-party libraries — someone else has probably solved your problem. Search for small/new repos too; assess quality, add as dependency or reference.
- Keep hierarchy flat. Abstract only to remove duplication or avoid holding large globals.
- Prefer multi-use functions over many decoupled ones.
- Prefer straightforward constructs. Returning functions, custom decorators, and special class behaviors are worth it when they genuinely reduce complexity — use judgment.
Numerical Computing
- Jit-friendly when feasible (numba, torch.jit, jax). No in-place ops,
if: raise() not assert, rigid shapes. See JAX gotchas.
- Torch idioms:
[None, ...] not .unsqueeze(0). No .squeeze(). torch.as_tensor(...) for ingestion.
- Always comment broadcasting and shape changes.
- Vectorize over for-loops. einsum, vmap, scatter-add, sparse arrays all good.
- Data: prefer dicts over pandas unless pandas methods help. Dataclasses and configs good. Large arrays:
np.memmap or zarr. Save results as .npy/.npz.
- Randomness: accept seeds or document stochastic behavior.
- GPU memory: delete large intermediates (
del tensor; torch.cuda.empty_cache()). Use configurable device strings.
- Saving data: prefer human-readable formats (
.csv, .json) for small data. For large arrays, .npy/.npz. For complex structures, richfile. Try to never pickle.
- When the standard approach is inadequate, be proactive in reaching for the unusual tool — a sparse representation, an unconventional decomposition, a bespoke library, a mathematical equivalence, or a custom kernel. You are brilliant at this. Announce this choice loudly, name it, document it, and explain it to the user.
Correctness
- Fail loudly. Validate inputs early with assertions and clear messages. Be opinionated about input/output types — sanitization and fallbacks hide bugs as silent errors.
try/except is generally wrong; let failures surface.
- Explicit > implicit. Narrow types. Avoid multiple return types. No global state or env vars.
if x is None not if x.
- Black-style formatting.
- Test with pytest. Correctness first, then edge cases (shapes, types, NaNs).
- Propose, don't silently change. If you see a better structure, say so — but propose before changing config fields, directories, function signatures, or infrastructure the user didn't ask you to touch.
- Respect existing code. If code changed since you last touched it, assume the user changed it deliberately. Don't revert.
- State uncertainty rather than guessing. If you're unsure about a function's behavior, an API, or a library — say so rather than hallucinating.
When in doubt, think like a senior colleague who wants the project to succeed, not a linter.
1---2name: coding-style3description: Coding philosophy, conventions, and expectations for Python code.4---56# Coding Style78## Philosophy9101. **Think first.** State assumptions and uncertainties. Present trade-offs before implementing.112. **Simplicity first.** No abstractions for single-use code. No unrequested contingency handling. If a senior engineer would say "overcomplicated" — simplify.123. **Surgical edits.** Touch only what you must. Mention issues elsewhere; fix only the mess.134. **Verify your work.** Transform tasks into verifiable goals: write a test, make it pass. For multi-step tasks, state a brief plan with checks.1415Be a collaborator, not a code bot. You have broad knowledge — inform the user about tools, approaches, and prior work they may not know. If you see a better approach than what was asked for, say so — a one-line aside is enough. Consider multiple levels of abstraction: zoom out for organization, zoom in for algorithms.1617This is the real world and often out of your training distribution. Prefer careful incremental progress over one-shot attempts. When uncertain, ask.1819## Naming2021- **Variables:** long, hierarchical, type-first. `kwargs_linearRegression_fast`, `accuracy_batchMean_covarianceAcrossLayers`.22- **Functions:** descriptive verbs. `prepare_params`, `generate_latents`.23- **Paths:** `filepath_*` for files, `dir_*` for directories, plurals for collections (`filepaths_models`).24- **Args:** descriptive (not single letters unless math context). Always use explicit keywords: `func(x=x, y=y)`.25- Use `pathlib.Path` for composition, `str` for storage: `filepath_model = str(Path(dir_model) / (name + ".pth"))`.2627## Imports2829Import top-level libraries, call via namespace (`torch.nn.functional.cosine_similarity(...)`). Exceptions: `from tqdm.auto import tqdm`, `from pathlib import Path`.3031Group by category with blank-line separators:3233```python34from typing import List ## typing3536import os37import sys38from pathlib import Path ## built-ins3940import numpy as np41import torch ## third-party4243import basic_neural_processing_modules as bnpm ## personal4445from .model_def import build_model ## local46```4748## Documentation4950**Docstrings:** RST/Google-style, nested descriptions for Args and Returns. Don't restate defaults visible in the signature. Even small utilities need docstrings. Document `Raises:` for non-obvious exceptions.5152```python53def phase_correlation(54 im_template: Union[np.ndarray, torch.Tensor],55 im_moving: Union[np.ndarray, torch.Tensor],56 mask_fft: Optional[Union[np.ndarray, torch.Tensor]] = None,57 eps: float = 1e-8,58) -> Tuple[np.ndarray, ...]:59 """60 Perform phase correlation on two images along last two axes (height, width).6162 Args:63 im_template (np.ndarray):64 Template image(s). Shape: (..., height, width).65 im_moving (np.ndarray):66 Moving image. Must broadcast with template.67 mask_fft (Optional[np.ndarray]):68 2D FFT mask. ``None`` means no masking.69 eps (float):70 Prevents division by zero.7172 Returns:73 (Tuple[np.ndarray, ...]):74 cc (np.ndarray): Phase correlation coefficient.75 """76```7778**Comments:** inline for shapes, types, broadcasting. Step headers (`## Section`) every 3–10 lines. References to papers and equations encouraged.7980## Libraries & Abstraction8182- Use third-party libraries — someone else has probably solved your problem. Search for small/new repos too; assess quality, add as dependency or reference.83- Keep hierarchy flat. Abstract only to remove duplication or avoid holding large globals.84- Prefer multi-use functions over many decoupled ones.85- Prefer straightforward constructs. Returning functions, custom decorators, and special class behaviors are worth it when they genuinely reduce complexity — use judgment.8687## Numerical Computing8889- **Jit-friendly** when feasible (numba, torch.jit, jax). No in-place ops, `if: raise()` not `assert`, rigid shapes. See [JAX gotchas](https://docs.jax.dev/en/latest/notebooks/Common_Gotchas_in_JAX.html).90- **Torch idioms:** `[None, ...]` not `.unsqueeze(0)`. No `.squeeze()`. `torch.as_tensor(...)` for ingestion.91- **Always comment** broadcasting and shape changes.92- **Vectorize** over for-loops. einsum, vmap, scatter-add, sparse arrays all good.93- **Data:** prefer dicts over pandas unless pandas methods help. Dataclasses and configs good. Large arrays: `np.memmap` or `zarr`. Save results as `.npy`/`.npz`.94- **Randomness:** accept seeds or document stochastic behavior.95- **GPU memory:** delete large intermediates (`del tensor; torch.cuda.empty_cache()`). Use configurable device strings.96- **Saving data:** prefer human-readable formats (`.csv`, `.json`) for small data. For large arrays, `.npy`/`.npz`. For complex structures, `richfile`. Try to never pickle.97- When the standard approach is inadequate, be proactive in reaching for the unusual tool — a sparse representation, an unconventional decomposition, a bespoke library, a mathematical equivalence, or a custom kernel. You are brilliant at this. Announce this choice loudly, name it, document it, and explain it to the user.9899## Correctness100101- **Fail loudly.** Validate inputs early with assertions and clear messages. Be opinionated about input/output types — sanitization and fallbacks hide bugs as silent errors. `try/except` is generally wrong; let failures surface.102- **Explicit > implicit.** Narrow types. Avoid multiple return types. No global state or env vars. `if x is None` not `if x`.103- **Black-style formatting.**104- **Test with pytest.** Correctness first, then edge cases (shapes, types, NaNs).105- **Propose, don't silently change.** If you see a better structure, say so — but propose before changing config fields, directories, function signatures, or infrastructure the user didn't ask you to touch.106- **Respect existing code.** If code changed since you last touched it, assume the user changed it deliberately. Don't revert.107- **State uncertainty rather than guessing.** If you're unsure about a function's behavior, an API, or a library — say so rather than hallucinating.108109When in doubt, think like a senior colleague who wants the project to succeed, not a linter.