diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 08c32de7..d1468988 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,7 +2,7 @@ # See https://pre-commit.com/hooks.html for more hooks repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.4.0 + rev: v4.6.0 hooks: - id: trailing-whitespace args: [--markdown-linebreak-ext=md] @@ -21,16 +21,16 @@ repos: hooks: - id: nbcheckorder - repo: https://github.com/myint/docformatter - rev: v1.7.2 + rev: v1.7.5 hooks: - id: docformatter - repo: https://github.com/pre-commit/mirrors-autopep8 - rev: v2.0.2 + rev: v2.0.4 hooks: - id: autopep8 args: ['--in-place', '--ignore=E501,W504'] - repo: https://github.com/charliermarsh/ruff-pre-commit - rev: 'v0.0.272' + rev: 'v0.4.9' hooks: - id: ruff args: [--fix] diff --git a/pyproject.toml b/pyproject.toml index 5d1d307e..ebf0ba43 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -121,52 +121,22 @@ publishing = [ "instamatic.autoconfig" = "instamatic.config.autoconfig:main" [tool.ruff] +target-version = 'py37' +line-length = 96 + +[tool.ruff.lint] # Enable Pyflakes `E` and `F` codes by default. select = [ 'F', # Pyflakes 'E', # pycodestyle (error) 'W', # pycodestyle (warning) - # 'C90', # mccabe 'I', # isort - # 'N', # pep8-naming - # 'D', # pydocstyle 'UP', # pyupgrade - # 'YTT', # flake8-2020 - # 'ANN', # flake8-annotations - # 'S', # flake8-bandit - 'BLE', # flake8-blind-except - # 'FBT', # flake8-boolean-trap - # 'B', # flake8-bugbear 'A', # flake8-builtins 'COM', # flake8-commas 'C4', # flake8-comprehensions - # 'DTZ', # flake8-datetimez - # 'T10', # flake8-debugger - # 'EM', # flake8-errmsg - # 'EXE', # flake8-executable - # 'ISC', # flake8-implicit-str-concat - # 'ICN', # flake8-import-conventions - # 'G', # flake8-logging-format - # 'INP', # flake8-no-pep420 - # 'PIE', # flake8-pie - # 'T20', # flake8-print - # 'PT', # flake8-pytest-style - # 'Q', # flake8-quotes - # 'RET', # flake8-return - # 'SIM', # flake8-simplify 'TID', # flake8-tidy-imports - # 'TCH', # flake8-type-checking - # 'ARG', # flake8-unused-arguments - # 'PTH', # flake8-use-pathlib - # 'ERA', # eradicate - # 'PD', # pandas-vet - # 'PGH', # pygrep-hooks - # 'PL', # Pylint - # 'PLC', # Convention - # 'PLE', # Error - # 'PLR', # Refactor - # 'PLW', # Warning - # 'TRY', # tryceratops + 'NPY201', # numpy deprecation ] ignore = [ 'F401', @@ -211,13 +181,5 @@ per-file-ignores = {} # Allow unused variables when underscore-prefixed. dummy-variable-rgx = '^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$' -target-version = 'py37' -line-length = 96 - - -[tool.ruff.isort] +[tool.ruff.lint.isort] known-first-party = ['instamatic'] - -[tool.isort] -profile = 'black' -# force-single-line = true diff --git a/scripts/center_images_smv.py b/scripts/center_images_smv.py index a26a77c1..d964535c 100644 --- a/scripts/center_images_smv.py +++ b/scripts/center_images_smv.py @@ -4,8 +4,7 @@ import numpy as np from scipy import ndimage as ndi -from instamatic.formats import read_adsc -from instamatic.formats import write_adsc +from instamatic.formats import read_adsc, write_adsc # Script to center the beam # diff --git a/scripts/diagnose_beam_drift.py b/scripts/diagnose_beam_drift.py index c1f4c374..0dce90c0 100644 --- a/scripts/diagnose_beam_drift.py +++ b/scripts/diagnose_beam_drift.py @@ -7,8 +7,7 @@ from tqdm.auto import tqdm from instamatic.formats import adscimage -from instamatic.tools import find_beam_center -from instamatic.tools import find_subranges +from instamatic.tools import find_beam_center, find_subranges def insert_nan(arr, interval=10): @@ -16,7 +15,7 @@ def insert_nan(arr, interval=10): new = [] for i, row in enumerate(arr): if not (i) % repeat: - new.append(np.array([np.NaN, np.NaN])) + new.append(np.array([np.nan, np.nan])) new.append(row) return np.array(new) @@ -64,7 +63,7 @@ def get_drifts_per_scan_range(xy): drifts.append(distance) normalized_xy.append(sub_xy - o) - normalized_xy.append([np.NaN, np.NaN]) + normalized_xy.append([np.nan, np.nan]) normalized_xy = np.vstack(normalized_xy) drifts = np.array(drifts) @@ -110,7 +109,7 @@ def get_drifts_per_scan_range(xy): xy = insert_nan(xy, interval=10) i = np.sum(xy, axis=1) == 0 - xy[i] = np.NaN + xy[i] = np.nan print() print(' mean std dev diff ') diff --git a/scripts/make_serialed_movie.py b/scripts/make_serialed_movie.py index fd35b5c7..15841235 100644 --- a/scripts/make_serialed_movie.py +++ b/scripts/make_serialed_movie.py @@ -21,7 +21,7 @@ def get_files(file_pat: str) -> list: df, d = read_ycsv(file_pat) fns = df.index.tolist() else: - f = open(file_pat, 'r') + f = open(file_pat) fns = [line.split('#')[0].strip() for line in f if not line.startswith('#')] else: fns = glob.glob(file_pat) diff --git a/scripts/process_dm.py b/scripts/process_dm.py index 09e2ef1a..39fb00a5 100644 --- a/scripts/process_dm.py +++ b/scripts/process_dm.py @@ -2,8 +2,8 @@ from pathlib import Path import numpy as np -from skimage.exposure import rescale_intensity from PIL import Image +from skimage.exposure import rescale_intensity from instamatic.processing.ImgConversionDM import ImgConversionDM as ImgConversion @@ -45,14 +45,14 @@ def img_convert(credlog, tiff_path='tiff2', mrc_path='RED', smv_path='SMV'): n = len(image_fns) if n == 0: - print(f'No files found matching `tiff/*.tif`') + print('No files found matching `tiff/*.tif`') exit() else: print(n) buffer = [] - with open(credlog, 'r') as f: + with open(credlog) as f: for line in f: if line.startswith('Data Collection Time'): timestamp = line.split(':', 1)[-1].strip() diff --git a/scripts/process_tpx.py b/scripts/process_tpx.py index eb8446b2..01f7d435 100644 --- a/scripts/process_tpx.py +++ b/scripts/process_tpx.py @@ -77,7 +77,7 @@ def reprocess(credlog, tiff_path=None, mrc_path=None, smv_path='SMV_reprocessed' n = len(image_fns) if n == 0: - print(f'No files found matching `tiff/*.tiff`') + print('No files found matching `tiff/*.tiff`') exit() else: print(n) @@ -89,7 +89,7 @@ def reprocess(credlog, tiff_path=None, mrc_path=None, smv_path='SMV_reprocessed' # osc_angle = 0.53 - with open(credlog, 'r') as f: + with open(credlog) as f: for line in f: if line.startswith('Camera length'): camera_length = float(line.split()[2]) diff --git a/scripts/process_tvips.py b/scripts/process_tvips.py index 6a825adc..e8d0f661 100644 --- a/scripts/process_tvips.py +++ b/scripts/process_tvips.py @@ -19,8 +19,7 @@ import tifffile from instamatic.processing.ImgConversionTVIPS import ImgConversionTVIPS as ImgConversion -from instamatic.tools import get_acquisition_time -from instamatic.tools import relativistic_wavelength +from instamatic.tools import get_acquisition_time, relativistic_wavelength def extract_image_number(s): @@ -59,7 +58,7 @@ def img_convert(credlog, tiff_path=None, pets_path='PETS', mrc_path='RED', smv_p acquisition_time = res.acquisition_time overhead = res.overhead - with open(credlog, 'r') as f: + with open(credlog) as f: for line in f: if line.startswith('Data Collection Time'): timestamp = line.split(':', 1)[-1].strip() diff --git a/src/instamatic/camera/merlin_io.py b/src/instamatic/camera/merlin_io.py index f049097d..0a4a3223 100644 --- a/src/instamatic/camera/merlin_io.py +++ b/src/instamatic/camera/merlin_io.py @@ -106,7 +106,7 @@ def load_mib(buffer: bytes, skip: int = 0): props = MIBProperties.from_buffer(buffer) merlin_frame_dtype = np.dtype([ - ('header', np.string_, props.headsize), + ('header', np.bytes_, props.headsize), ('data', props.pixeltype, props.merlin_size), ]) diff --git a/src/instamatic/config/utils.py b/src/instamatic/config/utils.py index 25b87d91..21d96f95 100644 --- a/src/instamatic/config/utils.py +++ b/src/instamatic/config/utils.py @@ -43,7 +43,6 @@ def array_representer(dumper, data): ]: representer.add_representer(np_type, representer.represent_int) for np_type in [ - np.float_, np.float16, np.float32, np.float64, diff --git a/src/instamatic/formats/mrc.py b/src/instamatic/formats/mrc.py index 61ef7ad6..cc41a81a 100644 --- a/src/instamatic/formats/mrc.py +++ b/src/instamatic/formats/mrc.py @@ -306,7 +306,7 @@ def is_readable(filename, no_strict_mrc=False): else: _logger.debug('Failed to read proper machine stamp - not MRC!') # return False - if not numpy.alltrue([h[v][0] > 0 for v in ('nx', 'ny', 'nz')]): + if not numpy.all([h[v][0] > 0 for v in ('nx', 'ny', 'nz')]): _logger.debug('Failed to read proper dimensions - not MRC!') return False return True @@ -549,7 +549,7 @@ def read_image(filename, index=None, cache=None, no_strict_mrc=False, force_volu out = out.byteswap() finally: util.close(filename, f) - # assert(numpy.alltrue(numpy.logical_not(numpy.isnan(out)))) + # assert(numpy.all(numpy.logical_not(numpy.isnan(out)))) # if header_image_dtype.newbyteorder()==h.dtype:out = out.byteswap() return out, header @@ -638,7 +638,7 @@ def write_image(filename, img, index=None, header=None, inplace=False): try: img = img.astype(mrc2numpy[numpy2mrc[img.dtype.type]]) except BaseException: - raise TypeError('Unsupported type for MRC writing: %s' % str(img.dtype)) + raise TypeError(f'Unsupported type for MRC writing: {img.dtype}') mode = 'rb+' if index is not None and (index > 0 or inplace and index > -1) else 'wb+' f = util.uopen(filename, mode) diff --git a/src/instamatic/formats/xdscbf.py b/src/instamatic/formats/xdscbf.py index 81e5eaa6..06eb2c36 100644 --- a/src/instamatic/formats/xdscbf.py +++ b/src/instamatic/formats/xdscbf.py @@ -95,13 +95,13 @@ def write(fname, data, header={}): b'Content-Type: application/octet-stream;', b' conversions="x-CBF_BYTE_OFFSET"', b'Content-Transfer-Encoding: BINARY', - np.string_('X-Binary-Size: %d' % (len(binary_blob))), + np.bytes_(f'X-Binary-Size: {len(binary_blob)}'), b'X-Binary-ID: 1', - np.string_('X-Binary-Element-Type: "%s"' % (dtype)), + np.bytes_(f'X-Binary-Element-Type: "{dtype}"'), b'X-Binary-Element-Byte-Order: LITTLE_ENDIAN', - np.string_('X-Binary-Number-of-Elements: %d' % (dim1 * dim2)), - np.string_('X-Binary-Size-Fastest-Dimension: %d' % dim1), - np.string_('X-Binary-Size-Second-Dimension: %d' % dim2), + np.bytes_(f'X-Binary-Number-of-Elements: {dim1 * dim2}'), + np.bytes_(f'X-Binary-Size-Fastest-Dimension: {dim1}'), + np.bytes_(f'X-Binary-Size-Second-Dimension: {dim2}'), b'X-Binary-Size-Padding: 1', b'', STARTER + binary_blob, diff --git a/src/instamatic/processing/ImgConversion.py b/src/instamatic/processing/ImgConversion.py index 8ca8ac71..6a96015a 100644 --- a/src/instamatic/processing/ImgConversion.py +++ b/src/instamatic/processing/ImgConversion.py @@ -602,7 +602,7 @@ def write_beam_centers(self, path: str) -> None: for i, h in self.headers.items(): centers[i - 1] = h['beam_center'] for i in self.missing_range: - centers[i - 1] = [np.NaN, np.NaN] + centers[i - 1] = [np.nan, np.nan] np.savetxt(path / 'beam_centers.txt', centers, fmt='%10.4f')