Skip to content

Commit 6e71c66

Browse files
committed
[feat] add tracking.txt load class
1 parent 3f711ee commit 6e71c66

File tree

9 files changed

+25213
-0
lines changed

9 files changed

+25213
-0
lines changed

.github/workflows/linux_tests.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Linux tests
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
linux:
13+
name: Linux Py${{ matrix.PYTHON_VERSION }}
14+
runs-on: ubuntu-latest
15+
env:
16+
CI: True
17+
PYTHON_VERSION: ${{ matrix.PYTHON_VERSION }}
18+
RUNNER_OS: 'ubuntu'
19+
strategy:
20+
fail-fast: false
21+
matrix:
22+
PYTHON_VERSION: ['3.6', '3.7', '3.8']
23+
steps:
24+
- name: Checkout branch
25+
uses: actions/checkout@v2
26+
- name: Install Conda
27+
uses: goanpeca/setup-miniconda@v1
28+
with:
29+
activate-environment: test
30+
auto-update-conda: true
31+
auto-activate-base: false
32+
python-version: ${{ matrix.PYTHON_VERSION }}
33+
- name: Install package dependencies
34+
shell: bash -l {0}
35+
run: conda install --file requirements/conda.txt -y -q
36+
- name: Install test dependencies
37+
shell: bash -l {0}
38+
run: |
39+
conda install --file requirements/tests.txt -y -q
40+
- name: Run tests
41+
shell: bash -l {0}
42+
run: |
43+
cd FastAnalysis/
44+
pytest tests/test_load.py

.github/workflows/macos_tests.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Macos tests
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
macos:
13+
name: Mac Py${{ matrix.PYTHON_VERSION }}
14+
runs-on: macos-latest
15+
env:
16+
CI: True
17+
PYTHON_VERSION: ${{ matrix.PYTHON_VERSION }}
18+
RUNNER_OS: 'macos'
19+
strategy:
20+
fail-fast: false
21+
matrix:
22+
PYTHON_VERSION: ['3.6', '3.7', '3.8']
23+
steps:
24+
- name: Checkout branch
25+
uses: actions/checkout@v2
26+
- name: Install Conda
27+
uses: goanpeca/setup-miniconda@v1
28+
with:
29+
activate-environment: test
30+
auto-update-conda: true
31+
auto-activate-base: false
32+
python-version: ${{ matrix.PYTHON_VERSION }}
33+
- name: Install package dependencies
34+
shell: bash -l {0}
35+
run: conda install --file requirements/conda.txt -y -q
36+
- name: Install test dependencies
37+
shell: bash -l {0}
38+
run: |
39+
conda install --file requirements/tests.txt -y -q
40+
- name: Run tests
41+
shell: bash -l {0}
42+
run: |
43+
cd FastAnalysis/
44+
pytest tests/test_load.py

.github/workflows/win_tests.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Windows tests
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
windows:
13+
name: Windows Py${{ matrix.PYTHON_VERSION }}
14+
runs-on: windows-latest
15+
env:
16+
CI: True
17+
PYTHON_VERSION: ${{ matrix.PYTHON_VERSION }}
18+
RUNNER_OS: 'windows'
19+
strategy:
20+
fail-fast: false
21+
matrix:
22+
PYTHON_VERSION: ['3.6', '3.7', '3.8']
23+
steps:
24+
- name: Checkout branch
25+
uses: actions/checkout@v2
26+
- name: Install Conda
27+
uses: goanpeca/setup-miniconda@v1
28+
with:
29+
activate-environment: test
30+
auto-update-conda: true
31+
auto-activate-base: false
32+
python-version: ${{ matrix.PYTHON_VERSION }}
33+
- name: Install package dependencies
34+
shell: bash -l {0}
35+
run: conda install --file requirements/conda.txt -y -q
36+
- name: Install test dependencies
37+
shell: bash -l {0}
38+
run: |
39+
conda install --file requirements/tests.txt -y -q
40+
- name: Run tests
41+
shell: bash -l {0}
42+
run: |
43+
cd FastAnalysis/
44+
pytest tests/test_load.py

