Skip to content

Commit

Permalink
Fix numpy 2.0 deprecation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
stefsmeets committed Jun 17, 2024
1 parent 01d354d commit 32a4a79
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 75 deletions.
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
50 changes: 6 additions & 44 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
3 changes: 1 addition & 2 deletions scripts/center_images_smv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
9 changes: 4 additions & 5 deletions scripts/diagnose_beam_drift.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
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):
repeat = interval - 1
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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 ')
Expand Down
2 changes: 1 addition & 1 deletion scripts/make_serialed_movie.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions scripts/process_dm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions scripts/process_tpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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])
Expand Down
5 changes: 2 additions & 3 deletions scripts/process_tvips.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/instamatic/camera/merlin_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
])

Expand Down
1 change: 0 additions & 1 deletion src/instamatic/config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/instamatic/formats/mrc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions src/instamatic/formats/xdscbf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/instamatic/processing/ImgConversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down

0 comments on commit 32a4a79

Please sign in to comment.