From 84885bd6b3aa4cd5d010e5090d9ab5bbcd53a627 Mon Sep 17 00:00:00 2001 From: Noemi Frisina Date: Wed, 6 Dec 2023 11:36:28 +0000 Subject: [PATCH] Run ruff --- docs/conf.py | 2 -- src/nexgen/beamlines/ED_singla_nxs.py | 12 ++++--- .../beamlines/GDAtools/GDAjson2params.py | 4 +-- src/nexgen/beamlines/I19_2_gda_nxs.py | 19 ++++++++--- src/nexgen/beamlines/I19_2_nxs.py | 11 +++++-- src/nexgen/beamlines/SSX_Eiger_nxs.py | 3 +- src/nexgen/beamlines/SSX_Tristan_nxs.py | 3 +- src/nexgen/beamlines/SSX_chip.py | 3 +- src/nexgen/beamlines/SSX_expt.py | 8 +++-- src/nexgen/beamlines/beamline_utils.py | 6 ++-- src/nexgen/command_line/ED_nexus.py | 2 +- src/nexgen/command_line/I19_2_cli.py | 2 +- src/nexgen/command_line/SSX_cli.py | 2 +- src/nexgen/command_line/cli_utils.py | 29 +++++++++------- src/nexgen/command_line/copy_nexus.py | 3 +- src/nexgen/command_line/nexus_generator.py | 28 +++++++++++----- src/nexgen/command_line/phil_files_cli.py | 2 +- src/nexgen/nxs_copy/CopyTristanNexus.py | 10 ++++-- src/nexgen/nxs_copy/copy_utils.py | 2 +- src/nexgen/nxs_utils/Axes.py | 3 +- src/nexgen/nxs_utils/Detector.py | 2 +- src/nexgen/nxs_utils/Goniometer.py | 9 +++-- src/nexgen/nxs_utils/Sample.py | 1 + src/nexgen/nxs_utils/Source.py | 2 +- src/nexgen/nxs_write/NXclassWriters.py | 30 ++++++++++------- src/nexgen/nxs_write/NXmxWriter.py | 33 ++++++++++++++----- src/nexgen/tools/DataWriter.py | 13 ++++++-- src/nexgen/tools/MetaReader.py | 6 ++-- src/nexgen/tools/VDS_tools.py | 11 +++---- tests/beamlines/test_GDA_tools.py | 6 ++-- tests/beamlines/test_SSX_chip.py | 12 +++---- tests/beamlines/test_SSX_expt.py | 2 +- tests/nxs_utils/test_detector.py | 6 ++-- tests/nxs_utils/test_goniometer.py | 2 +- 34 files changed, 180 insertions(+), 109 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 4fa05e2a..9d237ce0 100755 --- a/docs/conf.py +++ b/docs/conf.py @@ -20,8 +20,6 @@ import os import sys -import sphinx_rtd_theme # noqa; F401 - install theme - import nexgen sys.path.insert(0, os.path.abspath("..")) diff --git a/src/nexgen/beamlines/ED_singla_nxs.py b/src/nexgen/beamlines/ED_singla_nxs.py index 2bb60a82..788c5a10 100644 --- a/src/nexgen/beamlines/ED_singla_nxs.py +++ b/src/nexgen/beamlines/ED_singla_nxs.py @@ -13,11 +13,13 @@ from ..nxs_utils.ScanUtils import calculate_scan_points from ..nxs_write.NXmxWriter import EDNXmxFileWriter from ..nxs_write.write_utils import find_number_of_images -from ..tools.ED_tools import (extract_detector_info_from_master, - extract_exposure_time_from_master, - extract_start_time_from_master, find_beam_centre) -from ..utils import (coerce_to_path, find_in_dict, get_iso_timestamp, - get_nexus_filename) +from ..tools.ED_tools import ( + extract_detector_info_from_master, + extract_exposure_time_from_master, + extract_start_time_from_master, + find_beam_centre, +) +from ..utils import coerce_to_path, find_in_dict, get_iso_timestamp, get_nexus_filename from .ED_params import ED_coord_system, EDSingla, EDSource logger = logging.getLogger("nexgen.EDNeXusWriter") diff --git a/src/nexgen/beamlines/GDAtools/GDAjson2params.py b/src/nexgen/beamlines/GDAtools/GDAjson2params.py index 90701801..5e541bf5 100644 --- a/src/nexgen/beamlines/GDAtools/GDAjson2params.py +++ b/src/nexgen/beamlines/GDAtools/GDAjson2params.py @@ -38,7 +38,7 @@ def get_goniometer_axes_from_file(self) -> List[Axis]: """Read the axes information from the GDA-supplied json file.""" axes_list = [] for v in self.params.values(): - if type(v) is dict and v["location"] == "sample": + if isinstance(v, dict) and v["location"] == "sample": ax_depends = self._find_axis_depends_on(v["depends_on"]) ax_type = ( TransformationType.ROTATION @@ -54,7 +54,7 @@ def get_detector_axes_from_file(self) -> List[Axis]: """Read the detector axes information from the GDA-supplied json file.""" axes_list = [] for v in self.params.values(): - if type(v) is dict and v["location"] == "detector": + if isinstance(v, dict) and v["location"] == "detector": ax_type = ( TransformationType.ROTATION if v["type"] == "rotation" diff --git a/src/nexgen/beamlines/I19_2_gda_nxs.py b/src/nexgen/beamlines/I19_2_gda_nxs.py index 3faa953b..0f209ae1 100644 --- a/src/nexgen/beamlines/I19_2_gda_nxs.py +++ b/src/nexgen/beamlines/I19_2_gda_nxs.py @@ -12,16 +12,25 @@ from numpy.typing import DTypeLike from .. import log -from ..nxs_utils import (Attenuator, Beam, Detector, EigerDetector, Goniometer, - Source, TristanDetector) +from ..nxs_utils import ( + Attenuator, + Beam, + Detector, + EigerDetector, + Goniometer, + Source, + TristanDetector, +) from ..nxs_utils.Detector import DetectorType, UnknownDetectorTypeError from ..nxs_utils.ScanUtils import calculate_scan_points from ..nxs_write.NXmxWriter import EventNXmxFileWriter, NXmxFileWriter from ..utils import get_iso_timestamp, get_nexus_filename from .beamline_utils import BeamlineAxes, collection_summary_log -from .GDAtools.ExtendedRequest import (ExtendedRequestIO, - read_det_position_from_xml, - read_scan_from_xml) +from .GDAtools.ExtendedRequest import ( + ExtendedRequestIO, + read_det_position_from_xml, + read_scan_from_xml, +) from .GDAtools.GDAjson2params import JSONParamsIO # Define a logger object and a formatter diff --git a/src/nexgen/beamlines/I19_2_nxs.py b/src/nexgen/beamlines/I19_2_nxs.py index 1bee8e8d..aa6b5dca 100644 --- a/src/nexgen/beamlines/I19_2_nxs.py +++ b/src/nexgen/beamlines/I19_2_nxs.py @@ -13,8 +13,15 @@ import numpy as np from .. import log -from ..nxs_utils import (Attenuator, Beam, Detector, EigerDetector, Goniometer, - Source, TristanDetector) +from ..nxs_utils import ( + Attenuator, + Beam, + Detector, + EigerDetector, + Goniometer, + Source, + TristanDetector, +) from ..nxs_utils.ScanUtils import calculate_scan_points, identify_osc_axis from ..nxs_write.NXmxWriter import EventNXmxFileWriter, NXmxFileWriter from ..tools.Metafile import DectrisMetafile diff --git a/src/nexgen/beamlines/SSX_Eiger_nxs.py b/src/nexgen/beamlines/SSX_Eiger_nxs.py index 910c1cf3..e457dfd0 100644 --- a/src/nexgen/beamlines/SSX_Eiger_nxs.py +++ b/src/nexgen/beamlines/SSX_Eiger_nxs.py @@ -10,8 +10,7 @@ import h5py from .. import log -from ..nxs_utils import (Attenuator, Beam, Detector, EigerDetector, Goniometer, - Source) +from ..nxs_utils import Attenuator, Beam, Detector, EigerDetector, Goniometer, Source from ..nxs_write.NXmxWriter import NXmxFileWriter from ..tools.Metafile import DectrisMetafile from ..tools.MetaReader import define_vds_data_type, update_axes_from_meta diff --git a/src/nexgen/beamlines/SSX_Tristan_nxs.py b/src/nexgen/beamlines/SSX_Tristan_nxs.py index a50bc3d5..cd3f4992 100644 --- a/src/nexgen/beamlines/SSX_Tristan_nxs.py +++ b/src/nexgen/beamlines/SSX_Tristan_nxs.py @@ -8,8 +8,7 @@ from pathlib import Path from .. import log -from ..nxs_utils import (Attenuator, Beam, Detector, Goniometer, Source, - TristanDetector) +from ..nxs_utils import Attenuator, Beam, Detector, Goniometer, Source, TristanDetector from ..nxs_write.NXmxWriter import EventNXmxFileWriter from ..utils import Point3D, find_in_dict, get_iso_timestamp from .beamline_utils import collection_summary_log diff --git a/src/nexgen/beamlines/SSX_chip.py b/src/nexgen/beamlines/SSX_chip.py index cebf2cf2..0585ebd5 100644 --- a/src/nexgen/beamlines/SSX_chip.py +++ b/src/nexgen/beamlines/SSX_chip.py @@ -3,11 +3,10 @@ """ from __future__ import annotations +from dataclasses import dataclass, field from pathlib import Path from typing import Any, Dict, List, Tuple -from dataclasses import dataclass, field - from ..nxs_utils.ScanUtils import ScanDirection # I24 chip tools diff --git a/src/nexgen/beamlines/SSX_expt.py b/src/nexgen/beamlines/SSX_expt.py index 28dc93ea..7b8f5c75 100644 --- a/src/nexgen/beamlines/SSX_expt.py +++ b/src/nexgen/beamlines/SSX_expt.py @@ -15,8 +15,12 @@ from ..nxs_utils import Axis, TransformationType from ..nxs_utils.ScanUtils import calculate_scan_points from .beamline_utils import PumpProbe -from .SSX_chip import (Chip, compute_goniometer, fullchip_blocks_conversion, - read_chip_map) +from .SSX_chip import ( + Chip, + compute_goniometer, + fullchip_blocks_conversion, + read_chip_map, +) __all__ = ["run_extruder", "run_fixed_target", "run_3D_grid_scan"] diff --git a/src/nexgen/beamlines/beamline_utils.py b/src/nexgen/beamlines/beamline_utils.py index e6c6e29e..7a694869 100644 --- a/src/nexgen/beamlines/beamline_utils.py +++ b/src/nexgen/beamlines/beamline_utils.py @@ -4,12 +4,12 @@ from __future__ import annotations import logging +from dataclasses import dataclass from typing import List, Optional, Tuple -from dataclasses import dataclass from dataclasses_json import DataClassJsonMixin -from nexgen.nxs_utils import (Attenuator, Axis, Beam, Detector, Goniometer, - Source) + +from nexgen.nxs_utils import Attenuator, Axis, Beam, Detector, Goniometer, Source from nexgen.utils import Point3D diff --git a/src/nexgen/command_line/ED_nexus.py b/src/nexgen/command_line/ED_nexus.py index e4d0d83f..f0cee90a 100644 --- a/src/nexgen/command_line/ED_nexus.py +++ b/src/nexgen/command_line/ED_nexus.py @@ -11,11 +11,11 @@ import freephil -from . import config_parser, nexus_parser, version_parser from .. import log from ..beamlines.ED_params import ED_coord_system from ..beamlines.ED_singla_nxs import singla_nexus_writer from ..nxs_utils import Axis, TransformationType +from . import config_parser, nexus_parser, version_parser logger = logging.getLogger("nexgen.EDNeXusGeneratorCLI") diff --git a/src/nexgen/command_line/I19_2_cli.py b/src/nexgen/command_line/I19_2_cli.py index 6a92968d..948e13f0 100644 --- a/src/nexgen/command_line/I19_2_cli.py +++ b/src/nexgen/command_line/I19_2_cli.py @@ -8,8 +8,8 @@ from collections import namedtuple from datetime import datetime -from . import version_parser from .. import log +from . import version_parser logger = logging.getLogger("nexgen.I19-2_NeXus_cli") diff --git a/src/nexgen/command_line/SSX_cli.py b/src/nexgen/command_line/SSX_cli.py index 873daeee..be0f9072 100644 --- a/src/nexgen/command_line/SSX_cli.py +++ b/src/nexgen/command_line/SSX_cli.py @@ -7,10 +7,10 @@ from pathlib import Path from typing import Tuple -from . import version_parser from .. import log from ..beamlines.SSX_chip import CHIP_DICT_DEFAULT from ..utils import P +from . import version_parser logger = logging.getLogger("nexgen.SSX_cli") diff --git a/src/nexgen/command_line/cli_utils.py b/src/nexgen/command_line/cli_utils.py index 0a8d2fe3..62613b29 100644 --- a/src/nexgen/command_line/cli_utils.py +++ b/src/nexgen/command_line/cli_utils.py @@ -14,17 +14,22 @@ from scanspec.core import Path as ScanPath from scanspec.specs import Line -from ..nxs_write.NXclassWriters import (write_NXdata, write_NXdatetime, - write_NXdetector, - write_NXdetector_module, write_NXentry, - write_NXinstrument, write_NXnote, - write_NXsample, write_NXsource) +from ..nxs_write.NXclassWriters import ( + write_NXdata, + write_NXdatetime, + write_NXdetector, + write_NXdetector_module, + write_NXentry, + write_NXinstrument, + write_NXnote, + write_NXsample, + write_NXsource, +) from ..nxs_write.write_utils import find_number_of_images from ..tools.DataWriter import generate_event_files, generate_image_files from ..tools.MetaReader import overwrite_beam, overwrite_detector from ..tools.VDS_tools import image_vds_writer, vds_file_writer -from ..utils import (coord2mcstas, get_filename_template, imgcif2mcstas, - units_of_time) +from ..utils import coord2mcstas, get_filename_template, imgcif2mcstas, units_of_time def split_arrays(axes_names: List, array: List) -> Dict[str, Tuple]: @@ -280,9 +285,9 @@ def calculate_scan_range( Dict[str, ArrayLike]: A dictionary of ("axis_name": axis_range) key-value pairs. """ if ( - type(axes_names) is not list - or type(axes_starts) is not list - or type(axes_ends) is not list + not isinstance(axes_names, list) + or not isinstance(axes_starts, list) + or not isinstance(axes_ends, list) ): raise TypeError("Input values for axes must be passed as lists.") @@ -411,7 +416,7 @@ def ScanReader( transl_start = [goniometer["starts"][i] for i in transl_idx] transl_end = [goniometer["ends"][i] for i in transl_idx] transl_increment = [goniometer["increments"][i] for i in transl_idx] - if n_images and type(n_images) is int: + if n_images and isinstance(n_images, int): TRANSL = calculate_scan_range( transl_axes, transl_start, @@ -545,7 +550,7 @@ def call_writers( datafiles = [Path(f).expanduser().resolve() for f in datafiles] if metafile: - if type(metafile) is str: + if isinstance(metafile, str): metafile = Path(metafile).expanduser().resolve() # NXdata: entry/data diff --git a/src/nexgen/command_line/copy_nexus.py b/src/nexgen/command_line/copy_nexus.py index efa5da79..ec601ef8 100644 --- a/src/nexgen/command_line/copy_nexus.py +++ b/src/nexgen/command_line/copy_nexus.py @@ -9,10 +9,9 @@ import freephil -from . import (config_parser, full_copy_parser, tristan_copy_parser, - version_parser) from .. import log from ..nxs_copy import CopyNexus, CopyTristanNexus +from . import config_parser, full_copy_parser, tristan_copy_parser, version_parser # Define a logger object and a formatter logger = logging.getLogger("nexgen.CopyNeXus") diff --git a/src/nexgen/command_line/nexus_generator.py b/src/nexgen/command_line/nexus_generator.py index ec3956ee..355ff54f 100644 --- a/src/nexgen/command_line/nexus_generator.py +++ b/src/nexgen/command_line/nexus_generator.py @@ -14,18 +14,30 @@ import h5py import numpy as np -from . import (add_tristan_spec, config_parser, demo_parser, - detectormode_parser, nexus_parser, phil2dict, version_parser) from .. import log -from ..nxs_write.NXclassWriters import (write_NXdatetime, write_NXentry, - write_NXnote) +from ..nxs_write.NXclassWriters import write_NXdatetime, write_NXentry, write_NXnote from ..tools.DataWriter import generate_event_files, generate_image_files from ..tools.MetaReader import overwrite_beam, overwrite_detector from ..tools.VDS_tools import image_vds_writer, vds_file_writer -from ..utils import (get_filename_template, get_iso_timestamp, - get_nexus_filename, units_of_time) -from .cli_utils import ScanReader # write_nexus_demo, write_nexus -from .cli_utils import call_writers +from ..utils import ( + get_filename_template, + get_iso_timestamp, + get_nexus_filename, + units_of_time, +) +from . import ( + add_tristan_spec, + config_parser, + demo_parser, + detectormode_parser, + nexus_parser, + phil2dict, + version_parser, +) +from .cli_utils import ( + ScanReader, # write_nexus_demo, write_nexus + call_writers, +) # Define a logger object logger = logging.getLogger("nexgen.NeXusGenerator") diff --git a/src/nexgen/command_line/phil_files_cli.py b/src/nexgen/command_line/phil_files_cli.py index 5f25520e..53078031 100644 --- a/src/nexgen/command_line/phil_files_cli.py +++ b/src/nexgen/command_line/phil_files_cli.py @@ -11,8 +11,8 @@ import freephil -from . import config_parser, nexus_parser, version_parser from .. import log, templates +from . import config_parser, nexus_parser, version_parser try: from importlib.resources import files diff --git a/src/nexgen/nxs_copy/CopyTristanNexus.py b/src/nexgen/nxs_copy/CopyTristanNexus.py index 4bb7fbe2..773a0f38 100644 --- a/src/nexgen/nxs_copy/CopyTristanNexus.py +++ b/src/nexgen/nxs_copy/CopyTristanNexus.py @@ -11,9 +11,13 @@ from ..nxs_write.NXclassWriters import write_NXnote from ..nxs_write.write_utils import create_attributes -from .copy_utils import (check_and_fix_det_axis, compute_ssx_axes, - convert_scan_axis, get_nexus_tree, - identify_tristan_scan_axis) +from .copy_utils import ( + check_and_fix_det_axis, + compute_ssx_axes, + convert_scan_axis, + get_nexus_tree, + identify_tristan_scan_axis, +) tristan_logger = logging.getLogger("nexgen.CopyTristanNeXus") diff --git a/src/nexgen/nxs_copy/copy_utils.py b/src/nexgen/nxs_copy/copy_utils.py index 2ec82a6a..c5800566 100644 --- a/src/nexgen/nxs_copy/copy_utils.py +++ b/src/nexgen/nxs_copy/copy_utils.py @@ -141,7 +141,7 @@ def convert_scan_axis(nxsample: h5py.Group, nxdata: h5py.Group, ax: str): def check_and_fix_det_axis(nxs_in: h5py.File): det_z_grp = nxs_in["/entry/instrument/detector/transformations/detector_z"] det_z = det_z_grp["det_z"] - if type(det_z[()]) is bytes or type(det_z[()]) is str: + if isinstance(det_z[()], bytes) or isinstance(det_z[()], str): det_z_attrs = {} for k, v in det_z.attrs.items(): det_z_attrs[k] = v diff --git a/src/nexgen/nxs_utils/Axes.py b/src/nexgen/nxs_utils/Axes.py index 48eb2f90..fb0a1cd8 100644 --- a/src/nexgen/nxs_utils/Axes.py +++ b/src/nexgen/nxs_utils/Axes.py @@ -3,11 +3,10 @@ """ from __future__ import annotations +from dataclasses import dataclass from enum import Enum from typing import Tuple -from dataclasses import dataclass - from ..utils import Point3D diff --git a/src/nexgen/nxs_utils/Detector.py b/src/nexgen/nxs_utils/Detector.py index 93ca7c8f..a6bd5aeb 100644 --- a/src/nexgen/nxs_utils/Detector.py +++ b/src/nexgen/nxs_utils/Detector.py @@ -3,9 +3,9 @@ """ from __future__ import annotations +from dataclasses import dataclass, field from typing import Dict, List, Literal, Tuple, Union -from dataclasses import dataclass, field from dataclasses_json import DataClassJsonMixin from ..utils import Point3D diff --git a/src/nexgen/nxs_utils/Goniometer.py b/src/nexgen/nxs_utils/Goniometer.py index 4c5b90ba..28680d54 100644 --- a/src/nexgen/nxs_utils/Goniometer.py +++ b/src/nexgen/nxs_utils/Goniometer.py @@ -10,8 +10,13 @@ from numpy.typing import ArrayLike from .Axes import Axis -from .ScanUtils import (GridScanOptions, ScanDirection, calculate_scan_points, - identify_grid_scan_axes, identify_osc_axis) +from .ScanUtils import ( + GridScanOptions, + ScanDirection, + calculate_scan_points, + identify_grid_scan_axes, + identify_osc_axis, +) class Goniometer: diff --git a/src/nexgen/nxs_utils/Sample.py b/src/nexgen/nxs_utils/Sample.py index 67a329d7..d82c4aa7 100644 --- a/src/nexgen/nxs_utils/Sample.py +++ b/src/nexgen/nxs_utils/Sample.py @@ -4,6 +4,7 @@ from __future__ import annotations from dataclasses import dataclass + from dataclasses_json import DataClassJsonMixin diff --git a/src/nexgen/nxs_utils/Source.py b/src/nexgen/nxs_utils/Source.py index afc22d53..99f7db51 100644 --- a/src/nexgen/nxs_utils/Source.py +++ b/src/nexgen/nxs_utils/Source.py @@ -4,8 +4,8 @@ from __future__ import annotations from collections import namedtuple - from dataclasses import dataclass + from dataclasses_json import DataClassJsonMixin # Describe facility diff --git a/src/nexgen/nxs_write/NXclassWriters.py b/src/nexgen/nxs_write/NXclassWriters.py index 7728db15..e4d0b05f 100644 --- a/src/nexgen/nxs_write/NXclassWriters.py +++ b/src/nexgen/nxs_write/NXclassWriters.py @@ -13,16 +13,24 @@ from numpy.typing import ArrayLike from ..nxs_utils import Axis -from ..utils import (MAX_SUFFIX_DIGITS, get_iso_timestamp, units_of_length, - units_of_time, ureg) -from .write_utils import (TSdset, calculate_origin, create_attributes, - set_dependency, write_compressed_copy) +from ..utils import ( + MAX_SUFFIX_DIGITS, + get_iso_timestamp, + units_of_length, + units_of_time, + ureg, +) +from .write_utils import ( + TSdset, + calculate_origin, + create_attributes, + set_dependency, + write_compressed_copy, +) # from hdf5plugin import Bitshuffle # noqa: F401 - - NXclass_logger = logging.getLogger("nexgen.NXclass_writers") NXclass_logger.setLevel(logging.DEBUG) @@ -324,7 +332,7 @@ def write_NXsample( if sample_details: for k, v in sample_details.items(): - if type(v) is str: + if isinstance(v, str): v = np.string_(v) nxsample.create_dataset(k, data=v) @@ -524,7 +532,7 @@ def write_NXdetector( nxdetector["flatfield"] = h5py.ExternalLink(flatfield.name, image_key) else: # Flatfield - if type(detector["flatfield"]) is str: + if isinstance(detector["flatfield"], str): nxdetector.create_dataset( "flatfield_applied", data=detector["flatfield_applied"] ) @@ -540,7 +548,7 @@ def write_NXdetector( ) write_compressed_copy(nxdetector, "flatfield", data=detector["flatfield"]) # Bad pixel mask - if type(detector["pixel_mask"]) is str: + if isinstance(detector["pixel_mask"], str): nxdetector.create_dataset( "pixel_mask_applied", data=detector["pixel_mask_applied"] ) @@ -681,7 +689,7 @@ def write_NXdetector( if nxdetector.__contains__(dset) is False and dset in detector.keys(): val = ( np.string_(detector[dset]) - if type(detector[dset]) is str + if isinstance(detector[dset], str) else detector[dset] ) if val is not None: # FIXME bit of a gorilla here, for bit_depth_readout @@ -929,7 +937,7 @@ def write_NXnote(nxsfile: h5py.File, loc: str, info: Dict): # Write datasets for k, v in info.items(): if v: # Just in case one value is not recorded and set as None - if type(v) is str: + if isinstance(v, str): v = np.string_(v) nxnote.create_dataset(k, data=v) NXclass_logger.info(f"{k} dataset written in {loc}.") diff --git a/src/nexgen/nxs_write/NXmxWriter.py b/src/nexgen/nxs_write/NXmxWriter.py index 6f3f8b0f..22ac0f71 100644 --- a/src/nexgen/nxs_write/NXmxWriter.py +++ b/src/nexgen/nxs_write/NXmxWriter.py @@ -16,15 +16,30 @@ from ..nxs_utils.Detector import Detector from ..nxs_utils.Goniometer import Goniometer from ..nxs_utils.Source import Attenuator, Beam, Source -from ..tools.VDS_tools import (clean_unused_links, image_vds_writer, - jungfrau_vds_writer, vds_file_writer) -from ..utils import (MAX_FRAMES_PER_DATASET, MAX_SUFFIX_DIGITS, coord2mcstas, - get_filename_template) -from .NXclassWriters import (write_NXcoordinate_system_set, write_NXdata, - write_NXdatetime, write_NXdetector, - write_NXdetector_module, write_NXentry, - write_NXinstrument, write_NXnote, write_NXsample, - write_NXsource) +from ..tools.VDS_tools import ( + clean_unused_links, + image_vds_writer, + jungfrau_vds_writer, + vds_file_writer, +) +from ..utils import ( + MAX_FRAMES_PER_DATASET, + MAX_SUFFIX_DIGITS, + coord2mcstas, + get_filename_template, +) +from .NXclassWriters import ( + write_NXcoordinate_system_set, + write_NXdata, + write_NXdatetime, + write_NXdetector, + write_NXdetector_module, + write_NXentry, + write_NXinstrument, + write_NXnote, + write_NXsample, + write_NXsource, +) from .write_utils import TSdset, calculate_estimated_end_time # Logger diff --git a/src/nexgen/tools/DataWriter.py b/src/nexgen/tools/DataWriter.py index df12cc32..d2484fd9 100644 --- a/src/nexgen/tools/DataWriter.py +++ b/src/nexgen/tools/DataWriter.py @@ -13,9 +13,16 @@ from hdf5plugin import Bitshuffle from numpy.typing import ArrayLike -from .Constants import (clock_freq, eiger_gap_size, eiger_mod_size, - eiger_modules, tristan_chunk, tristan_gap_size, - tristan_mod_size, tristan_modules) +from .Constants import ( + clock_freq, + eiger_gap_size, + eiger_mod_size, + eiger_modules, + tristan_chunk, + tristan_gap_size, + tristan_mod_size, + tristan_modules, +) data_logger = logging.getLogger("nexgen.DataWriter") diff --git a/src/nexgen/tools/MetaReader.py b/src/nexgen/tools/MetaReader.py index e45ae071..352e5f36 100644 --- a/src/nexgen/tools/MetaReader.py +++ b/src/nexgen/tools/MetaReader.py @@ -44,7 +44,7 @@ def overwrite_beam(meta_file: h5py.File, name: str, beam: Dict | ScopeExtract): # If value exists, overwrite. Otherwise, create. overwrite_logger.warning("Wavelength will be overwritten.") overwrite_logger.info(f"Value for wavelength found in meta file: {wl}") - if type(beam) is dict: + if isinstance(beam, dict): beam["wavelength"] = wl else: try: @@ -72,7 +72,7 @@ def overwrite_detector( """ new_values = {} link_list = [[], []] - if type(detector) is dict: + if isinstance(detector, dict): detector_name = detector["description"].lower() else: detector_name = detector.description.lower() @@ -158,7 +158,7 @@ def overwrite_detector( del new_values[i] for k, v in new_values.items(): - if type(detector) is dict: + if isinstance(detector, dict): detector[k] = v else: try: diff --git a/src/nexgen/tools/VDS_tools.py b/src/nexgen/tools/VDS_tools.py index 4752fc0c..b173bd4b 100644 --- a/src/nexgen/tools/VDS_tools.py +++ b/src/nexgen/tools/VDS_tools.py @@ -5,18 +5,17 @@ import logging import operator +from dataclasses import dataclass from functools import reduce from pathlib import Path from typing import List, Tuple import h5py import numpy as np -from dataclasses import dataclass from numpy.typing import DTypeLike from ..utils import MAX_FRAMES_PER_DATASET -from .Constants import (jungfrau_fill_value, jungfrau_gap_size, - jungfrau_mod_size) +from .Constants import jungfrau_fill_value, jungfrau_gap_size, jungfrau_mod_size vds_logger = logging.getLogger("nexgen.VDSWriter") @@ -120,13 +119,13 @@ def split_datasets( if start_idx < 0: raise ValueError("Start index must be positive") - if type(data_shape[0]) is not int: + if not isinstance(data_shape[0], int): vds_logger.warning("Datashape not passed as int, will attempt to cast") - if type(start_idx) is not int: + if not isinstance(start_idx, int): vds_logger.warning("VDS start index not passed as int, will attempt to cast") - if vds_shape and type(vds_shape[0]) is not int: + if vds_shape and not isinstance(vds_shape[0], int): vds_logger.warning("VDS start index not passed as int, will attempt to cast") if vds_shape is None: diff --git a/tests/beamlines/test_GDA_tools.py b/tests/beamlines/test_GDA_tools.py index 486166b9..1ee6ef4b 100644 --- a/tests/beamlines/test_GDA_tools.py +++ b/tests/beamlines/test_GDA_tools.py @@ -12,14 +12,14 @@ def test_get_coordinate_frame_from_json(dummy_geometry_json): def test_get_gonio_axes_from_json(dummy_geometry_json): gonio_axes = JSONParamsIO(dummy_geometry_json.name).get_goniometer_axes_from_file() - assert type(gonio_axes) is list and len(gonio_axes) == 3 + assert isinstance(gonio_axes, list) and len(gonio_axes) == 3 assert gonio_axes[0].name == "omega" and gonio_axes[1].name == "sam_x" assert gonio_axes[2].name == "phi" and gonio_axes[2].depends == "sam_x" def test_get_detector_axes_from_json(dummy_geometry_json): det_axes = JSONParamsIO(dummy_geometry_json.name).get_detector_axes_from_file() - assert type(det_axes) is list and len(det_axes) == 1 + assert isinstance(det_axes, list) and len(det_axes) == 1 assert det_axes[0].name == "det_z" @@ -51,7 +51,7 @@ def test_read_scan_from_xml(dummy_xml_file): scan_axis, pos, num = read_scan_from_xml(test_ecr) assert scan_axis == test_ecr.getAxisChoice() assert num == 10 - assert type(pos) is dict and len(pos) == 6 # gonio axes on I19-2 + assert isinstance(pos, dict) and len(pos) == 6 # gonio axes on I19-2 assert pos["omega"] == (-180.0, -160.0, 2) assert pos["phi"] == (*2 * (test_ecr.getOtherAxis(),), 0.0) diff --git a/tests/beamlines/test_SSX_chip.py b/tests/beamlines/test_SSX_chip.py index 599da1b4..759ab549 100644 --- a/tests/beamlines/test_SSX_chip.py +++ b/tests/beamlines/test_SSX_chip.py @@ -29,20 +29,20 @@ def test_chip_windows(): def test_chip_size(): size = test_chip.chip_size() - assert type(size) is tuple + assert isinstance(size, tuple) assert size == (6.35, 6.35) def test_chip_types(): - assert type(test_chip.num_steps[0]) is int - assert type(test_chip.step_size[0]) is float - assert type(test_chip.num_blocks[0]) is int - assert type(test_chip.block_size[0]) is float + assert isinstance(test_chip.num_steps[0], int) + assert isinstance(test_chip.step_size[0], float) + assert isinstance(test_chip.num_blocks[0], int) + assert isinstance(test_chip.block_size[0], float) def test_no_chip_map_passed_returns_fullchip(): res = read_chip_map(None, 1, 1) - assert type(res) is dict + assert isinstance(res, dict) assert list(res.values())[0] == "fullchip" diff --git a/tests/beamlines/test_SSX_expt.py b/tests/beamlines/test_SSX_expt.py index 4535d142..675b651c 100644 --- a/tests/beamlines/test_SSX_expt.py +++ b/tests/beamlines/test_SSX_expt.py @@ -35,7 +35,7 @@ def test_run_extruder(): assert gonio[idx].num_steps == 10 assert list(osc.keys()) == ["omega"] assert_array_equal(osc["omega"], np.zeros(10)) - assert type(info) is dict + assert isinstance(info, dict) assert info["pump_exposure"] == 0.1 and info["pump_delay"] is None diff --git a/tests/nxs_utils/test_detector.py b/tests/nxs_utils/test_detector.py index cd3b449a..d177a317 100644 --- a/tests/nxs_utils/test_detector.py +++ b/tests/nxs_utils/test_detector.py @@ -44,7 +44,7 @@ def test_jungfrau_detector(): assert test_jungfrau.sensor_material == "Si" assert test_jungfrau.sensor_thickness == "0.320mm" assert test_jungfrau.hasMeta is False - assert type(test_jungfrau.constants) is dict + assert isinstance(test_jungfrau.constants, dict) def test_singla_detector(): @@ -58,7 +58,7 @@ def test_detector_axes(): det = Detector( test_eiger, det_axes, [100, 200], 0.1, [(0, 0, 1), Point3D(0, -1, 0)] ) - assert type(det.detector_axes) is list + assert isinstance(det.detector_axes, list) assert det.detector_axes[0].name == "two_theta" assert det.detector_axes[1].name == "det_z" assert [det.detector_axes[0].depends, det.detector_axes[1].depends] == [ @@ -105,7 +105,7 @@ def test_detector_to_module_dict(): mod = Detector( test_eiger, det_axes, [100, 200], 0.1, [(0, 0, 1), Point3D(0, -1, 0)] ).to_module_dict() - assert type(mod) is dict + assert isinstance(mod, dict) assert mod["module_offset"] == "1" assert_array_equal(mod["fast_axis"], [0, 0, 1]) assert_array_equal(mod["slow_axis"], [0, -1, 0]) diff --git a/tests/nxs_utils/test_goniometer.py b/tests/nxs_utils/test_goniometer.py index 64b7e8db..90f4bb99 100644 --- a/tests/nxs_utils/test_goniometer.py +++ b/tests/nxs_utils/test_goniometer.py @@ -15,7 +15,7 @@ def test_goniometer_to_dict(): gonio = Goniometer(axes_list[:2]).to_dict() - assert type(gonio) is dict + assert isinstance(gonio, dict) assert gonio["axes"] == ["omega", "sam_z"] assert gonio["depends"] == [".", "omega"] assert gonio["vectors"] == [(0, 0, -1), (0, 0, 1)]