Skip to content
Open

Ci #1

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5cc3f76
Stuff as I got it from the previous postdoc
dpshelio Sep 14, 2018
74aa837
Include a regression test
dpshelio Sep 14, 2018
e34d2ec
meaningful variable names
dpshelio Sep 14, 2018
4627da8
Update test to the new function name
dpshelio Sep 14, 2018
d115866
Add partial overlap test
dpshelio Sep 14, 2018
76a5fba
Add tests corner overlap... :warning: it fail!
dpshelio Sep 14, 2018
2bd4595
Fix inconsistency between plotting and area calculation
dpshelio Sep 14, 2018
1846b51
Make the limits in the plot as max value + 1
dpshelio Sep 14, 2018
106dd5a
test edge touching
dpshelio Sep 14, 2018
ac2a787
test 2 opposite edges touching
dpshelio Sep 14, 2018
6a7179f
Outside edge test
dpshelio Sep 15, 2018
1f5086f
Test when they are not overlapping :warning: test fails!
dpshelio Sep 15, 2018
edcbc51
Fix the tests - even when they are not touching
dpshelio Sep 15, 2018
2f80d58
Test floats - it seems to work!
dpshelio Sep 15, 2018
0b01aa6
Found a problem on floating points - :warning: test fails
dpshelio Sep 15, 2018
d19b2c4
Fixed the test for floats!
dpshelio Sep 15, 2018
305f471
Testing negative numbers - :warning: it fails!!
dpshelio Sep 15, 2018
b09dfea
New test with negative values... this works!
dpshelio Sep 15, 2018
ba9a5ba
Throw error if coordinates are in wrong order
dpshelio Sep 15, 2018
10f2d13
Add documentation on how to use overlap_area
dpshelio Sep 15, 2018
8ba79ce
Change negative values test to a negative test! It works!
dpshelio Sep 15, 2018
d034fc6
Add coverage settings
dpshelio Sep 15, 2018
311b17a
Example to test using doctest
dpshelio Sep 15, 2018
c6cf15c
Add doctest to pytest
dpshelio Sep 15, 2018
0833b70
Adds configuration to run in Travis
dpshelio Sep 17, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: python
python:
- "3.6"
- "3.7-dev" # 3.7 development branch
before_install:
- cd fields_overlap
# command to install dependencies
install:
- pip install -r requirements.txt
# command to run tests
script:
- pytest
65 changes: 65 additions & 0 deletions fields_overlap/overlap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches

def show_fields(field1, field2):
def vertices(left, bottom, right, top):
verts = [(left, bottom), (left, top), (right, top), (right, bottom), (left, bottom)]
return verts

codes = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY]
path1 = Path(vertices(*field1), codes)
path2 = Path(vertices(*field2), codes)
fig = plt.figure()
ax = fig.add_subplot(111)
patch1 = patches.PathPatch(path1, facecolor='orange', lw=2)
patch2 = patches.PathPatch(path2, facecolor='blue', lw=2)
ax.add_patch(patch1)
ax.add_patch(patch2)
ax.set_xlim(0, max(*field1, *field2) + 1)
ax.set_ylim(0, max(*field1, *field2) + 1)
fig.show()


def overlap_area(field1, field2):
'''
Calculates the area of overlapping fields from the coordinates
of their corners.

parameters
----------
field1: (tuple | list) of (int | float)
Coordinates of the first field. Order should be: (left, bottom, right, top)

field2: (tuple | list) of (int | float)
Coordinates of the second field. Order should be: (left, bottom, right, top)

Returns
-------
int or float
Area in the coordinates entered unit.

Example
-------
>>> from overlap import overlap_area
>>> field_a = (1, 1, 4, 4) # position in kms as (x_0, y_0, x_1, y_1)
>>> field_b = (2, 2, 3, 3) # smaller field inside field_a
>>> overlap_area(field_a, field_b)
1

'''

left1, bottom1, right1, top1 = field1
left2, bottom2, right2, top2 = field2

if (left1 > right1 or bottom1 > top1 or
left2 > right2 or bottom2 > top2):
raise ValueError(" Coordinates need to be entered (left, bottom, right, top)")

overlap_left = max(left1, left2)
overlap_bottom = max(bottom1, bottom2)
overlap_right = min(right1, right2)
overlap_top = min(top1, top2)
overlap_height = max(0, overlap_top - overlap_bottom)
overlap_width = max(0, overlap_right - overlap_left)
return overlap_height * overlap_width
2 changes: 2 additions & 0 deletions fields_overlap/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
addopts = --cov=overlap --cov-report html --doctest-modules
3 changes: 3 additions & 0 deletions fields_overlap/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pytest==3.2.0
pytest-cov==2.6.0
matplotlib==2.0.2
80 changes: 80 additions & 0 deletions fields_overlap/test_overlap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from pytest import approx, raises

from overlap import overlap_area

def test_basic():
''' Tests that basic example works '''
big_field = (1, 1, 4, 4)
inner_field = (2, 2, 3, 3)
assert overlap_area(big_field, inner_field) == 1


def test_partial_overlap():
''' Tests when there's a partial overlap'''
base_field = (1, 1, 4, 3)
over_field = (2, 2, 3, 4)
assert overlap_area(base_field, over_field) == 1


def test_corner_overlap():
''' Test when there is a box in a corner'''
base_field = (1, 0, 3, 5)
over_field = (2, 4, 4, 6)
assert overlap_area(base_field, over_field) == 1


def test_edge_touching():
''' Test when there is an edge '''
base_field = (1, 1, 4, 4)
over_field = (2, 2, 3, 4)
assert overlap_area(base_field, over_field) == 2


def test_2opposite_edge_touching():
''' Test when there is an edge '''
base_field = (1, 1, 4, 4)
over_field = (2, 1, 3, 4)
assert overlap_area(base_field, over_field) == 3


def test_outside_edge_touching():
''' Test when they are touching on the outside '''
base_field = (1, 1, 4, 4)
over_field = (2, 4, 3, 5)
assert overlap_area(base_field, over_field) == 0


def test_no_overlap():
''' Test when they are not touching each other '''
base_field = (0, 0, 3, 3)
over_field = (4, 4, 5, 5)
assert overlap_area(base_field, over_field) == 0


def test_floats():
''' Test that still works when using floats '''
base_field = (1, 1., 3.5, 3.5)
over_field = (3, 3, 5, 5)
assert overlap_area(base_field, over_field) == 0.5 * 0.5


def test_floats_again():
''' Test that still works when using floats '''
base_field = (1, 1., 3.3, 3.1)
over_field = (3, 3, 5, 5)
assert overlap_area(base_field, over_field) == approx(0.3 * 0.1, rel=1e-3)


def test_negative_basic():
''' Tests that basic example works '''
big_field = (-1, -1, -4, -4)
inner_field = (-2, -2, -3, -3)
with raises(ValueError, message=" Coordinates need to be entered (left, bottom, right, top) "):
overlap_area(big_field, inner_field)


def test_negative_basic2():
''' Tests that basic example works '''
big_field = (-1, -1, 1, 1)
inner_field = (0, -2, 1, 2)
assert overlap_area(big_field, inner_field) == 2