Skip to content

Commit a66da50

Browse files
authored
Merge pull request #6 from ParallelDots/documentation
Documentation
2 parents 483ca58 + abffc29 commit a66da50

9 files changed

Lines changed: 222 additions & 29 deletions

File tree

.github/workflows/test.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Run Tests
2+
3+
on:
4+
push:
5+
branches: main
6+
pull_request:
7+
branches: main
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
13+
steps:
14+
- name: Checkout repository
15+
uses: actions/checkout@v2
16+
17+
- name: Set up Python
18+
uses: actions/setup-python@v2
19+
with:
20+
python-version: 3.x
21+
22+
- name: Install dependencies
23+
run: pip install -r requirements.txt
24+
25+
- name: Run tests
26+
run: python -m unittest

README.md

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,42 @@ pip install retailtree
2121
# Usage
2222

2323
```
24+
# Import necessary modules and functions
2425
from retailtree import RetailTree, Annotation
2526
from retailtree.utils.dist_func import manhattan, euclidean
27+
import json
2628
27-
# Create annotation object
28-
ann1 = Annotation(id=1, x_min=2, y_min=1, x_max=3, y_max=2)
29-
ann2 = Annotation(id=2, x_min=1, y_min=2, x_max=2, y_max=3)
30-
ann3 = Annotation(id=3, x_min=2, y_min=2, x_max=3, y_max=3)
31-
ann4 = Annotation(id=4, x_min=3, y_min=2, x_max=4, y_max=3)
32-
ann5 = Annotation(id=5, x_min=2, y_min=3, x_max=3, y_max=4)
29+
# Define the path to the JSON file containing annotations
30+
file_path = './tests/test_data/test_data.json'
3331
34-
annotations = [ann1, ann2, ann3, ann4, ann5]
32+
# Open and load the JSON file
33+
with open(file_path, 'r') as file:
34+
annotations = json.load(file)
3535
36-
# Create retailtree object
36+
# Initialize a RetailTree object
3737
rt = RetailTree()
3838
39-
# Adding annotations to retailtree
39+
# Iterate over the loaded annotations and create Annotation objects
4040
for ann in annotations:
41-
rt.add_annotation(ann)
41+
# Create an Annotation object with the required properties
42+
ann_obj = Annotation(id=ann['id'], x_min=ann['x_min'], y_min=ann['y_min'], x_max=ann['x_max'], y_max=ann['y_max'])
43+
# Add the created Annotation object to the RetailTree
44+
rt.add_annotation(ann_obj)
4245
43-
44-
# Build tree
46+
# Build the spatial tree structure using the euclidean distance function
4547
rt.build_tree(dist_func=euclidean)
4648
47-
# Get neighbors-annotations within radius
49+
# Retrieve and print annotations within a radius of 1 from the annotation with id=3
4850
print(rt.neighbors(id=3, radius=1))
4951
50-
# Get Top, Bottom, Right, Left annotations
52+
# Retrieve and print the Top, Bottom, Left, and Right neighboring annotations
53+
# of the annotation with id=3 within a radius of 1 and a minimum overlap of 0.5
5154
print(rt.TBLR(id=3, radius=1, overlap=0.5))
5255
53-
# Get neighboring annotations within a particular angle range
56+
# Retrieve and print neighboring annotations of the annotation with id=3
57+
# within a radius of 1 and angle range from 0 to 180 degrees
5458
print(rt.neighbors_wa(id=3, radius=1, amin=0, amax=180))
5559
56-
# Get annotation properties
60+
# Retrieve and print the coordinates of the annotation with id=3
5761
print(rt.get(id=3).get_coords())
58-
5962
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
[{"id": 1, "distance": 90.0}, {"id": 4, "distance": 50.0}, {"id": 5, "distance": 102.95630140987001}],
3+
[{"id": 0, "distance": 90.0}, {"id": 24, "distance": 90.0}, {"id": 6, "distance": 102.95630140987001}, {"id": 4, "distance": 102.95630140987001}, {"id": 5, "distance": 50.0}, {"id": 2, "distance": 90.0}],
4+
[{"id": 7, "distance": 102.95630140987001}, {"id": 3, "distance": 90.0}, {"id": 6, "distance": 50.0}, {"id": 1, "distance": 90.0}, {"id": 25, "distance": 90.0}, {"id": 5, "distance": 102.95630140987001}],
5+
[{"id": 7, "distance": 50.0}, {"id": 6, "distance": 102.95630140987001}, {"id": 2, "distance": 90.0}],
6+
[{"id": 0, "distance": 50.0}, {"id": 1, "distance": 102.95630140987001}, {"id": 24, "distance": 98.48857801796105}, {"id": 5, "distance": 90.0}],
7+
[{"id": 0, "distance": 102.95630140987001}, {"id": 24, "distance": 40.0}, {"id": 6, "distance": 90.0}, {"id": 1, "distance": 50.0}, {"id": 25, "distance": 98.48857801796105}, {"id": 4, "distance": 90.0}, {"id": 2, "distance": 102.95630140987001}],
8+
[{"id": 7, "distance": 90.0}, {"id": 24, "distance": 98.48857801796105}, {"id": 3, "distance": 102.95630140987001}, {"id": 25, "distance": 40.0}, {"id": 5, "distance": 90.0}, {"id": 1, "distance": 102.95630140987001}, {"id": 2, "distance": 50.0}],
9+
[{"id": 3, "distance": 50.0}, {"id": 6, "distance": 90.0}, {"id": 2, "distance": 102.95630140987001}, {"id": 25, "distance": 98.48857801796105}],
10+
[{"id": 26, "distance": 90.0}, {"id": 13, "distance": 102.95630140987001}, {"id": 12, "distance": 50.0}, {"id": 9, "distance": 90.0}],
11+
[{"id": 24, "distance": 70.0}, {"id": 13, "distance": 50.0}, {"id": 12, "distance": 102.95630140987001}, {"id": 8, "distance": 90.0}, {"id": 14, "distance": 102.95630140987001}, {"id": 10, "distance": 90.0}]
12+
]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
[{"id": 1, "distance": 90.0, "angle": 0}, {"id": 4, "distance": 50.0, "angle": 90}, {"id": 5, "distance": 102.95630140987001, "angle": 29}],
3+
[{"id": 0, "distance": 90.0, "angle": 180}, {"id": 24, "distance": 90.0, "angle": 90}, {"id": 6, "distance": 102.95630140987001, "angle": 29}, {"id": 4, "distance": 102.95630140987001, "angle": 150}, {"id": 5, "distance": 50.0, "angle": 90}, {"id": 2, "distance": 90.0, "angle": 0}],
4+
[{"id": 7, "distance": 102.95630140987001, "angle": 29}, {"id": 3, "distance": 90.0, "angle": 0}, {"id": 6, "distance": 50.0, "angle": 90}, {"id": 1, "distance": 90.0, "angle": 180}, {"id": 25, "distance": 90.0, "angle": 90}, {"id": 5, "distance": 102.95630140987001, "angle": 150}],
5+
[{"id": 7, "distance": 50.0, "angle": 90}, {"id": 6, "distance": 102.95630140987001, "angle": 150}, {"id": 2, "distance": 90.0, "angle": 180}],
6+
[{"id": 0, "distance": 50.0, "angle": 270}, {"id": 24, "distance": 98.48857801796105, "angle": 23}, {"id": 5, "distance": 90.0, "angle": 0}],
7+
[{"id": 0, "distance": 102.95630140987001, "angle": 210}, {"id": 24, "distance": 40.0, "angle": 90}, {"id": 6, "distance": 90.0, "angle": 0}, {"id": 1, "distance": 50.0, "angle": 270}, {"id": 25, "distance": 98.48857801796105, "angle": 23}, {"id": 4, "distance": 90.0, "angle": 180}],
8+
[{"id": 7, "distance": 90.0, "angle": 0}, {"id": 24, "distance": 98.48857801796105, "angle": 156}, {"id": 25, "distance": 40.0, "angle": 90}, {"id": 5, "distance": 90.0, "angle": 180}, {"id": 1, "distance": 102.95630140987001, "angle": 210}, {"id": 2, "distance": 50.0, "angle": 270}],
9+
[{"id": 3, "distance": 50.0, "angle": 270}, {"id": 6, "distance": 90.0, "angle": 180}, {"id": 2, "distance": 102.95630140987001, "angle": 210}, {"id": 25, "distance": 98.48857801796105, "angle": 156}],
10+
[{"id": 26, "distance": 90.0, "angle": 90}, {"id": 13, "distance": 102.95630140987001, "angle": 29}, {"id": 12, "distance": 50.0, "angle": 90}, {"id": 9, "distance": 90.0, "angle": 0}],
11+
[{"id": 24, "distance": 70.0, "angle": 270}, {"id": 13, "distance": 50.0, "angle": 90}, {"id": 12, "distance": 102.95630140987001, "angle": 150}, {"id": 8, "distance": 90.0, "angle": 180}, {"id": 14, "distance": 102.95630140987001, "angle": 29}, {"id": 10, "distance": 90.0, "angle": 0}]
12+
]
File renamed without changes.

tests/test_neighbor.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import unittest
2+
from retailtree.retailtree import Annotation, RetailTree
3+
import json
4+
5+
6+
class TestAnnotation(unittest.TestCase):
7+
8+
def setUp(self):
9+
# Build tree before every test case
10+
file_path = './tests/test_data/test_data.json'
11+
file_path_output = './tests/test_data/neighbors_output.json'
12+
# Annotations data2
13+
with open(file_path, 'r') as file:
14+
self.annotation_data_json = json.load(file)
15+
16+
with open(file_path_output, 'r') as file:
17+
self.annotation_output_data = json.load(file)
18+
19+
self.RT = RetailTree()
20+
for data in self.annotation_data_json:
21+
ann = Annotation(data['id'], data['x_min'],
22+
data['y_min'], data['x_max'], data['y_max'])
23+
self.RT.add_annotation(ann)
24+
25+
self.RT.build_tree()
26+
self.assertIsNotNone(self.RT.tree)
27+
28+
def test_find_neighbors_0(self):
29+
self.assertEqual(self.RT.neighbors(
30+
id=0, radius=1.2), self.annotation_output_data[0])
31+
32+
def test_find_neighbors_1(self):
33+
self.assertEqual(self.RT.neighbors(
34+
id=1, radius=1.2), self.annotation_output_data[1])
35+
36+
def test_find_neighbors_2(self):
37+
self.assertEqual(self.RT.neighbors(
38+
id=2, radius=1.2), self.annotation_output_data[2])
39+
40+
def test_find_neighbors_3(self):
41+
self.assertEqual(self.RT.neighbors(
42+
id=3, radius=1.2), self.annotation_output_data[3])
43+
44+
def test_find_neighbors_4(self):
45+
self.assertEqual(self.RT.neighbors(
46+
id=4, radius=1.2), self.annotation_output_data[4])
47+
48+
def test_find_neighbors_5(self):
49+
self.assertEqual(self.RT.neighbors(
50+
id=5, radius=1.2), self.annotation_output_data[5])
51+
52+
def test_find_neighbors_6(self):
53+
self.assertEqual(self.RT.neighbors(
54+
id=6, radius=1.2), self.annotation_output_data[6])
55+
56+
def test_find_neighbors_7(self):
57+
self.assertEqual(self.RT.neighbors(
58+
id=7, radius=1.2), self.annotation_output_data[7])
59+
60+
def test_find_neighbors_8(self):
61+
self.assertEqual(self.RT.neighbors(
62+
id=8, radius=1.2), self.annotation_output_data[8])
63+
64+
def test_find_neighbors_9(self):
65+
self.assertEqual(self.RT.neighbors(
66+
id=9, radius=1.2), self.annotation_output_data[9])
67+
68+
69+
if __name__ == '__main__':
70+
unittest.main()

tests/tests.py renamed to tests/test_tblr.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ class TestAnnotation(unittest.TestCase):
77

88
def setUp(self):
99
# Build tree before every test case
10-
file_path = './tests/test_data/ideal_test_data.json'
11-
file_path_output = './tests/test_data/output_ideal_test_data.json'
10+
file_path = './tests/test_data/test_data.json'
11+
file_path_output = './tests/test_data/tblr_output.json'
1212
# Annotations data2
1313
with open(file_path, 'r') as file:
1414
self.annotation_data_json = json.load(file)
@@ -25,43 +25,43 @@ def setUp(self):
2525
self.RT.build_tree()
2626
self.assertIsNotNone(self.RT.tree)
2727

28-
def test_find_neighbors_0(self):
28+
def test_tblr_0(self):
2929
self.assertEqual(self.RT.TBLR(
3030
id=0, radius=2), self.annotation_output_data[0])
3131

32-
def test_find_neighbors_1(self):
32+
def test_tblr_1(self):
3333
self.assertEqual(self.RT.TBLR(
3434
id=1, radius=2), self.annotation_output_data[1])
3535

36-
def test_find_neighbors_2(self):
36+
def test_tblr_2(self):
3737
self.assertEqual(self.RT.TBLR(
3838
id=2, radius=2), self.annotation_output_data[2])
3939

40-
def test_find_neighbors_3(self):
40+
def test_tblr_3(self):
4141
self.assertEqual(self.RT.TBLR(
4242
id=3, radius=2), self.annotation_output_data[3])
4343

44-
def test_find_neighbors_4(self):
44+
def test_tblr_4(self):
4545
self.assertEqual(self.RT.TBLR(
4646
id=4, radius=2), self.annotation_output_data[4])
4747

48-
def test_find_neighbors_5(self):
48+
def test_tblr_5(self):
4949
self.assertEqual(self.RT.TBLR(
5050
id=5, radius=2), self.annotation_output_data[5])
5151

52-
def test_find_neighbors_6(self):
52+
def test_tblr_6(self):
5353
self.assertEqual(self.RT.TBLR(
5454
id=6, radius=2), self.annotation_output_data[6])
5555

56-
def test_find_neighbors_7(self):
56+
def test_tblr_7(self):
5757
self.assertEqual(self.RT.TBLR(
5858
id=7, radius=2), self.annotation_output_data[7])
5959

60-
def test_find_neighbors_8(self):
60+
def test_tblr_8(self):
6161
self.assertEqual(self.RT.TBLR(
6262
id=8, radius=2), self.annotation_output_data[8])
6363

64-
def test_find_neighbors_9(self):
64+
def test_tblr_9(self):
6565
self.assertEqual(self.RT.TBLR(
6666
id=9, radius=2), self.annotation_output_data[9])
6767

tests/test_wa.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import unittest
2+
from retailtree.retailtree import Annotation, RetailTree
3+
import json
4+
5+
6+
class TestAnnotation(unittest.TestCase):
7+
8+
def setUp(self):
9+
# Build tree before every test case
10+
file_path = './tests/test_data/test_data.json'
11+
file_path_output = './tests/test_data/neighbors_wa_output.json'
12+
# Annotations data2
13+
with open(file_path, 'r') as file:
14+
self.annotation_data_json = json.load(file)
15+
16+
with open(file_path_output, 'r') as file:
17+
self.annotation_output_data = json.load(file)
18+
19+
self.RT = RetailTree()
20+
for data in self.annotation_data_json:
21+
ann = Annotation(data['id'], data['x_min'],
22+
data['y_min'], data['x_max'], data['y_max'])
23+
self.RT.add_annotation(ann)
24+
25+
self.RT.build_tree()
26+
self.assertIsNotNone(self.RT.tree)
27+
28+
def test_find_neighbors_0(self):
29+
self.assertEqual(self.RT.neighbors_wa(
30+
id=0, radius=1.2, amin=0, amax=270), self.annotation_output_data[0])
31+
32+
def test_find_neighbors_1(self):
33+
self.assertEqual(self.RT.neighbors_wa(
34+
id=1, radius=1.2, amin=0, amax=270), self.annotation_output_data[1])
35+
36+
def test_find_neighbors_2(self):
37+
self.assertEqual(self.RT.neighbors_wa(
38+
id=2, radius=1.2, amin=0, amax=270), self.annotation_output_data[2])
39+
40+
def test_find_neighbors_3(self):
41+
self.assertEqual(self.RT.neighbors_wa(
42+
id=3, radius=1.2, amin=0, amax=270), self.annotation_output_data[3])
43+
44+
def test_find_neighbors_4(self):
45+
self.assertEqual(self.RT.neighbors_wa(
46+
id=4, radius=1.2, amin=0, amax=270), self.annotation_output_data[4])
47+
48+
def test_find_neighbors_5(self):
49+
self.assertEqual(self.RT.neighbors_wa(
50+
id=5, radius=1.2, amin=0, amax=270), self.annotation_output_data[5])
51+
52+
def test_find_neighbors_6(self):
53+
self.assertEqual(self.RT.neighbors_wa(
54+
id=6, radius=1.2, amin=0, amax=270), self.annotation_output_data[6])
55+
56+
def test_find_neighbors_7(self):
57+
self.assertEqual(self.RT.neighbors_wa(
58+
id=7, radius=1.2, amin=0, amax=270), self.annotation_output_data[7])
59+
60+
def test_find_neighbors_8(self):
61+
self.assertEqual(self.RT.neighbors_wa(
62+
id=8, radius=1.2, amin=0, amax=270), self.annotation_output_data[8])
63+
64+
def test_find_neighbors_9(self):
65+
self.assertEqual(self.RT.neighbors_wa(
66+
id=9, radius=1.2, amin=0, amax=270), self.annotation_output_data[9])
67+
68+
69+
if __name__ == '__main__':
70+
unittest.main()

0 commit comments

Comments
 (0)