diff --git a/birdwatcher/coordinatearrays.py b/birdwatcher/coordinatearrays.py index 71d1400..7a6986d 100644 --- a/birdwatcher/coordinatearrays.py +++ b/birdwatcher/coordinatearrays.py @@ -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: @@ -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) diff --git a/birdwatcher/movementdetection/movementdetection.py b/birdwatcher/movementdetection/movementdetection.py index b330c44..3c396cb 100644 --- a/birdwatcher/movementdetection/movementdetection.py +++ b/birdwatcher/movementdetection/movementdetection.py @@ -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', @@ -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, @@ -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 = [] diff --git a/birdwatcher/tests/test_coordinatearrays.py b/birdwatcher/tests/test_coordinatearrays.py index 2f1dcf6..e712c67 100644 --- a/birdwatcher/tests/test_coordinatearrays.py +++ b/birdwatcher/tests/test_coordinatearrays.py @@ -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): @@ -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))]) @@ -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]])) @@ -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): @@ -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))]) @@ -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') diff --git a/birdwatcher/tests/test_movementdetection.py b/birdwatcher/tests/test_movementdetection.py index da3f6a3..fca3278 100644 --- a/birdwatcher/tests/test_movementdetection.py +++ b/birdwatcher/tests/test_movementdetection.py @@ -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) @@ -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)) \ No newline at end of file + self.assertTrue(Path.exists(self.tempdirname1/'movement_video1/coords.darr.tar.xz')) + self.assertTrue(Path.exists(self.tempdirname1/'movement_video2/coords.darr.tar.xz')) \ No newline at end of file