From 8c47685a174aab2c0fb77f27cd35cfb29de09f4b Mon Sep 17 00:00:00 2001 From: Andrej Rode Date: Wed, 17 May 2023 14:49:27 +0200 Subject: [PATCH] Fix all flake8 issues --- .flake8 | 4 +++ pyproject.toml | 33 +++++++++++----------- src/mokka/channels/torch.py | 22 +++++++++------ src/mokka/inft/torch.py | 1 - src/mokka/mapping/torch.py | 17 +++++++----- src/mokka/normalization/__init__.py | 4 +-- src/mokka/pulseshaping/__init__.py | 2 +- src/mokka/pulseshaping/torch.py | 38 +++++++++++++------------- src/mokka/utils/generators/__init__.py | 4 +-- src/mokka/utils/generators/torch.py | 2 +- tests/mokka/test_channels.py | 9 +++--- tests/mokka/test_e2e.py | 2 +- tests/mokka/test_equalizers.py | 2 +- tests/mokka/test_pulseshaping.py | 2 +- tests/mokka/test_synchronizers.py | 2 +- 15 files changed, 78 insertions(+), 66 deletions(-) create mode 100644 .flake8 diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..bec1e3c --- /dev/null +++ b/.flake8 @@ -0,0 +1,4 @@ +[flake8] +max-line-length = 88 +extend-ignore = E203 +#max-complexity = 10 diff --git a/pyproject.toml b/pyproject.toml index 97ba520..0ab9e88 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,7 +14,8 @@ license = {file = "COPYING"} authors = [ {name = "Andrej Rode", email = "rode@kit.edu"}, {name = "Shrinivas Chimmalgi", email="shrinivas.chimmalgi@kit.edu"}, - {name = "Benedikt Geiger", email="benedikt.geiger@kit.edu"} + {name = "Benedikt Geiger", email="benedikt.geiger@kit.edu"}, + {name = "Laurent Schmalen"} ] dependencies = [ @@ -43,7 +44,6 @@ dev = [ "pytest", "black", "flake8", - "pyproject-flake8", "pylint", "python-lsp-server[all]", "pyls-flake8", @@ -55,11 +55,6 @@ remotelog = [ "wandb", ] -[tool.flake8] -max-line-length = 88 -extend-ignore = "E203," -max-complexity = 10 - [tool.pytest.ini_options] addopts = [ "--import-mode=importlib", @@ -103,8 +98,9 @@ legacy_tox_ini = """ [tox] description = list of environments against which tox runs the tests envlist = - reformat - check + flake8 + black + pydocstyle py3-fast py37 py38 @@ -121,7 +117,7 @@ python = 3.7: py37 3.8: py38 3.9: py39 - 3.10: reformat, check, py310 + 3.10: flake8, black, pydocstyle, py310 [testenv] # install pytest in the virtualenv where commands will be executed @@ -139,20 +135,25 @@ usedevelop = False commands = pytest {env:PYTEST_MARKERS:} {env:PYTEST_EXTRA_ARGS:} {posargs:-vv} -[testenv:check] +[testenv:flake8] description = this environments checks for flake8 and black deps = black==22.3.0 - pyproject-flake8 flake8 build skip_install = true commands = - pflake8 src tests - black --check --diff src tests - python -m build + flake8 src tests + +[testenv:pydocstyle] +description = this environments checks formatting with pydocstyle +deps = + pydocstyle +skip_install = true +commands = + pydocstyle --count src/ -[testenv:reformat] +[testenv:black] description = reformats the code using black deps = black==22.3.0 diff --git a/src/mokka/channels/torch.py b/src/mokka/channels/torch.py index 62584d2..5fd5c7d 100644 --- a/src/mokka/channels/torch.py +++ b/src/mokka/channels/torch.py @@ -570,7 +570,7 @@ def nonlinear_operator(u): return (h_dz, h_dz_2, nonlinear_operator) - def forward(self, u): + def forward(self, u): # noqa: C901 """ :param u0: input signal (array) :returns: array signal at the end of the fiber @@ -636,7 +636,8 @@ def forward(self, u): z = z + 2 * dz if iter >= self.maxiter: logger.info( - "WARNING: Failed to converge to goal local error of %s in %s iterations", + "WARNING: Failed to converge to goal local error \ + of %s in %s iterations", self.delta_G, self.maxiter, ) @@ -663,9 +664,10 @@ class SSFMPropagationDualPol(torch.nn.Module): This class is adapted from Code written by Dominik Rimpf published under the MIT license [https://gitlab.com/domrim/bachelorarbeit-code] - This class solves the coupled nonlinear Schrodinger equation for pulse propagation in an - optical fiber using the split-step Fourier method. - The implementation and name are based on https://photonics.umd.edu/software/ssprop/vector-version/ + This class solves the coupled nonlinear Schrodinger equation for pulse propagation + in an optical fiber using the split-step Fourier method. + The implementation and name are based on + https://photonics.umd.edu/software/ssprop/vector-version/ :param dt: time step between samples (sample time) :param dz: propagation stepsize (delta z) of SSFM (segment length) @@ -679,8 +681,11 @@ class SSFMPropagationDualPol(torch.nn.Module): length :param delta_G: Goal local error (optional) (default 1e-3) :param maxiter: Number of step-size iterations (optional) (default 4) - :param psp: Principal eigenstate of the fiber specified as a 2-vector containing the angles Psi and Chi (optional) (default [0,0]) - :param solution_method: String that specifies which method to use when performing the split-step calculations. Supported methods elliptical and circular (optional) (default elliptical) + :param psp: Principal eigenstate of the fiber specified as a 2-vector containing + the angles Psi and Chi (optional) (default [0,0]) + :param solution_method: String that specifies which method to use when performing + the split-step calculations. Supported methods elliptical + and circular (optional) (default elliptical) """ @@ -939,7 +944,8 @@ def forward(self, ux, uy): if iter >= self.maxiter: logger.info( - "WARNING: Failed to converge to goal local error of %s in %s iterations", + "WARNING: Failed to converge to goal local error \ + of %s in %s iterations", self.delta_G, self.maxiter, ) diff --git a/src/mokka/inft/torch.py b/src/mokka/inft/torch.py index 2e838ec..f78f4a5 100644 --- a/src/mokka/inft/torch.py +++ b/src/mokka/inft/torch.py @@ -48,7 +48,6 @@ def BMI(m, N, b_ij, L_ij, p=None): return BMI - def MI(M, PX, N, symbol_idx, Q_YX): """ Calculate the mutual information. diff --git a/src/mokka/mapping/torch.py b/src/mokka/mapping/torch.py index b7f0e2c..c9ab837 100644 --- a/src/mokka/mapping/torch.py +++ b/src/mokka/mapping/torch.py @@ -474,7 +474,8 @@ class ClassicalDemapper(torch.nn.Module): """ Classical Bitwise Soft Demapper with Gaussian Noise assumption. It takes the noise_sigma and constellation sorted from [0,2**m -1] and constructs - an more or less efficient demapper which outputs LLRS for all m bits for each received symbol + an more or less efficient demapper which outputs LLRS for all m bits for + each received symbol """ def __init__(self, noise_sigma, constellation, optimize=False): @@ -550,8 +551,9 @@ def symbolwise(self, y, *args): class GaussianDemapper(torch.nn.Module): """ Classical Bitwise Soft Demapper with Gaussian Noise assumption. - Learns bias and covariance matrix of noise for constellation sorted from [0,2**m -1] and constructs - an more or less efficient demapper which outputs LLRS for all m bits for each received symbol + Learns bias and covariance matrix of noise for constellation sorted from [0,2**m -1] + and constructs a more or less efficient demapper which outputs LLRS for all m bits + for each received symbol """ def __init__(self, constellation): @@ -637,7 +639,6 @@ def warm_start(self, sym_idx, y, learning_fraction=0.3, single_circular=False): ] rx_sym = y[idx].squeeze() if y.dtype == torch.complex64: - dim = 2 rx_sym = torch.stack( (torch.real(rx_sym), torch.imag(rx_sym)) ).squeeze() @@ -750,9 +751,10 @@ def forward(self, y, *args): class SeparatedSimpleDemapper(torch.nn.Module): """ - Simplified Demapper which shall approximates output LLRS of bits separated in real and imag + Simplified Demapper which shall approximates output LLRS of bits separated in real + and imaginary parts - Do simple function approximation with in -> Linear -> ReLU -> Linear -> out operation + Do simple function approximation with in -> Linear -> ReLU -> Linear -> out This should be easy to replicate in hardware with LUT """ @@ -888,7 +890,8 @@ def p_symbols(self, *args): class MBPCSSampler(torch.nn.Module): """ - This class is supposed to use NNs to find a lambda [0,1] for each given parameter (if given parameters) and then return p_symbols for further simulation + This class is supposed to use NNs to find a lambda [0,1] for each given parameter + (if given parameters) and then return p_symbols for further simulation """ def __init__( diff --git a/src/mokka/normalization/__init__.py b/src/mokka/normalization/__init__.py index 6c598e1..216d3be 100644 --- a/src/mokka/normalization/__init__.py +++ b/src/mokka/normalization/__init__.py @@ -1,3 +1,3 @@ -from . import torch +from . import torch # noqa -from .torch import energy, centered_energy +from .torch import energy, centered_energy # noqa diff --git a/src/mokka/pulseshaping/__init__.py b/src/mokka/pulseshaping/__init__.py index abef05a..86fbede 100644 --- a/src/mokka/pulseshaping/__init__.py +++ b/src/mokka/pulseshaping/__init__.py @@ -1 +1 @@ -from . import torch +from . import torch # noqa diff --git a/src/mokka/pulseshaping/torch.py b/src/mokka/pulseshaping/torch.py index a81fbb3..9ff0df3 100644 --- a/src/mokka/pulseshaping/torch.py +++ b/src/mokka/pulseshaping/torch.py @@ -10,8 +10,8 @@ @attr.define class PulseShaping(torch.nn.Module): """ - PulseShaping filter class which provides several classical shapes, a filter function and a - matched filter function. + PulseShaping filter class which provides several classical shapes, a filter function + and a matched filter function. """ _impulse_response: torch.tensor @@ -80,15 +80,15 @@ def get_rc_ir(cls, syms, r, n_up, learnable=False): with torch.no_grad(): # initialize output length and sample time T_symbol = 1.0 - T_sample = ( - 1.0 / n_up - ) # length of one sample is the symbol-duration divided by the oversampling factor (=1/sampling rate) - T_ir = ( - 2 * syms * T_symbol - ) # Duration of the impulse response is positive and negative normed symbols added multplied by Symbol Duration - ir = torch.zeros( - int(T_ir / T_sample) + 1 - ) # samples of impulse response is definied by duration of the ir divided by the sample time plus one for the 0th sample + T_sample = 1.0 / n_up + # length of one sample is the symbol-duration divided + # by the oversampling factor (=1/sampling rate) + T_ir = 2 * syms * T_symbol + # Duration of the impulse response is positive and negative + # normed symbols added multplied by Symbol Duration + ir = torch.zeros(int(T_ir / T_sample) + 1) + # samples of impulse response is definied by duration + # of the ir divided by the sample time plus one for the 0th sample # time indices and sampled time k_steps = torch.arange( @@ -121,8 +121,8 @@ def get_rc_ir(cls, syms, r, n_up, learnable=False): def get_rrc_ir(cls, syms, r, n_up, learnable=False): """Determines normed coefficients of an RRC filter - This function is adapted from Sourcecode written by Dominik Rimpf and was published under the - MIT license [https://gitlab.com/domrim/bachelorarbeit-code] + This function is adapted from Sourcecode written by Dominik Rimpf and was + published under the MIT license [https://gitlab.com/domrim/bachelorarbeit-code] Formula out of: K.-D. Kammeyer, Nachrichtenübertragung At poles, l'Hospital was used @@ -138,12 +138,12 @@ def get_rrc_ir(cls, syms, r, n_up, learnable=False): # initialize output length and sample time T_symbol = 1.0 T_sample = 1 / n_up - T_ir = ( - 2 * syms * T_symbol - ) # Duration of the impulse response is positive and negative normed symbols added multplied by Symbol Duration - ir = torch.zeros( - int(T_ir / T_sample) + 1 - ) # samples of impulse response is definied by duration of the ir divided by the sample time plus one for the 0th sample + T_ir = 2 * syms * T_symbol + # Duration of the impulse response is positive and negative + # normed symbols added multplied by Symbol Duration + ir = torch.zeros(int(T_ir / T_sample) + 1) + # samples of impulse response is definied by duration of + # the ir divided by the sample time plus one for the 0th sample # time indices and sampled time k_steps = torch.arange( diff --git a/src/mokka/utils/generators/__init__.py b/src/mokka/utils/generators/__init__.py index 6902229..4e358a4 100644 --- a/src/mokka/utils/generators/__init__.py +++ b/src/mokka/utils/generators/__init__.py @@ -1,6 +1,6 @@ -from . import numpy +from . import numpy # noqa try: - from . import torch + from . import torch # noqa except ImportError: pass diff --git a/src/mokka/utils/generators/torch.py b/src/mokka/utils/generators/torch.py index aaa3285..1e035de 100644 --- a/src/mokka/utils/generators/torch.py +++ b/src/mokka/utils/generators/torch.py @@ -1 +1 @@ -import torch +import torch # noqa diff --git a/tests/mokka/test_channels.py b/tests/mokka/test_channels.py index 94960a3..45cfd8e 100644 --- a/tests/mokka/test_channels.py +++ b/tests/mokka/test_channels.py @@ -54,9 +54,9 @@ def test_residualphasenoise(): def test_opticalnoise(): - wavelength_bw = 0.1 # nm - optical_snr = 25 # dB - channel = channels.OpticalNoise(dt, wavelength, wavelength_bw , optical_snr) + wavelength_bw = 0.1 # nm + optical_snr = 25 # dB + channel = channels.OpticalNoise(dt, wavelength, wavelength_bw, optical_snr) x = torch.zeros(num_samples, dtype=torch.complex64) y = channel(x) assert len(y) == num_samples @@ -103,9 +103,8 @@ def test_SSFM_singlepol(): assert len(y) == num_samples - def test_SSFM_dualpol(): - amp = channels.EDFAAmpDualPol( + amp = channels.EDFAAmpDualPol( # noqa span_length, amp_gain, alphaa_db, diff --git a/tests/mokka/test_e2e.py b/tests/mokka/test_e2e.py index f5f586a..944782e 100644 --- a/tests/mokka/test_e2e.py +++ b/tests/mokka/test_e2e.py @@ -1 +1 @@ -import mokka.e2e.torch as e2e +import mokka.e2e.torch as e2e # noqa diff --git a/tests/mokka/test_equalizers.py b/tests/mokka/test_equalizers.py index 9871527..2f4b9ac 100644 --- a/tests/mokka/test_equalizers.py +++ b/tests/mokka/test_equalizers.py @@ -1 +1 @@ -import mokka.equalizers.torch as equalizers +import mokka.equalizers.torch as equalizers # noqa diff --git a/tests/mokka/test_pulseshaping.py b/tests/mokka/test_pulseshaping.py index 4fcc4b7..56d3861 100644 --- a/tests/mokka/test_pulseshaping.py +++ b/tests/mokka/test_pulseshaping.py @@ -1 +1 @@ -import mokka.pulseshaping.torch as pulseshape +import mokka.pulseshaping.torch as pulseshape # noqa diff --git a/tests/mokka/test_synchronizers.py b/tests/mokka/test_synchronizers.py index 6b03519..4f70e3f 100644 --- a/tests/mokka/test_synchronizers.py +++ b/tests/mokka/test_synchronizers.py @@ -1 +1 @@ -import mokka.synchronizers.phase.torch as phase +import mokka.synchronizers.phase.torch as phase # noqa