Skip to content

Commit

Permalink
MAINT: Test using ruff with minimal changes to defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
MridulS committed Apr 16, 2024
1 parent f041c9b commit e1e46f0
Show file tree
Hide file tree
Showing 31 changed files with 128 additions and 151 deletions.
29 changes: 8 additions & 21 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,13 @@ repos:
- id: trailing-whitespace
args: [ --markdown-linebreak-ext=md ]
exclude: '\.svg'
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
name: isort (python)
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.11.0
hooks:
- id: black
- repo: https://github.com/kynan/nbstripout
rev: 0.6.0
hooks:
- id: nbstripout
types: [ "jupyter" ]
args: [ "--drop-empty-cells",
"--extra-keys 'metadata.language_info.version cell.metadata.jp-MarkdownHeadingCollapsed cell.metadata.pycharm'" ]
- repo: https://github.com/pycqa/flake8
rev: 6.1.0
hooks:
- id: flake8
types: ["python"]
additional_dependencies: ["flake8-bugbear==23.9.16"]
- repo: https://github.com/pycqa/bandit
rev: 1.7.5
hooks:
- id: bandit
additional_dependencies: ["bandit[toml]"]
args: ["-c", "pyproject.toml"]
- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
hooks:
Expand All @@ -57,3 +36,11 @@ repos:
- id: rst-directive-colons
- id: rst-inline-touching-normal
- id: text-unicode-replacement-char
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.7
hooks:
- id: ruff
types_or: [ python, pyi, jupyter ]
args: [ --fix]
- id: ruff-format
types_or: [ python, pyi]
8 changes: 3 additions & 5 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

import doctest
import os
import sys
Expand All @@ -8,9 +6,9 @@
sys.path.insert(0, os.path.abspath('.'))

# General information about the project.
project = u'ESSsans'
copyright = u'2024 Scipp contributors'
author = u'Scipp contributors'
project = 'ESSsans'
copyright = '2024 Scipp contributors'
author = 'Scipp contributors'

html_show_sourcelink = True

