Skip to content

Commit

Permalink
Merge pull request #13 from carienmol/archivecoords
Browse files Browse the repository at this point in the history
Tests for archive coordinate arrays
  • Loading branch information
carienmol committed Apr 4, 2024
2 parents 7ce96d0 + 556762f commit 42f704a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 39 deletions.
7 changes: 5 additions & 2 deletions birdwatcher/coordinatearrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
'delete_coordinatearray', 'move_coordinatearrays']


def _archive(rar):
rar.archive(overwrite=True)
delete_raggedarray(rar)


def _coordstoframe(coords, width, height, nchannels=None, dtype='uint8',
value=1):
if nchannels is None:
Expand Down Expand Up @@ -374,8 +379,6 @@ def extract_archivedcoordinatedata(path):

while path.suffix in {'.tar', '.xz'}: # remove extensions
path = path.with_suffix('')

print(path)

return CoordinateArrays(path)

Expand Down
8 changes: 2 additions & 6 deletions birdwatcher/movementdetection/movementdetection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import birdwatcher as bw
from birdwatcher.utils import derive_filepath
from birdwatcher.coordinatearrays import _archive


__all__ = ['batch_detect_movement', 'detect_movement', 'apply_settings',
Expand All @@ -17,11 +18,6 @@
'resizebyfactor': 1}} # use '1' for no change in size


def _f(rar):
rar.archive(overwrite=True)
darr.delete_raggedarray(rar)


