From 5633d242be314b65b12c6ca9fd9dd0f84cdcbc75 Mon Sep 17 00:00:00 2001 From: Oliver Papst Date: Wed, 17 Jan 2024 16:14:02 +0100 Subject: [PATCH] Refactor: ruff format and linting + fix problems --- .github/workflows/check-code-formatting.yml | 8 +-- .pre-commit-config.yaml | 14 +++-- hdtv/app.py | 63 ++++++++++----------- hdtv/backgroundmodels/__init__.py | 4 +- hdtv/backgroundmodels/background.py | 8 +-- hdtv/backgroundmodels/exponential.py | 3 +- hdtv/backgroundmodels/interpolation.py | 3 +- hdtv/backgroundmodels/polynomial.py | 3 +- hdtv/cal.py | 19 +++---- hdtv/cmdline.py | 56 ++++++------------ hdtv/color.py | 4 +- hdtv/cut.py | 3 +- hdtv/database/DDEPLibraries.py | 19 ++++--- hdtv/database/IAEALibraries.py | 12 ++-- hdtv/database/PGAALibraries.py | 16 ++++-- hdtv/database/__init__.py | 2 +- hdtv/database/common.py | 30 +++++----- hdtv/drawable.py | 11 ++-- hdtv/efficiency/__init__.py | 8 +-- hdtv/efficiency/efficiency.py | 43 +++++++------- hdtv/efficiency/exp.py | 8 ++- hdtv/efficiency/polyeff.py | 11 ++-- hdtv/efficiency/pow.py | 6 +- hdtv/efficiency/wiedenhoever.py | 8 ++- hdtv/efficiency/wunder.py | 8 ++- hdtv/fit.py | 36 ++++++------ hdtv/fitter.py | 28 +++++---- hdtv/fitxml.py | 55 +++++++++--------- hdtv/histogram.py | 40 ++++++------- hdtv/hsluv.py | 4 +- hdtv/integral.py | 4 +- hdtv/marker.py | 7 +-- hdtv/matrix.py | 5 +- hdtv/options.py | 4 +- hdtv/peakmodels/__init__.py | 4 +- hdtv/peakmodels/eePeak.py | 28 +++++---- hdtv/peakmodels/peak.py | 3 +- hdtv/peakmodels/theuerkaufPeak.py | 12 ++-- hdtv/plugins/EnergyCalibration.py | 11 ++-- hdtv/plugins/calInterface.py | 28 +++++---- hdtv/plugins/config.py | 6 +- hdtv/plugins/dblookup.py | 15 +++-- hdtv/plugins/fitInterface.py | 45 +++++++-------- hdtv/plugins/fitlist.py | 13 +++-- hdtv/plugins/fitmap.py | 3 +- hdtv/plugins/fittex.py | 11 ++-- hdtv/plugins/ls.py | 4 +- hdtv/plugins/matInterface.py | 20 +++---- hdtv/plugins/peakfinder.py | 12 ++-- hdtv/plugins/printing.py | 13 ++--- hdtv/plugins/rootInterface.py | 15 +++-- hdtv/plugins/run.py | 3 +- hdtv/plugins/specInterface.py | 49 ++++++++-------- hdtv/plugins/textInterface.py | 17 +++--- hdtv/rfile_utils.py | 4 +- hdtv/rootext/display.py | 2 +- hdtv/rootext/dlmgr.py | 7 ++- hdtv/session.py | 8 +-- hdtv/specreader.py | 9 +-- hdtv/tabformat.py | 13 ++--- hdtv/ui.py | 12 ++-- hdtv/util.py | 50 ++++++++-------- hdtv/window.py | 17 +++--- pyproject.toml | 52 ++++++++++------- setup.py | 9 +-- tests/conftest.py | 7 ++- tests/fitxml/test_cal.py | 10 +--- tests/fitxml/test_oldxml.py | 3 +- tests/fitxml/test_switch.py | 10 ++-- tests/fitxml/test_xml.py | 7 +-- tests/helpers/create_test_spectrum.py | 9 ++- tests/helpers/fixtures.py | 2 +- tests/helpers/utils.py | 5 +- tests/integral/inttest.py | 8 +-- tests/mat/mattest.py | 6 +- tests/peak/errtest.py | 2 +- tests/peak/peakgen.py | 15 ++--- tests/plugins/test_backgroundModels.py | 17 ++---- tests/plugins/test_calInterface.py | 16 ++---- tests/plugins/test_calbin.py | 17 ++---- tests/plugins/test_config.py | 4 +- tests/plugins/test_dblookup.py | 4 +- tests/plugins/test_fitInterface.py | 15 ++--- tests/plugins/test_fitmap.py | 10 ++-- tests/plugins/test_help.py | 20 +++---- tests/plugins/test_ls.py | 6 +- tests/plugins/test_matInterface.py | 6 +- tests/plugins/test_printing.py | 9 +-- tests/plugins/test_rootInterface.py | 12 ++-- tests/plugins/test_run.py | 6 +- tests/plugins/test_specInterface.py | 29 ++++------ tests/ui/test_table.py | 2 +- 92 files changed, 614 insertions(+), 691 deletions(-) diff --git a/.github/workflows/check-code-formatting.yml b/.github/workflows/check-code-formatting.yml index cfca1545..e8df33ab 100644 --- a/.github/workflows/check-code-formatting.yml +++ b/.github/workflows/check-code-formatting.yml @@ -57,7 +57,7 @@ jobs: - uses: actions/setup-python@v2 with: python-version: '3.x' - - name: Install black - run: pip install black && black --version - - name: Run black - run: black --check . + - name: Install ruff + run: pip install ruff && ruff --version + - name: Run ruff + run: ruff check diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ef9d17c8..fd41abd1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,17 +1,19 @@ repos: - repo: https://github.com/asottile/pyupgrade - rev: v3.3.1 + rev: v3.15.0 hooks: - id: pyupgrade - args: ["--py37-plus"] + args: ["--py38-plus"] -- repo: https://github.com/psf/black - rev: 23.1.0 +- repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.1.13 hooks: - - id: black + - id: ruff + args: ["--fix"] + - id: ruff-format - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.4.0 + rev: v4.5.0 hooks: - id: check-added-large-files - id: check-case-conflict diff --git a/hdtv/app.py b/hdtv/app.py index aeaea66c..553a0524 100755 --- a/hdtv/app.py +++ b/hdtv/app.py @@ -23,11 +23,10 @@ This is the main HDTV application. """ -import sys +import argparse import os -import glob +import sys from pathlib import Path -import argparse if __name__ == "__main__": project_dir = Path(__file__).absolute().parents[1].resolve() @@ -52,6 +51,23 @@ def check_root_version(): exit(1) +def get_path(env: str, default: str) -> Path: + """ + Get path used for user config and data + """ + if user_path := os.environ.get("HDTV_USER_PATH"): + return Path(user_path) + + legacy_path = Path.home() / ".hdtv" + if legacy_path.exists(): + return legacy_path + + if xdg_path := os.environ.get(env): + return Path(xdg_path) / "hdtv" + + return Path.home() / default / "hdtv" + + class App: def __init__(self): # Reset command line arguments so that ROOT does not stumble about them @@ -59,25 +75,8 @@ def __init__(self): sys.argv = [sys.argv[0]] # Get config and data directory - self.legacypath = Path.home() / ".hdtv" - self.configpath = Path( - os.getenv( - "HDTV_USER_PATH", - self.legacypath - if self.legacypath.is_dir() - else Path(os.getenv("XDG_CONFIG_HOME", Path.home() / ".config")) - / "hdtv", - ) - ) - self.datapath = Path( - os.getenv( - "HDTV_USER_PATH", - self.legacypath - if self.legacypath.is_dir() - else Path(os.getenv("XDG_DATA_HOME", Path.home() / ".local/share")) - / "hdtv", - ) - ) + self.configpath = get_path("XDG_CONFIG_HOME", ".config") + self.datapath = get_path("XDG_DATA_HOME", ".local/share") for path in [self.datapath, self.configpath]: try: @@ -135,27 +134,27 @@ def __init__(self): __main__.spectra = spectra # Import core plugins - import hdtv.plugins.textInterface - import hdtv.plugins.ls - import hdtv.plugins.run - import hdtv.plugins.specInterface - import hdtv.plugins.fitInterface import hdtv.plugins.calInterface - import hdtv.plugins.matInterface - import hdtv.plugins.rootInterface import hdtv.plugins.config + import hdtv.plugins.dblookup + import hdtv.plugins.fitInterface import hdtv.plugins.fitlist - import hdtv.plugins.fittex import hdtv.plugins.fitmap - import hdtv.plugins.dblookup + import hdtv.plugins.fittex + import hdtv.plugins.ls + import hdtv.plugins.matInterface import hdtv.plugins.peakfinder import hdtv.plugins.printing + import hdtv.plugins.rootInterface + import hdtv.plugins.run + import hdtv.plugins.specInterface + import hdtv.plugins.textInterface hdtv.ui.msg("HDTV - Nuclear Spectrum Analysis Tool") # Execute startup.py for user configuration in python try: - import startup + import startup # noqa: F401 except ImportError: hdtv.ui.debug("No startup.py file") diff --git a/hdtv/backgroundmodels/__init__.py b/hdtv/backgroundmodels/__init__.py index 43c866bb..7d8cfb4e 100644 --- a/hdtv/backgroundmodels/__init__.py +++ b/hdtv/backgroundmodels/__init__.py @@ -1,9 +1,9 @@ from .exponential import BackgroundModelExponential -from .polynomial import BackgroundModelPolynomial from .interpolation import BackgroundModelInterpolation +from .polynomial import BackgroundModelPolynomial # dictionary of available background models -BackgroundModels = dict() +BackgroundModels = {} BackgroundModels["exponential"] = BackgroundModelExponential BackgroundModels["polynomial"] = BackgroundModelPolynomial BackgroundModels["interpolation"] = BackgroundModelInterpolation diff --git a/hdtv/backgroundmodels/background.py b/hdtv/backgroundmodels/background.py index 5eba11ef..f9fee5c9 100644 --- a/hdtv/backgroundmodels/background.py +++ b/hdtv/backgroundmodels/background.py @@ -22,9 +22,6 @@ class to handle fitter setup and data transfer to the Python side """ -import ROOT -import hdtv.rootext.display - # Base class for all background models class BackgroundModel: @@ -36,7 +33,7 @@ class BackgroundModel: """ def __init__(self): - self.fGlobalParams = dict() + self.fGlobalParams = {} self.requiredBgRegions = 1 def ResetGlobalParams(self): @@ -136,8 +133,7 @@ def SetParameter(self, parname, status): if parname not in list(self.fValidParStatus.keys()): raise ValueError( - "Invalid parameter name %s for background model %s" - % (parname, self.name) + f"Invalid parameter name {parname} for background model {self.name}" ) self.fParStatus[parname] = self.ParseParamStatus(parname, status) diff --git a/hdtv/backgroundmodels/exponential.py b/hdtv/backgroundmodels/exponential.py index 514b3a97..430aae1c 100644 --- a/hdtv/backgroundmodels/exponential.py +++ b/hdtv/backgroundmodels/exponential.py @@ -18,6 +18,7 @@ # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA import ROOT + from .background import BackgroundModel @@ -55,7 +56,7 @@ def GetFitter(self, integrate, likelihood, nparams=None, nbg=None): else: msg = ( "Status specifier %s of background fitter is invalid." - % fParStatus["nparams"] + % self.fParStatus["nparams"] ) raise ValueError(msg) diff --git a/hdtv/backgroundmodels/interpolation.py b/hdtv/backgroundmodels/interpolation.py index ba34e102..d926c5af 100644 --- a/hdtv/backgroundmodels/interpolation.py +++ b/hdtv/backgroundmodels/interpolation.py @@ -18,6 +18,7 @@ # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA import ROOT + from .background import BackgroundModel @@ -58,7 +59,7 @@ def GetFitter(self, integrate, likelihood, nparams=None, nbg=None): else: msg = ( "Status specifier %s of background fitter is invalid." - % fParStatus["nparams"] + % self.fParStatus["nparams"] ) raise ValueError(msg) diff --git a/hdtv/backgroundmodels/polynomial.py b/hdtv/backgroundmodels/polynomial.py index 2af4e55a..434dc533 100644 --- a/hdtv/backgroundmodels/polynomial.py +++ b/hdtv/backgroundmodels/polynomial.py @@ -18,6 +18,7 @@ # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA import ROOT + from .background import BackgroundModel @@ -69,7 +70,7 @@ def GetFitter(self, integrate, likelihood, nparams=None, nbg=None): else: msg = ( "Status specifier %s of background fitter is invalid." - % fParStatus["nparams"] + % self.fParStatus["nparams"] ) raise ValueError(msg) diff --git a/hdtv/cal.py b/hdtv/cal.py index f8afebcc..44ef0897 100644 --- a/hdtv/cal.py +++ b/hdtv/cal.py @@ -17,17 +17,16 @@ # along with HDTV; if not, write to the Free Software Foundation, # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA +import fnmatch +import re from array import array from html import escape -import re -import fnmatch import ROOT -import hdtv.util -import hdtv.cal -import hdtv.options +import hdtv.options import hdtv.rootext.calibration +import hdtv.util class PositionCalibrationDict(dict): @@ -236,7 +235,7 @@ def FitCal(self, degree, ignore_errors=False): # Save the fit result self.calib = MakeCalibration( - [self.__TF1.GetParameter(i) for i in range(0, degree + 1)] + [self.__TF1.GetParameter(i) for i in range(degree + 1)] ) self.chi2 = self.__TF1.GetChisquare() @@ -262,10 +261,10 @@ def ResultTable(self): header = ["Channel", "E_given", "E_fit", "Residual"] keys = "channel", "e_given", "e_fit", "residual" - tabledata = list() + tabledata = [] for ch, e_given in self.pairs: - tableline = dict() + tableline = {} e_fit = self.calib.Ch2E(ch.nominal_value) residual = e_given - e_fit @@ -300,7 +299,7 @@ def DrawCalFit(self): try: graph.SetPoint(i, ch.nominal_value, e.nominal_value) - except BaseException: + except Exception: graph.SetPoint(i, ch.nominal_value, e) graph.SetPointError(i, ch.std_dev, 0) i += 1 @@ -344,7 +343,7 @@ def DrawCalResidual(self): try: # energie may be ufloat e = e.nominal_value - except BaseException: + except Exception: pass graph.SetPoint(i, ch.nominal_value, e - self.calib.Ch2E(ch.nominal_value)) diff --git a/hdtv/cmdline.py b/hdtv/cmdline.py index 1518f425..1de8ba23 100644 --- a/hdtv/cmdline.py +++ b/hdtv/cmdline.py @@ -21,40 +21,31 @@ HDTV command line """ +import argparse +import asyncio +import code +import glob import os -import sys -import signal import platform -import traceback -import code -import atexit -import subprocess -from pwd import getpwuid -import argparse -import itertools -import errno -from enum import Enum, auto import re -import glob -import asyncio +import subprocess import threading -import time +import traceback +from enum import Enum, auto +from pwd import getpwuid -from prompt_toolkit.shortcuts import PromptSession, CompleteStyle, clear +from prompt_toolkit.auto_suggest import AutoSuggestFromHistory from prompt_toolkit.completion import Completer, Completion -from prompt_toolkit.history import FileHistory from prompt_toolkit.enums import EditingMode +from prompt_toolkit.filters import Condition, emacs_mode +from prompt_toolkit.history import FileHistory from prompt_toolkit.key_binding import KeyBindings -from prompt_toolkit.formatted_text import HTML from prompt_toolkit.key_binding.key_processor import KeyPressEvent -from prompt_toolkit.filters import Condition, emacs_mode -from prompt_toolkit.auto_suggest import AutoSuggestFromHistory +from prompt_toolkit.shortcuts import CompleteStyle, PromptSession, clear -import hdtv.util -import hdtv.options import __main__ - -import ROOT +import hdtv.options +import hdtv.util class CMDType(Enum): @@ -128,10 +119,9 @@ def FindChild(self, title, use_levels=True): if there were unresolvable ambiguities or 0 if there were no matching childs at all. """ - l = len(title) node = 0 for child in self.childs: - if child.title[0:l] == title: + if child.title.startswith(title): if not node: node = child elif use_levels and node.level != child.level: @@ -314,11 +304,9 @@ def GetFileCompleteOptions(self, directory, text, dirs_only=False): except OSError: files = [] - l = len(text) - options = [] for f in files: - if f[0:l] == text: + if f.startswith(text): if os.path.isdir(directory + "/" + f): options.append(f + "/") elif not dirs_only: @@ -424,7 +412,7 @@ class CommandLine: command and Python mode. """ - cmds = dict() + cmds = {} cmds["__name__"] = "hdtv" def __init__(self, command_tree): @@ -445,11 +433,6 @@ def __init__(self, command_tree): self.fKeepRunning = True self.exit_handlers = [] - if os.sep == "\\": - eof = "Ctrl-Z plus Return" - else: - eof = "Ctrl-D (i.e. EOF)" - def StartEventLoop(self): def _loop(loop): asyncio.set_event_loop(loop) @@ -622,7 +605,7 @@ def DoLine(self, line): # We set the python mode accordingly. self.fPyMore = self._py_console.push(cmd) elif cmd_type == CMDType.shell: - subprocess.run(cmd, shell=True) + subprocess.run(cmd, shell=True, check=False) elif cmd_type == CMDType.cmdfile: self.ExecCmdfile(cmd) except KeyboardInterrupt: @@ -774,9 +757,6 @@ def __init__(self, command_tree, cmdline): self.loading = 0 def get_completions(self, document, complete_event): - word_before_cursor = document.get_word_before_cursor() - text_before_cursor = document.text_before_cursor - start = document.current_line_before_cursor.lstrip() yield from self.cmdline.GetCompleteOptions(document, complete_event) diff --git a/hdtv/color.py b/hdtv/color.py index b967b485..68696fd3 100644 --- a/hdtv/color.py +++ b/hdtv/color.py @@ -18,11 +18,11 @@ # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -import colorsys import math + import ROOT -from hdtv.hsluv import hsluv_to_rgb, rgb_to_hsluv +from hdtv.hsluv import hsluv_to_rgb, rgb_to_hsluv # some default colors zoom = ROOT.kWhite diff --git a/hdtv/cut.py b/hdtv/cut.py index 52ededc9..0018c6fe 100644 --- a/hdtv/cut.py +++ b/hdtv/cut.py @@ -18,10 +18,9 @@ # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA import hdtv.color - -from hdtv.weakref_proxy import weakref from hdtv.drawable import Drawable from hdtv.util import LockViewport +from hdtv.weakref_proxy import weakref class Cut(Drawable): diff --git a/hdtv/database/DDEPLibraries.py b/hdtv/database/DDEPLibraries.py index 8ab5d2cc..8ddca146 100644 --- a/hdtv/database/DDEPLibraries.py +++ b/hdtv/database/DDEPLibraries.py @@ -1,16 +1,17 @@ -# DDEP database, Decay Data Evaluation Project +"""DDEP database, Decay Data Evaluation Project""" -import json import hdtv.util try: - import urllib.request import urllib.error + import urllib.request except ImportError: import urllib -import hdtv.ui from uncertainties import ufloat -from hdtv.database.common import * + +import hdtv.ui + +# from hdtv.database.common import * def SearchNuclide(nuclide): @@ -28,7 +29,7 @@ def SearchNuclide(nuclide): "http://www.nucleide.org/DDEP_WG/Nuclides/" + str(nuclide) + ".lara.txt" ) as resource: data = resource.read().decode("utf-8") - except: + except Exception: raise hdtv.cmdline.HDTVCommandError(f"Error looking up nuclide {nuclide}") for line in data.split("\r\n"): @@ -45,17 +46,17 @@ def SearchNuclide(nuclide): energy = ufloat(float(sep[0]), 0) try: energy.std_dev = float(sep[1]) - except BaseException: + except Exception: pass intensity = ufloat(float(sep[2]) / 100, 0) try: intensity.std_dev = float(sep[3]) / 100 - except BaseException: + except Exception: pass out["transitions"].append({"energy": energy, "intensity": intensity}) - except: + except Exception: pass return out diff --git a/hdtv/database/IAEALibraries.py b/hdtv/database/IAEALibraries.py index 58246973..558dedc8 100644 --- a/hdtv/database/IAEALibraries.py +++ b/hdtv/database/IAEALibraries.py @@ -1,10 +1,14 @@ -# IAEA database, International Atomic Energy Agency +"""IAEA database, International Atomic Energy Agency""" import json -import hdtv.util -import hdtv.ui +import os + from uncertainties import ufloat -from hdtv.database.common import * + +import hdtv.ui +import hdtv.util + +# from hdtv.database.common import * # TODO: integrate this in a class? diff --git a/hdtv/database/PGAALibraries.py b/hdtv/database/PGAALibraries.py index e9c3f768..3ff2a2fb 100644 --- a/hdtv/database/PGAALibraries.py +++ b/hdtv/database/PGAALibraries.py @@ -1,11 +1,13 @@ -# PGAA database from Institute of Isotopes, Hungarian Academey of Science, -# Budapest +"""PGAA database from Institute of Isotopes, Hungarian Academey of Science, Budapest""" + +import csv +import os from uncertainties import ufloat, ufloat_fromstr -from hdtv.database.common import * import hdtv.cmdline import hdtv.ui +from hdtv.database.common import Gamma, GammaLib, Nuclides class PGAAGamma(Gamma): @@ -65,10 +67,11 @@ class PGAAlib_IKI2000(GammaLib): def __init__( self, - csvfile=os.path.join(hdtv.datadir, "PGAAlib-IKI2000.dat"), + csvfile=None, has_header=True, k0_comp=(1, 1), ): + csvfile = csvfile or os.path.join(hdtv.datadir, "PGAAlib-IKI2000.dat") super().__init__() # Header for table printout @@ -136,7 +139,7 @@ def open(self): ) self.append(gamma) except csv.Error as e: - raise HDTVCommandAbort( + raise hdtv.cmdline.HDTVCommandAbort( "file %s, line %d: %s" % (self.csvfile, reader.line_num, e) ) else: @@ -152,10 +155,11 @@ class PromptGammas(GammaLib): def __init__( self, - csvfile=os.path.join(hdtv.datadir, "PromptGammas.dat"), + csvfile=None, has_header=True, k0_comp=(1, 1), ): + csvfile = os.path.join(hdtv.datadir, "PromptGammas.dat") super().__init__() # Header for table printout diff --git a/hdtv/database/__init__.py b/hdtv/database/__init__.py index 317467ba..05422581 100644 --- a/hdtv/database/__init__.py +++ b/hdtv/database/__init__.py @@ -1,3 +1,3 @@ -from .PGAALibraries import PromptGammas, PGAAlib_IKI2000, Elements +from .PGAALibraries import PGAAlib_IKI2000, PromptGammas databases = {"promptgammas": PromptGammas, "pgaalib_iki2000": PGAAlib_IKI2000} diff --git a/hdtv/database/common.py b/hdtv/database/common.py index 96cbe2a0..c3983168 100644 --- a/hdtv/database/common.py +++ b/hdtv/database/common.py @@ -1,6 +1,8 @@ import csv import os + from uncertainties import ufloat_fromstr + import hdtv.cmdline import hdtv.ui @@ -45,10 +47,11 @@ class _Elements(list): Read and hold complete elements list """ - def __init__(self, csvfile=os.path.join(hdtv.datadir, "elements.dat")): + def __init__(self, csvfile=None): + csvfile = csvfile or os.path.join(hdtv.datadir, "elements.dat") super().__init__() - tmp = list() + tmp = [] try: try: @@ -75,7 +78,7 @@ def __init__(self, csvfile=os.path.join(hdtv.datadir, "elements.dat")): # Now store elements finally maxZ = max(tmp, key=lambda x: x.z) # Get highest Z - for i in range(maxZ.z): + for _ in range(maxZ.z): self.append(None) for e in tmp: @@ -165,8 +168,9 @@ def __str__(self): class _Nuclides: - def __init__(self, csvfile=os.path.join(hdtv.datadir, "nuclides.dat")): - self._storage = dict() + def __init__(self, csvfile=None): + csvfile = csvfile or os.path.join(hdtv.datadir, "nuclides.dat") + self._storage = {} try: try: datfile = open(csvfile, encoding="utf-8") @@ -193,7 +197,7 @@ def __init__(self, csvfile=os.path.join(hdtv.datadir, "nuclides.dat")): sigma = None if Z not in self._storage: - self._storage[Z] = dict() + self._storage[Z] = {} self._storage[Z][A] = _Nuclide( element, A, abundance=abd, sigma=sigma, M=M ) @@ -210,7 +214,7 @@ def __call__(self, Z=None, A=None, symbol=None, name=None): Nuclides(symbol="Au") or Nuclides(name="gold") or Nuclides(Z=79) return list of all gold nuclides """ - ret = list() + ret = [] if ( Z is not None @@ -226,7 +230,7 @@ def __call__(self, Z=None, A=None, symbol=None, name=None): for e in n.values(): ret.append(e) - tmp = list() + tmp = [] # Select by A if A is not None: @@ -235,7 +239,7 @@ def __call__(self, Z=None, A=None, symbol=None, name=None): tmp.append(n) ret = tmp - tmp = list() + tmp = [] # Select by symbol if symbol is not None: @@ -244,7 +248,7 @@ def __call__(self, Z=None, A=None, symbol=None, name=None): tmp.append(n) ret = tmp - tmp = list() + tmp = [] # Select by name if name is not None: @@ -383,8 +387,8 @@ def find(self, fuzziness=None, sort_key=None, sort_reverse=False, **args): fuzziness = self.fuzziness # convert keys to lowercase - fields_lower = dict() - args_lower = dict() + fields_lower = {} + args_lower = {} for key, conv in list(self.fParamConv.items()): fields_lower[key.lower()] = conv @@ -395,7 +399,7 @@ def find(self, fuzziness=None, sort_key=None, sort_reverse=False, **args): args_lower[key.lower()] = conv(value) # Prepare find args - fargs = dict() + fargs = {} for key, conv in list(fields_lower.items()): try: diff --git a/hdtv/drawable.py b/hdtv/drawable.py index c87c9974..1cc911e7 100644 --- a/hdtv/drawable.py +++ b/hdtv/drawable.py @@ -19,9 +19,9 @@ import hdtv.cal import hdtv.color +import hdtv.options import hdtv.ui from hdtv.util import LockViewport -import hdtv.options class Drawable: @@ -115,7 +115,6 @@ def Refresh(self): """ Refresh the objects data """ - pass def Show(self): """ @@ -159,7 +158,7 @@ class DrawableManager: def __init__(self, viewport=None): self.viewport = viewport # dictionary to store the drawable objects - self.dict = dict() + self.dict = {} self.visible = set() self.activeID = None # This should keep track of ID for nextID, prevID @@ -253,9 +252,9 @@ def Pop(self, ID): """ Remove object with ID """ - if ID == self.activeID: + if self.activeID == ID: self.ActivateObject(None) - if ID == self._iteratorID: + if self._iteratorID == ID: # set iterator to the ID before the one we remove self._iteratorID = self.prevID self.visible.discard(ID) @@ -289,7 +288,7 @@ def Draw(self, viewport): """ Draw function (sets the viewport and draws all components) """ - if not self.viewport is None and not self.viewport == viewport: + if self.viewport is not None and not self.viewport == viewport: # Unlike the Display object of the underlying implementation, # python objects can only be drawn on a single viewport raise RuntimeError("Object can only be drawn on a single viewport") diff --git a/hdtv/efficiency/__init__.py b/hdtv/efficiency/__init__.py index e8ce7d8f..54891c7c 100644 --- a/hdtv/efficiency/__init__.py +++ b/hdtv/efficiency/__init__.py @@ -17,10 +17,10 @@ # along with HDTV; if not, write to the Free Software Foundation, # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -from .wiedenhoever import WiedenhoeverEff -from .wunder import WunderEff -from .polyeff import PolyEff from .exp import ExpEff +from .polyeff import PolyEff from .pow import PowEff +from .wiedenhoever import WiedenhoeverEff +from .wunder import WunderEff -# from . orthogonal import OrthogonalEff +__all__ = ["WiedenhoeverEff", "WunderEff", "PolyEff", "ExpEff", "PowEff"] diff --git a/hdtv/efficiency/efficiency.py b/hdtv/efficiency/efficiency.py index 2ebf5fdc..88156ead 100644 --- a/hdtv/efficiency/efficiency.py +++ b/hdtv/efficiency/efficiency.py @@ -20,11 +20,12 @@ import array import string from math import sqrt + +from ROOT import TF2, TGraphErrors, TVirtualFitter from uncertainties import ufloat -from hdtv.util import TxtFile, Pairs import hdtv.ui -from ROOT import TF1, TF2, TGraphErrors, TVirtualFitter +from hdtv.util import Pairs, TxtFile class _Efficiency: @@ -36,7 +37,7 @@ def __init__(self, num_pars=0, pars=None, norm=True): self.fCov = [ [None for j in range(self._numPars)] for i in range(self._numPars) ] # Simple matrix replacement - self._dEff_dP = list() + self._dEff_dP = [] self.TGraph = TGraphErrors() # Normalization factors @@ -54,7 +55,7 @@ def __init__(self, num_pars=0, pars=None, norm=True): # self.TF1.SetParName(0, "N") # Normalization - for i in range(0, num_pars): + for i in range(num_pars): self._dEff_dP.append(None) if num_pars <= len(string.ascii_lowercase): self.TF1.SetParName(i + 1, string.ascii_lowercase[i]) @@ -63,7 +64,7 @@ def _getParameter(self): """ Get parameter of efficiency function """ - pars = list() + pars = [] for i in range(self._numPars): pars.append(self.TF1.GetParameter(i)) @@ -140,22 +141,22 @@ def fit(self, fitPairs=None, quiet=True): hasXerrors = False # Convert energies to array needed by ROOT try: - list(map(lambda x: E.append(x[0].nominal_value), self._fitInput)) - list(map(lambda x: delta_E.append(x[0].std_dev), self._fitInput)) - list(map(lambda x: EN.append(0.0), self._fitInput)) + [E.append(x[0].nominal_value) for x in self._fitInput] + [delta_E.append(x[0].std_dev) for x in self._fitInput] + [EN.append(0.0) for x in self._fitInput] hasXerrors = True except AttributeError: # energies does not seem to be ufloat list - list(map(lambda x: E.append(x[0]), self._fitInput)) - list(map(lambda x: delta_E.append(0.0), self._fitInput)) + [E.append(x[0]) for x in self._fitInput] + [delta_E.append(0.0) for x in self._fitInput] # Convert efficiencies to array needed by ROOT try: - list(map(lambda x: eff.append(x[1].nominal_value), self._fitInput)) - list(map(lambda x: delta_eff.append(x[1].std_dev), self._fitInput)) - list(map(lambda x: effN.append(0.0), self._fitInput)) + [eff.append(x[1].nominal_value) for x in self._fitInput] + [delta_eff.append(x[1].std_dev) for x in self._fitInput] + [effN.append(0.0) for x in self._fitInput] except AttributeError: # energies does not seem to be ufloat list - list(map(lambda x: eff.append(x[1]), self._fitInput)) - list(map(lambda x: delta_eff.append(0.0), self._fitInput)) + [eff.append(x[1]) for x in self._fitInput] + [delta_eff.append(0.0) for x in self._fitInput] # if fit has errors we first fit without errors to get good initial values # if hasXerrors == True: @@ -217,8 +218,8 @@ def fit(self, fitPairs=None, quiet=True): # Get covariance matrix tvf = TVirtualFitter.GetFitter() ## cov = tvf.GetCovarianceMatrix() - for i in range(0, self._numPars): - for j in range(0, self._numPars): + for i in range(self._numPars): + for j in range(self._numPars): self.fCov[i][j] = tvf.GetCovarianceMatrixElement(i, j) ## self.fCov[i][j] = cov[i * self._numPars + j] @@ -262,9 +263,9 @@ def error(self, E): res = 0.0 # Do matrix multiplication - for i in range(0, self._numPars): + for i in range(self._numPars): tmp = 0.0 - for j in range(0, self._numPars): + for j in range(self._numPars): tmp += self._dEff_dP[j](value, self.parameter) * self.fCov[i][j] res += self._dEff_dP[i](value, self.parameter) * tmp @@ -337,9 +338,9 @@ def saveCov(self, covfile): """ file = TxtFile(covfile, "w") - for i in range(0, self._numPars): + for i in range(self._numPars): line = "" - for j in range(0, self._numPars): + for j in range(self._numPars): line += str(self.fCov[i][j]) + " " file.lines.append(line.strip()) diff --git a/hdtv/efficiency/exp.py b/hdtv/efficiency/exp.py index 602c61f0..8f97f590 100644 --- a/hdtv/efficiency/exp.py +++ b/hdtv/efficiency/exp.py @@ -17,10 +17,12 @@ # along with HDTV; if not, write to the Free Software Foundation, # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -from .efficiency import _Efficiency -from ROOT import TF1 import math +from ROOT import TF1 + +from .efficiency import _Efficiency + class ExpEff(_Efficiency): """ @@ -29,7 +31,7 @@ class ExpEff(_Efficiency): eff(E) = a * exp(-b*E) + c * exp(-d*E) """ - def __init__(self, pars=list(), norm=True): + def __init__(self, pars=None, norm=True): self.name = "Exponential" self.id = self.name + "_" + hex(id(self)) # "( N *( a *exp(- b *E)+ c *exp(- d *E)))" diff --git a/hdtv/efficiency/polyeff.py b/hdtv/efficiency/polyeff.py index aac5e3f6..3068463d 100644 --- a/hdtv/efficiency/polyeff.py +++ b/hdtv/efficiency/polyeff.py @@ -18,11 +18,14 @@ # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA from math import pow -from uncertainties.umath import log, exp -from .efficiency import _Efficiency + from ROOT import TF1, TF2 +from uncertainties.umath import exp, log + from hdtv.util import Pairs +from .efficiency import _Efficiency + class PolyEff(_Efficiency): """ @@ -32,7 +35,7 @@ class PolyEff(_Efficiency): """ - def __init__(self, pars=list(), degree=4, norm=False): + def __init__(self, pars=None, degree=4, norm=False): self.name = "Polynom" self.id = self.name + "_" + hex(id(self)) self._degree = degree @@ -57,7 +60,7 @@ def __init__(self, pars=list(), degree=4, norm=False): def dEff_dP(i): return lambda logE, fPars: self.norm * pow(logE, i) - for i in range(0, degree + 1): + for i in range(degree + 1): self._dEff_dP[i] = dEff_dP(i) def _set_fitInput(self, fitPairs): diff --git a/hdtv/efficiency/pow.py b/hdtv/efficiency/pow.py index 4f01a9fd..062c0f23 100644 --- a/hdtv/efficiency/pow.py +++ b/hdtv/efficiency/pow.py @@ -17,9 +17,9 @@ # along with HDTV; if not, write to the Free Software Foundation, # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -from .efficiency import _Efficiency from ROOT import TF1 -import math + +from .efficiency import _Efficiency class PowEff(_Efficiency): @@ -29,7 +29,7 @@ class PowEff(_Efficiency): eff(E) = a + b * pow(E,-c) """ - def __init__(self, pars=list(), norm=True): + def __init__(self, pars=None, norm=True): self.name = "Power" self.id = self.name + "_" + hex(id(self)) # "( N *( a + b *E ^( -c )))" diff --git a/hdtv/efficiency/wiedenhoever.py b/hdtv/efficiency/wiedenhoever.py index 3b12288d..3a9ff807 100644 --- a/hdtv/efficiency/wiedenhoever.py +++ b/hdtv/efficiency/wiedenhoever.py @@ -17,10 +17,12 @@ # along with HDTV; if not, write to the Free Software Foundation, # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -from .efficiency import _Efficiency -from ROOT import TF1 import math +from ROOT import TF1 + +from .efficiency import _Efficiency + class WiedenhoeverEff(_Efficiency): """ @@ -30,7 +32,7 @@ class WiedenhoeverEff(_Efficiency): new formula: eff(E) = d * (E - b + c * exp(-d * E))^-a """ - def __init__(self, pars=list(), norm=True): + def __init__(self, pars=None, norm=True): self.name = "Wiedenhoever" self.id = self.name + "_" + hex(id(self)) # originally, [4] was not the first coefficient, this version seems to work better diff --git a/hdtv/efficiency/wunder.py b/hdtv/efficiency/wunder.py index 1e9167d9..108aa19a 100644 --- a/hdtv/efficiency/wunder.py +++ b/hdtv/efficiency/wunder.py @@ -17,10 +17,12 @@ # along with HDTV; if not, write to the Free Software Foundation, # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -from .efficiency import _Efficiency -from ROOT import TF1 import math +from ROOT import TF1 + +from .efficiency import _Efficiency + class WunderEff(_Efficiency): """ @@ -29,7 +31,7 @@ class WunderEff(_Efficiency): eff(E) = (a*E + b/E) * exp(c*E + d/E) """ - def __init__(self, pars=list(), norm=True): + def __init__(self, pars=None, norm=True): self.name = "Wunder" self.id = self.name + "_" + hex(id(self)) # ( N * ( a *E + b /E) * exp( c *E + d /E)) diff --git a/hdtv/fit.py b/hdtv/fit.py index a9839b16..f3cc81e5 100644 --- a/hdtv/fit.py +++ b/hdtv/fit.py @@ -18,17 +18,17 @@ # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA import copy -from uncertainties import ufloat import ROOT -import hdtv.color +from uncertainties import ufloat + import hdtv.cal +import hdtv.color import hdtv.integral import hdtv.ui - from hdtv.drawable import Drawable from hdtv.marker import MarkerCollection -from hdtv.util import Pairs, ID, Table, LockViewport +from hdtv.util import ID, LockViewport, Pairs, Table from hdtv.weakref_proxy import weakref @@ -47,8 +47,8 @@ class Fit(Drawable): # List of hook functions to be called before/after FitPeakFunc() # These hook functions should accept a reference to the Fit class # that calls them as first argument - FitPeakPreHooks = list() - FitPeakPostHooks = list() + FitPeakPreHooks = [] + FitPeakPostHooks = [] showDecomp = False # Default value for decomposition status def __init__(self, fitter, color=None, cal=None): @@ -163,7 +163,7 @@ def _get_spec(self): # ids property to get the ids of the peaks @property def ids(self): - ids = list() + ids = [] for i in range(len(self.peaks)): ids.append(ID(self.ID.major, i)) return ids @@ -223,11 +223,11 @@ def ExtractParams(self): peaklist: a list of dicts for each peak in the fit params : a ordered list of valid parameter names """ - peaklist = list() + peaklist = [] params = ["id", "stat", "chi"] # Get peaks for peak in self.peaks: - thispeak = dict() + thispeak = {} thispeak["chi"] = "%d" % self.chi if self.ID is None: thispeak["id"] = hdtv.util.ID(None, self.peaks.index(peak)) @@ -242,7 +242,7 @@ def ExtractParams(self): for p in self.fitter.peakModel.fValidParStatus.keys(): if p == "pos": # Store channel additionally to position - thispeak["channel"] = getattr(peak, "pos") + thispeak["channel"] = peak.pos if "channel" not in params: params.append("channel") # Use calibrated values of params if available @@ -277,7 +277,7 @@ def ExtractIntegralParams(self, integral_type="auto"): integrallist : a list of dicts for each peak in the fit params : a ordered list of valid parameter names """ - integrallist = list() + integrallist = [] params = ["id", "stat", "type"] # if not self.integral: @@ -298,7 +298,7 @@ def ExtractIntegralParams(self, integral_type="auto"): integral_types = [integral_type] for int_type, integral in integrals.items(): - if not integral or not int_type in integral_types: + if not integral or int_type not in integral_types: continue int_res = dict(integral["uncal"]) @@ -376,9 +376,7 @@ def _get_background_pairs(self): except AttributeError: pos = m.p1.pos_cal if m.p1.pos_cal else m.p2.pos_cal hdtv.ui.warning( - "Background region at {:.2f} without second marker was ignored".format( - pos - ) + f"Background region at {pos:.2f} without second marker was ignored" ) return backgrounds @@ -403,7 +401,7 @@ def FitBgFunc(self, spec=None): self.bgChi = self.fitter.bgFitter.GetChisquare() self.bgParams = [] nparams = self.fitter.bgFitter.GetNparams() - for i in range(0, nparams): + for i in range(nparams): self.bgParams.append( ufloat( self.fitter.bgFitter.GetCoeff(i), @@ -475,7 +473,7 @@ def FitPeakFunc(self, spec=None): self.dispPeakFunc.SetCal(self.cal) self.chi = self.fitter.peakFitter.GetChisquare() # create peak list - for i in range(0, self.fitter.peakFitter.GetNumPeaks()): + for i in range(self.fitter.peakFitter.GetNumPeaks()): cpeak = self.fitter.peakFitter.GetPeak(i) peak = self.fitter.peakModel.CopyPeak(cpeak, hdtv.color.peak, self.cal) self.peaks.append(peak) @@ -525,7 +523,7 @@ def Restore(self, spec): self.dispPeakFunc.SetCal(self.cal) # restore display functions of single peaks - for i in range(0, self.fitter.peakFitter.GetNumPeaks()): + for i in range(self.fitter.peakFitter.GetNumPeaks()): cpeak = self.fitter.peakFitter.GetPeak(i) func = cpeak.GetPeakFunc() self.peaks[i].displayObj = ROOT.HDTV.Display.DisplayFunc( @@ -756,7 +754,7 @@ def xdimensions(self): returns: tuple (x_start, x_end) """ - markers = list() + markers = [] # Get maximum of region markers, peak markers and peaks for r in self.regionMarkers: markers.append(r.p1.pos_cal) diff --git a/hdtv/fitter.py b/hdtv/fitter.py index e41dcb62..8addf889 100644 --- a/hdtv/fitter.py +++ b/hdtv/fitter.py @@ -19,8 +19,8 @@ import ROOT -import hdtv.peakmodels import hdtv.backgroundmodels +import hdtv.peakmodels from hdtv.util import Pairs @@ -40,7 +40,7 @@ def __init__(self, peakModel, backgroundModel): def params(self): params = ["background"] for param, valid_status in self.peakModel.fValidParStatus.items(): - if not "calculated" in valid_status: + if "calculated" not in valid_status: params.append(param) for param in self.peakModel.fValidOptStatus.keys(): params.append(param) @@ -50,10 +50,11 @@ def __getattr__(self, name): # Look in peakModel for unknown attributes return getattr(self.peakModel, name) - def FitBackground(self, spec, backgrounds=Pairs()): + def FitBackground(self, spec, backgrounds=None): """ Create Background Fitter object and do the background fit """ + backgrounds = backgrounds or Pairs() # create fitter self.bgFitter = self.backgroundModel.GetFitter( integrate=self.peakModel.GetOption("integrate"), @@ -62,11 +63,7 @@ def FitBackground(self, spec, backgrounds=Pairs()): nbg=len(backgrounds), ) if self.bgFitter is None: - msg = "Background model %s needs at least %i background regions to execute a fit. Found %i.".format( - self.backgroundModel.name, - self.backgroundModel.requiredBgRegions, - len(backgrounds), - ) + msg = "Background model %s needs at least %i background regions to execute a fit. Found %i." raise ValueError(msg) else: for bg in backgrounds: @@ -74,11 +71,13 @@ def FitBackground(self, spec, backgrounds=Pairs()): # do the background fit self.bgFitter.Fit(spec.hist.hist) - def RestoreBackground(self, backgrounds=Pairs(), params=list(), chisquare=0.0): + def RestoreBackground(self, backgrounds=None, params=None, chisquare=0.0): """ Create Background Fitter object and restore the background polynom from coeffs """ + backgrounds = backgrounds or Pairs() + params = params or [] self.bgFitter = self.backgroundModel.GetFitter( integrate=self.peakModel.GetOption("integrate"), likelihood=self.peakModel.GetOption("likelihood"), @@ -93,10 +92,12 @@ def RestoreBackground(self, backgrounds=Pairs(), params=list(), chisquare=0.0): errorArray[i] = param.std_dev self.bgFitter.Restore(valueArray, errorArray, chisquare) - def FitPeaks(self, spec, region=Pairs(), peaklist=list()): + def FitPeaks(self, spec, region=None, peaklist=None): """ Create the Peak Fitter object and do the peak fit """ + region = region or Pairs() + peaklist = peaklist or [] # create the fitter self.peakFitter = self.peakModel.GetFitter(region, peaklist, spec.cal) # Do the peak fit @@ -110,13 +111,16 @@ def FitPeaks(self, spec, region=Pairs(), peaklist=list()): ) def RestorePeaks( - self, cal=None, region=Pairs(), peaks=list(), chisquare=0.0, coeffs=list() + self, cal=None, region=None, peaks=None, chisquare=0.0, coeffs=None ): """ Create the Peak Fitter object and restore all peaks """ # create the fitter + region = region or Pairs + peaks = peaks or [] + coeffs = coeffs or [] peaklist = [p.pos.nominal_value for p in peaks] self.peakFitter = self.peakModel.GetFitter(region, peaklist, cal) # restore first the fitter and afterwards the peaks @@ -127,7 +131,7 @@ def RestorePeaks( # internal background values = ROOT.TArrayD(self.backgroundModel.fParStatus["nparams"]) errors = ROOT.TArrayD(self.backgroundModel.fParStatus["nparams"]) - for i in range(0, self.backgroundModel.fParStatus["nparams"]): + for i in range(self.backgroundModel.fParStatus["nparams"]): values[i] = coeffs[i].nominal_value errors[i] = coeffs[i].std_dev self.peakFitter.Restore(values, errors, chisquare) diff --git a/hdtv/fitxml.py b/hdtv/fitxml.py index a206da2d..3b8c7579 100644 --- a/hdtv/fitxml.py +++ b/hdtv/fitxml.py @@ -18,13 +18,14 @@ # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA import xml.etree.ElementTree as ET + from uncertainties import ufloat import hdtv.ui -from hdtv.util import Position, LockViewport -from hdtv.fitter import Fitter -from hdtv.fit import Fit from hdtv.cmdline import HDTVCommandError +from hdtv.fit import Fit +from hdtv.fitter import Fitter +from hdtv.util import LockViewport, Position # Increase the version number if you changed something related to the xml output. # If your change affect the reading, you should increase the major number @@ -149,7 +150,7 @@ def Fit2Xml(self, fit): bgElement.set("chisquare", str(fit.bgChi)) bgElement.set("backgroundModel", fit.fitter.backgroundModel.name) # - for i in range(0, nparams): + for i in range(nparams): paramElement = ET.SubElement(bgElement, "param") paramElement.set("npar", str(i)) # @@ -203,7 +204,7 @@ def Fit2Xml(self, fit): errorElement.text = str(param.std_dev) # extraElement = ET.SubElement(peakElement, "extras") - for param in sorted(list(peak.extras.keys())): + for param in sorted(peak.extras.keys()): paramElement = ET.SubElement(extraElement, param) param = peak.extras[param] try: @@ -224,7 +225,7 @@ def Fit2Xml(self, fit): for cal_type, cal_integral in integral.items(): calElement = ET.SubElement(integralElement, cal_type) for name, param in cal_integral.items(): - if not name in ["id", "stat", "type"]: + if name not in ["id", "stat", "type"]: paramElement = ET.SubElement(calElement, name) if param.nominal_value is not None: valueElement = ET.SubElement(paramElement, "value") @@ -239,20 +240,22 @@ def _indent(self, elem, level=0): This function formats the xml in-place for prettyprinting Source: http://effbot.org/zone/element-lib.htm#prettyprint + See also https://stackoverflow.com/a/4590052 """ i = "\n" + level * " " + j = "\n" + (level - 1) * " " if len(elem): if not elem.text or not elem.text.strip(): elem.text = i + " " if not elem.tail or not elem.tail.strip(): elem.tail = i - for elem in elem: - self._indent(elem, level + 1) + for subelem in elem: + self._indent(subelem, level + 1) if not elem.tail or not elem.tail.strip(): - elem.tail = i + elem.tail = j else: if level and (not elem.tail or not elem.tail.strip()): - elem.tail = i + elem.tail = j ##### Reading of xml ##################################################### @@ -319,7 +322,7 @@ def ReadFitlist( try: tree = ET.parse(file_object) root = tree.getroot() - if not root.tag == "hdtv" or root.get("version") is None: + if root.tag != "hdtv" or root.get("version") is None: e = "this is not a valid hdtv file" raise SyntaxError(e) # current version @@ -427,7 +430,7 @@ def RestoreFromXml_v1_4( spec = self.spectra.dict[sid] count = 0 do_fit = "" - fits = list() + fits = [] spec_name_last = "" for fitElement in root.findall("fit"): if calibrate: @@ -469,7 +472,7 @@ def RestoreFromXml_v1_4( fits.append(fit) # add fits to spectrum for fit in fits: - ID = spec.Insert(fit) + spec.Insert(fit) count += 1 if sid not in self.spectra.visible: fit.Hide() @@ -542,12 +545,12 @@ def RestoreFromXml_v1_0( if sids is None: sids = self.spectra.ids # create an index of spectra that are saved in xml - index = dict() + index = {} for specElement in root.iter(): name = specElement.get("name") index[name] = specElement # - all_fits = list() + all_fits = [] for sid in sids: spec = self.spectra.dict[sid] try: @@ -571,7 +574,7 @@ def RestoreFromXml_v1_0( ) hdtv.ui.warning(msg) # - fits = list() + fits = [] for fitElement in specElement.findall("fit"): (fit, success) = self.Xml2Fit_v1(fitElement, spec.cal) count = count + 1 @@ -604,7 +607,7 @@ def RestoreFromXml_v1_0( if do_fit in ["Y", "y", "", "A", "a"]: fit.FitPeakFunc(spec) # finish this fit - ID = spec.Insert(fit) + spec.Insert(fit) if sid not in self.spectra.visible: fit.Hide() fits.append(fit) @@ -671,7 +674,7 @@ def Xml2Fit_v1(self, fitElement, calibration=None): fit.bgChi = float(bgElement.get("chisquare")) except ValueError: pass - params = list() + params = [] # Distinguish between old notation of background parameters (coeff, ncoeff), # which interprets the parameters as coefficients of a polynomial, and the @@ -696,11 +699,11 @@ def Xml2Fit_v1(self, fitElement, calibration=None): fit.bgParams = [p[1] for p in params] # - statusdict = dict() + statusdict = {} for peakElement in fitElement.findall("peak"): # uncalElement = peakElement.find("uncal") - parameter = dict() + parameter = {} for paramElement in uncalElement: # parameter value/error name = paramElement.tag @@ -713,7 +716,7 @@ def Xml2Fit_v1(self, fitElement, calibration=None): statusdict[name] = [status] # extraElement = peakElement.find("extras") - extras = dict() + extras = {} if extraElement is not None: for paramElement in extraElement: name = paramElement.tag @@ -745,13 +748,13 @@ def Xml2Fit_v1(self, fitElement, calibration=None): else: status = statusdict[name] fitter.SetParameter(name, status) - integrals = dict() + integrals = {} for integral in fitElement.findall("integral"): integral_type = integral.get("integraltype") - integrals[integral_type] = dict() + integrals[integral_type] = {} for calElement in integral: cal_type = calElement.tag - integrals[integral_type][cal_type] = dict() + integrals[integral_type][cal_type] = {} for paramElement in calElement: # valueElement = paramElement.find("value") @@ -764,7 +767,7 @@ def Xml2Fit_v1(self, fitElement, calibration=None): if not integrals: integrals = None for integral_type in ["sub", "bg"]: - if integrals and not integral_type in integrals: + if integrals and integral_type not in integrals: integrals[integral_type] = None fit.integral = integrals return (fit, success) @@ -802,7 +805,7 @@ def RestoreFromXml_v0(self, root, do_fit=False): # model was a polynomial, and therefore it did not have to be stored fitter = Fitter(peakModel, "polynomial") # - params = dict() + params = {} for resultElement in fitElement.findall("result"): for paramElement in resultElement: parname = paramElement.tag diff --git a/hdtv/histogram.py b/hdtv/histogram.py index fffa86db..1ba1cf7f 100644 --- a/hdtv/histogram.py +++ b/hdtv/histogram.py @@ -20,17 +20,16 @@ import copy import os -from scipy.interpolate import InterpolatedUnivariateSpline import numpy as np - import ROOT +from scipy.interpolate import InterpolatedUnivariateSpline + +import hdtv.cal import hdtv.color -import hdtv.rootext.mfile import hdtv.rootext.calibration import hdtv.rootext.display import hdtv.rootext.fit -import hdtv.cal - +import hdtv.rootext.mfile from hdtv.drawable import Drawable from hdtv.specreader import SpecReader, SpecReaderError from hdtv.util import LockViewport @@ -42,7 +41,7 @@ def HasPrimitiveBinning(hist): if hist.GetNbinsX() != (hist.GetXaxis().GetXmax() - hist.GetXaxis().GetXmin()): return False - for bin in range(0, hist.GetNbinsX()): + for bin in range(hist.GetNbinsX()): if hist.GetBinWidth(bin) != 1.0: return False return True @@ -124,12 +123,8 @@ def info(self): xmin = self._hist.GetXaxis().GetXmin() xmax = self._hist.GetXaxis().GetXmax() if self.cal and not self.cal.IsTrivial(): - s += "Xmin: {:.2f} (cal) {:.2f} (uncal)\n".format( - self.cal.Ch2E(xmin), xmin - ) - s += "Xmax: {:.2f} (cal) {:.2f} (uncal)\n".format( - self.cal.Ch2E(xmax), xmax - ) + s += f"Xmin: {self.cal.Ch2E(xmin):.2f} (cal) {xmin:.2f} (uncal)\n" + s += f"Xmax: {self.cal.Ch2E(xmax):.2f} (cal) {xmax:.2f} (uncal)\n" else: s += "Xmin: %.2f\n" % xmin s += "Xmax: %.2f\n" % xmax @@ -162,7 +157,7 @@ def Plus(self, spec): else: hdtv.ui.info("Adding calibrated") nbins = self._hist.GetNbinsX() - for n in range(0, nbins): + for n in range(nbins): integral = ROOT.HDTV.TH1IntegrateWithPartialBins( spec._hist, spec.cal.E2Ch(self.cal.Ch2E(n - 0.5)), @@ -195,7 +190,7 @@ def Minus(self, spec): else: hdtv.ui.info("Subtracting calibrated") nbins = self._hist.GetNbinsX() - for n in range(0, nbins): + for n in range(nbins): integral = ROOT.HDTV.TH1IntegrateWithPartialBins( spec._hist, spec.cal.E2Ch(self.cal.Ch2E(n - 0.5)), @@ -263,10 +258,10 @@ def Calbin( nbins = int(np.ceil(upper_old / binsize)) + 1 if use_tv_binning: lower = -0.5 * binsize - upper = 0.5 * binsize + (upper_old // nbins) * (nbins - 1) + # upper = 0.5 * binsize + (upper_old // nbins) * (nbins - 1) else: lower = 0.0 - upper = binsize + (upper_old // nbins) * (nbins - 1) + # upper = binsize + (upper_old // nbins) * (nbins - 1) # Create new histogram with number of bins equal # to the calibrated range of the old histogram @@ -310,7 +305,7 @@ def Calbin( "Bins with negative energies in original spectrum were discarded." ) - for i in range(0, nbins): + for i in range(nbins): newhist.SetBinContent(i + 1, output_hist[i]) self._hist = newhist @@ -332,7 +327,7 @@ def Poisson(self): """ Randomize each bin content assuming a Poissonian distribution. """ - for i in range(0, self._hist.GetNbinsX() + 1): + for i in range(self._hist.GetNbinsX() + 1): counts = self._hist.GetBinContent(i) # error = self._hist.GetBinError(i) varied = np.random.poisson(counts) @@ -398,7 +393,7 @@ def SetHistWithPrimitiveBinning(self, hist, caldegree=4, silent=False): if caldegree: cf = hdtv.cal.CalibrationFitter() # TODO: Slow - for bin in range(0, hist.GetNbinsX()): + for bin in range(hist.GetNbinsX()): if caldegree: cf.AddPair(bin, hist.GetXaxis().GetBinUpEdge(bin)) self._hist.SetBinContent(bin, hist.GetBinContent(bin)) @@ -477,8 +472,7 @@ def Refresh(self): hist = SpecReader.GetSpectrum(self.filename, self.fmt) except SpecReaderError as msg: hdtv.ui.warning( - "Failed to load spectrum: %s (file: %s), keeping previous data" - % (msg, self.filename) + f"Failed to load spectrum: {msg} (file: {self.filename}), keeping previous data" ) return self.hist = hist @@ -498,7 +492,7 @@ def info(self): for i in range(len(self.gates)): g = self.gates[i] s += "%d - %d " % (g.p1.pos_cal, g.p2.pos_cal) - if not i == len(self.gates): + if i != len(self.gates): "and" return s @@ -775,7 +769,7 @@ def ExecuteCut(self, regionMarkers, bgMarkers, axis): return hist def GetBasename(self, fname): - if fname.endswith(".mtx") or fname.endswith(".mtx"): + if fname.endswith(".mtx"): return fname[:-4] else: return fname diff --git a/hdtv/hsluv.py b/hdtv/hsluv.py index fed1d4c0..0279d42d 100644 --- a/hdtv/hsluv.py +++ b/hdtv/hsluv.py @@ -26,9 +26,9 @@ haxe -cp haxe/src hsluv.Hsluv -python hsluv.py """ -from functools import wraps as _wraps, partial as _partial # unexport, see #17 import math as _math # unexport, see #17 - +from functools import partial as _partial +from functools import wraps as _wraps # unexport, see #17 __version__ = "5.0.0" diff --git a/hdtv/integral.py b/hdtv/integral.py index eab12aa5..b3bdd135 100644 --- a/hdtv/integral.py +++ b/hdtv/integral.py @@ -17,10 +17,8 @@ # along with HDTV; if not, write to the Free Software Foundation, # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -from uncertainties import ufloat - import ROOT -import hdtv.rootext.fit +from uncertainties import ufloat def Integrate(spec, bg, region): diff --git a/hdtv/marker.py b/hdtv/marker.py index 7b38251d..cbcf30a7 100644 --- a/hdtv/marker.py +++ b/hdtv/marker.py @@ -18,13 +18,12 @@ # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA import ROOT -import hdtv.rootext.display import hdtv.cal import hdtv.color - +import hdtv.rootext.display from hdtv.drawable import Drawable -from hdtv.util import Position, LockViewport +from hdtv.util import LockViewport class Marker(Drawable): @@ -293,7 +292,7 @@ def RemoveNearest(self, pos): if len(self) == 0: hdtv.ui.warning("No marker available, no action taken") return - index = dict() + index = {} for m in self: p1 = m.p1.pos_cal diff = abs(pos - p1) diff --git a/hdtv/matrix.py b/hdtv/matrix.py index 8992a288..718aa12c 100644 --- a/hdtv/matrix.py +++ b/hdtv/matrix.py @@ -18,10 +18,9 @@ # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA import hdtv.color - -from hdtv.weakref_proxy import weakref -from hdtv.spectrum import CutSpectrum from hdtv.drawable import DrawableManager +from hdtv.spectrum import CutSpectrum +from hdtv.weakref_proxy import weakref class Matrix(DrawableManager): diff --git a/hdtv/options.py b/hdtv/options.py index 30864d67..53fdb995 100644 --- a/hdtv/options.py +++ b/hdtv/options.py @@ -122,9 +122,7 @@ def Show(self, varname): """ Shows the value of the variable varname """ - return "{}: {}".format( - escape(varname), escape(str(self.__dict__[varname])) - ) + return f"{escape(varname)}: {escape(str(self.__dict__[varname]))}" def Str(self): """ diff --git a/hdtv/peakmodels/__init__.py b/hdtv/peakmodels/__init__.py index af575933..67653552 100644 --- a/hdtv/peakmodels/__init__.py +++ b/hdtv/peakmodels/__init__.py @@ -1,7 +1,7 @@ -from .theuerkaufPeak import PeakModelTheuerkauf from .eePeak import PeakModelEE +from .theuerkaufPeak import PeakModelTheuerkauf # dictionary of available peak models -PeakModels = dict() +PeakModels = {} PeakModels["theuerkauf"] = PeakModelTheuerkauf PeakModels["ee"] = PeakModelEE diff --git a/hdtv/peakmodels/eePeak.py b/hdtv/peakmodels/eePeak.py index 0f54b536..1c36c09a 100644 --- a/hdtv/peakmodels/eePeak.py +++ b/hdtv/peakmodels/eePeak.py @@ -22,14 +22,12 @@ Implementation requested by Oleksiy Burda """ +import ROOT from uncertainties import ufloat -import ROOT -import hdtv.rootext.display -import hdtv.rootext.fit +from hdtv.drawable import Drawable from .peak import PeakModel -from hdtv.drawable import Drawable class EEPeak(Drawable): @@ -48,7 +46,7 @@ def __init__(self, pos, amp, sigma1, sigma2, eta, gamma, vol, color=None, cal=No # vol is not a fit parameter, but rather the result of the fit self.vol = vol # dictionary for storing additional user supplied values - self.extras = dict() + self.extras = {} def __getattr__(self, name): """ @@ -110,15 +108,15 @@ def formatted_str(self, verbose=False): text = "" if verbose: text += ( - "Pos: {0.pos_cal:S}\n" - "Channel: {0.pos:S}\n" - "Amp: {0.amp:S}\n" - "Sigma1: {0.sigma1:S}\n" - "Sigma2: {0.sigma2:S}\n" - "Eta: {0.eta:S}\n" - "Gamma: {0.gamma:S}\n" - "Volume: {0.vol:S}" - ).format(self) + f"Pos: {self.pos_cal:S}\n" + f"Channel: {self.pos:S}\n" + f"Amp: {self.amp:S}\n" + f"Sigma1: {self.sigma1:S}\n" + f"Sigma2: {self.sigma2:S}\n" + f"Eta: {self.eta:S}\n" + f"Gamma: {self.gamma:S}\n" + f"Volume: {self.vol:S}" + ) else: text += f"Peak@ {self.pos_cal:S}" return text @@ -283,7 +281,7 @@ def GetFitter(self, region, peaklist, cal): # (the function raises a RuntimeError if the check fails) self.CheckParStatusLen(len(peaklist)) - for pid in range(0, len(peaklist)): + for pid in range(len(peaklist)): pos_uncal = peaklist[pid] pos = self.GetParam("pos", pid, pos_uncal, cal, pos_uncal) diff --git a/hdtv/peakmodels/peak.py b/hdtv/peakmodels/peak.py index f75da1f0..0aa18cee 100644 --- a/hdtv/peakmodels/peak.py +++ b/hdtv/peakmodels/peak.py @@ -23,7 +23,6 @@ class to handle fitter setup and data transfer to the Python side """ import ROOT -import hdtv.rootext.fit # Base class for all peak models @@ -35,7 +34,7 @@ class PeakModel: """ def __init__(self): - self.fGlobalParams = dict() + self.fGlobalParams = {} def ResetGlobalParams(self): self.fGlobalParams.clear() diff --git a/hdtv/peakmodels/theuerkaufPeak.py b/hdtv/peakmodels/theuerkaufPeak.py index 15407a1f..f9f8f1e4 100644 --- a/hdtv/peakmodels/theuerkaufPeak.py +++ b/hdtv/peakmodels/theuerkaufPeak.py @@ -19,15 +19,13 @@ import math from functools import total_ordering -from uncertainties import ufloat import ROOT -import hdtv.rootext.display -import hdtv.rootext.fit +from uncertainties import ufloat -from .peak import PeakModel from hdtv.drawable import Drawable -import hdtv.options + +from .peak import PeakModel @total_ordering @@ -48,7 +46,7 @@ def __init__(self, pos, vol, width, tl, tr, sh, sw, color=None, cal=None): self.sh = sh self.sw = sw # dictionary for storing additional user supplied values - self.extras = dict() + self.extras = {} def __getattr__(self, name): """ @@ -321,7 +319,7 @@ def GetFitter(self, region, peaklist, cal): self.CheckParStatusLen(len(peaklist)) # Copy peaks to the fitter - for pid in range(0, len(peaklist)): + for pid in range(len(peaklist)): pos_uncal = peaklist[pid] pos = self.GetParam("pos", pid, pos_uncal, cal, pos_uncal) diff --git a/hdtv/plugins/EnergyCalibration.py b/hdtv/plugins/EnergyCalibration.py index 6de6a624..4e33f13b 100644 --- a/hdtv/plugins/EnergyCalibration.py +++ b/hdtv/plugins/EnergyCalibration.py @@ -21,11 +21,10 @@ Function for energy calibration """ -from uncertainties import ufloat -import hdtv.util import hdtv.ui -from hdtv.database import IAEALibraries, DDEPLibraries +import hdtv.util +from hdtv.database import DDEPLibraries, IAEALibraries def SearchNuclide(nuclide, database): @@ -98,8 +97,8 @@ def MatchPeaksAndEnergies(peaks, energies, sigma): # compare all gradients with each other to find the most frequently one # (within sigma) - for i in range(0, len(gradient)): - for j in range(0, len(gradient)): + for i in range(len(gradient)): + for j in range(len(gradient)): if gradient[j] > gradient[i] - sigma and gradient[j] < gradient[i] + sigma: accordanceCount[i] = accordanceCount[i] + 1 if accordanceCount[i] > NumberHighestAccordance: @@ -108,7 +107,7 @@ def MatchPeaksAndEnergies(peaks, energies, sigma): accordance = [] # all pairs with the right gradient will be saved in this list - for i in range(0, len(gradient)): + for i in range(len(gradient)): if ( gradient[i] > bestAccordance - sigma and gradient[i] < bestAccordance + sigma diff --git a/hdtv/plugins/calInterface.py b/hdtv/plugins/calInterface.py index b7c9a15f..d396eab7 100644 --- a/hdtv/plugins/calInterface.py +++ b/hdtv/plugins/calInterface.py @@ -22,16 +22,16 @@ """ import argparse -import math -from uncertainties import ufloat, ufloat_fromstr -import hdtv.efficiency +from uncertainties import ufloat_fromstr + +import hdtv.cal import hdtv.cmdline +import hdtv.efficiency import hdtv.options import hdtv.ui -import hdtv.cal import hdtv.util -from hdtv.fitxml import FitXml + from . import EnergyCalibration @@ -55,7 +55,7 @@ def SetFun(self, spectrumID, name, parameter=None): * "pow" for power function efficiency """ if parameter is None: - parameter = list() + parameter = [] try: name = name.lower() @@ -109,7 +109,6 @@ def Assign(self, todo): """ Assign efficiency for fit """ - pass def ReadPar(self, spectrumID, filename): """ @@ -170,9 +169,9 @@ def List(self, ids=None): if ids is None: ids = self.spectra.ids - tabledata = list() + tabledata = [] for ID in ids: - tableline = dict() + tableline = {} tableline["ID"] = ID try: tableline["Name"] = self.spectra.dict[ID].effCal.name @@ -226,7 +225,7 @@ def Fit( ) fitValues = hdtv.util.Pairs() - tabledata = list() + tabledata = [] if filename is not None: # at the moment you can only fit from one file # TODO: maybe it makes more sense to write another method for this @@ -441,7 +440,7 @@ def __init__(self, EffCalIf): self.effIf = EffCalIf self.spectra = EffCalIf.spectra - self.opt = dict() + self.opt = {} self.opt["eff_fun"] = hdtv.options.Option( default="wunder", @@ -914,7 +913,7 @@ def CalsFromList(self, fname): : ... The calibrations are written into the calibration dictionary. """ - calDict = dict() + calDict = {} f = hdtv.util.TxtFile(fname) f.read() for l, n in zip(f.lines, f.linos): @@ -934,7 +933,7 @@ def CreateCalList(self, calDict, sort: bool = True): Creates a printable list of all calibrations in calDict : ... """ - lines = list() + lines = [] names = calDict.keys() if sort: names = sorted(names, key=hdtv.util.natural_sort_key) @@ -948,7 +947,6 @@ def CopyCal(self, source_id, ids): Copy a calibration """ cal = self.spectra.dict[source_id].cal - coeffs = hdtv.cal.GetCoeffs(cal) cal = hdtv.cal.MakeCalibration(cal) self.spectra.ApplyCalibration(ids, cal) @@ -1377,7 +1375,7 @@ def CalPosNuc(self, args): ) else: # check if active spectrum is visible - if not __main__.spectra.activeID in __main__.spectra.visible: + if __main__.spectra.activeID not in __main__.spectra.visible: raise hdtv.cmdline.HDTVCommandError( "Active spectrum is not visible, no action taken" ) diff --git a/hdtv/plugins/config.py b/hdtv/plugins/config.py index 1af5bea0..70d9b72a 100644 --- a/hdtv/plugins/config.py +++ b/hdtv/plugins/config.py @@ -21,8 +21,8 @@ from prompt_toolkit.completion import WordCompleter -import hdtv.options import hdtv.cmdline +import hdtv.options import hdtv.util @@ -33,9 +33,7 @@ def ConfigSet(args): raise hdtv.cmdline.HDTVCommandAbort(args.variable + ": no such option") except ValueError as err: raise hdtv.cmdline.HDTVCommandAbort( - "Invalid value ({}) for option {}. {}".format( - args.value, args.variable, err - ) + f"Invalid value ({args.value}) for option {args.variable}. {err}" ) diff --git a/hdtv/plugins/dblookup.py b/hdtv/plugins/dblookup.py index afd21bc0..f355729a 100644 --- a/hdtv/plugins/dblookup.py +++ b/hdtv/plugins/dblookup.py @@ -24,20 +24,19 @@ import re from html import escape -import hdtv.plugins import hdtv.cmdline -import hdtv.options import hdtv.database -import hdtv.ui - import hdtv.fit +import hdtv.options +import hdtv.plugins +import hdtv.ui class Database: def __init__(self): hdtv.ui.debug("Loaded Database Lookup plugin") - self.opt = dict() + self.opt = {} self.database = None # Register configuration variables for fit peakfind @@ -214,7 +213,7 @@ def showDBfields(self, db=None): html = "Valid fields: " if db is None: db = self.database - html += ", ".join([f"'{str(s)}'" for s in db.fParamConv.keys()]) + html += ", ".join([f"'{s!s}'" for s in db.fParamConv.keys()]) hdtv.ui.msg(html=html) def Lookup(self, args, defaults=False): @@ -227,10 +226,10 @@ def Lookup(self, args, defaults=False): self.assureOpen() - lookupargs = dict() + lookupargs = {} # Valid arguments - vargs = list() + vargs = [] for v in self.database.fParamConv.keys(): vargs.append(v.lower()) diff --git a/hdtv/plugins/fitInterface.py b/hdtv/plugins/fitInterface.py index 5c5f0232..ed5c3920 100644 --- a/hdtv/plugins/fitInterface.py +++ b/hdtv/plugins/fitInterface.py @@ -17,16 +17,15 @@ # along with HDTV; if not, write to the Free Software Foundation, # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -import copy -import sys from html import escape import ROOT + import hdtv.cmdline +import hdtv.fit import hdtv.options -import hdtv.util import hdtv.ui -import hdtv.fit +import hdtv.util class FitInterface: @@ -46,7 +45,7 @@ def __init__(self, spectra): # Register configuration variables for fit interface # default region width for quickfit - self.opt = dict() + self.opt = {} self.opt["quickfit.region"] = hdtv.options.Option( default=20.0, parse=lambda x: float(x) ) @@ -290,7 +289,6 @@ def ListIntegrals( if ids is None: ids = spec.ids fits = [spec.dict[ID] for ID in ids] - count_fits = len(fits) (objects, params) = self.ExtractIntegrals(fits, integral_type) # create the table @@ -334,8 +332,8 @@ def ExtractFits(self, fits): fitlist : a list of dicts for each peak in the fits params : a ordered list of valid parameter names """ - fitlist = list() - params = list() + fitlist = [] + params = [] # loop through fits for fit in fits: # Get peaks @@ -378,8 +376,8 @@ def ExtractIntegrals(self, fits, integral_type="auto"): integrallist : a list of dicts for each integral in the fits params : a ordered list of valid parameter names """ - integrallist = list() - params = list() + integrallist = [] + params = [] # loop through fits for fit in fits: (integrals, integralparams) = fit.ExtractIntegralParams(integral_type) @@ -403,7 +401,7 @@ def ShowFitterStatus(self, ids=None): IDs. """ if ids is None: - ids = list() + ids = [] ids.extend("a") statstr = "" for ID in ids: @@ -570,13 +568,13 @@ def ShowDecomposition(self, enable, sid=None, ids=None): Show decomposition of fits """ - fits = list() + fits = [] if sid is None: spec = self.spectra.GetActiveObject() else: spec = self.spectra.dict[sid] - fits = list() + fits = [] if ids is None: if self.spectra.workFit is not None: fits.append(self.spectra.workFit) @@ -999,11 +997,11 @@ def FitMarkerChange(self, args): elif action == "delete": self.spectra.RemoveMarker(mtype, pos) - def MarkerCompleter(self, text, args=[]): + def MarkerCompleter(self, text, args=None): """ Helper function for FitMarkerChange """ - if not args: + if args is None or not args: mtypes = ["background", "region", "peak"] return hdtv.util.GetCompleteOptions(text, mtypes) elif len(args) == 1: @@ -1083,8 +1081,7 @@ def FitIntegralExecute(self, args): for fitID in fitIDs: try: hdtv.ui.msg( - "Executing integral for region of fit %s in spectrum %s" - % (fitID, specID) + f"Executing integral for region of fit {fitID} in spectrum {specID}" ) self.fitIf.ExecuteReintegrate(specID=specID, fitID=fitID) except (KeyError, RuntimeError) as e: @@ -1217,7 +1214,7 @@ def FitFocus(self, args): """ sids = hdtv.util.ID.ParseIds(args.spectrum, self.spectra) - fits = list() + fits = [] if not args.fitid: fits.append(self.spectra.workFit) spec = self.spectra.GetActiveObject() @@ -1254,7 +1251,7 @@ def FitList(self, args): spec = self.spectra.dict[sid] ids = hdtv.util.ID.ParseIds(args.fit, spec) if args.visible: - ids = [ID for ID in spec.visible] + ids = list(spec.visible) if not ids: continue self.fitIf.ListFits( @@ -1278,7 +1275,7 @@ def FitIntegralList(self, args): spec = self.spectra.dict[sid] ids = hdtv.util.ID.ParseIds(args.fit, spec) if args.visible: - ids = [ID for ID in spec.visible] + ids = list(spec.visible) if not ids: continue self.fitIf.ListIntegrals( @@ -1305,7 +1302,7 @@ def FitSetPeakModel(self, args): raise hdtv.cmdline.HDTVCommandError("Invalid peak model '%s'" % name) else: name = models[0].strip() - ids = list() + ids = [] if args.fit: spec = self.spectra.GetActiveObject() if spec is None: @@ -1330,7 +1327,7 @@ def FitSetBackgroundModel(self, args): raise hdtv.cmdline.HDTVCommandError("Invalid background model '%s'" % name) else: name = models[0].strip() - ids = list() + ids = [] if args.fit: spec = self.spectra.GetActiveObject() if spec is None: @@ -1373,7 +1370,7 @@ def FitParam(self, args): "Parameter name %s is not valid" % param ) param = parameter[0].strip() - ids = list() + ids = [] if args.fit: spec = self.spectra.GetActiveObject() if spec is None: @@ -1401,7 +1398,7 @@ def ParamCompleter(self, text, args=None): params.extend(self.spectra.workFit.fitter.params) return hdtv.util.GetCompleteOptions(text, params) else: # args[0] = parameter name -> complete its possible values - states = list() + states = [] param = args[0] if param in ["status", "reset", "background"]: return [] # No further args to autocomplete diff --git a/hdtv/plugins/fitlist.py b/hdtv/plugins/fitlist.py index b56be526..2bcad8e1 100644 --- a/hdtv/plugins/fitlist.py +++ b/hdtv/plugins/fitlist.py @@ -21,11 +21,12 @@ Write and Read Fitlist saved in xml format """ -import os import glob +import os + import hdtv.cmdline -import hdtv.options import hdtv.fitxml +import hdtv.options import hdtv.ui import hdtv.util @@ -36,7 +37,7 @@ def __init__(self, spectra): self.spectra = spectra self.xml = hdtv.fitxml.FitXml(spectra) - self.list = dict() + self.list = {} self.tv = FitlistHDTVInterface(self) @@ -69,7 +70,7 @@ def ReadXML( ) def WriteList(self, fname): - lines = list() + lines = [] listpath = os.path.abspath(fname) for spec, xml in self.list.items(): # create relativ path name @@ -251,7 +252,7 @@ def FitRead(self, args): """ reading a fitlist from xml """ - fnames = dict() # Filenames for each spectrum ID + fnames = {} # Filenames for each spectrum ID sids = hdtv.util.ID.ParseIds(args.spectrum, __main__.spectra) if len(sids) == 0: raise hdtv.cmdline.HDTVCommandError("There is no active spectrum") @@ -270,7 +271,7 @@ def FitRead(self, args): else: fnames[sid] = (fname,) else: - fnames[sid] = list() # Filenames for this spectrum ID + fnames[sid] = [] # Filenames for this spectrum ID for fname_raw in args.filename: try: # Try to replace format placeholder (e.g. %s) with spectrum diff --git a/hdtv/plugins/fitmap.py b/hdtv/plugins/fitmap.py index 8ee53b6b..4db0340d 100644 --- a/hdtv/plugins/fitmap.py +++ b/hdtv/plugins/fitmap.py @@ -18,6 +18,7 @@ # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA import argparse + from uncertainties import ufloat_fromstr import hdtv.cmdline @@ -205,7 +206,7 @@ def FitPosMap(self, args): """ f = hdtv.util.TxtFile(args.filename) f.read() - energies = list() + energies = [] for line in f.lines: energies.append(ufloat_fromstr(line.split(",")[0])) if self.spectra.activeID is None: diff --git a/hdtv/plugins/fittex.py b/hdtv/plugins/fittex.py index db674217..c57f0935 100644 --- a/hdtv/plugins/fittex.py +++ b/hdtv/plugins/fittex.py @@ -18,9 +18,10 @@ # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA import os + +import hdtv.cmdline import hdtv.ui import hdtv.util -import hdtv.cmdline preamble = r"""\makeatletter \@ifundefined{standalonetrue}{\newif\ifstandalone}{\let\ifbackup=\ifstandalone} @@ -80,9 +81,7 @@ def build_header(self): for i in range(2): for string in self.header: string = string.replace("_", "-") - header += r"\multicolumn{{1}}{{{}}}{{\textbf{{{}}}}} &".format( - self.ha, string - ) + header += rf"\multicolumn{{1}}{{{self.ha}}}{{\textbf{{{string}}}}} &" header = header.rstrip("&") header += r"\\ \hline" + os.linesep if i == 0: @@ -122,7 +121,7 @@ def __str__(self): # Build lines for line in lines: line_str = "" - for col in range(0, len(line)): + for col in range(len(line)): if not self._ignore_col[col]: line_str += str(" " + line[col] + " ").rjust(self._col_width[col]) if col != len(line) - 1: @@ -197,7 +196,7 @@ def WriteTex(self, args): filename = os.path.expanduser(args.filename) # get list of fits - fits = list() + fits = [] sids = hdtv.util.ID.ParseIds(args.spectrum, self.spectra) if len(sids) == 0: hdtv.ui.warning("No spectra chosen or active") diff --git a/hdtv/plugins/ls.py b/hdtv/plugins/ls.py index 9562373e..02e5b1bb 100644 --- a/hdtv/plugins/ls.py +++ b/hdtv/plugins/ls.py @@ -22,12 +22,12 @@ his includes a ls-like command, by special request from R. Schulze :) """ +import glob import os import stat -import glob -import hdtv.tabformat import hdtv.cmdline +import hdtv.tabformat import hdtv.ui import hdtv.util diff --git a/hdtv/plugins/matInterface.py b/hdtv/plugins/matInterface.py index d162a858..21457fd2 100644 --- a/hdtv/plugins/matInterface.py +++ b/hdtv/plugins/matInterface.py @@ -21,18 +21,16 @@ Matrix interface for hdtv """ -import os import ROOT -import hdtv.rootext.display +import hdtv.cmdline +import hdtv.rootext.display import hdtv.ui import hdtv.util -import hdtv.cmdline -from hdtv.specreader import SpecReader, SpecReaderError - -from hdtv.matrix import Matrix from hdtv.histogram import MHisto2D +from hdtv.matrix import Matrix +from hdtv.specreader import SpecReader, SpecReaderError class MatInterface: @@ -187,14 +185,14 @@ def LoadMatrix(self, fname, sym, ID=None): def ListMatrix(self, matrix): params = ["ID", "stat", "axis", "gates", "bg", "specID"] - cuts = list() + cuts = [] count = 0 for ID, obj in matrix.dict.items(): - this = dict() + this = {} stat = "" - if ID == matrix.activeID: + if matrix.activeID == ID: stat += "A" if ID in matrix.visible: stat += "V" @@ -443,11 +441,11 @@ def CutMarkerChange(self, args): if action == "delete": self.spectra.RemoveMarker(mtype, args.position) - def MarkerCompleter(self, text, args=[]): + def MarkerCompleter(self, text, args=None): """ Helper function for CutMarkerChange """ - if not args: + if args is None or not args: mtypes = ["background", "region"] return hdtv.util.GetCompleteOptions(text, mtypes) elif len(args) == 1: diff --git a/hdtv/plugins/peakfinder.py b/hdtv/plugins/peakfinder.py index 3dd3f973..aa7c6463 100644 --- a/hdtv/plugins/peakfinder.py +++ b/hdtv/plugins/peakfinder.py @@ -23,12 +23,12 @@ import copy +import ROOT + import hdtv.cmdline import hdtv.options -import hdtv.ui import hdtv.plugins - -import ROOT +import hdtv.ui class PeakFinder: @@ -93,8 +93,8 @@ def PeakSearch(self, sigma, threshold, start=None, end=None): foundpeaks = tSpec.GetPositionX() # Sort by position - tmp = list() - for i in range(0, num_peaks): + tmp = [] + for i in range(num_peaks): tmp.append(foundpeaks[i]) # convert from array to list tmp.sort() foundpeaks = tmp @@ -151,7 +151,7 @@ def StoreFits(self, foundpeaks, autofit=False, reject=False): self.spec, fit.fitter.bgFitter, region ) # add fits to spectrum - ID = self.spec.Insert(fit) + self.spec.Insert(fit) # FIXME: no fit title # fit.title = fit.title + "(*)" # bookkeeping diff --git a/hdtv/plugins/printing.py b/hdtv/plugins/printing.py index d6f3274d..52ae256c 100644 --- a/hdtv/plugins/printing.py +++ b/hdtv/plugins/printing.py @@ -17,21 +17,18 @@ # along with HDTV; if not, write to the Free Software Foundation, # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -import os +import matplotlib import numpy import scipy -import matplotlib matplotlib.use("agg") # Must be before import pylab! import pylab +from matplotlib import transforms -import matplotlib.transforms as transforms - -import hdtv.ui -import hdtv.cmdline import hdtv.cal +import hdtv.cmdline import hdtv.color - +import hdtv.ui # TODO: add cut marker @@ -259,7 +256,7 @@ def Print(self, args): if args.xlabel: pylab.xlabel(args.xlabel) if args.legend and len(self.spectra) > 0: - legend = pylab.legend(prop=dict(size="x-small")) + legend = pylab.legend(prop={"size": "x-small"}) legend.draw_frame(False) # save finished plot to file diff --git a/hdtv/plugins/rootInterface.py b/hdtv/plugins/rootInterface.py index 311e56ba..5e4aedd8 100644 --- a/hdtv/plugins/rootInterface.py +++ b/hdtv/plugins/rootInterface.py @@ -21,21 +21,20 @@ Preliminary ROOT file interface for hdtv """ -import os import fnmatch +import os import ROOT -import hdtv.rootext.display import hdtv.cmdline -import hdtv.tabformat import hdtv.rfile_utils -import hdtv.util +import hdtv.rootext.display +import hdtv.tabformat import hdtv.ui - -from hdtv.spectrum import Spectrum +import hdtv.util from hdtv.histogram import Histogram, RHisto2D, THnSparseWrapper from hdtv.matrix import Matrix +from hdtv.spectrum import Spectrum class RootFileInterface: @@ -47,7 +46,7 @@ def __init__(self, spectra): self.caldict = spectra.caldict self.rootfile = None self.browser = None - self.matviews = list() + self.matviews = [] hdtv.cmdline.AddCommand("root ls", self.RootLs, maxargs=1) hdtv.cmdline.AddCommand("root ll", self.RootLL, maxargs=1, level=0) @@ -380,7 +379,7 @@ def RootGet(self, args): if args.replace: self.spectra.Clear() - loaded = list() + loaded = [] if self.window.viewport: self.window.viewport.LockUpdate() try: # We should really use a context manager here... diff --git a/hdtv/plugins/run.py b/hdtv/plugins/run.py index 9e198df5..ffc7c294 100644 --- a/hdtv/plugins/run.py +++ b/hdtv/plugins/run.py @@ -22,10 +22,9 @@ """ import os -import sys -import hdtv.ui import hdtv.cmdline +import hdtv.ui def run(args): diff --git a/hdtv/plugins/specInterface.py b/hdtv/plugins/specInterface.py index 85c715f5..3cf173cc 100644 --- a/hdtv/plugins/specInterface.py +++ b/hdtv/plugins/specInterface.py @@ -17,22 +17,21 @@ # along with HDTV; if not, write to the Free Software Foundation, # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -import ROOT -import os -import re -import glob import copy +import glob +import os + +import ROOT +import hdtv.cal import hdtv.cmdline import hdtv.color -import hdtv.cal import hdtv.options -import hdtv.util import hdtv.ui - -from hdtv.spectrum import Spectrum +import hdtv.util from hdtv.histogram import FileHistogram from hdtv.specreader import SpecReaderError +from hdtv.spectrum import Spectrum from hdtv.util import LockViewport @@ -204,22 +203,22 @@ def ListSpectra(self, visible=False): """ Create a list of all spectra (for printing) """ - spectra = list() + spectra = [] params = ["ID", "stat", "name", "fits"] - for ID, obj in self.spectra.dict.items(): + for ID in self.spectra.dict.keys(): if visible and (ID not in self.spectra.visible): continue - thisspec = dict() + thisspec = {} status = "" - if ID == self.spectra.activeID: + if self.spectra.activeID == ID: status += "A" if ID in self.spectra.visible: status += "V" - if ID == self.spectra.activeID: + if self.spectra.activeID == ID: color = self.spectra.dict[ID]._activeColor else: color = self.spectra.dict[ID]._passiveColor @@ -263,7 +262,7 @@ class TvSpecInterface: """ def __init__(self, specInterface): - self.opt = dict() + self.opt = {} self.specIf = specInterface self.spectra = self.specIf.spectra @@ -576,14 +575,14 @@ def SpectrumCopy(self, args): if len(ids) == 0: hdtv.ui.warning("Nothing to do") return - targetids = list() + targetids = [] if args.spectrum is not None: targetids = hdtv.util.ID.ParseIds( args.spectrum, self.spectra, only_existent=False ) targetids.sort() if len(targetids) == 0: - targetids = [None for i in range(0, len(ids))] + targetids = [None for i in range(len(ids))] elif len(targetids) == 1: # Only start ID is given startID = targetids[0] targetids = [ @@ -594,7 +593,7 @@ def SpectrumCopy(self, args): raise hdtv.cmdline.HDTVCommandError( "Number of target ids does not match number of ids to copy" ) - for i in range(0, len(ids)): + for i in range(len(ids)): try: self.specIf.CopySpectrum(ids[i], copyTo=targetids[i]) except KeyError: @@ -617,7 +616,7 @@ def SpectrumAdd(self, args): # if addTo is a new spectrum if addTo not in list(self.spectra.dict.keys()): # first copy last of the spectra that should be added - sid = self.specIf.CopySpectrum(ids.pop(), addTo) + self.specIf.CopySpectrum(ids.pop(), addTo) # add all other spectra to the last spectrum for i in ids[1:]: @@ -648,7 +647,7 @@ def SpectrumSub(self, args): subFrom = ids[0] if subFrom not in list(self.spectra.dict.keys()): - sid = self.specIf.CopySpectrum(ids.pop(), subFrom) + self.specIf.CopySpectrum(ids.pop(), subFrom) for i in ids[1:]: try: @@ -672,7 +671,7 @@ def SpectrumMultiply(self, args): ids = [self.spectra.activeID] else: hdtv.ui.msg("No active spectrum") - ids = list() + ids = [] else: ids = hdtv.util.ID.ParseIds(args.specid, self.spectra) @@ -700,7 +699,7 @@ def SpectrumRebin(self, args): ids = [self.spectra.activeID] else: hdtv.ui.msg("No active spectrum") - ids = list() + ids = [] else: ids = hdtv.util.ID.ParseIds(args.specid, self.spectra) @@ -734,7 +733,7 @@ def SpectrumCalbin(self, args): ids = [self.spectra.activeID] else: hdtv.ui.msg("No active spectrum") - ids = list() + ids = [] else: ids = hdtv.util.ID.ParseIds(args.specid, self.spectra) @@ -743,9 +742,9 @@ def SpectrumCalbin(self, args): use_tv_binning ^= args.root_binning if use_tv_binning: - hdtv.ui.debug(f"Calbinning using tv binning convention.") + hdtv.ui.debug("Calbinning using tv binning convention.") else: - hdtv.ui.debug(f"Calbinning using ROOT binning convention.") + hdtv.ui.debug("Calbinning using ROOT binning convention.") if len(ids) == 0: hdtv.ui.warning("Nothing to do") @@ -790,7 +789,7 @@ def SpectrumResample(self, args): ids = [self.spectra.activeID] else: hdtv.ui.msg("No active spectrum") - ids = list() + ids = [] else: ids = hdtv.util.ID.ParseIds(args.specid, self.spectra) diff --git a/hdtv/plugins/textInterface.py b/hdtv/plugins/textInterface.py index af23110c..8ad729b2 100644 --- a/hdtv/plugins/textInterface.py +++ b/hdtv/plugins/textInterface.py @@ -21,23 +21,24 @@ TextInterface functions """ -import hdtv.options -import hdtv.ui -import pydoc -from html import escape import fcntl -import termios +import pydoc import struct +import termios +from html import escape from prompt_toolkit.application import Application from prompt_toolkit.buffer import Buffer from prompt_toolkit.filters import to_filter -from prompt_toolkit.formatted_text import to_formatted_text, fragment_list_to_text, HTML +from prompt_toolkit.formatted_text import HTML, fragment_list_to_text, to_formatted_text from prompt_toolkit.key_binding import KeyBindings from prompt_toolkit.layout import BufferControl from prompt_toolkit.layout.containers import HSplit, Window from prompt_toolkit.layout.layout import Layout -from prompt_toolkit.layout.processors import Transformation, Processor +from prompt_toolkit.layout.processors import Processor, Transformation + +import hdtv.options +import hdtv.ui class FormatText(Processor): @@ -52,7 +53,7 @@ def __init__(self, height=25, width=80): super().__init__() # Set options - self.opt = dict() + self.opt = {} self.opt["ui.pager.cmd"] = hdtv.options.Option(default="less") # default pager self.opt["ui.pager.args"] = hdtv.options.Option( default="-F -X -R -S" diff --git a/hdtv/rfile_utils.py b/hdtv/rfile_utils.py index e68ef2dc..629d28a4 100644 --- a/hdtv/rfile_utils.py +++ b/hdtv/rfile_utils.py @@ -22,10 +22,12 @@ directories """ -import os import fnmatch +import os + import ROOT from prompt_toolkit.completion import Completer, Completion + from hdtv.util import SplitCmdlines # Required for Get() to work (otherwise, histogram objects are automatically diff --git a/hdtv/rootext/display.py b/hdtv/rootext/display.py index eb8b8de0..414bf330 100644 --- a/hdtv/rootext/display.py +++ b/hdtv/rootext/display.py @@ -1,4 +1,4 @@ -import hdtv.rootext.dlmgr import hdtv.rootext.calibration +import hdtv.rootext.dlmgr hdtv.rootext.dlmgr.LoadLibrary("display") diff --git a/hdtv/rootext/dlmgr.py b/hdtv/rootext/dlmgr.py index 37a7ca9a..577274a9 100644 --- a/hdtv/rootext/dlmgr.py +++ b/hdtv/rootext/dlmgr.py @@ -22,15 +22,16 @@ """ import os -import sys import shutil import subprocess +import sys import tempfile + import ROOT -import hdtv.ui -from hdtv.rootext import modules, libfmt +import hdtv.ui from hdtv._version import get_versions +from hdtv.rootext import libfmt, modules __version__ = get_versions()["version"] del get_versions diff --git a/hdtv/session.py b/hdtv/session.py index eb2d7b70..688a265e 100644 --- a/hdtv/session.py +++ b/hdtv/session.py @@ -24,14 +24,12 @@ import copy import hdtv.cal - -from hdtv.window import Window +from hdtv.cut import Cut from hdtv.drawable import DrawableManager -from hdtv.fitter import Fitter from hdtv.fit import Fit -from hdtv.cut import Cut +from hdtv.fitter import Fitter from hdtv.integral import Integrate -from hdtv.util import monkey_patch_ui +from hdtv.window import Window class Session(DrawableManager): diff --git a/hdtv/specreader.py b/hdtv/specreader.py index 7c672290..7a58794c 100644 --- a/hdtv/specreader.py +++ b/hdtv/specreader.py @@ -19,9 +19,11 @@ import array import os + import ROOT -import hdtv.ui + import hdtv.rootext.mfile +import hdtv.ui class SpecReaderError(Exception): @@ -62,7 +64,7 @@ def __init__(self, fmt=None, cmts=("#", "!", "//")): if fmt: self.ncols = len(fmt) - for col in range(0, len(fmt)): + for col in range(len(fmt)): c = fmt[col] if c in ("x", "y", "e"): @@ -121,7 +123,6 @@ def GetSpectrum(self, fname, histname, histtitle): data = [] f = open(fname) linenum = 1 - x = y = e = None try: for line in f: @@ -199,7 +200,7 @@ def GetSpectrum(self, fname, histname, histtitle): hist = ROOT.TH1D(histname, histtitle, nbins, -0.5, nbins - 0.5) # Fill ROOT histogram object - for b in range(0, nbins): + for b in range(nbins): hist.SetBinContent(b + 1, data[b][1]) if self.ecol is not None: hist.SetBinError(b + 1, data[b][2]) diff --git a/hdtv/tabformat.py b/hdtv/tabformat.py index 6f1c628a..4e5f3775 100644 --- a/hdtv/tabformat.py +++ b/hdtv/tabformat.py @@ -20,7 +20,6 @@ try: import curses - import io def get_term_width(): # We call curses.setupterm() every time, as the terminal width @@ -101,7 +100,7 @@ def tabformat(cells, **kwargs): # Try with n_cols columns, and calculate the table width. tbl_width = 0 - for i in range(0, n_cols): + for i in range(n_cols): tbl_width += max(cell_widths[i * n_rows : (i + 1) * n_rows]) # If the table is small enough, end the loop... if tbl_width + (n_cols - 1) * col_sep_width <= tabwidth: @@ -112,17 +111,17 @@ def tabformat(cells, **kwargs): # Now produce the actual output # Calculate the individual column widths col_widths = [] - for i in range(0, n_cols): + for i in range(n_cols): col_widths.append(max(cell_widths[i * n_rows : (i + 1) * n_rows])) # Distribute cells over rows, aloowing the table to be printed # row-by-row (note that successive cells go *below* each other) - rows = [[] for i in range(0, n_rows)] - for i in range(0, n_cells): + rows = [[] for i in range(n_rows)] + for i in range(n_cells): rows[i % n_rows].append(cells[i]) # Fill up rows with empty cells - for i in range(0, n_rows): + for i in range(n_rows): if len(rows[i]) < n_cols: rows[i].append("") @@ -130,5 +129,5 @@ def tabformat(cells, **kwargs): fmtstr = (" " * col_sep_width).join(["%%-%ds" % w for w in col_widths]) # Output the table, row by row - for i in range(0, n_rows): + for i in range(n_rows): print(fmtstr % tuple(rows[i])) diff --git a/hdtv/ui.py b/hdtv/ui.py index 2e46b543..7986d162 100644 --- a/hdtv/ui.py +++ b/hdtv/ui.py @@ -22,18 +22,14 @@ """ import sys -import os from html import escape -from typing import cast, TextIO -import asyncio +from prompt_toolkit import print_formatted_text +from prompt_toolkit.formatted_text import HTML +from prompt_toolkit.patch_stdout import patch_stdout -import hdtv.options import hdtv.cmdline - -from prompt_toolkit.patch_stdout import StdoutProxy, patch_stdout -from prompt_toolkit import print_formatted_text -from prompt_toolkit.formatted_text import HTML, ANSI +import hdtv.options class SimpleUI: diff --git a/hdtv/util.py b/hdtv/util.py index 728cc745..dcc8349b 100644 --- a/hdtv/util.py +++ b/hdtv/util.py @@ -17,23 +17,23 @@ # along with HDTV; if not, write to the Free Software Foundation, # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -import re +import contextlib import os +import re import shlex -from itertools import count -import contextlib from html import escape from html.parser import HTMLParser +from itertools import count from typing import Optional import numpy as np from prompt_toolkit.key_binding.key_bindings import KeyBindings -from prompt_toolkit.shortcuts import PromptSession from prompt_toolkit.keys import Keys +from prompt_toolkit.shortcuts import PromptSession +import hdtv.dummy import hdtv.options import hdtv.ui -import hdtv.dummy def Indent(s, indent=" "): @@ -73,8 +73,8 @@ class TxtFile: """ def __init__(self, filename, mode="r"): - self.lines = list() - self.linos = list() + self.lines = [] + self.linos = [] self.mode = mode # TODO: this has to be handled properly (escaped whitespaces, etc...) filename = str(filename).rstrip() @@ -164,7 +164,6 @@ def remove(self, pair): """ TODO """ - pass def fromFile(self, fname, sep=None): """ @@ -226,7 +225,7 @@ def __init__( self.extra_footer = extra_footer self.sortBy = sortBy self.keys = keys - self.data = list() + self.data = [] self.style = style = hdtv.options.Get("table") @@ -256,12 +255,12 @@ def __init__( self.crossing_char = "" self._width = 0 # Width of table - self._col_width = list() # width of columns + self._col_width = [] # width of columns self.raw_columns = raw_columns # One additional "border column" - self._ignore_col = [ignoreEmptyCols for i in range(0, len(keys) + 1)] + self._ignore_col = [ignoreEmptyCols for i in range(len(keys) + 1)] self.read_data(data, keys, header) if sortBy is not None: @@ -276,9 +275,9 @@ def num_rows(self): return len(self.data) def read_data(self, data, keys, header=None): - self.data = list() + self.data = [] for d in data: - tmp = dict() + tmp = {} for k in keys: try: tmp[k] = d[k] @@ -299,9 +298,9 @@ def read_data(self, data, keys, header=None): self._col_width.append(len(str(header)) + 2) def build_lines(self): - lines = list() + lines = [] for d in self.data: - line = list() + line = [] for i, key in enumerate(self.keys): try: value = d[key] @@ -358,7 +357,7 @@ def calc_width(self): def build_header(self): headerline = "" - for col in range(0, len(self.header)): + for col in range(len(self.header)): if not self._ignore_col[col]: headerline += ( "" @@ -374,9 +373,9 @@ def build_header(self): def build_sep(self): # Seperator between header and data header_sep_line = "" - for i in range(0, len(self._col_width)): + for i in range(len(self._col_width)): if not self._ignore_col[i]: - for j in range(0, self._col_width[i]): + for _ in range(self._col_width[i]): if self.header[i] == self.sortBy: header_sep_line += self.header_sep_char_sorted else: @@ -401,7 +400,7 @@ def __str__(self): for line in lines: line_str = "" - for col in range(0, len(line)): + for col in range(len(line)): if not self._ignore_col[col]: if self.raw_columns and self.keys[col] in self.raw_columns: fill_len = self._col_width[col] - len(strip_tags(line[col])) - 1 @@ -597,7 +596,7 @@ def __float__(self): @classmethod def _parseSpecialID(cls, string, manager): if string.upper() == "NONE": - return list() + return [] elif string.upper() == "NEXT": return [manager.nextID] elif string.upper() == "PREV": @@ -643,7 +642,7 @@ def ParseIds(cls, strings, manager, only_existent=True): # Split string parts = [p for p in strings.split(",") if p] - ids = list() + ids = [] for s in parts: # first deal with ranges if "-" in s: @@ -690,11 +689,11 @@ def ParseIds(cls, strings, manager, only_existent=True): # ID might be None, if e.g. activeID is None count = ids.count(None) - for i in range(count): + for _ in range(count): ids.remove(None) # filter non-existing ids - valid_ids = list() + valid_ids = [] if only_existent: for ID in ids: for mID in manager.ids: @@ -854,10 +853,11 @@ class monkey_patch_ui: """Replace ROOT.HDTV.Display by a noop dummy version""" def __init__(self): - import hdtv - import hdtv.rootext.display import ROOT + + import hdtv import hdtv.dummy + import hdtv.rootext.display self._orig = ROOT.HDTV.Display ROOT.HDTV.Display = hdtv.dummy diff --git a/hdtv/window.py b/hdtv/window.py index 8bc4e54e..cec859ca 100644 --- a/hdtv/window.py +++ b/hdtv/window.py @@ -18,21 +18,22 @@ # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA import traceback + import ROOT -import hdtv.options -import hdtv.color + import hdtv.cmdline +import hdtv.color +import hdtv.options +import hdtv.rootext.display import hdtv.ui from hdtv.marker import MarkerCollection -import hdtv.rootext.display -from hdtv.cmdline import HDTVCommandAbort, HDTVCommandError class HotkeyList: "Class to handle multi-key hotkeys." def __init__(self): - self.fKeyCmds = dict() + self.fKeyCmds = {} self.fCurNode = self.fKeyCmds def AddHotkey(self, key, cmd): @@ -53,7 +54,7 @@ def AddHotkey(self, key, cmd): if not isinstance(curNode, dict): raise RuntimeError("Refusing to overwrite non-matching hotkey") except KeyError: - curNode[k] = dict() + curNode[k] = {} curNode = curNode[k] key = key[-1] @@ -186,7 +187,7 @@ def KeyHandler(self): if handled is None: self.keyString += keyStr self.viewport.SetStatusText("Command: %s" % self.keyString) - elif handled == False: + elif not handled: self.keyString += keyStr self.viewport.SetStatusText("Invalid hotkey %s" % self.keyString) self.keyString = "" @@ -210,7 +211,7 @@ def __init__(self): self.viewer = ROOT.HDTV.Display.Viewer() self.viewport = self.viewer.GetViewport() - self._dispatchers = list() + self._dispatchers = [] # Handle closing of the main window (with an application exit) disp = ROOT.TPyDispatcher(hdtv.cmdline.AsyncExit) diff --git a/pyproject.toml b/pyproject.toml index 52b71024..c672761c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,19 +1,33 @@ -[tool.black] -target-version = ['py37', 'py38', 'py39', 'py310', 'py311'] -include = '\.pyi?$' -exclude = ''' -( - /( - \.asv - | \.eggs - | \.git - | \.venv - | benchmarks - | build - | conda.recipe - | dist - )/ - | _version.py - | versioneer.py -) -''' +[tool.ruff] +target-version = "py38" +exclude = ["*.asv", "benchmarks", "_version.py", "versioneer.py"] +ignore = [ + "E741", + "E402", + "E501", + "E722", + "PLR", + "PLW2901", + "RUF012", # enforces type annotations on a codebase that lacks type annotations + "B904", + "ISC001", +] +select = [ + "E", + "F", + "W", # flake8 + "B", + #"B904", # flake8-bugbear + "I", # isort + #"ARG", # flake8-unused-arguments + "C4", # flake8-comprehensions + "ISC", # flake8-implicit-str-concat + "PGH", # pygrep-hooks + "PIE", # flake8-pie + "PL", # pylint + #"PT", # flake8-pytest-style + #"RUF", # Ruff-specific + #"SIM", # flake8-simplify + "UP", # pyupgrade + "YTT" # flake8-2020 +] diff --git a/setup.py b/setup.py index bba16536..dc6b4051 100755 --- a/setup.py +++ b/setup.py @@ -1,9 +1,11 @@ #!/usr/bin/env python3 -import versioneer -from setuptools import setup -from pathlib import Path import glob +from pathlib import Path + +from setuptools import setup + +import versioneer manpages = glob.glob("doc/guide/*.1") @@ -34,7 +36,6 @@ Programming Language :: C++ Programming Language :: Python Programming Language :: Python :: 3 -Programming Language :: Python :: 3.7 Programming Language :: Python :: 3.8 Programming Language :: Python :: 3.9 Programming Language :: Python :: 3.10 diff --git a/tests/conftest.py b/tests/conftest.py index afefa1c0..8111e6d8 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -17,14 +17,17 @@ # along with HDTV; if not, write to the Free Software Foundation, # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -import sys import os -import tempfile import shutil +import sys +import tempfile sys.path.append(os.path.join(os.path.dirname(__file__), "helpers")) +pytest_plugins = ("tests.helpers.fixtures",) + + def pytest_addoption(parser): parser.addoption( "--force-rebuild", action="store_true", help="Force library rebuild." diff --git a/tests/fitxml/test_cal.py b/tests/fitxml/test_cal.py index ae802934..d8e46cfe 100755 --- a/tests/fitxml/test_cal.py +++ b/tests/fitxml/test_cal.py @@ -24,19 +24,15 @@ import os import pytest - from numpy import linspace, ones, savetxt, sqrt from scipy.stats import norm -from tests.helpers.utils import setup_io, redirect_stdout, isclose -from tests.helpers.fixtures import temp_file - from hdtv.util import monkey_patch_ui +from tests.helpers.utils import isclose, redirect_stdout, setup_io monkey_patch_ui() import __main__ - import hdtv.session try: @@ -45,9 +41,9 @@ pass spectra = __main__.spectra -from hdtv.plugins.specInterface import spec_interface from hdtv.plugins.fitInterface import fit_interface from hdtv.plugins.fitlist import fitxml +from hdtv.plugins.specInterface import spec_interface testspectrum = os.path.join(os.path.curdir, "tests", "share", "osiris_bg.spc") testspectrum_h1 = os.path.join(os.path.curdir, "tests", "share", "binning_h1.tv") @@ -140,7 +136,7 @@ def test_mark_and_calibrate(region1, region2, peak): markers_before = test_mark(region1, region2, peak) spectra.ApplyCalibration("0", [0, 2]) markers_after = get_markers() - assert all([x == y for (x, y) in zip(markers_before, markers_after)]) + assert all(x == y for (x, y) in zip(markers_before, markers_after)) @pytest.mark.parametrize("region1, region2, peak", [(2900.0, 2940.0, 2920.0)]) diff --git a/tests/fitxml/test_oldxml.py b/tests/fitxml/test_oldxml.py index 63b8cd0a..a4920675 100755 --- a/tests/fitxml/test_oldxml.py +++ b/tests/fitxml/test_oldxml.py @@ -27,7 +27,6 @@ monkey_patch_ui() import __main__ - import hdtv.session try: @@ -36,9 +35,9 @@ pass spectra = __main__.spectra -from hdtv.plugins.specInterface import spec_interface from hdtv.plugins.fitInterface import fit_interface from hdtv.plugins.fitlist import fitxml +from hdtv.plugins.specInterface import spec_interface testspectrum = os.path.join(os.path.curdir, "tests", "share", "osiris_bg.spc") diff --git a/tests/fitxml/test_switch.py b/tests/fitxml/test_switch.py index c302e696..f8f75074 100755 --- a/tests/fitxml/test_switch.py +++ b/tests/fitxml/test_switch.py @@ -21,14 +21,12 @@ import pytest -from tests.helpers.utils import setup_io, redirect_stdout, isclose - from hdtv.util import monkey_patch_ui, strip_tags +from tests.helpers.utils import redirect_stdout, setup_io monkey_patch_ui() import __main__ - import hdtv.session try: @@ -37,9 +35,9 @@ pass spectra = __main__.spectra -from hdtv.plugins.specInterface import spec_interface -from hdtv.plugins.fitInterface import fit_interface import hdtv.plugins.fitlist +from hdtv.plugins.fitInterface import fit_interface +from hdtv.plugins.specInterface import spec_interface testspectrum = os.path.join(os.path.curdir, "tests", "share", "osiris_bg.spc") @@ -181,7 +179,7 @@ def test_spectra_activate_hidden(): def test_spectra_remove_one(): spectra.Pop("3") res = get_list() - assert not "3 | " in res + assert "3 | " not in res def test_spectra_reload_one(): diff --git a/tests/fitxml/test_xml.py b/tests/fitxml/test_xml.py index 0626291d..edcbfbcb 100755 --- a/tests/fitxml/test_xml.py +++ b/tests/fitxml/test_xml.py @@ -28,15 +28,12 @@ import pytest -from tests.helpers.utils import setup_io, redirect_stdout -from tests.helpers.fixtures import temp_file_compressed - from hdtv.util import monkey_patch_ui +from tests.helpers.utils import redirect_stdout, setup_io monkey_patch_ui() import __main__ - import hdtv.session try: @@ -45,9 +42,9 @@ pass spectra = __main__.spectra -from hdtv.plugins.specInterface import spec_interface from hdtv.plugins.fitInterface import fit_interface from hdtv.plugins.fitlist import fitxml +from hdtv.plugins.specInterface import spec_interface testspectrum = os.path.join(os.path.curdir, "tests", "share", "osiris_bg.spc") diff --git a/tests/helpers/create_test_spectrum.py b/tests/helpers/create_test_spectrum.py index 7afdd388..ad9c3b60 100644 --- a/tests/helpers/create_test_spectrum.py +++ b/tests/helpers/create_test_spectrum.py @@ -19,11 +19,9 @@ import os -import pytest - -from numpy import arange, exp, log, ones, savetxt, sqrt -from numpy.random import poisson import numpy as np +from numpy import arange, exp, log, ones, savetxt +from numpy.random import poisson from scipy.stats import norm @@ -44,7 +42,8 @@ def __init__(self): class ArtificialSpec: - def __init__(self, path, prop=ArtificialSpecProp()): + def __init__(self, path, prop=None): + prop = prop or ArtificialSpecProp() self.nsteps = prop.nsteps self.bg_regions = prop.bg_regions self.peak_width = prop.peak_width diff --git a/tests/helpers/fixtures.py b/tests/helpers/fixtures.py index 6293d5e8..86dd3466 100644 --- a/tests/helpers/fixtures.py +++ b/tests/helpers/fixtures.py @@ -17,9 +17,9 @@ # along with HDTV; if not, write to the Free Software Foundation, # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -import tempfile import os import sys +import tempfile import pytest diff --git a/tests/helpers/utils.py b/tests/helpers/utils.py index 36e22236..a2a7e0d9 100644 --- a/tests/helpers/utils.py +++ b/tests/helpers/utils.py @@ -17,17 +17,16 @@ # along with HDTV; if not, write to the Free Software Foundation, # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -import sys -import os import contextlib +import sys try: # Python2 from StringIO import StringIO except ImportError: # Python3 from io import StringIO -from hdtv.ui import ui import hdtv.cmdline +from hdtv.ui import ui def setup_io(num=1): diff --git a/tests/integral/inttest.py b/tests/integral/inttest.py index 1ad7020a..0e53a5b5 100755 --- a/tests/integral/inttest.py +++ b/tests/integral/inttest.py @@ -3,12 +3,10 @@ import sys sys.path.append("/home/braun/projects/hdtv") -import hdtv.specreader -from hdtv.errvalue import ErrValue import ROOT -import math -hdtv.rootext.fit +import hdtv.specreader +from hdtv.errvalue import ErrValue def test(bgdeg): @@ -19,7 +17,7 @@ def test(bgdeg): bg.AddRegion(12.9, 18.1) bg.Fit(h) - for i in range(0, bg.GetDegree() + 1): + for i in range(bg.GetDegree() + 1): par = ErrValue(bg.GetFunc().GetParameter(i), bg.GetFunc().GetParError(i)) print("bg[%d]: %10s" % (i, par.fmt())) diff --git a/tests/mat/mattest.py b/tests/mat/mattest.py index b9f0e073..da9398eb 100755 --- a/tests/mat/mattest.py +++ b/tests/mat/mattest.py @@ -1,12 +1,12 @@ #!/usr/bin/env python3 -import struct import os +import struct f = open("mat.mtx", "wb") -for y in range(0, 1024): - for x in range(0, 1024): +for y in range(1024): + for x in range(1024): z = 1 if x >= 200 and y >= 200: z = 2 diff --git a/tests/peak/errtest.py b/tests/peak/errtest.py index 666e8bc5..89b53480 100755 --- a/tests/peak/errtest.py +++ b/tests/peak/errtest.py @@ -30,8 +30,8 @@ # This program can be used to test the handling of spectra with explicit # errors in hdtv. -import ROOT import peakgen +import ROOT spec = peakgen.Spectrum(1024, -0.5, 1023.5) spec.func.background = peakgen.PolyBg([10.0]) diff --git a/tests/peak/peakgen.py b/tests/peak/peakgen.py index 9ff96c8c..54339a16 100644 --- a/tests/peak/peakgen.py +++ b/tests/peak/peakgen.py @@ -23,6 +23,7 @@ import math + import ROOT @@ -54,9 +55,7 @@ def __init__(self, pos, vol, fwhm, tl=None, tr=None, sh=None, sw=None): # Contribution from left tail and left half of truncated Gaussian if self.tl: norm = ( - self.sigma**2 - / self.tl - * math.exp(-self.tl**2 / (2 * self.sigma**2)) + self.sigma**2 / self.tl * math.exp(-(self.tl**2) / (2 * self.sigma**2)) ) norm += ( math.sqrt(math.pi / 2) @@ -69,9 +68,7 @@ def __init__(self, pos, vol, fwhm, tl=None, tr=None, sh=None, sw=None): # Contribution from right tail and right half of truncated Gaussian if self.tr: norm += ( - self.sigma**2 - / self.tr - * math.exp(-self.tr**2 / (2 * self.sigma**2)) + self.sigma**2 / self.tr * math.exp(-(self.tr**2) / (2 * self.sigma**2)) ) norm += ( math.sqrt(math.pi / 2) @@ -122,11 +119,9 @@ def value(self, x): elif dx <= self.eta * self.sigma2: _y = math.exp(-math.log(2) * dx**2 / self.sigma2**2) else: - B = self.sigma2 * self.gamma - 2.0 * self.sigma2 * self.eta**2 * math.log( - 2 - ) + B = self.sigma2 * self.gamma - 2.0 * self.sigma2 * self.eta**2 * math.log(2) B /= 2.0 * self.eta * math.log(2) - A = 2 ** (-self.eta**2) * (self.sigma2 * self.eta + B) ** self.gamma + A = 2 ** (-(self.eta**2)) * (self.sigma2 * self.eta + B) ** self.gamma _y = A / (B + dx) ** self.gamma return _y * self.amp diff --git a/tests/plugins/test_backgroundModels.py b/tests/plugins/test_backgroundModels.py index ba93a722..acdc6999 100644 --- a/tests/plugins/test_backgroundModels.py +++ b/tests/plugins/test_backgroundModels.py @@ -24,34 +24,29 @@ """ import os - -import pytest import xml.etree.ElementTree as ET -from tests.helpers.utils import hdtvcmd, isclose -from tests.helpers.fixtures import temp_file -from tests.helpers.create_test_spectrum import ArtificialSpec, ArtificialSpecProp - +import pytest from numpy import exp, log, sqrt from hdtv.util import monkey_patch_ui +from tests.helpers.create_test_spectrum import ArtificialSpec, ArtificialSpecProp +from tests.helpers.utils import hdtvcmd, isclose monkey_patch_ui() +import __main__ import hdtv.cmdline import hdtv.options import hdtv.session -import __main__ - try: __main__.spectra = hdtv.session.Session() except RuntimeError: pass -from hdtv.plugins.specInterface import spec_interface -from hdtv.plugins.fitInterface import fit_interface from hdtv.plugins.fitlist import fitxml +from hdtv.plugins.specInterface import spec_interface spectra = __main__.spectra @@ -70,7 +65,7 @@ ts_prop = ArtificialSpecProp() -@pytest.fixture +@pytest.fixture() def test_spectrum(tmp_path): ts = ArtificialSpec(path=tmp_path) ts.create() diff --git a/tests/plugins/test_calInterface.py b/tests/plugins/test_calInterface.py index ed159deb..0f2d83c3 100644 --- a/tests/plugins/test_calInterface.py +++ b/tests/plugins/test_calInterface.py @@ -17,37 +17,33 @@ # along with HDTV; if not, write to the Free Software Foundation, # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -import re +import filecmp import os import sys -import filecmp import pytest -from tests.helpers.utils import redirect_stdout, hdtvcmd -from tests.helpers.fixtures import temp_file - from hdtv.util import monkey_patch_ui +from tests.helpers.utils import hdtvcmd monkey_patch_ui() +import __main__ import hdtv.cmdline import hdtv.options import hdtv.session -import __main__ - try: __main__.spectra = hdtv.session.Session() except RuntimeError: pass -from hdtv.plugins.specInterface import spec_interface import hdtv.plugins.calInterface -from hdtv.plugins.fitInterface import fit_interface -import hdtv.plugins.peakfinder import hdtv.plugins.fitmap +import hdtv.plugins.peakfinder +from hdtv.plugins.fitInterface import fit_interface +from hdtv.plugins.specInterface import spec_interface spectra = __main__.spectra diff --git a/tests/plugins/test_calbin.py b/tests/plugins/test_calbin.py index bd98448b..b3432ca7 100644 --- a/tests/plugins/test_calbin.py +++ b/tests/plugins/test_calbin.py @@ -22,34 +22,27 @@ """ import os - +import xml.etree.ElementTree as ET from math import sqrt import pytest -import xml.etree.ElementTree as ET - -from tests.helpers.utils import hdtvcmd, isclose -from tests.helpers.create_test_spectrum import ArtificialSpec, ArtificialSpecProp -from tests.helpers.fixtures import temp_file from hdtv.util import monkey_patch_ui +from tests.helpers.create_test_spectrum import ArtificialSpec +from tests.helpers.utils import hdtvcmd, isclose monkey_patch_ui() +import __main__ import hdtv.cmdline import hdtv.options import hdtv.session - -import __main__ - try: __main__.spectra = hdtv.session.Session() except RuntimeError: pass -from hdtv.plugins.specInterface import spec_interface -from hdtv.plugins.fitInterface import fit_interface from hdtv.plugins.fitlist import fitxml spectra = __main__.spectra @@ -61,7 +54,7 @@ WRITE_BATCHFILE = False # Determines whether the batch files of the test fits should be written to file. -@pytest.fixture +@pytest.fixture() def test_spectrum(tmp_path): ts = ArtificialSpec(path=tmp_path) ts.create() diff --git a/tests/plugins/test_config.py b/tests/plugins/test_config.py index a60c9e9a..838e27c9 100644 --- a/tests/plugins/test_config.py +++ b/tests/plugins/test_config.py @@ -21,14 +21,13 @@ import pytest -from tests.helpers.utils import redirect_stdout, hdtvcmd - import hdtv.cmdline import hdtv.options import hdtv.plugins.config # populate with a few options import hdtv.plugins.dblookup +from tests.helpers.utils import hdtvcmd @pytest.fixture(autouse=True) @@ -42,7 +41,6 @@ def prepare(): "database.sort_reverse", ]: hdtv.options.Reset(variable) - yield def test_cmd_config_show(): diff --git a/tests/plugins/test_dblookup.py b/tests/plugins/test_dblookup.py index c670a334..e02f3758 100644 --- a/tests/plugins/test_dblookup.py +++ b/tests/plugins/test_dblookup.py @@ -22,11 +22,10 @@ import pytest -from tests.helpers.utils import redirect_stdout, hdtvcmd - import hdtv.cmdline import hdtv.options import hdtv.plugins.dblookup +from tests.helpers.utils import hdtvcmd @pytest.fixture(autouse=True) @@ -34,7 +33,6 @@ def prepare(): hdtv.options.Set("table", "classic") hdtv.options.Set("uncertainties", "short") hdtv.options.Set("database.db", "pgaalib_iki2000") - yield def test_cmd_db_info(): diff --git a/tests/plugins/test_fitInterface.py b/tests/plugins/test_fitInterface.py index a7c5ad1d..2b21c77b 100644 --- a/tests/plugins/test_fitInterface.py +++ b/tests/plugins/test_fitInterface.py @@ -17,33 +17,30 @@ # along with HDTV; if not, write to the Free Software Foundation, # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -import re -import os -import sys import copy +import os +import re import pytest -from tests.helpers.utils import redirect_stdout, hdtvcmd - from hdtv.util import monkey_patch_ui +from tests.helpers.utils import hdtvcmd monkey_patch_ui() +import __main__ import hdtv.cmdline import hdtv.options import hdtv.session -import __main__ - try: __main__.spectra = hdtv.session.Session() except RuntimeError: pass -from hdtv.plugins.specInterface import spec_interface -from hdtv.plugins.fitInterface import fit_interface import hdtv.plugins.peakfinder +from hdtv.plugins.fitInterface import fit_interface +from hdtv.plugins.specInterface import spec_interface spectra = __main__.spectra diff --git a/tests/plugins/test_fitmap.py b/tests/plugins/test_fitmap.py index 8a8933a4..5544e0ea 100644 --- a/tests/plugins/test_fitmap.py +++ b/tests/plugins/test_fitmap.py @@ -21,29 +21,27 @@ import pytest -from tests.helpers.utils import redirect_stdout, hdtvcmd - from hdtv.util import monkey_patch_ui +from tests.helpers.utils import hdtvcmd monkey_patch_ui() +import __main__ import hdtv.cmdline import hdtv.options import hdtv.session -import __main__ - try: __main__.spectra = hdtv.session.Session() except RuntimeError: pass -from hdtv.plugins.specInterface import spec_interface import hdtv.plugins.calInterface import hdtv.plugins.fitInterface -import hdtv.plugins.peakfinder import hdtv.plugins.fitmap +import hdtv.plugins.peakfinder +from hdtv.plugins.specInterface import spec_interface spectra = __main__.spectra diff --git a/tests/plugins/test_help.py b/tests/plugins/test_help.py index 875231ba..91400648 100644 --- a/tests/plugins/test_help.py +++ b/tests/plugins/test_help.py @@ -19,18 +19,16 @@ import pytest -from tests.helpers.utils import redirect_stdout, hdtvcmd - from hdtv.util import monkey_patch_ui +from tests.helpers.utils import hdtvcmd monkey_patch_ui() +import __main__ import hdtv.cmdline import hdtv.options -import hdtv.window import hdtv.session - -import __main__ +import hdtv.window try: __main__.spectra = hdtv.session.Session() @@ -38,19 +36,19 @@ pass -import hdtv.plugins.specInterface import hdtv.plugins.calInterface -import hdtv.plugins.fitInterface -import hdtv.plugins.peakfinder -import hdtv.plugins.fitmap import hdtv.plugins.config import hdtv.plugins.dblookup +import hdtv.plugins.fitInterface +import hdtv.plugins.fitlist +import hdtv.plugins.fitmap import hdtv.plugins.fittex import hdtv.plugins.matInterface +import hdtv.plugins.peakfinder +import hdtv.plugins.printing import hdtv.plugins.rootInterface import hdtv.plugins.run -import hdtv.plugins.fitlist -import hdtv.plugins.printing +import hdtv.plugins.specInterface cmdlist = [ "calibration efficiency fit", diff --git a/tests/plugins/test_ls.py b/tests/plugins/test_ls.py index 4fba8716..c9b1a4ac 100644 --- a/tests/plugins/test_ls.py +++ b/tests/plugins/test_ls.py @@ -21,10 +21,8 @@ import pytest -from tests.helpers.utils import redirect_stdout, hdtvcmd - -import hdtv.cmdline -import hdtv.plugins.ls +import hdtv.plugins.ls # noqa: F401 +from tests.helpers.utils import hdtvcmd @pytest.fixture(autouse=True) diff --git a/tests/plugins/test_matInterface.py b/tests/plugins/test_matInterface.py index f8949fcf..b283e5da 100644 --- a/tests/plugins/test_matInterface.py +++ b/tests/plugins/test_matInterface.py @@ -19,25 +19,21 @@ import pytest -from tests.helpers.utils import redirect_stdout, hdtvcmd - from hdtv.util import monkey_patch_ui monkey_patch_ui() +import __main__ import hdtv.cmdline import hdtv.options import hdtv.session -import __main__ - try: if not hasattr(__main__, "spectra"): __main__.spectra = hdtv.session.Session() except RuntimeError: pass -from hdtv.plugins.specInterface import spec_interface import hdtv.plugins.matInterface spectra = __main__.spectra diff --git a/tests/plugins/test_printing.py b/tests/plugins/test_printing.py index 7c09e303..10836da7 100644 --- a/tests/plugins/test_printing.py +++ b/tests/plugins/test_printing.py @@ -22,19 +22,16 @@ import pytest -from tests.helpers.utils import redirect_stdout, hdtvcmd - from hdtv.util import monkey_patch_ui +from tests.helpers.utils import hdtvcmd monkey_patch_ui() +import __main__ import hdtv.cmdline import hdtv.options -import hdtv.ui import hdtv.session - - -import __main__ +import hdtv.ui try: __main__.spectra = hdtv.session.Session() diff --git a/tests/plugins/test_rootInterface.py b/tests/plugins/test_rootInterface.py index 05655a24..95ad4473 100644 --- a/tests/plugins/test_rootInterface.py +++ b/tests/plugins/test_rootInterface.py @@ -20,15 +20,14 @@ import os import pytest -from tests.helpers.utils import redirect_stdout, hdtvcmd, isclose, setup_io from hdtv.util import monkey_patch_ui +from tests.helpers.utils import hdtvcmd, isclose monkey_patch_ui() -import hdtv.session - import __main__ +import hdtv.session try: __main__.spectra = hdtv.session.Session() @@ -36,13 +35,12 @@ pass session = __main__.spectra -import hdtv.plugins.specInterface -from hdtv.plugins.fitInterface import fit_interface -import hdtv.plugins.rootInterface - import hdtv.cmdline import hdtv.options +import hdtv.plugins.rootInterface +import hdtv.plugins.specInterface import hdtv.rfile_utils +from hdtv.plugins.fitInterface import fit_interface @pytest.fixture(autouse=True) diff --git a/tests/plugins/test_run.py b/tests/plugins/test_run.py index 21b942ad..ab117246 100644 --- a/tests/plugins/test_run.py +++ b/tests/plugins/test_run.py @@ -19,11 +19,7 @@ import pytest -from tests.helpers.utils import redirect_stdout, hdtvcmd - -import hdtv.cmdline -import hdtv.plugins.run -import hdtv.plugins.ls +from tests.helpers.utils import hdtvcmd @pytest.mark.parametrize( diff --git a/tests/plugins/test_specInterface.py b/tests/plugins/test_specInterface.py index fca33f83..4ddcbd06 100644 --- a/tests/plugins/test_specInterface.py +++ b/tests/plugins/test_specInterface.py @@ -17,28 +17,23 @@ # along with HDTV; if not, write to the Free Software Foundation, # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA -import re import os -import sys +import re import warnings -import shutil from math import ceil import pytest -from tests.helpers.utils import redirect_stdout, hdtvcmd -from tests.helpers.fixtures import temp_file - from hdtv.util import monkey_patch_ui +from tests.helpers.utils import hdtvcmd monkey_patch_ui() +import __main__ import hdtv.cmdline import hdtv.options import hdtv.session -import __main__ - try: __main__.spectra = hdtv.session.Session() except RuntimeError: @@ -226,8 +221,8 @@ def test_cmd_spectrum_activate_multi(numspecs): if numspecs > 1: assert "Can only activate one spectrum" in ferr else: - assert "" == ferr - assert "" == f + assert ferr == "" + assert f == "" def test_cmd_spectrum_info(): @@ -275,7 +270,7 @@ def test_cmd_spectrum_rebin(ngroup): hdtvcmd(f"spectrum get {testspectrum}") assert len(s.spectra.dict) == 1 - with warnings.catch_warnings(record=True) as w: + with warnings.catch_warnings(record=True): f, ferr = hdtvcmd(f"spectrum rebin 0 {ngroup}") assert f"Rebinning 0 with {ngroup} bins per new bin" in f assert get_spec(0).hist.hist.GetNbinsX() == 8192 // ngroup @@ -287,7 +282,7 @@ def test_cmd_spectrum_calbin_binsize(binsize): hdtvcmd(f"spectrum get {testspectrum}") assert len(s.spectra.dict) == 1 - with warnings.catch_warnings(record=True) as w: + with warnings.catch_warnings(record=True): f, ferr = hdtvcmd(f"spectrum calbin -b {binsize} 0") assert f"Calbinning 0 with binsize={binsize}" in f assert get_spec(0).hist.hist.GetNbinsX() == int(ceil(8191 / binsize)) + 1 @@ -299,7 +294,7 @@ def test_cmd_spectrum_calbin_root_tv(parameter): hdtvcmd(f"spectrum get {testspectrum}") assert len(s.spectra.dict) == 1 - with warnings.catch_warnings(record=True) as w: + with warnings.catch_warnings(record=True): f, ferr = hdtvcmd(f"spectrum calbin {parameter} 0") assert "Calbinning 0 with binsize=" in f @@ -310,7 +305,7 @@ def test_cmd_spectrum_calbin_spline_order(spline_order): hdtvcmd(f"spectrum get {testspectrum}") assert len(s.spectra.dict) == 1 - with warnings.catch_warnings(record=True) as w: + with warnings.catch_warnings(record=True): f, ferr = hdtvcmd(f"spectrum calbin -k {spline_order} 0") assert "Calbinning 0 with" in f @@ -319,9 +314,9 @@ def test_cmd_spectrum_calbin_calibrated(): assert len(s.spectra.dict) == 0 hdtvcmd(f"spectrum get {testspectrum}") assert len(s.spectra.dict) == 1 - hdtvcmd(f"calibration position set 5 2.2") + hdtvcmd("calibration position set 5 2.2") - with warnings.catch_warnings(record=True) as w: + with warnings.catch_warnings(record=True): f, ferr = hdtvcmd("spectrum calbin -d 0") assert "Calbinning 0 with" in f @@ -331,7 +326,7 @@ def test_cmd_spectrum_resample(): hdtvcmd(f"spectrum get {testspectrum}") assert len(s.spectra.dict) == 1 - with warnings.catch_warnings(record=True) as w: + with warnings.catch_warnings(record=True): f, ferr = hdtvcmd("spectrum resample 0") assert "Total area changed by" in f diff --git a/tests/ui/test_table.py b/tests/ui/test_table.py index 41e08313..a3da0146 100755 --- a/tests/ui/test_table.py +++ b/tests/ui/test_table.py @@ -1,8 +1,8 @@ #!/usr/bin/env python3 -from hdtv.ui import * import hdtv.database +from hdtv.ui import Table db = hdtv.database.PGAALibraries.PGAAlib_IKI2000()