File size: 4,234 Bytes
c96678c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import os, sys
import unittest
from pathlib import Path
import pandas as pd

sys.path.insert(0, os.path.abspath("."))

########### MODIFY SESSION SETTINGS BEFORE GLOBAL CONFIG IS IMPORTED #########
from acodet.create_session_file import create_session_file

create_session_file()
import json

with open("acodet/src/tmp_session.json", "r") as f:
    session = json.load(f)
session["sound_files_source"] = "tests/test_files/test_audio_files"
session[
    "generated_annotation_source"
] = "tests/test_files/test_generated_annotations"
session[
    "annotation_destination"
] = "tests/test_files/test_combined_annotations"
session[
    "generated_annotations_folder"
] = "tests/test_files/test_generated_annotations"

session[
    "reviewed_annotation_source"
] = "tests/test_files/test_generated_annotations"
session["tfrecords_destination_folder"] = "tests/test_files/test_tfrecords"

with open("acodet/src/tmp_session.json", "w") as f:
    json.dump(session, f)
##############################################################################


from acodet.annotate import run_annotation, filter_annots_by_thresh
from acodet.funcs import return_windowed_file, get_train_set_size
from acodet.models import GoogleMod
from acodet.combine_annotations import generate_final_annotations
from acodet.tfrec import write_tfrec_dataset
from acodet.train import run_training
from acodet import global_config as conf



class TestDetection(unittest.TestCase):
    def test_annotation(self):
        self.time_stamp = run_annotation()
        df = pd.read_csv(
            (
                Path(conf.GEN_ANNOTS_DIR)
                .joinpath(self.time_stamp)
                .joinpath("stats.csv")
            )
        )
        self.assertEqual(
            df["number of predictions with thresh>0.8"][0],
            326,
            "Number of predictions is not what it should be.",
        )

        filter_annots_by_thresh(self.time_stamp)
        file = list(
            Path(conf.GEN_ANNOT_SRC)
            .joinpath(self.time_stamp)
            .joinpath(f"thresh_{conf.THRESH}")
            .glob("**/*.txt")
        )[0]
        df = pd.read_csv(file)
        self.assertEqual(
            len(df),
            309,
            "Number of predictions from filtered thresholds " "is incorrect.",
        )


class TestTraining(unittest.TestCase):
    def test_model_load(self):
        model = GoogleMod(load_g_ckpt=False).model
        self.assertGreater(len(model.layers), 15)

    # def test_tfrecord_loading(self):
    #     data_dir = list(Path(conf.TFREC_DESTINATION).iterdir())
    #     n_train, n_noise = get_train_set_size(data_dir)
    #     self.assertEqual(n_train, 517)
    #     self.assertEqual(n_noise, 42)

class TestTFRecordCreation(unittest.TestCase):
    def test_tfrecord(self):
        time_stamp = list(Path(conf.ANNOT_DEST).iterdir())[-1]
        write_tfrec_dataset(annot_dir=time_stamp, active_learning=False)
        metadata_file_path = Path(conf.TFREC_DESTINATION).joinpath(
            "dataset_meta_train.json"
        )
        self.assertEqual(
            metadata_file_path.exists(),
            1,
            "TFRecords metadata file was not created.",
        )

        with open(metadata_file_path, "r") as f:
            data = json.load(f)
            self.assertEqual(
                data["dataset"]["size"]["train"],
                517,
                "TFRecords files has wrong number of datapoints.",
            )

    def test_combined_annotation(self):
        generate_final_annotations(active_learning=False)
        time_stamp = list(Path(conf.GEN_ANNOTS_DIR).iterdir())[-1].stem
        combined_annots_path = (
            Path(conf.ANNOT_DEST)
            .joinpath(time_stamp)
            .joinpath("combined_annotations.csv")
        )
        self.assertEqual(
            combined_annots_path.exists(),
            1,
            "csv file containing combined_annotations does not exist.",
        )
        df = pd.read_csv(combined_annots_path)
        self.assertEqual(
            df.start.iloc[-1],
            1795.2825,
            "The annotations in combined_annotations.csv don't seem to be identical",
        )


if __name__ == "__main__":
    unittest.main()