Skip to content

Commit

Permalink
Use ruff for linting and autoformat code (#63)
Browse files Browse the repository at this point in the history
* Use ruff and update pre-commit config
* Lint/autoformat code
* Remove flake8 test
  • Loading branch information
stefsmeets authored Apr 14, 2023
1 parent 21a5d10 commit 2604172
Show file tree
Hide file tree
Showing 89 changed files with 365 additions and 332 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
python -m pip install --upgrade pip
pip install setuptools wheel twine
python setup.py sdist bdist_wheel
- name: Publish package
uses: pypa/gh-action-pypi-publish@master
with:
Expand Down
7 changes: 0 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ jobs:
run: |
python -m pip install -e .[develop]
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics --ignore E501
- name: Test with pytest
run: |
pytest tests
31 changes: 5 additions & 26 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,19 @@ repos:
# - id: check-json
# - id: check-added-large-files
- repo: https://github.com/myint/docformatter
rev: v1.5.1
rev: v1.6.0
hooks:
- id: docformatter
- repo: https://github.com/asottile/reorder_python_imports
rev: v3.9.0
hooks:
- id: reorder-python-imports
args: [--py3-plus]
- repo: https://github.com/pre-commit/mirrors-autopep8
rev: v2.0.2
hooks:
- id: autopep8
args: ['--in-place', '--ignore=E501,W504']
- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
hooks:
- id: pyupgrade
args: ['--py36-plus']
- repo: https://github.com/PyCQA/flake8
rev: '6.0.0'
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: 'v0.0.261'
hooks:
- id: flake8
args: ['--statistics', '--ignore=F401,F403,F405,F821,F841,E265,E501,A003']
additional_dependencies: [
'flake8-blind-except',
'flake8-commas',
'flake8-comprehensions',
'flake8-deprecated',
'flake8-mutable',
'flake8-quotes',
'flake8-tidy-imports',
# 'flake8-type-annotations',
'flake8-builtins',
]
- id: ruff
args: [--fix]
- repo: https://github.com/kynan/nbstripout
rev: 0.6.1
hooks:
Expand Down
2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ include instamatic/config/calibration/*.yaml
include instamatic/config/camera/*.yaml
include instamatic/config/microscope/*.yaml
include instamatic/config/scripts/*.md
include instamatic.neural_network/*.p
include instamatic.neural_network/*.p
14 changes: 5 additions & 9 deletions instamatic/TEMController/TEMController.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

import numpy as np

from .deflectors import *
from .lenses import *
from .microscope import Microscope
from .stage import *
from .states import *
from instamatic import config
from instamatic.camera import Camera
from instamatic.exceptions import TEMControllerError
from instamatic.formats import write_tiff
from instamatic.image_utils import rotate_image

from .deflectors import *
from .lenses import *
from .microscope import Microscope
from .stage import *
from .states import *

_ctrl = None # store reference of ctrl so it can be accessed without re-initializing

Expand Down Expand Up @@ -44,7 +44,6 @@ def initialize(tem_name: str = default_tem, cam_name: str = default_cam, stream:
ctrl : `TEMController`
Return TEM control object
"""

print(f"Microscope: {tem_name}{' (server)' if use_tem_server else ''}")
tem = Microscope(tem_name, use_server=use_tem_server)

Expand Down Expand Up @@ -267,7 +266,6 @@ def get_stagematrix(self, binning: int = None, mag: int = None, mode: int = None
stagematrix : np.array[2, 2]
Affine transformation matrix to convert from stage to pixel coordinates
"""

if not mode:
mode = self.mode.get()
if not mag:
Expand Down Expand Up @@ -443,7 +441,6 @@ def to_dict(self, *keys) -> dict:
self.to_dict('all') or self.to_dict() will return all properties
"""

# Each of these costs about 40-60 ms per call on a JEOL 2100, stage is 265 ms per call
funcs = {
'FunctionMode': self.tem.getFunctionMode,
Expand Down Expand Up @@ -477,7 +474,6 @@ def to_dict(self, *keys) -> dict:

def from_dict(self, dct: dict):
"""Restore microscope parameters from dict."""

funcs = {
# 'FunctionMode': self.tem.setFunctionMode,
'GunShift': self.gunshift.set,
Expand Down
3 changes: 1 addition & 2 deletions instamatic/TEMController/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
from .microscope import Microscope
from .TEMController import get_instance
from .TEMController import initialize
from .TEMController import get_instance, initialize
1 change: 0 additions & 1 deletion instamatic/TEMController/deflectors.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from collections import namedtuple
from typing import Tuple


DeflectorTuple = namedtuple('DeflectorTuple', ['x', 'y'])


Expand Down
4 changes: 2 additions & 2 deletions instamatic/TEMController/fei_microscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from numpy import pi

from instamatic import config
from instamatic.exceptions import FEIValueError
from instamatic.exceptions import TEMCommunicationError
from instamatic.exceptions import FEIValueError, TEMCommunicationError

logger = logging.getLogger(__name__)


Expand Down
4 changes: 2 additions & 2 deletions instamatic/TEMController/fei_simu_microscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import comtypes.client

from instamatic import config
from instamatic.exceptions import FEIValueError
from instamatic.exceptions import TEMCommunicationError
from instamatic.exceptions import FEIValueError, TEMCommunicationError

logger = logging.getLogger(__name__)


Expand Down
4 changes: 2 additions & 2 deletions instamatic/TEMController/jeol_microscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import comtypes.client

from instamatic import config
from instamatic.exceptions import JEOLValueError
from instamatic.exceptions import TEMCommunicationError
from instamatic.exceptions import JEOLValueError, TEMCommunicationError

logger = logging.getLogger(__name__)

NTRLMAPPING = {
Expand Down
2 changes: 0 additions & 2 deletions instamatic/TEMController/microscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

def get_tem(interface: str):
"""Grab tem class with the specific 'interface'."""

simulate = config.settings.simulate

if config.settings.tem_require_admin:
Expand Down Expand Up @@ -39,7 +38,6 @@ def Microscope(name: str = None, use_server: bool = False):
returns: TEM interface class
"""

if name is None:
interface = default_tem_interface
name = interface
Expand Down
9 changes: 2 additions & 7 deletions instamatic/TEMController/microscope_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@
from functools import wraps

from instamatic import config
from instamatic.exceptions import exception_list
from instamatic.exceptions import TEMCommunicationError
from instamatic.server.serializer import dumper
from instamatic.server.serializer import loader

from instamatic.exceptions import TEMCommunicationError, exception_list
from instamatic.server.serializer import dumper, loader

HOST = config.settings.tem_server_host
PORT = config.settings.tem_server_port
Expand Down Expand Up @@ -78,7 +75,6 @@ def connect(self):
print(f'Connected to TEM server ({HOST}:{PORT})')

def __getattr__(self, func_name):

try:
wrapped = self._dct[func_name]
except KeyError as e:
Expand All @@ -95,7 +91,6 @@ def wrapper(*args, **kwargs):

def _eval_dct(self, dct):
"""Takes approximately 0.2-0.3 ms per call if HOST=='localhost'."""

self.s.send(dumper(dct))

response = self.s.recv(self._bufsize)
Expand Down
1 change: 0 additions & 1 deletion instamatic/TEMController/simu_microscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from instamatic import config
from instamatic.exceptions import TEMValueError


NTRLMAPPING = {
'GUN1': 0,
'GUN2': 1,
Expand Down
7 changes: 3 additions & 4 deletions instamatic/TEMController/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import numpy as np


# namedtuples to store results from .get()
StagePositionTuple = namedtuple('StagePositionTuple', ['x', 'y', 'z', 'a', 'b'])

Expand Down Expand Up @@ -110,8 +109,9 @@ def xy(self, values: Tuple[int, int]):
self.set(x=x, y=y, wait=self._wait)

def move_in_projection(self, delta_x: int, delta_y: int) -> None:
r"""y and z are always perpendicular to the sample stage. To achieve the movement
in the projection, x and yshould be broken down into the components z' and y'.
r"""y and z are always perpendicular to the sample stage. To achieve the
movement in the projection, x and yshould be broken down into the
components z' and y'.
y = y' * cos(a)
z = y' * sin(a)
Expand Down Expand Up @@ -262,7 +262,6 @@ def move_xy_with_backlash_correction(self, shift_x: int = None, shift_y: int = N
wait: bool,
block until stage movement is complete (JEOL only)
"""

stage = self.get()

if shift_x:
Expand Down
4 changes: 2 additions & 2 deletions instamatic/acquire_at_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def __init__(self, ctrl,

if post_acquire:
self._post_acquire = self.validate(post_acquire)
print(f'Post-acquire:', ', '.join([func.__name__ for func in self._post_acquire]))
print('Post-acquire:', ', '.join([func.__name__ for func in self._post_acquire]))

self.backlash = backlash

Expand Down Expand Up @@ -139,8 +139,8 @@ def start(self, start_index: int = 0):
start_index : int
Start acquisition from this item.
"""
import time
import msvcrt
import time

ctrl = self.ctrl
nav_items = self.nav_items[start_index:]
Expand Down
2 changes: 1 addition & 1 deletion instamatic/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def onclick(self, event, double_click_delay=0.5):
self.update_ax3(ind)

if ind == self.current_ind and (t1 - self.t0 < double_click_delay):
print(f'Moving stage to:', self.coord)
print('Moving stage to:', self.coord)
self.ctrl.stage.xy = self.coord

self.t0 = time.perf_counter()
Expand Down
15 changes: 7 additions & 8 deletions instamatic/calibrate/calibrate_beamshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import numpy as np
from skimage.registration import phase_cross_correlation

from .filenames import *
from .fit import fit_affine_transformation
from instamatic import config
from instamatic.image_utils import autoscale
from instamatic.image_utils import imgscale
from instamatic.image_utils import autoscale, imgscale
from instamatic.processing.find_holes import find_holes
from instamatic.tools import find_beam_center
from instamatic.tools import printer
from instamatic.tools import find_beam_center, printer

from .filenames import *
from .fit import fit_affine_transformation

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -133,7 +133,6 @@ def calibrate_beamshift_live(ctrl, gridsize=None, stepsize=None, save_images=Fal
return:
instance of Calibration class with conversion methods
"""

exposure = kwargs.get('exposure', ctrl.cam.default_exposure)
binsize = kwargs.get('binsize', ctrl.cam.default_binsize)

Expand Down Expand Up @@ -170,7 +169,7 @@ def calibrate_beamshift_live(ctrl, gridsize=None, stepsize=None, save_images=Fal
for dx, dy in np.stack([x_grid, y_grid]).reshape(2, -1).T:
ctrl.beamshift.set(x=x_cent + dx, y=y_cent + dy)

printer('Position: {}/{}: {}'.format(i + 1, tot, ctrl.beamshift))
printer(f'Position: {i + 1}/{tot}: {ctrl.beamshift}')

outfile = os.path.join(outdir, 'calib_beamshift_{i:04d}') if save_images else None

Expand Down
6 changes: 3 additions & 3 deletions instamatic/calibrate/calibrate_brightness.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import matplotlib.pyplot as plt
import numpy as np

from .filenames import *
from instamatic.image_utils import autoscale
from instamatic.processing.find_holes import find_holes
from instamatic.tools import find_beam_center

from .filenames import *

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -88,7 +90,6 @@ def calibrate_brightness_live(ctrl, step=1000, save_images=False, **kwargs):
return:
instance of CalibBrightness class with conversion methods
"""

raise NotImplementedError('calibrate_brightness_live function needs fixing...')

exposure = kwargs.get('exposure', ctrl.cam.default_exposure)
Expand Down Expand Up @@ -140,7 +141,6 @@ def calibrate_brightness_from_image_fn(fns):
return:
instance of Calibration class with conversion methods
"""

values = []

for fn in fns:
Expand Down
10 changes: 5 additions & 5 deletions instamatic/calibrate/calibrate_directbeam.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import numpy as np
from skimage.registration import phase_cross_correlation

from .filenames import *
from .fit import fit_affine_transformation
from instamatic import config
from instamatic.image_utils import autoscale
from instamatic.image_utils import imgscale
from instamatic.image_utils import autoscale, imgscale
from instamatic.tools import printer

from .filenames import *
from .fit import fit_affine_transformation

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -195,7 +196,6 @@ def calibrate_directbeam_live(ctrl, key='DiffShift', gridsize=None, stepsize=Non
return:
instance of Calibration class with conversion methods
"""

if ctrl.mode != 'diff':
print(' >> Switching to diffraction mode')
ctrl.mode.set('diff')
Expand Down
2 changes: 1 addition & 1 deletion instamatic/calibrate/calibrate_imageshift12.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from tqdm.auto import tqdm

from instamatic.calibrate.fit import fit_affine_transformation

logger = logging.getLogger(__name__)


def Calibrate_Imageshift(ctrl, diff_defocus, stepsize, logger, key='IS1'):

if key != 'S':
input(f"""Calibrate {key}
-------------------
Expand Down
Loading

0 comments on commit 2604172

Please sign in to comment.