Expand Down
24 changes: 12 additions & 12 deletions docs/user-guide/common/beam-center-finder.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
},
"outputs": [],
"source": [
"import scipp as sc\n",
"import plopp as pp\n",
"import sciline\n",
"from ess import sans\n",
"import scipp as sc\n",
"from ess import isissans as isis\n",
"from ess import sans\n",
"from ess.sans.types import *"
]
},
Expand Down Expand Up @@ -377,7 +377,7 @@
"p.ax.axhline(0, color='cyan')\n",
"p.ax.plot(0, 0, '+', color='k', ms=10)\n",
"dx = 0.25\n",
"style = dict(ha='center', va='center', color='w')\n",
"style = {'ha': 'center', 'va': 'center', 'color': 'w'}\n",
"p.ax.text(dx, dx, 'North-East', **style)\n",
"p.ax.text(-dx, dx, 'North-West', **style)\n",
"p.ax.text(dx, -dx, 'South-East', **style)\n",
Expand All @@ -402,15 +402,15 @@
"metadata": {},
"outputs": [],
"source": [
"kwargs = dict(\n",
" data=masked,\n",
" norm=pipeline.compute(NormWavelengthTerm[SampleRun]),\n",
" graph=pipeline.compute(sans.conversions.ElasticCoordTransformGraph),\n",
" q_bins=params[sans.beam_center_finder.BeamCenterFinderQBins],\n",
" wavelength_bins=params[WavelengthBins],\n",
" transform=pipeline.compute(LabFrameTransform[SampleRun]),\n",
" pixel_shape=pipeline.compute(DetectorPixelShape[SampleRun]),\n",
")"
"kwargs = {\n",
" 'data': masked,\n",
" 'norm': pipeline.compute(NormWavelengthTerm[SampleRun]),\n",
" 'graph': pipeline.compute(sans.conversions.ElasticCoordTransformGraph),\n",
" 'q_bins': params[sans.beam_center_finder.BeamCenterFinderQBins],\n",
" 'wavelength_bins': params[WavelengthBins],\n",
" 'transform': pipeline.compute(LabFrameTransform[SampleRun]),\n",
" 'pixel_shape': pipeline.compute(DetectorPixelShape[SampleRun]),\n",
"}"
]
},
{
Expand Down
17 changes: 8 additions & 9 deletions docs/user-guide/isis/sans2d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
},
"outputs": [],
"source": [
"import scipp as sc\n",
"import sciline\n",
"import plopp as pp\n",
"from ess import sans\n",
"import sciline\n",
"import scipp as sc\n",
"from ess import isissans as isis\n",
"from ess import sans\n",
"from ess.sans.types import *"
]
},
Expand Down Expand Up @@ -102,11 +102,10 @@
"providers = (\n",
" sans.providers + isis.providers + isis.data.providers + isis.sans2d.providers\n",
")\n",
"providers = providers + (\n",
" isis.data.transmission_from_background_run,\n",
" isis.data.transmission_from_sample_run,\n",
" sans.beam_center_from_center_of_mass,\n",
")\n",
"providers = (*providers,\n",
" isis.data.transmission_from_background_run,\n",
" isis.data.transmission_from_sample_run,\n",
" sans.beam_center_from_center_of_mass)\n",
"\n",
"pipeline = sciline.Pipeline(providers=providers, params=params)"
]
Expand Down Expand Up @@ -248,7 +247,7 @@
")\n",
"parts = (CleanSummedQ[SampleRun, Numerator], CleanSummedQ[SampleRun, Denominator])\n",
"iofqs = (IofQ[SampleRun], IofQ[BackgroundRun], BackgroundSubtractedIofQ)\n",
"keys = monitors + (MaskedData[SampleRun],) + parts + iofqs\n",
"keys = (*monitors, MaskedData[SampleRun], *parts, *iofqs)\n",
"\n",
"results = pipeline.compute(keys)\n",
"\n",
Expand Down
18 changes: 8 additions & 10 deletions docs/user-guide/isis/zoom.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
"metadata": {},
"outputs": [],
"source": [
"import scipp as sc\n",
"import sciline\n",
"from ess import sans\n",
"import scipp as sc\n",
"from ess import isissans as isis\n",
"from ess import sans\n",
"from ess.sans.types import *"
]
},
Expand Down Expand Up @@ -131,11 +131,10 @@
"outputs": [],
"source": [
"providers = sans.providers + isis.providers + (isis.io.read_xml_detector_masking,)\n",
"providers = providers + (\n",
" isis.data.transmission_from_background_run,\n",
" isis.data.transmission_from_sample_run,\n",
" sans.beam_center_from_center_of_mass,\n",
")\n",
"providers = (*providers,\n",
" isis.data.transmission_from_background_run,\n",
" isis.data.transmission_from_sample_run,\n",
" sans.beam_center_from_center_of_mass)\n",
"pipeline = sciline.Pipeline(providers, params=params)\n",
"pipeline.set_param_series(PixelMaskFilename, masks)"
]
Expand All @@ -159,8 +158,8 @@
"outputs": [],
"source": [
"try:\n",
" import ess.isissans.mantidio # noqa: F401\n",
" from mantid import ConfigService\n",
" import ess.isissans.mantidio\n",
"\n",
" cfg = ConfigService.Instance()\n",
" cfg.setLogLevel(3) # Silence verbose load via Mantid\n",
Expand All @@ -169,7 +168,6 @@
" for provider in isis.mantidio.providers:\n",
" pipeline.insert(provider)\n",
"except ImportError:\n",
" import ess.isissans.io\n",
"\n",
" for provider in isis.data.providers:\n",
" pipeline.insert(provider)"
Expand Down Expand Up @@ -236,7 +234,7 @@
")\n",
"parts = (CleanSummedQ[SampleRun, Numerator], CleanSummedQ[SampleRun, Denominator])\n",
"iofqs = (IofQ[SampleRun],)\n",
"keys = monitors + (MaskedData[SampleRun],) + parts + iofqs\n",
"keys = (*monitors, MaskedData[SampleRun], *parts, *iofqs)\n",
"\n",
"results = pipeline.compute(keys)\n",
"\n",
Expand Down
11 changes: 4 additions & 7 deletions docs/user-guide/loki/loki-direct-beam.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,11 @@
},
"outputs": [],
"source": [
"import numpy as np\n",
"import scipp as sc\n",
"import sciline\n",
"import scippneutron as scn\n",
"import plopp as pp\n",
"from ess import sans\n",
"from ess import loki\n",
"import sciline\n",
"import scipp as sc\n",
"from ess import isissans as isis\n",
"from ess import loki, sans\n",
"from ess.sans.types import *"
]
},
Expand Down Expand Up @@ -252,8 +249,8 @@
"metadata": {},
"outputs": [],
"source": [
"from scipp.scipy.interpolate import interp1d\n",
"from ess.loki.data import get_path\n",
"from scipp.scipy.interpolate import interp1d\n",
"\n",
"Iq_theory = sc.io.load_hdf5(get_path('PolyGauss_I0-50_Rg-60.h5'))\n",
"f = interp1d(Iq_theory, 'Q')\n",
Expand Down
68 changes: 37 additions & 31 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[build-system]
build-backend = "setuptools.build_meta"
requires = [
"setuptools>=68",
"setuptools_scm[toml]>=8.0",
"setuptools_scm[toml]>=8",
]
build-backend = "setuptools.build_meta"

[project]
name = "esssans"
Expand All @@ -12,17 +12,14 @@ authors = [{ name = "Scipp contributors" }]
license = { file = "LICENSE" }
readme = "README.md"
classifiers = [
"Intended Audience :: Science/Research",
"License :: OSI Approved :: BSD License",
"Natural Language :: English",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: Scientific/Engineering",
"Typing :: Typed",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: BSD License",
"Natural Language :: English",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.10",
"Topic :: Scientific/Engineering",
"Typing :: Typed",
]
requires-python = ">=3.10"

Expand All @@ -31,25 +28,46 @@ requires-python = ">=3.10"
# Make sure to list one dependency per line.
dependencies = [
"dask",
"graphviz",
"essreduce",
"graphviz",
"plopp",
"pythreejs",
"sciline>=24.4.1",
"scipp>=23.8.0",
"scippneutron>=23.9.0",
"scippnexus>=23.12.1", # 23.12.0 and below deadlock in threaded use
"scipp>=23.8",
"scippneutron>=23.9",
"scippnexus>=23.12.1", # 23.12.0 and below deadlock in threaded use
]

dynamic = ["version"]
dynamic = [
"version",
]

[project.urls]
"Bug Tracker" = "https://github.com/scipp/esssans/issues"
"Documentation" = "https://scipp.github.io/esssans"
"Source" = "https://github.com/scipp/esssans"

[tool.setuptools_scm]

[tool.ruff.lint]
select = ["B", "C4", "DTZ", "E", "F", "FA",
"G", "I", "S", "T20", "PERF", "PGH",
"PT", "PYI", "RUF", "UP", "W"]
ignore = [
"E111", "E114", "E117", "D206", "D300", # conflict with ruff format
"UP006", "UP035", "UP007"] # FIXME: this breaks the tests
pydocstyle.convention = "numpy"

[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["I"]
# Assert in tests are fine
"tests/*" = ["S101"]
# Blocked by https://github.com/scipp/esssans/issues/95
"*.ipynb" = ["F403", "F405"]
"docs/*" = ["T201"] # printing is ok for demonstration purposes

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

[tool.pytest.ini_options]
minversion = "7.0"
addopts = """
Expand All @@ -65,18 +83,6 @@ filterwarnings = [
'ignore:\n Sentinel is not a public part of the traitlets API:DeprecationWarning',
]

[tool.bandit]
# Excluding tests because bandit doesn't like `assert`.
exclude_dirs = ["docs/conf.py", "tests"]

[tool.black]
skip-string-normalization = true

[tool.isort]
skip_gitignore = true
profile = "black"
known_first_party = ["esssans"]

[tool.mypy]
strict = true
ignore_missing_imports = true
Expand Down
5 changes: 1 addition & 4 deletions requirements/make_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ def write_dependencies(dependency_name: str, dependencies: List[str]) -> None:
path = Path(f"{dependency_name}.in")
if path.exists():
sections = path.read_text().split(CUSTOM_AUTO_SEPARATOR)
if len(sections) > 1:
custom = sections[0]
else:
custom = ""
custom = sections[0] if len(sections) > 1 else ""
else:
custom = ""
with path.open("w") as f:
Expand Down
3 changes: 2 additions & 1 deletion src/ess/isissans/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
Providers for the ISIS instruments.
"""

import scipp as sc

from ..sans.types import (
Expand Down Expand Up @@ -48,7 +49,7 @@ def data_to_tof(


def monitor_to_tof(
da: ConfiguredReducibleMonitor[RunType, MonitorType]
da: ConfiguredReducibleMonitor[RunType, MonitorType],
) -> TofMonitor[RunType, MonitorType]:
"""Dummy conversion of monitor data to time-of-flight data.
The monitor data already has a time-of-flight coordinate."""
Expand Down
5 changes: 3 additions & 2 deletions src/ess/isissans/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
File loading functions for ISIS data, NOT using Mantid.
"""

from typing import NewType

import scipp as sc
Expand Down Expand Up @@ -43,9 +44,9 @@ def read_xml_detector_masking(
filename:
Path to the XML file.
"""
import xml.etree.ElementTree as ET # nosec
import xml.etree.ElementTree as ET

tree = ET.parse(filename) # nosec
tree = ET.parse(filename) # noqa: S314
root = tree.getroot()

masked_detids = []
Expand Down
Loading

0 comments on commit e1e46f0

Please sign in to comment.