FastAnalysis/load.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import pandas
2+
import os
3+
4+
class Load:
5+
"""Base class to load tracking.txt files"""
6+
7+
def __init__(self, path):
8+
"""
9+
Constructor for Load.
10+
11+
:param [path]: [Path to the tracking.txt file.]
12+
:type [path]: [str]
13+
...
14+
:raises [Exception]: []
15+
"""
16+
self.path = os.path.abspath(path)
17+
try:
18+
self.tracking = pandas.read_csv(path, sep='\t')
19+
except Exception as e:
20+
raise e
21+
22+
def getDataframe(self):
23+
"""
24+
Get the tracking data in a DataFrame.
25+
26+
:raises [Exception]: [The selected file is empty]
27+
...
28+
:return: [Tracking data]
29+
:rtype: [DataFrame]
30+
"""
31+
if self.tracking.empty:
32+
raise Exception("The selected file is empty")
33+
else:
34+
return self.tracking
35+
36+
def getObjectNumber(self):
37+
"""
38+
Get the total number of objects.
39+
40+
...
41+
:return: [Total number of objects]
42+
:rtype: [int]
43+
"""
44+
maxObj = len(set(self.tracking.id.values))
45+
return maxObj
46+
47+
def getObject(self, iD):
48+
"""
49+
Get the data for the object with id.
50+
51+
:param [iD]: [Id of the object.]
52+
:type [index]: [int]
53+
...
54+
:return: [Data for the object id.]
55+
:rtype: [DataFrame]
56+
"""
57+
objectData = self.tracking[self.tracking.id == iD]
58+
return objectData
59+
60+
def getFrame(self, index):
61+
"""
62+
Get the data for the image number index.
63+
64+
:param [index]: [Index of the image.]
65+
:type [index]: [int]
66+
...
67+
:return: [Data for the image index.]
68+
:rtype: [DataFrame]
69+
"""
70+
objectData = self.tracking[self.tracking.imageNumber == index]
71+
return objectData
72+
73+
def getObjectInFrame(self, iD, index):
74+
"""
75+
Get the data for an object id is in a frame index.
76+
77+
:param [iD]: [Id of the object.]
78+
:type [index]: [int]
79+
:param [index]: [Index of the image.]
80+
:type [index]: [int]
81+
...
82+
:return: [True if object id in frame index.]
83+
:rtype: [bool]
84+
"""
85+
data = self.tracking[(self.tracking.imageNumber == index)&(self.tracking.id == iD)]
86+
return data
87+
88+
def isObjectInFrame(self, iD, index):
89+
"""
90+
Check if an object id is in a frame index.
91+
92+
:param [iD]: [Id of the object.]
93+
:type [index]: [int]
94+
:param [index]: [Index of the image.]
95+
:type [index]: [int]
96+
...
97+
:return: [True if object id in frame index.]
98+
:rtype: [bool]
99+
"""
100+
data = self.tracking[(self.tracking.imageNumber == index)&(self.tracking.id == iD)]
101+
if data.empty:
102+
return False
103+
else:
104+
return True

FastAnalysis/tests/__init__.py

Whitespace-only changes.

FastAnalysis/tests/test_load.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import pytest
2+
import pandas
3+
4+
import load
5+
6+
def test_load_file():
7+
"""Test that file is loaded as a dataframe."""
8+
tracking = load.Load("tests/tracking.txt").getDataframe()
9+
reference = pandas.read_csv("tests/tracking.txt", sep='\t')
10+
pandas.testing.assert_frame_equal(tracking, reference)
11+
12+
def test_load_file_error():
13+
"""Test that wrong path lead to Exception."""
14+
with pytest.raises(Exception):
15+
tracking = load.Load("tests/tracing.txt").getDataframe()
16+
assert tracking
17+
18+
def test_object_number():
19+
"""Test number of objects."""
20+
objectNumber = load.Load("tests/tracking.txt").getObjectNumber()
21+
assert objectNumber == 207
22+
23+
def test_get_object():
24+
"""Test get the data for an object"""
25+
reference = pandas.read_csv("tests/tracking.txt", sep='\t')
26+
tracking = load.Load("tests/tracking.txt").getObject(0)
27+
pandas.testing.assert_frame_equal(tracking, reference[reference.id==0])
28+
tracking = load.Load("tests/tracking.txt").getObject(1)
29+
pandas.testing.assert_frame_equal(tracking, reference[reference.id==1])
30+
31+
def test_get_frame():
32+
"""Test get the data for a frame"""
33+
reference = pandas.read_csv("tests/tracking.txt", sep='\t')
34+
tracking = load.Load("tests/tracking.txt").getFrame(10)
35+
pandas.testing.assert_frame_equal(tracking, reference[reference.imageNumber==10])
36+
tracking = load.Load("tests/tracking.txt").getFrame(1)
37+
pandas.testing.assert_frame_equal(tracking, reference[reference.imageNumber==1])
38+
39+
def test_get_object_in_frame():
40+
"""Test get the data for an frame"""
41+
reference = pandas.read_csv("tests/tracking.txt", sep='\t')
42+
tracking = load.Load("tests/tracking.txt").getObjectInFrame(0, 200)
43+
pandas.testing.assert_frame_equal(tracking, reference[(reference.imageNumber==200)&(reference.id==0)])
44+
tracking = load.Load("tests/tracking.txt").getObjectInFrame(1, 100)
45+
pandas.testing.assert_frame_equal(tracking, reference[(reference.imageNumber==100)&(reference.id==1)])
46+
47+
def test_is_object_in_frame():
48+
"""Test get the data for an frame"""
49+
reference = pandas.read_csv("tests/tracking.txt", sep='\t')
50+
tracking = load.Load("tests/tracking.txt").isObjectInFrame(0, 0)
51+
assert tracking
52+
tracking = load.Load("tests/tracking.txt").isObjectInFrame(0, 1500)
53+
assert not tracking
54+

0 commit comments

Comments
 (0)