def batch_detect_movement(vfs_list, settings=None, startat=None,
nframes=None, roi=None, nroi=None,
bgs_type=bw.BackgroundSubtractorMOG2,
Expand Down Expand Up @@ -63,7 +59,7 @@ def batch_detect_movement(vfs_list, settings=None, startat=None,
tobearchived.append(cd)
if (len(tobearchived) == nprocesses) or (i == (len(vfs_list)-1)):
with ThreadPool(processes=nprocesses) as pool:
list([i for i in pool.imap_unordered(_f, tobearchived)])
list([i for i in pool.imap_unordered(_archive, tobearchived)])
tobearchived = []


Expand Down
52 changes: 39 additions & 13 deletions birdwatcher/tests/test_coordinatearrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import numpy as np
from numpy.testing import assert_array_equal

from birdwatcher.coordinatearrays import create_coordarray, \
open_archivedcoordinatedata, move_coordinatearrays, CoordinateArrays
import birdwatcher as bw
from birdwatcher.coordinatearrays import _archive


class TestCoordinateArrays(unittest.TestCase):
Expand All @@ -17,10 +17,8 @@ def setUp(self):
self.tempdirname1 = tempfile.mkdtemp()
fh, self.tempvideoname = tempfile.mkstemp()
os.close(fh)
fh, self.temparchivename = tempfile.mkstemp(suffix='.tar.xz')
os.close(fh)
metadata = {'avgframerate': 5}
self.ca1 = create_coordarray(path=self.tempdirname1,
self.ca1 = bw.create_coordarray(path=self.tempdirname1,
framewidth=1080, frameheight=720,
metadata=metadata, overwrite=True)
self.ca1.iterappend([((1,2),(3,4)),((5,6),(7,8))])
Expand All @@ -29,8 +27,6 @@ def tearDown(self):
shutil.rmtree(self.tempdirname1)
if Path(self.tempvideoname).exists():
Path(self.tempvideoname).unlink()
if Path(self.temparchivename).exists():
Path(self.temparchivename).unlink()

def test_index(self):
assert_array_equal(self.ca1[1], np.array([[5,6],[7,8]]))
Expand Down Expand Up @@ -66,10 +62,40 @@ def test_coordmedian(self):
cmd = self.ca1.get_coordmedian()
assert_array_equal(cmd, np.array([[2,3],[6,7],[3,4]]))


class TestArchivedCoordinateArrays(unittest.TestCase):

def setUp(self):
self.tempdirname1 = Path(tempfile.mkdtemp())
self.archivename = self.tempdirname1.parent / (self.tempdirname1.name + '.tar.xz')
metadata = {'avgframerate': 5}
self.ca1 = bw.create_coordarray(path=self.tempdirname1,
framewidth=1080, frameheight=720,
metadata=metadata, overwrite=True)
self.ca1.iterappend([((1,2),(3,4)),((5,6),(7,8))])

def tearDown(self):
if self.tempdirname1.exists():
shutil.rmtree(self.tempdirname1)
if self.archivename.exists():
self.archivename.unlink()

def test_archive(self):
_archive(self.ca1)
self.assertTrue(self.archivename.exists())
self.assertFalse(self.tempdirname1.exists())

def test_open_archived(self):
ap = self.ca1.datadir.archive(self.temparchivename, overwrite=True)
with open_archivedcoordinatedata(ap) as ca:
assert_array_equal(ca[1], self.ca1[1])
coords1 = self.ca1[1]
_archive(self.ca1)
with bw.open_archivedcoordinatedata(self.archivename) as ca2:
assert_array_equal(ca2[1], coords1)

def test_extract_archived(self):
coords1 = self.ca1[1]
_archive(self.ca1)
ca2 = bw.extract_archivedcoordinatedata(self.archivename)
assert_array_equal(ca2[1], coords1)


class TestMoveCoordinateArrays(unittest.TestCase):
Expand All @@ -78,7 +104,7 @@ def setUp(self):
self.tempdirname1 = tempfile.mkdtemp()
self.tempdirname2 = tempfile.mkdtemp()
path = Path(self.tempdirname1)/'even.darr'
self.ca1 = create_coordarray(path=path, framewidth=1080,
self.ca1 = bw.create_coordarray(path=path, framewidth=1080,
frameheight=720, overwrite=True)
self.ca1.iterappend([((1, 2), (3, 4)), ((5, 6), (7, 8))])

Expand All @@ -87,8 +113,8 @@ def tearDown(self):
shutil.rmtree(self.tempdirname2)

def test_movecoordinatearrays(self):
move_coordinatearrays(self.tempdirname1, self.tempdirname2)
ca2 = CoordinateArrays(Path(self.tempdirname2) / 'even.darr')
bw.move_coordinatearrays(self.tempdirname1, self.tempdirname2)
ca2 = bw.CoordinateArrays(Path(self.tempdirname2) / 'even.darr')



Expand Down
31 changes: 13 additions & 18 deletions birdwatcher/tests/test_movementdetection.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ class TestDetectMovement(unittest.TestCase):

def setUp(self):
self.tempdirname1 = Path(tempfile.mkdtemp())
self.vfs = (bw.testvideosmall()
.iter_frames(nframes=200)
.tovideo(self.tempdirname1 / 'even1.mp4', framerate=25))
self.vfs = bw.testvideosmall()

def tearDown(self):
shutil.rmtree(self.tempdirname1)
Expand Down Expand Up @@ -68,29 +66,26 @@ class TestBatchDetectMovement(unittest.TestCase):

def setUp(self):
self.tempdirname1 = Path(tempfile.mkdtemp())
self.vfs = (bw.testvideosmall()
self.vfs1 = (bw.testvideosmall()
.iter_frames(nframes=200)
.tovideo(self.tempdirname1 / 'even1.mp4', framerate=25))
.tovideo(self.tempdirname1 / 'video1.mp4', framerate=25))
self.vfs2 = (bw.testvideosmall()
.iter_frames(nframes=200)
.tovideo(self.tempdirname1 / 'video2.mp4', framerate=25))

def tearDown(self):
shutil.rmtree(self.tempdirname1)

def test_batchdetection(self):
p1 = self.vfs
p2 = p1.iter_frames().tovideo(self.tempdirname1 / 'even2.mp4',
framerate=p1.avgframerate)
md.batch_detect_movement([p1,p2], nframes=200,
md.batch_detect_movement([self.vfs1,self.vfs2], nframes=200,
analysispath=self.tempdirname1,
overwrite=True, archived=False)
filepath = self.tempdirname1 / 'movement_even1/coords.darr'
self.assertTrue(Path.exists(filepath))
self.assertTrue(Path.exists(self.tempdirname1/'movement_video1/coords.darr'))
self.assertTrue(Path.exists(self.tempdirname1/'movement_video2/coords.darr'))

def test_batcharchive(self):
p1 = self.vfs
p2 = p1.iter_frames().tovideo(self.tempdirname1 / 'even2.mp4',
framerate=p1.avgframerate)
md.batch_detect_movement([p1,p2], nframes=200,
md.batch_detect_movement([self.vfs1,self.vfs2], nframes=200,
analysispath=self.tempdirname1,
overwrite=True, archived=True)
filepath = self.tempdirname1 / 'movement_even1/coords.darr.tar.xz'
self.assertTrue(Path.exists(filepath))
self.assertTrue(Path.exists(self.tempdirname1/'movement_video1/coords.darr.tar.xz'))
self.assertTrue(Path.exists(self.tempdirname1/'movement_video2/coords.darr.tar.xz'))

0 comments on commit 42f704a

Please sign in to comment.