Skip to content

Commit

Permalink
Merge pull request #23 from CarlosBergillos/develop
Browse files Browse the repository at this point in the history
Use C++ and refactor CLI
  • Loading branch information
CarlosBergillos authored Apr 10, 2023
2 parents 0d1dc60 + 74c9863 commit 8f10823
Show file tree
Hide file tree
Showing 17 changed files with 281 additions and 179 deletions.
11 changes: 11 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
Changelog
=========

**1.2.2**
---------

*10-04-2023*

Highlights:

+ Fixed and refactored CLI application.
+ Switched to C++ for Cython files and now using native C++ queue class.


**1.2.1**
---------

Expand Down
3 changes: 0 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ def main():

# fmt: off
extensions = [
Extension('ts2vg.utils.pairqueue',
[f'ts2vg/utils/pairqueue.pyx']),

Extension('ts2vg.graph._base',
[f'ts2vg/graph/_base.pyx'],
include_dirs=include_dirs,
Expand Down
18 changes: 14 additions & 4 deletions tests/naive_implementations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""


def natural_visibility_graph(ts, xs):
def natural_visibility_graph(ts, xs, penetrable_limit=0):
n = len(ts)
edges = []

Expand All @@ -18,19 +18,24 @@ def natural_visibility_graph(ts, xs):
x_b = xs[i_b]
y_b = ts[i_b]

penetrations = 0

for i_c in range(i_a + 1, i_b):
x_c = xs[i_c]
y_c = ts[i_c]

if y_c >= y_b + (y_a - y_b) * (x_b - x_c) / (x_b - x_a):
break
penetrations += 1

if penetrations > penetrable_limit:
break
else:
edges.append((i_a, i_b))

return edges


def horizontal_visibility_graph(ts, xs):
def horizontal_visibility_graph(ts, xs, penetrable_limit=0):
n = len(ts)
edges = []

Expand All @@ -42,12 +47,17 @@ def horizontal_visibility_graph(ts, xs):
x_b = xs[i_b]
y_b = ts[i_b]

penetrations = 0

for i_c in range(i_a + 1, i_b):
x_c = xs[i_c]
y_c = ts[i_c]

if y_c >= min(y_a, y_b):
break
penetrations += 1

if penetrations > penetrable_limit:
break
else:
edges.append((i_a, i_b))

Expand Down
64 changes: 55 additions & 9 deletions tests/test_horizontal_penetrable.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,9 @@
import pytest
from pytest import approx

from fixtures import *
import ts2vg
from fixtures import (
empty_ts,
flat_ts,
sample_ts,
sample_ts_2,
linear_ts_small,
linear_ts_large,
linear_ts_large_negative,
)
from naive_implementations import horizontal_visibility_graph as naive_hvg


def test_negative_parametric(sample_ts):
Expand Down Expand Up @@ -117,6 +110,59 @@ def test_penetrable_3(sample_ts):
assert sorted(sorted(e) for e in out_got) == sorted(sorted(e) for e in out_truth)



def test_penetrable_1_white_noise(white_noise_ts):
ts = white_noise_ts
xs = list(range(len(ts)))

vg = ts2vg.HorizontalVG(penetrable_limit=1)

out_got = vg.build(ts, xs).edges

out_truth = naive_hvg(ts, xs, penetrable_limit=1)

assert sorted(sorted(e) for e in out_got) == sorted(sorted(e) for e in out_truth)


def test_penetrable_3_white_noise(white_noise_ts):
ts = white_noise_ts
xs = list(range(len(ts)))

vg = ts2vg.HorizontalVG(penetrable_limit=3)

out_got = vg.build(ts, xs).edges

out_truth = naive_hvg(ts, xs, penetrable_limit=3)

assert sorted(sorted(e) for e in out_got) == sorted(sorted(e) for e in out_truth)


def test_penetrable_1_brownian_motion(brownian_motion_ts):
ts = brownian_motion_ts
xs = list(range(len(ts)))

vg = ts2vg.HorizontalVG(penetrable_limit=1)

out_got = vg.build(ts, xs).edges

out_truth = naive_hvg(ts, xs, penetrable_limit=1)

assert sorted(sorted(e) for e in out_got) == sorted(sorted(e) for e in out_truth)


def test_penetrable_3_brownian_motion(brownian_motion_ts):
ts = brownian_motion_ts
xs = list(range(len(ts)))

vg = ts2vg.HorizontalVG(penetrable_limit=3)

out_got = vg.build(ts, xs).edges

out_truth = naive_hvg(ts, xs, penetrable_limit=3)

assert sorted(sorted(e) for e in out_got) == sorted(sorted(e) for e in out_truth)


def test_penetrable_1_ltr(sample_ts):
vg = ts2vg.HorizontalVG(directed="left_to_right", penetrable_limit=1)
out_got = vg.build(sample_ts).edges
Expand Down
63 changes: 54 additions & 9 deletions tests/test_natural_penetrable.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,9 @@
import pytest
from pytest import approx

from fixtures import *
import ts2vg
from fixtures import (
empty_ts,
flat_ts,
sample_ts,
sample_ts_2,
linear_ts_small,
linear_ts_large,
linear_ts_large_negative,
)
from naive_implementations import natural_visibility_graph as naive_nvg


def test_negative_parametric(sample_ts):
Expand Down Expand Up @@ -128,6 +121,58 @@ def test_penetrable_3(sample_ts):
assert sorted(sorted(e) for e in out_got) == sorted(sorted(e) for e in out_truth)


def test_penetrable_1_white_noise(white_noise_ts):
ts = white_noise_ts
xs = list(range(len(ts)))

vg = ts2vg.NaturalVG(penetrable_limit=1)

out_got = vg.build(ts, xs).edges

out_truth = naive_nvg(ts, xs, penetrable_limit=1)

assert sorted(sorted(e) for e in out_got) == sorted(sorted(e) for e in out_truth)


def test_penetrable_3_white_noise(white_noise_ts):
ts = white_noise_ts
xs = list(range(len(ts)))

vg = ts2vg.NaturalVG(penetrable_limit=3)

out_got = vg.build(ts, xs).edges

out_truth = naive_nvg(ts, xs, penetrable_limit=3)

assert sorted(sorted(e) for e in out_got) == sorted(sorted(e) for e in out_truth)


def test_penetrable_1_brownian_motion(brownian_motion_ts):
ts = brownian_motion_ts
xs = list(range(len(ts)))

vg = ts2vg.NaturalVG(penetrable_limit=1)

out_got = vg.build(ts, xs).edges

out_truth = naive_nvg(ts, xs, penetrable_limit=1)

assert sorted(sorted(e) for e in out_got) == sorted(sorted(e) for e in out_truth)


def test_penetrable_3_brownian_motion(brownian_motion_ts):
ts = brownian_motion_ts
xs = list(range(len(ts)))

vg = ts2vg.NaturalVG(penetrable_limit=3)

out_got = vg.build(ts, xs).edges

out_truth = naive_nvg(ts, xs, penetrable_limit=3)

assert sorted(sorted(e) for e in out_got) == sorted(sorted(e) for e in out_truth)


def test_penetrable_1_ltr(sample_ts):
vg = ts2vg.NaturalVG(directed="left_to_right", penetrable_limit=1)
out_got = vg.build(sample_ts).edges
Expand Down
2 changes: 1 addition & 1 deletion ts2vg/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.2.1"
__version__ = "1.2.2"
Loading

0 comments on commit 8f10823

Please sign in to comment.