From 7cec3a3c36f6c6460a526bb1353048d0f445cff7 Mon Sep 17 00:00:00 2001 From: Carien Mol Date: Thu, 28 Mar 2024 09:31:57 +0100 Subject: [PATCH 1/6] rename _f into _archive --- birdwatcher/movementdetection/movementdetection.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/birdwatcher/movementdetection/movementdetection.py b/birdwatcher/movementdetection/movementdetection.py index b330c44..2e80c3b 100644 --- a/birdwatcher/movementdetection/movementdetection.py +++ b/birdwatcher/movementdetection/movementdetection.py @@ -17,7 +17,7 @@ 'resizebyfactor': 1}} # use '1' for no change in size -def _f(rar): +def _archive(rar): rar.archive(overwrite=True) darr.delete_raggedarray(rar) @@ -63,7 +63,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 = [] From ed97caaf93e93d69ba1c310bb0d6dac7a88bba44 Mon Sep 17 00:00:00 2001 From: Carien Mol Date: Thu, 28 Mar 2024 11:27:23 +0100 Subject: [PATCH 2/6] move _archive to coordinatearrays module --- birdwatcher/coordinatearrays.py | 7 +++++-- birdwatcher/movementdetection/movementdetection.py | 6 +----- 2 files changed, 6 insertions(+), 7 deletions(-) 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 2e80c3b..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 _archive(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, From 4db7a4df56b8361f1cb754233a5dbb677b451d31 Mon Sep 17 00:00:00 2001 From: Carien Mol Date: Thu, 28 Mar 2024 11:32:11 +0100 Subject: [PATCH 3/6] add test for _archive function --- birdwatcher/tests/test_coordinatearrays.py | 37 ++++++++++++++++++---- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/birdwatcher/tests/test_coordinatearrays.py b/birdwatcher/tests/test_coordinatearrays.py index 2f1dcf6..1720b3d 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): @@ -20,7 +20,7 @@ def setUp(self): 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))]) @@ -68,17 +68,40 @@ def test_coordmedian(self): def test_open_archived(self): ap = self.ca1.datadir.archive(self.temparchivename, overwrite=True) - with open_archivedcoordinatedata(ap) as ca: + with bw.open_archivedcoordinatedata(ap) as ca: assert_array_equal(ca[1], self.ca1[1]) +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()) + + class TestMoveCoordinateArrays(unittest.TestCase): 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 +110,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') From 3f3d57ba11f153a22a46c96896d36faddc208778 Mon Sep 17 00:00:00 2001 From: Carien Mol Date: Thu, 28 Mar 2024 13:31:33 +0100 Subject: [PATCH 4/6] modify test for open_archivedcoordinatedata --- birdwatcher/tests/test_coordinatearrays.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/birdwatcher/tests/test_coordinatearrays.py b/birdwatcher/tests/test_coordinatearrays.py index 1720b3d..68e644b 100644 --- a/birdwatcher/tests/test_coordinatearrays.py +++ b/birdwatcher/tests/test_coordinatearrays.py @@ -17,8 +17,6 @@ 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 = bw.create_coordarray(path=self.tempdirname1, framewidth=1080, frameheight=720, @@ -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,11 +62,6 @@ def test_coordmedian(self): cmd = self.ca1.get_coordmedian() assert_array_equal(cmd, np.array([[2,3],[6,7],[3,4]])) - def test_open_archived(self): - ap = self.ca1.datadir.archive(self.temparchivename, overwrite=True) - with bw.open_archivedcoordinatedata(ap) as ca: - assert_array_equal(ca[1], self.ca1[1]) - class TestArchivedCoordinateArrays(unittest.TestCase): @@ -79,8 +70,8 @@ def setUp(self): 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) + framewidth=1080, frameheight=720, + metadata=metadata, overwrite=True) self.ca1.iterappend([((1,2),(3,4)),((5,6),(7,8))]) def tearDown(self): @@ -93,6 +84,12 @@ def test_archive(self): _archive(self.ca1) self.assertTrue(self.archivename.exists()) self.assertFalse(self.tempdirname1.exists()) + + def test_open_archived(self): + coords1 = self.ca1[1] + _archive(self.ca1) + with bw.open_archivedcoordinatedata(self.archivename) as ca2: + assert_array_equal(ca2[1], coords1) class TestMoveCoordinateArrays(unittest.TestCase): From ed71b09d603ebe66539293d16e5e78a0c818d31e Mon Sep 17 00:00:00 2001 From: Carien Mol Date: Thu, 28 Mar 2024 13:40:40 +0100 Subject: [PATCH 5/6] add test extract_archivedcoordinatedata --- birdwatcher/tests/test_coordinatearrays.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/birdwatcher/tests/test_coordinatearrays.py b/birdwatcher/tests/test_coordinatearrays.py index 68e644b..e712c67 100644 --- a/birdwatcher/tests/test_coordinatearrays.py +++ b/birdwatcher/tests/test_coordinatearrays.py @@ -91,6 +91,12 @@ def test_open_archived(self): 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): From 556762f8312f3222d1a49f490ba027f7097e5ab0 Mon Sep 17 00:00:00 2001 From: Carien Mol Date: Thu, 28 Mar 2024 14:11:59 +0100 Subject: [PATCH 6/6] some small changes in tests for movementdetection --- birdwatcher/tests/test_movementdetection.py | 31 +++++++++------------ 1 file changed, 13 insertions(+), 18 deletions(-) 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