Skip to content

Commit

Permalink
code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
milescsmith committed Nov 6, 2023
1 parent 984e2a3 commit 7e9f15e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

$ [1.4.1]

## Changed

- Code cleaning.

* [1.4.0]

## Changed
Expand Down Expand Up @@ -71,6 +77,7 @@ Version bump for pypi
- `main` function, as it was unused and `pysmooth` is not intended to be a cli
application

[1.4.1]: https://github.com/olivierlacan/keep-a-changelog/compare/1.4.1...1.4.1
[1.4.0]: https://github.com/olivierlacan/keep-a-changelog/compare/1.3.0...1.4.0
[1.3.0]: https://github.com/olivierlacan/keep-a-changelog/compare/1.2.0...1.3.0
[1.2.0]: https://github.com/olivierlacan/keep-a-changelog/compare/1.1.3...1.2.0
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pysmooth"
version = "1.3.0"
version = "1.4.1"
description = "Pysmooth: a Python implementation of R's stats::smooth Tukey's (running median) smoother"
authors = ["Miles Smith <[email protected]>"]
license = "GPL-3.0"
Expand Down
6 changes: 3 additions & 3 deletions src/pysmooth/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ def smooth(
if kind.startswith("3RS") and not do_ends:
iend = -iend
elif kind == "S":
iend = int(bool(do_ends))
iend = int(do_ends)

n: int = len(x)
y: list[float] = np.zeros(n, dtype=np.float64).tolist()
split_ends = True if iend < 0 else False

if kind != "S":
z: list[float] = np.zeros(n, dtype=np.float64).tolist()
w: list[float] = np.zeros(n, dtype=np.float64).tolist()
split_ends = iend < 0

if kind == "3RS3R":
_, x, y, z, w = sm_3RS3R(x, y, z, w, n, abs(iend), split_ends)
elif kind == "3RSS":
Expand Down
31 changes: 12 additions & 19 deletions src/pysmooth/_smooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def med3(u: float, v: float, w: float) -> float:
float
median of u, v, w
"""
if (u <= v and v <= w) or (u >= v and v >= w):
if u <= v <= w or u >= v >= w:
return v
elif (u <= w and w <= v) or (u >= w and w >= v):
elif u <= w <= v or u >= w >= v:
return w
else:
return u
Expand All @@ -52,9 +52,9 @@ def imed3(u: float, v: float, w: float) -> int:
int
-1 if u, 0 if v, or 1 if w is the median
"""
if (u <= v and v <= w) or (u >= v and v >= w):
if u <= v <= w or u >= v >= w:
return 0
elif (u <= w and w <= v) or (u >= w and w >= v):
elif u <= w <= v or u >= w >= v:
return 1
else:
return -1
Expand Down Expand Up @@ -126,10 +126,7 @@ def sm_3R( # noqa N802
if n > 2:
chg, y, x = sm_do_endrule(y, x, n, chg, end_rule)

if it:
return it, x, y, z
else:
return int(chg), x, y, z
return (it, x, y, z) if it else (int(chg), x, y, z)


def sptest(x: list[float], i: int) -> bool:
Expand Down Expand Up @@ -167,18 +164,14 @@ def sm_split3(
if sptest(x, i):
# plateau at x[i] == x[i+1]
# at left :
if -1 < (j := imed3(x[i], x[i - 1], 3 * x[i - 1] - 2 * x[i - 2])):
if j == 0:
y[i] = x[i - 1]
else:
y[i] = 3 * x[i - 1] - 2 * x[i - 2]
if (j := imed3(x[i], x[i - 1], 3 * x[i - 1] - 2 * x[i - 2])) > -1:
y[i] = x[i - 1] if j == 0 else 3 * x[i - 1] - 2 * x[i - 2]
chg = y[i] != x[i]
# at right :
if -1 < (j := imed3(x[i + 1], x[i + 2], 3 * x[i + 2] - 2 * x[i + 3])):
if j == 0:
y[i + 1] = x[i + 2]
else:
y[i + 1] = 3 * x[i + 2] - 2 * x[i + 3]
if (
j := imed3(x[i + 1], x[i + 2], 3 * x[i + 2] - 2 * x[i + 3])
) > -1:
y[i + 1] = x[i + 2] if j == 0 else 3 * x[i + 2] - 2 * x[i + 3]
chg = y[i + 1] != x[i + 1]
if do_ends and sptest(x, n - 3):
chg = True
Expand Down Expand Up @@ -246,7 +239,7 @@ def sm_3RSR( # noqa 802

it, x, y, z = sm_3R(x, y, z, n, end_rule)
chg: bool = True
while chg is True:
while chg:
it += 1
chg, y, z = sm_split3(y, z, n, split_ends)
ch2, z, y, w = sm_3R(z, y, w, n, end_rule)
Expand Down

0 comments on commit 7e9f15e

Please sign in to comment.